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