1 /* SPDX-License-Identifier: GPL-2.0-or-later */
7 #include "alloc-util.h"
8 #include "blockdev-util.h"
9 #include "common-signal.h"
10 #include "device-monitor-private.h"
11 #include "device-private.h"
12 #include "device-util.h"
13 #include "errno-util.h"
16 #include "path-util.h"
17 #include "process-util.h"
18 #include "signal-util.h"
19 #include "string-util.h"
20 #include "udev-event.h"
21 #include "udev-spawn.h"
22 #include "udev-trace.h"
23 #include "udev-util.h"
24 #include "udev-watch.h"
25 #include "udev-worker.h"
27 void udev_worker_done(UdevWorker
*worker
) {
30 sd_event_unref(worker
->event
);
31 sd_netlink_unref(worker
->rtnl
);
32 sd_device_monitor_unref(worker
->monitor
);
33 hashmap_free(worker
->properties
);
34 udev_rules_free(worker
->rules
);
35 safe_close(worker
->pipe_fd
);
38 int udev_get_whole_disk(sd_device
*dev
, sd_device
**ret_device
, const char **ret_devname
) {
44 if (device_for_action(dev
, SD_DEVICE_REMOVE
))
47 r
= sd_device_get_sysname(dev
, &val
);
49 return log_device_debug_errno(dev
, r
, "Failed to get sysname: %m");
51 /* Exclude the following devices:
52 * For "dm-", see the comment added by e918a1b5a94f270186dca59156354acd2a596494.
53 * For "md", see the commit message of 2e5b17d01347d3c3118be2b8ad63d20415dbb1f0,
54 * but not sure the assumption is still valid even when partitions are created on the md
55 * devices, surprisingly which seems to be possible, see PR #22973.
56 * For "drbd", see the commit message of fee854ee8ccde0cd28e0f925dea18cce35f3993d. */
57 if (STARTSWITH_SET(val
, "dm-", "md", "drbd"))
60 r
= block_device_get_whole_disk(dev
, &dev
);
62 -ENOTBLK
, /* The device is not a block device. */
63 -ENODEV
/* The whole disk device was not found, it may already be removed. */))
66 return log_device_debug_errno(dev
, r
, "Failed to get whole disk device: %m");
68 r
= sd_device_get_devname(dev
, &val
);
70 return log_device_debug_errno(dev
, r
, "Failed to get devname: %m");
86 static int worker_lock_whole_disk(sd_device
*dev
, int *ret_fd
) {
87 _cleanup_close_
int fd
= -EBADF
;
88 sd_device
*dev_whole_disk
;
95 /* Take a shared lock on the device node; this establishes a concept of device "ownership" to
96 * serialize device access. External processes holding an exclusive lock will cause udev to skip the
97 * event handling; in the case udev acquired the lock, the external process can block until udev has
98 * finished its event handling. */
100 r
= udev_get_whole_disk(dev
, &dev_whole_disk
, &val
);
106 fd
= sd_device_open(dev_whole_disk
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
108 bool ignore
= ERRNO_IS_DEVICE_ABSENT(fd
);
110 log_device_debug_errno(dev
, fd
, "Failed to open '%s'%s: %m", val
, ignore
? ", ignoring" : "");
117 if (flock(fd
, LOCK_SH
|LOCK_NB
) < 0)
118 return log_device_debug_errno(dev
, errno
, "Failed to flock(%s): %m", val
);
120 *ret_fd
= TAKE_FD(fd
);
128 static int worker_mark_block_device_read_only(sd_device
*dev
) {
129 _cleanup_close_
int fd
= -EBADF
;
135 /* Do this only once, when the block device is new. If the device is later retriggered let's not
136 * toggle the bit again, so that people can boot up with full read-only mode and then unset the bit
137 * for specific devices only. */
138 if (!device_for_action(dev
, SD_DEVICE_ADD
))
141 if (!device_in_subsystem(dev
, "block"))
144 r
= sd_device_get_sysname(dev
, &val
);
146 return log_device_debug_errno(dev
, r
, "Failed to get sysname: %m");
148 /* Exclude synthetic devices for now, this is supposed to be a safety feature to avoid modification
149 * of physical devices, and what sits on top of those doesn't really matter if we don't allow the
150 * underlying block devices to receive changes. */
151 if (STARTSWITH_SET(val
, "dm-", "md", "drbd", "loop", "nbd", "zram"))
154 fd
= sd_device_open(dev
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
156 return log_device_debug_errno(dev
, fd
, "Failed to open '%s', ignoring: %m", val
);
158 if (ioctl(fd
, BLKROSET
, &state
) < 0)
159 return log_device_warning_errno(dev
, errno
, "Failed to mark block device '%s' read-only: %m", val
);
161 log_device_info(dev
, "Successfully marked block device '%s' read-only.", val
);
165 static int worker_process_device(UdevWorker
*worker
, sd_device
*dev
) {
166 _cleanup_(udev_event_freep
) UdevEvent
*udev_event
= NULL
;
167 _cleanup_close_
int fd_lock
= -EBADF
;
173 log_device_uevent(dev
, "Processing device");
175 udev_event
= udev_event_new(dev
, worker
);
179 /* If this is a block device and the device is locked currently via the BSD advisory locks,
180 * someone else is using it exclusively. We don't run our udev rules now to not interfere.
181 * Instead of processing the event, we requeue the event and will try again after a delay.
183 * The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING */
184 r
= worker_lock_whole_disk(dev
, &fd_lock
);
186 return EVENT_RESULT_TRY_AGAIN
;
190 if (worker
->blockdev_read_only
)
191 (void) worker_mark_block_device_read_only(dev
);
193 /* Disable watch during event processing. */
194 r
= udev_watch_end(worker
->inotify_fd
, dev
);
196 log_device_warning_errno(dev
, r
, "Failed to remove inotify watch, ignoring: %m");
198 /* apply rules, create node, symlinks */
199 r
= udev_event_execute_rules(udev_event
, worker
->rules
);
203 udev_event_execute_run(udev_event
);
206 /* in case rtnl was initialized */
207 worker
->rtnl
= sd_netlink_ref(udev_event
->rtnl
);
209 if (udev_event
->inotify_watch
) {
210 r
= udev_watch_begin(worker
->inotify_fd
, dev
);
211 if (r
< 0 && r
!= -ENOENT
) /* The device may be already removed, ignore -ENOENT. */
212 log_device_warning_errno(dev
, r
, "Failed to add inotify watch, ignoring: %m");
215 /* Finalize database. */
216 r
= device_add_property(dev
, "ID_PROCESSING", NULL
);
218 return log_device_warning_errno(dev
, r
, "Failed to remove 'ID_PROCESSING' property: %m");
220 r
= device_update_db(dev
);
222 return log_device_warning_errno(dev
, r
, "Failed to update database under /run/udev/data/: %m");
224 log_device_uevent(dev
, "Device processed");
228 void udev_broadcast_result(sd_device_monitor
*monitor
, sd_device
*dev
, EventResult result
) {
233 /* On exit, manager->monitor is already NULL. */
237 if (result
!= EVENT_RESULT_SUCCESS
) {
238 (void) device_add_property(dev
, "UDEV_WORKER_FAILED", "1");
241 case EVENT_RESULT_NERRNO_MIN
... EVENT_RESULT_NERRNO_MAX
: {
244 (void) device_add_propertyf(dev
, "UDEV_WORKER_ERRNO", "%i", -result
);
246 str
= errno_to_name(result
);
248 (void) device_add_property(dev
, "UDEV_WORKER_ERRNO_NAME", str
);
251 case EVENT_RESULT_EXIT_STATUS_BASE
... EVENT_RESULT_EXIT_STATUS_MAX
:
252 (void) device_add_propertyf(dev
, "UDEV_WORKER_EXIT_STATUS", "%i", result
- EVENT_RESULT_EXIT_STATUS_BASE
);
255 case EVENT_RESULT_TRY_AGAIN
:
256 assert_not_reached();
259 case EVENT_RESULT_SIGNAL_BASE
... EVENT_RESULT_SIGNAL_MAX
: {
262 (void) device_add_propertyf(dev
, "UDEV_WORKER_SIGNAL", "%i", result
- EVENT_RESULT_SIGNAL_BASE
);
264 str
= signal_to_string(result
- EVENT_RESULT_SIGNAL_BASE
);
266 (void) device_add_property(dev
, "UDEV_WORKER_SIGNAL_NAME", str
);
270 log_device_warning(dev
, "Unknown event result \"%i\", ignoring.", result
);
274 r
= device_monitor_send_device(monitor
, NULL
, dev
);
276 log_device_warning_errno(dev
, r
,
277 "Failed to broadcast event to libudev listeners, ignoring: %m");
280 static int worker_send_result(UdevWorker
*worker
, EventResult result
) {
282 assert(worker
->pipe_fd
>= 0);
284 return loop_write(worker
->pipe_fd
, &result
, sizeof(result
));
287 static int worker_device_monitor_handler(sd_device_monitor
*monitor
, sd_device
*dev
, void *userdata
) {
288 UdevWorker
*worker
= ASSERT_PTR(userdata
);
293 r
= worker_process_device(worker
, dev
);
294 if (r
== EVENT_RESULT_TRY_AGAIN
)
295 /* if we couldn't acquire the flock(), then requeue the event */
296 log_device_debug(dev
, "Block device is currently locked, requeueing the event.");
299 log_device_warning_errno(dev
, r
, "Failed to process device, ignoring: %m");
301 /* send processed event back to libudev listeners */
302 udev_broadcast_result(monitor
, dev
, r
);
305 /* send udevd the result of the event execution */
306 r
= worker_send_result(worker
, r
);
308 log_device_warning_errno(dev
, r
, "Failed to send signal to main daemon, ignoring: %m");
310 /* Reset the log level, as it might be changed by "OPTIONS=log_level=". */
311 log_set_max_level(worker
->log_level
);
316 int udev_worker_main(UdevWorker
*worker
, sd_device
*dev
) {
320 assert(worker
->monitor
);
323 DEVICE_TRACE_POINT(worker_spawned
, dev
, getpid_cached());
325 /* Reset OOM score, we only protect the main daemon. */
326 r
= set_oom_score_adjust(0);
328 log_debug_errno(r
, "Failed to reset OOM score, ignoring: %m");
330 r
= sd_event_new(&worker
->event
);
332 return log_error_errno(r
, "Failed to allocate event loop: %m");
334 r
= sd_event_add_signal(worker
->event
, NULL
, SIGTERM
| SD_EVENT_SIGNAL_PROCMASK
, NULL
, NULL
);
336 return log_error_errno(r
, "Failed to set SIGTERM event: %m");
338 r
= sd_device_monitor_attach_event(worker
->monitor
, worker
->event
);
340 return log_error_errno(r
, "Failed to attach event loop to device monitor: %m");
342 r
= sd_device_monitor_start(worker
->monitor
, worker_device_monitor_handler
, worker
);
344 return log_error_errno(r
, "Failed to start device monitor: %m");
346 /* Process first device */
347 (void) worker_device_monitor_handler(worker
->monitor
, dev
, worker
);
349 r
= sd_event_loop(worker
->event
);
351 return log_error_errno(r
, "Event loop failed: %m");