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