From: Yu Watanabe Date: Tue, 18 Jul 2023 17:11:16 +0000 (+0900) Subject: udev: split-out worker code from udevd.c X-Git-Tag: v255-rc1~870^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=987b90f93f59ac5f4a4240c17f7bb285e2f0859a;p=thirdparty%2Fsystemd.git udev: split-out worker code from udevd.c While doing that, the Manager object allocated by the main process is now _not_ freed in the spawned worker process, to follow our usual coding style. See discussion in #24043. --- diff --git a/src/udev/meson.build b/src/udev/meson.build index 1828c584707..5687b70a1e7 100644 --- a/src/udev/meson.build +++ b/src/udev/meson.build @@ -25,6 +25,7 @@ libudevd_core_sources = files( 'udev-rules.c', 'udev-spawn.c', 'udev-watch.c', + 'udev-worker.c', 'udev-builtin-btrfs.c', 'udev-builtin-hwdb.c', 'udev-builtin-input_id.c', diff --git a/src/udev/udev-worker.c b/src/udev/udev-worker.c new file mode 100644 index 00000000000..996406a142d --- /dev/null +++ b/src/udev/udev-worker.c @@ -0,0 +1,352 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include +#include +#include + +#include "alloc-util.h" +#include "blockdev-util.h" +#include "common-signal.h" +#include "device-monitor-private.h" +#include "device-private.h" +#include "device-util.h" +#include "errno-util.h" +#include "fd-util.h" +#include "io-util.h" +#include "path-util.h" +#include "process-util.h" +#include "signal-util.h" +#include "string-util.h" +#include "udev-event.h" +#include "udev-spawn.h" +#include "udev-trace.h" +#include "udev-util.h" +#include "udev-watch.h" +#include "udev-worker.h" + +void udev_worker_done(UdevWorker *worker) { + assert(worker); + + sd_event_unref(worker->event); + sd_netlink_unref(worker->rtnl); + sd_device_monitor_unref(worker->monitor); + hashmap_free(worker->properties); + udev_rules_free(worker->rules); + safe_close(worker->pipe_fd); +} + +int udev_get_whole_disk(sd_device *dev, sd_device **ret_device, const char **ret_devname) { + const char *val; + int r; + + assert(dev); + + if (device_for_action(dev, SD_DEVICE_REMOVE)) + goto irrelevant; + + r = sd_device_get_sysname(dev, &val); + if (r < 0) + return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); + + /* Exclude the following devices: + * For "dm-", see the comment added by e918a1b5a94f270186dca59156354acd2a596494. + * For "md", see the commit message of 2e5b17d01347d3c3118be2b8ad63d20415dbb1f0, + * but not sure the assumption is still valid even when partitions are created on the md + * devices, surprisingly which seems to be possible, see PR #22973. + * For "drbd", see the commit message of fee854ee8ccde0cd28e0f925dea18cce35f3993d. */ + if (STARTSWITH_SET(val, "dm-", "md", "drbd")) + goto irrelevant; + + r = block_device_get_whole_disk(dev, &dev); + if (IN_SET(r, + -ENOTBLK, /* The device is not a block device. */ + -ENODEV /* The whole disk device was not found, it may already be removed. */)) + goto irrelevant; + if (r < 0) + return log_device_debug_errno(dev, r, "Failed to get whole disk device: %m"); + + r = sd_device_get_devname(dev, &val); + if (r < 0) + return log_device_debug_errno(dev, r, "Failed to get devname: %m"); + + if (ret_device) + *ret_device = dev; + if (ret_devname) + *ret_devname = val; + return 1; + +irrelevant: + if (ret_device) + *ret_device = NULL; + if (ret_devname) + *ret_devname = NULL; + return 0; +} + +static int worker_lock_whole_disk(sd_device *dev, int *ret_fd) { + _cleanup_close_ int fd = -EBADF; + sd_device *dev_whole_disk; + const char *val; + int r; + + assert(dev); + assert(ret_fd); + + /* Take a shared lock on the device node; this establishes a concept of device "ownership" to + * serialize device access. External processes holding an exclusive lock will cause udev to skip the + * event handling; in the case udev acquired the lock, the external process can block until udev has + * finished its event handling. */ + + r = udev_get_whole_disk(dev, &dev_whole_disk, &val); + if (r < 0) + return r; + if (r == 0) + goto nolock; + + fd = sd_device_open(dev_whole_disk, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); + if (fd < 0) { + bool ignore = ERRNO_IS_DEVICE_ABSENT(fd); + + log_device_debug_errno(dev, fd, "Failed to open '%s'%s: %m", val, ignore ? ", ignoring" : ""); + if (!ignore) + return fd; + + goto nolock; + } + + if (flock(fd, LOCK_SH|LOCK_NB) < 0) + return log_device_debug_errno(dev, errno, "Failed to flock(%s): %m", val); + + *ret_fd = TAKE_FD(fd); + return 1; + +nolock: + *ret_fd = -EBADF; + return 0; +} + +static int worker_mark_block_device_read_only(sd_device *dev) { + _cleanup_close_ int fd = -EBADF; + const char *val; + int state = 1, r; + + assert(dev); + + /* Do this only once, when the block device is new. If the device is later retriggered let's not + * toggle the bit again, so that people can boot up with full read-only mode and then unset the bit + * for specific devices only. */ + if (!device_for_action(dev, SD_DEVICE_ADD)) + return 0; + + r = sd_device_get_subsystem(dev, &val); + if (r < 0) + return log_device_debug_errno(dev, r, "Failed to get subsystem: %m"); + + if (!streq(val, "block")) + return 0; + + r = sd_device_get_sysname(dev, &val); + if (r < 0) + return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); + + /* Exclude synthetic devices for now, this is supposed to be a safety feature to avoid modification + * of physical devices, and what sits on top of those doesn't really matter if we don't allow the + * underlying block devices to receive changes. */ + if (STARTSWITH_SET(val, "dm-", "md", "drbd", "loop", "nbd", "zram")) + return 0; + + fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); + if (fd < 0) + return log_device_debug_errno(dev, fd, "Failed to open '%s', ignoring: %m", val); + + if (ioctl(fd, BLKROSET, &state) < 0) + return log_device_warning_errno(dev, errno, "Failed to mark block device '%s' read-only: %m", val); + + log_device_info(dev, "Successfully marked block device '%s' read-only.", val); + return 0; +} + +static int worker_process_device(UdevWorker *worker, sd_device *dev) { + _cleanup_(udev_event_freep) UdevEvent *udev_event = NULL; + _cleanup_close_ int fd_lock = -EBADF; + int r; + + assert(worker); + assert(dev); + + log_device_uevent(dev, "Processing device"); + + udev_event = udev_event_new(dev, worker->exec_delay_usec, worker->rtnl, worker->log_level); + if (!udev_event) + return -ENOMEM; + + /* If this is a block device and the device is locked currently via the BSD advisory locks, + * someone else is using it exclusively. We don't run our udev rules now to not interfere. + * Instead of processing the event, we requeue the event and will try again after a delay. + * + * The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING */ + r = worker_lock_whole_disk(dev, &fd_lock); + if (r == -EAGAIN) + return EVENT_RESULT_TRY_AGAIN; + if (r < 0) + return r; + + if (worker->blockdev_read_only) + (void) worker_mark_block_device_read_only(dev); + + /* apply rules, create node, symlinks */ + r = udev_event_execute_rules( + udev_event, + worker->inotify_fd, + worker->timeout_usec, + worker->timeout_signal, + worker->properties, + worker->rules); + if (r < 0) + return r; + + udev_event_execute_run(udev_event, worker->timeout_usec, worker->timeout_signal); + + if (!worker->rtnl) + /* in case rtnl was initialized */ + worker->rtnl = sd_netlink_ref(udev_event->rtnl); + + if (udev_event->inotify_watch) { + r = udev_watch_begin(worker->inotify_fd, dev); + if (r < 0 && r != -ENOENT) /* The device may be already removed, ignore -ENOENT. */ + log_device_warning_errno(dev, r, "Failed to add inotify watch, ignoring: %m"); + } + + log_device_uevent(dev, "Device processed"); + return 0; +} + +void udev_broadcast_result(sd_device_monitor *monitor, sd_device *dev, EventResult result) { + int r; + + assert(dev); + + /* On exit, manager->monitor is already NULL. */ + if (!monitor) + return; + + if (result != EVENT_RESULT_SUCCESS) { + (void) device_add_property(dev, "UDEV_WORKER_FAILED", "1"); + + switch (result) { + case EVENT_RESULT_NERRNO_MIN ... EVENT_RESULT_NERRNO_MAX: { + const char *str; + + (void) device_add_propertyf(dev, "UDEV_WORKER_ERRNO", "%i", -result); + + str = errno_to_name(result); + if (str) + (void) device_add_property(dev, "UDEV_WORKER_ERRNO_NAME", str); + break; + } + case EVENT_RESULT_EXIT_STATUS_BASE ... EVENT_RESULT_EXIT_STATUS_MAX: + (void) device_add_propertyf(dev, "UDEV_WORKER_EXIT_STATUS", "%i", result - EVENT_RESULT_EXIT_STATUS_BASE); + break; + + case EVENT_RESULT_TRY_AGAIN: + assert_not_reached(); + break; + + case EVENT_RESULT_SIGNAL_BASE ... EVENT_RESULT_SIGNAL_MAX: { + const char *str; + + (void) device_add_propertyf(dev, "UDEV_WORKER_SIGNAL", "%i", result - EVENT_RESULT_SIGNAL_BASE); + + str = signal_to_string(result - EVENT_RESULT_SIGNAL_BASE); + if (str) + (void) device_add_property(dev, "UDEV_WORKER_SIGNAL_NAME", str); + break; + } + default: + log_device_warning(dev, "Unknown event result \"%i\", ignoring.", result); + } + } + + r = device_monitor_send_device(monitor, NULL, dev); + if (r < 0) + log_device_warning_errno(dev, r, + "Failed to broadcast event to libudev listeners, ignoring: %m"); +} + +static int worker_send_result(UdevWorker *worker, EventResult result) { + assert(worker); + assert(worker->pipe_fd >= 0); + + return loop_write(worker->pipe_fd, &result, sizeof(result), /* do_poll = */ false); +} + +static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *dev, void *userdata) { + UdevWorker *worker = ASSERT_PTR(userdata); + int r; + + assert(dev); + + r = worker_process_device(worker, dev); + if (r == EVENT_RESULT_TRY_AGAIN) + /* if we couldn't acquire the flock(), then requeue the event */ + log_device_debug(dev, "Block device is currently locked, requeueing the event."); + else { + if (r < 0) + log_device_warning_errno(dev, r, "Failed to process device, ignoring: %m"); + + /* send processed event back to libudev listeners */ + udev_broadcast_result(monitor, dev, r); + } + + /* send udevd the result of the event execution */ + r = worker_send_result(worker, r); + if (r < 0) + log_device_warning_errno(dev, r, "Failed to send signal to main daemon, ignoring: %m"); + + /* Reset the log level, as it might be changed by "OPTIONS=log_level=". */ + log_set_max_level(worker->log_level); + + return 1; +} + +int udev_worker_main(UdevWorker *worker, sd_device *dev) { + int r; + + assert(worker); + assert(worker->monitor); + assert(dev); + + DEVICE_TRACE_POINT(worker_spawned, dev, getpid_cached()); + + assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, -1) >= 0); + + /* Reset OOM score, we only protect the main daemon. */ + r = set_oom_score_adjust(0); + if (r < 0) + log_debug_errno(r, "Failed to reset OOM score, ignoring: %m"); + + r = sd_event_new(&worker->event); + if (r < 0) + return log_error_errno(r, "Failed to allocate event loop: %m"); + + r = sd_event_add_signal(worker->event, NULL, SIGTERM, NULL, NULL); + if (r < 0) + return log_error_errno(r, "Failed to set SIGTERM event: %m"); + + r = sd_device_monitor_attach_event(worker->monitor, worker->event); + if (r < 0) + return log_error_errno(r, "Failed to attach event loop to device monitor: %m"); + + r = sd_device_monitor_start(worker->monitor, worker_device_monitor_handler, worker); + if (r < 0) + return log_error_errno(r, "Failed to start device monitor: %m"); + + /* Process first device */ + (void) worker_device_monitor_handler(worker->monitor, dev, worker); + + r = sd_event_loop(worker->event); + if (r < 0) + return log_error_errno(r, "Event loop failed: %m"); + + return 0; +} diff --git a/src/udev/udev-worker.h b/src/udev/udev-worker.h new file mode 100644 index 00000000000..05c319e3097 --- /dev/null +++ b/src/udev/udev-worker.h @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#pragma once + +#include + +#include "sd-device.h" +#include "sd-event.h" +#include "sd-netlink.h" + +#include "errno-list.h" +#include "hashmap.h" +#include "time-util.h" + +typedef struct UdevRules UdevRules; + +typedef struct UdevWorker { + sd_event *event; + sd_netlink *rtnl; + sd_device_monitor *monitor; + + Hashmap *properties; + UdevRules *rules; + + int pipe_fd; + int inotify_fd; /* Do not close! */ + + usec_t exec_delay_usec; + usec_t timeout_usec; + int timeout_signal; + int log_level; + bool blockdev_read_only; +} UdevWorker; + +/* passed from worker to main process */ +typedef enum EventResult { + EVENT_RESULT_NERRNO_MIN = -ERRNO_MAX, + EVENT_RESULT_NERRNO_MAX = -1, + EVENT_RESULT_SUCCESS = 0, + EVENT_RESULT_EXIT_STATUS_BASE = 0, + EVENT_RESULT_EXIT_STATUS_MAX = 255, + EVENT_RESULT_TRY_AGAIN = 256, /* when the block device is locked by another process. */ + EVENT_RESULT_SIGNAL_BASE = 257, + EVENT_RESULT_SIGNAL_MAX = EVENT_RESULT_SIGNAL_BASE + _NSIG, + _EVENT_RESULT_MAX, + _EVENT_RESULT_INVALID = -EINVAL, +} EventResult; + +void udev_worker_done(UdevWorker *worker); +int udev_worker_main(UdevWorker *worker, sd_device *dev); + +void udev_broadcast_result(sd_device_monitor *monitor, sd_device *dev, EventResult result); +int udev_get_whole_disk(sd_device *dev, sd_device **ret_device, const char **ret_devname); diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 4fd6db80474..8c0ceccfd9c 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -72,6 +72,7 @@ #include "udev-trace.h" #include "udev-util.h" #include "udev-watch.h" +#include "udev-worker.h" #include "udevd.h" #include "user-util.h" #include "version.h" @@ -174,20 +175,6 @@ typedef struct Worker { Event *event; } Worker; -/* passed from worker to main process */ -typedef enum EventResult { - EVENT_RESULT_NERRNO_MIN = -ERRNO_MAX, - EVENT_RESULT_NERRNO_MAX = -1, - EVENT_RESULT_SUCCESS = 0, - EVENT_RESULT_EXIT_STATUS_BASE = 0, - EVENT_RESULT_EXIT_STATUS_MAX = 255, - EVENT_RESULT_TRY_AGAIN = 256, /* when the block device is locked by another process. */ - EVENT_RESULT_SIGNAL_BASE = 257, - EVENT_RESULT_SIGNAL_MAX = EVENT_RESULT_SIGNAL_BASE + _NSIG, - _EVENT_RESULT_MAX, - _EVENT_RESULT_INVALID = -EINVAL, -} EventResult; - static Event *event_free(Event *event) { if (!event) return NULL; @@ -423,327 +410,6 @@ static int on_kill_workers_event(sd_event_source *s, uint64_t usec, void *userda return 1; } -static void device_broadcast(sd_device_monitor *monitor, sd_device *dev, EventResult result) { - int r; - - assert(dev); - - /* On exit, manager->monitor is already NULL. */ - if (!monitor) - return; - - if (result != EVENT_RESULT_SUCCESS) { - (void) device_add_property(dev, "UDEV_WORKER_FAILED", "1"); - - switch (result) { - case EVENT_RESULT_NERRNO_MIN ... EVENT_RESULT_NERRNO_MAX: { - const char *str; - - (void) device_add_propertyf(dev, "UDEV_WORKER_ERRNO", "%i", -result); - - str = errno_to_name(result); - if (str) - (void) device_add_property(dev, "UDEV_WORKER_ERRNO_NAME", str); - break; - } - case EVENT_RESULT_EXIT_STATUS_BASE ... EVENT_RESULT_EXIT_STATUS_MAX: - (void) device_add_propertyf(dev, "UDEV_WORKER_EXIT_STATUS", "%i", result - EVENT_RESULT_EXIT_STATUS_BASE); - break; - - case EVENT_RESULT_TRY_AGAIN: - assert_not_reached(); - break; - - case EVENT_RESULT_SIGNAL_BASE ... EVENT_RESULT_SIGNAL_MAX: { - const char *str; - - (void) device_add_propertyf(dev, "UDEV_WORKER_SIGNAL", "%i", result - EVENT_RESULT_SIGNAL_BASE); - - str = signal_to_string(result - EVENT_RESULT_SIGNAL_BASE); - if (str) - (void) device_add_property(dev, "UDEV_WORKER_SIGNAL_NAME", str); - break; - } - default: - log_device_warning(dev, "Unknown event result \"%i\", ignoring.", result); - } - } - - r = device_monitor_send_device(monitor, NULL, dev); - if (r < 0) - log_device_warning_errno(dev, r, - "Failed to broadcast event to libudev listeners, ignoring: %m"); -} - -static int worker_send_result(Manager *manager, EventResult result) { - assert(manager); - assert(manager->worker_watch[WRITE_END] >= 0); - - return loop_write(manager->worker_watch[WRITE_END], &result, sizeof(result), false); -} - -static int device_get_whole_disk(sd_device *dev, sd_device **ret_device, const char **ret_devname) { - const char *val; - int r; - - assert(dev); - - if (device_for_action(dev, SD_DEVICE_REMOVE)) - goto irrelevant; - - r = sd_device_get_sysname(dev, &val); - if (r < 0) - return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); - - /* Exclude the following devices: - * For "dm-", see the comment added by e918a1b5a94f270186dca59156354acd2a596494. - * For "md", see the commit message of 2e5b17d01347d3c3118be2b8ad63d20415dbb1f0, - * but not sure the assumption is still valid even when partitions are created on the md - * devices, surprisingly which seems to be possible, see PR #22973. - * For "drbd", see the commit message of fee854ee8ccde0cd28e0f925dea18cce35f3993d. */ - if (STARTSWITH_SET(val, "dm-", "md", "drbd")) - goto irrelevant; - - r = block_device_get_whole_disk(dev, &dev); - if (IN_SET(r, - -ENOTBLK, /* The device is not a block device. */ - -ENODEV /* The whole disk device was not found, it may already be removed. */)) - goto irrelevant; - if (r < 0) - return log_device_debug_errno(dev, r, "Failed to get whole disk device: %m"); - - r = sd_device_get_devname(dev, &val); - if (r < 0) - return log_device_debug_errno(dev, r, "Failed to get devname: %m"); - - if (ret_device) - *ret_device = dev; - if (ret_devname) - *ret_devname = val; - return 1; - -irrelevant: - if (ret_device) - *ret_device = NULL; - if (ret_devname) - *ret_devname = NULL; - return 0; -} - -static int worker_lock_whole_disk(sd_device *dev, int *ret_fd) { - _cleanup_close_ int fd = -EBADF; - sd_device *dev_whole_disk; - const char *val; - int r; - - assert(dev); - assert(ret_fd); - - /* Take a shared lock on the device node; this establishes a concept of device "ownership" to - * serialize device access. External processes holding an exclusive lock will cause udev to skip the - * event handling; in the case udev acquired the lock, the external process can block until udev has - * finished its event handling. */ - - r = device_get_whole_disk(dev, &dev_whole_disk, &val); - if (r < 0) - return r; - if (r == 0) - goto nolock; - - fd = sd_device_open(dev_whole_disk, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); - if (fd < 0) { - bool ignore = ERRNO_IS_DEVICE_ABSENT(fd); - - log_device_debug_errno(dev, fd, "Failed to open '%s'%s: %m", val, ignore ? ", ignoring" : ""); - if (!ignore) - return fd; - - goto nolock; - } - - if (flock(fd, LOCK_SH|LOCK_NB) < 0) - return log_device_debug_errno(dev, errno, "Failed to flock(%s): %m", val); - - *ret_fd = TAKE_FD(fd); - return 1; - -nolock: - *ret_fd = -EBADF; - return 0; -} - -static int worker_mark_block_device_read_only(sd_device *dev) { - _cleanup_close_ int fd = -EBADF; - const char *val; - int state = 1, r; - - assert(dev); - - if (!arg_blockdev_read_only) - return 0; - - /* Do this only once, when the block device is new. If the device is later retriggered let's not - * toggle the bit again, so that people can boot up with full read-only mode and then unset the bit - * for specific devices only. */ - if (!device_for_action(dev, SD_DEVICE_ADD)) - return 0; - - r = sd_device_get_subsystem(dev, &val); - if (r < 0) - return log_device_debug_errno(dev, r, "Failed to get subsystem: %m"); - - if (!streq(val, "block")) - return 0; - - r = sd_device_get_sysname(dev, &val); - if (r < 0) - return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); - - /* Exclude synthetic devices for now, this is supposed to be a safety feature to avoid modification - * of physical devices, and what sits on top of those doesn't really matter if we don't allow the - * underlying block devices to receive changes. */ - if (STARTSWITH_SET(val, "dm-", "md", "drbd", "loop", "nbd", "zram")) - return 0; - - fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); - if (fd < 0) - return log_device_debug_errno(dev, fd, "Failed to open '%s', ignoring: %m", val); - - if (ioctl(fd, BLKROSET, &state) < 0) - return log_device_warning_errno(dev, errno, "Failed to mark block device '%s' read-only: %m", val); - - log_device_info(dev, "Successfully marked block device '%s' read-only.", val); - return 0; -} - -static int worker_process_device(Manager *manager, sd_device *dev) { - _cleanup_(udev_event_freep) UdevEvent *udev_event = NULL; - _cleanup_close_ int fd_lock = -EBADF; - int r; - - assert(manager); - assert(dev); - - log_device_uevent(dev, "Processing device"); - - udev_event = udev_event_new(dev, arg_exec_delay_usec, manager->rtnl, manager->log_level); - if (!udev_event) - return -ENOMEM; - - /* If this is a block device and the device is locked currently via the BSD advisory locks, - * someone else is using it exclusively. We don't run our udev rules now to not interfere. - * Instead of processing the event, we requeue the event and will try again after a delay. - * - * The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING */ - r = worker_lock_whole_disk(dev, &fd_lock); - if (r == -EAGAIN) - return EVENT_RESULT_TRY_AGAIN; - if (r < 0) - return r; - - (void) worker_mark_block_device_read_only(dev); - - /* apply rules, create node, symlinks */ - r = udev_event_execute_rules( - udev_event, - manager->inotify_fd, - arg_event_timeout_usec, - arg_timeout_signal, - manager->properties, - manager->rules); - if (r < 0) - return r; - - udev_event_execute_run(udev_event, arg_event_timeout_usec, arg_timeout_signal); - - if (!manager->rtnl) - /* in case rtnl was initialized */ - manager->rtnl = sd_netlink_ref(udev_event->rtnl); - - if (udev_event->inotify_watch) { - r = udev_watch_begin(manager->inotify_fd, dev); - if (r < 0 && r != -ENOENT) /* The device may be already removed, ignore -ENOENT. */ - log_device_warning_errno(dev, r, "Failed to add inotify watch, ignoring: %m"); - } - - log_device_uevent(dev, "Device processed"); - return 0; -} - -static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *dev, void *userdata) { - Manager *manager = ASSERT_PTR(userdata); - int r; - - assert(dev); - - r = worker_process_device(manager, dev); - if (r == EVENT_RESULT_TRY_AGAIN) - /* if we couldn't acquire the flock(), then requeue the event */ - log_device_debug(dev, "Block device is currently locked, requeueing the event."); - else { - if (r < 0) - log_device_warning_errno(dev, r, "Failed to process device, ignoring: %m"); - - /* send processed event back to libudev listeners */ - device_broadcast(monitor, dev, r); - } - - /* send udevd the result of the event execution */ - r = worker_send_result(manager, r); - if (r < 0) - log_device_warning_errno(dev, r, "Failed to send signal to main daemon, ignoring: %m"); - - /* Reset the log level, as it might be changed by "OPTIONS=log_level=". */ - log_set_max_level(manager->log_level); - - return 1; -} - -static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device *first_device) { - _cleanup_(sd_device_unrefp) sd_device *dev = first_device; - _cleanup_(manager_freep) Manager *manager = _manager; - int r; - - assert(manager); - assert(monitor); - assert(dev); - - assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, -1) >= 0); - - /* Reset OOM score, we only protect the main daemon. */ - r = set_oom_score_adjust(0); - if (r < 0) - log_debug_errno(r, "Failed to reset OOM score, ignoring: %m"); - - /* Clear unnecessary data in Manager object. */ - manager_clear_for_worker(manager); - - r = sd_event_new(&manager->event); - if (r < 0) - return log_error_errno(r, "Failed to allocate event loop: %m"); - - r = sd_event_add_signal(manager->event, NULL, SIGTERM, NULL, NULL); - if (r < 0) - return log_error_errno(r, "Failed to set SIGTERM event: %m"); - - r = sd_device_monitor_attach_event(monitor, manager->event); - if (r < 0) - return log_error_errno(r, "Failed to attach event loop to device monitor: %m"); - - r = sd_device_monitor_start(monitor, worker_device_monitor_handler, manager); - if (r < 0) - return log_error_errno(r, "Failed to start device monitor: %m"); - - /* Process first device */ - (void) worker_device_monitor_handler(monitor, dev, manager); - - r = sd_event_loop(manager->event); - if (r < 0) - return log_error_errno(r, "Event loop failed: %m"); - - return 0; -} - static int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) { Event *event = ASSERT_PTR(userdata); @@ -820,10 +486,21 @@ static int worker_spawn(Manager *manager, Event *event) { return log_error_errno(r, "Failed to fork() worker: %m"); } if (r == 0) { - DEVICE_TRACE_POINT(worker_spawned, event->dev, getpid_cached()); + _cleanup_(udev_worker_done) UdevWorker w = { + .monitor = TAKE_PTR(worker_monitor), + .properties = TAKE_PTR(manager->properties), + .rules = TAKE_PTR(manager->rules), + .pipe_fd = TAKE_FD(manager->worker_watch[WRITE_END]), + .inotify_fd = TAKE_FD(manager->inotify_fd), + .exec_delay_usec = arg_exec_delay_usec, + .timeout_usec = arg_event_timeout_usec, + .timeout_signal = arg_timeout_signal, + .log_level = manager->log_level, + .blockdev_read_only = arg_blockdev_read_only, + }; /* Worker process */ - r = worker_main(manager, worker_monitor, sd_device_ref(event->dev)); + r = udev_worker_main(&w, event->dev); log_close(); _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); } @@ -1083,7 +760,7 @@ static int event_queue_assume_block_device_unlocked(Manager *manager, sd_device * device is not locked anymore. The assumption may not be true, but that should not cause any * issues, as in that case events will be requeued soon. */ - r = device_get_whole_disk(dev, NULL, &devname); + r = udev_get_whole_disk(dev, NULL, &devname); if (r <= 0) return r; @@ -1096,7 +773,7 @@ static int event_queue_assume_block_device_unlocked(Manager *manager, sd_device if (event->retry_again_next_usec == 0) continue; - if (device_get_whole_disk(event->dev, NULL, &event_devname) <= 0) + if (udev_get_whole_disk(event->dev, NULL, &event_devname) <= 0) continue; if (!streq(devname, event_devname)) @@ -1249,7 +926,7 @@ static int on_worker(sd_event_source *s, int fd, uint32_t revents, void *userdat /* worker returned */ if (result == EVENT_RESULT_TRY_AGAIN && event_requeue(worker->event) < 0) - device_broadcast(manager->monitor, worker->event->dev, -ETIMEDOUT); + udev_broadcast_result(manager->monitor, worker->event->dev, -ETIMEDOUT); /* When event_requeue() succeeds, worker->event is NULL, and event_free() handles NULL gracefully. */ event_free(worker->event); @@ -1533,7 +1210,7 @@ static int on_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) { device_tag_index(dev, NULL, false); /* Forward kernel event to libudev listeners */ - device_broadcast(manager->monitor, dev, result); + udev_broadcast_result(manager->monitor, dev, result); } worker_free(worker); diff --git a/src/udev/udevd.h b/src/udev/udevd.h index 891196c474e..0af0bb8086d 100644 --- a/src/udev/udevd.h +++ b/src/udev/udevd.h @@ -1,6 +1,8 @@ /* SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once +#include + int run_udevd(int argc, char *argv[]); bool devpath_conflict(const char *a, const char *b);