]>
Commit | Line | Data |
---|---|---|
f13467ec | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
7fafc032 | 2 | /* |
810adae9 LP |
3 | * Copyright © 2004 Chris Friesen <chris_friesen@sympatico.ca> |
4 | * Copyright © 2009 Canonical Ltd. | |
5 | * Copyright © 2009 Scott James Remnant <scott@netsplit.com> | |
7fafc032 KS |
6 | */ |
7 | ||
7fafc032 | 8 | #include <errno.h> |
618234a5 LP |
9 | #include <fcntl.h> |
10 | #include <getopt.h> | |
618234a5 LP |
11 | #include <stdbool.h> |
12 | #include <stddef.h> | |
7fafc032 KS |
13 | #include <stdio.h> |
14 | #include <stdlib.h> | |
618234a5 | 15 | #include <sys/epoll.h> |
3ebdb81e | 16 | #include <sys/file.h> |
618234a5 LP |
17 | #include <sys/inotify.h> |
18 | #include <sys/ioctl.h> | |
19 | #include <sys/mount.h> | |
1e03b754 | 20 | #include <sys/prctl.h> |
1e03b754 | 21 | #include <sys/signalfd.h> |
dc117daa | 22 | #include <sys/stat.h> |
618234a5 LP |
23 | #include <sys/time.h> |
24 | #include <sys/wait.h> | |
25 | #include <unistd.h> | |
7fafc032 | 26 | |
392ef7a2 | 27 | #include "sd-daemon.h" |
693d371d | 28 | #include "sd-event.h" |
8314de1d | 29 | |
b5efdb8a | 30 | #include "alloc-util.h" |
194bbe33 | 31 | #include "cgroup-util.h" |
618234a5 | 32 | #include "cpu-set-util.h" |
5ba2dc25 | 33 | #include "dev-setup.h" |
7f2e3a14 | 34 | #include "device-monitor-private.h" |
abde5ea8 | 35 | #include "device-private.h" |
70068602 | 36 | #include "device-util.h" |
6d63048a | 37 | #include "event-util.h" |
3ffd4af2 | 38 | #include "fd-util.h" |
a5c32cff | 39 | #include "fileio.h" |
f97b34a6 | 40 | #include "format-util.h" |
f4f15635 | 41 | #include "fs-util.h" |
a505965d | 42 | #include "hashmap.h" |
c004493c | 43 | #include "io-util.h" |
eefc66aa | 44 | #include "limits-util.h" |
40a57716 | 45 | #include "list.h" |
0c5a109a | 46 | #include "main-func.h" |
5ea78a39 | 47 | #include "mkdir.h" |
618234a5 | 48 | #include "netlink-util.h" |
6bedfcbb | 49 | #include "parse-util.h" |
294bf0c3 | 50 | #include "pretty-print.h" |
4e731273 | 51 | #include "proc-cmdline.h" |
618234a5 LP |
52 | #include "process-util.h" |
53 | #include "selinux-util.h" | |
54 | #include "signal-util.h" | |
8f328d36 | 55 | #include "socket-util.h" |
07630cea | 56 | #include "string-util.h" |
49fe5c09 | 57 | #include "strv.h" |
5ea78a39 | 58 | #include "strxcpyx.h" |
46f0fbd8 | 59 | #include "syslog-util.h" |
63e2d171 | 60 | #include "udevd.h" |
07a26e42 | 61 | #include "udev-builtin.h" |
7d68eb1b | 62 | #include "udev-ctrl.h" |
25de7aa7 | 63 | #include "udev-event.h" |
618234a5 | 64 | #include "udev-util.h" |
70068602 | 65 | #include "udev-watch.h" |
ee104e11 | 66 | #include "user-util.h" |
47350c5f | 67 | #include "version.h" |
7fafc032 | 68 | |
88bd5a32 FB |
69 | #define WORKER_NUM_MAX 2048U |
70 | ||
bba7a484 TG |
71 | static bool arg_debug = false; |
72 | static int arg_daemonize = false; | |
c4d44cba | 73 | static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY; |
216e8bbe | 74 | static unsigned arg_children_max = 0; |
6b92f429 | 75 | static usec_t arg_exec_delay_usec = 0; |
bba7a484 | 76 | static usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC; |
e2099267 | 77 | static int arg_timeout_signal = SIGKILL; |
95ac5230 | 78 | static bool arg_blockdev_read_only = false; |
c0c6806b | 79 | |
d9239923 YW |
80 | typedef struct Event Event; |
81 | typedef struct Worker Worker; | |
82 | ||
c0c6806b | 83 | typedef struct Manager { |
693d371d | 84 | sd_event *event; |
c0c6806b | 85 | Hashmap *workers; |
d9239923 | 86 | LIST_HEAD(Event, events); |
c26d1879 | 87 | const char *cgroup; |
cb49a4f2 | 88 | pid_t pid; /* the process that originally allocated the manager object */ |
1a0bd015 | 89 | int log_level; |
c0c6806b | 90 | |
9a07157d | 91 | UdevRules *rules; |
9b5150b6 | 92 | Hashmap *properties; |
c0c6806b | 93 | |
0bed242c YW |
94 | sd_netlink *rtnl; |
95 | ||
7f2e3a14 | 96 | sd_device_monitor *monitor; |
e0d61dac | 97 | UdevCtrl *ctrl; |
e237d8cb TG |
98 | int worker_watch[2]; |
99 | ||
df7ee959 YW |
100 | /* used by udev-watch */ |
101 | int inotify_fd; | |
693d371d | 102 | sd_event_source *inotify_event; |
df7ee959 | 103 | |
eca195ec | 104 | sd_event_source *kill_workers_event; |
693d371d | 105 | |
7c4c7e89 TG |
106 | usec_t last_usec; |
107 | ||
481f24d1 YW |
108 | bool stop_exec_queue; |
109 | bool exit; | |
c0c6806b | 110 | } Manager; |
1e03b754 | 111 | |
d9239923 | 112 | typedef enum EventState { |
912541b0 KS |
113 | EVENT_UNDEF, |
114 | EVENT_QUEUED, | |
115 | EVENT_RUNNING, | |
d9239923 | 116 | } EventState; |
1e03b754 | 117 | |
d9239923 | 118 | typedef struct Event { |
cb49a4f2 | 119 | Manager *manager; |
d9239923 YW |
120 | Worker *worker; |
121 | EventState state; | |
eb546b35 YW |
122 | |
123 | sd_device *dev; | |
124 | sd_device *dev_kernel; /* clone of originally received device */ | |
125 | ||
126 | uint64_t seqnum; | |
127 | uint64_t delaying_seqnum; | |
d8f462b4 YW |
128 | |
129 | sd_event_source *timeout_warning_event; | |
130 | sd_event_source *timeout_event; | |
eb546b35 | 131 | |
d9239923 YW |
132 | LIST_FIELDS(Event, event); |
133 | } Event; | |
1e03b754 | 134 | |
d9239923 | 135 | typedef enum WorkerState { |
912541b0 KS |
136 | WORKER_UNDEF, |
137 | WORKER_RUNNING, | |
138 | WORKER_IDLE, | |
139 | WORKER_KILLED, | |
f257a8fc | 140 | WORKER_KILLING, |
d9239923 | 141 | } WorkerState; |
1e03b754 | 142 | |
d9239923 | 143 | typedef struct Worker { |
c0c6806b | 144 | Manager *manager; |
912541b0 | 145 | pid_t pid; |
7f2e3a14 | 146 | sd_device_monitor *monitor; |
d9239923 YW |
147 | WorkerState state; |
148 | Event *event; | |
149 | } Worker; | |
1e03b754 KS |
150 | |
151 | /* passed from worker to main process */ | |
d9239923 YW |
152 | typedef struct WorkerMessage { |
153 | } WorkerMessage; | |
1e03b754 | 154 | |
d9239923 | 155 | static void event_free(Event *event) { |
c6aa11f2 TG |
156 | if (!event) |
157 | return; | |
ba47b71c | 158 | |
40a57716 | 159 | assert(event->manager); |
c6aa11f2 | 160 | |
40a57716 | 161 | LIST_REMOVE(event, event->manager->events, event); |
eb546b35 YW |
162 | sd_device_unref(event->dev); |
163 | sd_device_unref(event->dev_kernel); | |
c6aa11f2 | 164 | |
d8f462b4 YW |
165 | sd_event_source_unref(event->timeout_warning_event); |
166 | sd_event_source_unref(event->timeout_event); | |
693d371d | 167 | |
c6aa11f2 TG |
168 | if (event->worker) |
169 | event->worker->event = NULL; | |
170 | ||
ba47b71c YW |
171 | /* only clean up the queue from the process that created it */ |
172 | if (LIST_IS_EMPTY(event->manager->events) && | |
173 | event->manager->pid == getpid_cached()) | |
6be97d67 YW |
174 | if (unlink("/run/udev/queue") < 0 && errno != ENOENT) |
175 | log_warning_errno(errno, "Failed to unlink /run/udev/queue, ignoring: %m"); | |
cb49a4f2 | 176 | |
912541b0 | 177 | free(event); |
aa8734ff | 178 | } |
7a770250 | 179 | |
419ec631 YW |
180 | static void event_queue_cleanup(Manager *manager, EventState match_state) { |
181 | Event *event, *tmp; | |
182 | ||
183 | LIST_FOREACH_SAFE(event, event, tmp, manager->events) { | |
184 | if (match_state != EVENT_UNDEF && match_state != event->state) | |
185 | continue; | |
186 | ||
187 | event_free(event); | |
188 | } | |
189 | } | |
190 | ||
d9239923 | 191 | static Worker *worker_free(Worker *worker) { |
c6aa11f2 | 192 | if (!worker) |
75db809a | 193 | return NULL; |
bc113de9 | 194 | |
c0c6806b TG |
195 | assert(worker->manager); |
196 | ||
4a0b58c4 | 197 | hashmap_remove(worker->manager->workers, PID_TO_PTR(worker->pid)); |
7f2e3a14 | 198 | sd_device_monitor_unref(worker->monitor); |
c6aa11f2 TG |
199 | event_free(worker->event); |
200 | ||
75db809a | 201 | return mfree(worker); |
ff2c503d KS |
202 | } |
203 | ||
d9239923 YW |
204 | DEFINE_TRIVIAL_CLEANUP_FUNC(Worker*, worker_free); |
205 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(worker_hash_op, void, trivial_hash_func, trivial_compare_func, Worker, worker_free); | |
fc465079 | 206 | |
419ec631 YW |
207 | static void manager_clear_for_worker(Manager *manager) { |
208 | assert(manager); | |
209 | ||
210 | manager->inotify_event = sd_event_source_unref(manager->inotify_event); | |
211 | manager->kill_workers_event = sd_event_source_unref(manager->kill_workers_event); | |
212 | ||
213 | manager->event = sd_event_unref(manager->event); | |
214 | ||
215 | manager->workers = hashmap_free(manager->workers); | |
216 | event_queue_cleanup(manager, EVENT_UNDEF); | |
217 | ||
218 | manager->monitor = sd_device_monitor_unref(manager->monitor); | |
219 | manager->ctrl = udev_ctrl_unref(manager->ctrl); | |
220 | ||
221 | manager->worker_watch[READ_END] = safe_close(manager->worker_watch[READ_END]); | |
222 | } | |
223 | ||
224 | static Manager* manager_free(Manager *manager) { | |
225 | if (!manager) | |
226 | return NULL; | |
227 | ||
228 | udev_builtin_exit(); | |
229 | ||
230 | if (manager->pid == getpid_cached()) | |
231 | udev_ctrl_cleanup(manager->ctrl); | |
232 | ||
233 | manager_clear_for_worker(manager); | |
234 | ||
235 | sd_netlink_unref(manager->rtnl); | |
236 | ||
237 | hashmap_free_free_free(manager->properties); | |
238 | udev_rules_free(manager->rules); | |
239 | ||
240 | safe_close(manager->inotify_fd); | |
241 | safe_close_pair(manager->worker_watch); | |
242 | ||
243 | return mfree(manager); | |
244 | } | |
245 | ||
246 | DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free); | |
247 | ||
d9239923 YW |
248 | static int worker_new(Worker **ret, Manager *manager, sd_device_monitor *worker_monitor, pid_t pid) { |
249 | _cleanup_(worker_freep) Worker *worker = NULL; | |
a505965d | 250 | int r; |
3a19b32a TG |
251 | |
252 | assert(ret); | |
c0c6806b | 253 | assert(manager); |
3a19b32a TG |
254 | assert(worker_monitor); |
255 | assert(pid > 1); | |
256 | ||
d4053464 YW |
257 | /* close monitor, but keep address around */ |
258 | device_monitor_disconnect(worker_monitor); | |
259 | ||
d9239923 | 260 | worker = new(Worker, 1); |
3a19b32a TG |
261 | if (!worker) |
262 | return -ENOMEM; | |
263 | ||
d9239923 | 264 | *worker = (Worker) { |
d4053464 YW |
265 | .manager = manager, |
266 | .monitor = sd_device_monitor_ref(worker_monitor), | |
267 | .pid = pid, | |
268 | }; | |
a505965d | 269 | |
92a74c47 | 270 | r = hashmap_ensure_put(&manager->workers, &worker_hash_op, PID_TO_PTR(pid), worker); |
a505965d TG |
271 | if (r < 0) |
272 | return r; | |
273 | ||
ae2a15bc | 274 | *ret = TAKE_PTR(worker); |
3a19b32a TG |
275 | |
276 | return 0; | |
277 | } | |
278 | ||
419ec631 YW |
279 | static void manager_kill_workers(Manager *manager, bool force) { |
280 | Worker *worker; | |
4fa4d885 | 281 | |
419ec631 | 282 | assert(manager); |
4fa4d885 | 283 | |
419ec631 YW |
284 | HASHMAP_FOREACH(worker, manager->workers) { |
285 | if (worker->state == WORKER_KILLED) | |
286 | continue; | |
4fa4d885 | 287 | |
419ec631 YW |
288 | if (worker->state == WORKER_RUNNING && !force) { |
289 | worker->state = WORKER_KILLING; | |
290 | continue; | |
291 | } | |
4fa4d885 | 292 | |
419ec631 YW |
293 | worker->state = WORKER_KILLED; |
294 | (void) kill(worker->pid, SIGTERM); | |
295 | } | |
4fa4d885 TG |
296 | } |
297 | ||
419ec631 YW |
298 | static void manager_exit(Manager *manager) { |
299 | assert(manager); | |
693d371d | 300 | |
419ec631 | 301 | manager->exit = true; |
693d371d | 302 | |
419ec631 YW |
303 | sd_notify(false, |
304 | "STOPPING=1\n" | |
305 | "STATUS=Starting shutdown..."); | |
39c19cf1 | 306 | |
419ec631 YW |
307 | /* close sources of new events and discard buffered events */ |
308 | manager->ctrl = udev_ctrl_unref(manager->ctrl); | |
0bed242c | 309 | |
0bed242c | 310 | manager->inotify_event = sd_event_source_unref(manager->inotify_event); |
419ec631 | 311 | manager->inotify_fd = safe_close(manager->inotify_fd); |
0bed242c | 312 | |
7f2e3a14 | 313 | manager->monitor = sd_device_monitor_unref(manager->monitor); |
0bed242c | 314 | |
419ec631 YW |
315 | /* discard queued events and kill workers */ |
316 | event_queue_cleanup(manager, EVENT_QUEUED); | |
317 | manager_kill_workers(manager, true); | |
0bed242c YW |
318 | } |
319 | ||
419ec631 YW |
320 | /* reload requested, HUP signal received, rules changed, builtin changed */ |
321 | static void manager_reload(Manager *manager) { | |
e237d8cb | 322 | |
419ec631 | 323 | assert(manager); |
b2d21d93 | 324 | |
419ec631 YW |
325 | sd_notify(false, |
326 | "RELOADING=1\n" | |
327 | "STATUS=Flushing configuration..."); | |
76e62a4d | 328 | |
419ec631 YW |
329 | manager_kill_workers(manager, false); |
330 | manager->rules = udev_rules_free(manager->rules); | |
331 | udev_builtin_exit(); | |
693d371d | 332 | |
419ec631 YW |
333 | sd_notifyf(false, |
334 | "READY=1\n" | |
335 | "STATUS=Processing with %u children at max", arg_children_max); | |
336 | } | |
e237d8cb | 337 | |
419ec631 YW |
338 | static int on_kill_workers_event(sd_event_source *s, uint64_t usec, void *userdata) { |
339 | Manager *manager = userdata; | |
e237d8cb | 340 | |
419ec631 | 341 | assert(manager); |
e237d8cb | 342 | |
419ec631 YW |
343 | log_debug("Cleanup idle workers"); |
344 | manager_kill_workers(manager, false); | |
e237d8cb | 345 | |
419ec631 YW |
346 | return 1; |
347 | } | |
e237d8cb | 348 | |
9a73bd7c | 349 | static int worker_send_message(int fd) { |
d9239923 | 350 | WorkerMessage message = {}; |
9a73bd7c TG |
351 | |
352 | return loop_write(fd, &message, sizeof(message), false); | |
353 | } | |
354 | ||
b97897e3 | 355 | static int worker_lock_block_device(sd_device *dev, int *ret_fd) { |
0bed242c | 356 | _cleanup_close_ int fd = -1; |
b97897e3 YW |
357 | const char *val; |
358 | int r; | |
fee854ee | 359 | |
0bed242c YW |
360 | assert(dev); |
361 | assert(ret_fd); | |
362 | ||
a1130022 LP |
363 | /* Take a shared lock on the device node; this establishes a concept of device "ownership" to |
364 | * serialize device access. External processes holding an exclusive lock will cause udev to skip the | |
365 | * event handling; in the case udev acquired the lock, the external process can block until udev has | |
366 | * finished its event handling. */ | |
367 | ||
368 | if (device_for_action(dev, SD_DEVICE_REMOVE)) | |
0bed242c YW |
369 | return 0; |
370 | ||
b97897e3 YW |
371 | r = sd_device_get_subsystem(dev, &val); |
372 | if (r < 0) | |
373 | return log_device_debug_errno(dev, r, "Failed to get subsystem: %m"); | |
374 | ||
375 | if (!streq(val, "block")) | |
0bed242c | 376 | return 0; |
fee854ee | 377 | |
b97897e3 YW |
378 | r = sd_device_get_sysname(dev, &val); |
379 | if (r < 0) | |
380 | return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); | |
381 | ||
49fe5c09 | 382 | if (STARTSWITH_SET(val, "dm-", "md", "drbd")) |
0bed242c YW |
383 | return 0; |
384 | ||
b97897e3 YW |
385 | r = sd_device_get_devtype(dev, &val); |
386 | if (r < 0 && r != -ENOENT) | |
387 | return log_device_debug_errno(dev, r, "Failed to get devtype: %m"); | |
388 | if (r >= 0 && streq(val, "partition")) { | |
389 | r = sd_device_get_parent(dev, &dev); | |
390 | if (r < 0) | |
391 | return log_device_debug_errno(dev, r, "Failed to get parent device: %m"); | |
392 | } | |
0bed242c | 393 | |
b97897e3 YW |
394 | r = sd_device_get_devname(dev, &val); |
395 | if (r == -ENOENT) | |
0bed242c | 396 | return 0; |
b97897e3 YW |
397 | if (r < 0) |
398 | return log_device_debug_errno(dev, r, "Failed to get devname: %m"); | |
0bed242c | 399 | |
b97897e3 YW |
400 | fd = open(val, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); |
401 | if (fd < 0) { | |
402 | log_device_debug_errno(dev, errno, "Failed to open '%s', ignoring: %m", val); | |
0bed242c | 403 | return 0; |
b97897e3 | 404 | } |
0bed242c YW |
405 | |
406 | if (flock(fd, LOCK_SH|LOCK_NB) < 0) | |
b97897e3 | 407 | return log_device_debug_errno(dev, errno, "Failed to flock(%s): %m", val); |
0bed242c YW |
408 | |
409 | *ret_fd = TAKE_FD(fd); | |
410 | return 1; | |
fee854ee RK |
411 | } |
412 | ||
95ac5230 LP |
413 | static int worker_mark_block_device_read_only(sd_device *dev) { |
414 | _cleanup_close_ int fd = -1; | |
415 | const char *val; | |
416 | int state = 1, r; | |
417 | ||
418 | assert(dev); | |
419 | ||
420 | if (!arg_blockdev_read_only) | |
421 | return 0; | |
422 | ||
423 | /* Do this only once, when the block device is new. If the device is later retriggered let's not | |
424 | * toggle the bit again, so that people can boot up with full read-only mode and then unset the bit | |
425 | * for specific devices only. */ | |
a1130022 | 426 | if (!device_for_action(dev, SD_DEVICE_ADD)) |
95ac5230 LP |
427 | return 0; |
428 | ||
429 | r = sd_device_get_subsystem(dev, &val); | |
430 | if (r < 0) | |
431 | return log_device_debug_errno(dev, r, "Failed to get subsystem: %m"); | |
432 | ||
433 | if (!streq(val, "block")) | |
434 | return 0; | |
435 | ||
436 | r = sd_device_get_sysname(dev, &val); | |
437 | if (r < 0) | |
438 | return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); | |
439 | ||
440 | /* Exclude synthetic devices for now, this is supposed to be a safety feature to avoid modification | |
441 | * of physical devices, and what sits on top of those doesn't really matter if we don't allow the | |
cb713f16 | 442 | * underlying block devices to receive changes. */ |
95ac5230 LP |
443 | if (STARTSWITH_SET(val, "dm-", "md", "drbd", "loop", "nbd", "zram")) |
444 | return 0; | |
445 | ||
446 | r = sd_device_get_devname(dev, &val); | |
447 | if (r == -ENOENT) | |
448 | return 0; | |
449 | if (r < 0) | |
450 | return log_device_debug_errno(dev, r, "Failed to get devname: %m"); | |
451 | ||
452 | fd = open(val, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); | |
453 | if (fd < 0) | |
454 | return log_device_debug_errno(dev, errno, "Failed to open '%s', ignoring: %m", val); | |
455 | ||
456 | if (ioctl(fd, BLKROSET, &state) < 0) | |
457 | return log_device_warning_errno(dev, errno, "Failed to mark block device '%s' read-only: %m", val); | |
458 | ||
459 | log_device_info(dev, "Successfully marked block device '%s' read-only.", val); | |
460 | return 0; | |
461 | } | |
462 | ||
abde5ea8 | 463 | static int worker_process_device(Manager *manager, sd_device *dev) { |
2e088715 | 464 | _cleanup_(udev_event_freep) UdevEvent *udev_event = NULL; |
0bed242c YW |
465 | _cleanup_close_ int fd_lock = -1; |
466 | int r; | |
912541b0 | 467 | |
0bed242c YW |
468 | assert(manager); |
469 | assert(dev); | |
470 | ||
b2d9e58f | 471 | log_device_uevent(dev, "Processing device"); |
abde5ea8 | 472 | |
1a0bd015 | 473 | udev_event = udev_event_new(dev, arg_exec_delay_usec, manager->rtnl, manager->log_level); |
0bed242c YW |
474 | if (!udev_event) |
475 | return -ENOMEM; | |
476 | ||
abde5ea8 | 477 | r = worker_lock_block_device(dev, &fd_lock); |
b8380cc6 LP |
478 | if (r == -EAGAIN) { |
479 | /* So this is a block device and the device is locked currently via the BSD advisory locks — | |
480 | * someone else is exclusively using it. This means we don't run our udev rules now, to not | |
481 | * interfere. However we want to know when the device is unlocked again, and retrigger the | |
482 | * device again then, so that the rules are run eventually. For that we use IN_CLOSE_WRITE | |
483 | * inotify watches (which isn't exactly the same as waiting for the BSD locks to release, but | |
484 | * not totally off, as long as unlock+close() is done together, as it usually is). | |
485 | * | |
486 | * (The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING) | |
487 | * | |
488 | * There's a bit of a chicken and egg problem here for this however: inotify watching is | |
489 | * supposed to be enabled via an option set via udev rules (OPTIONS+="watch"). If we skip the | |
490 | * udev rules here however (as we just said we do), we would thus never see that specific | |
491 | * udev rule, and thus never turn on inotify watching. But in order to catch up eventually | |
492 | * and run them we we need the inotify watching: hence a classic chicken and egg problem. | |
493 | * | |
494 | * Our way out here: if we see the block device locked, unconditionally watch the device via | |
495 | * inotify, regardless of any explicit request via OPTIONS+="watch". Thus, a device that is | |
496 | * currently locked via the BSD file locks will be treated as if we ran a single udev rule | |
497 | * only for it: the one that turns on inotify watching for it. If we eventually see the | |
498 | * inotify IN_CLOSE_WRITE event, and then run the rules after all and we then realize that | |
499 | * this wasn't actually requested (i.e. no OPTIONS+="watch" set) we'll simply turn off the | |
500 | * watching again (see below). Effectively this means: inotify watching is now enabled either | |
501 | * a) when the udev rules say so, or b) while the device is locked. | |
502 | * | |
503 | * Worst case scenario hence: in the (unlikely) case someone locked the device and we clash | |
504 | * with that we might do inotify watching for a brief moment for a device where we actually | |
505 | * weren't supposed to. But that shouldn't be too bad, in particular as BSD locks being taken | |
506 | * on a block device is kinda an indication that the inotify logic is desired too, to some | |
507 | * degree — they go hand-in-hand after all. */ | |
508 | ||
509 | log_device_debug(dev, "Block device is currently locked, installing watch to wait until the lock is released."); | |
df7ee959 | 510 | (void) udev_watch_begin(manager->inotify_fd, dev); |
b8380cc6 LP |
511 | |
512 | /* Now the watch is installed, let's lock the device again, maybe in the meantime things changed */ | |
513 | r = worker_lock_block_device(dev, &fd_lock); | |
514 | } | |
b6aab8ef | 515 | if (r < 0) |
0bed242c | 516 | return r; |
912541b0 | 517 | |
95ac5230 LP |
518 | (void) worker_mark_block_device_read_only(dev); |
519 | ||
0bed242c | 520 | /* apply rules, create node, symlinks */ |
df7ee959 YW |
521 | r = udev_event_execute_rules( |
522 | udev_event, | |
523 | manager->inotify_fd, | |
524 | arg_event_timeout_usec, | |
525 | arg_timeout_signal, | |
526 | manager->properties, | |
527 | manager->rules); | |
99058cd6 YW |
528 | if (r < 0) |
529 | return r; | |
530 | ||
e2099267 | 531 | udev_event_execute_run(udev_event, arg_event_timeout_usec, arg_timeout_signal); |
2dd9f98d | 532 | |
0bed242c YW |
533 | if (!manager->rtnl) |
534 | /* in case rtnl was initialized */ | |
535 | manager->rtnl = sd_netlink_ref(udev_event->rtnl); | |
912541b0 | 536 | |
0d77bc4a YW |
537 | r = udev_event_process_inotify_watch(udev_event, manager->inotify_fd); |
538 | if (r < 0) | |
539 | return r; | |
912541b0 | 540 | |
b2d9e58f | 541 | log_device_uevent(dev, "Device processed"); |
0bed242c YW |
542 | return 0; |
543 | } | |
912541b0 | 544 | |
e2130348 YW |
545 | static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *dev, void *userdata) { |
546 | Manager *manager = userdata; | |
547 | int r; | |
548 | ||
549 | assert(dev); | |
550 | assert(manager); | |
551 | ||
552 | r = worker_process_device(manager, dev); | |
5abee64e LP |
553 | if (r == -EAGAIN) |
554 | /* if we couldn't acquire the flock(), then proceed quietly */ | |
555 | log_device_debug_errno(dev, r, "Device currently locked, not processing."); | |
556 | else { | |
557 | if (r < 0) | |
558 | log_device_warning_errno(dev, r, "Failed to process device, ignoring: %m"); | |
e2130348 | 559 | |
5abee64e LP |
560 | /* send processed event back to libudev listeners */ |
561 | r = device_monitor_send_device(monitor, NULL, dev); | |
562 | if (r < 0) | |
563 | log_device_warning_errno(dev, r, "Failed to send device, ignoring: %m"); | |
564 | } | |
e2130348 YW |
565 | |
566 | /* send udevd the result of the event execution */ | |
567 | r = worker_send_message(manager->worker_watch[WRITE_END]); | |
568 | if (r < 0) | |
569 | log_device_warning_errno(dev, r, "Failed to send signal to main daemon, ignoring: %m"); | |
570 | ||
1a0bd015 | 571 | /* Reset the log level, as it might be changed by "OPTIONS=log_level=". */ |
3cc6b14a | 572 | log_set_max_level(manager->log_level); |
1a0bd015 | 573 | |
e2130348 YW |
574 | return 1; |
575 | } | |
576 | ||
7f2e3a14 YW |
577 | static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device *first_device) { |
578 | _cleanup_(sd_device_unrefp) sd_device *dev = first_device; | |
0bed242c | 579 | _cleanup_(manager_freep) Manager *manager = _manager; |
44dcf454 | 580 | int r; |
145dae7e | 581 | |
0bed242c YW |
582 | assert(manager); |
583 | assert(monitor); | |
584 | assert(dev); | |
912541b0 | 585 | |
44ee03d1 | 586 | assert_se(unsetenv("NOTIFY_SOCKET") == 0); |
3b64e4d4 | 587 | |
e2130348 YW |
588 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, -1) >= 0); |
589 | ||
590 | /* Reset OOM score, we only protect the main daemon. */ | |
591 | r = set_oom_score_adjust(0); | |
592 | if (r < 0) | |
593 | log_debug_errno(r, "Failed to reset OOM score, ignoring: %m"); | |
912541b0 | 594 | |
7802194a | 595 | /* Clear unnecessary data in Manager object. */ |
e2130348 | 596 | manager_clear_for_worker(manager); |
3ebdb81e | 597 | |
e2130348 YW |
598 | r = sd_event_new(&manager->event); |
599 | if (r < 0) | |
600 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
912541b0 | 601 | |
e2130348 YW |
602 | r = sd_event_add_signal(manager->event, NULL, SIGTERM, NULL, NULL); |
603 | if (r < 0) | |
604 | return log_error_errno(r, "Failed to set SIGTERM event: %m"); | |
4c83d994 | 605 | |
e2130348 YW |
606 | r = sd_device_monitor_attach_event(monitor, manager->event); |
607 | if (r < 0) | |
608 | return log_error_errno(r, "Failed to attach event loop to device monitor: %m"); | |
912541b0 | 609 | |
e2130348 | 610 | r = sd_device_monitor_start(monitor, worker_device_monitor_handler, manager); |
0bed242c | 611 | if (r < 0) |
e2130348 | 612 | return log_error_errno(r, "Failed to start device monitor: %m"); |
912541b0 | 613 | |
e2130348 | 614 | (void) sd_event_source_set_description(sd_device_monitor_get_event_source(monitor), "worker-device-monitor"); |
b66f29a1 | 615 | |
e2130348 YW |
616 | /* Process first device */ |
617 | (void) worker_device_monitor_handler(monitor, dev, manager); | |
912541b0 | 618 | |
e2130348 YW |
619 | r = sd_event_loop(manager->event); |
620 | if (r < 0) | |
621 | return log_error_errno(r, "Event loop failed: %m"); | |
622 | ||
44dcf454 | 623 | return 0; |
0bed242c YW |
624 | } |
625 | ||
419ec631 YW |
626 | static int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
627 | Event *event = userdata; | |
628 | ||
629 | assert(event); | |
630 | assert(event->worker); | |
631 | ||
632 | kill_and_sigcont(event->worker->pid, arg_timeout_signal); | |
633 | event->worker->state = WORKER_KILLED; | |
634 | ||
635 | log_device_error(event->dev, "Worker ["PID_FMT"] processing SEQNUM=%"PRIu64" killed", event->worker->pid, event->seqnum); | |
636 | ||
637 | return 1; | |
638 | } | |
639 | ||
640 | static int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) { | |
641 | Event *event = userdata; | |
642 | ||
643 | assert(event); | |
644 | assert(event->worker); | |
645 | ||
646 | log_device_warning(event->dev, "Worker ["PID_FMT"] processing SEQNUM=%"PRIu64" is taking a long time", event->worker->pid, event->seqnum); | |
647 | ||
648 | return 1; | |
649 | } | |
650 | ||
651 | static void worker_attach_event(Worker *worker, Event *event) { | |
652 | sd_event *e; | |
653 | ||
654 | assert(worker); | |
655 | assert(worker->manager); | |
656 | assert(event); | |
657 | assert(!event->worker); | |
658 | assert(!worker->event); | |
659 | ||
660 | worker->state = WORKER_RUNNING; | |
661 | worker->event = event; | |
662 | event->state = EVENT_RUNNING; | |
663 | event->worker = worker; | |
664 | ||
665 | e = worker->manager->event; | |
666 | ||
667 | (void) sd_event_add_time_relative(e, &event->timeout_warning_event, CLOCK_MONOTONIC, | |
668 | udev_warn_timeout(arg_event_timeout_usec), USEC_PER_SEC, | |
669 | on_event_timeout_warning, event); | |
670 | ||
671 | (void) sd_event_add_time_relative(e, &event->timeout_event, CLOCK_MONOTONIC, | |
672 | arg_event_timeout_usec, USEC_PER_SEC, | |
673 | on_event_timeout, event); | |
674 | } | |
675 | ||
d9239923 | 676 | static int worker_spawn(Manager *manager, Event *event) { |
7f2e3a14 | 677 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *worker_monitor = NULL; |
d9239923 | 678 | Worker *worker; |
0bed242c | 679 | pid_t pid; |
7443654e | 680 | int r; |
0bed242c YW |
681 | |
682 | /* listen for new events */ | |
7f2e3a14 YW |
683 | r = device_monitor_new_full(&worker_monitor, MONITOR_GROUP_NONE, -1); |
684 | if (r < 0) | |
685 | return r; | |
7443654e | 686 | |
0bed242c | 687 | /* allow the main daemon netlink address to send devices to the worker */ |
7f2e3a14 YW |
688 | r = device_monitor_allow_unicast_sender(worker_monitor, manager->monitor); |
689 | if (r < 0) | |
690 | return log_error_errno(r, "Worker: Failed to set unicast sender: %m"); | |
691 | ||
692 | r = device_monitor_enable_receiving(worker_monitor); | |
0bed242c | 693 | if (r < 0) |
7f2e3a14 | 694 | return log_error_errno(r, "Worker: Failed to enable receiving of device: %m"); |
0bed242c | 695 | |
ff86c92e | 696 | r = safe_fork(NULL, FORK_DEATHSIG, &pid); |
7443654e YW |
697 | if (r < 0) { |
698 | event->state = EVENT_QUEUED; | |
699 | return log_error_errno(r, "Failed to fork() worker: %m"); | |
700 | } | |
701 | if (r == 0) { | |
b428efa5 MS |
702 | DEVICE_TRACE_POINT(worker_spawned, event->dev, getpid()); |
703 | ||
7443654e | 704 | /* Worker process */ |
eb546b35 | 705 | r = worker_main(manager, worker_monitor, sd_device_ref(event->dev)); |
baa30fbc | 706 | log_close(); |
8b46c3fc | 707 | _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
912541b0 | 708 | } |
e03c7cc2 | 709 | |
7443654e YW |
710 | r = worker_new(&worker, manager, worker_monitor, pid); |
711 | if (r < 0) | |
712 | return log_error_errno(r, "Failed to create worker object: %m"); | |
e03c7cc2 | 713 | |
7443654e | 714 | worker_attach_event(worker, event); |
39c19cf1 | 715 | |
eb546b35 | 716 | log_device_debug(event->dev, "Worker ["PID_FMT"] is forked for processing SEQNUM=%"PRIu64".", pid, event->seqnum); |
7443654e | 717 | return 0; |
7fafc032 KS |
718 | } |
719 | ||
d9239923 | 720 | static void event_run(Manager *manager, Event *event) { |
5406c368 | 721 | static bool log_children_max_reached = true; |
d9239923 | 722 | Worker *worker; |
7f2e3a14 | 723 | int r; |
912541b0 | 724 | |
c0c6806b TG |
725 | assert(manager); |
726 | assert(event); | |
727 | ||
b2d9e58f | 728 | log_device_uevent(event->dev, "Device ready for processing"); |
7600dbb8 | 729 | |
90e74a66 | 730 | HASHMAP_FOREACH(worker, manager->workers) { |
912541b0 KS |
731 | if (worker->state != WORKER_IDLE) |
732 | continue; | |
733 | ||
eb546b35 | 734 | r = device_monitor_send_device(manager->monitor, worker->monitor, event->dev); |
7f2e3a14 | 735 | if (r < 0) { |
eb546b35 | 736 | log_device_error_errno(event->dev, r, "Worker ["PID_FMT"] did not accept message, killing the worker: %m", |
7f2e3a14 | 737 | worker->pid); |
cb542e84 | 738 | (void) kill(worker->pid, SIGKILL); |
912541b0 KS |
739 | worker->state = WORKER_KILLED; |
740 | continue; | |
741 | } | |
39c19cf1 | 742 | worker_attach_event(worker, event); |
912541b0 KS |
743 | return; |
744 | } | |
745 | ||
c0c6806b | 746 | if (hashmap_size(manager->workers) >= arg_children_max) { |
5406c368 FB |
747 | |
748 | /* Avoid spamming the debug logs if the limit is already reached and | |
749 | * many events still need to be processed */ | |
750 | if (log_children_max_reached && arg_children_max > 1) { | |
044497e2 | 751 | log_debug("Maximum number (%u) of children reached.", hashmap_size(manager->workers)); |
5406c368 FB |
752 | log_children_max_reached = false; |
753 | } | |
912541b0 KS |
754 | return; |
755 | } | |
756 | ||
5406c368 FB |
757 | /* Re-enable the debug message for the next batch of events */ |
758 | log_children_max_reached = true; | |
759 | ||
2df2152c CG |
760 | /* fork with up-to-date SELinux label database, so the child inherits the up-to-date db |
761 | and, until the next SELinux policy changes, we safe further reloads in future children */ | |
762 | mac_selinux_maybe_reload(); | |
763 | ||
912541b0 | 764 | /* start new worker and pass initial device */ |
c0c6806b | 765 | worker_spawn(manager, event); |
1e03b754 KS |
766 | } |
767 | ||
e3196993 | 768 | /* lookup event for identical, parent, child device */ |
d9239923 | 769 | static int is_device_busy(Manager *manager, Event *event) { |
eb546b35 YW |
770 | const char *subsystem, *devpath, *devpath_old = NULL; |
771 | dev_t devnum = makedev(0, 0); | |
d9239923 | 772 | Event *loop_event; |
eb546b35 YW |
773 | size_t devpath_len; |
774 | int r, ifindex = 0; | |
775 | bool is_block; | |
776 | ||
777 | r = sd_device_get_subsystem(event->dev, &subsystem); | |
778 | if (r < 0) | |
779 | return r; | |
780 | ||
781 | is_block = streq(subsystem, "block"); | |
782 | ||
783 | r = sd_device_get_devpath(event->dev, &devpath); | |
784 | if (r < 0) | |
785 | return r; | |
786 | ||
787 | devpath_len = strlen(devpath); | |
788 | ||
789 | r = sd_device_get_property_value(event->dev, "DEVPATH_OLD", &devpath_old); | |
790 | if (r < 0 && r != -ENOENT) | |
791 | return r; | |
792 | ||
793 | r = sd_device_get_devnum(event->dev, &devnum); | |
794 | if (r < 0 && r != -ENOENT) | |
795 | return r; | |
796 | ||
797 | r = sd_device_get_ifindex(event->dev, &ifindex); | |
798 | if (r < 0 && r != -ENOENT) | |
799 | return r; | |
912541b0 KS |
800 | |
801 | /* check if queue contains events we depend on */ | |
40a57716 | 802 | LIST_FOREACH(event, loop_event, manager->events) { |
eb546b35 YW |
803 | size_t loop_devpath_len, common; |
804 | const char *loop_devpath; | |
805 | ||
87ac8d99 | 806 | /* we already found a later event, earlier cannot block us, no need to check again */ |
912541b0 KS |
807 | if (loop_event->seqnum < event->delaying_seqnum) |
808 | continue; | |
809 | ||
810 | /* event we checked earlier still exists, no need to check again */ | |
811 | if (loop_event->seqnum == event->delaying_seqnum) | |
812 | return true; | |
813 | ||
814 | /* found ourself, no later event can block us */ | |
815 | if (loop_event->seqnum >= event->seqnum) | |
816 | break; | |
817 | ||
818 | /* check major/minor */ | |
eb546b35 YW |
819 | if (major(devnum) != 0) { |
820 | const char *s; | |
821 | dev_t d; | |
822 | ||
823 | if (sd_device_get_subsystem(loop_event->dev, &s) < 0) | |
824 | continue; | |
825 | ||
826 | if (sd_device_get_devnum(loop_event->dev, &d) >= 0 && | |
827 | devnum == d && is_block == streq(s, "block")) | |
86714116 | 828 | goto set_delaying_seqnum; |
eb546b35 | 829 | } |
912541b0 KS |
830 | |
831 | /* check network device ifindex */ | |
eb546b35 YW |
832 | if (ifindex > 0) { |
833 | int i; | |
834 | ||
835 | if (sd_device_get_ifindex(loop_event->dev, &i) >= 0 && | |
836 | ifindex == i) | |
86714116 | 837 | goto set_delaying_seqnum; |
eb546b35 YW |
838 | } |
839 | ||
840 | if (sd_device_get_devpath(loop_event->dev, &loop_devpath) < 0) | |
841 | continue; | |
912541b0 KS |
842 | |
843 | /* check our old name */ | |
0bd0407e YW |
844 | if (devpath_old && streq(devpath_old, loop_devpath)) |
845 | goto set_delaying_seqnum; | |
912541b0 | 846 | |
eb546b35 YW |
847 | loop_devpath_len = strlen(loop_devpath); |
848 | ||
912541b0 | 849 | /* compare devpath */ |
eb546b35 | 850 | common = MIN(devpath_len, loop_devpath_len); |
912541b0 KS |
851 | |
852 | /* one devpath is contained in the other? */ | |
eb546b35 | 853 | if (!strneq(devpath, loop_devpath, common)) |
912541b0 KS |
854 | continue; |
855 | ||
856 | /* identical device event found */ | |
baa461fc | 857 | if (devpath_len == loop_devpath_len) |
0bd0407e | 858 | goto set_delaying_seqnum; |
912541b0 KS |
859 | |
860 | /* parent device event found */ | |
0bd0407e YW |
861 | if (devpath[common] == '/') |
862 | goto set_delaying_seqnum; | |
912541b0 KS |
863 | |
864 | /* child device event found */ | |
0bd0407e YW |
865 | if (loop_devpath[common] == '/') |
866 | goto set_delaying_seqnum; | |
912541b0 KS |
867 | } |
868 | ||
869 | return false; | |
0bd0407e YW |
870 | |
871 | set_delaying_seqnum: | |
7600dbb8 UKK |
872 | log_device_debug(event->dev, "SEQNUM=%" PRIu64 " blocked by SEQNUM=%" PRIu64, |
873 | event->seqnum, loop_event->seqnum); | |
874 | ||
0bd0407e YW |
875 | event->delaying_seqnum = loop_event->seqnum; |
876 | return true; | |
7fafc032 KS |
877 | } |
878 | ||
c0c6806b | 879 | static void event_queue_start(Manager *manager) { |
d9239923 | 880 | Event *event; |
693d371d | 881 | usec_t usec; |
0725c4b9 | 882 | int r; |
8ab44e3f | 883 | |
c0c6806b TG |
884 | assert(manager); |
885 | ||
40a57716 | 886 | if (LIST_IS_EMPTY(manager->events) || |
7c4c7e89 TG |
887 | manager->exit || manager->stop_exec_queue) |
888 | return; | |
889 | ||
3285baa8 | 890 | assert_se(sd_event_now(manager->event, CLOCK_MONOTONIC, &usec) >= 0); |
38a03f06 LP |
891 | /* check for changed config, every 3 seconds at most */ |
892 | if (manager->last_usec == 0 || | |
33ad742a | 893 | usec - manager->last_usec > 3 * USEC_PER_SEC) { |
38a03f06 | 894 | if (udev_rules_check_timestamp(manager->rules) || |
2024ed61 | 895 | udev_builtin_validate()) |
38a03f06 | 896 | manager_reload(manager); |
693d371d | 897 | |
38a03f06 | 898 | manager->last_usec = usec; |
7c4c7e89 TG |
899 | } |
900 | ||
0725c4b9 YW |
901 | r = event_source_disable(manager->kill_workers_event); |
902 | if (r < 0) | |
903 | log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m"); | |
eca195ec | 904 | |
2024ed61 | 905 | udev_builtin_init(); |
7c4c7e89 TG |
906 | |
907 | if (!manager->rules) { | |
c238a1f5 | 908 | r = udev_rules_load(&manager->rules, arg_resolve_name_timing); |
1d791281 ZJS |
909 | if (r < 0) { |
910 | log_warning_errno(r, "Failed to read udev rules: %m"); | |
7c4c7e89 | 911 | return; |
1d791281 | 912 | } |
7c4c7e89 TG |
913 | } |
914 | ||
eb546b35 | 915 | LIST_FOREACH(event, event, manager->events) { |
912541b0 KS |
916 | if (event->state != EVENT_QUEUED) |
917 | continue; | |
0bc74ea7 | 918 | |
912541b0 | 919 | /* do not start event if parent or child event is still running */ |
eb546b35 | 920 | if (is_device_busy(manager, event) != 0) |
912541b0 | 921 | continue; |
fc465079 | 922 | |
c0c6806b | 923 | event_run(manager, event); |
912541b0 | 924 | } |
1e03b754 KS |
925 | } |
926 | ||
419ec631 YW |
927 | static int event_queue_insert(Manager *manager, sd_device *dev) { |
928 | _cleanup_(sd_device_unrefp) sd_device *clone = NULL; | |
929 | Event *event; | |
930 | uint64_t seqnum; | |
931 | int r; | |
ff2c503d | 932 | |
419ec631 YW |
933 | assert(manager); |
934 | assert(dev); | |
ff2c503d | 935 | |
419ec631 YW |
936 | /* only one process can add events to the queue */ |
937 | assert(manager->pid == getpid_cached()); | |
938 | ||
939 | /* We only accepts devices received by device monitor. */ | |
940 | r = sd_device_get_seqnum(dev, &seqnum); | |
941 | if (r < 0) | |
942 | return r; | |
943 | ||
944 | /* Save original device to restore the state on failures. */ | |
945 | r = device_shallow_clone(dev, &clone); | |
946 | if (r < 0) | |
947 | return r; | |
948 | ||
949 | r = device_copy_properties(clone, dev); | |
950 | if (r < 0) | |
951 | return r; | |
952 | ||
953 | event = new(Event, 1); | |
954 | if (!event) | |
955 | return -ENOMEM; | |
956 | ||
957 | *event = (Event) { | |
958 | .manager = manager, | |
959 | .dev = sd_device_ref(dev), | |
960 | .dev_kernel = TAKE_PTR(clone), | |
961 | .seqnum = seqnum, | |
962 | .state = EVENT_QUEUED, | |
963 | }; | |
964 | ||
965 | if (LIST_IS_EMPTY(manager->events)) { | |
966 | r = touch("/run/udev/queue"); | |
967 | if (r < 0) | |
6be97d67 | 968 | log_warning_errno(r, "Failed to touch /run/udev/queue, ignoring: %m"); |
912541b0 | 969 | } |
419ec631 YW |
970 | |
971 | LIST_APPEND(event, manager->events, event); | |
972 | ||
973 | log_device_uevent(dev, "Device is queued"); | |
974 | ||
975 | return 0; | |
976 | } | |
977 | ||
978 | static int on_uevent(sd_device_monitor *monitor, sd_device *dev, void *userdata) { | |
979 | Manager *manager = userdata; | |
980 | int r; | |
981 | ||
982 | assert(manager); | |
983 | ||
984 | DEVICE_TRACE_POINT(kernel_uevent_received, dev); | |
985 | ||
986 | device_ensure_usec_initialized(dev, NULL); | |
987 | ||
988 | r = event_queue_insert(manager, dev); | |
989 | if (r < 0) { | |
990 | log_device_error_errno(dev, r, "Failed to insert device into event queue: %m"); | |
991 | return 1; | |
992 | } | |
993 | ||
994 | /* we have fresh events, try to schedule them */ | |
995 | event_queue_start(manager); | |
996 | ||
997 | return 1; | |
ff2c503d KS |
998 | } |
999 | ||
e82e8fa5 | 1000 | static int on_worker(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
c0c6806b TG |
1001 | Manager *manager = userdata; |
1002 | ||
1003 | assert(manager); | |
1004 | ||
912541b0 | 1005 | for (;;) { |
d9239923 | 1006 | WorkerMessage msg; |
979558f3 TG |
1007 | struct iovec iovec = { |
1008 | .iov_base = &msg, | |
1009 | .iov_len = sizeof(msg), | |
1010 | }; | |
fb29cdbe | 1011 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control; |
979558f3 TG |
1012 | struct msghdr msghdr = { |
1013 | .msg_iov = &iovec, | |
1014 | .msg_iovlen = 1, | |
1015 | .msg_control = &control, | |
1016 | .msg_controllen = sizeof(control), | |
1017 | }; | |
912541b0 | 1018 | ssize_t size; |
371d72e0 | 1019 | struct ucred *ucred; |
d9239923 | 1020 | Worker *worker; |
912541b0 | 1021 | |
3691bcf3 LP |
1022 | size = recvmsg_safe(fd, &msghdr, MSG_DONTWAIT); |
1023 | if (size == -EINTR) | |
1024 | continue; | |
1025 | if (size == -EAGAIN) | |
1026 | /* nothing more to read */ | |
1027 | break; | |
1028 | if (size < 0) | |
1029 | return log_error_errno(size, "Failed to receive message: %m"); | |
1030 | ||
1031 | cmsg_close_all(&msghdr); | |
979558f3 | 1032 | |
d9239923 | 1033 | if (size != sizeof(WorkerMessage)) { |
d4e98880 | 1034 | log_warning("Ignoring worker message with invalid size %zi bytes", size); |
e82e8fa5 | 1035 | continue; |
979558f3 TG |
1036 | } |
1037 | ||
371d72e0 | 1038 | ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred); |
979558f3 | 1039 | if (!ucred || ucred->pid <= 0) { |
d4e98880 | 1040 | log_warning("Ignoring worker message without valid PID"); |
979558f3 TG |
1041 | continue; |
1042 | } | |
912541b0 KS |
1043 | |
1044 | /* lookup worker who sent the signal */ | |
4a0b58c4 | 1045 | worker = hashmap_get(manager->workers, PID_TO_PTR(ucred->pid)); |
a505965d | 1046 | if (!worker) { |
044497e2 | 1047 | log_debug("Worker ["PID_FMT"] returned, but is no longer tracked", ucred->pid); |
a505965d | 1048 | continue; |
912541b0 | 1049 | } |
c0bbfd72 | 1050 | |
f257a8fc YG |
1051 | if (worker->state == WORKER_KILLING) { |
1052 | worker->state = WORKER_KILLED; | |
1053 | (void) kill(worker->pid, SIGTERM); | |
1054 | } else if (worker->state != WORKER_KILLED) | |
a505965d TG |
1055 | worker->state = WORKER_IDLE; |
1056 | ||
1057 | /* worker returned */ | |
1058 | event_free(worker->event); | |
912541b0 | 1059 | } |
e82e8fa5 | 1060 | |
8302fe5a TG |
1061 | /* we have free workers, try to schedule events */ |
1062 | event_queue_start(manager); | |
1063 | ||
e82e8fa5 TG |
1064 | return 1; |
1065 | } | |
1066 | ||
3b47c739 | 1067 | /* receive the udevd message from userspace */ |
e0d61dac | 1068 | static int on_ctrl_msg(UdevCtrl *uctrl, UdevCtrlMessageType type, const UdevCtrlMessageValue *value, void *userdata) { |
c0c6806b | 1069 | Manager *manager = userdata; |
d02c6f54 | 1070 | int r; |
912541b0 | 1071 | |
d02c6f54 | 1072 | assert(value); |
c0c6806b | 1073 | assert(manager); |
e4f66b77 | 1074 | |
d02c6f54 YW |
1075 | switch (type) { |
1076 | case UDEV_CTRL_SET_LOG_LEVEL: | |
64a3494c | 1077 | log_debug("Received udev control message (SET_LOG_LEVEL), setting log_level=%i", value->intval); |
3cc6b14a | 1078 | log_set_max_level(value->intval); |
1a0bd015 | 1079 | manager->log_level = value->intval; |
f257a8fc | 1080 | manager_kill_workers(manager, false); |
d02c6f54 YW |
1081 | break; |
1082 | case UDEV_CTRL_STOP_EXEC_QUEUE: | |
044497e2 | 1083 | log_debug("Received udev control message (STOP_EXEC_QUEUE)"); |
c0c6806b | 1084 | manager->stop_exec_queue = true; |
d02c6f54 YW |
1085 | break; |
1086 | case UDEV_CTRL_START_EXEC_QUEUE: | |
044497e2 | 1087 | log_debug("Received udev control message (START_EXEC_QUEUE)"); |
c0c6806b | 1088 | manager->stop_exec_queue = false; |
8302fe5a | 1089 | event_queue_start(manager); |
d02c6f54 YW |
1090 | break; |
1091 | case UDEV_CTRL_RELOAD: | |
044497e2 | 1092 | log_debug("Received udev control message (RELOAD)"); |
62d43dac | 1093 | manager_reload(manager); |
d02c6f54 YW |
1094 | break; |
1095 | case UDEV_CTRL_SET_ENV: { | |
9b5150b6 | 1096 | _cleanup_free_ char *key = NULL, *val = NULL, *old_key = NULL, *old_val = NULL; |
d02c6f54 | 1097 | const char *eq; |
9b5150b6 | 1098 | |
d02c6f54 | 1099 | eq = strchr(value->buf, '='); |
9b5150b6 | 1100 | if (!eq) { |
d02c6f54 | 1101 | log_error("Invalid key format '%s'", value->buf); |
9b5150b6 YW |
1102 | return 1; |
1103 | } | |
1104 | ||
d02c6f54 | 1105 | key = strndup(value->buf, eq - value->buf); |
9b5150b6 YW |
1106 | if (!key) { |
1107 | log_oom(); | |
1108 | return 1; | |
1109 | } | |
1110 | ||
1111 | old_val = hashmap_remove2(manager->properties, key, (void **) &old_key); | |
1112 | ||
1113 | r = hashmap_ensure_allocated(&manager->properties, &string_hash_ops); | |
1114 | if (r < 0) { | |
1115 | log_oom(); | |
1116 | return 1; | |
912541b0 | 1117 | } |
9b5150b6 YW |
1118 | |
1119 | eq++; | |
f053fc33 | 1120 | if (isempty(eq)) { |
044497e2 | 1121 | log_debug("Received udev control message (ENV), unsetting '%s'", key); |
9b5150b6 YW |
1122 | |
1123 | r = hashmap_put(manager->properties, key, NULL); | |
1124 | if (r < 0) { | |
1125 | log_oom(); | |
1126 | return 1; | |
1127 | } | |
1128 | } else { | |
1129 | val = strdup(eq); | |
1130 | if (!val) { | |
1131 | log_oom(); | |
1132 | return 1; | |
1133 | } | |
1134 | ||
044497e2 | 1135 | log_debug("Received udev control message (ENV), setting '%s=%s'", key, val); |
9b5150b6 YW |
1136 | |
1137 | r = hashmap_put(manager->properties, key, val); | |
1138 | if (r < 0) { | |
1139 | log_oom(); | |
1140 | return 1; | |
1141 | } | |
1142 | } | |
1143 | ||
1144 | key = val = NULL; | |
f257a8fc | 1145 | manager_kill_workers(manager, false); |
d02c6f54 | 1146 | break; |
912541b0 | 1147 | } |
d02c6f54 YW |
1148 | case UDEV_CTRL_SET_CHILDREN_MAX: |
1149 | if (value->intval <= 0) { | |
1150 | log_debug("Received invalid udev control message (SET_MAX_CHILDREN, %i), ignoring.", value->intval); | |
1151 | return 0; | |
1152 | } | |
912541b0 | 1153 | |
d02c6f54 YW |
1154 | log_debug("Received udev control message (SET_MAX_CHILDREN), setting children_max=%i", value->intval); |
1155 | arg_children_max = value->intval; | |
1ef72b55 MS |
1156 | |
1157 | (void) sd_notifyf(false, | |
1158 | "READY=1\n" | |
1159 | "STATUS=Processing with %u children at max", arg_children_max); | |
d02c6f54 YW |
1160 | break; |
1161 | case UDEV_CTRL_PING: | |
d30f43ee | 1162 | log_debug("Received udev control message (PING)"); |
d02c6f54 YW |
1163 | break; |
1164 | case UDEV_CTRL_EXIT: | |
044497e2 | 1165 | log_debug("Received udev control message (EXIT)"); |
62d43dac | 1166 | manager_exit(manager); |
d02c6f54 YW |
1167 | break; |
1168 | default: | |
1169 | log_debug("Received unknown udev control message, ignoring"); | |
912541b0 | 1170 | } |
e4f66b77 | 1171 | |
e82e8fa5 | 1172 | return 1; |
88f4b648 | 1173 | } |
4a231017 | 1174 | |
25677a05 | 1175 | static int synthesize_change_one(sd_device *dev, sd_device *target) { |
0584b17a ZJS |
1176 | int r; |
1177 | ||
25677a05 YW |
1178 | if (DEBUG_LOGGING) { |
1179 | const char *syspath = NULL; | |
1180 | (void) sd_device_get_syspath(target, &syspath); | |
1181 | log_device_debug(dev, "device is closed, synthesising 'change' on %s", strna(syspath)); | |
1182 | } | |
1183 | ||
1184 | r = sd_device_trigger(target, SD_DEVICE_CHANGE); | |
0584b17a | 1185 | if (r < 0) |
25677a05 YW |
1186 | return log_device_debug_errno(target, r, "Failed to trigger 'change' uevent: %m"); |
1187 | ||
b428efa5 MS |
1188 | DEVICE_TRACE_POINT(synthetic_change_event, dev); |
1189 | ||
0584b17a ZJS |
1190 | return 0; |
1191 | } | |
1192 | ||
70068602 | 1193 | static int synthesize_change(sd_device *dev) { |
25677a05 | 1194 | const char *subsystem, *sysname, *devtype; |
f3a740a5 | 1195 | int r; |
edd32000 | 1196 | |
70068602 YW |
1197 | r = sd_device_get_subsystem(dev, &subsystem); |
1198 | if (r < 0) | |
1199 | return r; | |
1200 | ||
25677a05 | 1201 | r = sd_device_get_devtype(dev, &devtype); |
70068602 YW |
1202 | if (r < 0) |
1203 | return r; | |
1204 | ||
25677a05 | 1205 | r = sd_device_get_sysname(dev, &sysname); |
70068602 YW |
1206 | if (r < 0) |
1207 | return r; | |
1208 | ||
25677a05 YW |
1209 | if (streq_ptr(subsystem, "block") && |
1210 | streq_ptr(devtype, "disk") && | |
70068602 YW |
1211 | !startswith(sysname, "dm-")) { |
1212 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
1213 | bool part_table_read = false, has_partitions = false; | |
25677a05 | 1214 | const char *devname; |
70068602 | 1215 | sd_device *d; |
ede34445 | 1216 | int fd; |
f3a740a5 | 1217 | |
25677a05 YW |
1218 | r = sd_device_get_devname(dev, &devname); |
1219 | if (r < 0) | |
1220 | return r; | |
1221 | ||
1222 | /* Try to re-read the partition table. This only succeeds if none of the devices is | |
1223 | * busy. The kernel returns 0 if no partition table is found, and we will not get an | |
1224 | * event for the disk. */ | |
70068602 | 1225 | fd = open(devname, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK); |
ede34445 | 1226 | if (fd >= 0) { |
02ba8fb3 KS |
1227 | r = flock(fd, LOCK_EX|LOCK_NB); |
1228 | if (r >= 0) | |
1229 | r = ioctl(fd, BLKRRPART, 0); | |
1230 | ||
ede34445 KS |
1231 | close(fd); |
1232 | if (r >= 0) | |
e9fc29f4 | 1233 | part_table_read = true; |
ede34445 KS |
1234 | } |
1235 | ||
e9fc29f4 | 1236 | /* search for partitions */ |
70068602 | 1237 | r = sd_device_enumerator_new(&e); |
f3a740a5 KS |
1238 | if (r < 0) |
1239 | return r; | |
1240 | ||
70068602 | 1241 | r = sd_device_enumerator_allow_uninitialized(e); |
f3a740a5 KS |
1242 | if (r < 0) |
1243 | return r; | |
1244 | ||
70068602 | 1245 | r = sd_device_enumerator_add_match_parent(e, dev); |
47a3fa0f TA |
1246 | if (r < 0) |
1247 | return r; | |
e9fc29f4 | 1248 | |
70068602 YW |
1249 | r = sd_device_enumerator_add_match_subsystem(e, "block", true); |
1250 | if (r < 0) | |
1251 | return r; | |
e9fc29f4 | 1252 | |
70068602 YW |
1253 | FOREACH_DEVICE(e, d) { |
1254 | const char *t; | |
e9fc29f4 | 1255 | |
25677a05 | 1256 | if (sd_device_get_devtype(d, &t) < 0 || !streq(t, "partition")) |
e9fc29f4 KS |
1257 | continue; |
1258 | ||
1259 | has_partitions = true; | |
1260 | break; | |
1261 | } | |
1262 | ||
25677a05 YW |
1263 | /* We have partitions and re-read the table, the kernel already sent out a "change" |
1264 | * event for the disk, and "remove/add" for all partitions. */ | |
e9fc29f4 KS |
1265 | if (part_table_read && has_partitions) |
1266 | return 0; | |
1267 | ||
25677a05 YW |
1268 | /* We have partitions but re-reading the partition table did not work, synthesize |
1269 | * "change" for the disk and all partitions. */ | |
1270 | (void) synthesize_change_one(dev, dev); | |
e9fc29f4 | 1271 | |
70068602 | 1272 | FOREACH_DEVICE(e, d) { |
25677a05 | 1273 | const char *t; |
f3a740a5 | 1274 | |
25677a05 | 1275 | if (sd_device_get_devtype(d, &t) < 0 || !streq(t, "partition")) |
f3a740a5 KS |
1276 | continue; |
1277 | ||
25677a05 | 1278 | (void) synthesize_change_one(dev, d); |
f3a740a5 | 1279 | } |
ede34445 | 1280 | |
0584b17a | 1281 | } else |
25677a05 | 1282 | (void) synthesize_change_one(dev, dev); |
ede34445 | 1283 | |
f3a740a5 | 1284 | return 0; |
edd32000 KS |
1285 | } |
1286 | ||
e82e8fa5 | 1287 | static int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
c0c6806b | 1288 | Manager *manager = userdata; |
0254e944 | 1289 | union inotify_event_buffer buffer; |
f7c1ad4f LP |
1290 | struct inotify_event *e; |
1291 | ssize_t l; | |
0725c4b9 | 1292 | int r; |
912541b0 | 1293 | |
c0c6806b | 1294 | assert(manager); |
e82e8fa5 | 1295 | |
0725c4b9 YW |
1296 | r = event_source_disable(manager->kill_workers_event); |
1297 | if (r < 0) | |
1298 | log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m"); | |
eca195ec | 1299 | |
e82e8fa5 | 1300 | l = read(fd, &buffer, sizeof(buffer)); |
f7c1ad4f | 1301 | if (l < 0) { |
3742095b | 1302 | if (IN_SET(errno, EAGAIN, EINTR)) |
e82e8fa5 | 1303 | return 1; |
912541b0 | 1304 | |
f7c1ad4f | 1305 | return log_error_errno(errno, "Failed to read inotify fd: %m"); |
912541b0 KS |
1306 | } |
1307 | ||
f7c1ad4f | 1308 | FOREACH_INOTIFY_EVENT(e, buffer, l) { |
70068602 YW |
1309 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
1310 | const char *devnode; | |
1311 | ||
e7f781e4 YW |
1312 | r = device_new_from_watch_handle(&dev, e->wd); |
1313 | if (r < 0) { | |
1314 | log_debug_errno(r, "Failed to create sd_device object from watch handle, ignoring: %m"); | |
70068602 | 1315 | continue; |
e7f781e4 | 1316 | } |
912541b0 | 1317 | |
70068602 | 1318 | if (sd_device_get_devname(dev, &devnode) < 0) |
edd32000 | 1319 | continue; |
912541b0 | 1320 | |
7fe3324c | 1321 | log_device_debug(dev, "Inotify event: %x for %s", e->mask, devnode); |
da143134 | 1322 | if (e->mask & IN_CLOSE_WRITE) |
e7f781e4 YW |
1323 | (void) synthesize_change(dev); |
1324 | ||
1325 | /* Do not handle IN_IGNORED here. It should be handled by worker in 'remove' uevent; | |
1326 | * udev_event_execute_rules() -> event_execute_rules_on_remove() -> udev_watch_end(). */ | |
912541b0 KS |
1327 | } |
1328 | ||
e82e8fa5 | 1329 | return 1; |
bd284db1 SJR |
1330 | } |
1331 | ||
0561329d | 1332 | static int on_sigterm(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
c0c6806b TG |
1333 | Manager *manager = userdata; |
1334 | ||
1335 | assert(manager); | |
1336 | ||
62d43dac | 1337 | manager_exit(manager); |
912541b0 | 1338 | |
e82e8fa5 TG |
1339 | return 1; |
1340 | } | |
912541b0 | 1341 | |
0561329d | 1342 | static int on_sighup(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
c0c6806b TG |
1343 | Manager *manager = userdata; |
1344 | ||
1345 | assert(manager); | |
1346 | ||
62d43dac | 1347 | manager_reload(manager); |
912541b0 | 1348 | |
e82e8fa5 TG |
1349 | return 1; |
1350 | } | |
912541b0 | 1351 | |
e82e8fa5 | 1352 | static int on_sigchld(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
c0c6806b | 1353 | Manager *manager = userdata; |
0725c4b9 | 1354 | int r; |
c0c6806b TG |
1355 | |
1356 | assert(manager); | |
1357 | ||
e82e8fa5 TG |
1358 | for (;;) { |
1359 | pid_t pid; | |
1360 | int status; | |
d9239923 | 1361 | Worker *worker; |
d1317d02 | 1362 | |
e82e8fa5 TG |
1363 | pid = waitpid(-1, &status, WNOHANG); |
1364 | if (pid <= 0) | |
f29328d6 | 1365 | break; |
e82e8fa5 | 1366 | |
4a0b58c4 | 1367 | worker = hashmap_get(manager->workers, PID_TO_PTR(pid)); |
e82e8fa5 | 1368 | if (!worker) { |
dc7faf2a | 1369 | log_warning("Worker ["PID_FMT"] is unknown, ignoring", pid); |
f29328d6 | 1370 | continue; |
912541b0 | 1371 | } |
e82e8fa5 TG |
1372 | |
1373 | if (WIFEXITED(status)) { | |
1374 | if (WEXITSTATUS(status) == 0) | |
dc7faf2a | 1375 | log_debug("Worker ["PID_FMT"] exited", pid); |
e82e8fa5 | 1376 | else |
dc7faf2a | 1377 | log_warning("Worker ["PID_FMT"] exited with return code %i", pid, WEXITSTATUS(status)); |
33ad742a | 1378 | } else if (WIFSIGNALED(status)) |
dc7faf2a | 1379 | log_warning("Worker ["PID_FMT"] terminated by signal %i (%s)", pid, WTERMSIG(status), signal_to_string(WTERMSIG(status))); |
33ad742a | 1380 | else if (WIFSTOPPED(status)) { |
dc7faf2a | 1381 | log_info("Worker ["PID_FMT"] stopped", pid); |
f29328d6 | 1382 | continue; |
e82e8fa5 | 1383 | } else if (WIFCONTINUED(status)) { |
dc7faf2a | 1384 | log_info("Worker ["PID_FMT"] continued", pid); |
f29328d6 | 1385 | continue; |
e82e8fa5 | 1386 | } else |
dc7faf2a | 1387 | log_warning("Worker ["PID_FMT"] exit with status 0x%04x", pid, status); |
e82e8fa5 | 1388 | |
05e6d9c6 | 1389 | if ((!WIFEXITED(status) || WEXITSTATUS(status) != 0) && worker->event) { |
eb546b35 | 1390 | log_device_error(worker->event->dev, "Worker ["PID_FMT"] failed", pid); |
dc7faf2a | 1391 | |
05e6d9c6 | 1392 | /* delete state from disk */ |
eb546b35 YW |
1393 | device_delete_db(worker->event->dev); |
1394 | device_tag_index(worker->event->dev, NULL, false); | |
dc7faf2a | 1395 | |
030f4571 MW |
1396 | if (manager->monitor) { |
1397 | /* forward kernel event without amending it */ | |
1398 | r = device_monitor_send_device(manager->monitor, NULL, worker->event->dev_kernel); | |
1399 | if (r < 0) | |
1400 | log_device_error_errno(worker->event->dev_kernel, r, "Failed to send back device to kernel: %m"); | |
1401 | } | |
e82e8fa5 TG |
1402 | } |
1403 | ||
1404 | worker_free(worker); | |
912541b0 | 1405 | } |
e82e8fa5 | 1406 | |
8302fe5a TG |
1407 | /* we can start new workers, try to schedule events */ |
1408 | event_queue_start(manager); | |
1409 | ||
eca195ec | 1410 | /* Disable unnecessary cleanup event */ |
0725c4b9 YW |
1411 | if (hashmap_isempty(manager->workers)) { |
1412 | r = event_source_disable(manager->kill_workers_event); | |
1413 | if (r < 0) | |
1414 | log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m"); | |
1415 | } | |
eca195ec | 1416 | |
e82e8fa5 | 1417 | return 1; |
f27125f9 | 1418 | } |
1419 | ||
693d371d TG |
1420 | static int on_post(sd_event_source *s, void *userdata) { |
1421 | Manager *manager = userdata; | |
693d371d TG |
1422 | |
1423 | assert(manager); | |
1424 | ||
b6107f01 YW |
1425 | if (!LIST_IS_EMPTY(manager->events)) |
1426 | return 1; | |
1427 | ||
1428 | /* There are no pending events. Let's cleanup idle process. */ | |
1429 | ||
1430 | if (!hashmap_isempty(manager->workers)) { | |
1431 | /* There are idle workers */ | |
6d63048a YW |
1432 | (void) event_reset_time(manager->event, &manager->kill_workers_event, CLOCK_MONOTONIC, |
1433 | now(CLOCK_MONOTONIC) + 3 * USEC_PER_SEC, USEC_PER_SEC, | |
1434 | on_kill_workers_event, manager, 0, "kill-workers-event", false); | |
b6107f01 | 1435 | return 1; |
693d371d TG |
1436 | } |
1437 | ||
b6107f01 YW |
1438 | /* There are no idle workers. */ |
1439 | ||
1440 | if (manager->exit) | |
1441 | return sd_event_exit(manager->event, 0); | |
1442 | ||
1443 | if (manager->cgroup) | |
1444 | /* cleanup possible left-over processes in our cgroup */ | |
1445 | (void) cg_kill(SYSTEMD_CGROUP_CONTROLLER, manager->cgroup, SIGKILL, CGROUP_IGNORE_SELF, NULL, NULL, NULL); | |
1446 | ||
693d371d TG |
1447 | return 1; |
1448 | } | |
1449 | ||
c4b69e99 | 1450 | static int listen_fds(int *ret_ctrl, int *ret_netlink) { |
fcff1e72 | 1451 | int ctrl_fd = -1, netlink_fd = -1; |
c4b69e99 | 1452 | int fd, n; |
912541b0 | 1453 | |
c4b69e99 YW |
1454 | assert(ret_ctrl); |
1455 | assert(ret_netlink); | |
fcff1e72 | 1456 | |
912541b0 | 1457 | n = sd_listen_fds(true); |
fcff1e72 TG |
1458 | if (n < 0) |
1459 | return n; | |
912541b0 KS |
1460 | |
1461 | for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) { | |
c52cff07 | 1462 | if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1) > 0) { |
fcff1e72 TG |
1463 | if (ctrl_fd >= 0) |
1464 | return -EINVAL; | |
1465 | ctrl_fd = fd; | |
912541b0 KS |
1466 | continue; |
1467 | } | |
1468 | ||
c52cff07 | 1469 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { |
fcff1e72 TG |
1470 | if (netlink_fd >= 0) |
1471 | return -EINVAL; | |
1472 | netlink_fd = fd; | |
912541b0 KS |
1473 | continue; |
1474 | } | |
1475 | ||
fcff1e72 | 1476 | return -EINVAL; |
912541b0 KS |
1477 | } |
1478 | ||
c4b69e99 YW |
1479 | *ret_ctrl = ctrl_fd; |
1480 | *ret_netlink = netlink_fd; | |
912541b0 | 1481 | |
912541b0 | 1482 | return 0; |
7459bcdc KS |
1483 | } |
1484 | ||
e6f86cac | 1485 | /* |
3f85ef0f | 1486 | * read the kernel command line, in case we need to get into debug mode |
64a3494c | 1487 | * udev.log_level=<level> syslog priority |
1d84ad94 LP |
1488 | * udev.children_max=<number of workers> events are fully serialized if set to 1 |
1489 | * udev.exec_delay=<number of seconds> delay execution of every executed program | |
1490 | * udev.event_timeout=<number of seconds> seconds to wait before terminating an event | |
95ac5230 | 1491 | * udev.blockdev_read_only<=bool> mark all block devices read-only when they appear |
e6f86cac | 1492 | */ |
96287a49 | 1493 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
95ac5230 | 1494 | int r; |
e6f86cac | 1495 | |
614a823c | 1496 | assert(key); |
e6f86cac | 1497 | |
64a3494c FB |
1498 | if (proc_cmdline_key_streq(key, "udev.log_level") || |
1499 | proc_cmdline_key_streq(key, "udev.log_priority")) { /* kept for backward compatibility */ | |
1d84ad94 LP |
1500 | |
1501 | if (proc_cmdline_value_missing(key, value)) | |
1502 | return 0; | |
1503 | ||
46f0fbd8 | 1504 | r = log_level_from_string(value); |
92e72467 ZJS |
1505 | if (r >= 0) |
1506 | log_set_max_level(r); | |
1d84ad94 LP |
1507 | |
1508 | } else if (proc_cmdline_key_streq(key, "udev.event_timeout")) { | |
1509 | ||
1510 | if (proc_cmdline_value_missing(key, value)) | |
1511 | return 0; | |
1512 | ||
9d9264ba | 1513 | r = parse_sec(value, &arg_event_timeout_usec); |
1d84ad94 LP |
1514 | |
1515 | } else if (proc_cmdline_key_streq(key, "udev.children_max")) { | |
1516 | ||
1517 | if (proc_cmdline_value_missing(key, value)) | |
1518 | return 0; | |
1519 | ||
020328e1 | 1520 | r = safe_atou(value, &arg_children_max); |
1d84ad94 LP |
1521 | |
1522 | } else if (proc_cmdline_key_streq(key, "udev.exec_delay")) { | |
1523 | ||
1524 | if (proc_cmdline_value_missing(key, value)) | |
1525 | return 0; | |
1526 | ||
6b92f429 | 1527 | r = parse_sec(value, &arg_exec_delay_usec); |
1d84ad94 | 1528 | |
e2099267 | 1529 | } else if (proc_cmdline_key_streq(key, "udev.timeout_signal")) { |
95ac5230 | 1530 | |
e2099267 MS |
1531 | if (proc_cmdline_value_missing(key, value)) |
1532 | return 0; | |
1533 | ||
1534 | r = signal_from_string(value); | |
1535 | if (r > 0) | |
1536 | arg_timeout_signal = r; | |
95ac5230 LP |
1537 | |
1538 | } else if (proc_cmdline_key_streq(key, "udev.blockdev_read_only")) { | |
1539 | ||
1540 | if (!value) | |
1541 | arg_blockdev_read_only = true; | |
1542 | else { | |
1543 | r = parse_boolean(value); | |
1544 | if (r < 0) | |
1545 | log_warning_errno(r, "Failed to parse udev.blockdev-read-only argument, ignoring: %s", value); | |
1546 | else | |
1547 | arg_blockdev_read_only = r; | |
1548 | } | |
1549 | ||
1550 | if (arg_blockdev_read_only) | |
1551 | log_notice("All physical block devices will be marked read-only."); | |
1552 | ||
1553 | return 0; | |
1554 | ||
1555 | } else { | |
1556 | if (startswith(key, "udev.")) | |
1557 | log_warning("Unknown udev kernel command line option \"%s\", ignoring.", key); | |
1558 | ||
1559 | return 0; | |
1560 | } | |
614a823c | 1561 | |
92e72467 ZJS |
1562 | if (r < 0) |
1563 | log_warning_errno(r, "Failed to parse \"%s=%s\", ignoring: %m", key, value); | |
1d84ad94 | 1564 | |
614a823c | 1565 | return 0; |
e6f86cac KS |
1566 | } |
1567 | ||
37ec0fdd LP |
1568 | static int help(void) { |
1569 | _cleanup_free_ char *link = NULL; | |
1570 | int r; | |
1571 | ||
1572 | r = terminal_urlify_man("systemd-udevd.service", "8", &link); | |
1573 | if (r < 0) | |
1574 | return log_oom(); | |
1575 | ||
ed216e1f | 1576 | printf("%s [OPTIONS...]\n\n" |
d1109e12 | 1577 | "Rule-based manager for device events and files.\n\n" |
5ac0162c | 1578 | " -h --help Print this message\n" |
2d19c17e MF |
1579 | " -V --version Print version of the program\n" |
1580 | " -d --daemon Detach and run in the background\n" | |
1581 | " -D --debug Enable debug output\n" | |
1582 | " -c --children-max=INT Set maximum number of workers\n" | |
1583 | " -e --exec-delay=SECONDS Seconds to wait before executing RUN=\n" | |
1584 | " -t --event-timeout=SECONDS Seconds to wait before terminating an event\n" | |
1585 | " -N --resolve-names=early|late|never\n" | |
5ac0162c | 1586 | " When to resolve users and groups\n" |
bc556335 DDM |
1587 | "\nSee the %s for details.\n", |
1588 | program_invocation_short_name, | |
1589 | link); | |
37ec0fdd LP |
1590 | |
1591 | return 0; | |
ed216e1f TG |
1592 | } |
1593 | ||
bba7a484 | 1594 | static int parse_argv(int argc, char *argv[]) { |
e2099267 MS |
1595 | enum { |
1596 | ARG_TIMEOUT_SIGNAL, | |
1597 | }; | |
1598 | ||
912541b0 | 1599 | static const struct option options[] = { |
e2099267 MS |
1600 | { "daemon", no_argument, NULL, 'd' }, |
1601 | { "debug", no_argument, NULL, 'D' }, | |
1602 | { "children-max", required_argument, NULL, 'c' }, | |
1603 | { "exec-delay", required_argument, NULL, 'e' }, | |
1604 | { "event-timeout", required_argument, NULL, 't' }, | |
1605 | { "resolve-names", required_argument, NULL, 'N' }, | |
1606 | { "help", no_argument, NULL, 'h' }, | |
1607 | { "version", no_argument, NULL, 'V' }, | |
1608 | { "timeout-signal", required_argument, NULL, ARG_TIMEOUT_SIGNAL }, | |
912541b0 KS |
1609 | {} |
1610 | }; | |
689a97f5 | 1611 | |
044497e2 | 1612 | int c, r; |
689a97f5 | 1613 | |
bba7a484 TG |
1614 | assert(argc >= 0); |
1615 | assert(argv); | |
912541b0 | 1616 | |
e14b6f21 | 1617 | while ((c = getopt_long(argc, argv, "c:de:Dt:N:hV", options, NULL)) >= 0) { |
bba7a484 | 1618 | switch (c) { |
912541b0 | 1619 | |
912541b0 | 1620 | case 'd': |
bba7a484 | 1621 | arg_daemonize = true; |
912541b0 KS |
1622 | break; |
1623 | case 'c': | |
020328e1 | 1624 | r = safe_atou(optarg, &arg_children_max); |
6f5cf8a8 | 1625 | if (r < 0) |
389f9bf2 | 1626 | log_warning_errno(r, "Failed to parse --children-max= value '%s', ignoring: %m", optarg); |
912541b0 KS |
1627 | break; |
1628 | case 'e': | |
6b92f429 | 1629 | r = parse_sec(optarg, &arg_exec_delay_usec); |
6f5cf8a8 | 1630 | if (r < 0) |
6b92f429 | 1631 | log_warning_errno(r, "Failed to parse --exec-delay= value '%s', ignoring: %m", optarg); |
912541b0 | 1632 | break; |
e2099267 MS |
1633 | case ARG_TIMEOUT_SIGNAL: |
1634 | r = signal_from_string(optarg); | |
1635 | if (r <= 0) | |
1636 | log_warning_errno(r, "Failed to parse --timeout-signal= value '%s', ignoring: %m", optarg); | |
1637 | else | |
1638 | arg_timeout_signal = r; | |
1639 | ||
1640 | break; | |
9719859c | 1641 | case 't': |
9d9264ba | 1642 | r = parse_sec(optarg, &arg_event_timeout_usec); |
f1e8664e | 1643 | if (r < 0) |
9d9264ba | 1644 | log_warning_errno(r, "Failed to parse --event-timeout= value '%s', ignoring: %m", optarg); |
9719859c | 1645 | break; |
912541b0 | 1646 | case 'D': |
bba7a484 | 1647 | arg_debug = true; |
912541b0 | 1648 | break; |
c4d44cba YW |
1649 | case 'N': { |
1650 | ResolveNameTiming t; | |
1651 | ||
1652 | t = resolve_name_timing_from_string(optarg); | |
1653 | if (t < 0) | |
1654 | log_warning("Invalid --resolve-names= value '%s', ignoring.", optarg); | |
1655 | else | |
1656 | arg_resolve_name_timing = t; | |
912541b0 | 1657 | break; |
c4d44cba | 1658 | } |
912541b0 | 1659 | case 'h': |
37ec0fdd | 1660 | return help(); |
912541b0 | 1661 | case 'V': |
681bd2c5 | 1662 | printf("%s\n", GIT_VERSION); |
bba7a484 TG |
1663 | return 0; |
1664 | case '?': | |
1665 | return -EINVAL; | |
912541b0 | 1666 | default: |
04499a70 | 1667 | assert_not_reached(); |
bba7a484 | 1668 | |
912541b0 KS |
1669 | } |
1670 | } | |
1671 | ||
bba7a484 TG |
1672 | return 1; |
1673 | } | |
1674 | ||
b7f74dd4 | 1675 | static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cgroup) { |
c0c6806b | 1676 | _cleanup_(manager_freep) Manager *manager = NULL; |
b5af8c8c | 1677 | int r; |
c0c6806b TG |
1678 | |
1679 | assert(ret); | |
1680 | ||
6f19b42f | 1681 | manager = new(Manager, 1); |
c0c6806b TG |
1682 | if (!manager) |
1683 | return log_oom(); | |
1684 | ||
6f19b42f | 1685 | *manager = (Manager) { |
df7ee959 | 1686 | .inotify_fd = -1, |
6f19b42f YW |
1687 | .worker_watch = { -1, -1 }, |
1688 | .cgroup = cgroup, | |
1689 | }; | |
e237d8cb | 1690 | |
100bc5bf YW |
1691 | r = udev_ctrl_new_from_fd(&manager->ctrl, fd_ctrl); |
1692 | if (r < 0) | |
1693 | return log_error_errno(r, "Failed to initialize udev control socket: %m"); | |
e237d8cb | 1694 | |
b5af8c8c ZJS |
1695 | r = udev_ctrl_enable_receiving(manager->ctrl); |
1696 | if (r < 0) | |
1697 | return log_error_errno(r, "Failed to bind udev control socket: %m"); | |
c4b69e99 | 1698 | |
7f2e3a14 YW |
1699 | r = device_monitor_new_full(&manager->monitor, MONITOR_GROUP_KERNEL, fd_uevent); |
1700 | if (r < 0) | |
1701 | return log_error_errno(r, "Failed to initialize device monitor: %m"); | |
e237d8cb | 1702 | |
1ffadeaa FB |
1703 | /* Bump receiver buffer, but only if we are not called via socket activation, as in that |
1704 | * case systemd sets the receive buffer size for us, and the value in the .socket unit | |
1705 | * should take full effect. */ | |
e77f52e5 YW |
1706 | if (fd_uevent < 0) { |
1707 | r = sd_device_monitor_set_receive_buffer_size(manager->monitor, 128 * 1024 * 1024); | |
1708 | if (r < 0) | |
1709 | log_warning_errno(r, "Failed to set receive buffer size for device monitor, ignoring: %m"); | |
1710 | } | |
c4b69e99 | 1711 | |
b5af8c8c ZJS |
1712 | r = device_monitor_enable_receiving(manager->monitor); |
1713 | if (r < 0) | |
1714 | return log_error_errno(r, "Failed to bind netlink socket: %m"); | |
1715 | ||
1a0bd015 YW |
1716 | manager->log_level = log_get_max_level(); |
1717 | ||
b5af8c8c ZJS |
1718 | *ret = TAKE_PTR(manager); |
1719 | ||
1720 | return 0; | |
1721 | } | |
1722 | ||
1723 | static int main_loop(Manager *manager) { | |
d02c6f54 | 1724 | int fd_worker, r; |
b5af8c8c | 1725 | |
76e62a4d YW |
1726 | manager->pid = getpid_cached(); |
1727 | ||
e237d8cb TG |
1728 | /* unnamed socket from workers to the main daemon */ |
1729 | r = socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, manager->worker_watch); | |
1730 | if (r < 0) | |
044497e2 | 1731 | return log_error_errno(errno, "Failed to create socketpair for communicating with workers: %m"); |
e237d8cb | 1732 | |
693d371d | 1733 | fd_worker = manager->worker_watch[READ_END]; |
e237d8cb | 1734 | |
2ff48e98 | 1735 | r = setsockopt_int(fd_worker, SOL_SOCKET, SO_PASSCRED, true); |
e237d8cb | 1736 | if (r < 0) |
044497e2 | 1737 | return log_error_errno(r, "Failed to enable SO_PASSCRED: %m"); |
e237d8cb | 1738 | |
df7ee959 YW |
1739 | manager->inotify_fd = inotify_init1(IN_CLOEXEC); |
1740 | if (manager->inotify_fd < 0) | |
1741 | return log_error_errno(errno, "Failed to create inotify descriptor: %m"); | |
e237d8cb | 1742 | |
df7ee959 | 1743 | udev_watch_restore(manager->inotify_fd); |
e237d8cb TG |
1744 | |
1745 | /* block and listen to all signals on signalfd */ | |
72c0a2c2 | 1746 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0); |
693d371d | 1747 | |
49f997f3 TG |
1748 | r = sd_event_default(&manager->event); |
1749 | if (r < 0) | |
044497e2 | 1750 | return log_error_errno(r, "Failed to allocate event loop: %m"); |
49f997f3 | 1751 | |
693d371d TG |
1752 | r = sd_event_add_signal(manager->event, NULL, SIGINT, on_sigterm, manager); |
1753 | if (r < 0) | |
044497e2 | 1754 | return log_error_errno(r, "Failed to create SIGINT event source: %m"); |
693d371d TG |
1755 | |
1756 | r = sd_event_add_signal(manager->event, NULL, SIGTERM, on_sigterm, manager); | |
1757 | if (r < 0) | |
044497e2 | 1758 | return log_error_errno(r, "Failed to create SIGTERM event source: %m"); |
693d371d TG |
1759 | |
1760 | r = sd_event_add_signal(manager->event, NULL, SIGHUP, on_sighup, manager); | |
1761 | if (r < 0) | |
044497e2 | 1762 | return log_error_errno(r, "Failed to create SIGHUP event source: %m"); |
693d371d TG |
1763 | |
1764 | r = sd_event_add_signal(manager->event, NULL, SIGCHLD, on_sigchld, manager); | |
1765 | if (r < 0) | |
044497e2 | 1766 | return log_error_errno(r, "Failed to create SIGCHLD event source: %m"); |
693d371d TG |
1767 | |
1768 | r = sd_event_set_watchdog(manager->event, true); | |
1769 | if (r < 0) | |
044497e2 | 1770 | return log_error_errno(r, "Failed to create watchdog event source: %m"); |
693d371d | 1771 | |
d02c6f54 YW |
1772 | r = udev_ctrl_attach_event(manager->ctrl, manager->event); |
1773 | if (r < 0) | |
1774 | return log_error_errno(r, "Failed to attach event to udev control: %m"); | |
b5af8c8c | 1775 | |
d02c6f54 | 1776 | r = udev_ctrl_start(manager->ctrl, on_ctrl_msg, manager); |
693d371d | 1777 | if (r < 0) |
d02c6f54 | 1778 | return log_error_errno(r, "Failed to start device monitor: %m"); |
693d371d TG |
1779 | |
1780 | /* This needs to be after the inotify and uevent handling, to make sure | |
1781 | * that the ping is send back after fully processing the pending uevents | |
1782 | * (including the synthetic ones we may create due to inotify events). | |
1783 | */ | |
d02c6f54 | 1784 | r = sd_event_source_set_priority(udev_ctrl_get_event_source(manager->ctrl), SD_EVENT_PRIORITY_IDLE); |
693d371d | 1785 | if (r < 0) |
044497e2 | 1786 | return log_error_errno(r, "Failed to set IDLE event priority for udev control event source: %m"); |
693d371d | 1787 | |
df7ee959 | 1788 | r = sd_event_add_io(manager->event, &manager->inotify_event, manager->inotify_fd, EPOLLIN, on_inotify, manager); |
693d371d | 1789 | if (r < 0) |
044497e2 | 1790 | return log_error_errno(r, "Failed to create inotify event source: %m"); |
693d371d | 1791 | |
f00d2b6d YW |
1792 | r = sd_device_monitor_attach_event(manager->monitor, manager->event); |
1793 | if (r < 0) | |
1794 | return log_error_errno(r, "Failed to attach event to device monitor: %m"); | |
1795 | ||
1796 | r = sd_device_monitor_start(manager->monitor, on_uevent, manager); | |
693d371d | 1797 | if (r < 0) |
f00d2b6d YW |
1798 | return log_error_errno(r, "Failed to start device monitor: %m"); |
1799 | ||
1800 | (void) sd_event_source_set_description(sd_device_monitor_get_event_source(manager->monitor), "device-monitor"); | |
693d371d TG |
1801 | |
1802 | r = sd_event_add_io(manager->event, NULL, fd_worker, EPOLLIN, on_worker, manager); | |
1803 | if (r < 0) | |
044497e2 | 1804 | return log_error_errno(r, "Failed to create worker event source: %m"); |
693d371d TG |
1805 | |
1806 | r = sd_event_add_post(manager->event, NULL, on_post, manager); | |
1807 | if (r < 0) | |
044497e2 | 1808 | return log_error_errno(r, "Failed to create post event source: %m"); |
e237d8cb | 1809 | |
b5af8c8c | 1810 | udev_builtin_init(); |
077fc5e2 | 1811 | |
c238a1f5 | 1812 | r = udev_rules_load(&manager->rules, arg_resolve_name_timing); |
b5af8c8c ZJS |
1813 | if (!manager->rules) |
1814 | return log_error_errno(r, "Failed to read udev rules: %m"); | |
077fc5e2 DH |
1815 | |
1816 | r = udev_rules_apply_static_dev_perms(manager->rules); | |
1817 | if (r < 0) | |
044497e2 | 1818 | log_error_errno(r, "Failed to apply permissions on static device nodes: %m"); |
077fc5e2 | 1819 | |
1ef72b55 MS |
1820 | (void) sd_notifyf(false, |
1821 | "READY=1\n" | |
1822 | "STATUS=Processing with %u children at max", arg_children_max); | |
077fc5e2 DH |
1823 | |
1824 | r = sd_event_loop(manager->event); | |
44dcf454 | 1825 | if (r < 0) |
044497e2 | 1826 | log_error_errno(r, "Event loop failed: %m"); |
077fc5e2 | 1827 | |
077fc5e2 DH |
1828 | sd_notify(false, |
1829 | "STOPPING=1\n" | |
1830 | "STATUS=Shutting down..."); | |
077fc5e2 DH |
1831 | return r; |
1832 | } | |
1833 | ||
63e2d171 | 1834 | int run_udevd(int argc, char *argv[]) { |
c26d1879 | 1835 | _cleanup_free_ char *cgroup = NULL; |
b5af8c8c | 1836 | _cleanup_(manager_freep) Manager *manager = NULL; |
efa1606e | 1837 | int fd_ctrl = -1, fd_uevent = -1; |
e5d7bce1 | 1838 | int r; |
bba7a484 | 1839 | |
bba7a484 | 1840 | log_set_target(LOG_TARGET_AUTO); |
6b413782 | 1841 | log_open(); |
e2099267 | 1842 | udev_parse_config_full(&arg_children_max, &arg_exec_delay_usec, &arg_event_timeout_usec, &arg_resolve_name_timing, &arg_timeout_signal); |
bba7a484 | 1843 | log_parse_environment(); |
6b413782 | 1844 | log_open(); /* Done again to update after reading configuration. */ |
bba7a484 | 1845 | |
bba7a484 TG |
1846 | r = parse_argv(argc, argv); |
1847 | if (r <= 0) | |
0c5a109a | 1848 | return r; |
bba7a484 | 1849 | |
1d84ad94 | 1850 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
614a823c | 1851 | if (r < 0) |
044497e2 | 1852 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
912541b0 | 1853 | |
78d3e041 KS |
1854 | if (arg_debug) { |
1855 | log_set_target(LOG_TARGET_CONSOLE); | |
bba7a484 | 1856 | log_set_max_level(LOG_DEBUG); |
78d3e041 | 1857 | } |
bba7a484 | 1858 | |
fba868fa LP |
1859 | r = must_be_root(); |
1860 | if (r < 0) | |
0c5a109a | 1861 | return r; |
912541b0 | 1862 | |
712cebf1 | 1863 | if (arg_children_max == 0) { |
fe56acd8 | 1864 | unsigned long cpu_limit, mem_limit, cpu_count = 1; |
d457ff83 | 1865 | |
fe56acd8 LP |
1866 | r = cpus_in_affinity_mask(); |
1867 | if (r < 0) | |
1868 | log_warning_errno(r, "Failed to determine number of local CPUs, ignoring: %m"); | |
1869 | else | |
1870 | cpu_count = r; | |
88bd5a32 FB |
1871 | |
1872 | cpu_limit = cpu_count * 2 + 16; | |
1873 | mem_limit = MAX(physical_memory() / (128UL*1024*1024), 10U); | |
912541b0 | 1874 | |
88bd5a32 FB |
1875 | arg_children_max = MIN(cpu_limit, mem_limit); |
1876 | arg_children_max = MIN(WORKER_NUM_MAX, arg_children_max); | |
e438c57a | 1877 | |
044497e2 | 1878 | log_debug("Set children_max to %u", arg_children_max); |
d457ff83 | 1879 | } |
912541b0 | 1880 | |
712cebf1 | 1881 | /* set umask before creating any file/directory */ |
712cebf1 | 1882 | umask(022); |
912541b0 | 1883 | |
c3dacc8b | 1884 | r = mac_selinux_init(); |
0c5a109a | 1885 | if (r < 0) |
a9ba0e32 | 1886 | return r; |
912541b0 | 1887 | |
dae8b82e | 1888 | r = mkdir_errno_wrapper("/run/udev", 0755); |
0c5a109a ZJS |
1889 | if (r < 0 && r != -EEXIST) |
1890 | return log_error_errno(r, "Failed to create /run/udev: %m"); | |
712cebf1 | 1891 | |
31cbd202 YW |
1892 | if (getppid() == 1 && sd_booted() > 0) { |
1893 | /* Get our own cgroup, we regularly kill everything udev has left behind. | |
1894 | * We only do this on systemd systems, and only if we are directly spawned | |
1895 | * by PID1. Otherwise we are not guaranteed to have a dedicated cgroup. */ | |
c26d1879 | 1896 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup); |
11b9fb15 | 1897 | if (r < 0) { |
a2d61f07 | 1898 | if (IN_SET(r, -ENOENT, -ENOMEDIUM)) |
044497e2 | 1899 | log_debug_errno(r, "Dedicated cgroup not found: %m"); |
11b9fb15 | 1900 | else |
044497e2 | 1901 | log_warning_errno(r, "Failed to get cgroup: %m"); |
11b9fb15 | 1902 | } |
c26d1879 TG |
1903 | } |
1904 | ||
b7f74dd4 | 1905 | r = listen_fds(&fd_ctrl, &fd_uevent); |
0c5a109a ZJS |
1906 | if (r < 0) |
1907 | return log_error_errno(r, "Failed to listen on fds: %m"); | |
b7f74dd4 | 1908 | |
b5af8c8c ZJS |
1909 | r = manager_new(&manager, fd_ctrl, fd_uevent, cgroup); |
1910 | if (r < 0) | |
1911 | return log_error_errno(r, "Failed to create manager: %m"); | |
1912 | ||
bba7a484 | 1913 | if (arg_daemonize) { |
912541b0 | 1914 | pid_t pid; |
912541b0 | 1915 | |
b5af8c8c | 1916 | log_info("Starting version " GIT_VERSION); |
3cbb2057 | 1917 | |
40e749b5 | 1918 | /* connect /dev/null to stdin, stdout, stderr */ |
c76cf844 AK |
1919 | if (log_get_max_level() < LOG_DEBUG) { |
1920 | r = make_null_stdio(); | |
1921 | if (r < 0) | |
1922 | log_warning_errno(r, "Failed to redirect standard streams to /dev/null: %m"); | |
1923 | } | |
1924 | ||
912541b0 | 1925 | pid = fork(); |
0c5a109a ZJS |
1926 | if (pid < 0) |
1927 | return log_error_errno(errno, "Failed to fork daemon: %m"); | |
1928 | if (pid > 0) | |
1929 | /* parent */ | |
1930 | return 0; | |
912541b0 | 1931 | |
0c5a109a | 1932 | /* child */ |
ece0fe12 | 1933 | (void) setsid(); |
7500cd5e | 1934 | } |
912541b0 | 1935 | |
76e62a4d | 1936 | return main_loop(manager); |
7fafc032 | 1937 | } |