]> granicus.if.org Git - zfs/blob - cmd/zed/zed_conf.c
Use bitwise '&' instead of logical '&&'
[zfs] / cmd / zed / zed_conf.c
1 /*
2  * This file is part of the ZFS Event Daemon (ZED)
3  * for ZFS on Linux (ZoL) <http://zfsonlinux.org/>.
4  * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
5  * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
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.
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.");
158         fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-Z",
159             "Zero state file.");
160         fprintf(fp, "\n");
161 #if 0
162         fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-c FILE",
163             "Read configuration from FILE.", ZED_CONF_FILE);
164 #endif
165         fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-d DIR",
166             "Read enabled ZEDLETs from DIR.", ZED_ZEDLET_DIR);
167         fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-p FILE",
168             "Write daemon's PID to FILE.", ZED_PID_FILE);
169         fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-s FILE",
170             "Write daemon's state to FILE.", ZED_STATE_FILE);
171         fprintf(fp, "\n");
172
173         exit(got_err ? EXIT_FAILURE : EXIT_SUCCESS);
174 }
175
176 /*
177  * Display license information to stdout and exit.
178  */
179 static void
180 _zed_conf_display_license(void)
181 {
182         const char **pp;
183         const char *text[] = {
184             "The ZFS Event Daemon (ZED) is distributed under the terms of the",
185             "  Common Development and Distribution License (CDDL-1.0)",
186             "  <http://opensource.org/licenses/CDDL-1.0>.",
187             "",
188             "Developed at Lawrence Livermore National Laboratory"
189             " (LLNL-CODE-403049).",
190             "",
191             NULL
192         };
193
194         for (pp = text; *pp; pp++)
195                 printf("%s\n", *pp);
196
197         exit(EXIT_SUCCESS);
198 }
199
200 /*
201  * Display version information to stdout and exit.
202  */
203 static void
204 _zed_conf_display_version(void)
205 {
206         printf("%s-%s-%s\n",
207             ZFS_META_NAME, ZFS_META_VERSION, ZFS_META_RELEASE);
208
209         exit(EXIT_SUCCESS);
210 }
211
212 /*
213  * Copy the [path] string to the [resultp] ptr.
214  * If [path] is not an absolute path, prefix it with the current working dir.
215  * If [resultp] is non-null, free its existing string before assignment.
216  */
217 static void
218 _zed_conf_parse_path(char **resultp, const char *path)
219 {
220         char buf[PATH_MAX];
221
222         assert(resultp != NULL);
223         assert(path != NULL);
224
225         if (*resultp)
226                 free(*resultp);
227
228         if (path[0] == '/') {
229                 *resultp = strdup(path);
230         } else if (!getcwd(buf, sizeof (buf))) {
231                 zed_log_die("Failed to get current working dir: %s",
232                     strerror(errno));
233         } else if (strlcat(buf, "/", sizeof (buf)) >= sizeof (buf)) {
234                 zed_log_die("Failed to copy path: %s", strerror(ENAMETOOLONG));
235         } else if (strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) {
236                 zed_log_die("Failed to copy path: %s", strerror(ENAMETOOLONG));
237         } else {
238                 *resultp = strdup(buf);
239         }
240         if (!*resultp)
241                 zed_log_die("Failed to copy path: %s", strerror(ENOMEM));
242 }
243
244 /*
245  * Parse the command-line options into the configuration [zcp].
246  */
247 void
248 zed_conf_parse_opts(struct zed_conf *zcp, int argc, char **argv)
249 {
250         const char * const opts = ":hLVc:d:p:s:vfFMZ";
251         int opt;
252
253         if (!zcp || !argv || !argv[0])
254                 zed_log_die("Failed to parse options: Internal error");
255
256         opterr = 0;                     /* suppress default getopt err msgs */
257
258         while ((opt = getopt(argc, argv, opts)) != -1) {
259                 switch (opt) {
260                 case 'h':
261                         _zed_conf_display_help(argv[0], EXIT_SUCCESS);
262                         break;
263                 case 'L':
264                         _zed_conf_display_license();
265                         break;
266                 case 'V':
267                         _zed_conf_display_version();
268                         break;
269                 case 'c':
270                         _zed_conf_parse_path(&zcp->conf_file, optarg);
271                         break;
272                 case 'd':
273                         _zed_conf_parse_path(&zcp->zedlet_dir, optarg);
274                         break;
275                 case 'p':
276                         _zed_conf_parse_path(&zcp->pid_file, optarg);
277                         break;
278                 case 's':
279                         _zed_conf_parse_path(&zcp->state_file, optarg);
280                         break;
281                 case 'v':
282                         zcp->do_verbose = 1;
283                         break;
284                 case 'f':
285                         zcp->do_force = 1;
286                         break;
287                 case 'F':
288                         zcp->do_foreground = 1;
289                         break;
290                 case 'M':
291                         zcp->do_memlock = 1;
292                         break;
293                 case 'Z':
294                         zcp->do_zero = 1;
295                         break;
296                 case '?':
297                 default:
298                         if (optopt == '?')
299                                 _zed_conf_display_help(argv[0], EXIT_SUCCESS);
300
301                         fprintf(stderr, "%s: %s '-%c'\n\n", argv[0],
302                             "Invalid option", optopt);
303                         _zed_conf_display_help(argv[0], EXIT_FAILURE);
304                         break;
305                 }
306         }
307 }
308
309 /*
310  * Parse the configuration file into the configuration [zcp].
311  *
312  * FIXME: Not yet implemented.
313  */
314 void
315 zed_conf_parse_file(struct zed_conf *zcp)
316 {
317         if (!zcp)
318                 zed_log_die("Failed to parse config: %s", strerror(EINVAL));
319 }
320
321 /*
322  * Scan the [zcp] zedlet_dir for files to exec based on the event class.
323  * Files must be executable by user, but not writable by group or other.
324  * Dotfiles are ignored.
325  *
326  * Return 0 on success with an updated set of zedlets,
327  * or -1 on error with errno set.
328  *
329  * FIXME: Check if zedlet_dir and all parent dirs are secure.
330  */
331 int
332 zed_conf_scan_dir(struct zed_conf *zcp)
333 {
334         zed_strings_t *zedlets;
335         DIR *dirp;
336         struct dirent *direntp;
337         char pathname[PATH_MAX];
338         struct stat st;
339         int n;
340
341         if (!zcp) {
342                 errno = EINVAL;
343                 zed_log_msg(LOG_ERR, "Failed to scan zedlet dir: %s",
344                     strerror(errno));
345                 return (-1);
346         }
347         zedlets = zed_strings_create();
348         if (!zedlets) {
349                 errno = ENOMEM;
350                 zed_log_msg(LOG_WARNING, "Failed to scan dir \"%s\": %s",
351                     zcp->zedlet_dir, strerror(errno));
352                 return (-1);
353         }
354         dirp = opendir(zcp->zedlet_dir);
355         if (!dirp) {
356                 int errno_bak = errno;
357                 zed_log_msg(LOG_WARNING, "Failed to open dir \"%s\": %s",
358                     zcp->zedlet_dir, strerror(errno));
359                 zed_strings_destroy(zedlets);
360                 errno = errno_bak;
361                 return (-1);
362         }
363         while ((direntp = readdir(dirp))) {
364                 if (direntp->d_name[0] == '.')
365                         continue;
366
367                 n = snprintf(pathname, sizeof (pathname),
368                     "%s/%s", zcp->zedlet_dir, direntp->d_name);
369                 if ((n < 0) || (n >= sizeof (pathname))) {
370                         zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s",
371                             direntp->d_name, strerror(ENAMETOOLONG));
372                         continue;
373                 }
374                 if (stat(pathname, &st) < 0) {
375                         zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s",
376                             pathname, strerror(errno));
377                         continue;
378                 }
379                 if (!S_ISREG(st.st_mode)) {
380                         zed_log_msg(LOG_INFO,
381                             "Ignoring \"%s\": not a regular file",
382                             direntp->d_name);
383                         continue;
384                 }
385                 if ((st.st_uid != 0) && !zcp->do_force) {
386                         zed_log_msg(LOG_NOTICE,
387                             "Ignoring \"%s\": not owned by root",
388                             direntp->d_name);
389                         continue;
390                 }
391                 if (!(st.st_mode & S_IXUSR)) {
392                         zed_log_msg(LOG_INFO,
393                             "Ignoring \"%s\": not executable by user",
394                             direntp->d_name);
395                         continue;
396                 }
397                 if ((st.st_mode & S_IWGRP) && !zcp->do_force) {
398                         zed_log_msg(LOG_NOTICE,
399                             "Ignoring \"%s\": writable by group",
400                             direntp->d_name);
401                         continue;
402                 }
403                 if ((st.st_mode & S_IWOTH) && !zcp->do_force) {
404                         zed_log_msg(LOG_NOTICE,
405                             "Ignoring \"%s\": writable by other",
406                             direntp->d_name);
407                         continue;
408                 }
409                 if (zed_strings_add(zedlets, NULL, direntp->d_name) < 0) {
410                         zed_log_msg(LOG_WARNING,
411                             "Failed to register \"%s\": %s",
412                             direntp->d_name, strerror(errno));
413                         continue;
414                 }
415                 if (zcp->do_verbose)
416                         zed_log_msg(LOG_INFO,
417                             "Registered zedlet \"%s\"", direntp->d_name);
418         }
419         if (closedir(dirp) < 0) {
420                 int errno_bak = errno;
421                 zed_log_msg(LOG_WARNING, "Failed to close dir \"%s\": %s",
422                     zcp->zedlet_dir, strerror(errno));
423                 zed_strings_destroy(zedlets);
424                 errno = errno_bak;
425                 return (-1);
426         }
427         if (zcp->zedlets)
428                 zed_strings_destroy(zcp->zedlets);
429
430         zcp->zedlets = zedlets;
431         return (0);
432 }
433
434 /*
435  * Write the PID file specified in [zcp].
436  * Return 0 on success, -1 on error.
437  *
438  * This must be called after fork()ing to become a daemon (so the correct PID
439  * is recorded), but before daemonization is complete and the parent process
440  * exits (for synchronization with systemd).
441  */
442 int
443 zed_conf_write_pid(struct zed_conf *zcp)
444 {
445         const mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
446         const mode_t filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
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, dirmode) < 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), filemode);
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 (zed_file_write_n(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
546 zed_conf_open_state(struct zed_conf *zcp)
547 {
548         char dirbuf[PATH_MAX];
549         mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
550         int n;
551         char *p;
552         int rv;
553
554         if (!zcp || !zcp->state_file) {
555                 errno = EINVAL;
556                 zed_log_msg(LOG_ERR, "Failed to open state file: %s",
557                     strerror(errno));
558                 return (-1);
559         }
560         n = strlcpy(dirbuf, zcp->state_file, sizeof (dirbuf));
561         if (n >= sizeof (dirbuf)) {
562                 errno = ENAMETOOLONG;
563                 zed_log_msg(LOG_WARNING, "Failed to open state file: %s",
564                     strerror(errno));
565                 return (-1);
566         }
567         p = strrchr(dirbuf, '/');
568         if (p)
569                 *p = '\0';
570
571         if ((mkdirp(dirbuf, dirmode) < 0) && (errno != EEXIST)) {
572                 zed_log_msg(LOG_WARNING,
573                     "Failed to create directory \"%s\": %s",
574                     dirbuf, strerror(errno));
575                 return (-1);
576         }
577         if (zcp->state_fd >= 0) {
578                 if (close(zcp->state_fd) < 0) {
579                         zed_log_msg(LOG_WARNING,
580                             "Failed to close state file \"%s\": %s",
581                             zcp->state_file, strerror(errno));
582                         return (-1);
583                 }
584         }
585         if (zcp->do_zero)
586                 (void) unlink(zcp->state_file);
587
588         zcp->state_fd = open(zcp->state_file,
589             (O_RDWR | O_CREAT), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
590         if (zcp->state_fd < 0) {
591                 zed_log_msg(LOG_WARNING, "Failed to open state file \"%s\": %s",
592                     zcp->state_file, strerror(errno));
593                 return (-1);
594         }
595         rv = zed_file_lock(zcp->state_fd);
596         if (rv < 0) {
597                 zed_log_msg(LOG_WARNING, "Failed to lock state file \"%s\": %s",
598                     zcp->state_file, strerror(errno));
599                 return (-1);
600         }
601         if (rv > 0) {
602                 pid_t pid = zed_file_is_locked(zcp->state_fd);
603                 if (pid < 0) {
604                         zed_log_msg(LOG_WARNING,
605                             "Failed to test lock on state file \"%s\"",
606                             zcp->state_file);
607                 } else if (pid > 0) {
608                         zed_log_msg(LOG_WARNING,
609                             "Found PID %d bound to state file \"%s\"",
610                             pid, zcp->state_file);
611                 } else {
612                         zed_log_msg(LOG_WARNING,
613                             "Inconsistent lock state on state file \"%s\"",
614                             zcp->state_file);
615                 }
616                 return (-1);
617         }
618         return (0);
619 }
620
621 /*
622  * Read the opened [zcp] state_file to obtain the eid & etime of the last event
623  * processed.  Write the state from the last event to the [eidp] & [etime] args
624  * passed by reference.  Note that etime[] is an array of size 2.
625  * Return 0 on success, -1 on error.
626  */
627 int
628 zed_conf_read_state(struct zed_conf *zcp, uint64_t *eidp, int64_t etime[])
629 {
630         ssize_t len;
631         struct iovec iov[3];
632         ssize_t n;
633
634         if (!zcp || !eidp || !etime) {
635                 errno = EINVAL;
636                 zed_log_msg(LOG_ERR,
637                     "Failed to read state file: %s", strerror(errno));
638                 return (-1);
639         }
640         if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t)-1) {
641                 zed_log_msg(LOG_WARNING,
642                     "Failed to reposition state file offset: %s",
643                     strerror(errno));
644                 return (-1);
645         }
646         len = 0;
647         iov[0].iov_base = eidp;
648         len += iov[0].iov_len = sizeof (*eidp);
649         iov[1].iov_base = &etime[0];
650         len += iov[1].iov_len = sizeof (etime[0]);
651         iov[2].iov_base = &etime[1];
652         len += iov[2].iov_len = sizeof (etime[1]);
653
654         n = readv(zcp->state_fd, iov, 3);
655         if (n == 0) {
656                 *eidp = 0;
657         } else if (n < 0) {
658                 zed_log_msg(LOG_WARNING,
659                     "Failed to read state file \"%s\": %s",
660                     zcp->state_file, strerror(errno));
661                 return (-1);
662         } else if (n != len) {
663                 errno = EIO;
664                 zed_log_msg(LOG_WARNING,
665                     "Failed to read state file \"%s\": Read %d of %d bytes",
666                     zcp->state_file, n, len);
667                 return (-1);
668         }
669         return (0);
670 }
671
672 /*
673  * Write the [eid] & [etime] of the last processed event to the opened
674  * [zcp] state_file.  Note that etime[] is an array of size 2.
675  * Return 0 on success, -1 on error.
676  */
677 int
678 zed_conf_write_state(struct zed_conf *zcp, uint64_t eid, int64_t etime[])
679 {
680         ssize_t len;
681         struct iovec iov[3];
682         ssize_t n;
683
684         if (!zcp) {
685                 errno = EINVAL;
686                 zed_log_msg(LOG_ERR,
687                     "Failed to write state file: %s", strerror(errno));
688                 return (-1);
689         }
690         if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t)-1) {
691                 zed_log_msg(LOG_WARNING,
692                     "Failed to reposition state file offset: %s",
693                     strerror(errno));
694                 return (-1);
695         }
696         len = 0;
697         iov[0].iov_base = &eid;
698         len += iov[0].iov_len = sizeof (eid);
699         iov[1].iov_base = &etime[0];
700         len += iov[1].iov_len = sizeof (etime[0]);
701         iov[2].iov_base = &etime[1];
702         len += iov[2].iov_len = sizeof (etime[1]);
703
704         n = writev(zcp->state_fd, iov, 3);
705         if (n < 0) {
706                 zed_log_msg(LOG_WARNING,
707                     "Failed to write state file \"%s\": %s",
708                     zcp->state_file, strerror(errno));
709                 return (-1);
710         }
711         if (n != len) {
712                 errno = EIO;
713                 zed_log_msg(LOG_WARNING,
714                     "Failed to write state file \"%s\": Wrote %d of %d bytes",
715                     zcp->state_file, n, len);
716                 return (-1);
717         }
718         if (fdatasync(zcp->state_fd) < 0) {
719                 zed_log_msg(LOG_WARNING,
720                     "Failed to sync state file \"%s\": %s",
721                     zcp->state_file, strerror(errno));
722                 return (-1);
723         }
724         return (0);
725 }