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