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 * XXX: This must be called after fork()ing to become a daemon.
436 zed_conf_write_pid(struct zed_conf *zcp)
438 char dirbuf[PATH_MAX];
439 mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
445 if (!zcp || !zcp->pid_file) {
447 zed_log_msg(LOG_ERR, "Failed to write pid file: %s",
451 n = strlcpy(dirbuf, zcp->pid_file, sizeof (dirbuf));
452 if (n >= sizeof (dirbuf)) {
453 errno = ENAMETOOLONG;
454 zed_log_msg(LOG_WARNING, "Failed to write pid file: %s",
458 p = strrchr(dirbuf, '/');
462 if ((mkdirp(dirbuf, dirmode) < 0) && (errno != EEXIST)) {
463 zed_log_msg(LOG_WARNING,
464 "Failed to create directory \"%s\": %s",
465 dirbuf, strerror(errno));
468 (void) unlink(zcp->pid_file);
472 fp = fopen(zcp->pid_file, "w");
476 zed_log_msg(LOG_WARNING, "Failed to open pid file \"%s\": %s",
477 zcp->pid_file, strerror(errno));
478 } else if (fprintf(fp, "%d\n", (int) getpid()) == EOF) {
479 zed_log_msg(LOG_WARNING, "Failed to write pid file \"%s\": %s",
480 zcp->pid_file, strerror(errno));
481 } else if (fclose(fp) == EOF) {
482 zed_log_msg(LOG_WARNING, "Failed to close pid file \"%s\": %s",
483 zcp->pid_file, strerror(errno));
487 (void) unlink(zcp->pid_file);
492 * Open and lock the [zcp] state_file.
493 * Return 0 on success, -1 on error.
494 * FIXME: If state_file exists, verify ownership & permissions.
495 * FIXME: Move lock to pid_file instead.
498 zed_conf_open_state(struct zed_conf *zcp)
500 char dirbuf[PATH_MAX];
501 mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
506 if (!zcp || !zcp->state_file) {
508 zed_log_msg(LOG_ERR, "Failed to open state file: %s",
512 n = strlcpy(dirbuf, zcp->state_file, sizeof (dirbuf));
513 if (n >= sizeof (dirbuf)) {
514 errno = ENAMETOOLONG;
515 zed_log_msg(LOG_WARNING, "Failed to open state file: %s",
519 p = strrchr(dirbuf, '/');
523 if ((mkdirp(dirbuf, dirmode) < 0) && (errno != EEXIST)) {
524 zed_log_msg(LOG_WARNING,
525 "Failed to create directory \"%s\": %s",
526 dirbuf, strerror(errno));
529 if (zcp->state_fd >= 0) {
530 if (close(zcp->state_fd) < 0) {
531 zed_log_msg(LOG_WARNING,
532 "Failed to close state file \"%s\": %s",
533 zcp->state_file, strerror(errno));
538 (void) unlink(zcp->state_file);
540 zcp->state_fd = open(zcp->state_file,
541 (O_RDWR | O_CREAT), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
542 if (zcp->state_fd < 0) {
543 zed_log_msg(LOG_WARNING, "Failed to open state file \"%s\": %s",
544 zcp->state_file, strerror(errno));
547 rv = zed_file_lock(zcp->state_fd);
549 zed_log_msg(LOG_WARNING, "Failed to lock state file \"%s\": %s",
550 zcp->state_file, strerror(errno));
554 pid_t pid = zed_file_is_locked(zcp->state_fd);
556 zed_log_msg(LOG_WARNING,
557 "Failed to test lock on state file \"%s\"",
559 } else if (pid > 0) {
560 zed_log_msg(LOG_WARNING,
561 "Found pid %d bound to state file \"%s\"",
562 pid, zcp->state_file);
564 zed_log_msg(LOG_WARNING,
565 "Inconsistent lock state on state file \"%s\"",
574 * Read the opened [zcp] state_file to obtain the eid & etime
575 * of the last event processed.
576 * Write the state from the last event to the [eidp] & [etime] args
577 * passed by reference.
578 * Note that etime[] is an array of size 2.
579 * Return 0 on success, -1 on error.
582 zed_conf_read_state(struct zed_conf *zcp, uint64_t *eidp, int64_t etime[])
588 if (!zcp || !eidp || !etime) {
591 "Failed to read state file: %s", strerror(errno));
594 if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t) -1) {
595 zed_log_msg(LOG_WARNING,
596 "Failed to reposition state file offset: %s",
601 iov[0].iov_base = eidp;
602 len += iov[0].iov_len = sizeof (*eidp);
603 iov[1].iov_base = &etime[0];
604 len += iov[1].iov_len = sizeof (etime[0]);
605 iov[2].iov_base = &etime[1];
606 len += iov[2].iov_len = sizeof (etime[1]);
608 n = readv(zcp->state_fd, iov, 3);
612 zed_log_msg(LOG_WARNING,
613 "Failed to read state file \"%s\": %s",
614 zcp->state_file, strerror(errno));
616 } else if (n != len) {
618 zed_log_msg(LOG_WARNING,
619 "Failed to read state file \"%s\": Read %d of %d bytes",
620 zcp->state_file, n, len);
627 * Write the [eid] & [etime] of the last processed event to the opened
629 * Note that etime[] is an array of size 2.
630 * Return 0 on success, -1 on error.
633 zed_conf_write_state(struct zed_conf *zcp, uint64_t eid, int64_t etime[])
642 "Failed to write state file: %s", strerror(errno));
645 if (lseek(zcp->state_fd, 0, SEEK_SET) == (off_t) -1) {
646 zed_log_msg(LOG_WARNING,
647 "Failed to reposition state file offset: %s",
652 iov[0].iov_base = &eid;
653 len += iov[0].iov_len = sizeof (eid);
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]);
659 n = writev(zcp->state_fd, iov, 3);
661 zed_log_msg(LOG_WARNING,
662 "Failed to write state file \"%s\": %s",
663 zcp->state_file, strerror(errno));
668 zed_log_msg(LOG_WARNING,
669 "Failed to write state file \"%s\": Wrote %d of %d bytes",
670 zcp->state_file, n, len);
673 if (fdatasync(zcp->state_fd) < 0) {
674 zed_log_msg(LOG_WARNING,
675 "Failed to sync state file \"%s\": %s",
676 zcp->state_file, strerror(errno));