zprop: fix value help for ZPOOL_PROP_CAPACITY
[zfs.git] / cmd / zed / zed.c
blob4a413af6f927604f8b09cc84854b722da275fc50
1 /*
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.
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include "zed.h"
25 #include "zed_conf.h"
26 #include "zed_event.h"
27 #include "zed_file.h"
28 #include "zed_log.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.
36 static void
37 _exit_handler(int signum)
39 (void) signum;
40 _got_exit = 1;
44 * Signal handler for SIGHUP.
46 static void
47 _hup_handler(int signum)
49 (void) signum;
50 _got_hup = 1;
54 * Register signal handlers.
56 static void
57 _setup_sig_handlers(void)
59 struct sigaction sa;
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).
96 static void
97 _lock_memory(void)
99 #if HAVE_MLOCKALL
100 int i = 0;
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");
106 return;
108 if (errno != EAGAIN)
109 break;
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
124 * complete.
126 static void
127 _start_daemonize(void)
129 pid_t pid;
130 struct sigaction sa;
132 /* Create pipe for communicating with child during daemonization. */
133 zed_log_pipe_open();
135 /* Background process and ensure child is not process group leader. */
136 pid = fork();
137 if (pid < 0) {
138 zed_log_die("Failed to create child process: %s",
139 strerror(errno));
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. */
146 zed_log_pipe_wait();
148 zed_log_pipe_close_reads();
149 _exit(EXIT_SUCCESS);
152 /* Close reads since child will only write to pipe. */
153 zed_log_pipe_close_reads();
155 /* Create independent session and detach from terminal. */
156 if (setsid() < 0)
157 zed_log_die("Failed to create new session: %s",
158 strerror(errno));
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");
164 sa.sa_flags = 0;
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. */
171 pid = fork();
172 if (pid < 0) {
173 zed_log_die("Failed to create grandchild process: %s",
174 strerror(errno));
175 } else if (pid > 0) {
176 _exit(EXIT_SUCCESS);
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.
186 static void
187 _finish_daemonize(void)
189 int devnull;
191 /* Preserve fd 0/1/2, but discard data to/from stdin/stdout/stderr. */
192 devnull = open("/dev/null", O_RDWR);
193 if (devnull < 0)
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",
198 strerror(errno));
200 if (dup2(devnull, STDOUT_FILENO) < 0)
201 zed_log_die("Failed to dup /dev/null onto stdout: %s",
202 strerror(errno));
204 if (dup2(devnull, STDERR_FILENO) < 0)
205 zed_log_die("Failed to dup /dev/null onto stderr: %s",
206 strerror(errno));
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[])
221 struct zed_conf zcp;
222 uint64_t saved_eid;
223 int64_t saved_etime[2];
225 zed_log_init(argv[0]);
226 zed_log_stderr_open(LOG_NOTICE);
227 zed_conf_init(&zcp);
228 zed_conf_parse_opts(&zcp, argc, argv);
229 if (zcp.do_verbose)
230 zed_log_stderr_open(LOG_INFO);
232 if (geteuid() != 0)
233 zed_log_die("Must be run as root");
235 zed_file_close_from(STDERR_FILENO + 1);
237 (void) umask(0);
239 if (chdir("/") < 0)
240 zed_log_die("Failed to change to root directory");
242 if (zed_conf_scan_dir(&zcp) < 0)
243 exit(EXIT_FAILURE);
245 if (!zcp.do_foreground) {
246 _start_daemonize();
247 zed_log_syslog_open(LOG_DAEMON);
249 _setup_sig_handlers();
251 if (zcp.do_memlock)
252 _lock_memory();
254 if ((zed_conf_write_pid(&zcp) < 0) && (!zcp.do_force))
255 exit(EXIT_FAILURE);
257 if (!zcp.do_foreground)
258 _finish_daemonize();
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)
265 exit(EXIT_FAILURE);
267 if (zed_conf_read_state(&zcp, &saved_eid, saved_etime) < 0)
268 exit(EXIT_FAILURE);
270 idle:
272 * If -I is specified, attempt to open /dev/zfs repeatedly until
273 * successful.
275 do {
276 if (!zed_event_init(&zcp))
277 break;
278 /* Wait for some time and try again. tunable? */
279 sleep(30);
280 } while (!_got_exit && zcp.do_idle);
282 if (_got_exit)
283 goto out;
285 zed_event_seek(&zcp, saved_eid, saved_etime);
287 while (!_got_exit) {
288 int rv;
289 if (_got_hup) {
290 _got_hup = 0;
291 (void) zed_conf_scan_dir(&zcp);
293 rv = zed_event_service(&zcp);
295 /* ENODEV: When kernel module is unloaded (osx) */
296 if (rv != 0)
297 break;
300 zed_log_msg(LOG_NOTICE, "Exiting");
301 zed_event_fini(&zcp);
303 if (zcp.do_idle && !_got_exit)
304 goto idle;
306 out:
307 zed_conf_destroy(&zcp);
308 zed_log_fini();
309 exit(EXIT_SUCCESS);