]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udev-worker.c
resolved: add transaction result for upstream failures
[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 r = sd_device_get_subsystem(dev, &val);
142 if (r < 0)
143 return log_device_debug_errno(dev, r, "Failed to get subsystem: %m");
144
145 if (!streq(val, "block"))
146 return 0;
147
148 r = sd_device_get_sysname(dev, &val);
149 if (r < 0)
150 return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
151
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"))
156 return 0;
157
158 fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
159 if (fd < 0)
160 return log_device_debug_errno(dev, fd, "Failed to open '%s', ignoring: %m", val);
161
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);
164
165 log_device_info(dev, "Successfully marked block device '%s' read-only.", val);
166 return 0;
167 }
168
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;
172 int r;
173
174 assert(worker);
175 assert(dev);
176
177 log_device_uevent(dev, "Processing device");
178
179 udev_event = udev_event_new(dev, worker->exec_delay_usec, worker->rtnl, worker->log_level);
180 if (!udev_event)
181 return -ENOMEM;
182
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.
186 *
187 * The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING */
188 r = worker_lock_whole_disk(dev, &fd_lock);
189 if (r == -EAGAIN)
190 return EVENT_RESULT_TRY_AGAIN;
191 if (r < 0)
192 return r;
193
194 if (worker->blockdev_read_only)
195 (void) worker_mark_block_device_read_only(dev);
196
197 /* apply rules, create node, symlinks */
198 r = udev_event_execute_rules(
199 udev_event,
200 worker->inotify_fd,
201 worker->timeout_usec,
202 worker->timeout_signal,
203 worker->properties,
204 worker->rules);
205 if (r < 0)
206 return r;
207
208 udev_event_execute_run(udev_event, worker->timeout_usec, worker->timeout_signal);
209
210 if (!worker->rtnl)
211 /* in case rtnl was initialized */
212 worker->rtnl = sd_netlink_ref(udev_event->rtnl);
213
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");
218 }
219
220 log_device_uevent(dev, "Device processed");
221 return 0;
222 }
223
224 void udev_broadcast_result(sd_device_monitor *monitor, sd_device *dev, EventResult result) {
225 int r;
226
227 assert(dev);
228
229 /* On exit, manager->monitor is already NULL. */
230 if (!monitor)
231 return;
232
233 if (result != EVENT_RESULT_SUCCESS) {
234 (void) device_add_property(dev, "UDEV_WORKER_FAILED", "1");
235
236 switch (result) {
237 case EVENT_RESULT_NERRNO_MIN ... EVENT_RESULT_NERRNO_MAX: {
238 const char *str;
239
240 (void) device_add_propertyf(dev, "UDEV_WORKER_ERRNO", "%i", -result);
241
242 str = errno_to_name(result);
243 if (str)
244 (void) device_add_property(dev, "UDEV_WORKER_ERRNO_NAME", str);
245 break;
246 }
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);
249 break;
250
251 case EVENT_RESULT_TRY_AGAIN:
252 assert_not_reached();
253 break;
254
255 case EVENT_RESULT_SIGNAL_BASE ... EVENT_RESULT_SIGNAL_MAX: {
256 const char *str;
257
258 (void) device_add_propertyf(dev, "UDEV_WORKER_SIGNAL", "%i", result - EVENT_RESULT_SIGNAL_BASE);
259
260 str = signal_to_string(result - EVENT_RESULT_SIGNAL_BASE);
261 if (str)
262 (void) device_add_property(dev, "UDEV_WORKER_SIGNAL_NAME", str);
263 break;
264 }
265 default:
266 log_device_warning(dev, "Unknown event result \"%i\", ignoring.", result);
267 }
268 }
269
270 r = device_monitor_send_device(monitor, NULL, dev);
271 if (r < 0)
272 log_device_warning_errno(dev, r,
273 "Failed to broadcast event to libudev listeners, ignoring: %m");
274 }
275
276 static int worker_send_result(UdevWorker *worker, EventResult result) {
277 assert(worker);
278 assert(worker->pipe_fd >= 0);
279
280 return loop_write(worker->pipe_fd, &result, sizeof(result));
281 }
282
283 static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *dev, void *userdata) {
284 UdevWorker *worker = ASSERT_PTR(userdata);
285 int r;
286
287 assert(dev);
288
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.");
293 else {
294 if (r < 0)
295 log_device_warning_errno(dev, r, "Failed to process device, ignoring: %m");
296
297 /* send processed event back to libudev listeners */
298 udev_broadcast_result(monitor, dev, r);
299 }
300
301 /* send udevd the result of the event execution */
302 r = worker_send_result(worker, r);
303 if (r < 0)
304 log_device_warning_errno(dev, r, "Failed to send signal to main daemon, ignoring: %m");
305
306 /* Reset the log level, as it might be changed by "OPTIONS=log_level=". */
307 log_set_max_level(worker->log_level);
308
309 return 1;
310 }
311
312 int udev_worker_main(UdevWorker *worker, sd_device *dev) {
313 int r;
314
315 assert(worker);
316 assert(worker->monitor);
317 assert(dev);
318
319 DEVICE_TRACE_POINT(worker_spawned, dev, getpid_cached());
320
321 /* Reset OOM score, we only protect the main daemon. */
322 r = set_oom_score_adjust(0);
323 if (r < 0)
324 log_debug_errno(r, "Failed to reset OOM score, ignoring: %m");
325
326 r = sd_event_new(&worker->event);
327 if (r < 0)
328 return log_error_errno(r, "Failed to allocate event loop: %m");
329
330 r = sd_event_add_signal(worker->event, NULL, SIGTERM | SD_EVENT_SIGNAL_PROCMASK, NULL, NULL);
331 if (r < 0)
332 return log_error_errno(r, "Failed to set SIGTERM event: %m");
333
334 r = sd_device_monitor_attach_event(worker->monitor, worker->event);
335 if (r < 0)
336 return log_error_errno(r, "Failed to attach event loop to device monitor: %m");
337
338 r = sd_device_monitor_start(worker->monitor, worker_device_monitor_handler, worker);
339 if (r < 0)
340 return log_error_errno(r, "Failed to start device monitor: %m");
341
342 /* Process first device */
343 (void) worker_device_monitor_handler(worker->monitor, dev, worker);
344
345 r = sd_event_loop(worker->event);
346 if (r < 0)
347 return log_error_errno(r, "Event loop failed: %m");
348
349 return 0;
350 }