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 = malloc(sizeof (*zcp));
58 memset(zcp, 0, sizeof (*zcp));
60 zcp->syslog_facility = LOG_DAEMON;
61 zcp->min_events = ZED_MIN_EVENTS;
62 zcp->max_events = ZED_MAX_EVENTS;
63 zcp->scripts = NULL; /* created via zed_conf_scan_dir() */
64 zcp->state_fd = -1; /* opened via zed_conf_open_state() */
65 zcp->zfs_hdl = NULL; /* opened via zed_event_init() */
66 zcp->zevent_fd = -1; /* opened via zed_event_init() */
68 if (!(zcp->conf_file = strdup(ZED_CONF_FILE)))
71 if (!(zcp->pid_file = strdup(ZED_PID_FILE)))
74 if (!(zcp->script_dir = strdup(ZED_SCRIPT_DIR)))
77 if (!(zcp->state_file = strdup(ZED_STATE_FILE)))
83 zed_log_die("Failed to create conf: %s", strerror(errno));
88 * 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));
110 free(zcp->conf_file);
116 free(zcp->script_dir);
119 free(zcp->state_file);
122 zed_strings_destroy(zcp->scripts);
128 * Display command-line help and exit.
129 * If [got_err] is 0, output to stdout and exit normally;
130 * otherwise, output to stderr and exit with a failure status.
133 _zed_conf_display_help(const char *prog, int got_err)
135 FILE *fp = got_err ? stderr : stdout;
136 int w1 = 4; /* width of leading whitespace */
137 int w2 = 8; /* width of L-justified option field */
139 fprintf(fp, "Usage: %s [OPTION]...\n", (prog ? prog : "zed"));
141 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-h",
143 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-L",
144 "Display license information.");
145 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-V",
146 "Display version information.");
148 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-v",
150 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-f",
151 "Force daemon to run.");
152 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-F",
153 "Run daemon in the foreground.");
154 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-M",
155 "Lock all pages in memory.");
156 fprintf(fp, "%*c%*s %s\n", w1, 0x20, -w2, "-Z",
160 fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-c FILE",
161 "Read configuration from FILE.", ZED_CONF_FILE);
163 fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-d DIR",
164 "Read enabled scripts from DIR.", ZED_SCRIPT_DIR);
165 fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-p FILE",
166 "Write daemon's PID to FILE.", ZED_PID_FILE);
167 fprintf(fp, "%*c%*s %s [%s]\n", w1, 0x20, -w2, "-s FILE",
168 "Write daemon's state to FILE.", ZED_STATE_FILE);
171 exit(got_err ? EXIT_FAILURE : EXIT_SUCCESS);
175 * Display license information to stdout and exit.
178 _zed_conf_display_license(void)
181 const char *text[] = {
182 "The ZFS Event Daemon (ZED) is distributed under the terms of the",
183 " Common Development and Distribution License (CDDL-1.0)",
184 " <http://opensource.org/licenses/CDDL-1.0>.",
185 "Developed at Lawrence Livermore National Laboratory"
186 " (LLNL-CODE-403049).",
187 "Copyright (C) 2013-2014"
188 " Lawrence Livermore National Security, LLC.",
193 for (pp = text; *pp; pp++)
200 * Display version information to stdout and exit.
203 _zed_conf_display_version(void)
206 ZFS_META_NAME, ZFS_META_VERSION, ZFS_META_RELEASE);
212 * Copy the [path] string to the [resultp] ptr.
213 * If [path] is not an absolute path, prefix it with the current working dir.
214 * If [resultp] is non-null, free its existing string before assignment.
217 _zed_conf_parse_path(char **resultp, const char *path)
221 assert(resultp != NULL);
222 assert(path != NULL);
227 if (path[0] == '/') {
228 *resultp = strdup(path);
229 } else if (!getcwd(buf, sizeof (buf))) {
230 zed_log_die("Failed to get current working dir: %s",
232 } else if (strlcat(buf, "/", sizeof (buf)) >= sizeof (buf)) {
233 zed_log_die("Failed to copy path: %s", strerror(ENAMETOOLONG));
234 } else if (strlcat(buf, path, sizeof (buf)) >= sizeof (buf)) {
235 zed_log_die("Failed to copy path: %s", strerror(ENAMETOOLONG));
237 *resultp = strdup(buf);
240 zed_log_die("Failed to copy path: %s", strerror(ENOMEM));
244 * Parse the command-line options into the configuration [zcp].
247 zed_conf_parse_opts(struct zed_conf *zcp, int argc, char **argv)
249 const char * const opts = ":hLVc:d:p:s:vfFMZ";
252 if (!zcp || !argv || !argv[0])
253 zed_log_die("Failed to parse options: Internal error");
255 opterr = 0; /* suppress default getopt err msgs */
257 while ((opt = getopt(argc, argv, opts)) != -1) {
260 _zed_conf_display_help(argv[0], EXIT_SUCCESS);
263 _zed_conf_display_license();
266 _zed_conf_display_version();
269 _zed_conf_parse_path(&zcp->conf_file, optarg);
272 _zed_conf_parse_path(&zcp->script_dir, optarg);
275 _zed_conf_parse_path(&zcp->pid_file, optarg);
278 _zed_conf_parse_path(&zcp->state_file, optarg);
287 zcp->do_foreground = 1;
298 _zed_conf_display_help(argv[0], EXIT_SUCCESS);
300 fprintf(stderr, "%s: %s '-%c'\n\n", argv[0],
301 "Invalid option", optopt);
302 _zed_conf_display_help(argv[0], EXIT_FAILURE);
309 * Parse the configuration file into the configuration [zcp].
310 * FIXME: Not yet implemented.
313 zed_conf_parse_file(struct zed_conf *zcp)
316 zed_log_die("Failed to parse config: %s", strerror(EINVAL));
320 * Scan the [zcp] script_dir for files to exec based on the event class.
321 * Files must be executable by user, but not writable by group or other.
322 * Dotfiles are ignored.
323 * Return 0 on success with an updated set of scripts,
324 * or -1 on error with errno set.
325 * FIXME: Check if script_dir and all parent dirs are secure.
328 zed_conf_scan_dir(struct zed_conf *zcp)
330 zed_strings_t *scripts;
332 struct dirent *direntp;
333 char pathname[PATH_MAX];
339 zed_log_msg(LOG_ERR, "Failed to scan script dir: %s",
343 scripts = zed_strings_create();
346 zed_log_msg(LOG_WARNING, "Failed to scan dir \"%s\": %s",
347 zcp->script_dir, strerror(errno));
350 dirp = opendir(zcp->script_dir);
352 int errno_bak = errno;
353 zed_log_msg(LOG_WARNING, "Failed to open dir \"%s\": %s",
354 zcp->script_dir, strerror(errno));
355 zed_strings_destroy(scripts);
359 while ((direntp = readdir(dirp))) {
360 if (direntp->d_name[0] == '.')
363 n = snprintf(pathname, sizeof (pathname),
364 "%s/%s", zcp->script_dir, direntp->d_name);
365 if ((n < 0) || (n >= sizeof (pathname))) {
366 zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s",
367 direntp->d_name, strerror(ENAMETOOLONG));
370 if (stat(pathname, &st) < 0) {
371 zed_log_msg(LOG_WARNING, "Failed to stat \"%s\": %s",
372 pathname, strerror(errno));
375 if (!S_ISREG(st.st_mode)) {
376 zed_log_msg(LOG_INFO,
377 "Ignoring \"%s\": not a regular file",
381 if ((st.st_uid != 0) && !zcp->do_force) {
382 zed_log_msg(LOG_NOTICE,
383 "Ignoring \"%s\": not owned by root",
387 if (!(st.st_mode & S_IXUSR)) {
388 zed_log_msg(LOG_INFO,
389 "Ignoring \"%s\": not executable by user",
393 if ((st.st_mode & S_IWGRP) & !zcp->do_force) {
394 zed_log_msg(LOG_NOTICE,
395 "Ignoring \"%s\": writable by group",
399 if ((st.st_mode & S_IWOTH) & !zcp->do_force) {
400 zed_log_msg(LOG_NOTICE,
401 "Ignoring \"%s\": writable by other",
405 if (zed_strings_add(scripts, direntp->d_name) < 0) {
406 zed_log_msg(LOG_WARNING,
407 "Failed to register \"%s\": %s",
408 direntp->d_name, strerror(errno));
412 zed_log_msg(LOG_INFO,
413 "Registered script \"%s\"", direntp->d_name);
415 if (closedir(dirp) < 0) {
416 int errno_bak = errno;
417 zed_log_msg(LOG_WARNING, "Failed to close dir \"%s\": %s",
418 zcp->script_dir, strerror(errno));
419 zed_strings_destroy(scripts);
424 zed_strings_destroy(zcp->scripts);
426 zcp->scripts = scripts;
431 * Write the PID file specified in [zcp].
432 * Return 0 on success, -1 on error.
433 * This must be called after fork()ing to become a daemon (so the correct PID
434 * is recorded), but before daemonization is complete and the parent process
435 * exits (for synchronization with systemd).
436 * FIXME: Only update the PID file after verifying the PID previously stored
437 * in the PID file no longer exists or belongs to a foreign process
438 * in order to ensure the daemon cannot be started more than once.
439 * (This check is currently done by zed_conf_open_state().)
442 zed_conf_write_pid(struct zed_conf *zcp)
444 char dirbuf[PATH_MAX];
445 mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
451 if (!zcp || !zcp->pid_file) {
453 zed_log_msg(LOG_ERR, "Failed to write PID file: %s",
457 n = strlcpy(dirbuf, zcp->pid_file, sizeof (dirbuf));
458 if (n >= sizeof (dirbuf)) {
459 errno = ENAMETOOLONG;
460 zed_log_msg(LOG_WARNING, "Failed to write PID file: %s",
464 p = strrchr(dirbuf, '/');
468 if ((mkdirp(dirbuf, dirmode) < 0) && (errno != EEXIST)) {
469 zed_log_msg(LOG_WARNING,
470 "Failed to create directory \"%s\": %s",
471 dirbuf, strerror(errno));
474 (void) unlink(zcp->pid_file);
478 fp = fopen(zcp->pid_file, "w");
482 zed_log_msg(LOG_WARNING, "Failed to open PID file \"%s\": %s",
483 zcp->pid_file, strerror(errno));
484 } else if (fprintf(fp, "%d\n", (int) getpid()) == EOF) {
485 zed_log_msg(LOG_WARNING, "Failed to write PID file \"%s\": %s",
486 zcp->pid_file, strerror(errno));
487 } else if (fclose(fp) == EOF) {
488 zed_log_msg(LOG_WARNING, "Failed to close PID file \"%s\": %s",
489 zcp->pid_file, strerror(errno));
493 (void) unlink(zcp->pid_file);
498 * Open and lock the [zcp] state_file.
499 * Return 0 on success, -1 on error.
500 * FIXME: If state_file exists, verify ownership & permissions.
501 * FIXME: Move lock to pid_file instead.
504 zed_conf_open_state(struct zed_conf *zcp)
506 char dirbuf[PATH_MAX];
507 mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
512 if (!zcp || !zcp->state_file) {
514 zed_log_msg(LOG_ERR, "Failed to open state file: %s",
518 n = strlcpy(dirbuf, zcp->state_file, sizeof (dirbuf));
519 if (n >= sizeof (dirbuf)) {
520 errno = ENAMETOOLONG;
521 zed_log_msg(LOG_WARNING, "Failed to open state file: %s",
525 p = strrchr(dirbuf, '/');
529 if ((mkdirp(dirbuf, dirmode) < 0) && (errno != EEXIST)) {
530 zed_log_msg(LOG_WARNING,
531 "Failed to create directory \"%s\": %s",
532 dirbuf, strerror(errno));
535 if (zcp->state_fd >= 0) {
536 if (close(zcp->state_fd) < 0) {
537 zed_log_msg(LOG_WARNING,
538 "Failed to close state file \"%s\": %s",
539 zcp->state_file, strerror(errno));
544 (void) unlink(zcp->state_file);
546 zcp->state_fd = open(zcp->state_file,
547 (O_RDWR | O_CREAT), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
548 if (zcp->state_fd < 0) {
549 zed_log_msg(LOG_WARNING, "Failed to open state file \"%s\": %s",
550 zcp->state_file, strerror(errno));
553 rv = zed_file_lock(zcp->state_fd);
555 zed_log_msg(LOG_WARNING, "Failed to lock state file \"%s\": %s",
556 zcp->state_file, strerror(errno));
560 pid_t pid = zed_file_is_locked(zcp->state_fd);
562 zed_log_msg(LOG_WARNING,
563 "Failed to test lock on state file \"%s\"",
565 } else if (pid > 0) {
566 zed_log_msg(LOG_WARNING,
567 "Found PID %d bound to state file \"%s\"",
568 pid, zcp->state_file);
570 zed_log_msg(LOG_WARNING,
571 "Inconsistent lock state on state file \"%s\"",
580 * Read the opened [zcp] state_file to obtain the eid & etime
581 * of the last event processed.
582 * Write the state from the last event to the [eidp] & [etime] args
583 * passed by reference.
584 * Note that etime[] is an array of size 2.
585 * Return 0 on success, -1 on error.
588 zed_conf_read_state(struct zed_conf *zcp, uint64_t *eidp, int64_t etime[])
594 if (!zcp || !eidp || !etime) {
597 "Failed to read state file: %s", strerror(errno));
600 if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t) -1) {
601 zed_log_msg(LOG_WARNING,
602 "Failed to reposition state file offset: %s",
607 iov[0].iov_base = eidp;
608 len += iov[0].iov_len = sizeof (*eidp);
609 iov[1].iov_base = &etime[0];
610 len += iov[1].iov_len = sizeof (etime[0]);
611 iov[2].iov_base = &etime[1];
612 len += iov[2].iov_len = sizeof (etime[1]);
614 n = readv(zcp->state_fd, iov, 3);
618 zed_log_msg(LOG_WARNING,
619 "Failed to read state file \"%s\": %s",
620 zcp->state_file, strerror(errno));
622 } else if (n != len) {
624 zed_log_msg(LOG_WARNING,
625 "Failed to read state file \"%s\": Read %d of %d bytes",
626 zcp->state_file, n, len);
633 * Write the [eid] & [etime] of the last processed event to the opened
635 * Note that etime[] is an array of size 2.
636 * Return 0 on success, -1 on error.
639 zed_conf_write_state(struct zed_conf *zcp, uint64_t eid, int64_t etime[])
648 "Failed to write state file: %s", strerror(errno));
651 if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t) -1) {
652 zed_log_msg(LOG_WARNING,
653 "Failed to reposition state file offset: %s",
658 iov[0].iov_base = &eid;
659 len += iov[0].iov_len = sizeof (eid);
660 iov[1].iov_base = &etime[0];
661 len += iov[1].iov_len = sizeof (etime[0]);
662 iov[2].iov_base = &etime[1];
663 len += iov[2].iov_len = sizeof (etime[1]);
665 n = writev(zcp->state_fd, iov, 3);
667 zed_log_msg(LOG_WARNING,
668 "Failed to write state file \"%s\": %s",
669 zcp->state_file, strerror(errno));
674 zed_log_msg(LOG_WARNING,
675 "Failed to write state file \"%s\": Wrote %d of %d bytes",
676 zcp->state_file, n, len);
679 if (fdatasync(zcp->state_fd) < 0) {
680 zed_log_msg(LOG_WARNING,
681 "Failed to sync state file \"%s\": %s",
682 zcp->state_file, strerror(errno));