]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevd.c
Merge pull request #12390 from poettering/string-file-mkdir
[thirdparty/systemd.git] / src / udev / udevd.c
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright © 2004 Chris Friesen <chris_friesen@sympatico.ca>
4 * Copyright © 2009 Canonical Ltd.
5 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
6 */
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <getopt.h>
11 #include <signal.h>
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/epoll.h>
18 #include <sys/file.h>
19 #include <sys/inotify.h>
20 #include <sys/ioctl.h>
21 #include <sys/mount.h>
22 #include <sys/prctl.h>
23 #include <sys/signalfd.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/time.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29
30 #include "sd-daemon.h"
31 #include "sd-event.h"
32
33 #include "alloc-util.h"
34 #include "build.h"
35 #include "cgroup-util.h"
36 #include "cpu-set-util.h"
37 #include "dev-setup.h"
38 #include "device-monitor-private.h"
39 #include "device-private.h"
40 #include "device-util.h"
41 #include "event-util.h"
42 #include "fd-util.h"
43 #include "fileio.h"
44 #include "format-util.h"
45 #include "fs-util.h"
46 #include "hashmap.h"
47 #include "io-util.h"
48 #include "libudev-device-internal.h"
49 #include "limits-util.h"
50 #include "list.h"
51 #include "main-func.h"
52 #include "mkdir.h"
53 #include "netlink-util.h"
54 #include "parse-util.h"
55 #include "pretty-print.h"
56 #include "proc-cmdline.h"
57 #include "process-util.h"
58 #include "selinux-util.h"
59 #include "signal-util.h"
60 #include "socket-util.h"
61 #include "string-util.h"
62 #include "strv.h"
63 #include "strxcpyx.h"
64 #include "syslog-util.h"
65 #include "udev-builtin.h"
66 #include "udev-ctrl.h"
67 #include "udev-util.h"
68 #include "udev-watch.h"
69 #include "udev.h"
70 #include "user-util.h"
71
72 #define WORKER_NUM_MAX 2048U
73
74 static bool arg_debug = false;
75 static int arg_daemonize = false;
76 static ResolveNameTiming arg_resolve_name_timing = RESOLVE_NAME_EARLY;
77 static unsigned arg_children_max = 0;
78 static usec_t arg_exec_delay_usec = 0;
79 static usec_t arg_event_timeout_usec = 180 * USEC_PER_SEC;
80
81 typedef struct Manager {
82 sd_event *event;
83 Hashmap *workers;
84 LIST_HEAD(struct event, events);
85 const char *cgroup;
86 pid_t pid; /* the process that originally allocated the manager object */
87
88 UdevRules *rules;
89 Hashmap *properties;
90
91 sd_netlink *rtnl;
92
93 sd_device_monitor *monitor;
94 struct udev_ctrl *ctrl;
95 int fd_inotify;
96 int worker_watch[2];
97
98 sd_event_source *inotify_event;
99 sd_event_source *kill_workers_event;
100
101 usec_t last_usec;
102
103 bool stop_exec_queue:1;
104 bool exit:1;
105 } Manager;
106
107 enum event_state {
108 EVENT_UNDEF,
109 EVENT_QUEUED,
110 EVENT_RUNNING,
111 };
112
113 struct event {
114 Manager *manager;
115 struct worker *worker;
116 enum event_state state;
117
118 sd_device *dev;
119 sd_device *dev_kernel; /* clone of originally received device */
120
121 uint64_t seqnum;
122 uint64_t delaying_seqnum;
123
124 sd_event_source *timeout_warning_event;
125 sd_event_source *timeout_event;
126
127 LIST_FIELDS(struct event, event);
128 };
129
130 static void event_queue_cleanup(Manager *manager, enum event_state type);
131
132 enum worker_state {
133 WORKER_UNDEF,
134 WORKER_RUNNING,
135 WORKER_IDLE,
136 WORKER_KILLED,
137 };
138
139 struct worker {
140 Manager *manager;
141 pid_t pid;
142 sd_device_monitor *monitor;
143 enum worker_state state;
144 struct event *event;
145 };
146
147 /* passed from worker to main process */
148 struct worker_message {
149 };
150
151 static void event_free(struct event *event) {
152 if (!event)
153 return;
154
155 assert(event->manager);
156
157 LIST_REMOVE(event, event->manager->events, event);
158 sd_device_unref(event->dev);
159 sd_device_unref(event->dev_kernel);
160
161 sd_event_source_unref(event->timeout_warning_event);
162 sd_event_source_unref(event->timeout_event);
163
164 if (event->worker)
165 event->worker->event = NULL;
166
167 /* only clean up the queue from the process that created it */
168 if (LIST_IS_EMPTY(event->manager->events) &&
169 event->manager->pid == getpid_cached())
170 if (unlink("/run/udev/queue") < 0)
171 log_warning_errno(errno, "Failed to unlink /run/udev/queue: %m");
172
173 free(event);
174 }
175
176 static void worker_free(struct worker *worker) {
177 if (!worker)
178 return;
179
180 assert(worker->manager);
181
182 hashmap_remove(worker->manager->workers, PID_TO_PTR(worker->pid));
183 sd_device_monitor_unref(worker->monitor);
184 event_free(worker->event);
185
186 free(worker);
187 }
188
189 DEFINE_TRIVIAL_CLEANUP_FUNC(struct worker *, worker_free);
190 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(worker_hash_op, void, trivial_hash_func, trivial_compare_func, struct worker, worker_free);
191
192 static int worker_new(struct worker **ret, Manager *manager, sd_device_monitor *worker_monitor, pid_t pid) {
193 _cleanup_(worker_freep) struct worker *worker = NULL;
194 int r;
195
196 assert(ret);
197 assert(manager);
198 assert(worker_monitor);
199 assert(pid > 1);
200
201 /* close monitor, but keep address around */
202 device_monitor_disconnect(worker_monitor);
203
204 worker = new(struct worker, 1);
205 if (!worker)
206 return -ENOMEM;
207
208 *worker = (struct worker) {
209 .manager = manager,
210 .monitor = sd_device_monitor_ref(worker_monitor),
211 .pid = pid,
212 };
213
214 r = hashmap_ensure_allocated(&manager->workers, &worker_hash_op);
215 if (r < 0)
216 return r;
217
218 r = hashmap_put(manager->workers, PID_TO_PTR(pid), worker);
219 if (r < 0)
220 return r;
221
222 *ret = TAKE_PTR(worker);
223
224 return 0;
225 }
226
227 static int on_event_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
228 struct event *event = userdata;
229
230 assert(event);
231 assert(event->worker);
232
233 kill_and_sigcont(event->worker->pid, SIGKILL);
234 event->worker->state = WORKER_KILLED;
235
236 log_device_error(event->dev, "Worker ["PID_FMT"] processing SEQNUM=%"PRIu64" killed", event->worker->pid, event->seqnum);
237
238 return 1;
239 }
240
241 static int on_event_timeout_warning(sd_event_source *s, uint64_t usec, void *userdata) {
242 struct event *event = userdata;
243
244 assert(event);
245 assert(event->worker);
246
247 log_device_warning(event->dev, "Worker ["PID_FMT"] processing SEQNUM=%"PRIu64" is taking a long time", event->worker->pid, event->seqnum);
248
249 return 1;
250 }
251
252 static void worker_attach_event(struct worker *worker, struct event *event) {
253 sd_event *e;
254 uint64_t usec;
255
256 assert(worker);
257 assert(worker->manager);
258 assert(event);
259 assert(!event->worker);
260 assert(!worker->event);
261
262 worker->state = WORKER_RUNNING;
263 worker->event = event;
264 event->state = EVENT_RUNNING;
265 event->worker = worker;
266
267 e = worker->manager->event;
268
269 assert_se(sd_event_now(e, CLOCK_MONOTONIC, &usec) >= 0);
270
271 (void) sd_event_add_time(e, &event->timeout_warning_event, CLOCK_MONOTONIC,
272 usec + udev_warn_timeout(arg_event_timeout_usec), USEC_PER_SEC, on_event_timeout_warning, event);
273
274 (void) sd_event_add_time(e, &event->timeout_event, CLOCK_MONOTONIC,
275 usec + arg_event_timeout_usec, USEC_PER_SEC, on_event_timeout, event);
276 }
277
278 static void manager_clear_for_worker(Manager *manager) {
279 assert(manager);
280
281 manager->inotify_event = sd_event_source_unref(manager->inotify_event);
282 manager->kill_workers_event = sd_event_source_unref(manager->kill_workers_event);
283
284 manager->event = sd_event_unref(manager->event);
285
286 manager->workers = hashmap_free(manager->workers);
287 event_queue_cleanup(manager, EVENT_UNDEF);
288
289 manager->monitor = sd_device_monitor_unref(manager->monitor);
290 manager->ctrl = udev_ctrl_unref(manager->ctrl);
291
292 manager->worker_watch[READ_END] = safe_close(manager->worker_watch[READ_END]);
293 }
294
295 static void manager_free(Manager *manager) {
296 if (!manager)
297 return;
298
299 udev_builtin_exit();
300
301 if (manager->pid == getpid_cached())
302 udev_ctrl_cleanup(manager->ctrl);
303
304 manager_clear_for_worker(manager);
305
306 sd_netlink_unref(manager->rtnl);
307
308 hashmap_free_free_free(manager->properties);
309 udev_rules_free(manager->rules);
310
311 safe_close(manager->fd_inotify);
312 safe_close_pair(manager->worker_watch);
313
314 free(manager);
315 }
316
317 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
318
319 static int worker_send_message(int fd) {
320 struct worker_message message = {};
321
322 return loop_write(fd, &message, sizeof(message), false);
323 }
324
325 static int worker_lock_block_device(sd_device *dev, int *ret_fd) {
326 _cleanup_close_ int fd = -1;
327 const char *val;
328 int r;
329
330 assert(dev);
331 assert(ret_fd);
332
333 /*
334 * Take a shared lock on the device node; this establishes
335 * a concept of device "ownership" to serialize device
336 * access. External processes holding an exclusive lock will
337 * cause udev to skip the event handling; in the case udev
338 * acquired the lock, the external process can block until
339 * udev has finished its event handling.
340 */
341
342 if (device_for_action(dev, DEVICE_ACTION_REMOVE))
343 return 0;
344
345 r = sd_device_get_subsystem(dev, &val);
346 if (r < 0)
347 return log_device_debug_errno(dev, r, "Failed to get subsystem: %m");
348
349 if (!streq(val, "block"))
350 return 0;
351
352 r = sd_device_get_sysname(dev, &val);
353 if (r < 0)
354 return log_device_debug_errno(dev, r, "Failed to get sysname: %m");
355
356 if (STARTSWITH_SET(val, "dm-", "md", "drbd"))
357 return 0;
358
359 r = sd_device_get_devtype(dev, &val);
360 if (r < 0 && r != -ENOENT)
361 return log_device_debug_errno(dev, r, "Failed to get devtype: %m");
362 if (r >= 0 && streq(val, "partition")) {
363 r = sd_device_get_parent(dev, &dev);
364 if (r < 0)
365 return log_device_debug_errno(dev, r, "Failed to get parent device: %m");
366 }
367
368 r = sd_device_get_devname(dev, &val);
369 if (r == -ENOENT)
370 return 0;
371 if (r < 0)
372 return log_device_debug_errno(dev, r, "Failed to get devname: %m");
373
374 fd = open(val, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
375 if (fd < 0) {
376 log_device_debug_errno(dev, errno, "Failed to open '%s', ignoring: %m", val);
377 return 0;
378 }
379
380 if (flock(fd, LOCK_SH|LOCK_NB) < 0)
381 return log_device_debug_errno(dev, errno, "Failed to flock(%s): %m", val);
382
383 *ret_fd = TAKE_FD(fd);
384 return 1;
385 }
386
387 static int worker_process_device(Manager *manager, sd_device *dev) {
388 _cleanup_(udev_event_freep) UdevEvent *udev_event = NULL;
389 _cleanup_close_ int fd_lock = -1;
390 DeviceAction action;
391 uint64_t seqnum;
392 int r;
393
394 assert(manager);
395 assert(dev);
396
397 r = device_get_seqnum(dev, &seqnum);
398 if (r < 0)
399 return log_device_debug_errno(dev, r, "Failed to get SEQNUM: %m");
400
401 r = device_get_action(dev, &action);
402 if (r < 0)
403 return log_device_debug_errno(dev, r, "Failed to get ACTION: %m");
404
405 log_device_debug(dev, "Processing device (SEQNUM=%"PRIu64", ACTION=%s)",
406 seqnum, device_action_to_string(action));
407
408 udev_event = udev_event_new(dev, arg_exec_delay_usec, manager->rtnl);
409 if (!udev_event)
410 return -ENOMEM;
411
412 r = worker_lock_block_device(dev, &fd_lock);
413 if (r < 0)
414 return r;
415
416 /* apply rules, create node, symlinks */
417 udev_event_execute_rules(udev_event, arg_event_timeout_usec, manager->properties, manager->rules);
418 udev_event_execute_run(udev_event, arg_event_timeout_usec);
419
420 if (!manager->rtnl)
421 /* in case rtnl was initialized */
422 manager->rtnl = sd_netlink_ref(udev_event->rtnl);
423
424 /* apply/restore inotify watch */
425 if (udev_event->inotify_watch) {
426 (void) udev_watch_begin(dev);
427 r = device_update_db(dev);
428 if (r < 0)
429 return log_device_debug_errno(dev, r, "Failed to update database under /run/udev/data/: %m");
430 }
431
432 log_device_debug(dev, "Device (SEQNUM=%"PRIu64", ACTION=%s) processed",
433 seqnum, device_action_to_string(action));
434
435 return 0;
436 }
437
438 static int worker_device_monitor_handler(sd_device_monitor *monitor, sd_device *dev, void *userdata) {
439 Manager *manager = userdata;
440 int r;
441
442 assert(dev);
443 assert(manager);
444
445 r = worker_process_device(manager, dev);
446 if (r < 0)
447 log_device_warning_errno(dev, r, "Failed to process device, ignoring: %m");
448
449 /* send processed event back to libudev listeners */
450 r = device_monitor_send_device(monitor, NULL, dev);
451 if (r < 0)
452 log_device_warning_errno(dev, r, "Failed to send device, ignoring: %m");
453
454 /* send udevd the result of the event execution */
455 r = worker_send_message(manager->worker_watch[WRITE_END]);
456 if (r < 0)
457 log_device_warning_errno(dev, r, "Failed to send signal to main daemon, ignoring: %m");
458
459 return 1;
460 }
461
462 static int worker_main(Manager *_manager, sd_device_monitor *monitor, sd_device *first_device) {
463 _cleanup_(sd_device_unrefp) sd_device *dev = first_device;
464 _cleanup_(manager_freep) Manager *manager = _manager;
465 int r;
466
467 assert(manager);
468 assert(monitor);
469 assert(dev);
470
471 unsetenv("NOTIFY_SOCKET");
472
473 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, -1) >= 0);
474
475 /* Reset OOM score, we only protect the main daemon. */
476 r = set_oom_score_adjust(0);
477 if (r < 0)
478 log_debug_errno(r, "Failed to reset OOM score, ignoring: %m");
479
480 /* Clear unnecessary data in Manager object.*/
481 manager_clear_for_worker(manager);
482
483 r = sd_event_new(&manager->event);
484 if (r < 0)
485 return log_error_errno(r, "Failed to allocate event loop: %m");
486
487 r = sd_event_add_signal(manager->event, NULL, SIGTERM, NULL, NULL);
488 if (r < 0)
489 return log_error_errno(r, "Failed to set SIGTERM event: %m");
490
491 r = sd_device_monitor_attach_event(monitor, manager->event);
492 if (r < 0)
493 return log_error_errno(r, "Failed to attach event loop to device monitor: %m");
494
495 r = sd_device_monitor_start(monitor, worker_device_monitor_handler, manager);
496 if (r < 0)
497 return log_error_errno(r, "Failed to start device monitor: %m");
498
499 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(monitor), "worker-device-monitor");
500
501 /* Process first device */
502 (void) worker_device_monitor_handler(monitor, dev, manager);
503
504 r = sd_event_loop(manager->event);
505 if (r < 0)
506 return log_error_errno(r, "Event loop failed: %m");
507
508 return 0;
509 }
510
511 static int worker_spawn(Manager *manager, struct event *event) {
512 _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *worker_monitor = NULL;
513 struct worker *worker;
514 pid_t pid;
515 int r;
516
517 /* listen for new events */
518 r = device_monitor_new_full(&worker_monitor, MONITOR_GROUP_NONE, -1);
519 if (r < 0)
520 return r;
521
522 /* allow the main daemon netlink address to send devices to the worker */
523 r = device_monitor_allow_unicast_sender(worker_monitor, manager->monitor);
524 if (r < 0)
525 return log_error_errno(r, "Worker: Failed to set unicast sender: %m");
526
527 r = device_monitor_enable_receiving(worker_monitor);
528 if (r < 0)
529 return log_error_errno(r, "Worker: Failed to enable receiving of device: %m");
530
531 r = safe_fork(NULL, FORK_DEATHSIG, &pid);
532 if (r < 0) {
533 event->state = EVENT_QUEUED;
534 return log_error_errno(r, "Failed to fork() worker: %m");
535 }
536 if (r == 0) {
537 /* Worker process */
538 r = worker_main(manager, worker_monitor, sd_device_ref(event->dev));
539 log_close();
540 _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
541 }
542
543 r = worker_new(&worker, manager, worker_monitor, pid);
544 if (r < 0)
545 return log_error_errno(r, "Failed to create worker object: %m");
546
547 worker_attach_event(worker, event);
548
549 log_device_debug(event->dev, "Worker ["PID_FMT"] is forked for processing SEQNUM=%"PRIu64".", pid, event->seqnum);
550 return 0;
551 }
552
553 static void event_run(Manager *manager, struct event *event) {
554 static bool log_children_max_reached = true;
555 struct worker *worker;
556 Iterator i;
557 int r;
558
559 assert(manager);
560 assert(event);
561
562 HASHMAP_FOREACH(worker, manager->workers, i) {
563 if (worker->state != WORKER_IDLE)
564 continue;
565
566 r = device_monitor_send_device(manager->monitor, worker->monitor, event->dev);
567 if (r < 0) {
568 log_device_error_errno(event->dev, r, "Worker ["PID_FMT"] did not accept message, killing the worker: %m",
569 worker->pid);
570 (void) kill(worker->pid, SIGKILL);
571 worker->state = WORKER_KILLED;
572 continue;
573 }
574 worker_attach_event(worker, event);
575 return;
576 }
577
578 if (hashmap_size(manager->workers) >= arg_children_max) {
579
580 /* Avoid spamming the debug logs if the limit is already reached and
581 * many events still need to be processed */
582 if (log_children_max_reached && arg_children_max > 1) {
583 log_debug("Maximum number (%u) of children reached.", hashmap_size(manager->workers));
584 log_children_max_reached = false;
585 }
586 return;
587 }
588
589 /* Re-enable the debug message for the next batch of events */
590 log_children_max_reached = true;
591
592 /* start new worker and pass initial device */
593 worker_spawn(manager, event);
594 }
595
596 static int event_queue_insert(Manager *manager, sd_device *dev) {
597 _cleanup_(sd_device_unrefp) sd_device *clone = NULL;
598 struct event *event;
599 DeviceAction action;
600 uint64_t seqnum;
601 int r;
602
603 assert(manager);
604 assert(dev);
605
606 /* only one process can add events to the queue */
607 assert(manager->pid == getpid_cached());
608
609 /* We only accepts devices received by device monitor. */
610 r = device_get_seqnum(dev, &seqnum);
611 if (r < 0)
612 return r;
613
614 /* Refuse devices do not have ACTION property. */
615 r = device_get_action(dev, &action);
616 if (r < 0)
617 return r;
618
619 /* Save original device to restore the state on failures. */
620 r = device_shallow_clone(dev, &clone);
621 if (r < 0)
622 return r;
623
624 r = device_copy_properties(clone, dev);
625 if (r < 0)
626 return r;
627
628 event = new(struct event, 1);
629 if (!event)
630 return -ENOMEM;
631
632 *event = (struct event) {
633 .manager = manager,
634 .dev = sd_device_ref(dev),
635 .dev_kernel = TAKE_PTR(clone),
636 .seqnum = seqnum,
637 .state = EVENT_QUEUED,
638 };
639
640 if (LIST_IS_EMPTY(manager->events)) {
641 r = touch("/run/udev/queue");
642 if (r < 0)
643 log_warning_errno(r, "Failed to touch /run/udev/queue: %m");
644 }
645
646 LIST_APPEND(event, manager->events, event);
647
648 log_device_debug(dev, "Device (SEQNUM=%"PRIu64", ACTION=%s) is queued",
649 seqnum, device_action_to_string(action));
650
651 return 0;
652 }
653
654 static void manager_kill_workers(Manager *manager) {
655 struct worker *worker;
656 Iterator i;
657
658 assert(manager);
659
660 HASHMAP_FOREACH(worker, manager->workers, i) {
661 if (worker->state == WORKER_KILLED)
662 continue;
663
664 worker->state = WORKER_KILLED;
665 (void) kill(worker->pid, SIGTERM);
666 }
667 }
668
669 /* lookup event for identical, parent, child device */
670 static int is_device_busy(Manager *manager, struct event *event) {
671 const char *subsystem, *devpath, *devpath_old = NULL;
672 dev_t devnum = makedev(0, 0);
673 struct event *loop_event;
674 size_t devpath_len;
675 int r, ifindex = 0;
676 bool is_block;
677
678 r = sd_device_get_subsystem(event->dev, &subsystem);
679 if (r < 0)
680 return r;
681
682 is_block = streq(subsystem, "block");
683
684 r = sd_device_get_devpath(event->dev, &devpath);
685 if (r < 0)
686 return r;
687
688 devpath_len = strlen(devpath);
689
690 r = sd_device_get_property_value(event->dev, "DEVPATH_OLD", &devpath_old);
691 if (r < 0 && r != -ENOENT)
692 return r;
693
694 r = sd_device_get_devnum(event->dev, &devnum);
695 if (r < 0 && r != -ENOENT)
696 return r;
697
698 r = sd_device_get_ifindex(event->dev, &ifindex);
699 if (r < 0 && r != -ENOENT)
700 return r;
701
702 /* check if queue contains events we depend on */
703 LIST_FOREACH(event, loop_event, manager->events) {
704 size_t loop_devpath_len, common;
705 const char *loop_devpath;
706
707 /* we already found a later event, earlier cannot block us, no need to check again */
708 if (loop_event->seqnum < event->delaying_seqnum)
709 continue;
710
711 /* event we checked earlier still exists, no need to check again */
712 if (loop_event->seqnum == event->delaying_seqnum)
713 return true;
714
715 /* found ourself, no later event can block us */
716 if (loop_event->seqnum >= event->seqnum)
717 break;
718
719 /* check major/minor */
720 if (major(devnum) != 0) {
721 const char *s;
722 dev_t d;
723
724 if (sd_device_get_subsystem(loop_event->dev, &s) < 0)
725 continue;
726
727 if (sd_device_get_devnum(loop_event->dev, &d) >= 0 &&
728 devnum == d && is_block == streq(s, "block"))
729 goto set_delaying_seqnum;
730 }
731
732 /* check network device ifindex */
733 if (ifindex > 0) {
734 int i;
735
736 if (sd_device_get_ifindex(loop_event->dev, &i) >= 0 &&
737 ifindex == i)
738 goto set_delaying_seqnum;
739 }
740
741 if (sd_device_get_devpath(loop_event->dev, &loop_devpath) < 0)
742 continue;
743
744 /* check our old name */
745 if (devpath_old && streq(devpath_old, loop_devpath))
746 goto set_delaying_seqnum;
747
748 loop_devpath_len = strlen(loop_devpath);
749
750 /* compare devpath */
751 common = MIN(devpath_len, loop_devpath_len);
752
753 /* one devpath is contained in the other? */
754 if (!strneq(devpath, loop_devpath, common))
755 continue;
756
757 /* identical device event found */
758 if (devpath_len == loop_devpath_len)
759 goto set_delaying_seqnum;
760
761 /* parent device event found */
762 if (devpath[common] == '/')
763 goto set_delaying_seqnum;
764
765 /* child device event found */
766 if (loop_devpath[common] == '/')
767 goto set_delaying_seqnum;
768 }
769
770 return false;
771
772 set_delaying_seqnum:
773 event->delaying_seqnum = loop_event->seqnum;
774 return true;
775 }
776
777 static int on_exit_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
778 Manager *manager = userdata;
779
780 assert(manager);
781
782 log_error("Giving up waiting for workers to finish.");
783 sd_event_exit(manager->event, -ETIMEDOUT);
784
785 return 1;
786 }
787
788 static void manager_exit(Manager *manager) {
789 uint64_t usec;
790 int r;
791
792 assert(manager);
793
794 manager->exit = true;
795
796 sd_notify(false,
797 "STOPPING=1\n"
798 "STATUS=Starting shutdown...");
799
800 /* close sources of new events and discard buffered events */
801 manager->ctrl = udev_ctrl_unref(manager->ctrl);
802
803 manager->inotify_event = sd_event_source_unref(manager->inotify_event);
804 manager->fd_inotify = safe_close(manager->fd_inotify);
805
806 manager->monitor = sd_device_monitor_unref(manager->monitor);
807
808 /* discard queued events and kill workers */
809 event_queue_cleanup(manager, EVENT_QUEUED);
810 manager_kill_workers(manager);
811
812 assert_se(sd_event_now(manager->event, CLOCK_MONOTONIC, &usec) >= 0);
813
814 r = sd_event_add_time(manager->event, NULL, CLOCK_MONOTONIC,
815 usec + 30 * USEC_PER_SEC, USEC_PER_SEC, on_exit_timeout, manager);
816 if (r < 0)
817 return;
818 }
819
820 /* reload requested, HUP signal received, rules changed, builtin changed */
821 static void manager_reload(Manager *manager) {
822
823 assert(manager);
824
825 sd_notify(false,
826 "RELOADING=1\n"
827 "STATUS=Flushing configuration...");
828
829 manager_kill_workers(manager);
830 manager->rules = udev_rules_free(manager->rules);
831 udev_builtin_exit();
832
833 sd_notifyf(false,
834 "READY=1\n"
835 "STATUS=Processing with %u children at max", arg_children_max);
836 }
837
838 static int on_kill_workers_event(sd_event_source *s, uint64_t usec, void *userdata) {
839 Manager *manager = userdata;
840
841 assert(manager);
842
843 log_debug("Cleanup idle workers");
844 manager_kill_workers(manager);
845
846 return 1;
847 }
848
849 static void event_queue_start(Manager *manager) {
850 struct event *event;
851 usec_t usec;
852 int r;
853
854 assert(manager);
855
856 if (LIST_IS_EMPTY(manager->events) ||
857 manager->exit || manager->stop_exec_queue)
858 return;
859
860 assert_se(sd_event_now(manager->event, CLOCK_MONOTONIC, &usec) >= 0);
861 /* check for changed config, every 3 seconds at most */
862 if (manager->last_usec == 0 ||
863 usec - manager->last_usec > 3 * USEC_PER_SEC) {
864 if (udev_rules_check_timestamp(manager->rules) ||
865 udev_builtin_validate())
866 manager_reload(manager);
867
868 manager->last_usec = usec;
869 }
870
871 r = event_source_disable(manager->kill_workers_event);
872 if (r < 0)
873 log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m");
874
875 udev_builtin_init();
876
877 if (!manager->rules) {
878 r = udev_rules_new(&manager->rules, arg_resolve_name_timing);
879 if (r < 0) {
880 log_warning_errno(r, "Failed to read udev rules: %m");
881 return;
882 }
883 }
884
885 LIST_FOREACH(event, event, manager->events) {
886 if (event->state != EVENT_QUEUED)
887 continue;
888
889 /* do not start event if parent or child event is still running */
890 if (is_device_busy(manager, event) != 0)
891 continue;
892
893 event_run(manager, event);
894 }
895 }
896
897 static void event_queue_cleanup(Manager *manager, enum event_state match_type) {
898 struct event *event, *tmp;
899
900 LIST_FOREACH_SAFE(event, event, tmp, manager->events) {
901 if (match_type != EVENT_UNDEF && match_type != event->state)
902 continue;
903
904 event_free(event);
905 }
906 }
907
908 static int on_worker(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
909 Manager *manager = userdata;
910
911 assert(manager);
912
913 for (;;) {
914 struct worker_message msg;
915 struct iovec iovec = {
916 .iov_base = &msg,
917 .iov_len = sizeof(msg),
918 };
919 union {
920 struct cmsghdr cmsghdr;
921 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
922 } control = {};
923 struct msghdr msghdr = {
924 .msg_iov = &iovec,
925 .msg_iovlen = 1,
926 .msg_control = &control,
927 .msg_controllen = sizeof(control),
928 };
929 struct cmsghdr *cmsg;
930 ssize_t size;
931 struct ucred *ucred = NULL;
932 struct worker *worker;
933
934 size = recvmsg(fd, &msghdr, MSG_DONTWAIT);
935 if (size < 0) {
936 if (errno == EINTR)
937 continue;
938 else if (errno == EAGAIN)
939 /* nothing more to read */
940 break;
941
942 return log_error_errno(errno, "Failed to receive message: %m");
943 } else if (size != sizeof(struct worker_message)) {
944 log_warning("Ignoring worker message with invalid size %zi bytes", size);
945 continue;
946 }
947
948 CMSG_FOREACH(cmsg, &msghdr)
949 if (cmsg->cmsg_level == SOL_SOCKET &&
950 cmsg->cmsg_type == SCM_CREDENTIALS &&
951 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
952 ucred = (struct ucred*) CMSG_DATA(cmsg);
953
954 if (!ucred || ucred->pid <= 0) {
955 log_warning("Ignoring worker message without valid PID");
956 continue;
957 }
958
959 /* lookup worker who sent the signal */
960 worker = hashmap_get(manager->workers, PID_TO_PTR(ucred->pid));
961 if (!worker) {
962 log_debug("Worker ["PID_FMT"] returned, but is no longer tracked", ucred->pid);
963 continue;
964 }
965
966 if (worker->state != WORKER_KILLED)
967 worker->state = WORKER_IDLE;
968
969 /* worker returned */
970 event_free(worker->event);
971 }
972
973 /* we have free workers, try to schedule events */
974 event_queue_start(manager);
975
976 return 1;
977 }
978
979 static int on_uevent(sd_device_monitor *monitor, sd_device *dev, void *userdata) {
980 Manager *manager = userdata;
981 int r;
982
983 assert(manager);
984
985 device_ensure_usec_initialized(dev, NULL);
986
987 r = event_queue_insert(manager, dev);
988 if (r < 0) {
989 log_device_error_errno(dev, r, "Failed to insert device into event queue: %m");
990 return 1;
991 }
992
993 /* we have fresh events, try to schedule them */
994 event_queue_start(manager);
995
996 return 1;
997 }
998
999 /* receive the udevd message from userspace */
1000 static int on_ctrl_msg(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, const union udev_ctrl_msg_value *value, void *userdata) {
1001 Manager *manager = userdata;
1002 int r;
1003
1004 assert(value);
1005 assert(manager);
1006
1007 switch (type) {
1008 case UDEV_CTRL_SET_LOG_LEVEL:
1009 log_debug("Received udev control message (SET_LOG_LEVEL), setting log_priority=%i", value->intval);
1010 log_set_max_level_realm(LOG_REALM_UDEV, value->intval);
1011 log_set_max_level_realm(LOG_REALM_SYSTEMD, value->intval);
1012 manager_kill_workers(manager);
1013 break;
1014 case UDEV_CTRL_STOP_EXEC_QUEUE:
1015 log_debug("Received udev control message (STOP_EXEC_QUEUE)");
1016 manager->stop_exec_queue = true;
1017 break;
1018 case UDEV_CTRL_START_EXEC_QUEUE:
1019 log_debug("Received udev control message (START_EXEC_QUEUE)");
1020 manager->stop_exec_queue = false;
1021 event_queue_start(manager);
1022 break;
1023 case UDEV_CTRL_RELOAD:
1024 log_debug("Received udev control message (RELOAD)");
1025 manager_reload(manager);
1026 break;
1027 case UDEV_CTRL_SET_ENV: {
1028 _cleanup_free_ char *key = NULL, *val = NULL, *old_key = NULL, *old_val = NULL;
1029 const char *eq;
1030
1031 eq = strchr(value->buf, '=');
1032 if (!eq) {
1033 log_error("Invalid key format '%s'", value->buf);
1034 return 1;
1035 }
1036
1037 key = strndup(value->buf, eq - value->buf);
1038 if (!key) {
1039 log_oom();
1040 return 1;
1041 }
1042
1043 old_val = hashmap_remove2(manager->properties, key, (void **) &old_key);
1044
1045 r = hashmap_ensure_allocated(&manager->properties, &string_hash_ops);
1046 if (r < 0) {
1047 log_oom();
1048 return 1;
1049 }
1050
1051 eq++;
1052 if (!isempty(eq)) {
1053 log_debug("Received udev control message (ENV), unsetting '%s'", key);
1054
1055 r = hashmap_put(manager->properties, key, NULL);
1056 if (r < 0) {
1057 log_oom();
1058 return 1;
1059 }
1060 } else {
1061 val = strdup(eq);
1062 if (!val) {
1063 log_oom();
1064 return 1;
1065 }
1066
1067 log_debug("Received udev control message (ENV), setting '%s=%s'", key, val);
1068
1069 r = hashmap_put(manager->properties, key, val);
1070 if (r < 0) {
1071 log_oom();
1072 return 1;
1073 }
1074 }
1075
1076 key = val = NULL;
1077 manager_kill_workers(manager);
1078 break;
1079 }
1080 case UDEV_CTRL_SET_CHILDREN_MAX:
1081 if (value->intval <= 0) {
1082 log_debug("Received invalid udev control message (SET_MAX_CHILDREN, %i), ignoring.", value->intval);
1083 return 0;
1084 }
1085
1086 log_debug("Received udev control message (SET_MAX_CHILDREN), setting children_max=%i", value->intval);
1087 arg_children_max = value->intval;
1088
1089 (void) sd_notifyf(false,
1090 "READY=1\n"
1091 "STATUS=Processing with %u children at max", arg_children_max);
1092 break;
1093 case UDEV_CTRL_PING:
1094 log_debug("Received udev control message (PING)");
1095 break;
1096 case UDEV_CTRL_EXIT:
1097 log_debug("Received udev control message (EXIT)");
1098 manager_exit(manager);
1099 break;
1100 default:
1101 log_debug("Received unknown udev control message, ignoring");
1102 }
1103
1104 return 1;
1105 }
1106
1107 static int synthesize_change(sd_device *dev) {
1108 const char *subsystem, *sysname, *devname, *syspath, *devtype;
1109 char filename[PATH_MAX];
1110 int r;
1111
1112 r = sd_device_get_subsystem(dev, &subsystem);
1113 if (r < 0)
1114 return r;
1115
1116 r = sd_device_get_sysname(dev, &sysname);
1117 if (r < 0)
1118 return r;
1119
1120 r = sd_device_get_devname(dev, &devname);
1121 if (r < 0)
1122 return r;
1123
1124 r = sd_device_get_syspath(dev, &syspath);
1125 if (r < 0)
1126 return r;
1127
1128 r = sd_device_get_devtype(dev, &devtype);
1129 if (r < 0)
1130 return r;
1131
1132 if (streq_ptr("block", subsystem) &&
1133 streq_ptr("disk", devtype) &&
1134 !startswith(sysname, "dm-")) {
1135 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
1136 bool part_table_read = false, has_partitions = false;
1137 sd_device *d;
1138 int fd;
1139
1140 /*
1141 * Try to re-read the partition table. This only succeeds if
1142 * none of the devices is busy. The kernel returns 0 if no
1143 * partition table is found, and we will not get an event for
1144 * the disk.
1145 */
1146 fd = open(devname, O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
1147 if (fd >= 0) {
1148 r = flock(fd, LOCK_EX|LOCK_NB);
1149 if (r >= 0)
1150 r = ioctl(fd, BLKRRPART, 0);
1151
1152 close(fd);
1153 if (r >= 0)
1154 part_table_read = true;
1155 }
1156
1157 /* search for partitions */
1158 r = sd_device_enumerator_new(&e);
1159 if (r < 0)
1160 return r;
1161
1162 r = sd_device_enumerator_allow_uninitialized(e);
1163 if (r < 0)
1164 return r;
1165
1166 r = sd_device_enumerator_add_match_parent(e, dev);
1167 if (r < 0)
1168 return r;
1169
1170 r = sd_device_enumerator_add_match_subsystem(e, "block", true);
1171 if (r < 0)
1172 return r;
1173
1174 FOREACH_DEVICE(e, d) {
1175 const char *t;
1176
1177 if (sd_device_get_devtype(d, &t) < 0 ||
1178 !streq("partition", t))
1179 continue;
1180
1181 has_partitions = true;
1182 break;
1183 }
1184
1185 /*
1186 * We have partitions and re-read the table, the kernel already sent
1187 * out a "change" event for the disk, and "remove/add" for all
1188 * partitions.
1189 */
1190 if (part_table_read && has_partitions)
1191 return 0;
1192
1193 /*
1194 * We have partitions but re-reading the partition table did not
1195 * work, synthesize "change" for the disk and all partitions.
1196 */
1197 log_debug("Device '%s' is closed, synthesising 'change'", devname);
1198 strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL);
1199 write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER);
1200
1201 FOREACH_DEVICE(e, d) {
1202 const char *t, *n, *s;
1203
1204 if (sd_device_get_devtype(d, &t) < 0 ||
1205 !streq("partition", t))
1206 continue;
1207
1208 if (sd_device_get_devname(d, &n) < 0 ||
1209 sd_device_get_syspath(d, &s) < 0)
1210 continue;
1211
1212 log_debug("Device '%s' is closed, synthesising partition '%s' 'change'", devname, n);
1213 strscpyl(filename, sizeof(filename), s, "/uevent", NULL);
1214 write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER);
1215 }
1216
1217 return 0;
1218 }
1219
1220 log_debug("Device %s is closed, synthesising 'change'", devname);
1221 strscpyl(filename, sizeof(filename), syspath, "/uevent", NULL);
1222 write_string_file(filename, "change", WRITE_STRING_FILE_DISABLE_BUFFER);
1223
1224 return 0;
1225 }
1226
1227 static int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
1228 Manager *manager = userdata;
1229 union inotify_event_buffer buffer;
1230 struct inotify_event *e;
1231 ssize_t l;
1232 int r;
1233
1234 assert(manager);
1235
1236 r = event_source_disable(manager->kill_workers_event);
1237 if (r < 0)
1238 log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m");
1239
1240 l = read(fd, &buffer, sizeof(buffer));
1241 if (l < 0) {
1242 if (IN_SET(errno, EAGAIN, EINTR))
1243 return 1;
1244
1245 return log_error_errno(errno, "Failed to read inotify fd: %m");
1246 }
1247
1248 FOREACH_INOTIFY_EVENT(e, buffer, l) {
1249 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
1250 const char *devnode;
1251
1252 if (udev_watch_lookup(e->wd, &dev) <= 0)
1253 continue;
1254
1255 if (sd_device_get_devname(dev, &devnode) < 0)
1256 continue;
1257
1258 log_device_debug(dev, "Inotify event: %x for %s", e->mask, devnode);
1259 if (e->mask & IN_CLOSE_WRITE)
1260 synthesize_change(dev);
1261 else if (e->mask & IN_IGNORED)
1262 udev_watch_end(dev);
1263 }
1264
1265 return 1;
1266 }
1267
1268 static int on_sigterm(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1269 Manager *manager = userdata;
1270
1271 assert(manager);
1272
1273 manager_exit(manager);
1274
1275 return 1;
1276 }
1277
1278 static int on_sighup(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1279 Manager *manager = userdata;
1280
1281 assert(manager);
1282
1283 manager_reload(manager);
1284
1285 return 1;
1286 }
1287
1288 static int on_sigchld(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1289 Manager *manager = userdata;
1290 int r;
1291
1292 assert(manager);
1293
1294 for (;;) {
1295 pid_t pid;
1296 int status;
1297 struct worker *worker;
1298
1299 pid = waitpid(-1, &status, WNOHANG);
1300 if (pid <= 0)
1301 break;
1302
1303 worker = hashmap_get(manager->workers, PID_TO_PTR(pid));
1304 if (!worker) {
1305 log_warning("Worker ["PID_FMT"] is unknown, ignoring", pid);
1306 continue;
1307 }
1308
1309 if (WIFEXITED(status)) {
1310 if (WEXITSTATUS(status) == 0)
1311 log_debug("Worker ["PID_FMT"] exited", pid);
1312 else
1313 log_warning("Worker ["PID_FMT"] exited with return code %i", pid, WEXITSTATUS(status));
1314 } else if (WIFSIGNALED(status))
1315 log_warning("Worker ["PID_FMT"] terminated by signal %i (%s)", pid, WTERMSIG(status), signal_to_string(WTERMSIG(status)));
1316 else if (WIFSTOPPED(status)) {
1317 log_info("Worker ["PID_FMT"] stopped", pid);
1318 continue;
1319 } else if (WIFCONTINUED(status)) {
1320 log_info("Worker ["PID_FMT"] continued", pid);
1321 continue;
1322 } else
1323 log_warning("Worker ["PID_FMT"] exit with status 0x%04x", pid, status);
1324
1325 if ((!WIFEXITED(status) || WEXITSTATUS(status) != 0) && worker->event) {
1326 log_device_error(worker->event->dev, "Worker ["PID_FMT"] failed", pid);
1327
1328 /* delete state from disk */
1329 device_delete_db(worker->event->dev);
1330 device_tag_index(worker->event->dev, NULL, false);
1331
1332 /* forward kernel event without amending it */
1333 r = device_monitor_send_device(manager->monitor, NULL, worker->event->dev_kernel);
1334 if (r < 0)
1335 log_device_error_errno(worker->event->dev_kernel, r, "Failed to send back device to kernel: %m");
1336 }
1337
1338 worker_free(worker);
1339 }
1340
1341 /* we can start new workers, try to schedule events */
1342 event_queue_start(manager);
1343
1344 /* Disable unnecessary cleanup event */
1345 if (hashmap_isempty(manager->workers)) {
1346 r = event_source_disable(manager->kill_workers_event);
1347 if (r < 0)
1348 log_warning_errno(r, "Failed to disable event source for cleaning up idle workers, ignoring: %m");
1349 }
1350
1351 return 1;
1352 }
1353
1354 static int on_post(sd_event_source *s, void *userdata) {
1355 Manager *manager = userdata;
1356
1357 assert(manager);
1358
1359 if (!LIST_IS_EMPTY(manager->events))
1360 return 1;
1361
1362 /* There are no pending events. Let's cleanup idle process. */
1363
1364 if (!hashmap_isempty(manager->workers)) {
1365 /* There are idle workers */
1366 (void) event_reset_time(manager->event, &manager->kill_workers_event, CLOCK_MONOTONIC,
1367 now(CLOCK_MONOTONIC) + 3 * USEC_PER_SEC, USEC_PER_SEC,
1368 on_kill_workers_event, manager, 0, "kill-workers-event", false);
1369 return 1;
1370 }
1371
1372 /* There are no idle workers. */
1373
1374 if (manager->exit)
1375 return sd_event_exit(manager->event, 0);
1376
1377 if (manager->cgroup)
1378 /* cleanup possible left-over processes in our cgroup */
1379 (void) cg_kill(SYSTEMD_CGROUP_CONTROLLER, manager->cgroup, SIGKILL, CGROUP_IGNORE_SELF, NULL, NULL, NULL);
1380
1381 return 1;
1382 }
1383
1384 static int listen_fds(int *ret_ctrl, int *ret_netlink) {
1385 int ctrl_fd = -1, netlink_fd = -1;
1386 int fd, n;
1387
1388 assert(ret_ctrl);
1389 assert(ret_netlink);
1390
1391 n = sd_listen_fds(true);
1392 if (n < 0)
1393 return n;
1394
1395 for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) {
1396 if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1) > 0) {
1397 if (ctrl_fd >= 0)
1398 return -EINVAL;
1399 ctrl_fd = fd;
1400 continue;
1401 }
1402
1403 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
1404 if (netlink_fd >= 0)
1405 return -EINVAL;
1406 netlink_fd = fd;
1407 continue;
1408 }
1409
1410 return -EINVAL;
1411 }
1412
1413 *ret_ctrl = ctrl_fd;
1414 *ret_netlink = netlink_fd;
1415
1416 return 0;
1417 }
1418
1419 /*
1420 * read the kernel command line, in case we need to get into debug mode
1421 * udev.log_priority=<level> syslog priority
1422 * udev.children_max=<number of workers> events are fully serialized if set to 1
1423 * udev.exec_delay=<number of seconds> delay execution of every executed program
1424 * udev.event_timeout=<number of seconds> seconds to wait before terminating an event
1425 */
1426 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
1427 int r = 0;
1428
1429 assert(key);
1430
1431 if (!value)
1432 return 0;
1433
1434 if (proc_cmdline_key_streq(key, "udev.log_priority")) {
1435
1436 if (proc_cmdline_value_missing(key, value))
1437 return 0;
1438
1439 r = log_level_from_string(value);
1440 if (r >= 0)
1441 log_set_max_level(r);
1442
1443 } else if (proc_cmdline_key_streq(key, "udev.event_timeout")) {
1444
1445 if (proc_cmdline_value_missing(key, value))
1446 return 0;
1447
1448 r = parse_sec(value, &arg_event_timeout_usec);
1449
1450 } else if (proc_cmdline_key_streq(key, "udev.children_max")) {
1451
1452 if (proc_cmdline_value_missing(key, value))
1453 return 0;
1454
1455 r = safe_atou(value, &arg_children_max);
1456
1457 } else if (proc_cmdline_key_streq(key, "udev.exec_delay")) {
1458
1459 if (proc_cmdline_value_missing(key, value))
1460 return 0;
1461
1462 r = parse_sec(value, &arg_exec_delay_usec);
1463
1464 } else if (startswith(key, "udev."))
1465 log_warning("Unknown udev kernel command line option \"%s\", ignoring", key);
1466
1467 if (r < 0)
1468 log_warning_errno(r, "Failed to parse \"%s=%s\", ignoring: %m", key, value);
1469
1470 return 0;
1471 }
1472
1473 static int help(void) {
1474 _cleanup_free_ char *link = NULL;
1475 int r;
1476
1477 r = terminal_urlify_man("systemd-udevd.service", "8", &link);
1478 if (r < 0)
1479 return log_oom();
1480
1481 printf("%s [OPTIONS...]\n\n"
1482 "Manages devices.\n\n"
1483 " -h --help Print this message\n"
1484 " -V --version Print version of the program\n"
1485 " -d --daemon Detach and run in the background\n"
1486 " -D --debug Enable debug output\n"
1487 " -c --children-max=INT Set maximum number of workers\n"
1488 " -e --exec-delay=SECONDS Seconds to wait before executing RUN=\n"
1489 " -t --event-timeout=SECONDS Seconds to wait before terminating an event\n"
1490 " -N --resolve-names=early|late|never\n"
1491 " When to resolve users and groups\n"
1492 "\nSee the %s for details.\n"
1493 , program_invocation_short_name
1494 , link
1495 );
1496
1497 return 0;
1498 }
1499
1500 static int parse_argv(int argc, char *argv[]) {
1501 static const struct option options[] = {
1502 { "daemon", no_argument, NULL, 'd' },
1503 { "debug", no_argument, NULL, 'D' },
1504 { "children-max", required_argument, NULL, 'c' },
1505 { "exec-delay", required_argument, NULL, 'e' },
1506 { "event-timeout", required_argument, NULL, 't' },
1507 { "resolve-names", required_argument, NULL, 'N' },
1508 { "help", no_argument, NULL, 'h' },
1509 { "version", no_argument, NULL, 'V' },
1510 {}
1511 };
1512
1513 int c, r;
1514
1515 assert(argc >= 0);
1516 assert(argv);
1517
1518 while ((c = getopt_long(argc, argv, "c:de:Dt:N:hV", options, NULL)) >= 0) {
1519 switch (c) {
1520
1521 case 'd':
1522 arg_daemonize = true;
1523 break;
1524 case 'c':
1525 r = safe_atou(optarg, &arg_children_max);
1526 if (r < 0)
1527 log_warning_errno(r, "Failed to parse --children-max= value '%s', ignoring: %m", optarg);
1528 break;
1529 case 'e':
1530 r = parse_sec(optarg, &arg_exec_delay_usec);
1531 if (r < 0)
1532 log_warning_errno(r, "Failed to parse --exec-delay= value '%s', ignoring: %m", optarg);
1533 break;
1534 case 't':
1535 r = parse_sec(optarg, &arg_event_timeout_usec);
1536 if (r < 0)
1537 log_warning_errno(r, "Failed to parse --event-timeout= value '%s', ignoring: %m", optarg);
1538 break;
1539 case 'D':
1540 arg_debug = true;
1541 break;
1542 case 'N': {
1543 ResolveNameTiming t;
1544
1545 t = resolve_name_timing_from_string(optarg);
1546 if (t < 0)
1547 log_warning("Invalid --resolve-names= value '%s', ignoring.", optarg);
1548 else
1549 arg_resolve_name_timing = t;
1550 break;
1551 }
1552 case 'h':
1553 return help();
1554 case 'V':
1555 printf("%s\n", GIT_VERSION);
1556 return 0;
1557 case '?':
1558 return -EINVAL;
1559 default:
1560 assert_not_reached("Unhandled option");
1561
1562 }
1563 }
1564
1565 return 1;
1566 }
1567
1568 static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cgroup) {
1569 _cleanup_(manager_freep) Manager *manager = NULL;
1570 int r;
1571
1572 assert(ret);
1573
1574 manager = new(Manager, 1);
1575 if (!manager)
1576 return log_oom();
1577
1578 *manager = (Manager) {
1579 .fd_inotify = -1,
1580 .worker_watch = { -1, -1 },
1581 .cgroup = cgroup,
1582 };
1583
1584 r = udev_ctrl_new_from_fd(&manager->ctrl, fd_ctrl);
1585 if (r < 0)
1586 return log_error_errno(r, "Failed to initialize udev control socket: %m");
1587
1588 r = udev_ctrl_enable_receiving(manager->ctrl);
1589 if (r < 0)
1590 return log_error_errno(r, "Failed to bind udev control socket: %m");
1591
1592 r = device_monitor_new_full(&manager->monitor, MONITOR_GROUP_KERNEL, fd_uevent);
1593 if (r < 0)
1594 return log_error_errno(r, "Failed to initialize device monitor: %m");
1595
1596 (void) sd_device_monitor_set_receive_buffer_size(manager->monitor, 128 * 1024 * 1024);
1597
1598 r = device_monitor_enable_receiving(manager->monitor);
1599 if (r < 0)
1600 return log_error_errno(r, "Failed to bind netlink socket: %m");
1601
1602 *ret = TAKE_PTR(manager);
1603
1604 return 0;
1605 }
1606
1607 static int main_loop(Manager *manager) {
1608 int fd_worker, r;
1609
1610 manager->pid = getpid_cached();
1611
1612 /* unnamed socket from workers to the main daemon */
1613 r = socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, manager->worker_watch);
1614 if (r < 0)
1615 return log_error_errno(errno, "Failed to create socketpair for communicating with workers: %m");
1616
1617 fd_worker = manager->worker_watch[READ_END];
1618
1619 r = setsockopt_int(fd_worker, SOL_SOCKET, SO_PASSCRED, true);
1620 if (r < 0)
1621 return log_error_errno(r, "Failed to enable SO_PASSCRED: %m");
1622
1623 r = udev_watch_init();
1624 if (r < 0)
1625 return log_error_errno(r, "Failed to create inotify descriptor: %m");
1626 manager->fd_inotify = r;
1627
1628 udev_watch_restore();
1629
1630 /* block and listen to all signals on signalfd */
1631 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
1632
1633 r = sd_event_default(&manager->event);
1634 if (r < 0)
1635 return log_error_errno(r, "Failed to allocate event loop: %m");
1636
1637 r = sd_event_add_signal(manager->event, NULL, SIGINT, on_sigterm, manager);
1638 if (r < 0)
1639 return log_error_errno(r, "Failed to create SIGINT event source: %m");
1640
1641 r = sd_event_add_signal(manager->event, NULL, SIGTERM, on_sigterm, manager);
1642 if (r < 0)
1643 return log_error_errno(r, "Failed to create SIGTERM event source: %m");
1644
1645 r = sd_event_add_signal(manager->event, NULL, SIGHUP, on_sighup, manager);
1646 if (r < 0)
1647 return log_error_errno(r, "Failed to create SIGHUP event source: %m");
1648
1649 r = sd_event_add_signal(manager->event, NULL, SIGCHLD, on_sigchld, manager);
1650 if (r < 0)
1651 return log_error_errno(r, "Failed to create SIGCHLD event source: %m");
1652
1653 r = sd_event_set_watchdog(manager->event, true);
1654 if (r < 0)
1655 return log_error_errno(r, "Failed to create watchdog event source: %m");
1656
1657 r = udev_ctrl_attach_event(manager->ctrl, manager->event);
1658 if (r < 0)
1659 return log_error_errno(r, "Failed to attach event to udev control: %m");
1660
1661 r = udev_ctrl_start(manager->ctrl, on_ctrl_msg, manager);
1662 if (r < 0)
1663 return log_error_errno(r, "Failed to start device monitor: %m");
1664
1665 /* This needs to be after the inotify and uevent handling, to make sure
1666 * that the ping is send back after fully processing the pending uevents
1667 * (including the synthetic ones we may create due to inotify events).
1668 */
1669 r = sd_event_source_set_priority(udev_ctrl_get_event_source(manager->ctrl), SD_EVENT_PRIORITY_IDLE);
1670 if (r < 0)
1671 return log_error_errno(r, "Failed to set IDLE event priority for udev control event source: %m");
1672
1673 r = sd_event_add_io(manager->event, &manager->inotify_event, manager->fd_inotify, EPOLLIN, on_inotify, manager);
1674 if (r < 0)
1675 return log_error_errno(r, "Failed to create inotify event source: %m");
1676
1677 r = sd_device_monitor_attach_event(manager->monitor, manager->event);
1678 if (r < 0)
1679 return log_error_errno(r, "Failed to attach event to device monitor: %m");
1680
1681 r = sd_device_monitor_start(manager->monitor, on_uevent, manager);
1682 if (r < 0)
1683 return log_error_errno(r, "Failed to start device monitor: %m");
1684
1685 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(manager->monitor), "device-monitor");
1686
1687 r = sd_event_add_io(manager->event, NULL, fd_worker, EPOLLIN, on_worker, manager);
1688 if (r < 0)
1689 return log_error_errno(r, "Failed to create worker event source: %m");
1690
1691 r = sd_event_add_post(manager->event, NULL, on_post, manager);
1692 if (r < 0)
1693 return log_error_errno(r, "Failed to create post event source: %m");
1694
1695 udev_builtin_init();
1696
1697 r = udev_rules_new(&manager->rules, arg_resolve_name_timing);
1698 if (!manager->rules)
1699 return log_error_errno(r, "Failed to read udev rules: %m");
1700
1701 r = udev_rules_apply_static_dev_perms(manager->rules);
1702 if (r < 0)
1703 log_error_errno(r, "Failed to apply permissions on static device nodes: %m");
1704
1705 (void) sd_notifyf(false,
1706 "READY=1\n"
1707 "STATUS=Processing with %u children at max", arg_children_max);
1708
1709 r = sd_event_loop(manager->event);
1710 if (r < 0)
1711 log_error_errno(r, "Event loop failed: %m");
1712
1713 sd_notify(false,
1714 "STOPPING=1\n"
1715 "STATUS=Shutting down...");
1716 return r;
1717 }
1718
1719 static int run(int argc, char *argv[]) {
1720 _cleanup_free_ char *cgroup = NULL;
1721 _cleanup_(manager_freep) Manager *manager = NULL;
1722 int fd_ctrl = -1, fd_uevent = -1;
1723 int r;
1724
1725 log_set_target(LOG_TARGET_AUTO);
1726 udev_parse_config_full(&arg_children_max, &arg_exec_delay_usec, &arg_event_timeout_usec, &arg_resolve_name_timing);
1727 log_parse_environment();
1728 log_open();
1729
1730 r = parse_argv(argc, argv);
1731 if (r <= 0)
1732 return r;
1733
1734 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
1735 if (r < 0)
1736 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
1737
1738 if (arg_debug) {
1739 log_set_target(LOG_TARGET_CONSOLE);
1740 log_set_max_level(LOG_DEBUG);
1741 }
1742
1743 log_set_max_level_realm(LOG_REALM_SYSTEMD, log_get_max_level());
1744
1745 r = must_be_root();
1746 if (r < 0)
1747 return r;
1748
1749 if (arg_children_max == 0) {
1750 unsigned long cpu_limit, mem_limit;
1751 unsigned long cpu_count = 1;
1752 cpu_set_t cpu_set;
1753
1754 if (sched_getaffinity(0, sizeof(cpu_set), &cpu_set) == 0)
1755 cpu_count = CPU_COUNT(&cpu_set);
1756
1757 cpu_limit = cpu_count * 2 + 16;
1758 mem_limit = MAX(physical_memory() / (128UL*1024*1024), 10U);
1759
1760 arg_children_max = MIN(cpu_limit, mem_limit);
1761 arg_children_max = MIN(WORKER_NUM_MAX, arg_children_max);
1762
1763 log_debug("Set children_max to %u", arg_children_max);
1764 }
1765
1766 /* set umask before creating any file/directory */
1767 r = chdir("/");
1768 if (r < 0)
1769 return log_error_errno(errno, "Failed to change dir to '/': %m");
1770
1771 umask(022);
1772
1773 r = mac_selinux_init();
1774 if (r < 0)
1775 return log_error_errno(r, "Could not initialize labelling: %m");
1776
1777 r = mkdir_errno_wrapper("/run/udev", 0755);
1778 if (r < 0 && r != -EEXIST)
1779 return log_error_errno(r, "Failed to create /run/udev: %m");
1780
1781 dev_setup(NULL, UID_INVALID, GID_INVALID);
1782
1783 if (getppid() == 1 && sd_booted() > 0) {
1784 /* Get our own cgroup, we regularly kill everything udev has left behind.
1785 * We only do this on systemd systems, and only if we are directly spawned
1786 * by PID1. Otherwise we are not guaranteed to have a dedicated cgroup. */
1787 r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &cgroup);
1788 if (r < 0) {
1789 if (IN_SET(r, -ENOENT, -ENOMEDIUM))
1790 log_debug_errno(r, "Dedicated cgroup not found: %m");
1791 else
1792 log_warning_errno(r, "Failed to get cgroup: %m");
1793 }
1794 }
1795
1796 r = listen_fds(&fd_ctrl, &fd_uevent);
1797 if (r < 0)
1798 return log_error_errno(r, "Failed to listen on fds: %m");
1799
1800 r = manager_new(&manager, fd_ctrl, fd_uevent, cgroup);
1801 if (r < 0)
1802 return log_error_errno(r, "Failed to create manager: %m");
1803
1804 if (arg_daemonize) {
1805 pid_t pid;
1806
1807 log_info("Starting version " GIT_VERSION);
1808
1809 /* connect /dev/null to stdin, stdout, stderr */
1810 if (log_get_max_level() < LOG_DEBUG) {
1811 r = make_null_stdio();
1812 if (r < 0)
1813 log_warning_errno(r, "Failed to redirect standard streams to /dev/null: %m");
1814 }
1815
1816 pid = fork();
1817 if (pid < 0)
1818 return log_error_errno(errno, "Failed to fork daemon: %m");
1819 if (pid > 0)
1820 /* parent */
1821 return 0;
1822
1823 /* child */
1824 (void) setsid();
1825
1826 r = set_oom_score_adjust(-1000);
1827 if (r < 0)
1828 log_debug_errno(r, "Failed to adjust OOM score, ignoring: %m");
1829 }
1830
1831 return main_loop(manager);
1832 }
1833
1834 DEFINE_MAIN_FUNCTION(run);