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