2 * This file is part of the ZFS Event Daemon (ZED).
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.
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.
26 #include "zed_event.h"
30 static volatile sig_atomic_t _got_exit = 0;
31 static volatile sig_atomic_t _got_hup = 0;
34 * Signal handler for SIGINT & SIGTERM.
37 _exit_handler(int signum)
43 * Signal handler for SIGHUP.
46 _hup_handler(int signum)
52 * Register signal handlers.
55 _setup_sig_handlers(void)
59 if (sigemptyset(&sa.sa_mask) < 0)
60 zed_log_die("Failed to initialize sigset");
62 sa.sa_flags = SA_RESTART;
63 sa.sa_handler = SIG_IGN;
65 if (sigaction(SIGPIPE, &sa, NULL) < 0)
66 zed_log_die("Failed to ignore SIGPIPE");
68 sa.sa_handler = _exit_handler;
69 if (sigaction(SIGINT, &sa, NULL) < 0)
70 zed_log_die("Failed to register SIGINT handler");
72 if (sigaction(SIGTERM, &sa, NULL) < 0)
73 zed_log_die("Failed to register SIGTERM handler");
75 sa.sa_handler = _hup_handler;
76 if (sigaction(SIGHUP, &sa, NULL) < 0)
77 zed_log_die("Failed to register SIGHUP handler");
81 * Lock all current and future pages in the virtual memory address space.
82 * Access to locked pages will never be delayed by a page fault.
84 * EAGAIN is tested up to max_tries in case this is a transient error.
86 * Note that memory locks are not inherited by a child created via fork()
87 * and are automatically removed during an execve(). As such, this must
88 * be called after the daemon fork()s (when running in the background).
95 const int max_tries = 10;
97 for (i = 0; i < max_tries; i++) {
98 if (mlockall(MCL_CURRENT | MCL_FUTURE) == 0) {
99 zed_log_msg(LOG_INFO, "Locked all pages in memory");
105 zed_log_die("Failed to lock memory pages: %s", strerror(errno));
107 #else /* HAVE_MLOCKALL */
108 zed_log_die("Failed to lock memory pages: mlockall() not supported");
109 #endif /* HAVE_MLOCKALL */
113 * Start daemonization of the process including the double fork().
115 * The parent process will block here until _finish_daemonize() is called
116 * (in the grandchild process), at which point the parent process will exit.
117 * This prevents the parent process from exiting until initialization is
121 _start_daemonize(void)
126 /* Create pipe for communicating with child during daemonization. */
129 /* Background process and ensure child is not process group leader. */
132 zed_log_die("Failed to create child process: %s",
134 } else if (pid > 0) {
136 /* Close writes since parent will only read from pipe. */
137 zed_log_pipe_close_writes();
139 /* Wait for notification that daemonization is complete. */
142 zed_log_pipe_close_reads();
146 /* Close reads since child will only write to pipe. */
147 zed_log_pipe_close_reads();
149 /* Create independent session and detach from terminal. */
151 zed_log_die("Failed to create new session: %s",
154 /* Prevent child from terminating on HUP when session leader exits. */
155 if (sigemptyset(&sa.sa_mask) < 0)
156 zed_log_die("Failed to initialize sigset");
159 sa.sa_handler = SIG_IGN;
161 if (sigaction(SIGHUP, &sa, NULL) < 0)
162 zed_log_die("Failed to ignore SIGHUP");
164 /* Ensure process cannot re-acquire terminal. */
167 zed_log_die("Failed to create grandchild process: %s",
169 } else if (pid > 0) {
175 * Finish daemonization of the process by closing stdin/stdout/stderr.
177 * This must be called at the end of initialization after all external
178 * communication channels are established and accessible.
181 _finish_daemonize(void)
185 /* Preserve fd 0/1/2, but discard data to/from stdin/stdout/stderr. */
186 devnull = open("/dev/null", O_RDWR);
188 zed_log_die("Failed to open /dev/null: %s", strerror(errno));
190 if (dup2(devnull, STDIN_FILENO) < 0)
191 zed_log_die("Failed to dup /dev/null onto stdin: %s",
194 if (dup2(devnull, STDOUT_FILENO) < 0)
195 zed_log_die("Failed to dup /dev/null onto stdout: %s",
198 if (dup2(devnull, STDERR_FILENO) < 0)
199 zed_log_die("Failed to dup /dev/null onto stderr: %s",
202 if ((devnull > STDERR_FILENO) && (close(devnull) < 0))
203 zed_log_die("Failed to close /dev/null: %s", strerror(errno));
205 /* Notify parent that daemonization is complete. */
206 zed_log_pipe_close_writes();
210 * ZFS Event Daemon (ZED).
213 main(int argc, char *argv[])
215 struct zed_conf *zcp;
217 int64_t saved_etime[2];
219 zed_log_init(argv[0]);
220 zed_log_stderr_open(LOG_NOTICE);
221 zcp = zed_conf_create();
222 zed_conf_parse_opts(zcp, argc, argv);
224 zed_log_stderr_open(LOG_INFO);
227 zed_log_die("Must be run as root");
229 zed_conf_parse_file(zcp);
231 zed_file_close_from(STDERR_FILENO + 1);
236 zed_log_die("Failed to change to root directory");
238 if (zed_conf_scan_dir(zcp) < 0)
241 if (!zcp->do_foreground) {
243 zed_log_syslog_open(LOG_DAEMON);
245 _setup_sig_handlers();
250 if ((zed_conf_write_pid(zcp) < 0) && (!zcp->do_force))
253 if (!zcp->do_foreground)
256 zed_log_msg(LOG_NOTICE,
257 "ZFS Event Daemon %s-%s (PID %d)",
258 ZFS_META_VERSION, ZFS_META_RELEASE, (int)getpid());
260 if (zed_conf_open_state(zcp) < 0)
263 if (zed_conf_read_state(zcp, &saved_eid, saved_etime) < 0)
268 * If -I is specified, attempt to open /dev/zfs repeatedly until
272 if (!zed_event_init(zcp))
274 /* Wait for some time and try again. tunable? */
276 } while (!_got_exit && zcp->do_idle);
281 zed_event_seek(zcp, saved_eid, saved_etime);
287 (void) zed_conf_scan_dir(zcp);
289 rv = zed_event_service(zcp);
291 /* ENODEV: When kernel module is unloaded (osx) */
296 zed_log_msg(LOG_NOTICE, "Exiting");
299 if (zcp->do_idle && !_got_exit)
303 zed_conf_destroy(zcp);