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 OpenZFS 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
)
44 * Signal handler for SIGHUP.
47 _hup_handler(int signum
)
54 * Register signal handlers.
57 _setup_sig_handlers(void)
61 if (sigemptyset(&sa
.sa_mask
) < 0)
62 zed_log_die("Failed to initialize sigset");
64 sa
.sa_flags
= SA_RESTART
;
66 sa
.sa_handler
= SIG_IGN
;
67 if (sigaction(SIGPIPE
, &sa
, NULL
) < 0)
68 zed_log_die("Failed to ignore SIGPIPE");
70 sa
.sa_handler
= _exit_handler
;
71 if (sigaction(SIGINT
, &sa
, NULL
) < 0)
72 zed_log_die("Failed to register SIGINT handler");
74 if (sigaction(SIGTERM
, &sa
, NULL
) < 0)
75 zed_log_die("Failed to register SIGTERM handler");
77 sa
.sa_handler
= _hup_handler
;
78 if (sigaction(SIGHUP
, &sa
, NULL
) < 0)
79 zed_log_die("Failed to register SIGHUP handler");
81 (void) sigaddset(&sa
.sa_mask
, SIGCHLD
);
82 if (pthread_sigmask(SIG_BLOCK
, &sa
.sa_mask
, NULL
) < 0)
83 zed_log_die("Failed to block SIGCHLD");
87 * Lock all current and future pages in the virtual memory address space.
88 * Access to locked pages will never be delayed by a page fault.
90 * EAGAIN is tested up to max_tries in case this is a transient error.
92 * Note that memory locks are not inherited by a child created via fork()
93 * and are automatically removed during an execve(). As such, this must
94 * be called after the daemon fork()s (when running in the background).
101 const int max_tries
= 10;
103 for (i
= 0; i
< max_tries
; i
++) {
104 if (mlockall(MCL_CURRENT
| MCL_FUTURE
) == 0) {
105 zed_log_msg(LOG_INFO
, "Locked all pages in memory");
111 zed_log_die("Failed to lock memory pages: %s", strerror(errno
));
113 #else /* HAVE_MLOCKALL */
114 zed_log_die("Failed to lock memory pages: mlockall() not supported");
115 #endif /* HAVE_MLOCKALL */
119 * Start daemonization of the process including the double fork().
121 * The parent process will block here until _finish_daemonize() is called
122 * (in the grandchild process), at which point the parent process will exit.
123 * This prevents the parent process from exiting until initialization is
127 _start_daemonize(void)
132 /* Create pipe for communicating with child during daemonization. */
135 /* Background process and ensure child is not process group leader. */
138 zed_log_die("Failed to create child process: %s",
140 } else if (pid
> 0) {
142 /* Close writes since parent will only read from pipe. */
143 zed_log_pipe_close_writes();
145 /* Wait for notification that daemonization is complete. */
148 zed_log_pipe_close_reads();
152 /* Close reads since child will only write to pipe. */
153 zed_log_pipe_close_reads();
155 /* Create independent session and detach from terminal. */
157 zed_log_die("Failed to create new session: %s",
160 /* Prevent child from terminating on HUP when session leader exits. */
161 if (sigemptyset(&sa
.sa_mask
) < 0)
162 zed_log_die("Failed to initialize sigset");
165 sa
.sa_handler
= SIG_IGN
;
167 if (sigaction(SIGHUP
, &sa
, NULL
) < 0)
168 zed_log_die("Failed to ignore SIGHUP");
170 /* Ensure process cannot re-acquire terminal. */
173 zed_log_die("Failed to create grandchild process: %s",
175 } else if (pid
> 0) {
181 * Finish daemonization of the process by closing stdin/stdout/stderr.
183 * This must be called at the end of initialization after all external
184 * communication channels are established and accessible.
187 _finish_daemonize(void)
191 /* Preserve fd 0/1/2, but discard data to/from stdin/stdout/stderr. */
192 devnull
= open("/dev/null", O_RDWR
);
194 zed_log_die("Failed to open /dev/null: %s", strerror(errno
));
196 if (dup2(devnull
, STDIN_FILENO
) < 0)
197 zed_log_die("Failed to dup /dev/null onto stdin: %s",
200 if (dup2(devnull
, STDOUT_FILENO
) < 0)
201 zed_log_die("Failed to dup /dev/null onto stdout: %s",
204 if (dup2(devnull
, STDERR_FILENO
) < 0)
205 zed_log_die("Failed to dup /dev/null onto stderr: %s",
208 if ((devnull
> STDERR_FILENO
) && (close(devnull
) < 0))
209 zed_log_die("Failed to close /dev/null: %s", strerror(errno
));
211 /* Notify parent that daemonization is complete. */
212 zed_log_pipe_close_writes();
216 * ZFS Event Daemon (ZED).
219 main(int argc
, char *argv
[])
223 int64_t saved_etime
[2];
225 zed_log_init(argv
[0]);
226 zed_log_stderr_open(LOG_NOTICE
);
228 zed_conf_parse_opts(&zcp
, argc
, argv
);
230 zed_log_stderr_open(LOG_INFO
);
233 zed_log_die("Must be run as root");
235 zed_file_close_from(STDERR_FILENO
+ 1);
240 zed_log_die("Failed to change to root directory");
242 if (zed_conf_scan_dir(&zcp
) < 0)
245 if (!zcp
.do_foreground
) {
247 zed_log_syslog_open(LOG_DAEMON
);
249 _setup_sig_handlers();
254 if ((zed_conf_write_pid(&zcp
) < 0) && (!zcp
.do_force
))
257 if (!zcp
.do_foreground
)
260 zed_log_msg(LOG_NOTICE
,
261 "ZFS Event Daemon %s-%s (PID %d)",
262 ZFS_META_VERSION
, ZFS_META_RELEASE
, (int)getpid());
264 if (zed_conf_open_state(&zcp
) < 0)
267 if (zed_conf_read_state(&zcp
, &saved_eid
, saved_etime
) < 0)
272 * If -I is specified, attempt to open /dev/zfs repeatedly until
276 if (!zed_event_init(&zcp
))
278 /* Wait for some time and try again. tunable? */
280 } while (!_got_exit
&& zcp
.do_idle
);
285 zed_event_seek(&zcp
, saved_eid
, saved_etime
);
291 (void) zed_conf_scan_dir(&zcp
);
293 rv
= zed_event_service(&zcp
);
295 /* ENODEV: When kernel module is unloaded (osx) */
300 zed_log_msg(LOG_NOTICE
, "Exiting");
301 zed_event_fini(&zcp
);
303 if (zcp
.do_idle
&& !_got_exit
)
307 zed_conf_destroy(&zcp
);