1eda14cbcSMatt Macy /*
2180f8225SMatt Macy * This file is part of the ZFS Event Daemon (ZED).
3180f8225SMatt Macy *
4eda14cbcSMatt Macy * Developed at Lawrence Livermore National Laboratory (LLNL-CODE-403049).
5eda14cbcSMatt Macy * Copyright (C) 2013-2014 Lawrence Livermore National Security, LLC.
616038816SMartin Matuska * Refer to the OpenZFS git commit log for authoritative copyright attribution.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
9eda14cbcSMatt Macy * Common Development and Distribution License Version 1.0 (CDDL-1.0).
10eda14cbcSMatt Macy * You can obtain a copy of the license from the top-level file
11eda14cbcSMatt Macy * "OPENSOLARIS.LICENSE" or at <http://opensource.org/licenses/CDDL-1.0>.
12eda14cbcSMatt Macy * You may not use this file except in compliance with the license.
13eda14cbcSMatt Macy */
14eda14cbcSMatt Macy
15eda14cbcSMatt Macy #include <errno.h>
16eda14cbcSMatt Macy #include <fcntl.h>
17eda14cbcSMatt Macy #include <signal.h>
18eda14cbcSMatt Macy #include <stdio.h>
19eda14cbcSMatt Macy #include <stdlib.h>
20eda14cbcSMatt Macy #include <string.h>
21eda14cbcSMatt Macy #include <sys/mman.h>
22eda14cbcSMatt Macy #include <sys/stat.h>
23eda14cbcSMatt Macy #include <unistd.h>
24eda14cbcSMatt Macy #include "zed.h"
25eda14cbcSMatt Macy #include "zed_conf.h"
26eda14cbcSMatt Macy #include "zed_event.h"
27eda14cbcSMatt Macy #include "zed_file.h"
28eda14cbcSMatt Macy #include "zed_log.h"
29eda14cbcSMatt Macy
30eda14cbcSMatt Macy static volatile sig_atomic_t _got_exit = 0;
31eda14cbcSMatt Macy static volatile sig_atomic_t _got_hup = 0;
32eda14cbcSMatt Macy
33eda14cbcSMatt Macy /*
34eda14cbcSMatt Macy * Signal handler for SIGINT & SIGTERM.
35eda14cbcSMatt Macy */
36eda14cbcSMatt Macy static void
_exit_handler(int signum)37eda14cbcSMatt Macy _exit_handler(int signum)
38eda14cbcSMatt Macy {
39*e92ffd9bSMartin Matuska (void) signum;
40eda14cbcSMatt Macy _got_exit = 1;
41eda14cbcSMatt Macy }
42eda14cbcSMatt Macy
43eda14cbcSMatt Macy /*
44eda14cbcSMatt Macy * Signal handler for SIGHUP.
45eda14cbcSMatt Macy */
46eda14cbcSMatt Macy static void
_hup_handler(int signum)47eda14cbcSMatt Macy _hup_handler(int signum)
48eda14cbcSMatt Macy {
49*e92ffd9bSMartin Matuska (void) signum;
50eda14cbcSMatt Macy _got_hup = 1;
51eda14cbcSMatt Macy }
52eda14cbcSMatt Macy
53eda14cbcSMatt Macy /*
54eda14cbcSMatt Macy * Register signal handlers.
55eda14cbcSMatt Macy */
56eda14cbcSMatt Macy static void
_setup_sig_handlers(void)57eda14cbcSMatt Macy _setup_sig_handlers(void)
58eda14cbcSMatt Macy {
59eda14cbcSMatt Macy struct sigaction sa;
60eda14cbcSMatt Macy
61eda14cbcSMatt Macy if (sigemptyset(&sa.sa_mask) < 0)
62eda14cbcSMatt Macy zed_log_die("Failed to initialize sigset");
63eda14cbcSMatt Macy
64eda14cbcSMatt Macy sa.sa_flags = SA_RESTART;
65eda14cbcSMatt Macy
6616038816SMartin Matuska sa.sa_handler = SIG_IGN;
67eda14cbcSMatt Macy if (sigaction(SIGPIPE, &sa, NULL) < 0)
68eda14cbcSMatt Macy zed_log_die("Failed to ignore SIGPIPE");
69eda14cbcSMatt Macy
70eda14cbcSMatt Macy sa.sa_handler = _exit_handler;
71eda14cbcSMatt Macy if (sigaction(SIGINT, &sa, NULL) < 0)
72eda14cbcSMatt Macy zed_log_die("Failed to register SIGINT handler");
73eda14cbcSMatt Macy
74eda14cbcSMatt Macy if (sigaction(SIGTERM, &sa, NULL) < 0)
75eda14cbcSMatt Macy zed_log_die("Failed to register SIGTERM handler");
76eda14cbcSMatt Macy
77eda14cbcSMatt Macy sa.sa_handler = _hup_handler;
78eda14cbcSMatt Macy if (sigaction(SIGHUP, &sa, NULL) < 0)
79eda14cbcSMatt Macy zed_log_die("Failed to register SIGHUP handler");
8016038816SMartin Matuska
8116038816SMartin Matuska (void) sigaddset(&sa.sa_mask, SIGCHLD);
8216038816SMartin Matuska if (pthread_sigmask(SIG_BLOCK, &sa.sa_mask, NULL) < 0)
8316038816SMartin Matuska zed_log_die("Failed to block SIGCHLD");
84eda14cbcSMatt Macy }
85eda14cbcSMatt Macy
86eda14cbcSMatt Macy /*
87eda14cbcSMatt Macy * Lock all current and future pages in the virtual memory address space.
88eda14cbcSMatt Macy * Access to locked pages will never be delayed by a page fault.
89eda14cbcSMatt Macy *
90eda14cbcSMatt Macy * EAGAIN is tested up to max_tries in case this is a transient error.
91eda14cbcSMatt Macy *
92eda14cbcSMatt Macy * Note that memory locks are not inherited by a child created via fork()
93eda14cbcSMatt Macy * and are automatically removed during an execve(). As such, this must
94eda14cbcSMatt Macy * be called after the daemon fork()s (when running in the background).
95eda14cbcSMatt Macy */
96eda14cbcSMatt Macy static void
_lock_memory(void)97eda14cbcSMatt Macy _lock_memory(void)
98eda14cbcSMatt Macy {
99eda14cbcSMatt Macy #if HAVE_MLOCKALL
100eda14cbcSMatt Macy int i = 0;
101eda14cbcSMatt Macy const int max_tries = 10;
102eda14cbcSMatt Macy
103eda14cbcSMatt Macy for (i = 0; i < max_tries; i++) {
104eda14cbcSMatt Macy if (mlockall(MCL_CURRENT | MCL_FUTURE) == 0) {
105eda14cbcSMatt Macy zed_log_msg(LOG_INFO, "Locked all pages in memory");
106eda14cbcSMatt Macy return;
107eda14cbcSMatt Macy }
108eda14cbcSMatt Macy if (errno != EAGAIN)
109eda14cbcSMatt Macy break;
110eda14cbcSMatt Macy }
111eda14cbcSMatt Macy zed_log_die("Failed to lock memory pages: %s", strerror(errno));
112eda14cbcSMatt Macy
113eda14cbcSMatt Macy #else /* HAVE_MLOCKALL */
114eda14cbcSMatt Macy zed_log_die("Failed to lock memory pages: mlockall() not supported");
115eda14cbcSMatt Macy #endif /* HAVE_MLOCKALL */
116eda14cbcSMatt Macy }
117eda14cbcSMatt Macy
118eda14cbcSMatt Macy /*
119eda14cbcSMatt Macy * Start daemonization of the process including the double fork().
120eda14cbcSMatt Macy *
121eda14cbcSMatt Macy * The parent process will block here until _finish_daemonize() is called
122eda14cbcSMatt Macy * (in the grandchild process), at which point the parent process will exit.
123eda14cbcSMatt Macy * This prevents the parent process from exiting until initialization is
124eda14cbcSMatt Macy * complete.
125eda14cbcSMatt Macy */
126eda14cbcSMatt Macy static void
_start_daemonize(void)127eda14cbcSMatt Macy _start_daemonize(void)
128eda14cbcSMatt Macy {
129eda14cbcSMatt Macy pid_t pid;
130eda14cbcSMatt Macy struct sigaction sa;
131eda14cbcSMatt Macy
132eda14cbcSMatt Macy /* Create pipe for communicating with child during daemonization. */
133eda14cbcSMatt Macy zed_log_pipe_open();
134eda14cbcSMatt Macy
135eda14cbcSMatt Macy /* Background process and ensure child is not process group leader. */
136eda14cbcSMatt Macy pid = fork();
137eda14cbcSMatt Macy if (pid < 0) {
138eda14cbcSMatt Macy zed_log_die("Failed to create child process: %s",
139eda14cbcSMatt Macy strerror(errno));
140eda14cbcSMatt Macy } else if (pid > 0) {
141eda14cbcSMatt Macy
142eda14cbcSMatt Macy /* Close writes since parent will only read from pipe. */
143eda14cbcSMatt Macy zed_log_pipe_close_writes();
144eda14cbcSMatt Macy
145eda14cbcSMatt Macy /* Wait for notification that daemonization is complete. */
146eda14cbcSMatt Macy zed_log_pipe_wait();
147eda14cbcSMatt Macy
148eda14cbcSMatt Macy zed_log_pipe_close_reads();
149eda14cbcSMatt Macy _exit(EXIT_SUCCESS);
150eda14cbcSMatt Macy }
151eda14cbcSMatt Macy
152eda14cbcSMatt Macy /* Close reads since child will only write to pipe. */
153eda14cbcSMatt Macy zed_log_pipe_close_reads();
154eda14cbcSMatt Macy
155eda14cbcSMatt Macy /* Create independent session and detach from terminal. */
156eda14cbcSMatt Macy if (setsid() < 0)
157eda14cbcSMatt Macy zed_log_die("Failed to create new session: %s",
158eda14cbcSMatt Macy strerror(errno));
159eda14cbcSMatt Macy
160eda14cbcSMatt Macy /* Prevent child from terminating on HUP when session leader exits. */
161eda14cbcSMatt Macy if (sigemptyset(&sa.sa_mask) < 0)
162eda14cbcSMatt Macy zed_log_die("Failed to initialize sigset");
163eda14cbcSMatt Macy
164eda14cbcSMatt Macy sa.sa_flags = 0;
165eda14cbcSMatt Macy sa.sa_handler = SIG_IGN;
166eda14cbcSMatt Macy
167eda14cbcSMatt Macy if (sigaction(SIGHUP, &sa, NULL) < 0)
168eda14cbcSMatt Macy zed_log_die("Failed to ignore SIGHUP");
169eda14cbcSMatt Macy
170eda14cbcSMatt Macy /* Ensure process cannot re-acquire terminal. */
171eda14cbcSMatt Macy pid = fork();
172eda14cbcSMatt Macy if (pid < 0) {
173eda14cbcSMatt Macy zed_log_die("Failed to create grandchild process: %s",
174eda14cbcSMatt Macy strerror(errno));
175eda14cbcSMatt Macy } else if (pid > 0) {
176eda14cbcSMatt Macy _exit(EXIT_SUCCESS);
177eda14cbcSMatt Macy }
178eda14cbcSMatt Macy }
179eda14cbcSMatt Macy
180eda14cbcSMatt Macy /*
181eda14cbcSMatt Macy * Finish daemonization of the process by closing stdin/stdout/stderr.
182eda14cbcSMatt Macy *
183eda14cbcSMatt Macy * This must be called at the end of initialization after all external
184eda14cbcSMatt Macy * communication channels are established and accessible.
185eda14cbcSMatt Macy */
186eda14cbcSMatt Macy static void
_finish_daemonize(void)187eda14cbcSMatt Macy _finish_daemonize(void)
188eda14cbcSMatt Macy {
189eda14cbcSMatt Macy int devnull;
190eda14cbcSMatt Macy
191eda14cbcSMatt Macy /* Preserve fd 0/1/2, but discard data to/from stdin/stdout/stderr. */
192eda14cbcSMatt Macy devnull = open("/dev/null", O_RDWR);
193eda14cbcSMatt Macy if (devnull < 0)
194eda14cbcSMatt Macy zed_log_die("Failed to open /dev/null: %s", strerror(errno));
195eda14cbcSMatt Macy
196eda14cbcSMatt Macy if (dup2(devnull, STDIN_FILENO) < 0)
197eda14cbcSMatt Macy zed_log_die("Failed to dup /dev/null onto stdin: %s",
198eda14cbcSMatt Macy strerror(errno));
199eda14cbcSMatt Macy
200eda14cbcSMatt Macy if (dup2(devnull, STDOUT_FILENO) < 0)
201eda14cbcSMatt Macy zed_log_die("Failed to dup /dev/null onto stdout: %s",
202eda14cbcSMatt Macy strerror(errno));
203eda14cbcSMatt Macy
204eda14cbcSMatt Macy if (dup2(devnull, STDERR_FILENO) < 0)
205eda14cbcSMatt Macy zed_log_die("Failed to dup /dev/null onto stderr: %s",
206eda14cbcSMatt Macy strerror(errno));
207eda14cbcSMatt Macy
208eda14cbcSMatt Macy if ((devnull > STDERR_FILENO) && (close(devnull) < 0))
209eda14cbcSMatt Macy zed_log_die("Failed to close /dev/null: %s", strerror(errno));
210eda14cbcSMatt Macy
211eda14cbcSMatt Macy /* Notify parent that daemonization is complete. */
212eda14cbcSMatt Macy zed_log_pipe_close_writes();
213eda14cbcSMatt Macy }
214eda14cbcSMatt Macy
215eda14cbcSMatt Macy /*
216eda14cbcSMatt Macy * ZFS Event Daemon (ZED).
217eda14cbcSMatt Macy */
218eda14cbcSMatt Macy int
main(int argc,char * argv[])219eda14cbcSMatt Macy main(int argc, char *argv[])
220eda14cbcSMatt Macy {
22116038816SMartin Matuska struct zed_conf zcp;
222eda14cbcSMatt Macy uint64_t saved_eid;
223eda14cbcSMatt Macy int64_t saved_etime[2];
224eda14cbcSMatt Macy
225eda14cbcSMatt Macy zed_log_init(argv[0]);
226eda14cbcSMatt Macy zed_log_stderr_open(LOG_NOTICE);
22716038816SMartin Matuska zed_conf_init(&zcp);
22816038816SMartin Matuska zed_conf_parse_opts(&zcp, argc, argv);
22916038816SMartin Matuska if (zcp.do_verbose)
230eda14cbcSMatt Macy zed_log_stderr_open(LOG_INFO);
231eda14cbcSMatt Macy
232eda14cbcSMatt Macy if (geteuid() != 0)
233eda14cbcSMatt Macy zed_log_die("Must be run as root");
234eda14cbcSMatt Macy
235eda14cbcSMatt Macy zed_file_close_from(STDERR_FILENO + 1);
236eda14cbcSMatt Macy
237eda14cbcSMatt Macy (void) umask(0);
238eda14cbcSMatt Macy
239eda14cbcSMatt Macy if (chdir("/") < 0)
240eda14cbcSMatt Macy zed_log_die("Failed to change to root directory");
241eda14cbcSMatt Macy
24216038816SMartin Matuska if (zed_conf_scan_dir(&zcp) < 0)
243eda14cbcSMatt Macy exit(EXIT_FAILURE);
244eda14cbcSMatt Macy
24516038816SMartin Matuska if (!zcp.do_foreground) {
246eda14cbcSMatt Macy _start_daemonize();
247eda14cbcSMatt Macy zed_log_syslog_open(LOG_DAEMON);
248eda14cbcSMatt Macy }
249eda14cbcSMatt Macy _setup_sig_handlers();
250eda14cbcSMatt Macy
25116038816SMartin Matuska if (zcp.do_memlock)
252eda14cbcSMatt Macy _lock_memory();
253eda14cbcSMatt Macy
25416038816SMartin Matuska if ((zed_conf_write_pid(&zcp) < 0) && (!zcp.do_force))
255eda14cbcSMatt Macy exit(EXIT_FAILURE);
256eda14cbcSMatt Macy
25716038816SMartin Matuska if (!zcp.do_foreground)
258eda14cbcSMatt Macy _finish_daemonize();
259eda14cbcSMatt Macy
260eda14cbcSMatt Macy zed_log_msg(LOG_NOTICE,
261eda14cbcSMatt Macy "ZFS Event Daemon %s-%s (PID %d)",
262eda14cbcSMatt Macy ZFS_META_VERSION, ZFS_META_RELEASE, (int)getpid());
263eda14cbcSMatt Macy
26416038816SMartin Matuska if (zed_conf_open_state(&zcp) < 0)
265eda14cbcSMatt Macy exit(EXIT_FAILURE);
266eda14cbcSMatt Macy
26716038816SMartin Matuska if (zed_conf_read_state(&zcp, &saved_eid, saved_etime) < 0)
268eda14cbcSMatt Macy exit(EXIT_FAILURE);
269eda14cbcSMatt Macy
270eda14cbcSMatt Macy idle:
271eda14cbcSMatt Macy /*
272eda14cbcSMatt Macy * If -I is specified, attempt to open /dev/zfs repeatedly until
273eda14cbcSMatt Macy * successful.
274eda14cbcSMatt Macy */
275eda14cbcSMatt Macy do {
27616038816SMartin Matuska if (!zed_event_init(&zcp))
277eda14cbcSMatt Macy break;
278eda14cbcSMatt Macy /* Wait for some time and try again. tunable? */
279eda14cbcSMatt Macy sleep(30);
28016038816SMartin Matuska } while (!_got_exit && zcp.do_idle);
281eda14cbcSMatt Macy
282eda14cbcSMatt Macy if (_got_exit)
283eda14cbcSMatt Macy goto out;
284eda14cbcSMatt Macy
28516038816SMartin Matuska zed_event_seek(&zcp, saved_eid, saved_etime);
286eda14cbcSMatt Macy
287eda14cbcSMatt Macy while (!_got_exit) {
288eda14cbcSMatt Macy int rv;
289eda14cbcSMatt Macy if (_got_hup) {
290eda14cbcSMatt Macy _got_hup = 0;
29116038816SMartin Matuska (void) zed_conf_scan_dir(&zcp);
292eda14cbcSMatt Macy }
29316038816SMartin Matuska rv = zed_event_service(&zcp);
294eda14cbcSMatt Macy
295eda14cbcSMatt Macy /* ENODEV: When kernel module is unloaded (osx) */
29653b70c86SMartin Matuska if (rv != 0)
297eda14cbcSMatt Macy break;
298eda14cbcSMatt Macy }
299eda14cbcSMatt Macy
300eda14cbcSMatt Macy zed_log_msg(LOG_NOTICE, "Exiting");
30116038816SMartin Matuska zed_event_fini(&zcp);
302eda14cbcSMatt Macy
30316038816SMartin Matuska if (zcp.do_idle && !_got_exit)
304eda14cbcSMatt Macy goto idle;
305eda14cbcSMatt Macy
306eda14cbcSMatt Macy out:
30716038816SMartin Matuska zed_conf_destroy(&zcp);
308eda14cbcSMatt Macy zed_log_fini();
309eda14cbcSMatt Macy exit(EXIT_SUCCESS);
310eda14cbcSMatt Macy }
311