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 r
= sd_device_get_subsystem(dev
, &val
);
143 return log_device_debug_errno(dev
, r
, "Failed to get subsystem: %m");
145 if (!streq(val
, "block"))
148 r
= sd_device_get_sysname(dev
, &val
);
150 return log_device_debug_errno(dev
, r
, "Failed to get sysname: %m");
152 /* Exclude synthetic devices for now, this is supposed to be a safety feature to avoid modification
153 * of physical devices, and what sits on top of those doesn't really matter if we don't allow the
154 * underlying block devices to receive changes. */
155 if (STARTSWITH_SET(val
, "dm-", "md", "drbd", "loop", "nbd", "zram"))
158 fd
= sd_device_open(dev
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
160 return log_device_debug_errno(dev
, fd
, "Failed to open '%s', ignoring: %m", val
);
162 if (ioctl(fd
, BLKROSET
, &state
) < 0)
163 return log_device_warning_errno(dev
, errno
, "Failed to mark block device '%s' read-only: %m", val
);
165 log_device_info(dev
, "Successfully marked block device '%s' read-only.", val
);
169 static int worker_process_device(UdevWorker
*worker
, sd_device
*dev
) {
170 _cleanup_(udev_event_freep
) UdevEvent
*udev_event
= NULL
;
171 _cleanup_close_
int fd_lock
= -EBADF
;
177 log_device_uevent(dev
, "Processing device");
179 udev_event
= udev_event_new(dev
, worker
->exec_delay_usec
, worker
->rtnl
, worker
->log_level
);
183 /* If this is a block device and the device is locked currently via the BSD advisory locks,
184 * someone else is using it exclusively. We don't run our udev rules now to not interfere.
185 * Instead of processing the event, we requeue the event and will try again after a delay.
187 * The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING */
188 r
= worker_lock_whole_disk(dev
, &fd_lock
);
190 return EVENT_RESULT_TRY_AGAIN
;
194 if (worker
->blockdev_read_only
)
195 (void) worker_mark_block_device_read_only(dev
);
197 /* apply rules, create node, symlinks */
198 r
= udev_event_execute_rules(
201 worker
->timeout_usec
,
202 worker
->timeout_signal
,
208 udev_event_execute_run(udev_event
, worker
->timeout_usec
, worker
->timeout_signal
);
211 /* in case rtnl was initialized */
212 worker
->rtnl
= sd_netlink_ref(udev_event
->rtnl
);
214 if (udev_event
->inotify_watch
) {
215 r
= udev_watch_begin(worker
->inotify_fd
, dev
);
216 if (r
< 0 && r
!= -ENOENT
) /* The device may be already removed, ignore -ENOENT. */
217 log_device_warning_errno(dev
, r
, "Failed to add inotify watch, ignoring: %m");
220 log_device_uevent(dev
, "Device processed");
224 void udev_broadcast_result(sd_device_monitor
*monitor
, sd_device
*dev
, EventResult result
) {
229 /* On exit, manager->monitor is already NULL. */
233 if (result
!= EVENT_RESULT_SUCCESS
) {
234 (void) device_add_property(dev
, "UDEV_WORKER_FAILED", "1");
237 case EVENT_RESULT_NERRNO_MIN
... EVENT_RESULT_NERRNO_MAX
: {
240 (void) device_add_propertyf(dev
, "UDEV_WORKER_ERRNO", "%i", -result
);
242 str
= errno_to_name(result
);
244 (void) device_add_property(dev
, "UDEV_WORKER_ERRNO_NAME", str
);
247 case EVENT_RESULT_EXIT_STATUS_BASE
... EVENT_RESULT_EXIT_STATUS_MAX
:
248 (void) device_add_propertyf(dev
, "UDEV_WORKER_EXIT_STATUS", "%i", result
- EVENT_RESULT_EXIT_STATUS_BASE
);
251 case EVENT_RESULT_TRY_AGAIN
:
252 assert_not_reached();
255 case EVENT_RESULT_SIGNAL_BASE
... EVENT_RESULT_SIGNAL_MAX
: {
258 (void) device_add_propertyf(dev
, "UDEV_WORKER_SIGNAL", "%i", result
- EVENT_RESULT_SIGNAL_BASE
);
260 str
= signal_to_string(result
- EVENT_RESULT_SIGNAL_BASE
);
262 (void) device_add_property(dev
, "UDEV_WORKER_SIGNAL_NAME", str
);
266 log_device_warning(dev
, "Unknown event result \"%i\", ignoring.", result
);
270 r
= device_monitor_send_device(monitor
, NULL
, dev
);
272 log_device_warning_errno(dev
, r
,
273 "Failed to broadcast event to libudev listeners, ignoring: %m");
276 static int worker_send_result(UdevWorker
*worker
, EventResult result
) {
278 assert(worker
->pipe_fd
>= 0);
280 return loop_write(worker
->pipe_fd
, &result
, sizeof(result
));
283 static int worker_device_monitor_handler(sd_device_monitor
*monitor
, sd_device
*dev
, void *userdata
) {
284 UdevWorker
*worker
= ASSERT_PTR(userdata
);
289 r
= worker_process_device(worker
, dev
);
290 if (r
== EVENT_RESULT_TRY_AGAIN
)
291 /* if we couldn't acquire the flock(), then requeue the event */
292 log_device_debug(dev
, "Block device is currently locked, requeueing the event.");
295 log_device_warning_errno(dev
, r
, "Failed to process device, ignoring: %m");
297 /* send processed event back to libudev listeners */
298 udev_broadcast_result(monitor
, dev
, r
);
301 /* send udevd the result of the event execution */
302 r
= worker_send_result(worker
, r
);
304 log_device_warning_errno(dev
, r
, "Failed to send signal to main daemon, ignoring: %m");
306 /* Reset the log level, as it might be changed by "OPTIONS=log_level=". */
307 log_set_max_level(worker
->log_level
);
312 int udev_worker_main(UdevWorker
*worker
, sd_device
*dev
) {
316 assert(worker
->monitor
);
319 DEVICE_TRACE_POINT(worker_spawned
, dev
, getpid_cached());
321 /* Reset OOM score, we only protect the main daemon. */
322 r
= set_oom_score_adjust(0);
324 log_debug_errno(r
, "Failed to reset OOM score, ignoring: %m");
326 r
= sd_event_new(&worker
->event
);
328 return log_error_errno(r
, "Failed to allocate event loop: %m");
330 r
= sd_event_add_signal(worker
->event
, NULL
, SIGTERM
| SD_EVENT_SIGNAL_PROCMASK
, NULL
, NULL
);
332 return log_error_errno(r
, "Failed to set SIGTERM event: %m");
334 r
= sd_device_monitor_attach_event(worker
->monitor
, worker
->event
);
336 return log_error_errno(r
, "Failed to attach event loop to device monitor: %m");
338 r
= sd_device_monitor_start(worker
->monitor
, worker_device_monitor_handler
, worker
);
340 return log_error_errno(r
, "Failed to start device monitor: %m");
342 /* Process first device */
343 (void) worker_device_monitor_handler(worker
->monitor
, dev
, worker
);
345 r
= sd_event_loop(worker
->event
);
347 return log_error_errno(r
, "Event loop failed: %m");