]>
Commit | Line | Data |
---|---|---|
a032b68d | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
663996b3 | 2 | /* |
b012e921 MB |
3 | * Copyright © 2004 Chris Friesen <chris_friesen@sympatico.ca> |
4 | * Copyright © 2009 Canonical Ltd. | |
5 | * Copyright © 2009 Scott James Remnant <scott@netsplit.com> | |
663996b3 MS |
6 | */ |
7 | ||
663996b3 | 8 | #include <errno.h> |
6300502b MP |
9 | #include <fcntl.h> |
10 | #include <getopt.h> | |
6300502b MP |
11 | #include <stdbool.h> |
12 | #include <stddef.h> | |
663996b3 MS |
13 | #include <stdio.h> |
14 | #include <stdlib.h> | |
6300502b | 15 | #include <sys/epoll.h> |
60f067b4 | 16 | #include <sys/file.h> |
6300502b MP |
17 | #include <sys/inotify.h> |
18 | #include <sys/ioctl.h> | |
19 | #include <sys/mount.h> | |
663996b3 | 20 | #include <sys/prctl.h> |
663996b3 | 21 | #include <sys/signalfd.h> |
663996b3 | 22 | #include <sys/stat.h> |
6300502b MP |
23 | #include <sys/time.h> |
24 | #include <sys/wait.h> | |
25 | #include <unistd.h> | |
663996b3 | 26 | |
663996b3 | 27 | #include "sd-daemon.h" |
86f210e9 MP |
28 | #include "sd-event.h" |
29 | ||
db2df898 | 30 | #include "alloc-util.h" |
086111aa | 31 | #include "blockdev-util.h" |
f5caa8fa | 32 | #include "cgroup-setup.h" |
663996b3 | 33 | #include "cgroup-util.h" |
6300502b | 34 | #include "cpu-set-util.h" |
663996b3 | 35 | #include "dev-setup.h" |
6e866b33 MB |
36 | #include "device-monitor-private.h" |
37 | #include "device-private.h" | |
38 | #include "device-util.h" | |
f5caa8fa | 39 | #include "errno-list.h" |
6e866b33 | 40 | #include "event-util.h" |
db2df898 | 41 | #include "fd-util.h" |
663996b3 | 42 | #include "fileio.h" |
2897b343 | 43 | #include "format-util.h" |
db2df898 | 44 | #include "fs-util.h" |
e3bff60a | 45 | #include "hashmap.h" |
ea0999c9 | 46 | #include "inotify-util.h" |
db2df898 | 47 | #include "io-util.h" |
bb4f798a | 48 | #include "limits-util.h" |
52ad194e | 49 | #include "list.h" |
6e866b33 MB |
50 | #include "main-func.h" |
51 | #include "mkdir.h" | |
6300502b | 52 | #include "netlink-util.h" |
db2df898 | 53 | #include "parse-util.h" |
f5caa8fa | 54 | #include "path-util.h" |
6e866b33 | 55 | #include "pretty-print.h" |
db2df898 | 56 | #include "proc-cmdline.h" |
6300502b MP |
57 | #include "process-util.h" |
58 | #include "selinux-util.h" | |
59 | #include "signal-util.h" | |
db2df898 MP |
60 | #include "socket-util.h" |
61 | #include "string-util.h" | |
6e866b33 MB |
62 | #include "strv.h" |
63 | #include "strxcpyx.h" | |
64 | #include "syslog-util.h" | |
a10f5d05 | 65 | #include "udevd.h" |
6e866b33 MB |
66 | #include "udev-builtin.h" |
67 | #include "udev-ctrl.h" | |
f2dec872 | 68 | #include "udev-event.h" |
086111aa | 69 | #include "udev-node.h" |
6300502b | 70 | #include "udev-util.h" |
6e866b33 | 71 | #include "udev-watch.h" |
db2df898 | 72 | #include "user-util.h" |
8b3d4ff0 | 73 | #include "version.h" |
663996b3 | 74 | |
f2dec872 | 75 | #define WORKER_NUM_MAX 2048U |
f5caa8fa MB |
76 | #define EVENT_RETRY_INTERVAL_USEC (200 * USEC_PER_MSEC) |
77 | #define EVENT_RETRY_TIMEOUT_USEC (3 * USEC_PER_MINUTE) | |
f2dec872 | 78 | |
5eef597e MP |
79 | static bool arg_debug = false; |
80 | static int arg_daemonize = false; | |
6e866b33 MB |
81 | static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY; |
82 | static unsigned arg_children_max = 0; | |
83 | static usec_t arg_exec_delay_usec = 0; | |
5eef597e | 84 | static usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC; |
a10f5d05 MB |
85 | static int arg_timeout_signal = SIGKILL; |
86 | static bool arg_blockdev_read_only = false; | |
e3bff60a | 87 | |
ea0999c9 MB |
88 | typedef struct Event Event; |
89 | typedef struct Worker Worker; | |
90 | ||
e3bff60a | 91 | typedef struct Manager { |
86f210e9 | 92 | sd_event *event; |
e3bff60a | 93 | Hashmap *workers; |
ea0999c9 | 94 | LIST_HEAD(Event, events); |
f5caa8fa | 95 | char *cgroup; |
e3bff60a | 96 | pid_t pid; /* the process that originally allocated the manager object */ |
3a6ce677 | 97 | int log_level; |
e3bff60a | 98 | |
6e866b33 MB |
99 | UdevRules *rules; |
100 | Hashmap *properties; | |
e3bff60a | 101 | |
6e866b33 MB |
102 | sd_netlink *rtnl; |
103 | ||
104 | sd_device_monitor *monitor; | |
ea0999c9 | 105 | UdevCtrl *ctrl; |
e3bff60a MP |
106 | int worker_watch[2]; |
107 | ||
8b3d4ff0 MB |
108 | /* used by udev-watch */ |
109 | int inotify_fd; | |
86f210e9 | 110 | sd_event_source *inotify_event; |
8b3d4ff0 | 111 | |
6e866b33 | 112 | sd_event_source *kill_workers_event; |
86f210e9 MP |
113 | |
114 | usec_t last_usec; | |
115 | ||
086111aa | 116 | bool udev_node_needs_cleanup; |
3a6ce677 BR |
117 | bool stop_exec_queue; |
118 | bool exit; | |
e3bff60a | 119 | } Manager; |
663996b3 | 120 | |
ea0999c9 | 121 | typedef enum EventState { |
663996b3 MS |
122 | EVENT_UNDEF, |
123 | EVENT_QUEUED, | |
124 | EVENT_RUNNING, | |
ea0999c9 | 125 | } EventState; |
663996b3 | 126 | |
ea0999c9 | 127 | typedef struct Event { |
e3bff60a | 128 | Manager *manager; |
ea0999c9 MB |
129 | Worker *worker; |
130 | EventState state; | |
6e866b33 MB |
131 | |
132 | sd_device *dev; | |
6e866b33 | 133 | |
f5caa8fa | 134 | sd_device_action_t action; |
6e866b33 | 135 | uint64_t seqnum; |
ea0999c9 | 136 | uint64_t blocker_seqnum; |
086111aa LB |
137 | const char *id; |
138 | const char *devpath; | |
139 | const char *devpath_old; | |
140 | const char *devnode; | |
d9f5095a MB |
141 | |
142 | /* Used when the device is locked by another program. */ | |
f5caa8fa MB |
143 | usec_t retry_again_next_usec; |
144 | usec_t retry_again_timeout_usec; | |
d9f5095a | 145 | sd_event_source *retry_event_source; |
6e866b33 MB |
146 | |
147 | sd_event_source *timeout_warning_event; | |
148 | sd_event_source *timeout_event; | |
149 | ||
ea0999c9 MB |
150 | LIST_FIELDS(Event, event); |
151 | } Event; | |
663996b3 | 152 | |
ea0999c9 | 153 | typedef enum WorkerState { |
663996b3 MS |
154 | WORKER_UNDEF, |
155 | WORKER_RUNNING, | |
156 | WORKER_IDLE, | |
157 | WORKER_KILLED, | |
8b3d4ff0 | 158 | WORKER_KILLING, |
ea0999c9 | 159 | } WorkerState; |
663996b3 | 160 | |
ea0999c9 | 161 | typedef struct Worker { |
e3bff60a | 162 | Manager *manager; |
663996b3 | 163 | pid_t pid; |
8f232108 | 164 | sd_event_source *child_event_source; |
6e866b33 | 165 | sd_device_monitor *monitor; |
ea0999c9 MB |
166 | WorkerState state; |
167 | Event *event; | |
168 | } Worker; | |
663996b3 MS |
169 | |
170 | /* passed from worker to main process */ | |
f5caa8fa MB |
171 | typedef enum EventResult { |
172 | EVENT_RESULT_NERRNO_MIN = -ERRNO_MAX, | |
173 | EVENT_RESULT_NERRNO_MAX = -1, | |
8f232108 | 174 | EVENT_RESULT_SUCCESS = 0, |
f5caa8fa MB |
175 | EVENT_RESULT_EXIT_STATUS_BASE = 0, |
176 | EVENT_RESULT_EXIT_STATUS_MAX = 255, | |
177 | EVENT_RESULT_TRY_AGAIN = 256, /* when the block device is locked by another process. */ | |
178 | EVENT_RESULT_SIGNAL_BASE = 257, | |
179 | EVENT_RESULT_SIGNAL_MAX = EVENT_RESULT_SIGNAL_BASE + _NSIG, | |
180 | _EVENT_RESULT_MAX, | |
181 | _EVENT_RESULT_INVALID = -EINVAL, | |
182 | } EventResult; | |
663996b3 | 183 | |
ea0999c9 | 184 | static Event *event_free(Event *event) { |
e3bff60a | 185 | if (!event) |
ea0999c9 | 186 | return NULL; |
6e866b33 | 187 | |
52ad194e | 188 | assert(event->manager); |
663996b3 | 189 | |
52ad194e | 190 | LIST_REMOVE(event, event->manager->events, event); |
6e866b33 | 191 | sd_device_unref(event->dev); |
e3bff60a | 192 | |
8f232108 MB |
193 | /* Do not use sd_event_source_disable_unref() here, as this is called by both workers and the |
194 | * main process. */ | |
d9f5095a | 195 | sd_event_source_unref(event->retry_event_source); |
8f232108 MB |
196 | sd_event_source_unref(event->timeout_warning_event); |
197 | sd_event_source_unref(event->timeout_event); | |
86f210e9 | 198 | |
e3bff60a MP |
199 | if (event->worker) |
200 | event->worker->event = NULL; | |
201 | ||
ea0999c9 MB |
202 | return mfree(event); |
203 | } | |
204 | ||
205 | static void event_queue_cleanup(Manager *manager, EventState match_state) { | |
f5caa8fa | 206 | LIST_FOREACH(event, event, manager->events) { |
ea0999c9 MB |
207 | if (match_state != EVENT_UNDEF && match_state != event->state) |
208 | continue; | |
e3bff60a | 209 | |
ea0999c9 MB |
210 | event_free(event); |
211 | } | |
663996b3 MS |
212 | } |
213 | ||
ea0999c9 | 214 | static Worker *worker_free(Worker *worker) { |
e3bff60a | 215 | if (!worker) |
3a6ce677 | 216 | return NULL; |
663996b3 | 217 | |
8f232108 MB |
218 | if (worker->manager) |
219 | hashmap_remove(worker->manager->workers, PID_TO_PTR(worker->pid)); | |
e3bff60a | 220 | |
8f232108 | 221 | sd_event_source_unref(worker->child_event_source); |
6e866b33 | 222 | sd_device_monitor_unref(worker->monitor); |
e3bff60a MP |
223 | event_free(worker->event); |
224 | ||
3a6ce677 | 225 | return mfree(worker); |
663996b3 MS |
226 | } |
227 | ||
ea0999c9 MB |
228 | DEFINE_TRIVIAL_CLEANUP_FUNC(Worker*, worker_free); |
229 | DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(worker_hash_op, void, trivial_hash_func, trivial_compare_func, Worker, worker_free); | |
230 | ||
231 | static void manager_clear_for_worker(Manager *manager) { | |
232 | assert(manager); | |
233 | ||
f5caa8fa MB |
234 | /* Do not use sd_event_source_disable_unref() here, as this is called by both workers and the |
235 | * main process. */ | |
ea0999c9 MB |
236 | manager->inotify_event = sd_event_source_unref(manager->inotify_event); |
237 | manager->kill_workers_event = sd_event_source_unref(manager->kill_workers_event); | |
238 | ||
239 | manager->event = sd_event_unref(manager->event); | |
240 | ||
241 | manager->workers = hashmap_free(manager->workers); | |
242 | event_queue_cleanup(manager, EVENT_UNDEF); | |
243 | ||
244 | manager->monitor = sd_device_monitor_unref(manager->monitor); | |
245 | manager->ctrl = udev_ctrl_unref(manager->ctrl); | |
246 | ||
247 | manager->worker_watch[READ_END] = safe_close(manager->worker_watch[READ_END]); | |
248 | } | |
e3bff60a | 249 | |
ea0999c9 MB |
250 | static Manager* manager_free(Manager *manager) { |
251 | if (!manager) | |
252 | return NULL; | |
253 | ||
254 | udev_builtin_exit(); | |
255 | ||
256 | manager_clear_for_worker(manager); | |
257 | ||
258 | sd_netlink_unref(manager->rtnl); | |
259 | ||
260 | hashmap_free_free_free(manager->properties); | |
261 | udev_rules_free(manager->rules); | |
262 | ||
263 | safe_close(manager->inotify_fd); | |
264 | safe_close_pair(manager->worker_watch); | |
265 | ||
f5caa8fa | 266 | free(manager->cgroup); |
ea0999c9 MB |
267 | return mfree(manager); |
268 | } | |
269 | ||
270 | DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free); | |
271 | ||
8f232108 MB |
272 | static int on_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata); |
273 | ||
ea0999c9 MB |
274 | static int worker_new(Worker **ret, Manager *manager, sd_device_monitor *worker_monitor, pid_t pid) { |
275 | _cleanup_(worker_freep) Worker *worker = NULL; | |
e3bff60a MP |
276 | int r; |
277 | ||
278 | assert(ret); | |
279 | assert(manager); | |
280 | assert(worker_monitor); | |
281 | assert(pid > 1); | |
282 | ||
7c20daf6 FS |
283 | /* close monitor, but keep address around */ |
284 | device_monitor_disconnect(worker_monitor); | |
285 | ||
ea0999c9 | 286 | worker = new(Worker, 1); |
e3bff60a MP |
287 | if (!worker) |
288 | return -ENOMEM; | |
289 | ||
ea0999c9 | 290 | *worker = (Worker) { |
7c20daf6 FS |
291 | .monitor = sd_device_monitor_ref(worker_monitor), |
292 | .pid = pid, | |
293 | }; | |
e3bff60a | 294 | |
8f232108 MB |
295 | r = sd_event_add_child(manager->event, &worker->child_event_source, pid, WEXITED, on_sigchld, worker); |
296 | if (r < 0) | |
297 | return r; | |
298 | ||
3a6ce677 | 299 | r = hashmap_ensure_put(&manager->workers, &worker_hash_op, PID_TO_PTR(pid), worker); |
e3bff60a MP |
300 | if (r < 0) |
301 | return r; | |
302 | ||
8f232108 | 303 | worker->manager = manager; |
e3bff60a | 304 | |
8f232108 | 305 | *ret = TAKE_PTR(worker); |
e3bff60a MP |
306 | return 0; |
307 | } | |
308 | ||
ea0999c9 MB |
309 | static void manager_kill_workers(Manager *manager, bool force) { |
310 | Worker *worker; | |
e3bff60a | 311 | |
ea0999c9 | 312 | assert(manager); |
e3bff60a | 313 | |
ea0999c9 MB |
314 | HASHMAP_FOREACH(worker, manager->workers) { |
315 | if (worker->state == WORKER_KILLED) | |
316 | continue; | |
e3bff60a | 317 | |
ea0999c9 MB |
318 | if (worker->state == WORKER_RUNNING && !force) { |
319 | worker->state = WORKER_KILLING; | |
320 | continue; | |
321 | } | |
e3bff60a | 322 | |
ea0999c9 MB |
323 | worker->state = WORKER_KILLED; |
324 | (void) kill(worker->pid, SIGTERM); | |
325 | } | |
e3bff60a MP |
326 | } |
327 | ||
ea0999c9 MB |
328 | static void manager_exit(Manager *manager) { |
329 | assert(manager); | |
e3bff60a | 330 | |
ea0999c9 | 331 | manager->exit = true; |
e3bff60a | 332 | |
ea0999c9 MB |
333 | sd_notify(false, |
334 | "STOPPING=1\n" | |
335 | "STATUS=Starting shutdown..."); | |
e3bff60a | 336 | |
ea0999c9 MB |
337 | /* close sources of new events and discard buffered events */ |
338 | manager->ctrl = udev_ctrl_unref(manager->ctrl); | |
86f210e9 | 339 | |
f5caa8fa | 340 | manager->inotify_event = sd_event_source_disable_unref(manager->inotify_event); |
ea0999c9 | 341 | manager->inotify_fd = safe_close(manager->inotify_fd); |
e3bff60a | 342 | |
ea0999c9 | 343 | manager->monitor = sd_device_monitor_unref(manager->monitor); |
86f210e9 | 344 | |
ea0999c9 MB |
345 | /* discard queued events and kill workers */ |
346 | event_queue_cleanup(manager, EVENT_QUEUED); | |
347 | manager_kill_workers(manager, true); | |
348 | } | |
86f210e9 | 349 | |
ea0999c9 MB |
350 | static void notify_ready(void) { |
351 | int r; | |
86f210e9 | 352 | |
ea0999c9 MB |
353 | r = sd_notifyf(false, |
354 | "READY=1\n" | |
355 | "STATUS=Processing with %u children at max", arg_children_max); | |
356 | if (r < 0) | |
357 | log_warning_errno(r, "Failed to send readiness notification, ignoring: %m"); | |
e3bff60a MP |
358 | } |
359 | ||
ea0999c9 | 360 | /* reload requested, HUP signal received, rules changed, builtin changed */ |
086111aa LB |
361 | static void manager_reload(Manager *manager, bool force) { |
362 | _cleanup_(udev_rules_freep) UdevRules *rules = NULL; | |
363 | usec_t now_usec; | |
364 | int r; | |
365 | ||
6e866b33 | 366 | assert(manager); |
e3bff60a | 367 | |
086111aa LB |
368 | assert_se(sd_event_now(manager->event, CLOCK_MONOTONIC, &now_usec) >= 0); |
369 | if (!force && now_usec < usec_add(manager->last_usec, 3 * USEC_PER_SEC)) | |
370 | /* check for changed config, every 3 seconds at most */ | |
371 | return; | |
372 | manager->last_usec = now_usec; | |
373 | ||
374 | /* Reload SELinux label database, to make the child inherit the up-to-date database. */ | |
375 | mac_selinux_maybe_reload(); | |
376 | ||
377 | /* Nothing changed. It is not necessary to reload. */ | |
378 | if (!udev_rules_should_reload(manager->rules) && !udev_builtin_should_reload()) | |
379 | return; | |
380 | ||
ea0999c9 MB |
381 | sd_notify(false, |
382 | "RELOADING=1\n" | |
383 | "STATUS=Flushing configuration..."); | |
6e866b33 | 384 | |
ea0999c9 | 385 | manager_kill_workers(manager, false); |
086111aa | 386 | |
6e866b33 | 387 | udev_builtin_exit(); |
086111aa LB |
388 | udev_builtin_init(); |
389 | ||
390 | r = udev_rules_load(&rules, arg_resolve_name_timing); | |
391 | if (r < 0) | |
392 | log_warning_errno(r, "Failed to read udev rules, using the previously loaded rules, ignoring: %m"); | |
393 | else | |
394 | udev_rules_free_and_replace(manager->rules, rules); | |
6e866b33 | 395 | |
ea0999c9 MB |
396 | notify_ready(); |
397 | } | |
6e866b33 | 398 | |
ea0999c9 | 399 | static int on_kill_workers_event(sd_event_source *s, uint64_t usec, void *userdata) { |
086111aa | 400 | Manager *manager = ASSERT_PTR(userdata); |
e3bff60a | 401 | |
ea0999c9 MB |
402 | log_debug("Cleanup idle workers"); |
403 | manager_kill_workers(manager, false); | |
e3bff60a | 404 | |
ea0999c9 | 405 | return 1; |
663996b3 MS |
406 | } |
407 | ||
8f232108 | 408 | static void device_broadcast(sd_device_monitor *monitor, sd_device *dev, EventResult result) { |
f5caa8fa MB |
409 | int r; |
410 | ||
411 | assert(dev); | |
412 | ||
413 | /* On exit, manager->monitor is already NULL. */ | |
414 | if (!monitor) | |
415 | return; | |
416 | ||
8f232108 | 417 | if (result != EVENT_RESULT_SUCCESS) { |
f5caa8fa MB |
418 | (void) device_add_property(dev, "UDEV_WORKER_FAILED", "1"); |
419 | ||
420 | switch (result) { | |
8f232108 MB |
421 | case EVENT_RESULT_NERRNO_MIN ... EVENT_RESULT_NERRNO_MAX: { |
422 | const char *str; | |
423 | ||
f5caa8fa | 424 | (void) device_add_propertyf(dev, "UDEV_WORKER_ERRNO", "%i", -result); |
f5caa8fa | 425 | |
8f232108 MB |
426 | str = errno_to_name(result); |
427 | if (str) | |
428 | (void) device_add_property(dev, "UDEV_WORKER_ERRNO_NAME", str); | |
429 | break; | |
430 | } | |
f5caa8fa MB |
431 | case EVENT_RESULT_EXIT_STATUS_BASE ... EVENT_RESULT_EXIT_STATUS_MAX: |
432 | (void) device_add_propertyf(dev, "UDEV_WORKER_EXIT_STATUS", "%i", result - EVENT_RESULT_EXIT_STATUS_BASE); | |
433 | break; | |
434 | ||
435 | case EVENT_RESULT_TRY_AGAIN: | |
436 | assert_not_reached(); | |
437 | break; | |
438 | ||
8f232108 MB |
439 | case EVENT_RESULT_SIGNAL_BASE ... EVENT_RESULT_SIGNAL_MAX: { |
440 | const char *str; | |
441 | ||
f5caa8fa | 442 | (void) device_add_propertyf(dev, "UDEV_WORKER_SIGNAL", "%i", result - EVENT_RESULT_SIGNAL_BASE); |
f5caa8fa | 443 | |
8f232108 MB |
444 | str = signal_to_string(result - EVENT_RESULT_SIGNAL_BASE); |
445 | if (str) | |
446 | (void) device_add_property(dev, "UDEV_WORKER_SIGNAL_NAME", str); | |
447 | break; | |
448 | } | |
f5caa8fa MB |
449 | default: |
450 | log_device_warning(dev, "Unknown event result \"%i\", ignoring.", result); | |
451 | } | |
452 | } | |
663996b3 | 453 | |
f5caa8fa MB |
454 | r = device_monitor_send_device(monitor, NULL, dev); |
455 | if (r < 0) | |
456 | log_device_warning_errno(dev, r, | |
457 | "Failed to broadcast event to libudev listeners, ignoring: %m"); | |
663996b3 MS |
458 | } |
459 | ||
8f232108 | 460 | static int worker_send_result(Manager *manager, EventResult result) { |
f5caa8fa MB |
461 | assert(manager); |
462 | assert(manager->worker_watch[WRITE_END] >= 0); | |
463 | ||
464 | return loop_write(manager->worker_watch[WRITE_END], &result, sizeof(result), false); | |
465 | } | |
466 | ||
8f232108 | 467 | static int device_get_whole_disk(sd_device *dev, sd_device **ret_device, const char **ret_devname) { |
6e866b33 MB |
468 | const char *val; |
469 | int r; | |
470 | ||
471 | assert(dev); | |
6e866b33 | 472 | |
3a6ce677 | 473 | if (device_for_action(dev, SD_DEVICE_REMOVE)) |
f5caa8fa | 474 | goto irrelevant; |
6e866b33 | 475 | |
6e866b33 MB |
476 | r = sd_device_get_sysname(dev, &val); |
477 | if (r < 0) | |
478 | return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); | |
479 | ||
8f232108 MB |
480 | /* Exclude the following devices: |
481 | * For "dm-", see the comment added by e918a1b5a94f270186dca59156354acd2a596494. | |
482 | * For "md", see the commit message of 2e5b17d01347d3c3118be2b8ad63d20415dbb1f0, | |
483 | * but not sure the assumption is still valid even when partitions are created on the md | |
484 | * devices, surprisingly which seems to be possible, see PR #22973. | |
485 | * For "drbd", see the commit message of fee854ee8ccde0cd28e0f925dea18cce35f3993d. */ | |
6e866b33 | 486 | if (STARTSWITH_SET(val, "dm-", "md", "drbd")) |
f5caa8fa | 487 | goto irrelevant; |
6e866b33 | 488 | |
086111aa LB |
489 | r = block_device_get_whole_disk(dev, &dev); |
490 | if (IN_SET(r, | |
491 | -ENOTBLK, /* The device is not a block device. */ | |
492 | -ENODEV /* The whole disk device was not found, it may already be removed. */)) | |
493 | goto irrelevant; | |
494 | if (r < 0) | |
495 | return log_device_debug_errno(dev, r, "Failed to get whole disk device: %m"); | |
6e866b33 MB |
496 | |
497 | r = sd_device_get_devname(dev, &val); | |
6e866b33 MB |
498 | if (r < 0) |
499 | return log_device_debug_errno(dev, r, "Failed to get devname: %m"); | |
b012e921 | 500 | |
8f232108 MB |
501 | if (ret_device) |
502 | *ret_device = dev; | |
503 | if (ret_devname) | |
504 | *ret_devname = val; | |
f5caa8fa MB |
505 | return 1; |
506 | ||
507 | irrelevant: | |
8f232108 MB |
508 | if (ret_device) |
509 | *ret_device = NULL; | |
510 | if (ret_devname) | |
511 | *ret_devname = NULL; | |
f5caa8fa MB |
512 | return 0; |
513 | } | |
514 | ||
8f232108 | 515 | static int worker_lock_whole_disk(sd_device *dev, int *ret_fd) { |
f5caa8fa | 516 | _cleanup_close_ int fd = -1; |
8f232108 | 517 | sd_device *dev_whole_disk; |
f5caa8fa MB |
518 | const char *val; |
519 | int r; | |
520 | ||
521 | assert(dev); | |
522 | assert(ret_fd); | |
523 | ||
524 | /* Take a shared lock on the device node; this establishes a concept of device "ownership" to | |
525 | * serialize device access. External processes holding an exclusive lock will cause udev to skip the | |
526 | * event handling; in the case udev acquired the lock, the external process can block until udev has | |
527 | * finished its event handling. */ | |
528 | ||
8f232108 | 529 | r = device_get_whole_disk(dev, &dev_whole_disk, &val); |
f5caa8fa MB |
530 | if (r < 0) |
531 | return r; | |
532 | if (r == 0) | |
533 | goto nolock; | |
534 | ||
086111aa | 535 | fd = sd_device_open(dev_whole_disk, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
6e866b33 | 536 | if (fd < 0) { |
8f232108 | 537 | bool ignore = ERRNO_IS_DEVICE_ABSENT(fd); |
f5caa8fa | 538 | |
8f232108 | 539 | log_device_debug_errno(dev, fd, "Failed to open '%s'%s: %m", val, ignore ? ", ignoring" : ""); |
f5caa8fa | 540 | if (!ignore) |
8f232108 | 541 | return fd; |
f5caa8fa MB |
542 | |
543 | goto nolock; | |
6e866b33 MB |
544 | } |
545 | ||
546 | if (flock(fd, LOCK_SH|LOCK_NB) < 0) | |
547 | return log_device_debug_errno(dev, errno, "Failed to flock(%s): %m", val); | |
b012e921 | 548 | |
6e866b33 MB |
549 | *ret_fd = TAKE_FD(fd); |
550 | return 1; | |
f5caa8fa MB |
551 | |
552 | nolock: | |
553 | *ret_fd = -1; | |
554 | return 0; | |
b012e921 MB |
555 | } |
556 | ||
a10f5d05 MB |
557 | static int worker_mark_block_device_read_only(sd_device *dev) { |
558 | _cleanup_close_ int fd = -1; | |
559 | const char *val; | |
560 | int state = 1, r; | |
561 | ||
562 | assert(dev); | |
563 | ||
564 | if (!arg_blockdev_read_only) | |
565 | return 0; | |
566 | ||
567 | /* Do this only once, when the block device is new. If the device is later retriggered let's not | |
568 | * toggle the bit again, so that people can boot up with full read-only mode and then unset the bit | |
569 | * for specific devices only. */ | |
3a6ce677 | 570 | if (!device_for_action(dev, SD_DEVICE_ADD)) |
a10f5d05 MB |
571 | return 0; |
572 | ||
573 | r = sd_device_get_subsystem(dev, &val); | |
574 | if (r < 0) | |
575 | return log_device_debug_errno(dev, r, "Failed to get subsystem: %m"); | |
576 | ||
577 | if (!streq(val, "block")) | |
578 | return 0; | |
579 | ||
580 | r = sd_device_get_sysname(dev, &val); | |
581 | if (r < 0) | |
582 | return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); | |
583 | ||
584 | /* Exclude synthetic devices for now, this is supposed to be a safety feature to avoid modification | |
585 | * of physical devices, and what sits on top of those doesn't really matter if we don't allow the | |
586 | * underlying block devices to receive changes. */ | |
587 | if (STARTSWITH_SET(val, "dm-", "md", "drbd", "loop", "nbd", "zram")) | |
588 | return 0; | |
589 | ||
086111aa | 590 | fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
a10f5d05 | 591 | if (fd < 0) |
8f232108 | 592 | return log_device_debug_errno(dev, fd, "Failed to open '%s', ignoring: %m", val); |
a10f5d05 MB |
593 | |
594 | if (ioctl(fd, BLKROSET, &state) < 0) | |
595 | return log_device_warning_errno(dev, errno, "Failed to mark block device '%s' read-only: %m", val); | |
596 | ||
597 | log_device_info(dev, "Successfully marked block device '%s' read-only.", val); | |
598 | return 0; | |
599 | } | |
600 | ||
6e866b33 MB |
601 | static int worker_process_device(Manager *manager, sd_device *dev) { |
602 | _cleanup_(udev_event_freep) UdevEvent *udev_event = NULL; | |
603 | _cleanup_close_ int fd_lock = -1; | |
6e866b33 | 604 | int r; |
663996b3 | 605 | |
6e866b33 MB |
606 | assert(manager); |
607 | assert(dev); | |
608 | ||
3a6ce677 | 609 | log_device_uevent(dev, "Processing device"); |
663996b3 | 610 | |
3a6ce677 | 611 | udev_event = udev_event_new(dev, arg_exec_delay_usec, manager->rtnl, manager->log_level); |
6e866b33 MB |
612 | if (!udev_event) |
613 | return -ENOMEM; | |
663996b3 | 614 | |
f5caa8fa MB |
615 | /* If this is a block device and the device is locked currently via the BSD advisory locks, |
616 | * someone else is using it exclusively. We don't run our udev rules now to not interfere. | |
617 | * Instead of processing the event, we requeue the event and will try again after a delay. | |
618 | * | |
619 | * The user-facing side of this: https://systemd.io/BLOCK_DEVICE_LOCKING */ | |
8f232108 | 620 | r = worker_lock_whole_disk(dev, &fd_lock); |
f5caa8fa MB |
621 | if (r == -EAGAIN) |
622 | return EVENT_RESULT_TRY_AGAIN; | |
6e866b33 MB |
623 | if (r < 0) |
624 | return r; | |
663996b3 | 625 | |
a10f5d05 MB |
626 | (void) worker_mark_block_device_read_only(dev); |
627 | ||
6e866b33 | 628 | /* apply rules, create node, symlinks */ |
8b3d4ff0 MB |
629 | r = udev_event_execute_rules( |
630 | udev_event, | |
631 | manager->inotify_fd, | |
632 | arg_event_timeout_usec, | |
633 | arg_timeout_signal, | |
634 | manager->properties, | |
635 | manager->rules); | |
f2dec872 BR |
636 | if (r < 0) |
637 | return r; | |
638 | ||
a10f5d05 | 639 | udev_event_execute_run(udev_event, arg_event_timeout_usec, arg_timeout_signal); |
663996b3 | 640 | |
6e866b33 MB |
641 | if (!manager->rtnl) |
642 | /* in case rtnl was initialized */ | |
643 | manager->rtnl = sd_netlink_ref(udev_event->rtnl); | |
663996b3 | 644 | |
086111aa | 645 | udev_event_process_inotify_watch(udev_event, manager->inotify_fd); |
663996b3 | 646 | |
3a6ce677 | 647 | log_device_uevent(dev, "Device processed"); |
6e866b33 MB |
648 | return 0; |
649 | } | |
663996b3 | 650 | |
6e866b33 | 651 | static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *dev, void *userdata) { |
086111aa | 652 | Manager *manager = ASSERT_PTR(userdata); |
6e866b33 | 653 | int r; |
60f067b4 | 654 | |
6e866b33 | 655 | assert(dev); |
5eef597e | 656 | |
6e866b33 | 657 | r = worker_process_device(manager, dev); |
f5caa8fa MB |
658 | if (r == EVENT_RESULT_TRY_AGAIN) |
659 | /* if we couldn't acquire the flock(), then requeue the event */ | |
660 | log_device_debug(dev, "Block device is currently locked, requeueing the event."); | |
20a6e51f MB |
661 | else { |
662 | if (r < 0) | |
663 | log_device_warning_errno(dev, r, "Failed to process device, ignoring: %m"); | |
5eef597e | 664 | |
20a6e51f | 665 | /* send processed event back to libudev listeners */ |
f5caa8fa | 666 | device_broadcast(monitor, dev, r); |
20a6e51f | 667 | } |
663996b3 | 668 | |
6e866b33 | 669 | /* send udevd the result of the event execution */ |
f5caa8fa | 670 | r = worker_send_result(manager, r); |
6e866b33 MB |
671 | if (r < 0) |
672 | log_device_warning_errno(dev, r, "Failed to send signal to main daemon, ignoring: %m"); | |
663996b3 | 673 | |
3a6ce677 BR |
674 | /* Reset the log level, as it might be changed by "OPTIONS=log_level=". */ |
675 | log_set_max_level(manager->log_level); | |
676 | ||
6e866b33 MB |
677 | return 1; |
678 | } | |
663996b3 | 679 | |
6e866b33 MB |
680 | static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device *first_device) { |
681 | _cleanup_(sd_device_unrefp) sd_device *dev = first_device; | |
682 | _cleanup_(manager_freep) Manager *manager = _manager; | |
7c20daf6 | 683 | int r; |
60f067b4 | 684 | |
6e866b33 MB |
685 | assert(manager); |
686 | assert(monitor); | |
687 | assert(dev); | |
663996b3 | 688 | |
a032b68d | 689 | assert_se(unsetenv("NOTIFY_SOCKET") == 0); |
663996b3 | 690 | |
6e866b33 MB |
691 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, -1) >= 0); |
692 | ||
693 | /* Reset OOM score, we only protect the main daemon. */ | |
694 | r = set_oom_score_adjust(0); | |
695 | if (r < 0) | |
696 | log_debug_errno(r, "Failed to reset OOM score, ignoring: %m"); | |
697 | ||
8b3d4ff0 | 698 | /* Clear unnecessary data in Manager object. */ |
6e866b33 MB |
699 | manager_clear_for_worker(manager); |
700 | ||
701 | r = sd_event_new(&manager->event); | |
702 | if (r < 0) | |
703 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
704 | ||
705 | r = sd_event_add_signal(manager->event, NULL, SIGTERM, NULL, NULL); | |
706 | if (r < 0) | |
707 | return log_error_errno(r, "Failed to set SIGTERM event: %m"); | |
708 | ||
709 | r = sd_device_monitor_attach_event(monitor, manager->event); | |
710 | if (r < 0) | |
711 | return log_error_errno(r, "Failed to attach event loop to device monitor: %m"); | |
712 | ||
713 | r = sd_device_monitor_start(monitor, worker_device_monitor_handler, manager); | |
714 | if (r < 0) | |
715 | return log_error_errno(r, "Failed to start device monitor: %m"); | |
716 | ||
6e866b33 MB |
717 | /* Process first device */ |
718 | (void) worker_device_monitor_handler(monitor, dev, manager); | |
719 | ||
720 | r = sd_event_loop(manager->event); | |
721 | if (r < 0) | |
722 | return log_error_errno(r, "Event loop failed: %m"); | |
723 | ||
7c20daf6 | 724 | return 0; |
6e866b33 MB |
725 | } |
726 | ||
ea0999c9 | 727 | static int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) { |
086111aa | 728 | Event *event = ASSERT_PTR(userdata); |
ea0999c9 | 729 | |
ea0999c9 MB |
730 | assert(event->worker); |
731 | ||
732 | kill_and_sigcont(event->worker->pid, arg_timeout_signal); | |
733 | event->worker->state = WORKER_KILLED; | |
734 | ||
735 | log_device_error(event->dev, "Worker ["PID_FMT"] processing SEQNUM=%"PRIu64" killed", event->worker->pid, event->seqnum); | |
736 | ||
737 | return 1; | |
738 | } | |
739 | ||
740 | static int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) { | |
086111aa | 741 | Event *event = ASSERT_PTR(userdata); |
ea0999c9 | 742 | |
ea0999c9 MB |
743 | assert(event->worker); |
744 | ||
745 | log_device_warning(event->dev, "Worker ["PID_FMT"] processing SEQNUM=%"PRIu64" is taking a long time", event->worker->pid, event->seqnum); | |
746 | ||
747 | return 1; | |
748 | } | |
749 | ||
750 | static void worker_attach_event(Worker *worker, Event *event) { | |
751 | sd_event *e; | |
752 | ||
753 | assert(worker); | |
754 | assert(worker->manager); | |
755 | assert(event); | |
756 | assert(!event->worker); | |
757 | assert(!worker->event); | |
758 | ||
759 | worker->state = WORKER_RUNNING; | |
760 | worker->event = event; | |
761 | event->state = EVENT_RUNNING; | |
762 | event->worker = worker; | |
763 | ||
764 | e = worker->manager->event; | |
765 | ||
766 | (void) sd_event_add_time_relative(e, &event->timeout_warning_event, CLOCK_MONOTONIC, | |
767 | udev_warn_timeout(arg_event_timeout_usec), USEC_PER_SEC, | |
768 | on_event_timeout_warning, event); | |
769 | ||
770 | (void) sd_event_add_time_relative(e, &event->timeout_event, CLOCK_MONOTONIC, | |
771 | arg_event_timeout_usec, USEC_PER_SEC, | |
772 | on_event_timeout, event); | |
773 | } | |
774 | ||
775 | static int worker_spawn(Manager *manager, Event *event) { | |
6e866b33 | 776 | _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *worker_monitor = NULL; |
ea0999c9 | 777 | Worker *worker; |
6e866b33 MB |
778 | pid_t pid; |
779 | int r; | |
780 | ||
781 | /* listen for new events */ | |
782 | r = device_monitor_new_full(&worker_monitor, MONITOR_GROUP_NONE, -1); | |
783 | if (r < 0) | |
784 | return r; | |
785 | ||
086111aa LB |
786 | (void) sd_device_monitor_set_description(worker_monitor, "worker"); |
787 | ||
6e866b33 MB |
788 | /* allow the main daemon netlink address to send devices to the worker */ |
789 | r = device_monitor_allow_unicast_sender(worker_monitor, manager->monitor); | |
790 | if (r < 0) | |
791 | return log_error_errno(r, "Worker: Failed to set unicast sender: %m"); | |
792 | ||
793 | r = device_monitor_enable_receiving(worker_monitor); | |
794 | if (r < 0) | |
795 | return log_error_errno(r, "Worker: Failed to enable receiving of device: %m"); | |
796 | ||
82126c13 | 797 | r = safe_fork("(udev-worker)", FORK_DEATHSIG, &pid); |
6e866b33 MB |
798 | if (r < 0) { |
799 | event->state = EVENT_QUEUED; | |
800 | return log_error_errno(r, "Failed to fork() worker: %m"); | |
801 | } | |
802 | if (r == 0) { | |
8b3d4ff0 MB |
803 | DEVICE_TRACE_POINT(worker_spawned, event->dev, getpid()); |
804 | ||
6e866b33 MB |
805 | /* Worker process */ |
806 | r = worker_main(manager, worker_monitor, sd_device_ref(event->dev)); | |
663996b3 | 807 | log_close(); |
e3bff60a | 808 | _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS); |
663996b3 | 809 | } |
e3bff60a | 810 | |
6e866b33 MB |
811 | r = worker_new(&worker, manager, worker_monitor, pid); |
812 | if (r < 0) | |
813 | return log_error_errno(r, "Failed to create worker object: %m"); | |
e3bff60a | 814 | |
6e866b33 | 815 | worker_attach_event(worker, event); |
e3bff60a | 816 | |
6e866b33 MB |
817 | log_device_debug(event->dev, "Worker ["PID_FMT"] is forked for processing SEQNUM=%"PRIu64".", pid, event->seqnum); |
818 | return 0; | |
663996b3 MS |
819 | } |
820 | ||
ea0999c9 | 821 | static int event_run(Event *event) { |
f2dec872 | 822 | static bool log_children_max_reached = true; |
ea0999c9 MB |
823 | Manager *manager; |
824 | Worker *worker; | |
6e866b33 | 825 | int r; |
e3bff60a | 826 | |
e3bff60a | 827 | assert(event); |
ea0999c9 | 828 | assert(event->manager); |
663996b3 | 829 | |
3a6ce677 | 830 | log_device_uevent(event->dev, "Device ready for processing"); |
a10f5d05 | 831 | |
d9f5095a MB |
832 | (void) event_source_disable(event->retry_event_source); |
833 | ||
ea0999c9 | 834 | manager = event->manager; |
a032b68d | 835 | HASHMAP_FOREACH(worker, manager->workers) { |
663996b3 MS |
836 | if (worker->state != WORKER_IDLE) |
837 | continue; | |
838 | ||
6e866b33 MB |
839 | r = device_monitor_send_device(manager->monitor, worker->monitor, event->dev); |
840 | if (r < 0) { | |
841 | log_device_error_errno(event->dev, r, "Worker ["PID_FMT"] did not accept message, killing the worker: %m", | |
842 | worker->pid); | |
843 | (void) kill(worker->pid, SIGKILL); | |
663996b3 MS |
844 | worker->state = WORKER_KILLED; |
845 | continue; | |
846 | } | |
e3bff60a | 847 | worker_attach_event(worker, event); |
ea0999c9 | 848 | return 1; /* event is now processing. */ |
663996b3 MS |
849 | } |
850 | ||
e3bff60a | 851 | if (hashmap_size(manager->workers) >= arg_children_max) { |
f2dec872 BR |
852 | /* Avoid spamming the debug logs if the limit is already reached and |
853 | * many events still need to be processed */ | |
854 | if (log_children_max_reached && arg_children_max > 1) { | |
6e866b33 | 855 | log_debug("Maximum number (%u) of children reached.", hashmap_size(manager->workers)); |
f2dec872 BR |
856 | log_children_max_reached = false; |
857 | } | |
ea0999c9 | 858 | return 0; /* no free worker */ |
663996b3 MS |
859 | } |
860 | ||
f2dec872 BR |
861 | /* Re-enable the debug message for the next batch of events */ |
862 | log_children_max_reached = true; | |
863 | ||
663996b3 | 864 | /* start new worker and pass initial device */ |
ea0999c9 | 865 | r = worker_spawn(manager, event); |
6e866b33 MB |
866 | if (r < 0) |
867 | return r; | |
663996b3 | 868 | |
ea0999c9 MB |
869 | return 1; /* event is now processing. */ |
870 | } | |
e3bff60a | 871 | |
ea0999c9 | 872 | static int event_is_blocked(Event *event) { |
f5caa8fa | 873 | Event *loop_event = NULL; |
086111aa | 874 | int r; |
6e866b33 | 875 | |
ea0999c9 | 876 | /* lookup event for identical, parent, child device */ |
663996b3 | 877 | |
ea0999c9 MB |
878 | assert(event); |
879 | assert(event->manager); | |
880 | assert(event->blocker_seqnum <= event->seqnum); | |
663996b3 | 881 | |
f5caa8fa MB |
882 | if (event->retry_again_next_usec > 0) { |
883 | usec_t now_usec; | |
884 | ||
885 | r = sd_event_now(event->manager->event, CLOCK_BOOTTIME, &now_usec); | |
886 | if (r < 0) | |
887 | return r; | |
888 | ||
d9f5095a | 889 | if (event->retry_again_next_usec > now_usec) |
f5caa8fa MB |
890 | return true; |
891 | } | |
892 | ||
ea0999c9 MB |
893 | if (event->blocker_seqnum == event->seqnum) |
894 | /* we have checked previously and no blocker found */ | |
895 | return false; | |
663996b3 | 896 | |
f5caa8fa MB |
897 | LIST_FOREACH(event, e, event->manager->events) { |
898 | loop_event = e; | |
899 | ||
ea0999c9 MB |
900 | /* we already found a later event, earlier cannot block us, no need to check again */ |
901 | if (loop_event->seqnum < event->blocker_seqnum) | |
663996b3 MS |
902 | continue; |
903 | ||
ea0999c9 MB |
904 | /* event we checked earlier still exists, no need to check again */ |
905 | if (loop_event->seqnum == event->blocker_seqnum) | |
906 | return true; | |
8b3d4ff0 | 907 | |
ea0999c9 MB |
908 | /* found ourself, no later event can block us */ |
909 | if (loop_event->seqnum >= event->seqnum) | |
910 | goto no_blocker; | |
911 | ||
912 | /* found event we have not checked */ | |
913 | break; | |
663996b3 | 914 | } |
663996b3 | 915 | |
ea0999c9 MB |
916 | assert(loop_event); |
917 | assert(loop_event->seqnum > event->blocker_seqnum && | |
918 | loop_event->seqnum < event->seqnum); | |
6e866b33 | 919 | |
663996b3 | 920 | /* check if queue contains events we depend on */ |
f5caa8fa | 921 | LIST_FOREACH(event, e, loop_event) { |
f5caa8fa MB |
922 | loop_event = e; |
923 | ||
663996b3 MS |
924 | /* found ourself, no later event can block us */ |
925 | if (loop_event->seqnum >= event->seqnum) | |
ea0999c9 | 926 | goto no_blocker; |
663996b3 | 927 | |
086111aa | 928 | if (streq_ptr(loop_event->id, event->id)) |
ea0999c9 | 929 | break; |
663996b3 | 930 | |
086111aa LB |
931 | if (devpath_conflict(event->devpath, loop_event->devpath) || |
932 | devpath_conflict(event->devpath, loop_event->devpath_old) || | |
933 | devpath_conflict(event->devpath_old, loop_event->devpath)) | |
ea0999c9 | 934 | break; |
663996b3 | 935 | |
086111aa | 936 | if (event->devnode && streq_ptr(event->devnode, loop_event->devnode)) |
ea0999c9 | 937 | break; |
663996b3 MS |
938 | } |
939 | ||
ea0999c9 | 940 | assert(loop_event); |
bb4f798a | 941 | |
a10f5d05 MB |
942 | log_device_debug(event->dev, "SEQNUM=%" PRIu64 " blocked by SEQNUM=%" PRIu64, |
943 | event->seqnum, loop_event->seqnum); | |
944 | ||
ea0999c9 | 945 | event->blocker_seqnum = loop_event->seqnum; |
bb4f798a | 946 | return true; |
6e866b33 | 947 | |
ea0999c9 MB |
948 | no_blocker: |
949 | event->blocker_seqnum = event->seqnum; | |
950 | return false; | |
6e866b33 MB |
951 | } |
952 | ||
ea0999c9 | 953 | static int event_queue_start(Manager *manager) { |
6e866b33 | 954 | int r; |
663996b3 | 955 | |
e3bff60a MP |
956 | assert(manager); |
957 | ||
086111aa | 958 | if (!manager->events || manager->exit || manager->stop_exec_queue) |
ea0999c9 | 959 | return 0; |
86f210e9 | 960 | |
086111aa LB |
961 | /* To make the stack directory /run/udev/links cleaned up later. */ |
962 | manager->udev_node_needs_cleanup = true; | |
86f210e9 | 963 | |
6e866b33 MB |
964 | r = event_source_disable(manager->kill_workers_event); |
965 | if (r < 0) | |
966 | log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m"); | |
967 | ||
086111aa | 968 | manager_reload(manager, /* force = */ false); |
f5caa8fa MB |
969 | |
970 | LIST_FOREACH(event, event, manager->events) { | |
663996b3 MS |
971 | if (event->state != EVENT_QUEUED) |
972 | continue; | |
973 | ||
ea0999c9 MB |
974 | /* do not start event if parent or child event is still running or queued */ |
975 | r = event_is_blocked(event); | |
f5caa8fa MB |
976 | if (r > 0) |
977 | continue; | |
978 | if (r < 0) | |
ea0999c9 | 979 | log_device_warning_errno(event->dev, r, |
f5caa8fa MB |
980 | "Failed to check dependencies for event (SEQNUM=%"PRIu64", ACTION=%s), " |
981 | "assuming there is no blocking event, ignoring: %m", | |
ea0999c9 | 982 | event->seqnum, |
f5caa8fa | 983 | strna(device_action_to_string(event->action))); |
ea0999c9 | 984 | |
f5caa8fa MB |
985 | r = event_run(event); |
986 | if (r <= 0) /* 0 means there are no idle workers. Let's escape from the loop. */ | |
ea0999c9 | 987 | return r; |
f5caa8fa MB |
988 | } |
989 | ||
990 | return 0; | |
991 | } | |
992 | ||
d9f5095a MB |
993 | static int on_event_retry(sd_event_source *s, uint64_t usec, void *userdata) { |
994 | /* This does nothing. The on_post() callback will start the event if there exists an idle worker. */ | |
995 | return 1; | |
996 | } | |
997 | ||
f5caa8fa MB |
998 | static int event_requeue(Event *event) { |
999 | usec_t now_usec; | |
1000 | int r; | |
1001 | ||
1002 | assert(event); | |
1003 | assert(event->manager); | |
1004 | assert(event->manager->event); | |
1005 | ||
1006 | event->timeout_warning_event = sd_event_source_disable_unref(event->timeout_warning_event); | |
1007 | event->timeout_event = sd_event_source_disable_unref(event->timeout_event); | |
1008 | ||
1009 | /* add a short delay to suppress busy loop */ | |
1010 | r = sd_event_now(event->manager->event, CLOCK_BOOTTIME, &now_usec); | |
1011 | if (r < 0) | |
1012 | return log_device_warning_errno(event->dev, r, | |
1013 | "Failed to get current time, " | |
1014 | "skipping event (SEQNUM=%"PRIu64", ACTION=%s): %m", | |
1015 | event->seqnum, strna(device_action_to_string(event->action))); | |
1016 | ||
1017 | if (event->retry_again_timeout_usec > 0 && event->retry_again_timeout_usec <= now_usec) | |
1018 | return log_device_warning_errno(event->dev, SYNTHETIC_ERRNO(ETIMEDOUT), | |
1019 | "The underlying block device is locked by a process more than %s, " | |
1020 | "skipping event (SEQNUM=%"PRIu64", ACTION=%s).", | |
1021 | FORMAT_TIMESPAN(EVENT_RETRY_TIMEOUT_USEC, USEC_PER_MINUTE), | |
1022 | event->seqnum, strna(device_action_to_string(event->action))); | |
1023 | ||
1024 | event->retry_again_next_usec = usec_add(now_usec, EVENT_RETRY_INTERVAL_USEC); | |
1025 | if (event->retry_again_timeout_usec == 0) | |
1026 | event->retry_again_timeout_usec = usec_add(now_usec, EVENT_RETRY_TIMEOUT_USEC); | |
1027 | ||
d9f5095a MB |
1028 | r = event_reset_time_relative(event->manager->event, &event->retry_event_source, |
1029 | CLOCK_MONOTONIC, EVENT_RETRY_INTERVAL_USEC, 0, | |
1030 | on_event_retry, NULL, | |
1031 | 0, "retry-event", true); | |
1032 | if (r < 0) | |
1033 | return log_device_warning_errno(event->dev, r, "Failed to reset timer event source for retrying event, " | |
1034 | "skipping event (SEQNUM=%"PRIu64", ACTION=%s): %m", | |
1035 | event->seqnum, strna(device_action_to_string(event->action))); | |
1036 | ||
f5caa8fa MB |
1037 | if (event->worker && event->worker->event == event) |
1038 | event->worker->event = NULL; | |
1039 | event->worker = NULL; | |
1040 | ||
1041 | event->state = EVENT_QUEUED; | |
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | static int event_queue_assume_block_device_unlocked(Manager *manager, sd_device *dev) { | |
1046 | const char *devname; | |
1047 | int r; | |
1048 | ||
1049 | /* When a new event for a block device is queued or we get an inotify event, assume that the | |
1050 | * device is not locked anymore. The assumption may not be true, but that should not cause any | |
1051 | * issues, as in that case events will be requeued soon. */ | |
1052 | ||
8f232108 | 1053 | r = device_get_whole_disk(dev, NULL, &devname); |
f5caa8fa MB |
1054 | if (r <= 0) |
1055 | return r; | |
1056 | ||
1057 | LIST_FOREACH(event, event, manager->events) { | |
1058 | const char *event_devname; | |
1059 | ||
1060 | if (event->state != EVENT_QUEUED) | |
663996b3 MS |
1061 | continue; |
1062 | ||
f5caa8fa MB |
1063 | if (event->retry_again_next_usec == 0) |
1064 | continue; | |
1065 | ||
8f232108 | 1066 | if (device_get_whole_disk(event->dev, NULL, &event_devname) <= 0) |
f5caa8fa MB |
1067 | continue; |
1068 | ||
1069 | if (!streq(devname, event_devname)) | |
1070 | continue; | |
1071 | ||
1072 | event->retry_again_next_usec = 0; | |
663996b3 | 1073 | } |
ea0999c9 MB |
1074 | |
1075 | return 0; | |
663996b3 MS |
1076 | } |
1077 | ||
ea0999c9 | 1078 | static int event_queue_insert(Manager *manager, sd_device *dev) { |
086111aa | 1079 | const char *devpath, *devpath_old = NULL, *id = NULL, *devnode = NULL; |
f5caa8fa | 1080 | sd_device_action_t action; |
ea0999c9 | 1081 | uint64_t seqnum; |
f5caa8fa | 1082 | Event *event; |
ea0999c9 | 1083 | int r; |
663996b3 | 1084 | |
ea0999c9 MB |
1085 | assert(manager); |
1086 | assert(dev); | |
663996b3 | 1087 | |
ea0999c9 MB |
1088 | /* only one process can add events to the queue */ |
1089 | assert(manager->pid == getpid_cached()); | |
1090 | ||
1091 | /* We only accepts devices received by device monitor. */ | |
1092 | r = sd_device_get_seqnum(dev, &seqnum); | |
1093 | if (r < 0) | |
1094 | return r; | |
1095 | ||
f5caa8fa | 1096 | r = sd_device_get_action(dev, &action); |
ea0999c9 MB |
1097 | if (r < 0) |
1098 | return r; | |
1099 | ||
086111aa LB |
1100 | r = sd_device_get_devpath(dev, &devpath); |
1101 | if (r < 0) | |
1102 | return r; | |
1103 | ||
1104 | r = sd_device_get_property_value(dev, "DEVPATH_OLD", &devpath_old); | |
1105 | if (r < 0 && r != -ENOENT) | |
1106 | return r; | |
1107 | ||
1108 | r = device_get_device_id(dev, &id); | |
1109 | if (r < 0 && r != -ENOENT) | |
1110 | return r; | |
1111 | ||
1112 | r = sd_device_get_devname(dev, &devnode); | |
1113 | if (r < 0 && r != -ENOENT) | |
1114 | return r; | |
1115 | ||
ea0999c9 MB |
1116 | event = new(Event, 1); |
1117 | if (!event) | |
1118 | return -ENOMEM; | |
1119 | ||
1120 | *event = (Event) { | |
1121 | .manager = manager, | |
1122 | .dev = sd_device_ref(dev), | |
ea0999c9 | 1123 | .seqnum = seqnum, |
f5caa8fa | 1124 | .action = action, |
086111aa LB |
1125 | .id = id, |
1126 | .devpath = devpath, | |
1127 | .devpath_old = devpath_old, | |
1128 | .devnode = devnode, | |
ea0999c9 MB |
1129 | .state = EVENT_QUEUED, |
1130 | }; | |
1131 | ||
086111aa | 1132 | if (!manager->events) { |
ea0999c9 MB |
1133 | r = touch("/run/udev/queue"); |
1134 | if (r < 0) | |
1135 | log_warning_errno(r, "Failed to touch /run/udev/queue, ignoring: %m"); | |
1136 | } | |
1137 | ||
1138 | LIST_APPEND(event, manager->events, event); | |
1139 | ||
1140 | log_device_uevent(dev, "Device is queued"); | |
1141 | ||
1142 | return 0; | |
1143 | } | |
1144 | ||
1145 | static int on_uevent(sd_device_monitor *monitor, sd_device *dev, void *userdata) { | |
086111aa | 1146 | Manager *manager = ASSERT_PTR(userdata); |
ea0999c9 MB |
1147 | int r; |
1148 | ||
ea0999c9 MB |
1149 | DEVICE_TRACE_POINT(kernel_uevent_received, dev); |
1150 | ||
1151 | device_ensure_usec_initialized(dev, NULL); | |
1152 | ||
1153 | r = event_queue_insert(manager, dev); | |
1154 | if (r < 0) { | |
1155 | log_device_error_errno(dev, r, "Failed to insert device into event queue: %m"); | |
1156 | return 1; | |
663996b3 | 1157 | } |
ea0999c9 | 1158 | |
f5caa8fa MB |
1159 | (void) event_queue_assume_block_device_unlocked(manager, dev); |
1160 | ||
ea0999c9 | 1161 | return 1; |
663996b3 MS |
1162 | } |
1163 | ||
e3bff60a | 1164 | static int on_worker(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
086111aa | 1165 | Manager *manager = ASSERT_PTR(userdata); |
e3bff60a | 1166 | |
663996b3 | 1167 | for (;;) { |
8f232108 | 1168 | EventResult result; |
f5caa8fa | 1169 | struct iovec iovec = IOVEC_MAKE(&result, sizeof(result)); |
a10f5d05 | 1170 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct ucred))) control; |
e3bff60a MP |
1171 | struct msghdr msghdr = { |
1172 | .msg_iov = &iovec, | |
1173 | .msg_iovlen = 1, | |
1174 | .msg_control = &control, | |
1175 | .msg_controllen = sizeof(control), | |
1176 | }; | |
663996b3 | 1177 | ssize_t size; |
a10f5d05 | 1178 | struct ucred *ucred; |
ea0999c9 | 1179 | Worker *worker; |
663996b3 | 1180 | |
20a6e51f MB |
1181 | size = recvmsg_safe(fd, &msghdr, MSG_DONTWAIT); |
1182 | if (size == -EINTR) | |
1183 | continue; | |
1184 | if (size == -EAGAIN) | |
1185 | /* nothing more to read */ | |
1186 | break; | |
1187 | if (size < 0) | |
1188 | return log_error_errno(size, "Failed to receive message: %m"); | |
1189 | ||
1190 | cmsg_close_all(&msghdr); | |
663996b3 | 1191 | |
f5caa8fa | 1192 | if (size != sizeof(result)) { |
6e866b33 | 1193 | log_warning("Ignoring worker message with invalid size %zi bytes", size); |
e3bff60a MP |
1194 | continue; |
1195 | } | |
663996b3 | 1196 | |
a10f5d05 | 1197 | ucred = CMSG_FIND_DATA(&msghdr, SOL_SOCKET, SCM_CREDENTIALS, struct ucred); |
e3bff60a | 1198 | if (!ucred || ucred->pid <= 0) { |
6e866b33 | 1199 | log_warning("Ignoring worker message without valid PID"); |
e3bff60a | 1200 | continue; |
663996b3 | 1201 | } |
e3bff60a MP |
1202 | |
1203 | /* lookup worker who sent the signal */ | |
db2df898 | 1204 | worker = hashmap_get(manager->workers, PID_TO_PTR(ucred->pid)); |
e3bff60a | 1205 | if (!worker) { |
6e866b33 | 1206 | log_debug("Worker ["PID_FMT"] returned, but is no longer tracked", ucred->pid); |
e3bff60a MP |
1207 | continue; |
1208 | } | |
1209 | ||
8b3d4ff0 MB |
1210 | if (worker->state == WORKER_KILLING) { |
1211 | worker->state = WORKER_KILLED; | |
1212 | (void) kill(worker->pid, SIGTERM); | |
1213 | } else if (worker->state != WORKER_KILLED) | |
e3bff60a MP |
1214 | worker->state = WORKER_IDLE; |
1215 | ||
1216 | /* worker returned */ | |
f5caa8fa MB |
1217 | if (result == EVENT_RESULT_TRY_AGAIN && |
1218 | event_requeue(worker->event) < 0) | |
1219 | device_broadcast(manager->monitor, worker->event->dev, -ETIMEDOUT); | |
1220 | ||
1221 | /* When event_requeue() succeeds, worker->event is NULL, and event_free() handles NULL gracefully. */ | |
e3bff60a | 1222 | event_free(worker->event); |
663996b3 | 1223 | } |
e3bff60a MP |
1224 | |
1225 | return 1; | |
1226 | } | |
1227 | ||
663996b3 | 1228 | /* receive the udevd message from userspace */ |
ea0999c9 | 1229 | static int on_ctrl_msg(UdevCtrl *uctrl, UdevCtrlMessageType type, const UdevCtrlMessageValue *value, void *userdata) { |
086111aa | 1230 | Manager *manager = ASSERT_PTR(userdata); |
bb4f798a | 1231 | int r; |
663996b3 | 1232 | |
bb4f798a | 1233 | assert(value); |
e3bff60a | 1234 | |
bb4f798a MB |
1235 | switch (type) { |
1236 | case UDEV_CTRL_SET_LOG_LEVEL: | |
086111aa LB |
1237 | if ((value->intval & LOG_PRIMASK) != value->intval) { |
1238 | log_debug("Received invalid udev control message (SET_LOG_LEVEL, %i), ignoring.", value->intval); | |
1239 | break; | |
1240 | } | |
1241 | ||
a032b68d | 1242 | log_debug("Received udev control message (SET_LOG_LEVEL), setting log_level=%i", value->intval); |
086111aa LB |
1243 | |
1244 | r = log_get_max_level(); | |
1245 | if (r == value->intval) | |
1246 | break; | |
1247 | ||
3a6ce677 BR |
1248 | log_set_max_level(value->intval); |
1249 | manager->log_level = value->intval; | |
8b3d4ff0 | 1250 | manager_kill_workers(manager, false); |
bb4f798a MB |
1251 | break; |
1252 | case UDEV_CTRL_STOP_EXEC_QUEUE: | |
6e866b33 | 1253 | log_debug("Received udev control message (STOP_EXEC_QUEUE)"); |
e3bff60a | 1254 | manager->stop_exec_queue = true; |
bb4f798a MB |
1255 | break; |
1256 | case UDEV_CTRL_START_EXEC_QUEUE: | |
6e866b33 | 1257 | log_debug("Received udev control message (START_EXEC_QUEUE)"); |
e3bff60a | 1258 | manager->stop_exec_queue = false; |
086111aa | 1259 | /* It is not necessary to call event_queue_start() here, as it will be called in on_post() if necessary. */ |
bb4f798a MB |
1260 | break; |
1261 | case UDEV_CTRL_RELOAD: | |
6e866b33 | 1262 | log_debug("Received udev control message (RELOAD)"); |
086111aa | 1263 | manager_reload(manager, /* force = */ true); |
bb4f798a MB |
1264 | break; |
1265 | case UDEV_CTRL_SET_ENV: { | |
ea0999c9 MB |
1266 | _unused_ _cleanup_free_ char *old_val = NULL; |
1267 | _cleanup_free_ char *key = NULL, *val = NULL, *old_key = NULL; | |
bb4f798a | 1268 | const char *eq; |
6e866b33 | 1269 | |
bb4f798a | 1270 | eq = strchr(value->buf, '='); |
6e866b33 | 1271 | if (!eq) { |
bb4f798a | 1272 | log_error("Invalid key format '%s'", value->buf); |
6e866b33 MB |
1273 | return 1; |
1274 | } | |
1275 | ||
bb4f798a | 1276 | key = strndup(value->buf, eq - value->buf); |
6e866b33 MB |
1277 | if (!key) { |
1278 | log_oom(); | |
1279 | return 1; | |
663996b3 | 1280 | } |
6e866b33 MB |
1281 | |
1282 | old_val = hashmap_remove2(manager->properties, key, (void **) &old_key); | |
1283 | ||
1284 | r = hashmap_ensure_allocated(&manager->properties, &string_hash_ops); | |
1285 | if (r < 0) { | |
1286 | log_oom(); | |
1287 | return 1; | |
1288 | } | |
1289 | ||
1290 | eq++; | |
f2dec872 | 1291 | if (isempty(eq)) { |
6e866b33 MB |
1292 | log_debug("Received udev control message (ENV), unsetting '%s'", key); |
1293 | ||
1294 | r = hashmap_put(manager->properties, key, NULL); | |
1295 | if (r < 0) { | |
1296 | log_oom(); | |
1297 | return 1; | |
1298 | } | |
1299 | } else { | |
1300 | val = strdup(eq); | |
1301 | if (!val) { | |
1302 | log_oom(); | |
1303 | return 1; | |
1304 | } | |
1305 | ||
1306 | log_debug("Received udev control message (ENV), setting '%s=%s'", key, val); | |
1307 | ||
1308 | r = hashmap_put(manager->properties, key, val); | |
1309 | if (r < 0) { | |
1310 | log_oom(); | |
1311 | return 1; | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | key = val = NULL; | |
8b3d4ff0 | 1316 | manager_kill_workers(manager, false); |
bb4f798a | 1317 | break; |
663996b3 | 1318 | } |
bb4f798a MB |
1319 | case UDEV_CTRL_SET_CHILDREN_MAX: |
1320 | if (value->intval <= 0) { | |
1321 | log_debug("Received invalid udev control message (SET_MAX_CHILDREN, %i), ignoring.", value->intval); | |
1322 | return 0; | |
1323 | } | |
663996b3 | 1324 | |
bb4f798a MB |
1325 | log_debug("Received udev control message (SET_MAX_CHILDREN), setting children_max=%i", value->intval); |
1326 | arg_children_max = value->intval; | |
8a584da2 | 1327 | |
ea0999c9 | 1328 | notify_ready(); |
bb4f798a MB |
1329 | break; |
1330 | case UDEV_CTRL_PING: | |
7c20daf6 | 1331 | log_debug("Received udev control message (PING)"); |
bb4f798a MB |
1332 | break; |
1333 | case UDEV_CTRL_EXIT: | |
6e866b33 | 1334 | log_debug("Received udev control message (EXIT)"); |
86f210e9 | 1335 | manager_exit(manager); |
bb4f798a MB |
1336 | break; |
1337 | default: | |
1338 | log_debug("Received unknown udev control message, ignoring"); | |
663996b3 | 1339 | } |
e3bff60a MP |
1340 | |
1341 | return 1; | |
663996b3 MS |
1342 | } |
1343 | ||
3a6ce677 | 1344 | static int synthesize_change_one(sd_device *dev, sd_device *target) { |
f2dec872 BR |
1345 | int r; |
1346 | ||
3a6ce677 BR |
1347 | if (DEBUG_LOGGING) { |
1348 | const char *syspath = NULL; | |
1349 | (void) sd_device_get_syspath(target, &syspath); | |
1350 | log_device_debug(dev, "device is closed, synthesising 'change' on %s", strna(syspath)); | |
1351 | } | |
1352 | ||
1353 | r = sd_device_trigger(target, SD_DEVICE_CHANGE); | |
f2dec872 | 1354 | if (r < 0) |
3a6ce677 BR |
1355 | return log_device_debug_errno(target, r, "Failed to trigger 'change' uevent: %m"); |
1356 | ||
8b3d4ff0 MB |
1357 | DEVICE_TRACE_POINT(synthetic_change_event, dev); |
1358 | ||
f2dec872 BR |
1359 | return 0; |
1360 | } | |
1361 | ||
6e866b33 | 1362 | static int synthesize_change(sd_device *dev) { |
086111aa LB |
1363 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
1364 | bool part_table_read; | |
1365 | const char *sysname; | |
1366 | sd_device *d; | |
1367 | int r, k; | |
60f067b4 | 1368 | |
086111aa | 1369 | r = sd_device_get_sysname(dev, &sysname); |
6e866b33 MB |
1370 | if (r < 0) |
1371 | return r; | |
1372 | ||
086111aa LB |
1373 | if (startswith(sysname, "dm-") || block_device_is_whole_disk(dev) <= 0) |
1374 | return synthesize_change_one(dev, dev); | |
1375 | ||
1376 | r = blockdev_reread_partition_table(dev); | |
6e866b33 | 1377 | if (r < 0) |
086111aa LB |
1378 | log_device_debug_errno(dev, r, "Failed to re-read partition table, ignoring: %m"); |
1379 | part_table_read = r >= 0; | |
6e866b33 | 1380 | |
086111aa LB |
1381 | /* search for partitions */ |
1382 | r = partition_enumerator_new(dev, &e); | |
6e866b33 MB |
1383 | if (r < 0) |
1384 | return r; | |
1385 | ||
086111aa LB |
1386 | /* We have partitions and re-read the table, the kernel already sent out a "change" |
1387 | * event for the disk, and "remove/add" for all partitions. */ | |
1388 | if (part_table_read && sd_device_enumerator_get_device_first(e)) | |
1389 | return 0; | |
60f067b4 | 1390 | |
086111aa LB |
1391 | /* We have partitions but re-reading the partition table did not work, synthesize |
1392 | * "change" for the disk and all partitions. */ | |
1393 | r = synthesize_change_one(dev, dev); | |
1394 | FOREACH_DEVICE(e, d) { | |
1395 | k = synthesize_change_one(dev, d); | |
1396 | if (k < 0 && r >= 0) | |
1397 | r = k; | |
1398 | } | |
60f067b4 | 1399 | |
086111aa | 1400 | return r; |
60f067b4 JS |
1401 | } |
1402 | ||
e3bff60a | 1403 | static int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
086111aa | 1404 | Manager *manager = ASSERT_PTR(userdata); |
e735f4d4 | 1405 | union inotify_event_buffer buffer; |
f47781d8 | 1406 | ssize_t l; |
6e866b33 | 1407 | int r; |
663996b3 | 1408 | |
e3bff60a | 1409 | l = read(fd, &buffer, sizeof(buffer)); |
f47781d8 | 1410 | if (l < 0) { |
ea0999c9 | 1411 | if (ERRNO_IS_TRANSIENT(errno)) |
086111aa | 1412 | return 0; |
663996b3 | 1413 | |
f47781d8 | 1414 | return log_error_errno(errno, "Failed to read inotify fd: %m"); |
663996b3 MS |
1415 | } |
1416 | ||
f5caa8fa | 1417 | FOREACH_INOTIFY_EVENT_WARN(e, buffer, l) { |
6e866b33 MB |
1418 | _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
1419 | const char *devnode; | |
663996b3 | 1420 | |
086111aa LB |
1421 | /* Do not handle IN_IGNORED here. Especially, do not try to call udev_watch_end() from the |
1422 | * main process. Otherwise, the pair of the symlinks may become inconsistent, and several | |
1423 | * garbage may remain. The old symlinks are removed by a worker that processes the | |
1424 | * corresponding 'remove' uevent; | |
1425 | * udev_event_execute_rules() -> event_execute_rules_on_remove() -> udev_watch_end(). */ | |
1426 | ||
1427 | if (!FLAGS_SET(e->mask, IN_CLOSE_WRITE)) | |
1428 | continue; | |
1429 | ||
8b3d4ff0 MB |
1430 | r = device_new_from_watch_handle(&dev, e->wd); |
1431 | if (r < 0) { | |
086111aa | 1432 | /* Device may be removed just after closed. */ |
8b3d4ff0 | 1433 | log_debug_errno(r, "Failed to create sd_device object from watch handle, ignoring: %m"); |
60f067b4 | 1434 | continue; |
8b3d4ff0 | 1435 | } |
663996b3 | 1436 | |
086111aa LB |
1437 | r = sd_device_get_devname(dev, &devnode); |
1438 | if (r < 0) { | |
1439 | /* Also here, device may be already removed. */ | |
1440 | log_device_debug_errno(dev, r, "Failed to get device node, ignoring: %m"); | |
6e866b33 | 1441 | continue; |
f5caa8fa | 1442 | } |
8b3d4ff0 | 1443 | |
086111aa LB |
1444 | log_device_debug(dev, "Received inotify event for %s.", devnode); |
1445 | ||
1446 | (void) event_queue_assume_block_device_unlocked(manager, dev); | |
1447 | (void) synthesize_change(dev); | |
663996b3 MS |
1448 | } |
1449 | ||
086111aa | 1450 | return 0; |
663996b3 MS |
1451 | } |
1452 | ||
e3bff60a | 1453 | static int on_sigterm(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
086111aa | 1454 | Manager *manager = ASSERT_PTR(userdata); |
663996b3 | 1455 | |
86f210e9 | 1456 | manager_exit(manager); |
663996b3 | 1457 | |
e3bff60a MP |
1458 | return 1; |
1459 | } | |
663996b3 | 1460 | |
e3bff60a | 1461 | static int on_sighup(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
086111aa | 1462 | Manager *manager = ASSERT_PTR(userdata); |
e3bff60a | 1463 | |
086111aa | 1464 | manager_reload(manager, /* force = */ true); |
e3bff60a MP |
1465 | |
1466 | return 1; | |
1467 | } | |
1468 | ||
8f232108 MB |
1469 | static int on_sigchld(sd_event_source *s, const siginfo_t *si, void *userdata) { |
1470 | Worker *worker = ASSERT_PTR(userdata); | |
1471 | Manager *manager = ASSERT_PTR(worker->manager); | |
1472 | sd_device *dev = worker->event ? ASSERT_PTR(worker->event->dev) : NULL; | |
1473 | EventResult result; | |
e3bff60a | 1474 | |
8f232108 | 1475 | assert(si); |
e3bff60a | 1476 | |
8f232108 MB |
1477 | switch (si->si_code) { |
1478 | case CLD_EXITED: | |
1479 | if (si->si_status == 0) | |
1480 | log_device_debug(dev, "Worker ["PID_FMT"] exited.", si->si_pid); | |
1481 | else | |
1482 | log_device_warning(dev, "Worker ["PID_FMT"] exited with return code %i.", | |
1483 | si->si_pid, si->si_status); | |
1484 | result = EVENT_RESULT_EXIT_STATUS_BASE + si->si_status; | |
1485 | break; | |
6e866b33 | 1486 | |
8f232108 MB |
1487 | case CLD_KILLED: |
1488 | case CLD_DUMPED: | |
1489 | log_device_warning(dev, "Worker ["PID_FMT"] terminated by signal %i (%s).", | |
1490 | si->si_pid, si->si_status, signal_to_string(si->si_status)); | |
1491 | result = EVENT_RESULT_SIGNAL_BASE + si->si_status; | |
1492 | break; | |
6e866b33 | 1493 | |
8f232108 MB |
1494 | default: |
1495 | assert_not_reached(); | |
1496 | } | |
6e866b33 | 1497 | |
8f232108 MB |
1498 | if (result != EVENT_RESULT_SUCCESS && dev) { |
1499 | /* delete state from disk */ | |
1500 | device_delete_db(dev); | |
1501 | device_tag_index(dev, NULL, false); | |
e3bff60a | 1502 | |
8f232108 MB |
1503 | /* Forward kernel event to libudev listeners */ |
1504 | device_broadcast(manager->monitor, dev, result); | |
663996b3 | 1505 | } |
e3bff60a | 1506 | |
8f232108 MB |
1507 | worker_free(worker); |
1508 | ||
86f210e9 MP |
1509 | return 1; |
1510 | } | |
1511 | ||
1512 | static int on_post(sd_event_source *s, void *userdata) { | |
086111aa | 1513 | Manager *manager = ASSERT_PTR(userdata); |
86f210e9 | 1514 | |
086111aa | 1515 | if (manager->events) { |
f5caa8fa MB |
1516 | /* Try to process pending events if idle workers exist. Why is this necessary? |
1517 | * When a worker finished an event and became idle, even if there was a pending event, | |
1518 | * the corresponding device might have been locked and the processing of the event | |
1519 | * delayed for a while, preventing the worker from processing the event immediately. | |
1520 | * Now, the device may be unlocked. Let's try again! */ | |
1521 | event_queue_start(manager); | |
6e866b33 | 1522 | return 1; |
f5caa8fa MB |
1523 | } |
1524 | ||
1525 | /* There are no queued events. Let's remove /run/udev/queue and clean up the idle processes. */ | |
6e866b33 | 1526 | |
f5caa8fa MB |
1527 | if (unlink("/run/udev/queue") < 0) { |
1528 | if (errno != ENOENT) | |
1529 | log_warning_errno(errno, "Failed to unlink /run/udev/queue, ignoring: %m"); | |
1530 | } else | |
1531 | log_debug("No events are queued, removing /run/udev/queue."); | |
6e866b33 MB |
1532 | |
1533 | if (!hashmap_isempty(manager->workers)) { | |
1534 | /* There are idle workers */ | |
086111aa LB |
1535 | (void) event_reset_time_relative(manager->event, &manager->kill_workers_event, |
1536 | CLOCK_MONOTONIC, 3 * USEC_PER_SEC, USEC_PER_SEC, | |
1537 | on_kill_workers_event, manager, | |
1538 | 0, "kill-workers-event", false); | |
6e866b33 | 1539 | return 1; |
86f210e9 MP |
1540 | } |
1541 | ||
6e866b33 MB |
1542 | /* There are no idle workers. */ |
1543 | ||
086111aa LB |
1544 | if (manager->udev_node_needs_cleanup) { |
1545 | (void) udev_node_cleanup(); | |
1546 | manager->udev_node_needs_cleanup = false; | |
1547 | } | |
1548 | ||
6e866b33 MB |
1549 | if (manager->exit) |
1550 | return sd_event_exit(manager->event, 0); | |
1551 | ||
1552 | if (manager->cgroup) | |
1553 | /* cleanup possible left-over processes in our cgroup */ | |
1554 | (void) cg_kill(SYSTEMD_CGROUP_CONTROLLER, manager->cgroup, SIGKILL, CGROUP_IGNORE_SELF, NULL, NULL, NULL); | |
1555 | ||
e3bff60a | 1556 | return 1; |
663996b3 MS |
1557 | } |
1558 | ||
6e866b33 | 1559 | static int listen_fds(int *ret_ctrl, int *ret_netlink) { |
86f210e9 | 1560 | int ctrl_fd = -1, netlink_fd = -1; |
6e866b33 | 1561 | int fd, n; |
86f210e9 | 1562 | |
6e866b33 MB |
1563 | assert(ret_ctrl); |
1564 | assert(ret_netlink); | |
663996b3 MS |
1565 | |
1566 | n = sd_listen_fds(true); | |
86f210e9 MP |
1567 | if (n < 0) |
1568 | return n; | |
663996b3 MS |
1569 | |
1570 | for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) { | |
aff6a67f | 1571 | if (sd_is_socket(fd, AF_UNIX, SOCK_SEQPACKET, -1) > 0) { |
86f210e9 MP |
1572 | if (ctrl_fd >= 0) |
1573 | return -EINVAL; | |
1574 | ctrl_fd = fd; | |
663996b3 MS |
1575 | continue; |
1576 | } | |
1577 | ||
6e866b33 | 1578 | if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) { |
86f210e9 MP |
1579 | if (netlink_fd >= 0) |
1580 | return -EINVAL; | |
1581 | netlink_fd = fd; | |
663996b3 MS |
1582 | continue; |
1583 | } | |
1584 | ||
86f210e9 MP |
1585 | return -EINVAL; |
1586 | } | |
1587 | ||
6e866b33 MB |
1588 | *ret_ctrl = ctrl_fd; |
1589 | *ret_netlink = netlink_fd; | |
663996b3 | 1590 | |
663996b3 MS |
1591 | return 0; |
1592 | } | |
1593 | ||
1594 | /* | |
f47781d8 | 1595 | * read the kernel command line, in case we need to get into debug mode |
a032b68d | 1596 | * udev.log_level=<level> syslog priority |
2897b343 MP |
1597 | * udev.children_max=<number of workers> events are fully serialized if set to 1 |
1598 | * udev.exec_delay=<number of seconds> delay execution of every executed program | |
1599 | * udev.event_timeout=<number of seconds> seconds to wait before terminating an event | |
a10f5d05 | 1600 | * udev.blockdev_read_only<=bool> mark all block devices read-only when they appear |
663996b3 | 1601 | */ |
8a584da2 | 1602 | static int parse_proc_cmdline_item(const char *key, const char *value, void *data) { |
a10f5d05 | 1603 | int r; |
663996b3 | 1604 | |
e3bff60a | 1605 | assert(key); |
663996b3 | 1606 | |
a032b68d MB |
1607 | if (proc_cmdline_key_streq(key, "udev.log_level") || |
1608 | proc_cmdline_key_streq(key, "udev.log_priority")) { /* kept for backward compatibility */ | |
2897b343 MP |
1609 | |
1610 | if (proc_cmdline_value_missing(key, value)) | |
1611 | return 0; | |
1612 | ||
6e866b33 | 1613 | r = log_level_from_string(value); |
8a584da2 MP |
1614 | if (r >= 0) |
1615 | log_set_max_level(r); | |
2897b343 MP |
1616 | |
1617 | } else if (proc_cmdline_key_streq(key, "udev.event_timeout")) { | |
1618 | ||
1619 | if (proc_cmdline_value_missing(key, value)) | |
1620 | return 0; | |
1621 | ||
6e866b33 | 1622 | r = parse_sec(value, &arg_event_timeout_usec); |
2897b343 MP |
1623 | |
1624 | } else if (proc_cmdline_key_streq(key, "udev.children_max")) { | |
1625 | ||
1626 | if (proc_cmdline_value_missing(key, value)) | |
1627 | return 0; | |
1628 | ||
e3bff60a | 1629 | r = safe_atou(value, &arg_children_max); |
2897b343 MP |
1630 | |
1631 | } else if (proc_cmdline_key_streq(key, "udev.exec_delay")) { | |
1632 | ||
1633 | if (proc_cmdline_value_missing(key, value)) | |
1634 | return 0; | |
1635 | ||
6e866b33 | 1636 | r = parse_sec(value, &arg_exec_delay_usec); |
2897b343 | 1637 | |
a10f5d05 MB |
1638 | } else if (proc_cmdline_key_streq(key, "udev.timeout_signal")) { |
1639 | ||
1640 | if (proc_cmdline_value_missing(key, value)) | |
1641 | return 0; | |
1642 | ||
1643 | r = signal_from_string(value); | |
1644 | if (r > 0) | |
1645 | arg_timeout_signal = r; | |
1646 | ||
1647 | } else if (proc_cmdline_key_streq(key, "udev.blockdev_read_only")) { | |
1648 | ||
1649 | if (!value) | |
1650 | arg_blockdev_read_only = true; | |
1651 | else { | |
1652 | r = parse_boolean(value); | |
1653 | if (r < 0) | |
1654 | log_warning_errno(r, "Failed to parse udev.blockdev-read-only argument, ignoring: %s", value); | |
1655 | else | |
1656 | arg_blockdev_read_only = r; | |
1657 | } | |
1658 | ||
1659 | if (arg_blockdev_read_only) | |
1660 | log_notice("All physical block devices will be marked read-only."); | |
1661 | ||
1662 | return 0; | |
1663 | ||
1664 | } else { | |
1665 | if (startswith(key, "udev.")) | |
1666 | log_warning("Unknown udev kernel command line option \"%s\", ignoring.", key); | |
1667 | ||
1668 | return 0; | |
1669 | } | |
e3bff60a | 1670 | |
8a584da2 MP |
1671 | if (r < 0) |
1672 | log_warning_errno(r, "Failed to parse \"%s=%s\", ignoring: %m", key, value); | |
2897b343 | 1673 | |
e3bff60a | 1674 | return 0; |
663996b3 MS |
1675 | } |
1676 | ||
6e866b33 MB |
1677 | static int help(void) { |
1678 | _cleanup_free_ char *link = NULL; | |
1679 | int r; | |
1680 | ||
1681 | r = terminal_urlify_man("systemd-udevd.service", "8", &link); | |
1682 | if (r < 0) | |
1683 | return log_oom(); | |
1684 | ||
5eef597e | 1685 | printf("%s [OPTIONS...]\n\n" |
a10f5d05 | 1686 | "Rule-based manager for device events and files.\n\n" |
e735f4d4 | 1687 | " -h --help Print this message\n" |
52ad194e MB |
1688 | " -V --version Print version of the program\n" |
1689 | " -d --daemon Detach and run in the background\n" | |
1690 | " -D --debug Enable debug output\n" | |
1691 | " -c --children-max=INT Set maximum number of workers\n" | |
1692 | " -e --exec-delay=SECONDS Seconds to wait before executing RUN=\n" | |
1693 | " -t --event-timeout=SECONDS Seconds to wait before terminating an event\n" | |
1694 | " -N --resolve-names=early|late|never\n" | |
e735f4d4 | 1695 | " When to resolve users and groups\n" |
3a6ce677 BR |
1696 | "\nSee the %s for details.\n", |
1697 | program_invocation_short_name, | |
1698 | link); | |
6e866b33 MB |
1699 | |
1700 | return 0; | |
5eef597e MP |
1701 | } |
1702 | ||
1703 | static int parse_argv(int argc, char *argv[]) { | |
a10f5d05 MB |
1704 | enum { |
1705 | ARG_TIMEOUT_SIGNAL, | |
1706 | }; | |
1707 | ||
663996b3 | 1708 | static const struct option options[] = { |
a10f5d05 MB |
1709 | { "daemon", no_argument, NULL, 'd' }, |
1710 | { "debug", no_argument, NULL, 'D' }, | |
1711 | { "children-max", required_argument, NULL, 'c' }, | |
1712 | { "exec-delay", required_argument, NULL, 'e' }, | |
1713 | { "event-timeout", required_argument, NULL, 't' }, | |
1714 | { "resolve-names", required_argument, NULL, 'N' }, | |
1715 | { "help", no_argument, NULL, 'h' }, | |
1716 | { "version", no_argument, NULL, 'V' }, | |
1717 | { "timeout-signal", required_argument, NULL, ARG_TIMEOUT_SIGNAL }, | |
663996b3 MS |
1718 | {} |
1719 | }; | |
663996b3 | 1720 | |
6e866b33 | 1721 | int c, r; |
663996b3 | 1722 | |
5eef597e MP |
1723 | assert(argc >= 0); |
1724 | assert(argv); | |
14228c0d | 1725 | |
7035cd9e | 1726 | while ((c = getopt_long(argc, argv, "c:de:Dt:N:hV", options, NULL)) >= 0) { |
5eef597e | 1727 | switch (c) { |
663996b3 | 1728 | |
663996b3 | 1729 | case 'd': |
5eef597e | 1730 | arg_daemonize = true; |
663996b3 MS |
1731 | break; |
1732 | case 'c': | |
e3bff60a | 1733 | r = safe_atou(optarg, &arg_children_max); |
5eef597e | 1734 | if (r < 0) |
6e866b33 | 1735 | log_warning_errno(r, "Failed to parse --children-max= value '%s', ignoring: %m", optarg); |
663996b3 MS |
1736 | break; |
1737 | case 'e': | |
6e866b33 | 1738 | r = parse_sec(optarg, &arg_exec_delay_usec); |
5eef597e | 1739 | if (r < 0) |
6e866b33 | 1740 | log_warning_errno(r, "Failed to parse --exec-delay= value '%s', ignoring: %m", optarg); |
5eef597e | 1741 | break; |
a10f5d05 MB |
1742 | case ARG_TIMEOUT_SIGNAL: |
1743 | r = signal_from_string(optarg); | |
1744 | if (r <= 0) | |
1745 | log_warning_errno(r, "Failed to parse --timeout-signal= value '%s', ignoring: %m", optarg); | |
1746 | else | |
1747 | arg_timeout_signal = r; | |
1748 | ||
1749 | break; | |
5eef597e | 1750 | case 't': |
6e866b33 | 1751 | r = parse_sec(optarg, &arg_event_timeout_usec); |
5eef597e | 1752 | if (r < 0) |
6e866b33 | 1753 | log_warning_errno(r, "Failed to parse --event-timeout= value '%s', ignoring: %m", optarg); |
663996b3 MS |
1754 | break; |
1755 | case 'D': | |
5eef597e | 1756 | arg_debug = true; |
663996b3 | 1757 | break; |
6e866b33 MB |
1758 | case 'N': { |
1759 | ResolveNameTiming t; | |
1760 | ||
1761 | t = resolve_name_timing_from_string(optarg); | |
1762 | if (t < 0) | |
1763 | log_warning("Invalid --resolve-names= value '%s', ignoring.", optarg); | |
1764 | else | |
1765 | arg_resolve_name_timing = t; | |
663996b3 | 1766 | break; |
6e866b33 | 1767 | } |
663996b3 | 1768 | case 'h': |
6e866b33 | 1769 | return help(); |
663996b3 | 1770 | case 'V': |
7c20daf6 | 1771 | printf("%s\n", GIT_VERSION); |
5eef597e MP |
1772 | return 0; |
1773 | case '?': | |
1774 | return -EINVAL; | |
663996b3 | 1775 | default: |
ea0999c9 | 1776 | assert_not_reached(); |
5eef597e | 1777 | |
663996b3 MS |
1778 | } |
1779 | } | |
1780 | ||
5eef597e MP |
1781 | return 1; |
1782 | } | |
1783 | ||
f5caa8fa MB |
1784 | static int create_subcgroup(char **ret) { |
1785 | _cleanup_free_ char *cgroup = NULL, *subcgroup = NULL; | |
1786 | int r; | |
1787 | ||
1788 | if (getppid() != 1) | |
1789 | return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not invoked by PID1."); | |
1790 | ||
1791 | r = sd_booted(); | |
1792 | if (r < 0) | |
1793 | return log_debug_errno(r, "Failed to check if systemd is running: %m"); | |
1794 | if (r == 0) | |
1795 | return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "systemd is not running."); | |
1796 | ||
1797 | /* Get our own cgroup, we regularly kill everything udev has left behind. | |
1798 | * We only do this on systemd systems, and only if we are directly spawned | |
1799 | * by PID1. Otherwise we are not guaranteed to have a dedicated cgroup. */ | |
1800 | ||
1801 | r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup); | |
1802 | if (r < 0) { | |
1803 | if (IN_SET(r, -ENOENT, -ENOMEDIUM)) | |
1804 | return log_debug_errno(r, "Dedicated cgroup not found: %m"); | |
1805 | return log_debug_errno(r, "Failed to get cgroup: %m"); | |
1806 | } | |
1807 | ||
1808 | r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, cgroup, "trusted.delegate"); | |
086111aa | 1809 | if (r == 0 || (r < 0 && ERRNO_IS_XATTR_ABSENT(r))) |
f5caa8fa MB |
1810 | return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "The cgroup %s is not delegated to us.", cgroup); |
1811 | if (r < 0) | |
1812 | return log_debug_errno(r, "Failed to read trusted.delegate attribute: %m"); | |
1813 | ||
1814 | /* We are invoked with our own delegated cgroup tree, let's move us one level down, so that we | |
1815 | * don't collide with the "no processes in inner nodes" rule of cgroups, when the service | |
1816 | * manager invokes the ExecReload= job in the .control/ subcgroup. */ | |
1817 | ||
1818 | subcgroup = path_join(cgroup, "/udev"); | |
1819 | if (!subcgroup) | |
1820 | return log_oom_debug(); | |
1821 | ||
1822 | r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, subcgroup, 0); | |
1823 | if (r < 0) | |
1824 | return log_debug_errno(r, "Failed to create %s subcgroup: %m", subcgroup); | |
1825 | ||
1826 | log_debug("Created %s subcgroup.", subcgroup); | |
1827 | if (ret) | |
1828 | *ret = TAKE_PTR(subcgroup); | |
1829 | return 0; | |
1830 | } | |
1831 | ||
1832 | static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent) { | |
e3bff60a | 1833 | _cleanup_(manager_freep) Manager *manager = NULL; |
f5caa8fa | 1834 | _cleanup_free_ char *cgroup = NULL; |
7c20daf6 | 1835 | int r; |
5eef597e | 1836 | |
e3bff60a MP |
1837 | assert(ret); |
1838 | ||
f5caa8fa MB |
1839 | (void) create_subcgroup(&cgroup); |
1840 | ||
6e866b33 | 1841 | manager = new(Manager, 1); |
e3bff60a MP |
1842 | if (!manager) |
1843 | return log_oom(); | |
1844 | ||
6e866b33 | 1845 | *manager = (Manager) { |
8b3d4ff0 | 1846 | .inotify_fd = -1, |
6e866b33 | 1847 | .worker_watch = { -1, -1 }, |
f5caa8fa | 1848 | .cgroup = TAKE_PTR(cgroup), |
6e866b33 | 1849 | }; |
e3bff60a | 1850 | |
bb4f798a MB |
1851 | r = udev_ctrl_new_from_fd(&manager->ctrl, fd_ctrl); |
1852 | if (r < 0) | |
1853 | return log_error_errno(r, "Failed to initialize udev control socket: %m"); | |
e3bff60a | 1854 | |
7c20daf6 FS |
1855 | r = udev_ctrl_enable_receiving(manager->ctrl); |
1856 | if (r < 0) | |
1857 | return log_error_errno(r, "Failed to bind udev control socket: %m"); | |
e3bff60a | 1858 | |
6e866b33 MB |
1859 | r = device_monitor_new_full(&manager->monitor, MONITOR_GROUP_KERNEL, fd_uevent); |
1860 | if (r < 0) | |
1861 | return log_error_errno(r, "Failed to initialize device monitor: %m"); | |
1862 | ||
46cdbd49 BR |
1863 | /* Bump receiver buffer, but only if we are not called via socket activation, as in that |
1864 | * case systemd sets the receive buffer size for us, and the value in the .socket unit | |
1865 | * should take full effect. */ | |
7de6915e MB |
1866 | if (fd_uevent < 0) { |
1867 | r = sd_device_monitor_set_receive_buffer_size(manager->monitor, 128 * 1024 * 1024); | |
1868 | if (r < 0) | |
1869 | log_warning_errno(r, "Failed to set receive buffer size for device monitor, ignoring: %m"); | |
1870 | } | |
e3bff60a | 1871 | |
086111aa LB |
1872 | (void) sd_device_monitor_set_description(manager->monitor, "manager"); |
1873 | ||
7c20daf6 FS |
1874 | r = device_monitor_enable_receiving(manager->monitor); |
1875 | if (r < 0) | |
1876 | return log_error_errno(r, "Failed to bind netlink socket: %m"); | |
1877 | ||
3a6ce677 BR |
1878 | manager->log_level = log_get_max_level(); |
1879 | ||
7c20daf6 FS |
1880 | *ret = TAKE_PTR(manager); |
1881 | ||
1882 | return 0; | |
1883 | } | |
1884 | ||
1885 | static int main_loop(Manager *manager) { | |
bb4f798a MB |
1886 | int fd_worker, r; |
1887 | ||
1888 | manager->pid = getpid_cached(); | |
7c20daf6 | 1889 | |
e3bff60a | 1890 | /* unnamed socket from workers to the main daemon */ |
aff6a67f | 1891 | r = socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, manager->worker_watch); |
e3bff60a | 1892 | if (r < 0) |
6e866b33 | 1893 | return log_error_errno(errno, "Failed to create socketpair for communicating with workers: %m"); |
e3bff60a | 1894 | |
86f210e9 | 1895 | fd_worker = manager->worker_watch[READ_END]; |
e3bff60a | 1896 | |
6e866b33 | 1897 | r = setsockopt_int(fd_worker, SOL_SOCKET, SO_PASSCRED, true); |
e3bff60a | 1898 | if (r < 0) |
6e866b33 | 1899 | return log_error_errno(r, "Failed to enable SO_PASSCRED: %m"); |
e3bff60a | 1900 | |
8b3d4ff0 MB |
1901 | manager->inotify_fd = inotify_init1(IN_CLOEXEC); |
1902 | if (manager->inotify_fd < 0) | |
1903 | return log_error_errno(errno, "Failed to create inotify descriptor: %m"); | |
e3bff60a | 1904 | |
8b3d4ff0 | 1905 | udev_watch_restore(manager->inotify_fd); |
e3bff60a MP |
1906 | |
1907 | /* block and listen to all signals on signalfd */ | |
86f210e9 MP |
1908 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0); |
1909 | ||
1910 | r = sd_event_default(&manager->event); | |
1911 | if (r < 0) | |
6e866b33 | 1912 | return log_error_errno(r, "Failed to allocate event loop: %m"); |
86f210e9 MP |
1913 | |
1914 | r = sd_event_add_signal(manager->event, NULL, SIGINT, on_sigterm, manager); | |
1915 | if (r < 0) | |
6e866b33 | 1916 | return log_error_errno(r, "Failed to create SIGINT event source: %m"); |
86f210e9 MP |
1917 | |
1918 | r = sd_event_add_signal(manager->event, NULL, SIGTERM, on_sigterm, manager); | |
1919 | if (r < 0) | |
6e866b33 | 1920 | return log_error_errno(r, "Failed to create SIGTERM event source: %m"); |
86f210e9 MP |
1921 | |
1922 | r = sd_event_add_signal(manager->event, NULL, SIGHUP, on_sighup, manager); | |
1923 | if (r < 0) | |
6e866b33 | 1924 | return log_error_errno(r, "Failed to create SIGHUP event source: %m"); |
86f210e9 | 1925 | |
86f210e9 MP |
1926 | r = sd_event_set_watchdog(manager->event, true); |
1927 | if (r < 0) | |
6e866b33 | 1928 | return log_error_errno(r, "Failed to create watchdog event source: %m"); |
86f210e9 | 1929 | |
bb4f798a MB |
1930 | r = udev_ctrl_attach_event(manager->ctrl, manager->event); |
1931 | if (r < 0) | |
1932 | return log_error_errno(r, "Failed to attach event to udev control: %m"); | |
7c20daf6 | 1933 | |
bb4f798a | 1934 | r = udev_ctrl_start(manager->ctrl, on_ctrl_msg, manager); |
86f210e9 | 1935 | if (r < 0) |
bb4f798a | 1936 | return log_error_errno(r, "Failed to start device monitor: %m"); |
86f210e9 MP |
1937 | |
1938 | /* This needs to be after the inotify and uevent handling, to make sure | |
1939 | * that the ping is send back after fully processing the pending uevents | |
1940 | * (including the synthetic ones we may create due to inotify events). | |
1941 | */ | |
bb4f798a | 1942 | r = sd_event_source_set_priority(udev_ctrl_get_event_source(manager->ctrl), SD_EVENT_PRIORITY_IDLE); |
86f210e9 | 1943 | if (r < 0) |
6e866b33 | 1944 | return log_error_errno(r, "Failed to set IDLE event priority for udev control event source: %m"); |
86f210e9 | 1945 | |
8b3d4ff0 | 1946 | r = sd_event_add_io(manager->event, &manager->inotify_event, manager->inotify_fd, EPOLLIN, on_inotify, manager); |
86f210e9 | 1947 | if (r < 0) |
6e866b33 | 1948 | return log_error_errno(r, "Failed to create inotify event source: %m"); |
86f210e9 | 1949 | |
6e866b33 | 1950 | r = sd_device_monitor_attach_event(manager->monitor, manager->event); |
86f210e9 | 1951 | if (r < 0) |
6e866b33 MB |
1952 | return log_error_errno(r, "Failed to attach event to device monitor: %m"); |
1953 | ||
1954 | r = sd_device_monitor_start(manager->monitor, on_uevent, manager); | |
1955 | if (r < 0) | |
1956 | return log_error_errno(r, "Failed to start device monitor: %m"); | |
1957 | ||
86f210e9 MP |
1958 | r = sd_event_add_io(manager->event, NULL, fd_worker, EPOLLIN, on_worker, manager); |
1959 | if (r < 0) | |
6e866b33 | 1960 | return log_error_errno(r, "Failed to create worker event source: %m"); |
86f210e9 MP |
1961 | |
1962 | r = sd_event_add_post(manager->event, NULL, on_post, manager); | |
1963 | if (r < 0) | |
6e866b33 | 1964 | return log_error_errno(r, "Failed to create post event source: %m"); |
e3bff60a | 1965 | |
086111aa LB |
1966 | manager->last_usec = now(CLOCK_MONOTONIC); |
1967 | ||
7c20daf6 | 1968 | udev_builtin_init(); |
fb183854 | 1969 | |
a10f5d05 | 1970 | r = udev_rules_load(&manager->rules, arg_resolve_name_timing); |
086111aa | 1971 | if (r < 0) |
7c20daf6 | 1972 | return log_error_errno(r, "Failed to read udev rules: %m"); |
fb183854 MP |
1973 | |
1974 | r = udev_rules_apply_static_dev_perms(manager->rules); | |
1975 | if (r < 0) | |
9a96df7a | 1976 | log_warning_errno(r, "Failed to apply permissions on static device nodes, ignoring: %m"); |
fb183854 | 1977 | |
ea0999c9 | 1978 | notify_ready(); |
fb183854 MP |
1979 | |
1980 | r = sd_event_loop(manager->event); | |
7c20daf6 | 1981 | if (r < 0) |
6e866b33 | 1982 | log_error_errno(r, "Event loop failed: %m"); |
fb183854 | 1983 | |
fb183854 MP |
1984 | sd_notify(false, |
1985 | "STOPPING=1\n" | |
1986 | "STATUS=Shutting down..."); | |
fb183854 MP |
1987 | return r; |
1988 | } | |
1989 | ||
a10f5d05 | 1990 | int run_udevd(int argc, char *argv[]) { |
7c20daf6 | 1991 | _cleanup_(manager_freep) Manager *manager = NULL; |
4c89c718 MP |
1992 | int fd_ctrl = -1, fd_uevent = -1; |
1993 | int r; | |
5eef597e MP |
1994 | |
1995 | log_set_target(LOG_TARGET_AUTO); | |
f2dec872 | 1996 | log_open(); |
a10f5d05 | 1997 | udev_parse_config_full(&arg_children_max, &arg_exec_delay_usec, &arg_event_timeout_usec, &arg_resolve_name_timing, &arg_timeout_signal); |
5eef597e | 1998 | log_parse_environment(); |
f2dec872 | 1999 | log_open(); /* Done again to update after reading configuration. */ |
5eef597e | 2000 | |
5eef597e MP |
2001 | r = parse_argv(argc, argv); |
2002 | if (r <= 0) | |
6e866b33 | 2003 | return r; |
5eef597e | 2004 | |
2897b343 | 2005 | r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); |
e3bff60a | 2006 | if (r < 0) |
6e866b33 | 2007 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
663996b3 | 2008 | |
fb183854 MP |
2009 | if (arg_debug) { |
2010 | log_set_target(LOG_TARGET_CONSOLE); | |
5eef597e | 2011 | log_set_max_level(LOG_DEBUG); |
fb183854 | 2012 | } |
5eef597e | 2013 | |
52ad194e MB |
2014 | r = must_be_root(); |
2015 | if (r < 0) | |
6e866b33 | 2016 | return r; |
5eef597e | 2017 | |
e3bff60a | 2018 | if (arg_children_max == 0) { |
a10f5d05 | 2019 | unsigned long cpu_limit, mem_limit, cpu_count = 1; |
663996b3 | 2020 | |
a10f5d05 MB |
2021 | r = cpus_in_affinity_mask(); |
2022 | if (r < 0) | |
2023 | log_warning_errno(r, "Failed to determine number of local CPUs, ignoring: %m"); | |
2024 | else | |
2025 | cpu_count = r; | |
f2dec872 BR |
2026 | |
2027 | cpu_limit = cpu_count * 2 + 16; | |
2028 | mem_limit = MAX(physical_memory() / (128UL*1024*1024), 10U); | |
f47781d8 | 2029 | |
f2dec872 BR |
2030 | arg_children_max = MIN(cpu_limit, mem_limit); |
2031 | arg_children_max = MIN(WORKER_NUM_MAX, arg_children_max); | |
b012e921 | 2032 | |
6e866b33 | 2033 | log_debug("Set children_max to %u", arg_children_max); |
5eef597e | 2034 | } |
663996b3 | 2035 | |
e3bff60a | 2036 | /* set umask before creating any file/directory */ |
e3bff60a MP |
2037 | umask(022); |
2038 | ||
aa27b158 | 2039 | r = mac_selinux_init(); |
6e866b33 | 2040 | if (r < 0) |
a10f5d05 | 2041 | return r; |
663996b3 | 2042 | |
ea0999c9 | 2043 | r = RET_NERRNO(mkdir("/run/udev", 0755)); |
6e866b33 MB |
2044 | if (r < 0 && r != -EEXIST) |
2045 | return log_error_errno(r, "Failed to create /run/udev: %m"); | |
663996b3 | 2046 | |
86f210e9 | 2047 | r = listen_fds(&fd_ctrl, &fd_uevent); |
6e866b33 MB |
2048 | if (r < 0) |
2049 | return log_error_errno(r, "Failed to listen on fds: %m"); | |
5eef597e | 2050 | |
f5caa8fa | 2051 | r = manager_new(&manager, fd_ctrl, fd_uevent); |
7c20daf6 FS |
2052 | if (r < 0) |
2053 | return log_error_errno(r, "Failed to create manager: %m"); | |
2054 | ||
5eef597e | 2055 | if (arg_daemonize) { |
663996b3 MS |
2056 | pid_t pid; |
2057 | ||
086111aa | 2058 | log_info("Starting systemd-udevd version " GIT_VERSION); |
86f210e9 MP |
2059 | |
2060 | /* connect /dev/null to stdin, stdout, stderr */ | |
8a584da2 MP |
2061 | if (log_get_max_level() < LOG_DEBUG) { |
2062 | r = make_null_stdio(); | |
2063 | if (r < 0) | |
2064 | log_warning_errno(r, "Failed to redirect standard streams to /dev/null: %m"); | |
2065 | } | |
2066 | ||
663996b3 | 2067 | pid = fork(); |
6e866b33 MB |
2068 | if (pid < 0) |
2069 | return log_error_errno(errno, "Failed to fork daemon: %m"); | |
2070 | if (pid > 0) | |
2071 | /* parent */ | |
2072 | return 0; | |
663996b3 | 2073 | |
6e866b33 MB |
2074 | /* child */ |
2075 | (void) setsid(); | |
86f210e9 | 2076 | } |
663996b3 | 2077 | |
bb4f798a | 2078 | return main_loop(manager); |
663996b3 | 2079 | } |