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