summaryrefslogtreecommitdiffstats
path: root/cmd/zed/zed_file.c
blob: d73e64976f6507d15f44070b4b0702ac89258984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license from the top-level
 * OPENSOLARIS.LICENSE or <http://opensource.org/licenses/CDDL-1.0>.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each file
 * and include the License file from the top-level OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
 * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
 */

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "zed_log.h"

/*
 * Read up to [n] bytes from [fd] into [buf].
 * Return the number of bytes read, 0 on EOF, or -1 on error.
 */
ssize_t
zed_file_read_n(int fd, void *buf, size_t n)
{
	unsigned char *p;
	size_t n_left;
	ssize_t n_read;

	p = buf;
	n_left = n;
	while (n_left > 0) {
		if ((n_read = read(fd, p, n_left)) < 0) {
			if (errno == EINTR)
				continue;
			else
				return (-1);

		} else if (n_read == 0) {
			break;
		}
		n_left -= n_read;
		p += n_read;
	}
	return (n - n_left);
}

/*
 * Write [n] bytes from [buf] out to [fd].
 * Return the number of bytes written, or -1 on error.
 */
ssize_t
zed_file_write_n(int fd, void *buf, size_t n)
{
	const unsigned char *p;
	size_t n_left;
	ssize_t n_written;

	p = buf;
	n_left = n;
	while (n_left > 0) {
		if ((n_written = write(fd, p, n_left)) < 0) {
			if (errno == EINTR)
				continue;
			else
				return (-1);

		}
		n_left -= n_written;
		p += n_written;
	}
	return (n);
}

/*
 * Set an exclusive advisory lock on the open file descriptor [fd].
 * Return 0 on success, 1 if a conflicting lock is held by another process,
 *   or -1 on error (with errno set).
 */
int
zed_file_lock(int fd)
{
	struct flock lock;

	if (fd < 0) {
		errno = EBADF;
		return (-1);
	}
	lock.l_type = F_WRLCK;
	lock.l_whence = SEEK_SET;
	lock.l_start = 0;
	lock.l_len = 0;

	if (fcntl(fd, F_SETLK, &lock) < 0) {
		if ((errno == EACCES) || (errno == EAGAIN))
			return (1);

		return (-1);
	}
	return (0);
}

/*
 * Release an advisory lock held on the open file descriptor [fd].
 * Return 0 on success, or -1 on error (with errno set).
 */
int
zed_file_unlock(int fd)
{
	struct flock lock;

	if (fd < 0) {
		errno = EBADF;
		return (-1);
	}
	lock.l_type = F_UNLCK;
	lock.l_whence = SEEK_SET;
	lock.l_start = 0;
	lock.l_len = 0;

	if (fcntl(fd, F_SETLK, &lock) < 0)
		return (-1);

	return (0);
}

/*
 * Test whether an exclusive advisory lock could be obtained for the open
 *   file descriptor [fd].
 * Return 0 if the file is not locked, >0 for the pid of another process
 *   holding a conflicting lock, or -1 on error (with errno set).
 */
pid_t
zed_file_is_locked(int fd)
{
	struct flock lock;

	if (fd < 0) {
		errno = EBADF;
		return (-1);
	}
	lock.l_type = F_WRLCK;
	lock.l_whence = SEEK_SET;
	lock.l_start = 0;
	lock.l_len = 0;

	if (fcntl(fd, F_GETLK, &lock) < 0)
		return (-1);

	if (lock.l_type == F_UNLCK)
		return (0);

	return (lock.l_pid);
}

/*
 * Close all open file descriptors greater than or equal to [lowfd].
 * Any errors encountered while closing file descriptors are ignored.
 */
void
zed_file_close_from(int lowfd)
{
	const int maxfd_def = 256;
	int errno_bak;
	struct rlimit rl;
	int maxfd;
	int fd;

	errno_bak = errno;

	if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
		maxfd = maxfd_def;
	} else if (rl.rlim_max == RLIM_INFINITY) {
		maxfd = maxfd_def;
	} else {
		maxfd = rl.rlim_max;
	}
	for (fd = lowfd; fd < maxfd; fd++)
		(void) close(fd);

	errno = errno_bak;
}

/*
 * Set the CLOEXEC flag on file descriptor [fd] so it will be automatically
 *   closed upon successful execution of one of the exec functions.
 * Return 0 on success, or -1 on error.
 * FIXME: No longer needed?
 */
int
zed_file_close_on_exec(int fd)
{
	int flags;

	if (fd < 0) {
		errno = EBADF;
		return (-1);
	}
	flags = fcntl(fd, F_GETFD);
	if (flags == -1)
		return (-1);

	flags |= FD_CLOEXEC;

	if (fcntl(fd, F_SETFD, flags) == -1)
		return (-1);

	return (0);
}

/*
 * Create the directory [dir_name] and any missing parent directories.
 *   Directories will be created with permissions 0755 modified by the umask.
 * Return 0 on success, or -1 on error.
 * FIXME: Deprecate in favor of mkdirp(). (lib/libspl/mkdirp.c)
 */
int
zed_file_create_dirs(const char *dir_name)
{
	struct stat st;
	char dir_buf[PATH_MAX];
	mode_t dir_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
	char *p;

	if ((dir_name == NULL) || (dir_name[0] == '\0')) {
		zed_log_msg(LOG_WARNING,
		    "Failed to create directory: no directory specified");
		errno = EINVAL;
		return (-1);
	}
	if (dir_name[0] != '/') {
		zed_log_msg(LOG_WARNING,
		    "Failed to create directory \"%s\": not absolute path",
		    dir_name);
		errno = EINVAL;
		return (-1);
	}
	/* Check if directory already exists. */
	if (stat(dir_name, &st) == 0) {
		if (S_ISDIR(st.st_mode))
			return (0);

		errno = EEXIST;
		zed_log_msg(LOG_WARNING,
		    "Failed to create directory \"%s\": %s",
		    dir_name, strerror(errno));
		return (-1);
	}
	/* Create copy for modification. */
	if (strlen(dir_name) >= sizeof (dir_buf)) {
		errno = ENAMETOOLONG;
		zed_log_msg(LOG_WARNING,
		    "Failed to create directory \"%s\": %s",
		    dir_name, strerror(errno));
		return (-1);
	}
	strncpy(dir_buf, dir_name, sizeof (dir_buf));

	/* Remove trailing slashes. */
	p = dir_buf + strlen(dir_buf) - 1;
	while ((p > dir_buf) && (*p == '/'))
		*p-- = '\0';

	/* Process directory components starting from the root dir. */
	p = dir_buf;

	while (1) {

		/* Skip over adjacent slashes. */
		while (*p == '/')
			p++;

		/* Advance to the next path component. */
		p = strchr(p, '/');
		if (p != NULL)
			*p = '\0';

		/* Create directory. */
		if (mkdir(dir_buf, dir_mode) < 0) {

			int mkdir_errno = errno;

			if ((mkdir_errno == EEXIST) ||
			    (stat(dir_buf, &st) < 0) ||
			    (!S_ISDIR(st.st_mode))) {
				zed_log_msg(LOG_WARNING,
				    "Failed to create directory \"%s\": %s",
				    dir_buf, strerror(mkdir_errno));
				return (-1);
			}
		}
		if (p == NULL)
			break;

		*p++ = '/';
	}
	return (0);
}