Debian Codegraph @

src/udev/udev-watch.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright © 2009 Canonical Ltd.
4 * Copyright © 2009 Scott James Remnant <[email protected]>
5 */
6
7 #include <sys/signalfd.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10
11 #include "alloc-util.h"
12 #include "blockdev-util.h"
13 #include "daemon-util.h"
14 #include "device-util.h"
15 #include "dirent-util.h"
16 #include "errno-util.h"
17 #include "event-util.h"
18 #include "fd-util.h"
19 #include "format-util.h"
20 #include "fs-util.h"
21 #include "id128-util.h"
22 #include "inotify-util.h"
23 #include "parse-util.h"
24 #include "pidref.h"
25 #include "process-util.h"
26 #include "reread-partition-table.h"
27 #include "rm-rf.h"
28 #include "set.h"
29 #include "signal-util.h"
30 #include "stdio-util.h"
31 #include "string-util.h"
32 #include "time-util.h"
33 #include "udev-manager.h"
34 #include "udev-trace.h"
35 #include "udev-util.h"
36 #include "udev-watch.h"
37 #include "udev-worker.h"
38
39 static int udev_watch_clear_by_wd(sd_device *dev, int dirfd, int wd);
40
41 static int device_new_from_watch_handle_at(sd_device **ret, int dirfd, int wd) {
42 char path_wd[STRLEN("/run/udev/watch/") + DECIMAL_STR_MAX(int)];
43 _cleanup_free_ char *id = NULL;
44 int r;
45
46 assert(ret);
47
48 if (wd < 0)
49 return -EBADF;
50
51 if (dirfd >= 0) {
52 xsprintf(path_wd, "%d", wd);
53 r = readlinkat_malloc(dirfd, path_wd, &id);
54 } else {
55 xsprintf(path_wd, "/run/udev/watch/%d", wd);
56 r = readlink_malloc(path_wd, &id);
57 }
58 if (r < 0)
59 return r;
60
61 return sd_device_new_from_device_id(ret, id);
62 }
63
64 void udev_watch_dump(void) {
65 int r;
66
67 _cleanup_closedir_ DIR *dir = opendir("/run/udev/watch/");
68 if (!dir)
69 return (void) log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
70 "Failed to open old watches directory '/run/udev/watch/': %m");
71
72 _cleanup_set_free_ Set *pending_wds = NULL, *verified_wds = NULL;
73 FOREACH_DIRENT(de, dir, break) {
74 if (safe_atoi(de->d_name, NULL) >= 0) {
75 /* This should be wd -> ID symlink */
76
77 if (set_contains(verified_wds, de->d_name))
78 continue;
79
80 r = set_put_strdup(&pending_wds, de->d_name);
81 if (r < 0)
82 log_warning_errno(r, "Failed to store pending watch handle %s, ignoring: %m", de->d_name);
83 continue;
84 }
85
86 _cleanup_free_ char *wd = NULL;
87 r = readlinkat_malloc(dirfd(dir), de->d_name, &wd);
88 if (r < 0) {
89 log_warning_errno(r, "Found broken inotify watch, failed to read symlink %s, ignoring: %m", de->d_name);
90 continue;
91 }
92
93 const char *devnode = NULL;
94 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
95 if (sd_device_new_from_device_id(&dev, de->d_name) >= 0)
96 (void) sd_device_get_devname(dev, &devnode);
97
98 _cleanup_free_ char *id = NULL;
99 r = readlinkat_malloc(dirfd(dir), wd, &id);
100 if (r < 0) {
101 log_warning_errno(r, "Found broken inotify watch %s on %s (%s), failed to read symlink %s, ignoring: %m",
102 wd, strna(devnode), de->d_name, wd);
103 continue;
104 }
105
106 if (!streq(de->d_name, id)) {
107 log_warning("Found broken inotify watch %s on %s (%s), broken symlink chain: %s → %s → %s",
108 wd, strna(devnode), de->d_name, de->d_name, wd, id);
109 continue;
110 }
111
112 log_debug("Found inotify watch %s on %s (%s).", wd, strna(devnode), de->d_name);
113
114 free(set_remove(pending_wds, wd));
115
116 r = set_ensure_put(&verified_wds, &string_hash_ops_free, wd);
117 if (r < 0) {
118 log_warning_errno(r, "Failed to store verified watch handle %s, ignoring: %m", wd);
119 continue;
120 }
121 TAKE_PTR(wd);
122 }
123
124 const char *w;
125 SET_FOREACH(w, pending_wds) {
126 _cleanup_free_ char *id = NULL;
127 r = readlinkat_malloc(dirfd(dir), w, &id);
128 if (r < 0) {
129 log_warning_errno(r, "Found broken inotify watch %s, failed to read symlink %s, ignoring: %m", w, w);
130 continue;
131 }
132
133 const char *devnode = NULL;
134 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
135 if (sd_device_new_from_device_id(&dev, id) >= 0)
136 (void) sd_device_get_devname(dev, &devnode);
137
138 _cleanup_free_ char *wd = NULL;
139 (void) readlinkat_malloc(dirfd(dir), id, &wd);
140
141 log_warning("Found broken inotify watch %s on %s (%s), broken symlink chain: %s → %s → %s",
142 wd, strna(devnode), id, w, id, wd);
143 }
144 }
145
146 static int on_synthesized_events_clear(sd_event_source *s, uint64_t usec, void *userdata) {
147 Manager *manager = ASSERT_PTR(userdata);
148
149 for (;;) {
150 _cleanup_free_ sd_id128_t *uuid = set_steal_first(manager->synthesized_events);
151 if (!uuid)
152 return 0;
153
154 log_warning("Could not receive synthesized event with UUID %s, ignoring.",
155 SD_ID128_TO_STRING(*uuid));
156 }
157 }
158
159 static int synthesize_change_one(Manager *manager, sd_device *dev) {
160 int r;
161
162 assert(manager);
163 assert(dev);
164
165 if (DEBUG_LOGGING) {
166 const char *syspath = NULL;
167 (void) sd_device_get_syspath(dev, &syspath);
168 log_device_debug(dev, "device is closed, synthesising 'change' on %s", strna(syspath));
169 }
170
171 sd_id128_t uuid;
172 r = sd_device_trigger_with_uuid(dev, SD_DEVICE_CHANGE, &uuid);
173 if (r < 0)
174 return log_device_debug_errno(dev, r, "Failed to trigger 'change' uevent: %m");
175
176 DEVICE_TRACE_POINT(synthetic_change_event, dev);
177
178 /* Avoid /run/udev/queue file being removed by on_post(). */
179 sd_id128_t *copy = newdup(sd_id128_t, &uuid, 1);
180 if (!copy)
181 return log_oom_debug();
182
183 /* Let's not wait for too many events, to make not udevd consume huge amount of memory.
184 * Typically (but unfortunately, not always), the kernel provides events in the order we triggered.
185 * Hence, remembering the newest UUID should be mostly enough. */
186 while (set_size(manager->synthesized_events) >= 1024) {
187 _cleanup_free_ sd_id128_t *id = ASSERT_PTR(set_steal_first(manager->synthesized_events));
188 log_debug("Too many synthesized events are waiting, forgetting synthesized event with UUID %s.",
189 SD_ID128_TO_STRING(*id));
190 }
191
192 r = set_ensure_consume(&manager->synthesized_events, &id128_hash_ops_free, copy);
193 if (r < 0)
194 return log_oom_debug();
195
196 r = event_reset_time_relative(
197 manager->event,
198 &manager->synthesized_events_clear_event_source,
199 CLOCK_MONOTONIC,
200 1 * USEC_PER_MINUTE,
201 USEC_PER_SEC,
202 on_synthesized_events_clear,
203 manager,
204 SD_EVENT_PRIORITY_NORMAL,
205 "synthesized-events-clear",
206 /* force_reset= */ true);
207 if (r < 0)
208 log_debug_errno(r, "Failed to reset timer event source for clearing synthesized event UUIDs: %m");
209
210 return 0;
211 }
212
213 static int synthesize_change_child_handler(sd_event_source *s, const siginfo_t *si, void *userdata) {
214 Manager *manager = ASSERT_PTR(userdata);
215 assert(s);
216
217 sd_event_source_unref(set_remove(manager->synthesize_change_child_event_sources, s));
218 return 0;
219 }
220
221 static int synthesize_change(Manager *manager, sd_device *dev) {
222 int r;
223
224 assert(manager);
225 assert(dev);
226
227 r = device_sysname_startswith(dev, "dm-");
228 if (r < 0)
229 return r;
230 if (r > 0)
231 return synthesize_change_one(manager, dev);
232
233 r = block_device_is_whole_disk(dev);
234 if (r < 0)
235 return r;
236 if (r == 0)
237 return synthesize_change_one(manager, dev);
238
239 _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
240 r = pidref_safe_fork(
241 "(udev-synth)",
242 FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_REOPEN_LOG,
243 &pidref);
244 if (r < 0)
245 return r;
246 if (r == 0) {
247 /* child */
248 (void) reread_partition_table(dev, REREADPT_FORCE_UEVENT|REREADPT_BSD_LOCK);
249 _exit(EXIT_SUCCESS);
250 }
251
252 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
253 r = event_add_child_pidref(manager->event, &s, &pidref, WEXITED, synthesize_change_child_handler, manager);
254 if (r < 0) {
255 log_debug_errno(r, "Failed to add child event source for "PID_FMT", ignoring: %m", pidref.pid);
256 return 0;
257 }
258
259 r = set_ensure_put(&manager->synthesize_change_child_event_sources, &event_source_hash_ops, s);
260 if (r < 0)
261 return r;
262 TAKE_PTR(s);
263
264 return 0;
265 }
266
267 static int manager_process_inotify(Manager *manager, const struct inotify_event *e) {
268 int r;
269
270 assert(manager);
271 assert(e);
272
273 if (FLAGS_SET(e->mask, IN_IGNORED)) {
274 log_debug("Received inotify event about removal of watch handle %i.", e->wd);
275
276 r = udev_watch_clear_by_wd(/* dev= */ NULL, /* dirfd= */ -EBADF, e->wd);
277 if (r < 0)
278 log_warning_errno(r, "Failed to remove saved symlink(s) for watch handle %i, ignoring: %m", e->wd);
279
280 return 0;
281 }
282
283 if (!FLAGS_SET(e->mask, IN_CLOSE_WRITE))
284 return 0;
285
286 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
287 r = device_new_from_watch_handle_at(&dev, -EBADF, e->wd);
288 if (r < 0) /* Device may be removed just after closed. */
289 return log_debug_errno(r, "Failed to create sd_device object from watch handle, ignoring: %m");
290
291 log_device_debug(dev, "Received inotify event of watch handle %i.", e->wd);
292
293 (void) manager_create_queue_file(manager);
294 (void) manager_requeue_locked_events_by_device(manager, dev);
295 (void) synthesize_change(manager, dev);
296 return 0;
297 }
298
299 static int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
300 Manager *manager = ASSERT_PTR(userdata);
301
302 assert(fd >= 0);
303
304 union inotify_event_buffer buffer;
305 ssize_t l = read(fd, &buffer, sizeof(buffer));
306 if (l < 0) {
307 if (ERRNO_IS_TRANSIENT(errno))
308 return 0;
309
310 return log_error_errno(errno, "Failed to read inotify fd: %m");
311 }
312
313 FOREACH_INOTIFY_EVENT_WARN(e, buffer, l)
314 (void) manager_process_inotify(manager, e);
315
316 return 0;
317 }
318
319 static int udev_watch_restore(Manager *manager) {
320 _cleanup_(rm_rf_safep) const char *old = "/run/udev/watch.old/";
321 int r;
322
323 /* Move any old watches directory out of the way, and then restore the watches. */
324
325 assert(manager);
326
327 rm_rf_safe(old);
328 if (rename("/run/udev/watch/", old) < 0) {
329 if (errno == ENOENT)
330 return 0;
331
332 return log_warning_errno(errno, "Failed to move watches directory '/run/udev/watch/': %m");
333 }
334
335 _cleanup_closedir_ DIR *dir = opendir(old);
336 if (!dir)
337 return log_warning_errno(errno, "Failed to open old watches directory '%s': %m", old);
338
339 FOREACH_DIRENT(de, dir, break) {
340 if (in_charset(de->d_name, DIGITS))
341 continue; /* This should be wd -> ID symlink. Skipping. */
342
343 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
344 r = sd_device_new_from_device_id(&dev, de->d_name);
345 if (r < 0) {
346 _cleanup_free_ char *wd_str = NULL;
347
348 if (ERRNO_IS_NEG_DEVICE_ABSENT(r) || DEBUG_LOGGING)
349 (void) readlinkat_malloc(dirfd(dir), de->d_name, &wd_str);
350
351 log_full_errno(ERRNO_IS_NEG_DEVICE_ABSENT(r) ? LOG_DEBUG : LOG_WARNING, r,
352 "Failed to create sd_device object from device ID '%s' for watch handle '%s', ignoring: %m",
353 de->d_name, strna(wd_str));
354 continue;
355 }
356
357 (void) manager_add_watch(manager, dev);
358 }
359
360 return 0;
361 }
362
363 int manager_init_inotify(Manager *manager, int fd) {
364 int r;
365
366 assert(manager);
367
368 /* This takes passed file descriptor on success. */
369
370 if (fd >= 0) {
371 if (manager->inotify_fd >= 0)
372 return log_warning_errno(SYNTHETIC_ERRNO(EALREADY), "Received multiple inotify fd (%i), ignoring.", fd);
373
374 log_debug("Received inotify fd (%i) from service manager.", fd);
375 manager->inotify_fd = fd;
376 return 0;
377 }
378
379 if (manager->inotify_fd >= 0)
380 return 0;
381
382 fd = inotify_init1(IN_CLOEXEC);
383 if (fd < 0)
384 return log_error_errno(errno, "Failed to create inotify descriptor: %m");
385
386 log_debug("Initialized new inotify instance, restoring inotify watches of previous invocation.");
387 manager->inotify_fd = fd;
388 (void) udev_watch_restore(manager);
389
390 r = notify_push_fd(manager->inotify_fd, "inotify");
391 if (r < 0)
392 log_warning_errno(r, "Failed to push inotify fd to service manager, ignoring: %m");
393 else
394 log_debug("Pushed inotify fd to service manager.");
395
396 return 0;
397 }
398
399 int manager_start_inotify(Manager *manager) {
400 int r;
401
402 assert(manager);
403 assert(manager->event);
404
405 r = manager_init_inotify(manager, -EBADF);
406 if (r < 0)
407 return r;
408
409 udev_watch_dump();
410
411 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
412 r = sd_event_add_io(manager->event, &s, manager->inotify_fd, EPOLLIN, on_inotify, manager);
413 if (r < 0)
414 return log_error_errno(r, "Failed to create inotify event source: %m");
415
416 r = sd_event_source_set_priority(s, EVENT_PRIORITY_INOTIFY_WATCH);
417 if (r < 0)
418 return log_error_errno(r, "Failed to set priority to inotify event source: %m");
419
420 (void) sd_event_source_set_description(s, "manager-inotify");
421
422 manager->inotify_event = TAKE_PTR(s);
423 return 0;
424 }
425
426 static int udev_watch_clear_by_wd(sd_device *dev, int dirfd, int wd) {
427 int r;
428
429 _cleanup_close_ int dirfd_close = -EBADF;
430 if (dirfd < 0) {
431 dirfd_close = RET_NERRNO(open("/run/udev/watch/", O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW | O_RDONLY));
432 if (dirfd_close < 0)
433 return log_device_debug_errno(dev, dirfd_close, "Failed to open %s: %m", "/run/udev/watch/");
434
435 dirfd = dirfd_close;
436 }
437
438 char wd_str[DECIMAL_STR_MAX(int)];
439 xsprintf(wd_str, "%d", wd);
440
441 _cleanup_free_ char *id = NULL, *wd_alloc = NULL;
442 r = readlinkat_malloc(dirfd, wd_str, &id);
443 if (r == -ENOENT)
444 return 0;
445 if (r < 0) {
446 log_device_debug_errno(dev, r, "Failed to read '/run/udev/watch/%s': %m", wd_str);
447 goto finalize;
448 }
449
450 r = readlinkat_malloc(dirfd, id, &wd_alloc);
451 if (r < 0) {
452 log_device_debug_errno(dev, r, "Failed to read '/run/udev/watch/%s': %m", id);
453 goto finalize;
454 }
455
456 if (!streq(wd_str, wd_alloc)) {
457 r = log_device_debug_errno(dev, SYNTHETIC_ERRNO(ESTALE), "Unmatching watch handle found: %s -> %s -> %s", wd_str, id, wd_alloc);
458 goto finalize;
459 }
460
461 if (unlinkat(dirfd, id, 0) < 0 && errno != ENOENT)
462 r = log_device_debug_errno(dev, errno, "Failed to remove '/run/udev/watch/%s': %m", id);
463
464 finalize:
465 if (unlinkat(dirfd, wd_str, 0) < 0 && errno != ENOENT)
466 RET_GATHER(r, log_device_debug_errno(dev, errno, "Failed to remove '/run/udev/watch/%s': %m", wd_str));
467
468 return r;
469 }
470
471 static int udev_watch_clear(sd_device *dev, int dirfd, int *ret_wd) {
472 _cleanup_free_ char *wd_str = NULL, *buf = NULL;
473 const char *id;
474 int wd = -1, r;
475
476 assert(dev);
477 assert(dirfd >= 0);
478
479 r = sd_device_get_device_id(dev, &id);
480 if (r < 0)
481 return log_device_debug_errno(dev, r, "Failed to get device ID: %m");
482
483 /* 1. read symlink ID -> wd */
484 r = readlinkat_malloc(dirfd, id, &wd_str);
485 if (r == -ENOENT) {
486 if (ret_wd)
487 *ret_wd = -1;
488 return 0;
489 }
490 if (r < 0) {
491 log_device_debug_errno(dev, r, "Failed to read symlink '/run/udev/watch/%s': %m", id);
492 goto finalize;
493 }
494
495 r = safe_atoi(wd_str, &wd);
496 if (r < 0) {
497 log_device_debug_errno(dev, r, "Failed to parse watch handle from symlink '/run/udev/watch/%s': %m", id);
498 goto finalize;
499 }
500
501 if (wd < 0) {
502 r = log_device_debug_errno(dev, SYNTHETIC_ERRNO(EBADF), "Invalid watch handle %i.", wd);
503 goto finalize;
504 }
505
506 /* 2. read symlink wd -> ID */
507 r = readlinkat_malloc(dirfd, wd_str, &buf);
508 if (r < 0) {
509 log_device_debug_errno(dev, r, "Failed to read symlink '/run/udev/watch/%s': %m", wd_str);
510 goto finalize;
511 }
512
513 /* 3. check if the symlink wd -> ID is owned by the device. */
514 if (!streq(buf, id)) {
515 r = log_device_debug_errno(dev, SYNTHETIC_ERRNO(ENOENT),
516 "Symlink '/run/udev/watch/%s' is owned by another device '%s'.", wd_str, buf);
517 goto finalize;
518 }
519
520 /* 4. remove symlink wd -> ID.
521 * In the above, we already confirmed that the symlink is owned by us. Hence, no other workers remove
522 * the symlink and cannot create a new symlink with the same filename but to a different ID. Hence,
523 * the removal below is safe even the steps in this function are not atomic. */
524 if (unlinkat(dirfd, wd_str, 0) < 0 && errno != ENOENT)
525 log_device_debug_errno(dev, errno, "Failed to remove '/run/udev/watch/%s', ignoring: %m", wd_str);
526
527 if (ret_wd)
528 *ret_wd = wd;
529 r = 1;
530
531 finalize:
532 /* 5. remove symlink ID -> wd.
533 * The file is always owned by the device. Hence, it is safe to remove it unconditionally. */
534 if (unlinkat(dirfd, id, 0) < 0 && errno != ENOENT)
535 log_device_debug_errno(dev, errno, "Failed to remove '/run/udev/watch/%s', ignoring: %m", id);
536
537 return r;
538 }
539
540 int manager_add_watch(Manager *manager, sd_device *dev) {
541 char wd_str[DECIMAL_STR_MAX(int)];
542 _cleanup_close_ int dirfd = -EBADF;
543 const char *devnode, *id;
544 int wd, r;
545
546 assert(manager);
547 assert(dev);
548
549 /* Ignore the request of watching the device node on remove event, as the device node specified by
550 * DEVNAME= has already been removed, and may already be assigned to another device. Consider the
551 * case e.g. a USB stick memory was unplugged and then another one is plugged. */
552 if (device_for_action(dev, SD_DEVICE_REMOVE))
553 return 0;
554
555 r = sd_device_get_devname(dev, &devnode);
556 if (r < 0)
557 return log_device_debug_errno(dev, r, "Failed to get device node: %m");
558
559 r = sd_device_get_device_id(dev, &id);
560 if (r < 0)
561 return log_device_debug_errno(dev, r, "Failed to get device ID: %m");
562
563 r = dirfd = open_mkdir("/run/udev/watch", O_CLOEXEC | O_RDONLY, 0755);
564 if (r < 0)
565 return log_device_debug_errno(dev, r, "Failed to create and open '/run/udev/watch/': %m");
566
567 /* 1. Clear old symlinks */
568 (void) udev_watch_clear(dev, dirfd, NULL);
569
570 /* 2. Add inotify watch */
571 log_device_debug(dev, "Adding watch on '%s'", devnode);
572 wd = inotify_add_watch(manager->inotify_fd, devnode, IN_CLOSE_WRITE);
573 if (wd < 0)
574 return log_device_debug_errno(dev, errno, "Failed to watch device node '%s': %m", devnode);
575
576 /* 3. Clear old symlinks by the newly acquired watch handle, for the case that the watch handle is reused. */
577 (void) udev_watch_clear_by_wd(dev, dirfd, wd);
578
579 xsprintf(wd_str, "%d", wd);
580
581 /* 4. Create new symlinks */
582 if (symlinkat(wd_str, dirfd, id) < 0) {
583 r = log_device_debug_errno(dev, errno, "Failed to create symlink '/run/udev/watch/%s' to '%s': %m", id, wd_str);
584 goto on_failure;
585 }
586
587 if (symlinkat(id, dirfd, wd_str) < 0) {
588 /* Possibly, the watch handle is previously assigned to another device, and udev_watch_end()
589 * is not called for the device yet. */
590 r = log_device_debug_errno(dev, errno, "Failed to create symlink '/run/udev/watch/%s' to '%s': %m", wd_str, id);
591 goto on_failure;
592 }
593
594 return 0;
595
596 on_failure:
597 (void) unlinkat(dirfd, id, 0);
598 (void) inotify_rm_watch(manager->inotify_fd, wd);
599 return r;
600 }
601
602 int manager_remove_watch(Manager *manager, sd_device *dev) {
603 _cleanup_close_ int dirfd = -EBADF;
604 int wd, r;
605
606 assert(manager);
607 assert(dev);
608
609 dirfd = RET_NERRNO(open("/run/udev/watch", O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW | O_RDONLY));
610 if (dirfd == -ENOENT)
611 return 0;
612 if (dirfd < 0)
613 return log_device_debug_errno(dev, dirfd, "Failed to open %s: %m", "/run/udev/watch/");
614
615 /* First, clear symlinks. */
616 r = udev_watch_clear(dev, dirfd, &wd);
617 if (r <= 0)
618 return r;
619
620 /* Then, remove inotify watch. */
621 log_device_debug(dev, "Removing watch handle %i.", wd);
622 (void) inotify_rm_watch(manager->inotify_fd, wd);
623
624 return 0;
625 }
626
627 static int on_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
628 UdevWorker *worker = ASSERT_PTR(userdata);
629
630 if (!si_code_from_process(si->ssi_code)) {
631 log_debug("Received SIGUSR1 with unexpected .si_code %i, ignoring.", si->ssi_code);
632 return 0;
633 }
634
635 if ((pid_t) si->ssi_pid != worker->manager_pid) {
636 log_debug("Received SIGUSR1 from unexpected process [%"PRIu32"], ignoring.", si->ssi_pid);
637 return 0;
638 }
639
640 return sd_event_exit(sd_event_source_get_event(s), 0);
641 }
642
643 static int notify_and_wait_signal(UdevWorker *worker, sd_device *dev, const char *msg) {
644 int r;
645
646 assert(worker);
647 assert(dev);
648 assert(msg);
649
650 if (sd_device_get_devname(dev, NULL) < 0)
651 return 0;
652
653 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
654 r = sd_event_new(&e);
655 if (r < 0)
656 return r;
657
658 r = sd_event_add_signal(e, /* ret= */ NULL, SIGUSR1 | SD_EVENT_SIGNAL_PROCMASK, on_sigusr1, worker);
659 if (r < 0)
660 return r;
661
662 r = sd_notify(/* unset_environment= */ false, msg);
663 if (r <= 0)
664 return r;
665
666 return sd_event_loop(e);
667 }
668
669 int udev_watch_begin(UdevWorker *worker, sd_device *dev) {
670 assert(worker);
671 assert(dev);
672
673 if (device_for_action(dev, SD_DEVICE_REMOVE))
674 return 0;
675
676 return notify_and_wait_signal(worker, dev, "INOTIFY_WATCH_ADD=1");
677 }
678
679 int udev_watch_end(UdevWorker *worker, sd_device *dev) {
680 assert(worker);
681 assert(dev);
682
683 return notify_and_wait_signal(worker, dev, "INOTIFY_WATCH_REMOVE=1");
684 }