]>
Commit | Line | Data |
---|---|---|
ea04106b | 1 | /* |
e10b0808 AX |
2 | * This file is part of the ZFS Event Daemon (ZED) |
3 | * for ZFS on Linux (ZoL) <http://zfsonlinux.org/>. | |
ea04106b AX |
4 | * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049). |
5 | * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC. | |
e10b0808 AX |
6 | * Refer to the ZoL git commit log for authoritative copyright attribution. |
7 | * | |
8 | * The contents of this file are subject to the terms of the | |
9 | * Common Development and Distribution License Version 1.0 (CDDL-1.0). | |
10 | * You can obtain a copy of the license from the top-level file | |
11 | * "OPENSOLARIS.LICENSE" or at <http://opensource.org/licenses/CDDL-1.0>. | |
12 | * You may not use this file except in compliance with the license. | |
ea04106b AX |
13 | */ |
14 | ||
15 | #include <assert.h> | |
16 | #include <ctype.h> | |
17 | #include <dirent.h> | |
18 | #include <errno.h> | |
19 | #include <fcntl.h> | |
20 | #include <libgen.h> | |
21 | #include <limits.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/stat.h> | |
26 | #include <sys/uio.h> | |
27 | #include <unistd.h> | |
28 | #include "zed.h" | |
29 | #include "zed_conf.h" | |
30 | #include "zed_file.h" | |
31 | #include "zed_log.h" | |
32 | #include "zed_strings.h" | |
33 | ||
34 | /* | |
35 | * Return a new configuration with default values. | |
36 | */ | |
37 | struct zed_conf * | |
38 | zed_conf_create(void) | |
39 | { | |
40 | struct zed_conf *zcp; | |
41 | ||
42 | zcp = calloc(1, sizeof (*zcp)); | |
43 | if (!zcp) | |
44 | goto nomem; | |
45 | ||
46 | zcp->syslog_facility = LOG_DAEMON; | |
47 | zcp->min_events = ZED_MIN_EVENTS; | |
48 | zcp->max_events = ZED_MAX_EVENTS; | |
49 | zcp->pid_fd = -1; | |
50 | zcp->zedlets = NULL; /* created via zed_conf_scan_dir() */ | |
51 | zcp->state_fd = -1; /* opened via zed_conf_open_state() */ | |
52 | zcp->zfs_hdl = NULL; /* opened via zed_event_init() */ | |
53 | zcp->zevent_fd = -1; /* opened via zed_event_init() */ | |
54 | ||
55 | if (!(zcp->conf_file = strdup(ZED_CONF_FILE))) | |
56 | goto nomem; | |
57 | ||
58 | if (!(zcp->pid_file = strdup(ZED_PID_FILE))) | |
59 | goto nomem; | |
60 | ||
61 | if (!(zcp->zedlet_dir = strdup(ZED_ZEDLET_DIR))) | |
62 | goto nomem; | |
63 | ||
64 | if (!(zcp->state_file = strdup(ZED_STATE_FILE))) | |
65 | goto nomem; | |
66 | ||
67 | return (zcp); | |
68 | ||
69 | nomem: | |
70 | zed_log_die("Failed to create conf: %s", strerror(errno)); | |
71 | return (NULL); | |
72 | } | |
73 | ||
74 | /* | |
75 | * Destroy the configuration [zcp]. | |
76 | * | |
77 | * Note: zfs_hdl & zevent_fd are destroyed via zed_event_fini(). | |
78 | */ | |
79 | void | |
80 | zed_conf_destroy(struct zed_conf *zcp) | |
81 | { | |
82 | if (!zcp) | |
83 | return; | |
84 | ||
85 | if (zcp->state_fd >= 0) { | |
86 | if (close(zcp->state_fd) < 0) | |
87 | zed_log_msg(LOG_WARNING, | |
88 | "Failed to close state file \"%s\": %s", | |
89 | zcp->state_file, strerror(errno)); | |
90 | zcp->state_fd = -1; | |
91 | } | |
92 | if (zcp->pid_file) { | |
93 | if ((unlink(zcp->pid_file) < 0) && (errno != ENOENT)) | |
94 | zed_log_msg(LOG_WARNING, | |
95 | "Failed to remove PID file \"%s\": %s", | |
96 | zcp->pid_file, strerror(errno)); | |
97 | } | |
98 | if (zcp->pid_fd >= 0) { | |
99 | if (close(zcp->pid_fd) < 0) | |
100 | zed_log_msg(LOG_WARNING, | |
101 | "Failed to close PID file \"%s\": %s", | |
102 | zcp->pid_file, strerror(errno)); | |
103 | zcp->pid_fd = -1; | |
104 | } | |
105 | if (zcp->conf_file) { | |
106 | free(zcp->conf_file); | |
107 | zcp->conf_file = NULL; | |
108 | } | |
109 | if (zcp->pid_file) { | |
110 | free(zcp->pid_file); | |
111 | zcp->pid_file = NULL; | |
112 | } | |
113 | if (zcp->zedlet_dir) { | |
114 | free(zcp->zedlet_dir); | |
115 | zcp->zedlet_dir = NULL; | |
116 | } | |
117 | if (zcp->state_file) { | |
118 | free(zcp->state_file); | |
119 | zcp->state_file = NULL; | |
120 | } | |
121 | if (zcp->zedlets) { | |
122 | zed_strings_destroy(zcp->zedlets); | |
123 | zcp->zedlets = NULL; | |
124 | } | |
125 | free(zcp); | |
126 | } | |
127 | ||
128 | /* | |
129 | * Display command-line help and exit. | |
130 | * | |
131 | * If [got_err] is 0, output to stdout and exit normally; | |
132 | * otherwise, output to stderr and exit with a failure status. | |
133 | */ | |
134 | static void | |
135 | _zed_conf_display_help(const char *prog, int got_err) | |
136 | { | |
137 | FILE *fp = got_err ? stderr : stdout; | |
138 | int w1 = 4; /* width of leading whitespace */ | |
139 | int w2 = 8; /* width of L-justified option field */ | |
140 | ||
141 | fprintf(fp, "Usage: %s [OPTION]...\n", (prog ? prog : "zed")); | |
142 | fprintf(fp, "\n"); | |
143 | fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-h", | |
144 | "Display help."); | |
145 | fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-L", | |
146 | "Display license information."); | |
147 | fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-V", | |
148 | "Display version information."); | |
149 | fprintf(fp, "\n"); | |
150 | fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-v", | |
151 | "Be verbose."); | |
152 | fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-f", | |
153 | "Force daemon to run."); | |
154 | fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-F", | |
155 | "Run daemon in the foreground."); | |
156 | fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-M", | |
157 | "Lock all pages in memory."); | |
42f7b73b AX |
158 | fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-P", |
159 | "$PATH for ZED to use (only used by ZTS)."); | |
ea04106b AX |
160 | fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-Z", |
161 | "Zero state file."); | |
162 | fprintf(fp, "\n"); | |
163 | #if 0 | |
164 | fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-c FILE", | |
165 | "Read configuration from FILE.", ZED_CONF_FILE); | |
166 | #endif | |
167 | fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-d DIR", | |
168 | "Read enabled ZEDLETs from DIR.", ZED_ZEDLET_DIR); | |
169 | fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-p FILE", | |
170 | "Write daemon's PID to FILE.", ZED_PID_FILE); | |
171 | fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-s FILE", | |
172 | "Write daemon's state to FILE.", ZED_STATE_FILE); | |
173 | fprintf(fp, "\n"); | |
174 | ||
175 | exit(got_err ? EXIT_FAILURE : EXIT_SUCCESS); | |
176 | } | |
177 | ||
178 | /* | |
179 | * Display license information to stdout and exit. | |
180 | */ | |
181 | static void | |
182 | _zed_conf_display_license(void) | |
183 | { | |
184 | const char **pp; | |
185 | const char *text[] = { | |
186 | "The ZFS Event Daemon (ZED) is distributed under the terms of the", | |
187 | " Common Development and Distribution License (CDDL-1.0)", | |
188 | " <http://opensource.org/licenses/CDDL-1.0>.", | |
e10b0808 | 189 | "", |
ea04106b AX |
190 | "Developed at Lawrence Livermore National Laboratory" |
191 | " (LLNL-CODE-403049).", | |
ea04106b AX |
192 | "", |
193 | NULL | |
194 | }; | |
195 | ||
196 | for (pp = text; *pp; pp++) | |
197 | printf("%s\n", *pp); | |
198 | ||
199 | exit(EXIT_SUCCESS); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Display version information to stdout and exit. | |
204 | */ | |
205 | static void | |
206 | _zed_conf_display_version(void) | |
207 | { | |
208 | printf("%s-%s-%s\n", | |
209 | ZFS_META_NAME, ZFS_META_VERSION, ZFS_META_RELEASE); | |
210 | ||
211 | exit(EXIT_SUCCESS); | |
212 | } | |
213 | ||
214 | /* | |
215 | * Copy the [path] string to the [resultp] ptr. | |
216 | * If [path] is not an absolute path, prefix it with the current working dir. | |
217 | * If [resultp] is non-null, free its existing string before assignment. | |
218 | */ | |
219 | static void | |
220 | _zed_conf_parse_path(char **resultp, const char *path) | |
221 | { | |
222 | char buf[PATH_MAX]; | |
223 | ||
224 | assert(resultp != NULL); | |
225 | assert(path != NULL); | |
226 | ||
227 | if (*resultp) | |
228 | free(*resultp); | |
229 | ||
230 | if (path[0] == '/') { | |
231 | *resultp = strdup(path); | |
232 | } else if (!getcwd(buf, sizeof (buf))) { | |
233 | zed_log_die("Failed to get current working dir: %s", | |
234 | strerror(errno)); | |
235 | } else if (strlcat(buf, "/", sizeof (buf)) >= sizeof (buf)) { | |
236 | zed_log_die("Failed to copy path: %s", strerror(ENAMETOOLONG)); | |
237 | } else if (strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) { | |
238 | zed_log_die("Failed to copy path: %s", strerror(ENAMETOOLONG)); | |
239 | } else { | |
240 | *resultp = strdup(buf); | |
241 | } | |
242 | if (!*resultp) | |
243 | zed_log_die("Failed to copy path: %s", strerror(ENOMEM)); | |
244 | } | |
245 | ||
246 | /* | |
247 | * Parse the command-line options into the configuration [zcp]. | |
248 | */ | |
249 | void | |
250 | zed_conf_parse_opts(struct zed_conf *zcp, int argc, char **argv) | |
251 | { | |
42f7b73b | 252 | const char * const opts = ":hLVc:d:p:P:s:vfFMZ"; |
ea04106b AX |
253 | int opt; |
254 | ||
255 | if (!zcp || !argv || !argv[0]) | |
256 | zed_log_die("Failed to parse options: Internal error"); | |
257 | ||
258 | opterr = 0; /* suppress default getopt err msgs */ | |
259 | ||
260 | while ((opt = getopt(argc, argv, opts)) != -1) { | |
261 | switch (opt) { | |
262 | case 'h': | |
263 | _zed_conf_display_help(argv[0], EXIT_SUCCESS); | |
264 | break; | |
265 | case 'L': | |
266 | _zed_conf_display_license(); | |
267 | break; | |
268 | case 'V': | |
269 | _zed_conf_display_version(); | |
270 | break; | |
271 | case 'c': | |
272 | _zed_conf_parse_path(&zcp->conf_file, optarg); | |
273 | break; | |
274 | case 'd': | |
275 | _zed_conf_parse_path(&zcp->zedlet_dir, optarg); | |
276 | break; | |
277 | case 'p': | |
278 | _zed_conf_parse_path(&zcp->pid_file, optarg); | |
279 | break; | |
42f7b73b AX |
280 | case 'P': |
281 | _zed_conf_parse_path(&zcp->path, optarg); | |
282 | break; | |
ea04106b AX |
283 | case 's': |
284 | _zed_conf_parse_path(&zcp->state_file, optarg); | |
285 | break; | |
286 | case 'v': | |
287 | zcp->do_verbose = 1; | |
288 | break; | |
289 | case 'f': | |
290 | zcp->do_force = 1; | |
291 | break; | |
292 | case 'F': | |
293 | zcp->do_foreground = 1; | |
294 | break; | |
295 | case 'M': | |
296 | zcp->do_memlock = 1; | |
297 | break; | |
298 | case 'Z': | |
299 | zcp->do_zero = 1; | |
300 | break; | |
301 | case '?': | |
302 | default: | |
303 | if (optopt == '?') | |
304 | _zed_conf_display_help(argv[0], EXIT_SUCCESS); | |
305 | ||
306 | fprintf(stderr, "%s: %s '-%c'\n\n", argv[0], | |
307 | "Invalid option", optopt); | |
308 | _zed_conf_display_help(argv[0], EXIT_FAILURE); | |
309 | break; | |
310 | } | |
311 | } | |
312 | } | |
313 | ||
314 | /* | |
315 | * Parse the configuration file into the configuration [zcp]. | |
316 | * | |
317 | * FIXME: Not yet implemented. | |
318 | */ | |
319 | void | |
320 | zed_conf_parse_file(struct zed_conf *zcp) | |
321 | { | |
322 | if (!zcp) | |
323 | zed_log_die("Failed to parse config: %s", strerror(EINVAL)); | |
324 | } | |
325 | ||
326 | /* | |
327 | * Scan the [zcp] zedlet_dir for files to exec based on the event class. | |
328 | * Files must be executable by user, but not writable by group or other. | |
329 | * Dotfiles are ignored. | |
330 | * | |
331 | * Return 0 on success with an updated set of zedlets, | |
332 | * or -1 on error with errno set. | |
333 | * | |
334 | * FIXME: Check if zedlet_dir and all parent dirs are secure. | |
335 | */ | |
336 | int | |
337 | zed_conf_scan_dir(struct zed_conf *zcp) | |
338 | { | |
339 | zed_strings_t *zedlets; | |
340 | DIR *dirp; | |
341 | struct dirent *direntp; | |
342 | char pathname[PATH_MAX]; | |
343 | struct stat st; | |
344 | int n; | |
345 | ||
346 | if (!zcp) { | |
347 | errno = EINVAL; | |
348 | zed_log_msg(LOG_ERR, "Failed to scan zedlet dir: %s", | |
349 | strerror(errno)); | |
350 | return (-1); | |
351 | } | |
352 | zedlets = zed_strings_create(); | |
353 | if (!zedlets) { | |
354 | errno = ENOMEM; | |
355 | zed_log_msg(LOG_WARNING, "Failed to scan dir \"%s\": %s", | |
356 | zcp->zedlet_dir, strerror(errno)); | |
357 | return (-1); | |
358 | } | |
359 | dirp = opendir(zcp->zedlet_dir); | |
360 | if (!dirp) { | |
361 | int errno_bak = errno; | |
362 | zed_log_msg(LOG_WARNING, "Failed to open dir \"%s\": %s", | |
363 | zcp->zedlet_dir, strerror(errno)); | |
364 | zed_strings_destroy(zedlets); | |
365 | errno = errno_bak; | |
366 | return (-1); | |
367 | } | |
368 | while ((direntp = readdir(dirp))) { | |
369 | if (direntp->d_name[0] == '.') | |
370 | continue; | |
371 | ||
372 | n = snprintf(pathname, sizeof (pathname), | |
373 | "%s/%s", zcp->zedlet_dir, direntp->d_name); | |
374 | if ((n < 0) || (n >= sizeof (pathname))) { | |
375 | zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s", | |
376 | direntp->d_name, strerror(ENAMETOOLONG)); | |
377 | continue; | |
378 | } | |
379 | if (stat(pathname, &st) < 0) { | |
380 | zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s", | |
381 | pathname, strerror(errno)); | |
382 | continue; | |
383 | } | |
384 | if (!S_ISREG(st.st_mode)) { | |
385 | zed_log_msg(LOG_INFO, | |
386 | "Ignoring \"%s\": not a regular file", | |
387 | direntp->d_name); | |
388 | continue; | |
389 | } | |
390 | if ((st.st_uid != 0) && !zcp->do_force) { | |
391 | zed_log_msg(LOG_NOTICE, | |
392 | "Ignoring \"%s\": not owned by root", | |
393 | direntp->d_name); | |
394 | continue; | |
395 | } | |
396 | if (!(st.st_mode & S_IXUSR)) { | |
397 | zed_log_msg(LOG_INFO, | |
398 | "Ignoring \"%s\": not executable by user", | |
399 | direntp->d_name); | |
400 | continue; | |
401 | } | |
8ec27e97 | 402 | if ((st.st_mode & S_IWGRP) && !zcp->do_force) { |
ea04106b AX |
403 | zed_log_msg(LOG_NOTICE, |
404 | "Ignoring \"%s\": writable by group", | |
405 | direntp->d_name); | |
406 | continue; | |
407 | } | |
8ec27e97 | 408 | if ((st.st_mode & S_IWOTH) && !zcp->do_force) { |
ea04106b AX |
409 | zed_log_msg(LOG_NOTICE, |
410 | "Ignoring \"%s\": writable by other", | |
411 | direntp->d_name); | |
412 | continue; | |
413 | } | |
414 | if (zed_strings_add(zedlets, NULL, direntp->d_name) < 0) { | |
415 | zed_log_msg(LOG_WARNING, | |
416 | "Failed to register \"%s\": %s", | |
417 | direntp->d_name, strerror(errno)); | |
418 | continue; | |
419 | } | |
420 | if (zcp->do_verbose) | |
421 | zed_log_msg(LOG_INFO, | |
422 | "Registered zedlet \"%s\"", direntp->d_name); | |
423 | } | |
424 | if (closedir(dirp) < 0) { | |
425 | int errno_bak = errno; | |
426 | zed_log_msg(LOG_WARNING, "Failed to close dir \"%s\": %s", | |
427 | zcp->zedlet_dir, strerror(errno)); | |
428 | zed_strings_destroy(zedlets); | |
429 | errno = errno_bak; | |
430 | return (-1); | |
431 | } | |
432 | if (zcp->zedlets) | |
433 | zed_strings_destroy(zcp->zedlets); | |
434 | ||
435 | zcp->zedlets = zedlets; | |
436 | return (0); | |
437 | } | |
438 | ||
439 | /* | |
440 | * Write the PID file specified in [zcp]. | |
441 | * Return 0 on success, -1 on error. | |
442 | * | |
443 | * This must be called after fork()ing to become a daemon (so the correct PID | |
444 | * is recorded), but before daemonization is complete and the parent process | |
445 | * exits (for synchronization with systemd). | |
446 | */ | |
447 | int | |
448 | zed_conf_write_pid(struct zed_conf *zcp) | |
449 | { | |
450 | const mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; | |
451 | const mode_t filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; | |
452 | char buf[PATH_MAX]; | |
453 | int n; | |
454 | char *p; | |
455 | mode_t mask; | |
456 | int rv; | |
457 | ||
458 | if (!zcp || !zcp->pid_file) { | |
459 | errno = EINVAL; | |
460 | zed_log_msg(LOG_ERR, "Failed to create PID file: %s", | |
461 | strerror(errno)); | |
462 | return (-1); | |
463 | } | |
464 | assert(zcp->pid_fd == -1); | |
465 | /* | |
466 | * Create PID file directory if needed. | |
467 | */ | |
468 | n = strlcpy(buf, zcp->pid_file, sizeof (buf)); | |
469 | if (n >= sizeof (buf)) { | |
470 | errno = ENAMETOOLONG; | |
471 | zed_log_msg(LOG_ERR, "Failed to create PID file: %s", | |
472 | strerror(errno)); | |
473 | goto err; | |
474 | } | |
475 | p = strrchr(buf, '/'); | |
476 | if (p) | |
477 | *p = '\0'; | |
478 | ||
479 | if ((mkdirp(buf, dirmode) < 0) && (errno != EEXIST)) { | |
480 | zed_log_msg(LOG_ERR, "Failed to create directory \"%s\": %s", | |
481 | buf, strerror(errno)); | |
482 | goto err; | |
483 | } | |
484 | /* | |
485 | * Obtain PID file lock. | |
486 | */ | |
487 | mask = umask(0); | |
488 | umask(mask | 022); | |
489 | zcp->pid_fd = open(zcp->pid_file, (O_RDWR | O_CREAT), filemode); | |
490 | umask(mask); | |
491 | if (zcp->pid_fd < 0) { | |
492 | zed_log_msg(LOG_ERR, "Failed to open PID file \"%s\": %s", | |
493 | zcp->pid_file, strerror(errno)); | |
494 | goto err; | |
495 | } | |
496 | rv = zed_file_lock(zcp->pid_fd); | |
497 | if (rv < 0) { | |
498 | zed_log_msg(LOG_ERR, "Failed to lock PID file \"%s\": %s", | |
499 | zcp->pid_file, strerror(errno)); | |
500 | goto err; | |
501 | } else if (rv > 0) { | |
502 | pid_t pid = zed_file_is_locked(zcp->pid_fd); | |
503 | if (pid < 0) { | |
504 | zed_log_msg(LOG_ERR, | |
505 | "Failed to test lock on PID file \"%s\"", | |
506 | zcp->pid_file); | |
507 | } else if (pid > 0) { | |
508 | zed_log_msg(LOG_ERR, | |
509 | "Found PID %d bound to PID file \"%s\"", | |
510 | pid, zcp->pid_file); | |
511 | } else { | |
512 | zed_log_msg(LOG_ERR, | |
513 | "Inconsistent lock state on PID file \"%s\"", | |
514 | zcp->pid_file); | |
515 | } | |
516 | goto err; | |
517 | } | |
518 | /* | |
519 | * Write PID file. | |
520 | */ | |
cae5b340 | 521 | n = snprintf(buf, sizeof (buf), "%d\n", (int)getpid()); |
ea04106b AX |
522 | if ((n < 0) || (n >= sizeof (buf))) { |
523 | errno = ERANGE; | |
524 | zed_log_msg(LOG_ERR, "Failed to write PID file \"%s\": %s", | |
525 | zcp->pid_file, strerror(errno)); | |
526 | } else if (zed_file_write_n(zcp->pid_fd, buf, n) != n) { | |
527 | zed_log_msg(LOG_ERR, "Failed to write PID file \"%s\": %s", | |
528 | zcp->pid_file, strerror(errno)); | |
529 | } else if (fdatasync(zcp->pid_fd) < 0) { | |
530 | zed_log_msg(LOG_ERR, "Failed to sync PID file \"%s\": %s", | |
531 | zcp->pid_file, strerror(errno)); | |
532 | } else { | |
533 | return (0); | |
534 | } | |
535 | ||
536 | err: | |
537 | if (zcp->pid_fd >= 0) { | |
538 | (void) close(zcp->pid_fd); | |
539 | zcp->pid_fd = -1; | |
540 | } | |
541 | return (-1); | |
542 | } | |
543 | ||
544 | /* | |
545 | * Open and lock the [zcp] state_file. | |
546 | * Return 0 on success, -1 on error. | |
547 | * | |
548 | * FIXME: Move state information into kernel. | |
549 | */ | |
550 | int | |
551 | zed_conf_open_state(struct zed_conf *zcp) | |
552 | { | |
553 | char dirbuf[PATH_MAX]; | |
554 | mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; | |
555 | int n; | |
556 | char *p; | |
557 | int rv; | |
558 | ||
559 | if (!zcp || !zcp->state_file) { | |
560 | errno = EINVAL; | |
561 | zed_log_msg(LOG_ERR, "Failed to open state file: %s", | |
562 | strerror(errno)); | |
563 | return (-1); | |
564 | } | |
565 | n = strlcpy(dirbuf, zcp->state_file, sizeof (dirbuf)); | |
566 | if (n >= sizeof (dirbuf)) { | |
567 | errno = ENAMETOOLONG; | |
568 | zed_log_msg(LOG_WARNING, "Failed to open state file: %s", | |
569 | strerror(errno)); | |
570 | return (-1); | |
571 | } | |
572 | p = strrchr(dirbuf, '/'); | |
573 | if (p) | |
574 | *p = '\0'; | |
575 | ||
576 | if ((mkdirp(dirbuf, dirmode) < 0) && (errno != EEXIST)) { | |
577 | zed_log_msg(LOG_WARNING, | |
578 | "Failed to create directory \"%s\": %s", | |
579 | dirbuf, strerror(errno)); | |
580 | return (-1); | |
581 | } | |
582 | if (zcp->state_fd >= 0) { | |
583 | if (close(zcp->state_fd) < 0) { | |
584 | zed_log_msg(LOG_WARNING, | |
585 | "Failed to close state file \"%s\": %s", | |
586 | zcp->state_file, strerror(errno)); | |
587 | return (-1); | |
588 | } | |
589 | } | |
590 | if (zcp->do_zero) | |
591 | (void) unlink(zcp->state_file); | |
592 | ||
593 | zcp->state_fd = open(zcp->state_file, | |
594 | (O_RDWR | O_CREAT), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); | |
595 | if (zcp->state_fd < 0) { | |
596 | zed_log_msg(LOG_WARNING, "Failed to open state file \"%s\": %s", | |
597 | zcp->state_file, strerror(errno)); | |
598 | return (-1); | |
599 | } | |
600 | rv = zed_file_lock(zcp->state_fd); | |
601 | if (rv < 0) { | |
602 | zed_log_msg(LOG_WARNING, "Failed to lock state file \"%s\": %s", | |
603 | zcp->state_file, strerror(errno)); | |
604 | return (-1); | |
605 | } | |
606 | if (rv > 0) { | |
607 | pid_t pid = zed_file_is_locked(zcp->state_fd); | |
608 | if (pid < 0) { | |
609 | zed_log_msg(LOG_WARNING, | |
610 | "Failed to test lock on state file \"%s\"", | |
611 | zcp->state_file); | |
612 | } else if (pid > 0) { | |
613 | zed_log_msg(LOG_WARNING, | |
614 | "Found PID %d bound to state file \"%s\"", | |
615 | pid, zcp->state_file); | |
616 | } else { | |
617 | zed_log_msg(LOG_WARNING, | |
618 | "Inconsistent lock state on state file \"%s\"", | |
619 | zcp->state_file); | |
620 | } | |
621 | return (-1); | |
622 | } | |
623 | return (0); | |
624 | } | |
625 | ||
626 | /* | |
627 | * Read the opened [zcp] state_file to obtain the eid & etime of the last event | |
628 | * processed. Write the state from the last event to the [eidp] & [etime] args | |
629 | * passed by reference. Note that etime[] is an array of size 2. | |
630 | * Return 0 on success, -1 on error. | |
631 | */ | |
632 | int | |
633 | zed_conf_read_state(struct zed_conf *zcp, uint64_t *eidp, int64_t etime[]) | |
634 | { | |
635 | ssize_t len; | |
636 | struct iovec iov[3]; | |
637 | ssize_t n; | |
638 | ||
639 | if (!zcp || !eidp || !etime) { | |
640 | errno = EINVAL; | |
641 | zed_log_msg(LOG_ERR, | |
642 | "Failed to read state file: %s", strerror(errno)); | |
643 | return (-1); | |
644 | } | |
cae5b340 | 645 | if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t)-1) { |
ea04106b AX |
646 | zed_log_msg(LOG_WARNING, |
647 | "Failed to reposition state file offset: %s", | |
648 | strerror(errno)); | |
649 | return (-1); | |
650 | } | |
651 | len = 0; | |
652 | iov[0].iov_base = eidp; | |
653 | len += iov[0].iov_len = sizeof (*eidp); | |
654 | iov[1].iov_base = &etime[0]; | |
655 | len += iov[1].iov_len = sizeof (etime[0]); | |
656 | iov[2].iov_base = &etime[1]; | |
657 | len += iov[2].iov_len = sizeof (etime[1]); | |
658 | ||
659 | n = readv(zcp->state_fd, iov, 3); | |
660 | if (n == 0) { | |
661 | *eidp = 0; | |
662 | } else if (n < 0) { | |
663 | zed_log_msg(LOG_WARNING, | |
664 | "Failed to read state file \"%s\": %s", | |
665 | zcp->state_file, strerror(errno)); | |
666 | return (-1); | |
667 | } else if (n != len) { | |
668 | errno = EIO; | |
669 | zed_log_msg(LOG_WARNING, | |
670 | "Failed to read state file \"%s\": Read %d of %d bytes", | |
671 | zcp->state_file, n, len); | |
672 | return (-1); | |
673 | } | |
674 | return (0); | |
675 | } | |
676 | ||
677 | /* | |
678 | * Write the [eid] & [etime] of the last processed event to the opened | |
679 | * [zcp] state_file. Note that etime[] is an array of size 2. | |
680 | * Return 0 on success, -1 on error. | |
681 | */ | |
682 | int | |
683 | zed_conf_write_state(struct zed_conf *zcp, uint64_t eid, int64_t etime[]) | |
684 | { | |
685 | ssize_t len; | |
686 | struct iovec iov[3]; | |
687 | ssize_t n; | |
688 | ||
689 | if (!zcp) { | |
690 | errno = EINVAL; | |
691 | zed_log_msg(LOG_ERR, | |
692 | "Failed to write state file: %s", strerror(errno)); | |
693 | return (-1); | |
694 | } | |
cae5b340 | 695 | if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t)-1) { |
ea04106b AX |
696 | zed_log_msg(LOG_WARNING, |
697 | "Failed to reposition state file offset: %s", | |
698 | strerror(errno)); | |
699 | return (-1); | |
700 | } | |
701 | len = 0; | |
702 | iov[0].iov_base = &eid; | |
703 | len += iov[0].iov_len = sizeof (eid); | |
704 | iov[1].iov_base = &etime[0]; | |
705 | len += iov[1].iov_len = sizeof (etime[0]); | |
706 | iov[2].iov_base = &etime[1]; | |
707 | len += iov[2].iov_len = sizeof (etime[1]); | |
708 | ||
709 | n = writev(zcp->state_fd, iov, 3); | |
710 | if (n < 0) { | |
711 | zed_log_msg(LOG_WARNING, | |
712 | "Failed to write state file \"%s\": %s", | |
713 | zcp->state_file, strerror(errno)); | |
714 | return (-1); | |
715 | } | |
716 | if (n != len) { | |
717 | errno = EIO; | |
718 | zed_log_msg(LOG_WARNING, | |
719 | "Failed to write state file \"%s\": Wrote %d of %d bytes", | |
720 | zcp->state_file, n, len); | |
721 | return (-1); | |
722 | } | |
723 | if (fdatasync(zcp->state_fd) < 0) { | |
724 | zed_log_msg(LOG_WARNING, | |
725 | "Failed to sync state file \"%s\": %s", | |
726 | zcp->state_file, strerror(errno)); | |
727 | return (-1); | |
728 | } | |
729 | return (0); | |
730 | } |