1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright © 2004 Chris Friesen <chris_friesen@sympatico.ca>
4 * Copyright © 2009 Canonical Ltd.
5 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
15 #include <sys/epoll.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>
27 #include "sd-daemon.h"
30 #include "alloc-util.h"
31 #include "blockdev-util.h"
32 #include "cgroup-setup.h"
33 #include "cgroup-util.h"
34 #include "cpu-set-util.h"
35 #include "dev-setup.h"
36 #include "device-monitor-private.h"
37 #include "device-private.h"
38 #include "device-util.h"
39 #include "errno-list.h"
40 #include "event-util.h"
43 #include "format-util.h"
46 #include "inotify-util.h"
48 #include "limits-util.h"
50 #include "main-func.h"
52 #include "netlink-util.h"
53 #include "parse-util.h"
54 #include "path-util.h"
55 #include "pretty-print.h"
56 #include "proc-cmdline.h"
57 #include "process-util.h"
58 #include "selinux-util.h"
59 #include "signal-util.h"
60 #include "socket-util.h"
61 #include "string-util.h"
64 #include "syslog-util.h"
66 #include "udev-builtin.h"
67 #include "udev-ctrl.h"
68 #include "udev-event.h"
69 #include "udev-node.h"
70 #include "udev-util.h"
71 #include "udev-watch.h"
72 #include "user-util.h"
75 #define WORKER_NUM_MAX 2048U
76 #define EVENT_RETRY_INTERVAL_USEC (200 * USEC_PER_MSEC)
77 #define EVENT_RETRY_TIMEOUT_USEC (3 * USEC_PER_MINUTE)
79 static bool arg_debug
= false;
80 static int arg_daemonize
= false;
81 static ResolveNameTiming arg_resolve_name_timing
= RESOLVE_NAME_EARLY
;
82 static unsigned arg_children_max
= 0;
83 static usec_t arg_exec_delay_usec
= 0;
84 static usec_t arg_event_timeout_usec
= 180 * USEC_PER_SEC
;
85 static int arg_timeout_signal
= SIGKILL
;
86 static bool arg_blockdev_read_only
= false;
88 typedef struct Event Event
;
89 typedef struct Worker Worker
;
91 typedef struct Manager
{
94 LIST_HEAD(Event
, events
);
96 pid_t pid
; /* the process that originally allocated the manager object */
104 sd_device_monitor
*monitor
;
108 /* used by udev-watch */
110 sd_event_source
*inotify_event
;
112 sd_event_source
*kill_workers_event
;
116 bool udev_node_needs_cleanup
;
117 bool stop_exec_queue
;
121 typedef enum EventState
{
127 typedef struct Event
{
134 sd_device_action_t action
;
136 uint64_t blocker_seqnum
;
139 const char *devpath_old
;
142 /* Used when the device is locked by another program. */
143 usec_t retry_again_next_usec
;
144 usec_t retry_again_timeout_usec
;
145 sd_event_source
*retry_event_source
;
147 sd_event_source
*timeout_warning_event
;
148 sd_event_source
*timeout_event
;
150 LIST_FIELDS(Event
, event
);
153 typedef enum WorkerState
{
161 typedef struct Worker
{
164 sd_event_source
*child_event_source
;
165 sd_device_monitor
*monitor
;
170 /* passed from worker to main process */
171 typedef enum EventResult
{
172 EVENT_RESULT_NERRNO_MIN
= -ERRNO_MAX
,
173 EVENT_RESULT_NERRNO_MAX
= -1,
174 EVENT_RESULT_SUCCESS
= 0,
175 EVENT_RESULT_EXIT_STATUS_BASE
= 0,
176 EVENT_RESULT_EXIT_STATUS_MAX
= 255,
177 EVENT_RESULT_TRY_AGAIN
= 256, /* when the block device is locked by another process. */
178 EVENT_RESULT_SIGNAL_BASE
= 257,
179 EVENT_RESULT_SIGNAL_MAX
= EVENT_RESULT_SIGNAL_BASE
+ _NSIG
,
181 _EVENT_RESULT_INVALID
= -EINVAL
,
184 static Event
*event_free(Event
*event
) {
188 assert(event
->manager
);
190 LIST_REMOVE(event
, event
->manager
->events
, event
);
191 sd_device_unref(event
->dev
);
193 /* Do not use sd_event_source_disable_unref() here, as this is called by both workers and the
195 sd_event_source_unref(event
->retry_event_source
);
196 sd_event_source_unref(event
->timeout_warning_event
);
197 sd_event_source_unref(event
->timeout_event
);
200 event
->worker
->event
= NULL
;
205 static void event_queue_cleanup(Manager
*manager
, EventState match_state
) {
206 LIST_FOREACH(event
, event
, manager
->events
) {
207 if (match_state
!= EVENT_UNDEF
&& match_state
!= event
->state
)
214 static Worker
*worker_free(Worker
*worker
) {
219 hashmap_remove(worker
->manager
->workers
, PID_TO_PTR(worker
->pid
));
221 sd_event_source_unref(worker
->child_event_source
);
222 sd_device_monitor_unref(worker
->monitor
);
223 event_free(worker
->event
);
225 return mfree(worker
);
228 DEFINE_TRIVIAL_CLEANUP_FUNC(Worker
*, worker_free
);
229 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(worker_hash_op
, void, trivial_hash_func
, trivial_compare_func
, Worker
, worker_free
);
231 static void manager_clear_for_worker(Manager
*manager
) {
234 /* Do not use sd_event_source_disable_unref() here, as this is called by both workers and the
236 manager
->inotify_event
= sd_event_source_unref(manager
->inotify_event
);
237 manager
->kill_workers_event
= sd_event_source_unref(manager
->kill_workers_event
);
239 manager
->event
= sd_event_unref(manager
->event
);
241 manager
->workers
= hashmap_free(manager
->workers
);
242 event_queue_cleanup(manager
, EVENT_UNDEF
);
244 manager
->monitor
= sd_device_monitor_unref(manager
->monitor
);
245 manager
->ctrl
= udev_ctrl_unref(manager
->ctrl
);
247 manager
->worker_watch
[READ_END
] = safe_close(manager
->worker_watch
[READ_END
]);
250 static Manager
* manager_free(Manager
*manager
) {
256 manager_clear_for_worker(manager
);
258 sd_netlink_unref(manager
->rtnl
);
260 hashmap_free_free_free(manager
->properties
);
261 udev_rules_free(manager
->rules
);
263 safe_close(manager
->inotify_fd
);
264 safe_close_pair(manager
->worker_watch
);
266 free(manager
->cgroup
);
267 return mfree(manager
);
270 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager
*, manager_free
);
272 static int on_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
);
274 static int worker_new(Worker
**ret
, Manager
*manager
, sd_device_monitor
*worker_monitor
, pid_t pid
) {
275 _cleanup_(worker_freep
) Worker
*worker
= NULL
;
280 assert(worker_monitor
);
283 /* close monitor, but keep address around */
284 device_monitor_disconnect(worker_monitor
);
286 worker
= new(Worker
, 1);
291 .monitor
= sd_device_monitor_ref(worker_monitor
),
295 r
= sd_event_add_child(manager
->event
, &worker
->child_event_source
, pid
, WEXITED
, on_sigchld
, worker
);
299 r
= hashmap_ensure_put(&manager
->workers
, &worker_hash_op
, PID_TO_PTR(pid
), worker
);
303 worker
->manager
= manager
;
305 *ret
= TAKE_PTR(worker
);
309 static void manager_kill_workers(Manager
*manager
, bool force
) {
314 HASHMAP_FOREACH(worker
, manager
->workers
) {
315 if (worker
->state
== WORKER_KILLED
)
318 if (worker
->state
== WORKER_RUNNING
&& !force
) {
319 worker
->state
= WORKER_KILLING
;
323 worker
->state
= WORKER_KILLED
;
324 (void) kill(worker
->pid
, SIGTERM
);
328 static void manager_exit(Manager
*manager
) {
331 manager
->exit
= true;
335 "STATUS=Starting shutdown...");
337 /* close sources of new events and discard buffered events */
338 manager
->ctrl
= udev_ctrl_unref(manager
->ctrl
);
340 manager
->inotify_event
= sd_event_source_disable_unref(manager
->inotify_event
);
341 manager
->inotify_fd
= safe_close(manager
->inotify_fd
);
343 manager
->monitor
= sd_device_monitor_unref(manager
->monitor
);
345 /* discard queued events and kill workers */
346 event_queue_cleanup(manager
, EVENT_QUEUED
);
347 manager_kill_workers(manager
, true);
350 static void notify_ready(void) {
353 r
= sd_notifyf(false,
355 "STATUS=Processing with %u children at max", arg_children_max
);
357 log_warning_errno(r
, "Failed to send readiness notification, ignoring: %m");
360 /* reload requested, HUP signal received, rules changed, builtin changed */
361 static void manager_reload(Manager
*manager
, bool force
) {
362 _cleanup_(udev_rules_freep
) UdevRules
*rules
= NULL
;
368 assert_se(sd_event_now(manager
->event
, CLOCK_MONOTONIC
, &now_usec
) >= 0);
369 if (!force
&& now_usec
< usec_add(manager
->last_usec
, 3 * USEC_PER_SEC
))
370 /* check for changed config, every 3 seconds at most */
372 manager
->last_usec
= now_usec
;
374 /* Reload SELinux label database, to make the child inherit the up-to-date database. */
375 mac_selinux_maybe_reload();
377 /* Nothing changed. It is not necessary to reload. */
378 if (!udev_rules_should_reload(manager
->rules
) && !udev_builtin_should_reload())
383 "STATUS=Flushing configuration...");
385 manager_kill_workers(manager
, false);
390 r
= udev_rules_load(&rules
, arg_resolve_name_timing
);
392 log_warning_errno(r
, "Failed to read udev rules, using the previously loaded rules, ignoring: %m");
394 udev_rules_free_and_replace(manager
->rules
, rules
);
399 static int on_kill_workers_event(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
400 Manager
*manager
= ASSERT_PTR(userdata
);
402 log_debug("Cleanup idle workers");
403 manager_kill_workers(manager
, false);
408 static void device_broadcast(sd_device_monitor
*monitor
, sd_device
*dev
, EventResult result
) {
413 /* On exit, manager->monitor is already NULL. */
417 if (result
!= EVENT_RESULT_SUCCESS
) {
418 (void) device_add_property(dev
, "UDEV_WORKER_FAILED", "1");
421 case EVENT_RESULT_NERRNO_MIN
... EVENT_RESULT_NERRNO_MAX
: {
424 (void) device_add_propertyf(dev
, "UDEV_WORKER_ERRNO", "%i", -result
);
426 str
= errno_to_name(result
);
428 (void) device_add_property(dev
, "UDEV_WORKER_ERRNO_NAME", str
);
431 case EVENT_RESULT_EXIT_STATUS_BASE
... EVENT_RESULT_EXIT_STATUS_MAX
:
432 (void) device_add_propertyf(dev
, "UDEV_WORKER_EXIT_STATUS", "%i", result
- EVENT_RESULT_EXIT_STATUS_BASE
);
435 case EVENT_RESULT_TRY_AGAIN
:
436 assert_not_reached();
439 case EVENT_RESULT_SIGNAL_BASE
... EVENT_RESULT_SIGNAL_MAX
: {
442 (void) device_add_propertyf(dev
, "UDEV_WORKER_SIGNAL", "%i", result
- EVENT_RESULT_SIGNAL_BASE
);
444 str
= signal_to_string(result
- EVENT_RESULT_SIGNAL_BASE
);
446 (void) device_add_property(dev
, "UDEV_WORKER_SIGNAL_NAME", str
);
450 log_device_warning(dev
, "Unknown event result \"%i\", ignoring.", result
);
454 r
= device_monitor_send_device(monitor
, NULL
, dev
);
456 log_device_warning_errno(dev
, r
,
457 "Failed to broadcast event to libudev listeners, ignoring: %m");
460 static int worker_send_result(Manager
*manager
, EventResult result
) {
462 assert(manager
->worker_watch
[WRITE_END
] >= 0);
464 return loop_write(manager
->worker_watch
[WRITE_END
], &result
, sizeof(result
), false);
467 static int device_get_whole_disk(sd_device
*dev
, sd_device
**ret_device
, const char **ret_devname
) {
473 if (device_for_action(dev
, SD_DEVICE_REMOVE
))
476 r
= sd_device_get_sysname(dev
, &val
);
478 return log_device_debug_errno(dev
, r
, "Failed to get sysname: %m");
480 /* Exclude the following devices:
481 * For "dm-", see the comment added by e918a1b5a94f270186dca59156354acd2a596494.
482 * For "md", see the commit message of 2e5b17d01347d3c3118be2b8ad63d20415dbb1f0,
483 * but not sure the assumption is still valid even when partitions are created on the md
484 * devices, surprisingly which seems to be possible, see PR #22973.
485 * For "drbd", see the commit message of fee854ee8ccde0cd28e0f925dea18cce35f3993d. */
486 if (STARTSWITH_SET(val
, "dm-", "md", "drbd"))
489 r
= block_device_get_whole_disk(dev
, &dev
);
491 -ENOTBLK
, /* The device is not a block device. */
492 -ENODEV
/* The whole disk device was not found, it may already be removed. */))
495 return log_device_debug_errno(dev
, r
, "Failed to get whole disk device: %m");
497 r
= sd_device_get_devname(dev
, &val
);
499 return log_device_debug_errno(dev
, r
, "Failed to get devname: %m");
515 static int worker_lock_whole_disk(sd_device
*dev
, int *ret_fd
) {
516 _cleanup_close_
int fd
= -1;
517 sd_device
*dev_whole_disk
;
524 /* Take a shared lock on the device node; this establishes a concept of device "ownership" to
525 * serialize device access. External processes holding an exclusive lock will cause udev to skip the
526 * event handling; in the case udev acquired the lock, the external process can block until udev has
527 * finished its event handling. */
529 r
= device_get_whole_disk(dev
, &dev_whole_disk
, &val
);
535 fd
= sd_device_open(dev_whole_disk
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
537 bool ignore
= ERRNO_IS_DEVICE_ABSENT(fd
);
539 log_device_debug_errno(dev
, fd
, "Failed to open '%s'%s: %m", val
, ignore
? ", ignoring" : "");
546 if (flock(fd
, LOCK_SH
|LOCK_NB
) < 0)
547 return log_device_debug_errno(dev
, errno
, "Failed to flock(%s): %m", val
);
549 *ret_fd
= TAKE_FD(fd
);
557 static int worker_mark_block_device_read_only(sd_device
*dev
) {
558 _cleanup_close_
int fd
= -1;
564 if (!arg_blockdev_read_only
)
567 /* Do this only once, when the block device is new. If the device is later retriggered let's not
568 * toggle the bit again, so that people can boot up with full read-only mode and then unset the bit
569 * for specific devices only. */
570 if (!device_for_action(dev
, SD_DEVICE_ADD
))
573 r
= sd_device_get_subsystem(dev
, &val
);
575 return log_device_debug_errno(dev
, r
, "Failed to get subsystem: %m");
577 if (!streq(val
, "block"))
580 r
= sd_device_get_sysname(dev
, &val
);
582 return log_device_debug_errno(dev
, r
, "Failed to get sysname: %m");
584 /* Exclude synthetic devices for now, this is supposed to be a safety feature to avoid modification
585 * of physical devices, and what sits on top of those doesn't really matter if we don't allow the
586 * underlying block devices to receive changes. */
587 if (STARTSWITH_SET(val
, "dm-", "md", "drbd", "loop", "nbd", "zram"))
590 fd
= sd_device_open(dev
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
592 return log_device_debug_errno(dev
, fd
, "Failed to open '%s', ignoring: %m", val
);
594 if (ioctl(fd
, BLKROSET
, &state
) < 0)
595 return log_device_warning_errno(dev
, errno
, "Failed to mark block device '%s' read-only: %m", val
);
597 log_device_info(dev
, "Successfully marked block device '%s' read-only.", val
);
601 static int worker_process_device(Manager
*manager
, sd_device
*dev
) {
602 _cleanup_(udev_event_freep
) UdevEvent
*udev_event
= NULL
;
603 _cleanup_close_
int fd_lock
= -1;
609 log_device_uevent(dev
, "Processing device");
611 udev_event
= udev_event_new(dev
, arg_exec_delay_usec
, manager
->rtnl
, manager
->log_level
);
615 /* If this is a block device and the device is locked currently via the BSD advisory locks,
616 * someone else is using it exclusively. We don't run our udev rules now to not interfere.
617 * Instead of processing the event, we requeue the event and will try again after a delay.
619 * The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING */
620 r
= worker_lock_whole_disk(dev
, &fd_lock
);
622 return EVENT_RESULT_TRY_AGAIN
;
626 (void) worker_mark_block_device_read_only(dev
);
628 /* apply rules, create node, symlinks */
629 r
= udev_event_execute_rules(
632 arg_event_timeout_usec
,
639 udev_event_execute_run(udev_event
, arg_event_timeout_usec
, arg_timeout_signal
);
642 /* in case rtnl was initialized */
643 manager
->rtnl
= sd_netlink_ref(udev_event
->rtnl
);
645 if (udev_event
->inotify_watch
) {
646 r
= udev_watch_begin(manager
->inotify_fd
, dev
);
647 if (r
< 0 && r
!= -ENOENT
) /* The device may be already removed, ignore -ENOENT. */
648 log_device_warning_errno(dev
, r
, "Failed to add inotify watch, ignoring: %m");
651 log_device_uevent(dev
, "Device processed");
655 static int worker_device_monitor_handler(sd_device_monitor
*monitor
, sd_device
*dev
, void *userdata
) {
656 Manager
*manager
= ASSERT_PTR(userdata
);
661 r
= worker_process_device(manager
, dev
);
662 if (r
== EVENT_RESULT_TRY_AGAIN
)
663 /* if we couldn't acquire the flock(), then requeue the event */
664 log_device_debug(dev
, "Block device is currently locked, requeueing the event.");
667 log_device_warning_errno(dev
, r
, "Failed to process device, ignoring: %m");
669 /* send processed event back to libudev listeners */
670 device_broadcast(monitor
, dev
, r
);
673 /* send udevd the result of the event execution */
674 r
= worker_send_result(manager
, r
);
676 log_device_warning_errno(dev
, r
, "Failed to send signal to main daemon, ignoring: %m");
678 /* Reset the log level, as it might be changed by "OPTIONS=log_level=". */
679 log_set_max_level(manager
->log_level
);
684 static int worker_main(Manager
*_manager
, sd_device_monitor
*monitor
, sd_device
*first_device
) {
685 _cleanup_(sd_device_unrefp
) sd_device
*dev
= first_device
;
686 _cleanup_(manager_freep
) Manager
*manager
= _manager
;
693 assert_se(unsetenv("NOTIFY_SOCKET") == 0);
695 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, -1) >= 0);
697 /* Reset OOM score, we only protect the main daemon. */
698 r
= set_oom_score_adjust(0);
700 log_debug_errno(r
, "Failed to reset OOM score, ignoring: %m");
702 /* Clear unnecessary data in Manager object. */
703 manager_clear_for_worker(manager
);
705 r
= sd_event_new(&manager
->event
);
707 return log_error_errno(r
, "Failed to allocate event loop: %m");
709 r
= sd_event_add_signal(manager
->event
, NULL
, SIGTERM
, NULL
, NULL
);
711 return log_error_errno(r
, "Failed to set SIGTERM event: %m");
713 r
= sd_device_monitor_attach_event(monitor
, manager
->event
);
715 return log_error_errno(r
, "Failed to attach event loop to device monitor: %m");
717 r
= sd_device_monitor_start(monitor
, worker_device_monitor_handler
, manager
);
719 return log_error_errno(r
, "Failed to start device monitor: %m");
721 /* Process first device */
722 (void) worker_device_monitor_handler(monitor
, dev
, manager
);
724 r
= sd_event_loop(manager
->event
);
726 return log_error_errno(r
, "Event loop failed: %m");
731 static int on_event_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
732 Event
*event
= ASSERT_PTR(userdata
);
734 assert(event
->worker
);
736 kill_and_sigcont(event
->worker
->pid
, arg_timeout_signal
);
737 event
->worker
->state
= WORKER_KILLED
;
739 log_device_error(event
->dev
, "Worker ["PID_FMT
"] processing SEQNUM=%"PRIu64
" killed", event
->worker
->pid
, event
->seqnum
);
744 static int on_event_timeout_warning(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
745 Event
*event
= ASSERT_PTR(userdata
);
747 assert(event
->worker
);
749 log_device_warning(event
->dev
, "Worker ["PID_FMT
"] processing SEQNUM=%"PRIu64
" is taking a long time", event
->worker
->pid
, event
->seqnum
);
754 static void worker_attach_event(Worker
*worker
, Event
*event
) {
758 assert(worker
->manager
);
760 assert(!event
->worker
);
761 assert(!worker
->event
);
763 worker
->state
= WORKER_RUNNING
;
764 worker
->event
= event
;
765 event
->state
= EVENT_RUNNING
;
766 event
->worker
= worker
;
768 e
= worker
->manager
->event
;
770 (void) sd_event_add_time_relative(e
, &event
->timeout_warning_event
, CLOCK_MONOTONIC
,
771 udev_warn_timeout(arg_event_timeout_usec
), USEC_PER_SEC
,
772 on_event_timeout_warning
, event
);
774 (void) sd_event_add_time_relative(e
, &event
->timeout_event
, CLOCK_MONOTONIC
,
775 arg_event_timeout_usec
, USEC_PER_SEC
,
776 on_event_timeout
, event
);
779 static int worker_spawn(Manager
*manager
, Event
*event
) {
780 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*worker_monitor
= NULL
;
785 /* listen for new events */
786 r
= device_monitor_new_full(&worker_monitor
, MONITOR_GROUP_NONE
, -1);
790 (void) sd_device_monitor_set_description(worker_monitor
, "worker");
792 /* allow the main daemon netlink address to send devices to the worker */
793 r
= device_monitor_allow_unicast_sender(worker_monitor
, manager
->monitor
);
795 return log_error_errno(r
, "Worker: Failed to set unicast sender: %m");
797 r
= device_monitor_enable_receiving(worker_monitor
);
799 return log_error_errno(r
, "Worker: Failed to enable receiving of device: %m");
801 r
= safe_fork(NULL
, FORK_DEATHSIG
, &pid
);
803 event
->state
= EVENT_QUEUED
;
804 return log_error_errno(r
, "Failed to fork() worker: %m");
807 DEVICE_TRACE_POINT(worker_spawned
, event
->dev
, getpid());
810 r
= worker_main(manager
, worker_monitor
, sd_device_ref(event
->dev
));
812 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
815 r
= worker_new(&worker
, manager
, worker_monitor
, pid
);
817 return log_error_errno(r
, "Failed to create worker object: %m");
819 worker_attach_event(worker
, event
);
821 log_device_debug(event
->dev
, "Worker ["PID_FMT
"] is forked for processing SEQNUM=%"PRIu64
".", pid
, event
->seqnum
);
825 static int event_run(Event
*event
) {
826 static bool log_children_max_reached
= true;
832 assert(event
->manager
);
834 log_device_uevent(event
->dev
, "Device ready for processing");
836 (void) event_source_disable(event
->retry_event_source
);
838 manager
= event
->manager
;
839 HASHMAP_FOREACH(worker
, manager
->workers
) {
840 if (worker
->state
!= WORKER_IDLE
)
843 r
= device_monitor_send_device(manager
->monitor
, worker
->monitor
, event
->dev
);
845 log_device_error_errno(event
->dev
, r
, "Worker ["PID_FMT
"] did not accept message, killing the worker: %m",
847 (void) kill(worker
->pid
, SIGKILL
);
848 worker
->state
= WORKER_KILLED
;
851 worker_attach_event(worker
, event
);
852 return 1; /* event is now processing. */
855 if (hashmap_size(manager
->workers
) >= arg_children_max
) {
856 /* Avoid spamming the debug logs if the limit is already reached and
857 * many events still need to be processed */
858 if (log_children_max_reached
&& arg_children_max
> 1) {
859 log_debug("Maximum number (%u) of children reached.", hashmap_size(manager
->workers
));
860 log_children_max_reached
= false;
862 return 0; /* no free worker */
865 /* Re-enable the debug message for the next batch of events */
866 log_children_max_reached
= true;
868 /* start new worker and pass initial device */
869 r
= worker_spawn(manager
, event
);
873 return 1; /* event is now processing. */
876 static int event_is_blocked(Event
*event
) {
877 Event
*loop_event
= NULL
;
880 /* lookup event for identical, parent, child device */
883 assert(event
->manager
);
884 assert(event
->blocker_seqnum
<= event
->seqnum
);
886 if (event
->retry_again_next_usec
> 0) {
889 r
= sd_event_now(event
->manager
->event
, CLOCK_BOOTTIME
, &now_usec
);
893 if (event
->retry_again_next_usec
> now_usec
)
897 if (event
->blocker_seqnum
== event
->seqnum
)
898 /* we have checked previously and no blocker found */
901 LIST_FOREACH(event
, e
, event
->manager
->events
) {
904 /* we already found a later event, earlier cannot block us, no need to check again */
905 if (loop_event
->seqnum
< event
->blocker_seqnum
)
908 /* event we checked earlier still exists, no need to check again */
909 if (loop_event
->seqnum
== event
->blocker_seqnum
)
912 /* found ourself, no later event can block us */
913 if (loop_event
->seqnum
>= event
->seqnum
)
916 /* found event we have not checked */
921 assert(loop_event
->seqnum
> event
->blocker_seqnum
&&
922 loop_event
->seqnum
< event
->seqnum
);
924 /* check if queue contains events we depend on */
925 LIST_FOREACH(event
, e
, loop_event
) {
928 /* found ourself, no later event can block us */
929 if (loop_event
->seqnum
>= event
->seqnum
)
932 if (streq_ptr(loop_event
->id
, event
->id
))
935 if (devpath_conflict(event
->devpath
, loop_event
->devpath
) ||
936 devpath_conflict(event
->devpath
, loop_event
->devpath_old
) ||
937 devpath_conflict(event
->devpath_old
, loop_event
->devpath
))
940 if (event
->devnode
&& streq_ptr(event
->devnode
, loop_event
->devnode
))
946 log_device_debug(event
->dev
, "SEQNUM=%" PRIu64
" blocked by SEQNUM=%" PRIu64
,
947 event
->seqnum
, loop_event
->seqnum
);
949 event
->blocker_seqnum
= loop_event
->seqnum
;
953 event
->blocker_seqnum
= event
->seqnum
;
957 static int event_queue_start(Manager
*manager
) {
962 if (!manager
->events
|| manager
->exit
|| manager
->stop_exec_queue
)
965 /* To make the stack directory /run/udev/links cleaned up later. */
966 manager
->udev_node_needs_cleanup
= true;
968 r
= event_source_disable(manager
->kill_workers_event
);
970 log_warning_errno(r
, "Failed to disable event source for cleaning up idle workers, ignoring: %m");
972 manager_reload(manager
, /* force = */ false);
974 LIST_FOREACH(event
, event
, manager
->events
) {
975 if (event
->state
!= EVENT_QUEUED
)
978 /* do not start event if parent or child event is still running or queued */
979 r
= event_is_blocked(event
);
983 log_device_warning_errno(event
->dev
, r
,
984 "Failed to check dependencies for event (SEQNUM=%"PRIu64
", ACTION=%s), "
985 "assuming there is no blocking event, ignoring: %m",
987 strna(device_action_to_string(event
->action
)));
989 r
= event_run(event
);
990 if (r
<= 0) /* 0 means there are no idle workers. Let's escape from the loop. */
997 static int on_event_retry(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
998 /* This does nothing. The on_post() callback will start the event if there exists an idle worker. */
1002 static int event_requeue(Event
*event
) {
1007 assert(event
->manager
);
1008 assert(event
->manager
->event
);
1010 event
->timeout_warning_event
= sd_event_source_disable_unref(event
->timeout_warning_event
);
1011 event
->timeout_event
= sd_event_source_disable_unref(event
->timeout_event
);
1013 /* add a short delay to suppress busy loop */
1014 r
= sd_event_now(event
->manager
->event
, CLOCK_BOOTTIME
, &now_usec
);
1016 return log_device_warning_errno(event
->dev
, r
,
1017 "Failed to get current time, "
1018 "skipping event (SEQNUM=%"PRIu64
", ACTION=%s): %m",
1019 event
->seqnum
, strna(device_action_to_string(event
->action
)));
1021 if (event
->retry_again_timeout_usec
> 0 && event
->retry_again_timeout_usec
<= now_usec
)
1022 return log_device_warning_errno(event
->dev
, SYNTHETIC_ERRNO(ETIMEDOUT
),
1023 "The underlying block device is locked by a process more than %s, "
1024 "skipping event (SEQNUM=%"PRIu64
", ACTION=%s).",
1025 FORMAT_TIMESPAN(EVENT_RETRY_TIMEOUT_USEC
, USEC_PER_MINUTE
),
1026 event
->seqnum
, strna(device_action_to_string(event
->action
)));
1028 event
->retry_again_next_usec
= usec_add(now_usec
, EVENT_RETRY_INTERVAL_USEC
);
1029 if (event
->retry_again_timeout_usec
== 0)
1030 event
->retry_again_timeout_usec
= usec_add(now_usec
, EVENT_RETRY_TIMEOUT_USEC
);
1032 r
= event_reset_time_relative(event
->manager
->event
, &event
->retry_event_source
,
1033 CLOCK_MONOTONIC
, EVENT_RETRY_INTERVAL_USEC
, 0,
1034 on_event_retry
, NULL
,
1035 0, "retry-event", true);
1037 return log_device_warning_errno(event
->dev
, r
, "Failed to reset timer event source for retrying event, "
1038 "skipping event (SEQNUM=%"PRIu64
", ACTION=%s): %m",
1039 event
->seqnum
, strna(device_action_to_string(event
->action
)));
1041 if (event
->worker
&& event
->worker
->event
== event
)
1042 event
->worker
->event
= NULL
;
1043 event
->worker
= NULL
;
1045 event
->state
= EVENT_QUEUED
;
1049 static int event_queue_assume_block_device_unlocked(Manager
*manager
, sd_device
*dev
) {
1050 const char *devname
;
1053 /* When a new event for a block device is queued or we get an inotify event, assume that the
1054 * device is not locked anymore. The assumption may not be true, but that should not cause any
1055 * issues, as in that case events will be requeued soon. */
1057 r
= device_get_whole_disk(dev
, NULL
, &devname
);
1061 LIST_FOREACH(event
, event
, manager
->events
) {
1062 const char *event_devname
;
1064 if (event
->state
!= EVENT_QUEUED
)
1067 if (event
->retry_again_next_usec
== 0)
1070 if (device_get_whole_disk(event
->dev
, NULL
, &event_devname
) <= 0)
1073 if (!streq(devname
, event_devname
))
1076 event
->retry_again_next_usec
= 0;
1082 static int event_queue_insert(Manager
*manager
, sd_device
*dev
) {
1083 const char *devpath
, *devpath_old
= NULL
, *id
= NULL
, *devnode
= NULL
;
1084 sd_device_action_t action
;
1092 /* only one process can add events to the queue */
1093 assert(manager
->pid
== getpid_cached());
1095 /* We only accepts devices received by device monitor. */
1096 r
= sd_device_get_seqnum(dev
, &seqnum
);
1100 r
= sd_device_get_action(dev
, &action
);
1104 r
= sd_device_get_devpath(dev
, &devpath
);
1108 r
= sd_device_get_property_value(dev
, "DEVPATH_OLD", &devpath_old
);
1109 if (r
< 0 && r
!= -ENOENT
)
1112 r
= device_get_device_id(dev
, &id
);
1113 if (r
< 0 && r
!= -ENOENT
)
1116 r
= sd_device_get_devname(dev
, &devnode
);
1117 if (r
< 0 && r
!= -ENOENT
)
1120 event
= new(Event
, 1);
1126 .dev
= sd_device_ref(dev
),
1131 .devpath_old
= devpath_old
,
1133 .state
= EVENT_QUEUED
,
1136 if (!manager
->events
) {
1137 r
= touch("/run/udev/queue");
1139 log_warning_errno(r
, "Failed to touch /run/udev/queue, ignoring: %m");
1142 LIST_APPEND(event
, manager
->events
, event
);
1144 log_device_uevent(dev
, "Device is queued");
1149 static int on_uevent(sd_device_monitor
*monitor
, sd_device
*dev
, void *userdata
) {
1150 Manager
*manager
= ASSERT_PTR(userdata
);
1153 DEVICE_TRACE_POINT(kernel_uevent_received
, dev
);
1155 device_ensure_usec_initialized(dev
, NULL
);
1157 r
= event_queue_insert(manager
, dev
);
1159 log_device_error_errno(dev
, r
, "Failed to insert device into event queue: %m");
1163 (void) event_queue_assume_block_device_unlocked(manager
, dev
);
1168 static int on_worker(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
1169 Manager
*manager
= ASSERT_PTR(userdata
);
1173 struct iovec iovec
= IOVEC_MAKE(&result
, sizeof(result
));
1174 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred
))) control
;
1175 struct msghdr msghdr
= {
1178 .msg_control
= &control
,
1179 .msg_controllen
= sizeof(control
),
1182 struct ucred
*ucred
;
1185 size
= recvmsg_safe(fd
, &msghdr
, MSG_DONTWAIT
);
1188 if (size
== -EAGAIN
)
1189 /* nothing more to read */
1192 return log_error_errno(size
, "Failed to receive message: %m");
1194 cmsg_close_all(&msghdr
);
1196 if (size
!= sizeof(result
)) {
1197 log_warning("Ignoring worker message with invalid size %zi bytes", size
);
1201 ucred
= CMSG_FIND_DATA(&msghdr
, SOL_SOCKET
, SCM_CREDENTIALS
, struct ucred
);
1202 if (!ucred
|| ucred
->pid
<= 0) {
1203 log_warning("Ignoring worker message without valid PID");
1207 /* lookup worker who sent the signal */
1208 worker
= hashmap_get(manager
->workers
, PID_TO_PTR(ucred
->pid
));
1210 log_debug("Worker ["PID_FMT
"] returned, but is no longer tracked", ucred
->pid
);
1214 if (worker
->state
== WORKER_KILLING
) {
1215 worker
->state
= WORKER_KILLED
;
1216 (void) kill(worker
->pid
, SIGTERM
);
1217 } else if (worker
->state
!= WORKER_KILLED
)
1218 worker
->state
= WORKER_IDLE
;
1220 /* worker returned */
1221 if (result
== EVENT_RESULT_TRY_AGAIN
&&
1222 event_requeue(worker
->event
) < 0)
1223 device_broadcast(manager
->monitor
, worker
->event
->dev
, -ETIMEDOUT
);
1225 /* When event_requeue() succeeds, worker->event is NULL, and event_free() handles NULL gracefully. */
1226 event_free(worker
->event
);
1232 /* receive the udevd message from userspace */
1233 static int on_ctrl_msg(UdevCtrl
*uctrl
, UdevCtrlMessageType type
, const UdevCtrlMessageValue
*value
, void *userdata
) {
1234 Manager
*manager
= ASSERT_PTR(userdata
);
1240 case UDEV_CTRL_SET_LOG_LEVEL
:
1241 if ((value
->intval
& LOG_PRIMASK
) != value
->intval
) {
1242 log_debug("Received invalid udev control message (SET_LOG_LEVEL, %i), ignoring.", value
->intval
);
1246 log_debug("Received udev control message (SET_LOG_LEVEL), setting log_level=%i", value
->intval
);
1248 r
= log_get_max_level();
1249 if (r
== value
->intval
)
1252 log_set_max_level(value
->intval
);
1253 manager
->log_level
= value
->intval
;
1254 manager_kill_workers(manager
, false);
1256 case UDEV_CTRL_STOP_EXEC_QUEUE
:
1257 log_debug("Received udev control message (STOP_EXEC_QUEUE)");
1258 manager
->stop_exec_queue
= true;
1260 case UDEV_CTRL_START_EXEC_QUEUE
:
1261 log_debug("Received udev control message (START_EXEC_QUEUE)");
1262 manager
->stop_exec_queue
= false;
1263 /* It is not necessary to call event_queue_start() here, as it will be called in on_post() if necessary. */
1265 case UDEV_CTRL_RELOAD
:
1266 log_debug("Received udev control message (RELOAD)");
1267 manager_reload(manager
, /* force = */ true);
1269 case UDEV_CTRL_SET_ENV
: {
1270 _unused_ _cleanup_free_
char *old_val
= NULL
;
1271 _cleanup_free_
char *key
= NULL
, *val
= NULL
, *old_key
= NULL
;
1274 eq
= strchr(value
->buf
, '=');
1276 log_error("Invalid key format '%s'", value
->buf
);
1280 key
= strndup(value
->buf
, eq
- value
->buf
);
1286 old_val
= hashmap_remove2(manager
->properties
, key
, (void **) &old_key
);
1288 r
= hashmap_ensure_allocated(&manager
->properties
, &string_hash_ops
);
1296 log_debug("Received udev control message (ENV), unsetting '%s'", key
);
1298 r
= hashmap_put(manager
->properties
, key
, NULL
);
1310 log_debug("Received udev control message (ENV), setting '%s=%s'", key
, val
);
1312 r
= hashmap_put(manager
->properties
, key
, val
);
1320 manager_kill_workers(manager
, false);
1323 case UDEV_CTRL_SET_CHILDREN_MAX
:
1324 if (value
->intval
<= 0) {
1325 log_debug("Received invalid udev control message (SET_MAX_CHILDREN, %i), ignoring.", value
->intval
);
1329 log_debug("Received udev control message (SET_MAX_CHILDREN), setting children_max=%i", value
->intval
);
1330 arg_children_max
= value
->intval
;
1334 case UDEV_CTRL_PING
:
1335 log_debug("Received udev control message (PING)");
1337 case UDEV_CTRL_EXIT
:
1338 log_debug("Received udev control message (EXIT)");
1339 manager_exit(manager
);
1342 log_debug("Received unknown udev control message, ignoring");
1348 static int synthesize_change_one(sd_device
*dev
, sd_device
*target
) {
1351 if (DEBUG_LOGGING
) {
1352 const char *syspath
= NULL
;
1353 (void) sd_device_get_syspath(target
, &syspath
);
1354 log_device_debug(dev
, "device is closed, synthesising 'change' on %s", strna(syspath
));
1357 r
= sd_device_trigger(target
, SD_DEVICE_CHANGE
);
1359 return log_device_debug_errno(target
, r
, "Failed to trigger 'change' uevent: %m");
1361 DEVICE_TRACE_POINT(synthetic_change_event
, dev
);
1366 static int synthesize_change(sd_device
*dev
) {
1367 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1368 bool part_table_read
;
1369 const char *sysname
;
1373 r
= sd_device_get_sysname(dev
, &sysname
);
1377 if (startswith(sysname
, "dm-") || block_device_is_whole_disk(dev
) <= 0)
1378 return synthesize_change_one(dev
, dev
);
1380 r
= blockdev_reread_partition_table(dev
);
1382 log_device_debug_errno(dev
, r
, "Failed to re-read partition table, ignoring: %m");
1383 part_table_read
= r
>= 0;
1385 /* search for partitions */
1386 r
= partition_enumerator_new(dev
, &e
);
1390 /* We have partitions and re-read the table, the kernel already sent out a "change"
1391 * event for the disk, and "remove/add" for all partitions. */
1392 if (part_table_read
&& sd_device_enumerator_get_device_first(e
))
1395 /* We have partitions but re-reading the partition table did not work, synthesize
1396 * "change" for the disk and all partitions. */
1397 r
= synthesize_change_one(dev
, dev
);
1398 FOREACH_DEVICE(e
, d
) {
1399 k
= synthesize_change_one(dev
, d
);
1400 if (k
< 0 && r
>= 0)
1407 static int on_inotify(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
1408 Manager
*manager
= ASSERT_PTR(userdata
);
1409 union inotify_event_buffer buffer
;
1413 l
= read(fd
, &buffer
, sizeof(buffer
));
1415 if (ERRNO_IS_TRANSIENT(errno
))
1418 return log_error_errno(errno
, "Failed to read inotify fd: %m");
1421 FOREACH_INOTIFY_EVENT_WARN(e
, buffer
, l
) {
1422 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
1423 const char *devnode
;
1425 /* Do not handle IN_IGNORED here. Especially, do not try to call udev_watch_end() from the
1426 * main process. Otherwise, the pair of the symlinks may become inconsistent, and several
1427 * garbage may remain. The old symlinks are removed by a worker that processes the
1428 * corresponding 'remove' uevent;
1429 * udev_event_execute_rules() -> event_execute_rules_on_remove() -> udev_watch_end(). */
1431 if (!FLAGS_SET(e
->mask
, IN_CLOSE_WRITE
))
1434 r
= device_new_from_watch_handle(&dev
, e
->wd
);
1436 /* Device may be removed just after closed. */
1437 log_debug_errno(r
, "Failed to create sd_device object from watch handle, ignoring: %m");
1441 r
= sd_device_get_devname(dev
, &devnode
);
1443 /* Also here, device may be already removed. */
1444 log_device_debug_errno(dev
, r
, "Failed to get device node, ignoring: %m");
1448 log_device_debug(dev
, "Received inotify event for %s.", devnode
);
1450 (void) event_queue_assume_block_device_unlocked(manager
, dev
);
1451 (void) synthesize_change(dev
);
1457 static int on_sigterm(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1458 Manager
*manager
= ASSERT_PTR(userdata
);
1460 manager_exit(manager
);
1465 static int on_sighup(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1466 Manager
*manager
= ASSERT_PTR(userdata
);
1468 manager_reload(manager
, /* force = */ true);
1473 static int on_sigchld(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
1474 Worker
*worker
= ASSERT_PTR(userdata
);
1475 Manager
*manager
= ASSERT_PTR(worker
->manager
);
1476 sd_device
*dev
= worker
->event
? ASSERT_PTR(worker
->event
->dev
) : NULL
;
1481 switch (si
->si_code
) {
1483 if (si
->si_status
== 0)
1484 log_device_debug(dev
, "Worker ["PID_FMT
"] exited.", si
->si_pid
);
1486 log_device_warning(dev
, "Worker ["PID_FMT
"] exited with return code %i.",
1487 si
->si_pid
, si
->si_status
);
1488 result
= EVENT_RESULT_EXIT_STATUS_BASE
+ si
->si_status
;
1493 log_device_warning(dev
, "Worker ["PID_FMT
"] terminated by signal %i (%s).",
1494 si
->si_pid
, si
->si_status
, signal_to_string(si
->si_status
));
1495 result
= EVENT_RESULT_SIGNAL_BASE
+ si
->si_status
;
1499 assert_not_reached();
1502 if (result
!= EVENT_RESULT_SUCCESS
&& dev
) {
1503 /* delete state from disk */
1504 device_delete_db(dev
);
1505 device_tag_index(dev
, NULL
, false);
1507 /* Forward kernel event to libudev listeners */
1508 device_broadcast(manager
->monitor
, dev
, result
);
1511 worker_free(worker
);
1516 static int on_post(sd_event_source
*s
, void *userdata
) {
1517 Manager
*manager
= ASSERT_PTR(userdata
);
1519 if (manager
->events
) {
1520 /* Try to process pending events if idle workers exist. Why is this necessary?
1521 * When a worker finished an event and became idle, even if there was a pending event,
1522 * the corresponding device might have been locked and the processing of the event
1523 * delayed for a while, preventing the worker from processing the event immediately.
1524 * Now, the device may be unlocked. Let's try again! */
1525 event_queue_start(manager
);
1529 /* There are no queued events. Let's remove /run/udev/queue and clean up the idle processes. */
1531 if (unlink("/run/udev/queue") < 0) {
1532 if (errno
!= ENOENT
)
1533 log_warning_errno(errno
, "Failed to unlink /run/udev/queue, ignoring: %m");
1535 log_debug("No events are queued, removing /run/udev/queue.");
1537 if (!hashmap_isempty(manager
->workers
)) {
1538 /* There are idle workers */
1539 (void) event_reset_time_relative(manager
->event
, &manager
->kill_workers_event
,
1540 CLOCK_MONOTONIC
, 3 * USEC_PER_SEC
, USEC_PER_SEC
,
1541 on_kill_workers_event
, manager
,
1542 0, "kill-workers-event", false);
1546 /* There are no idle workers. */
1548 if (manager
->udev_node_needs_cleanup
) {
1549 (void) udev_node_cleanup();
1550 manager
->udev_node_needs_cleanup
= false;
1554 return sd_event_exit(manager
->event
, 0);
1556 if (manager
->cgroup
)
1557 /* cleanup possible left-over processes in our cgroup */
1558 (void) cg_kill(SYSTEMD_CGROUP_CONTROLLER
, manager
->cgroup
, SIGKILL
, CGROUP_IGNORE_SELF
, NULL
, NULL
, NULL
);
1563 static int listen_fds(int *ret_ctrl
, int *ret_netlink
) {
1564 int ctrl_fd
= -1, netlink_fd
= -1;
1568 assert(ret_netlink
);
1570 n
= sd_listen_fds(true);
1574 for (fd
= SD_LISTEN_FDS_START
; fd
< n
+ SD_LISTEN_FDS_START
; fd
++) {
1575 if (sd_is_socket(fd
, AF_UNIX
, SOCK_SEQPACKET
, -1) > 0) {
1582 if (sd_is_socket(fd
, AF_NETLINK
, SOCK_RAW
, -1) > 0) {
1583 if (netlink_fd
>= 0)
1592 *ret_ctrl
= ctrl_fd
;
1593 *ret_netlink
= netlink_fd
;
1599 * read the kernel command line, in case we need to get into debug mode
1600 * udev.log_level=<level> syslog priority
1601 * udev.children_max=<number of workers> events are fully serialized if set to 1
1602 * udev.exec_delay=<number of seconds> delay execution of every executed program
1603 * udev.event_timeout=<number of seconds> seconds to wait before terminating an event
1604 * udev.blockdev_read_only<=bool> mark all block devices read-only when they appear
1606 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
1611 if (proc_cmdline_key_streq(key
, "udev.log_level") ||
1612 proc_cmdline_key_streq(key
, "udev.log_priority")) { /* kept for backward compatibility */
1614 if (proc_cmdline_value_missing(key
, value
))
1617 r
= log_level_from_string(value
);
1619 log_set_max_level(r
);
1621 } else if (proc_cmdline_key_streq(key
, "udev.event_timeout")) {
1623 if (proc_cmdline_value_missing(key
, value
))
1626 r
= parse_sec(value
, &arg_event_timeout_usec
);
1628 } else if (proc_cmdline_key_streq(key
, "udev.children_max")) {
1630 if (proc_cmdline_value_missing(key
, value
))
1633 r
= safe_atou(value
, &arg_children_max
);
1635 } else if (proc_cmdline_key_streq(key
, "udev.exec_delay")) {
1637 if (proc_cmdline_value_missing(key
, value
))
1640 r
= parse_sec(value
, &arg_exec_delay_usec
);
1642 } else if (proc_cmdline_key_streq(key
, "udev.timeout_signal")) {
1644 if (proc_cmdline_value_missing(key
, value
))
1647 r
= signal_from_string(value
);
1649 arg_timeout_signal
= r
;
1651 } else if (proc_cmdline_key_streq(key
, "udev.blockdev_read_only")) {
1654 arg_blockdev_read_only
= true;
1656 r
= parse_boolean(value
);
1658 log_warning_errno(r
, "Failed to parse udev.blockdev-read-only argument, ignoring: %s", value
);
1660 arg_blockdev_read_only
= r
;
1663 if (arg_blockdev_read_only
)
1664 log_notice("All physical block devices will be marked read-only.");
1669 if (startswith(key
, "udev."))
1670 log_warning("Unknown udev kernel command line option \"%s\", ignoring.", key
);
1676 log_warning_errno(r
, "Failed to parse \"%s=%s\", ignoring: %m", key
, value
);
1681 static int help(void) {
1682 _cleanup_free_
char *link
= NULL
;
1685 r
= terminal_urlify_man("systemd-udevd.service", "8", &link
);
1689 printf("%s [OPTIONS...]\n\n"
1690 "Rule-based manager for device events and files.\n\n"
1691 " -h --help Print this message\n"
1692 " -V --version Print version of the program\n"
1693 " -d --daemon Detach and run in the background\n"
1694 " -D --debug Enable debug output\n"
1695 " -c --children-max=INT Set maximum number of workers\n"
1696 " -e --exec-delay=SECONDS Seconds to wait before executing RUN=\n"
1697 " -t --event-timeout=SECONDS Seconds to wait before terminating an event\n"
1698 " -N --resolve-names=early|late|never\n"
1699 " When to resolve users and groups\n"
1700 "\nSee the %s for details.\n",
1701 program_invocation_short_name
,
1707 static int parse_argv(int argc
, char *argv
[]) {
1712 static const struct option options
[] = {
1713 { "daemon", no_argument
, NULL
, 'd' },
1714 { "debug", no_argument
, NULL
, 'D' },
1715 { "children-max", required_argument
, NULL
, 'c' },
1716 { "exec-delay", required_argument
, NULL
, 'e' },
1717 { "event-timeout", required_argument
, NULL
, 't' },
1718 { "resolve-names", required_argument
, NULL
, 'N' },
1719 { "help", no_argument
, NULL
, 'h' },
1720 { "version", no_argument
, NULL
, 'V' },
1721 { "timeout-signal", required_argument
, NULL
, ARG_TIMEOUT_SIGNAL
},
1730 while ((c
= getopt_long(argc
, argv
, "c:de:Dt:N:hV", options
, NULL
)) >= 0) {
1734 arg_daemonize
= true;
1737 r
= safe_atou(optarg
, &arg_children_max
);
1739 log_warning_errno(r
, "Failed to parse --children-max= value '%s', ignoring: %m", optarg
);
1742 r
= parse_sec(optarg
, &arg_exec_delay_usec
);
1744 log_warning_errno(r
, "Failed to parse --exec-delay= value '%s', ignoring: %m", optarg
);
1746 case ARG_TIMEOUT_SIGNAL
:
1747 r
= signal_from_string(optarg
);
1749 log_warning_errno(r
, "Failed to parse --timeout-signal= value '%s', ignoring: %m", optarg
);
1751 arg_timeout_signal
= r
;
1755 r
= parse_sec(optarg
, &arg_event_timeout_usec
);
1757 log_warning_errno(r
, "Failed to parse --event-timeout= value '%s', ignoring: %m", optarg
);
1763 ResolveNameTiming t
;
1765 t
= resolve_name_timing_from_string(optarg
);
1767 log_warning("Invalid --resolve-names= value '%s', ignoring.", optarg
);
1769 arg_resolve_name_timing
= t
;
1775 printf("%s\n", GIT_VERSION
);
1780 assert_not_reached();
1788 static int create_subcgroup(char **ret
) {
1789 _cleanup_free_
char *cgroup
= NULL
, *subcgroup
= NULL
;
1793 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Not invoked by PID1.");
1797 return log_debug_errno(r
, "Failed to check if systemd is running: %m");
1799 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "systemd is not running.");
1801 /* Get our own cgroup, we regularly kill everything udev has left behind.
1802 * We only do this on systemd systems, and only if we are directly spawned
1803 * by PID1. Otherwise we are not guaranteed to have a dedicated cgroup. */
1805 r
= cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER
, 0, &cgroup
);
1807 if (IN_SET(r
, -ENOENT
, -ENOMEDIUM
))
1808 return log_debug_errno(r
, "Dedicated cgroup not found: %m");
1809 return log_debug_errno(r
, "Failed to get cgroup: %m");
1812 r
= cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER
, cgroup
, "trusted.delegate");
1813 if (r
== 0 || (r
< 0 && ERRNO_IS_XATTR_ABSENT(r
)))
1814 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "The cgroup %s is not delegated to us.", cgroup
);
1816 return log_debug_errno(r
, "Failed to read trusted.delegate attribute: %m");
1818 /* We are invoked with our own delegated cgroup tree, let's move us one level down, so that we
1819 * don't collide with the "no processes in inner nodes" rule of cgroups, when the service
1820 * manager invokes the ExecReload= job in the .control/ subcgroup. */
1822 subcgroup
= path_join(cgroup
, "/udev");
1824 return log_oom_debug();
1826 r
= cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER
, subcgroup
, 0);
1828 return log_debug_errno(r
, "Failed to create %s subcgroup: %m", subcgroup
);
1830 log_debug("Created %s subcgroup.", subcgroup
);
1832 *ret
= TAKE_PTR(subcgroup
);
1836 static int manager_new(Manager
**ret
, int fd_ctrl
, int fd_uevent
) {
1837 _cleanup_(manager_freep
) Manager
*manager
= NULL
;
1838 _cleanup_free_
char *cgroup
= NULL
;
1843 (void) create_subcgroup(&cgroup
);
1845 manager
= new(Manager
, 1);
1849 *manager
= (Manager
) {
1851 .worker_watch
= { -1, -1 },
1852 .cgroup
= TAKE_PTR(cgroup
),
1855 r
= udev_ctrl_new_from_fd(&manager
->ctrl
, fd_ctrl
);
1857 return log_error_errno(r
, "Failed to initialize udev control socket: %m");
1859 r
= udev_ctrl_enable_receiving(manager
->ctrl
);
1861 return log_error_errno(r
, "Failed to bind udev control socket: %m");
1863 r
= device_monitor_new_full(&manager
->monitor
, MONITOR_GROUP_KERNEL
, fd_uevent
);
1865 return log_error_errno(r
, "Failed to initialize device monitor: %m");
1867 /* Bump receiver buffer, but only if we are not called via socket activation, as in that
1868 * case systemd sets the receive buffer size for us, and the value in the .socket unit
1869 * should take full effect. */
1870 if (fd_uevent
< 0) {
1871 r
= sd_device_monitor_set_receive_buffer_size(manager
->monitor
, 128 * 1024 * 1024);
1873 log_warning_errno(r
, "Failed to set receive buffer size for device monitor, ignoring: %m");
1876 (void) sd_device_monitor_set_description(manager
->monitor
, "manager");
1878 r
= device_monitor_enable_receiving(manager
->monitor
);
1880 return log_error_errno(r
, "Failed to bind netlink socket: %m");
1882 manager
->log_level
= log_get_max_level();
1884 *ret
= TAKE_PTR(manager
);
1889 static int main_loop(Manager
*manager
) {
1892 manager
->pid
= getpid_cached();
1894 /* unnamed socket from workers to the main daemon */
1895 r
= socketpair(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
, 0, manager
->worker_watch
);
1897 return log_error_errno(errno
, "Failed to create socketpair for communicating with workers: %m");
1899 fd_worker
= manager
->worker_watch
[READ_END
];
1901 r
= setsockopt_int(fd_worker
, SOL_SOCKET
, SO_PASSCRED
, true);
1903 return log_error_errno(r
, "Failed to enable SO_PASSCRED: %m");
1905 manager
->inotify_fd
= inotify_init1(IN_CLOEXEC
);
1906 if (manager
->inotify_fd
< 0)
1907 return log_error_errno(errno
, "Failed to create inotify descriptor: %m");
1909 udev_watch_restore(manager
->inotify_fd
);
1911 /* block and listen to all signals on signalfd */
1912 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, SIGHUP
, SIGCHLD
, -1) >= 0);
1914 r
= sd_event_default(&manager
->event
);
1916 return log_error_errno(r
, "Failed to allocate event loop: %m");
1918 r
= sd_event_add_signal(manager
->event
, NULL
, SIGINT
, on_sigterm
, manager
);
1920 return log_error_errno(r
, "Failed to create SIGINT event source: %m");
1922 r
= sd_event_add_signal(manager
->event
, NULL
, SIGTERM
, on_sigterm
, manager
);
1924 return log_error_errno(r
, "Failed to create SIGTERM event source: %m");
1926 r
= sd_event_add_signal(manager
->event
, NULL
, SIGHUP
, on_sighup
, manager
);
1928 return log_error_errno(r
, "Failed to create SIGHUP event source: %m");
1930 r
= sd_event_set_watchdog(manager
->event
, true);
1932 return log_error_errno(r
, "Failed to create watchdog event source: %m");
1934 r
= udev_ctrl_attach_event(manager
->ctrl
, manager
->event
);
1936 return log_error_errno(r
, "Failed to attach event to udev control: %m");
1938 r
= udev_ctrl_start(manager
->ctrl
, on_ctrl_msg
, manager
);
1940 return log_error_errno(r
, "Failed to start device monitor: %m");
1942 /* This needs to be after the inotify and uevent handling, to make sure
1943 * that the ping is send back after fully processing the pending uevents
1944 * (including the synthetic ones we may create due to inotify events).
1946 r
= sd_event_source_set_priority(udev_ctrl_get_event_source(manager
->ctrl
), SD_EVENT_PRIORITY_IDLE
);
1948 return log_error_errno(r
, "Failed to set IDLE event priority for udev control event source: %m");
1950 r
= sd_event_add_io(manager
->event
, &manager
->inotify_event
, manager
->inotify_fd
, EPOLLIN
, on_inotify
, manager
);
1952 return log_error_errno(r
, "Failed to create inotify event source: %m");
1954 r
= sd_device_monitor_attach_event(manager
->monitor
, manager
->event
);
1956 return log_error_errno(r
, "Failed to attach event to device monitor: %m");
1958 r
= sd_device_monitor_start(manager
->monitor
, on_uevent
, manager
);
1960 return log_error_errno(r
, "Failed to start device monitor: %m");
1962 r
= sd_event_add_io(manager
->event
, NULL
, fd_worker
, EPOLLIN
, on_worker
, manager
);
1964 return log_error_errno(r
, "Failed to create worker event source: %m");
1966 r
= sd_event_add_post(manager
->event
, NULL
, on_post
, manager
);
1968 return log_error_errno(r
, "Failed to create post event source: %m");
1970 manager
->last_usec
= now(CLOCK_MONOTONIC
);
1972 udev_builtin_init();
1974 r
= udev_rules_load(&manager
->rules
, arg_resolve_name_timing
);
1976 return log_error_errno(r
, "Failed to read udev rules: %m");
1978 r
= udev_rules_apply_static_dev_perms(manager
->rules
);
1980 log_warning_errno(r
, "Failed to apply permissions on static device nodes, ignoring: %m");
1984 r
= sd_event_loop(manager
->event
);
1986 log_error_errno(r
, "Event loop failed: %m");
1990 "STATUS=Shutting down...");
1994 int run_udevd(int argc
, char *argv
[]) {
1995 _cleanup_(manager_freep
) Manager
*manager
= NULL
;
1996 int fd_ctrl
= -1, fd_uevent
= -1;
1999 log_set_target(LOG_TARGET_AUTO
);
2001 udev_parse_config_full(&arg_children_max
, &arg_exec_delay_usec
, &arg_event_timeout_usec
, &arg_resolve_name_timing
, &arg_timeout_signal
);
2002 log_parse_environment();
2003 log_open(); /* Done again to update after reading configuration. */
2005 r
= parse_argv(argc
, argv
);
2009 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
2011 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
2014 log_set_target(LOG_TARGET_CONSOLE
);
2015 log_set_max_level(LOG_DEBUG
);
2022 if (arg_children_max
== 0) {
2023 unsigned long cpu_limit
, mem_limit
, cpu_count
= 1;
2025 r
= cpus_in_affinity_mask();
2027 log_warning_errno(r
, "Failed to determine number of local CPUs, ignoring: %m");
2031 cpu_limit
= cpu_count
* 2 + 16;
2032 mem_limit
= MAX(physical_memory() / (128UL*1024*1024), 10U);
2034 arg_children_max
= MIN(cpu_limit
, mem_limit
);
2035 arg_children_max
= MIN(WORKER_NUM_MAX
, arg_children_max
);
2037 log_debug("Set children_max to %u", arg_children_max
);
2040 /* set umask before creating any file/directory */
2043 r
= mac_selinux_init();
2047 r
= RET_NERRNO(mkdir("/run/udev", 0755));
2048 if (r
< 0 && r
!= -EEXIST
)
2049 return log_error_errno(r
, "Failed to create /run/udev: %m");
2051 r
= listen_fds(&fd_ctrl
, &fd_uevent
);
2053 return log_error_errno(r
, "Failed to listen on fds: %m");
2055 r
= manager_new(&manager
, fd_ctrl
, fd_uevent
);
2057 return log_error_errno(r
, "Failed to create manager: %m");
2059 if (arg_daemonize
) {
2062 log_info("Starting systemd-udevd version " GIT_VERSION
);
2064 /* connect /dev/null to stdin, stdout, stderr */
2065 if (log_get_max_level() < LOG_DEBUG
) {
2066 r
= make_null_stdio();
2068 log_warning_errno(r
, "Failed to redirect standard streams to /dev/null: %m");
2073 return log_error_errno(errno
, "Failed to fork daemon: %m");
2082 return main_loop(manager
);