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