4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license from the top-level
9 * OPENSOLARIS.LICENSE or <http://opensource.org/licenses/CDDL-1.0>.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each file
14 * and include the License file from the top-level OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
24 * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
44 #include "zed_strings.h"
47 * Return a new configuration with default values.
54 zcp = calloc(1, sizeof (*zcp));
58 zcp->syslog_facility = LOG_DAEMON;
59 zcp->min_events = ZED_MIN_EVENTS;
60 zcp->max_events = ZED_MAX_EVENTS;
62 zcp->zedlets = NULL; /* created via zed_conf_scan_dir() */
63 zcp->state_fd = -1; /* opened via zed_conf_open_state() */
64 zcp->zfs_hdl = NULL; /* opened via zed_event_init() */
65 zcp->zevent_fd = -1; /* opened via zed_event_init() */
67 if (!(zcp->conf_file = strdup(ZED_CONF_FILE)))
70 if (!(zcp->pid_file = strdup(ZED_PID_FILE)))
73 if (!(zcp->zedlet_dir = strdup(ZED_ZEDLET_DIR)))
76 if (!(zcp->state_file = strdup(ZED_STATE_FILE)))
82 zed_log_die("Failed to create conf: %s", strerror(errno));
87 * Destroy the configuration [zcp].
89 * Note: zfs_hdl & zevent_fd are destroyed via zed_event_fini().
92 zed_conf_destroy(struct zed_conf *zcp)
97 if (zcp->state_fd >= 0) {
98 if (close(zcp->state_fd) < 0)
99 zed_log_msg(LOG_WARNING,
100 "Failed to close state file \"%s\": %s",
101 zcp->state_file, strerror(errno));
104 if ((unlink(zcp->pid_file) < 0) && (errno != ENOENT))
105 zed_log_msg(LOG_WARNING,
106 "Failed to remove PID file \"%s\": %s",
107 zcp->pid_file, strerror(errno));
109 if (zcp->pid_fd >= 0) {
110 if (close(zcp->pid_fd) < 0)
111 zed_log_msg(LOG_WARNING,
112 "Failed to close PID file \"%s\": %s",
113 zcp->pid_file, strerror(errno));
117 free(zcp->conf_file);
123 free(zcp->zedlet_dir);
126 free(zcp->state_file);
129 zed_strings_destroy(zcp->zedlets);
135 * Display command-line help and exit.
137 * If [got_err] is 0, output to stdout and exit normally;
138 * otherwise, output to stderr and exit with a failure status.
141 _zed_conf_display_help(const char *prog, int got_err)
143 FILE *fp = got_err ? stderr : stdout;
144 int w1 = 4; /* width of leading whitespace */
145 int w2 = 8; /* width of L-justified option field */
147 fprintf(fp, "Usage: %s [OPTION]...\n", (prog ? prog : "zed"));
149 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-h",
151 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-L",
152 "Display license information.");
153 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-V",
154 "Display version information.");
156 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-v",
158 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-f",
159 "Force daemon to run.");
160 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-F",
161 "Run daemon in the foreground.");
162 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-M",
163 "Lock all pages in memory.");
164 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-Z",
168 fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-c FILE",
169 "Read configuration from FILE.", ZED_CONF_FILE);
171 fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-d DIR",
172 "Read enabled ZEDLETs from DIR.", ZED_ZEDLET_DIR);
173 fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-p FILE",
174 "Write daemon's PID to FILE.", ZED_PID_FILE);
175 fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-s FILE",
176 "Write daemon's state to FILE.", ZED_STATE_FILE);
179 exit(got_err ? EXIT_FAILURE : EXIT_SUCCESS);
183 * Display license information to stdout and exit.
186 _zed_conf_display_license(void)
189 const char *text[] = {
190 "The ZFS Event Daemon (ZED) is distributed under the terms of the",
191 " Common Development and Distribution License (CDDL-1.0)",
192 " <http://opensource.org/licenses/CDDL-1.0>.",
193 "Developed at Lawrence Livermore National Laboratory"
194 " (LLNL-CODE-403049).",
195 "Copyright (C) 2013-2014"
196 " Lawrence Livermore National Security, LLC.",
201 for (pp = text; *pp; pp++)
208 * Display version information to stdout and exit.
211 _zed_conf_display_version(void)
214 ZFS_META_NAME, ZFS_META_VERSION, ZFS_META_RELEASE);
220 * Copy the [path] string to the [resultp] ptr.
221 * If [path] is not an absolute path, prefix it with the current working dir.
222 * If [resultp] is non-null, free its existing string before assignment.
225 _zed_conf_parse_path(char **resultp, const char *path)
229 assert(resultp != NULL);
230 assert(path != NULL);
235 if (path[0] == '/') {
236 *resultp = strdup(path);
237 } else if (!getcwd(buf, sizeof (buf))) {
238 zed_log_die("Failed to get current working dir: %s",
240 } else if (strlcat(buf, "/", sizeof (buf)) >= sizeof (buf)) {
241 zed_log_die("Failed to copy path: %s", strerror(ENAMETOOLONG));
242 } else if (strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) {
243 zed_log_die("Failed to copy path: %s", strerror(ENAMETOOLONG));
245 *resultp = strdup(buf);
248 zed_log_die("Failed to copy path: %s", strerror(ENOMEM));
252 * Parse the command-line options into the configuration [zcp].
255 zed_conf_parse_opts(struct zed_conf *zcp, int argc, char **argv)
257 const char * const opts = ":hLVc:d:p:s:vfFMZ";
260 if (!zcp || !argv || !argv[0])
261 zed_log_die("Failed to parse options: Internal error");
263 opterr = 0; /* suppress default getopt err msgs */
265 while ((opt = getopt(argc, argv, opts)) != -1) {
268 _zed_conf_display_help(argv[0], EXIT_SUCCESS);
271 _zed_conf_display_license();
274 _zed_conf_display_version();
277 _zed_conf_parse_path(&zcp->conf_file, optarg);
280 _zed_conf_parse_path(&zcp->zedlet_dir, optarg);
283 _zed_conf_parse_path(&zcp->pid_file, optarg);
286 _zed_conf_parse_path(&zcp->state_file, optarg);
295 zcp->do_foreground = 1;
306 _zed_conf_display_help(argv[0], EXIT_SUCCESS);
308 fprintf(stderr, "%s: %s '-%c'\n\n", argv[0],
309 "Invalid option", optopt);
310 _zed_conf_display_help(argv[0], EXIT_FAILURE);
317 * Parse the configuration file into the configuration [zcp].
319 * FIXME: Not yet implemented.
322 zed_conf_parse_file(struct zed_conf *zcp)
325 zed_log_die("Failed to parse config: %s", strerror(EINVAL));
329 * Scan the [zcp] zedlet_dir for files to exec based on the event class.
330 * Files must be executable by user, but not writable by group or other.
331 * Dotfiles are ignored.
333 * Return 0 on success with an updated set of zedlets,
334 * or -1 on error with errno set.
336 * FIXME: Check if zedlet_dir and all parent dirs are secure.
339 zed_conf_scan_dir(struct zed_conf *zcp)
341 zed_strings_t *zedlets;
343 struct dirent *direntp;
344 char pathname[PATH_MAX];
350 zed_log_msg(LOG_ERR, "Failed to scan zedlet dir: %s",
354 zedlets = zed_strings_create();
357 zed_log_msg(LOG_WARNING, "Failed to scan dir \"%s\": %s",
358 zcp->zedlet_dir, strerror(errno));
361 dirp = opendir(zcp->zedlet_dir);
363 int errno_bak = errno;
364 zed_log_msg(LOG_WARNING, "Failed to open dir \"%s\": %s",
365 zcp->zedlet_dir, strerror(errno));
366 zed_strings_destroy(zedlets);
370 while ((direntp = readdir(dirp))) {
371 if (direntp->d_name[0] == '.')
374 n = snprintf(pathname, sizeof (pathname),
375 "%s/%s", zcp->zedlet_dir, direntp->d_name);
376 if ((n < 0) || (n >= sizeof (pathname))) {
377 zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s",
378 direntp->d_name, strerror(ENAMETOOLONG));
381 if (stat(pathname, &st) < 0) {
382 zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s",
383 pathname, strerror(errno));
386 if (!S_ISREG(st.st_mode)) {
387 zed_log_msg(LOG_INFO,
388 "Ignoring \"%s\": not a regular file",
392 if ((st.st_uid != 0) && !zcp->do_force) {
393 zed_log_msg(LOG_NOTICE,
394 "Ignoring \"%s\": not owned by root",
398 if (!(st.st_mode & S_IXUSR)) {
399 zed_log_msg(LOG_INFO,
400 "Ignoring \"%s\": not executable by user",
404 if ((st.st_mode & S_IWGRP) & !zcp->do_force) {
405 zed_log_msg(LOG_NOTICE,
406 "Ignoring \"%s\": writable by group",
410 if ((st.st_mode & S_IWOTH) & !zcp->do_force) {
411 zed_log_msg(LOG_NOTICE,
412 "Ignoring \"%s\": writable by other",
416 if (zed_strings_add(zedlets, direntp->d_name) < 0) {
417 zed_log_msg(LOG_WARNING,
418 "Failed to register \"%s\": %s",
419 direntp->d_name, strerror(errno));
423 zed_log_msg(LOG_INFO,
424 "Registered zedlet \"%s\"", direntp->d_name);
426 if (closedir(dirp) < 0) {
427 int errno_bak = errno;
428 zed_log_msg(LOG_WARNING, "Failed to close dir \"%s\": %s",
429 zcp->zedlet_dir, strerror(errno));
430 zed_strings_destroy(zedlets);
435 zed_strings_destroy(zcp->zedlets);
437 zcp->zedlets = zedlets;
442 * Write the PID file specified in [zcp].
443 * Return 0 on success, -1 on error.
445 * This must be called after fork()ing to become a daemon (so the correct PID
446 * is recorded), but before daemonization is complete and the parent process
447 * exits (for synchronization with systemd).
450 zed_conf_write_pid(struct zed_conf *zcp)
452 const mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
453 const mode_t filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
460 if (!zcp || !zcp->pid_file) {
462 zed_log_msg(LOG_ERR, "Failed to create PID file: %s",
466 assert(zcp->pid_fd == -1);
468 * Create PID file directory if needed.
470 n = strlcpy(buf, zcp->pid_file, sizeof (buf));
471 if (n >= sizeof (buf)) {
472 errno = ENAMETOOLONG;
473 zed_log_msg(LOG_ERR, "Failed to create PID file: %s",
477 p = strrchr(buf, '/');
481 if ((mkdirp(buf, dirmode) < 0) && (errno != EEXIST)) {
482 zed_log_msg(LOG_ERR, "Failed to create directory \"%s\": %s",
483 buf, strerror(errno));
487 * Obtain PID file lock.
491 zcp->pid_fd = open(zcp->pid_file, (O_RDWR | O_CREAT), filemode);
493 if (zcp->pid_fd < 0) {
494 zed_log_msg(LOG_ERR, "Failed to open PID file \"%s\": %s",
495 zcp->pid_file, strerror(errno));
498 rv = zed_file_lock(zcp->pid_fd);
500 zed_log_msg(LOG_ERR, "Failed to lock PID file \"%s\": %s",
501 zcp->pid_file, strerror(errno));
504 pid_t pid = zed_file_is_locked(zcp->pid_fd);
507 "Failed to test lock on PID file \"%s\"",
509 } else if (pid > 0) {
511 "Found PID %d bound to PID file \"%s\"",
515 "Inconsistent lock state on PID file \"%s\"",
523 n = snprintf(buf, sizeof (buf), "%d\n", (int) getpid());
524 if ((n < 0) || (n >= sizeof (buf))) {
526 zed_log_msg(LOG_ERR, "Failed to write PID file \"%s\": %s",
527 zcp->pid_file, strerror(errno));
528 } else if (zed_file_write_n(zcp->pid_fd, buf, n) != n) {
529 zed_log_msg(LOG_ERR, "Failed to write PID file \"%s\": %s",
530 zcp->pid_file, strerror(errno));
531 } else if (fdatasync(zcp->pid_fd) < 0) {
532 zed_log_msg(LOG_ERR, "Failed to sync PID file \"%s\": %s",
533 zcp->pid_file, strerror(errno));
539 if (zcp->pid_fd >= 0) {
540 (void) close(zcp->pid_fd);
547 * Open and lock the [zcp] state_file.
548 * Return 0 on success, -1 on error.
550 * FIXME: Move state information into kernel.
553 zed_conf_open_state(struct zed_conf *zcp)
555 char dirbuf[PATH_MAX];
556 mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
561 if (!zcp || !zcp->state_file) {
563 zed_log_msg(LOG_ERR, "Failed to open state file: %s",
567 n = strlcpy(dirbuf, zcp->state_file, sizeof (dirbuf));
568 if (n >= sizeof (dirbuf)) {
569 errno = ENAMETOOLONG;
570 zed_log_msg(LOG_WARNING, "Failed to open state file: %s",
574 p = strrchr(dirbuf, '/');
578 if ((mkdirp(dirbuf, dirmode) < 0) && (errno != EEXIST)) {
579 zed_log_msg(LOG_WARNING,
580 "Failed to create directory \"%s\": %s",
581 dirbuf, strerror(errno));
584 if (zcp->state_fd >= 0) {
585 if (close(zcp->state_fd) < 0) {
586 zed_log_msg(LOG_WARNING,
587 "Failed to close state file \"%s\": %s",
588 zcp->state_file, strerror(errno));
593 (void) unlink(zcp->state_file);
595 zcp->state_fd = open(zcp->state_file,
596 (O_RDWR | O_CREAT), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
597 if (zcp->state_fd < 0) {
598 zed_log_msg(LOG_WARNING, "Failed to open state file \"%s\": %s",
599 zcp->state_file, strerror(errno));
602 rv = zed_file_lock(zcp->state_fd);
604 zed_log_msg(LOG_WARNING, "Failed to lock state file \"%s\": %s",
605 zcp->state_file, strerror(errno));
609 pid_t pid = zed_file_is_locked(zcp->state_fd);
611 zed_log_msg(LOG_WARNING,
612 "Failed to test lock on state file \"%s\"",
614 } else if (pid > 0) {
615 zed_log_msg(LOG_WARNING,
616 "Found PID %d bound to state file \"%s\"",
617 pid, zcp->state_file);
619 zed_log_msg(LOG_WARNING,
620 "Inconsistent lock state on state file \"%s\"",
629 * Read the opened [zcp] state_file to obtain the eid & etime of the last event
630 * processed. Write the state from the last event to the [eidp] & [etime] args
631 * passed by reference. Note that etime[] is an array of size 2.
632 * Return 0 on success, -1 on error.
635 zed_conf_read_state(struct zed_conf *zcp, uint64_t *eidp, int64_t etime[])
641 if (!zcp || !eidp || !etime) {
644 "Failed to read state file: %s", strerror(errno));
647 if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t) -1) {
648 zed_log_msg(LOG_WARNING,
649 "Failed to reposition state file offset: %s",
654 iov[0].iov_base = eidp;
655 len += iov[0].iov_len = sizeof (*eidp);
656 iov[1].iov_base = &etime[0];
657 len += iov[1].iov_len = sizeof (etime[0]);
658 iov[2].iov_base = &etime[1];
659 len += iov[2].iov_len = sizeof (etime[1]);
661 n = readv(zcp->state_fd, iov, 3);
665 zed_log_msg(LOG_WARNING,
666 "Failed to read state file \"%s\": %s",
667 zcp->state_file, strerror(errno));
669 } else if (n != len) {
671 zed_log_msg(LOG_WARNING,
672 "Failed to read state file \"%s\": Read %d of %d bytes",
673 zcp->state_file, n, len);
680 * Write the [eid] & [etime] of the last processed event to the opened
681 * [zcp] state_file. Note that etime[] is an array of size 2.
682 * Return 0 on success, -1 on error.
685 zed_conf_write_state(struct zed_conf *zcp, uint64_t eid, int64_t etime[])
694 "Failed to write state file: %s", strerror(errno));
697 if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t) -1) {
698 zed_log_msg(LOG_WARNING,
699 "Failed to reposition state file offset: %s",
704 iov[0].iov_base = &eid;
705 len += iov[0].iov_len = sizeof (eid);
706 iov[1].iov_base = &etime[0];
707 len += iov[1].iov_len = sizeof (etime[0]);
708 iov[2].iov_base = &etime[1];
709 len += iov[2].iov_len = sizeof (etime[1]);
711 n = writev(zcp->state_fd, iov, 3);
713 zed_log_msg(LOG_WARNING,
714 "Failed to write state file \"%s\": %s",
715 zcp->state_file, strerror(errno));
720 zed_log_msg(LOG_WARNING,
721 "Failed to write state file \"%s\": Wrote %d of %d bytes",
722 zcp->state_file, n, len);
725 if (fdatasync(zcp->state_fd) < 0) {
726 zed_log_msg(LOG_WARNING,
727 "Failed to sync state file \"%s\": %s",
728 zcp->state_file, strerror(errno));