]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-worker.c
udev: introduce ID_PROCESSING=1 boolean property
[thirdparty/systemd.git] / src / udev / udev-worker.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <sys/file.h>
4 #include <sys/ioctl.h>
5 #include <sys/mount.h>
6
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"
14 #include "fd-util.h"
15 #include "io-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"
26
27 void udev_worker_done(UdevWorker *worker) {
28 assert(worker);
29
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);
36 }
37
38 int udev_get_whole_disk(sd_device *dev, sd_device **ret_device, const char **ret_devname) {
39 const char *val;
40 int r;
41
42 assert(dev);
43
44 if (device_for_action(dev, SD_DEVICE_REMOVE))
45 goto irrelevant;
46
47 r = sd_device_get_sysname(dev, &val);
48 if (r < 0)
49 return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
50
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"))
58 goto irrelevant;
59
60 r = block_device_get_whole_disk(dev, &dev);
61 if (IN_SET(r,
62 -ENOTBLK, /* The device is not a block device. */
63 -ENODEV /* The whole disk device was not found, it may already be removed. */))
64 goto irrelevant;
65 if (r < 0)
66 return log_device_debug_errno(dev, r, "Failed to get whole disk device: %m");
67
68 r = sd_device_get_devname(dev, &val);
69 if (r < 0)
70 return log_device_debug_errno(dev, r, "Failed to get devname: %m");
71
72 if (ret_device)
73 *ret_device = dev;
74 if (ret_devname)
75 *ret_devname = val;
76 return 1;
77
78 irrelevant:
79 if (ret_device)
80 *ret_device = NULL;
81 if (ret_devname)
82 *ret_devname = NULL;
83 return 0;
84 }
85
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;
89 const char *val;
90 int r;
91
92 assert(dev);
93 assert(ret_fd);
94
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. */
99
100 r = udev_get_whole_disk(dev, &dev_whole_disk, &val);
101 if (r < 0)
102 return r;
103 if (r == 0)
104 goto nolock;
105
106 fd = sd_device_open(dev_whole_disk, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
107 if (fd < 0) {
108 bool ignore = ERRNO_IS_DEVICE_ABSENT(fd);
109
110 log_device_debug_errno(dev, fd, "Failed to open '%s'%s: %m", val, ignore ? ", ignoring" : "");
111 if (!ignore)
112 return fd;
113
114 goto nolock;
115 }
116
117 if (flock(fd, LOCK_SH|LOCK_NB) < 0)
118 return log_device_debug_errno(dev, errno, "Failed to flock(%s): %m", val);
119
120 *ret_fd = TAKE_FD(fd);
121 return 1;
122
123 nolock:
124 *ret_fd = -EBADF;
125 return 0;
126 }
127
128 static int worker_mark_block_device_read_only(sd_device *dev) {
129 _cleanup_close_ int fd = -EBADF;
130 const char *val;
131 int state = 1, r;
132
133 assert(dev);
134
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))
139 return 0;
140
141 if (!device_in_subsystem(dev, "block"))
142 return 0;
143
144 r = sd_device_get_sysname(dev, &val);
145 if (r < 0)
146 return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
147
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"))
152 return 0;
153
154 fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
155 if (fd < 0)
156 return log_device_debug_errno(dev, fd, "Failed to open '%s', ignoring: %m", val);
157
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);
160
161 log_device_info(dev, "Successfully marked block device '%s' read-only.", val);
162 return 0;
163 }
164
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;
168 int r;
169
170 assert(worker);
171 assert(dev);
172
173 log_device_uevent(dev, "Processing device");
174
175 udev_event = udev_event_new(dev, worker);
176 if (!udev_event)
177 return -ENOMEM;
178
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.
182 *
183 * The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING */
184 r = worker_lock_whole_disk(dev, &fd_lock);
185 if (r == -EAGAIN)
186 return EVENT_RESULT_TRY_AGAIN;
187 if (r < 0)
188 return r;
189
190 if (worker->blockdev_read_only)
191 (void) worker_mark_block_device_read_only(dev);
192
193 /* Disable watch during event processing. */
194 r = udev_watch_end(worker->inotify_fd, dev);
195 if (r < 0)
196 log_device_warning_errno(dev, r, "Failed to remove inotify watch, ignoring: %m");
197
198 /* apply rules, create node, symlinks */
199 r = udev_event_execute_rules(udev_event, worker->rules);
200 if (r < 0)
201 return r;
202
203 udev_event_execute_run(udev_event);
204
205 if (!worker->rtnl)
206 /* in case rtnl was initialized */
207 worker->rtnl = sd_netlink_ref(udev_event->rtnl);
208
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");
213 }
214
215 /* Finalize database. */
216 r = device_add_property(dev, "ID_PROCESSING", NULL);
217 if (r < 0)
218 return log_device_warning_errno(dev, r, "Failed to remove 'ID_PROCESSING' property: %m");
219
220 r = device_update_db(dev);
221 if (r < 0)
222 return log_device_warning_errno(dev, r, "Failed to update database under /run/udev/data/: %m");
223
224 log_device_uevent(dev, "Device processed");
225 return 0;
226 }
227
228 void udev_broadcast_result(sd_device_monitor *monitor, sd_device *dev, EventResult result) {
229 int r;
230
231 assert(dev);
232
233 /* On exit, manager->monitor is already NULL. */
234 if (!monitor)
235 return;
236
237 if (result != EVENT_RESULT_SUCCESS) {
238 (void) device_add_property(dev, "UDEV_WORKER_FAILED", "1");
239
240 switch (result) {
241 case EVENT_RESULT_NERRNO_MIN ... EVENT_RESULT_NERRNO_MAX: {
242 const char *str;
243
244 (void) device_add_propertyf(dev, "UDEV_WORKER_ERRNO", "%i", -result);
245
246 str = errno_to_name(result);
247 if (str)
248 (void) device_add_property(dev, "UDEV_WORKER_ERRNO_NAME", str);
249 break;
250 }
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);
253 break;
254
255 case EVENT_RESULT_TRY_AGAIN:
256 assert_not_reached();
257 break;
258
259 case EVENT_RESULT_SIGNAL_BASE ... EVENT_RESULT_SIGNAL_MAX: {
260 const char *str;
261
262 (void) device_add_propertyf(dev, "UDEV_WORKER_SIGNAL", "%i", result - EVENT_RESULT_SIGNAL_BASE);
263
264 str = signal_to_string(result - EVENT_RESULT_SIGNAL_BASE);
265 if (str)
266 (void) device_add_property(dev, "UDEV_WORKER_SIGNAL_NAME", str);
267 break;
268 }
269 default:
270 log_device_warning(dev, "Unknown event result \"%i\", ignoring.", result);
271 }
272 }
273
274 r = device_monitor_send_device(monitor, NULL, dev);
275 if (r < 0)
276 log_device_warning_errno(dev, r,
277 "Failed to broadcast event to libudev listeners, ignoring: %m");
278 }
279
280 static int worker_send_result(UdevWorker *worker, EventResult result) {
281 assert(worker);
282 assert(worker->pipe_fd >= 0);
283
284 return loop_write(worker->pipe_fd, &result, sizeof(result));
285 }
286
287 static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *dev, void *userdata) {
288 UdevWorker *worker = ASSERT_PTR(userdata);
289 int r;
290
291 assert(dev);
292
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.");
297 else {
298 if (r < 0)
299 log_device_warning_errno(dev, r, "Failed to process device, ignoring: %m");
300
301 /* send processed event back to libudev listeners */
302 udev_broadcast_result(monitor, dev, r);
303 }
304
305 /* send udevd the result of the event execution */
306 r = worker_send_result(worker, r);
307 if (r < 0)
308 log_device_warning_errno(dev, r, "Failed to send signal to main daemon, ignoring: %m");
309
310 /* Reset the log level, as it might be changed by "OPTIONS=log_level=". */
311 log_set_max_level(worker->log_level);
312
313 return 1;
314 }
315
316 int udev_worker_main(UdevWorker *worker, sd_device *dev) {
317 int r;
318
319 assert(worker);
320 assert(worker->monitor);
321 assert(dev);
322
323 DEVICE_TRACE_POINT(worker_spawned, dev, getpid_cached());
324
325 /* Reset OOM score, we only protect the main daemon. */
326 r = set_oom_score_adjust(0);
327 if (r < 0)
328 log_debug_errno(r, "Failed to reset OOM score, ignoring: %m");
329
330 r = sd_event_new(&worker->event);
331 if (r < 0)
332 return log_error_errno(r, "Failed to allocate event loop: %m");
333
334 r = sd_event_add_signal(worker->event, NULL, SIGTERM | SD_EVENT_SIGNAL_PROCMASK, NULL, NULL);
335 if (r < 0)
336 return log_error_errno(r, "Failed to set SIGTERM event: %m");
337
338 r = sd_device_monitor_attach_event(worker->monitor, worker->event);
339 if (r < 0)
340 return log_error_errno(r, "Failed to attach event loop to device monitor: %m");
341
342 r = sd_device_monitor_start(worker->monitor, worker_device_monitor_handler, worker);
343 if (r < 0)
344 return log_error_errno(r, "Failed to start device monitor: %m");
345
346 /* Process first device */
347 (void) worker_device_monitor_handler(worker->monitor, dev, worker);
348
349 r = sd_event_loop(worker->event);
350 if (r < 0)
351 return log_error_errno(r, "Event loop failed: %m");
352
353 return 0;
354 }