]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevd.c
udevd: add --event-timeout commandline option
[thirdparty/systemd.git] / src / udev / udevd.c
1 /*
2 * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
4 * Copyright (C) 2009 Canonical Ltd.
5 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <stddef.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <getopt.h>
33 #include <dirent.h>
34 #include <sys/file.h>
35 #include <sys/time.h>
36 #include <sys/prctl.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 #include <sys/signalfd.h>
40 #include <sys/epoll.h>
41 #include <sys/mount.h>
42 #include <sys/poll.h>
43 #include <sys/wait.h>
44 #include <sys/stat.h>
45 #include <sys/ioctl.h>
46 #include <sys/inotify.h>
47 #include <sys/utsname.h>
48
49 #include "udev.h"
50 #include "udev-util.h"
51 #include "sd-daemon.h"
52 #include "cgroup-util.h"
53 #include "dev-setup.h"
54 #include "fileio.h"
55
56 static bool debug;
57
58 void udev_main_log(struct udev *udev, int priority,
59 const char *file, int line, const char *fn,
60 const char *format, va_list args)
61 {
62 log_metav(priority, file, line, fn, format, args);
63 }
64
65 static struct udev_rules *rules;
66 static struct udev_ctrl *udev_ctrl;
67 static struct udev_monitor *monitor;
68 static int worker_watch[2] = { -1, -1 };
69 static int fd_signal = -1;
70 static int fd_ep = -1;
71 static int fd_inotify = -1;
72 static bool stop_exec_queue;
73 static bool reload;
74 static int children;
75 static int children_max;
76 static int exec_delay;
77 static int event_timeout = 30;
78 static sigset_t sigmask_orig;
79 static UDEV_LIST(event_list);
80 static UDEV_LIST(worker_list);
81 static char *udev_cgroup;
82 static bool udev_exit;
83
84 enum event_state {
85 EVENT_UNDEF,
86 EVENT_QUEUED,
87 EVENT_RUNNING,
88 };
89
90 struct event {
91 struct udev_list_node node;
92 struct udev *udev;
93 struct udev_device *dev;
94 enum event_state state;
95 int exitcode;
96 unsigned long long int delaying_seqnum;
97 unsigned long long int seqnum;
98 const char *devpath;
99 size_t devpath_len;
100 const char *devpath_old;
101 dev_t devnum;
102 int ifindex;
103 bool is_block;
104 #ifdef HAVE_FIRMWARE
105 bool nodelay;
106 #endif
107 };
108
109 static inline struct event *node_to_event(struct udev_list_node *node)
110 {
111 return container_of(node, struct event, node);
112 }
113
114 static void event_queue_cleanup(struct udev *udev, enum event_state type);
115
116 enum worker_state {
117 WORKER_UNDEF,
118 WORKER_RUNNING,
119 WORKER_IDLE,
120 WORKER_KILLED,
121 };
122
123 struct worker {
124 struct udev_list_node node;
125 struct udev *udev;
126 int refcount;
127 pid_t pid;
128 struct udev_monitor *monitor;
129 enum worker_state state;
130 struct event *event;
131 usec_t event_start_usec;
132 };
133
134 /* passed from worker to main process */
135 struct worker_message {
136 pid_t pid;
137 int exitcode;
138 };
139
140 static inline struct worker *node_to_worker(struct udev_list_node *node)
141 {
142 return container_of(node, struct worker, node);
143 }
144
145 static void event_queue_delete(struct event *event)
146 {
147 udev_list_node_remove(&event->node);
148 udev_device_unref(event->dev);
149 free(event);
150 }
151
152 static struct worker *worker_ref(struct worker *worker)
153 {
154 worker->refcount++;
155 return worker;
156 }
157
158 static void worker_cleanup(struct worker *worker)
159 {
160 udev_list_node_remove(&worker->node);
161 udev_monitor_unref(worker->monitor);
162 children--;
163 free(worker);
164 }
165
166 static void worker_unref(struct worker *worker)
167 {
168 worker->refcount--;
169 if (worker->refcount > 0)
170 return;
171 log_debug("worker [%u] cleaned up", worker->pid);
172 worker_cleanup(worker);
173 }
174
175 static void worker_list_cleanup(struct udev *udev)
176 {
177 struct udev_list_node *loop, *tmp;
178
179 udev_list_node_foreach_safe(loop, tmp, &worker_list) {
180 struct worker *worker = node_to_worker(loop);
181
182 worker_cleanup(worker);
183 }
184 }
185
186 static void worker_new(struct event *event)
187 {
188 struct udev *udev = event->udev;
189 struct worker *worker;
190 struct udev_monitor *worker_monitor;
191 pid_t pid;
192
193 /* listen for new events */
194 worker_monitor = udev_monitor_new_from_netlink(udev, NULL);
195 if (worker_monitor == NULL)
196 return;
197 /* allow the main daemon netlink address to send devices to the worker */
198 udev_monitor_allow_unicast_sender(worker_monitor, monitor);
199 udev_monitor_enable_receiving(worker_monitor);
200
201 worker = new0(struct worker, 1);
202 if (worker == NULL) {
203 udev_monitor_unref(worker_monitor);
204 return;
205 }
206 /* worker + event reference */
207 worker->refcount = 2;
208 worker->udev = udev;
209
210 pid = fork();
211 switch (pid) {
212 case 0: {
213 struct udev_device *dev = NULL;
214 int fd_monitor;
215 struct epoll_event ep_signal, ep_monitor;
216 sigset_t mask;
217 int rc = EXIT_SUCCESS;
218
219 /* take initial device from queue */
220 dev = event->dev;
221 event->dev = NULL;
222
223 free(worker);
224 worker_list_cleanup(udev);
225 event_queue_cleanup(udev, EVENT_UNDEF);
226 udev_monitor_unref(monitor);
227 udev_ctrl_unref(udev_ctrl);
228 close(fd_signal);
229 close(fd_ep);
230 close(worker_watch[READ_END]);
231
232 sigfillset(&mask);
233 fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
234 if (fd_signal < 0) {
235 log_error("error creating signalfd %m");
236 rc = 2;
237 goto out;
238 }
239
240 fd_ep = epoll_create1(EPOLL_CLOEXEC);
241 if (fd_ep < 0) {
242 log_error("error creating epoll fd: %m");
243 rc = 3;
244 goto out;
245 }
246
247 memzero(&ep_signal, sizeof(struct epoll_event));
248 ep_signal.events = EPOLLIN;
249 ep_signal.data.fd = fd_signal;
250
251 fd_monitor = udev_monitor_get_fd(worker_monitor);
252 memzero(&ep_monitor, sizeof(struct epoll_event));
253 ep_monitor.events = EPOLLIN;
254 ep_monitor.data.fd = fd_monitor;
255
256 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
257 epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
258 log_error("fail to add fds to epoll: %m");
259 rc = 4;
260 goto out;
261 }
262
263 /* request TERM signal if parent exits */
264 prctl(PR_SET_PDEATHSIG, SIGTERM);
265
266 /* reset OOM score, we only protect the main daemon */
267 write_string_file("/proc/self/oom_score_adj", "0");
268
269 for (;;) {
270 struct udev_event *udev_event;
271 struct worker_message msg;
272 int fd_lock = -1;
273 int err = 0;
274
275 log_debug("seq %llu running", udev_device_get_seqnum(dev));
276 udev_event = udev_event_new(dev);
277 if (udev_event == NULL) {
278 rc = 5;
279 goto out;
280 }
281
282 /* needed for SIGCHLD/SIGTERM in spawn() */
283 udev_event->fd_signal = fd_signal;
284
285 if (exec_delay > 0)
286 udev_event->exec_delay = exec_delay;
287
288 /*
289 * Take a shared lock on the device node; this establishes
290 * a concept of device "ownership" to serialize device
291 * access. External processes holding an exclusive lock will
292 * cause udev to skip the event handling; in the case udev
293 * acquired the lock, the external process can block until
294 * udev has finished its event handling.
295 */
296 if (!streq_ptr(udev_device_get_action(dev), "remove") &&
297 streq_ptr("block", udev_device_get_subsystem(dev)) &&
298 !startswith(udev_device_get_sysname(dev), "dm-") &&
299 !startswith(udev_device_get_sysname(dev), "md")) {
300 struct udev_device *d = dev;
301
302 if (streq_ptr("partition", udev_device_get_devtype(d)))
303 d = udev_device_get_parent(d);
304
305 if (d) {
306 fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
307 if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
308 log_debug("Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
309 err = -EWOULDBLOCK;
310 fd_lock = safe_close(fd_lock);
311 goto skip;
312 }
313 }
314 }
315
316 if (event_timeout != 30)
317 udev_event->timeout_usec = event_timeout * USEC_PER_SEC;
318
319 /* apply rules, create node, symlinks */
320 udev_event_execute_rules(udev_event, rules, &sigmask_orig);
321
322 udev_event_execute_run(udev_event, &sigmask_orig);
323
324 /* apply/restore inotify watch */
325 if (udev_event->inotify_watch) {
326 udev_watch_begin(udev, dev);
327 udev_device_update_db(dev);
328 }
329
330 safe_close(fd_lock);
331
332 /* send processed event back to libudev listeners */
333 udev_monitor_send_device(worker_monitor, NULL, dev);
334
335 skip:
336 /* send udevd the result of the event execution */
337 memzero(&msg, sizeof(struct worker_message));
338 msg.exitcode = err;
339 msg.pid = getpid();
340 send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
341
342 log_debug("seq %llu processed with %i", udev_device_get_seqnum(dev), err);
343
344 udev_device_unref(dev);
345 dev = NULL;
346
347 if (udev_event->sigterm) {
348 udev_event_unref(udev_event);
349 goto out;
350 }
351
352 udev_event_unref(udev_event);
353
354 /* wait for more device messages from main udevd, or term signal */
355 while (dev == NULL) {
356 struct epoll_event ev[4];
357 int fdcount;
358 int i;
359
360 fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
361 if (fdcount < 0) {
362 if (errno == EINTR)
363 continue;
364 log_error("failed to poll: %m");
365 goto out;
366 }
367
368 for (i = 0; i < fdcount; i++) {
369 if (ev[i].data.fd == fd_monitor && ev[i].events & EPOLLIN) {
370 dev = udev_monitor_receive_device(worker_monitor);
371 break;
372 } else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN) {
373 struct signalfd_siginfo fdsi;
374 ssize_t size;
375
376 size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
377 if (size != sizeof(struct signalfd_siginfo))
378 continue;
379 switch (fdsi.ssi_signo) {
380 case SIGTERM:
381 goto out;
382 }
383 }
384 }
385 }
386 }
387 out:
388 udev_device_unref(dev);
389 safe_close(fd_signal);
390 safe_close(fd_ep);
391 close(fd_inotify);
392 close(worker_watch[WRITE_END]);
393 udev_rules_unref(rules);
394 udev_builtin_exit(udev);
395 udev_monitor_unref(worker_monitor);
396 udev_unref(udev);
397 log_close();
398 exit(rc);
399 }
400 case -1:
401 udev_monitor_unref(worker_monitor);
402 event->state = EVENT_QUEUED;
403 free(worker);
404 log_error("fork of child failed: %m");
405 break;
406 default:
407 /* close monitor, but keep address around */
408 udev_monitor_disconnect(worker_monitor);
409 worker->monitor = worker_monitor;
410 worker->pid = pid;
411 worker->state = WORKER_RUNNING;
412 worker->event_start_usec = now(CLOCK_MONOTONIC);
413 worker->event = event;
414 event->state = EVENT_RUNNING;
415 udev_list_node_append(&worker->node, &worker_list);
416 children++;
417 log_debug("seq %llu forked new worker [%u]", udev_device_get_seqnum(event->dev), pid);
418 break;
419 }
420 }
421
422 static void event_run(struct event *event)
423 {
424 struct udev_list_node *loop;
425
426 udev_list_node_foreach(loop, &worker_list) {
427 struct worker *worker = node_to_worker(loop);
428 ssize_t count;
429
430 if (worker->state != WORKER_IDLE)
431 continue;
432
433 count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
434 if (count < 0) {
435 log_error("worker [%u] did not accept message %zi (%m), kill it", worker->pid, count);
436 kill(worker->pid, SIGKILL);
437 worker->state = WORKER_KILLED;
438 continue;
439 }
440 worker_ref(worker);
441 worker->event = event;
442 worker->state = WORKER_RUNNING;
443 worker->event_start_usec = now(CLOCK_MONOTONIC);
444 event->state = EVENT_RUNNING;
445 return;
446 }
447
448 if (children >= children_max) {
449 if (children_max > 1)
450 log_debug("maximum number (%i) of children reached", children);
451 return;
452 }
453
454 /* start new worker and pass initial device */
455 worker_new(event);
456 }
457
458 static int event_queue_insert(struct udev_device *dev)
459 {
460 struct event *event;
461
462 event = new0(struct event, 1);
463 if (event == NULL)
464 return -1;
465
466 event->udev = udev_device_get_udev(dev);
467 event->dev = dev;
468 event->seqnum = udev_device_get_seqnum(dev);
469 event->devpath = udev_device_get_devpath(dev);
470 event->devpath_len = strlen(event->devpath);
471 event->devpath_old = udev_device_get_devpath_old(dev);
472 event->devnum = udev_device_get_devnum(dev);
473 event->is_block = streq("block", udev_device_get_subsystem(dev));
474 event->ifindex = udev_device_get_ifindex(dev);
475 #ifdef HAVE_FIRMWARE
476 if (streq(udev_device_get_subsystem(dev), "firmware"))
477 event->nodelay = true;
478 #endif
479
480 log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev),
481 udev_device_get_action(dev), udev_device_get_subsystem(dev));
482
483 event->state = EVENT_QUEUED;
484 udev_list_node_append(&event->node, &event_list);
485 return 0;
486 }
487
488 static void worker_kill(struct udev *udev)
489 {
490 struct udev_list_node *loop;
491
492 udev_list_node_foreach(loop, &worker_list) {
493 struct worker *worker = node_to_worker(loop);
494
495 if (worker->state == WORKER_KILLED)
496 continue;
497
498 worker->state = WORKER_KILLED;
499 kill(worker->pid, SIGTERM);
500 }
501 }
502
503 /* lookup event for identical, parent, child device */
504 static bool is_devpath_busy(struct event *event)
505 {
506 struct udev_list_node *loop;
507 size_t common;
508
509 /* check if queue contains events we depend on */
510 udev_list_node_foreach(loop, &event_list) {
511 struct event *loop_event = node_to_event(loop);
512
513 /* we already found a later event, earlier can not block us, no need to check again */
514 if (loop_event->seqnum < event->delaying_seqnum)
515 continue;
516
517 /* event we checked earlier still exists, no need to check again */
518 if (loop_event->seqnum == event->delaying_seqnum)
519 return true;
520
521 /* found ourself, no later event can block us */
522 if (loop_event->seqnum >= event->seqnum)
523 break;
524
525 /* check major/minor */
526 if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
527 return true;
528
529 /* check network device ifindex */
530 if (event->ifindex != 0 && event->ifindex == loop_event->ifindex)
531 return true;
532
533 /* check our old name */
534 if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
535 event->delaying_seqnum = loop_event->seqnum;
536 return true;
537 }
538
539 /* compare devpath */
540 common = MIN(loop_event->devpath_len, event->devpath_len);
541
542 /* one devpath is contained in the other? */
543 if (memcmp(loop_event->devpath, event->devpath, common) != 0)
544 continue;
545
546 /* identical device event found */
547 if (loop_event->devpath_len == event->devpath_len) {
548 /* devices names might have changed/swapped in the meantime */
549 if (major(event->devnum) != 0 && (event->devnum != loop_event->devnum || event->is_block != loop_event->is_block))
550 continue;
551 if (event->ifindex != 0 && event->ifindex != loop_event->ifindex)
552 continue;
553 event->delaying_seqnum = loop_event->seqnum;
554 return true;
555 }
556
557 #ifdef HAVE_FIRMWARE
558 /* allow to bypass the dependency tracking */
559 if (event->nodelay)
560 continue;
561 #endif
562
563 /* parent device event found */
564 if (event->devpath[common] == '/') {
565 event->delaying_seqnum = loop_event->seqnum;
566 return true;
567 }
568
569 /* child device event found */
570 if (loop_event->devpath[common] == '/') {
571 event->delaying_seqnum = loop_event->seqnum;
572 return true;
573 }
574
575 /* no matching device */
576 continue;
577 }
578
579 return false;
580 }
581
582 static void event_queue_start(struct udev *udev)
583 {
584 struct udev_list_node *loop;
585
586 udev_list_node_foreach(loop, &event_list) {
587 struct event *event = node_to_event(loop);
588
589 if (event->state != EVENT_QUEUED)
590 continue;
591
592 /* do not start event if parent or child event is still running */
593 if (is_devpath_busy(event))
594 continue;
595
596 event_run(event);
597 }
598 }
599
600 static void event_queue_cleanup(struct udev *udev, enum event_state match_type)
601 {
602 struct udev_list_node *loop, *tmp;
603
604 udev_list_node_foreach_safe(loop, tmp, &event_list) {
605 struct event *event = node_to_event(loop);
606
607 if (match_type != EVENT_UNDEF && match_type != event->state)
608 continue;
609
610 event_queue_delete(event);
611 }
612 }
613
614 static void worker_returned(int fd_worker)
615 {
616 for (;;) {
617 struct worker_message msg;
618 ssize_t size;
619 struct udev_list_node *loop;
620
621 size = recv(fd_worker, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
622 if (size != sizeof(struct worker_message))
623 break;
624
625 /* lookup worker who sent the signal */
626 udev_list_node_foreach(loop, &worker_list) {
627 struct worker *worker = node_to_worker(loop);
628
629 if (worker->pid != msg.pid)
630 continue;
631
632 /* worker returned */
633 if (worker->event) {
634 worker->event->exitcode = msg.exitcode;
635 event_queue_delete(worker->event);
636 worker->event = NULL;
637 }
638 if (worker->state != WORKER_KILLED)
639 worker->state = WORKER_IDLE;
640 worker_unref(worker);
641 break;
642 }
643 }
644 }
645
646 /* receive the udevd message from userspace */
647 static struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl)
648 {
649 struct udev *udev = udev_ctrl_get_udev(uctrl);
650 struct udev_ctrl_connection *ctrl_conn;
651 struct udev_ctrl_msg *ctrl_msg = NULL;
652 const char *str;
653 int i;
654
655 ctrl_conn = udev_ctrl_get_connection(uctrl);
656 if (ctrl_conn == NULL)
657 goto out;
658
659 ctrl_msg = udev_ctrl_receive_msg(ctrl_conn);
660 if (ctrl_msg == NULL)
661 goto out;
662
663 i = udev_ctrl_get_set_log_level(ctrl_msg);
664 if (i >= 0) {
665 log_debug("udevd message (SET_LOG_LEVEL) received, log_priority=%i", i);
666 log_set_max_level(i);
667 udev_set_log_priority(udev, i);
668 worker_kill(udev);
669 }
670
671 if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
672 log_debug("udevd message (STOP_EXEC_QUEUE) received");
673 stop_exec_queue = true;
674 }
675
676 if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
677 log_debug("udevd message (START_EXEC_QUEUE) received");
678 stop_exec_queue = false;
679 }
680
681 if (udev_ctrl_get_reload(ctrl_msg) > 0) {
682 log_debug("udevd message (RELOAD) received");
683 reload = true;
684 }
685
686 str = udev_ctrl_get_set_env(ctrl_msg);
687 if (str != NULL) {
688 char *key;
689
690 key = strdup(str);
691 if (key != NULL) {
692 char *val;
693
694 val = strchr(key, '=');
695 if (val != NULL) {
696 val[0] = '\0';
697 val = &val[1];
698 if (val[0] == '\0') {
699 log_debug("udevd message (ENV) received, unset '%s'", key);
700 udev_add_property(udev, key, NULL);
701 } else {
702 log_debug("udevd message (ENV) received, set '%s=%s'", key, val);
703 udev_add_property(udev, key, val);
704 }
705 } else {
706 log_error("wrong key format '%s'", key);
707 }
708 free(key);
709 }
710 worker_kill(udev);
711 }
712
713 i = udev_ctrl_get_set_children_max(ctrl_msg);
714 if (i >= 0) {
715 log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i", i);
716 children_max = i;
717 }
718
719 if (udev_ctrl_get_ping(ctrl_msg) > 0)
720 log_debug("udevd message (SYNC) received");
721
722 if (udev_ctrl_get_exit(ctrl_msg) > 0) {
723 log_debug("udevd message (EXIT) received");
724 udev_exit = true;
725 /* keep reference to block the client until we exit */
726 udev_ctrl_connection_ref(ctrl_conn);
727 }
728 out:
729 udev_ctrl_msg_unref(ctrl_msg);
730 return udev_ctrl_connection_unref(ctrl_conn);
731 }
732
733 static int synthesize_change(struct udev_device *dev) {
734 char filename[UTIL_PATH_SIZE];
735 int r;
736
737 if (streq_ptr("block", udev_device_get_subsystem(dev)) &&
738 streq_ptr("disk", udev_device_get_devtype(dev)) &&
739 !startswith(udev_device_get_sysname(dev), "dm-")) {
740 bool part_table_read = false;
741 bool has_partitions = false;
742 int fd;
743 struct udev *udev = udev_device_get_udev(dev);
744 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
745 struct udev_list_entry *item;
746
747 /*
748 * Try to re-read the partition table. This only succeeds if
749 * none of the devices is busy. The kernel returns 0 if no
750 * partition table is found, and we will not get an event for
751 * the disk.
752 */
753 fd = open(udev_device_get_devnode(dev), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
754 if (fd >= 0) {
755 r = flock(fd, LOCK_EX|LOCK_NB);
756 if (r >= 0)
757 r = ioctl(fd, BLKRRPART, 0);
758
759 close(fd);
760 if (r >= 0)
761 part_table_read = true;
762 }
763
764 /* search for partitions */
765 e = udev_enumerate_new(udev);
766 if (!e)
767 return -ENOMEM;
768
769 r = udev_enumerate_add_match_parent(e, dev);
770 if (r < 0)
771 return r;
772
773 r = udev_enumerate_add_match_subsystem(e, "block");
774 if (r < 0)
775 return r;
776
777 r = udev_enumerate_scan_devices(e);
778 if (r < 0)
779 return r;
780
781 udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
782 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
783
784 d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
785 if (!d)
786 continue;
787
788 if (!streq_ptr("partition", udev_device_get_devtype(d)))
789 continue;
790
791 has_partitions = true;
792 break;
793 }
794
795 /*
796 * We have partitions and re-read the table, the kernel already sent
797 * out a "change" event for the disk, and "remove/add" for all
798 * partitions.
799 */
800 if (part_table_read && has_partitions)
801 return 0;
802
803 /*
804 * We have partitions but re-reading the partition table did not
805 * work, synthesize "change" for the disk and all partitions.
806 */
807 log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
808 strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
809 write_string_file(filename, "change");
810
811 udev_list_entry_foreach(item, udev_enumerate_get_list_entry(e)) {
812 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
813
814 d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
815 if (!d)
816 continue;
817
818 if (!streq_ptr("partition", udev_device_get_devtype(d)))
819 continue;
820
821 log_debug("device %s closed, synthesising partition '%s' 'change'",
822 udev_device_get_devnode(dev), udev_device_get_devnode(d));
823 strscpyl(filename, sizeof(filename), udev_device_get_syspath(d), "/uevent", NULL);
824 write_string_file(filename, "change");
825 }
826
827 return 0;
828 }
829
830 log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
831 strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
832 write_string_file(filename, "change");
833
834 return 0;
835 }
836
837 static int handle_inotify(struct udev *udev)
838 {
839 int nbytes, pos;
840 char *buf;
841 struct inotify_event *ev;
842 int r;
843
844 r = ioctl(fd_inotify, FIONREAD, &nbytes);
845 if (r < 0 || nbytes <= 0)
846 return -errno;
847
848 buf = malloc(nbytes);
849 if (!buf) {
850 log_error("error getting buffer for inotify");
851 return -ENOMEM;
852 }
853
854 nbytes = read(fd_inotify, buf, nbytes);
855
856 for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
857 struct udev_device *dev;
858
859 ev = (struct inotify_event *)(buf + pos);
860 dev = udev_watch_lookup(udev, ev->wd);
861 if (!dev)
862 continue;
863
864 log_debug("inotify event: %x for %s", ev->mask, udev_device_get_devnode(dev));
865 if (ev->mask & IN_CLOSE_WRITE)
866 synthesize_change(dev);
867 else if (ev->mask & IN_IGNORED)
868 udev_watch_end(udev, dev);
869
870 udev_device_unref(dev);
871 }
872
873 free(buf);
874 return 0;
875 }
876
877 static void handle_signal(struct udev *udev, int signo)
878 {
879 switch (signo) {
880 case SIGINT:
881 case SIGTERM:
882 udev_exit = true;
883 break;
884 case SIGCHLD:
885 for (;;) {
886 pid_t pid;
887 int status;
888 struct udev_list_node *loop, *tmp;
889
890 pid = waitpid(-1, &status, WNOHANG);
891 if (pid <= 0)
892 break;
893
894 udev_list_node_foreach_safe(loop, tmp, &worker_list) {
895 struct worker *worker = node_to_worker(loop);
896
897 if (worker->pid != pid)
898 continue;
899 log_debug("worker [%u] exit", pid);
900
901 if (WIFEXITED(status)) {
902 if (WEXITSTATUS(status) != 0)
903 log_error("worker [%u] exit with return code %i",
904 pid, WEXITSTATUS(status));
905 } else if (WIFSIGNALED(status)) {
906 log_error("worker [%u] terminated by signal %i (%s)",
907 pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
908 } else if (WIFSTOPPED(status)) {
909 log_error("worker [%u] stopped", pid);
910 } else if (WIFCONTINUED(status)) {
911 log_error("worker [%u] continued", pid);
912 } else {
913 log_error("worker [%u] exit with status 0x%04x", pid, status);
914 }
915
916 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
917 if (worker->event) {
918 log_error("worker [%u] failed while handling '%s'",
919 pid, worker->event->devpath);
920 worker->event->exitcode = -32;
921 event_queue_delete(worker->event);
922
923 /* drop reference taken for state 'running' */
924 worker_unref(worker);
925 }
926 }
927 worker_unref(worker);
928 break;
929 }
930 }
931 break;
932 case SIGHUP:
933 reload = true;
934 break;
935 }
936 }
937
938 static int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink)
939 {
940 int ctrl = -1, netlink = -1;
941 int fd, n;
942
943 n = sd_listen_fds(true);
944 if (n <= 0)
945 return -1;
946
947 for (fd = SD_LISTEN_FDS_START; fd < n + SD_LISTEN_FDS_START; fd++) {
948 if (sd_is_socket(fd, AF_LOCAL, SOCK_SEQPACKET, -1)) {
949 if (ctrl >= 0)
950 return -1;
951 ctrl = fd;
952 continue;
953 }
954
955 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1)) {
956 if (netlink >= 0)
957 return -1;
958 netlink = fd;
959 continue;
960 }
961
962 return -1;
963 }
964
965 if (ctrl < 0 || netlink < 0)
966 return -1;
967
968 log_debug("ctrl=%i netlink=%i", ctrl, netlink);
969 *rctrl = ctrl;
970 *rnetlink = netlink;
971 return 0;
972 }
973
974 /*
975 * read the kernel commandline, in case we need to get into debug mode
976 * udev.log-priority=<level> syslog priority
977 * udev.children-max=<number of workers> events are fully serialized if set to 1
978 * udev.exec-delay=<number of seconds> delay execution of every executed program
979 */
980 static void kernel_cmdline_options(struct udev *udev)
981 {
982 _cleanup_free_ char *line = NULL;
983 char *w, *state;
984 size_t l;
985 int r;
986
987 r = proc_cmdline(&line);
988 if (r < 0)
989 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
990 if (r <= 0)
991 return;
992
993 FOREACH_WORD_QUOTED(w, l, line, state) {
994 char *s, *opt;
995
996 s = strndup(w, l);
997 if (!s)
998 break;
999
1000 /* accept the same options for the initrd, prefixed with "rd." */
1001 if (in_initrd() && startswith(s, "rd."))
1002 opt = s + 3;
1003 else
1004 opt = s;
1005
1006 if (startswith(opt, "udev.log-priority=")) {
1007 int prio;
1008
1009 prio = util_log_priority(opt + 18);
1010 log_set_max_level(prio);
1011 udev_set_log_priority(udev, prio);
1012 } else if (startswith(opt, "udev.children-max=")) {
1013 children_max = strtoul(opt + 18, NULL, 0);
1014 } else if (startswith(opt, "udev.exec-delay=")) {
1015 exec_delay = strtoul(opt + 16, NULL, 0);
1016 } else if (startswith(opt, "udev.event-timeout=")) {
1017 event_timeout = strtoul(opt + 16, NULL, 0);
1018 }
1019
1020 free(s);
1021 }
1022 }
1023
1024 int main(int argc, char *argv[])
1025 {
1026 struct udev *udev;
1027 sigset_t mask;
1028 int daemonize = false;
1029 int resolve_names = 1;
1030 static const struct option options[] = {
1031 { "daemon", no_argument, NULL, 'd' },
1032 { "debug", no_argument, NULL, 'D' },
1033 { "children-max", required_argument, NULL, 'c' },
1034 { "exec-delay", required_argument, NULL, 'e' },
1035 { "event-timeout", required_argument, NULL, 't' },
1036 { "resolve-names", required_argument, NULL, 'N' },
1037 { "help", no_argument, NULL, 'h' },
1038 { "version", no_argument, NULL, 'V' },
1039 {}
1040 };
1041 int fd_ctrl = -1;
1042 int fd_netlink = -1;
1043 int fd_worker = -1;
1044 struct epoll_event ep_ctrl, ep_inotify, ep_signal, ep_netlink, ep_worker;
1045 struct udev_ctrl_connection *ctrl_conn = NULL;
1046 int rc = 1;
1047
1048 udev = udev_new();
1049 if (udev == NULL)
1050 goto exit;
1051
1052 log_set_target(LOG_TARGET_AUTO);
1053 log_parse_environment();
1054 log_open();
1055
1056 udev_set_log_fn(udev, udev_main_log);
1057 log_set_max_level(udev_get_log_priority(udev));
1058
1059 log_debug("version %s", VERSION);
1060 label_init("/dev");
1061
1062 for (;;) {
1063 int option;
1064
1065 option = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL);
1066 if (option == -1)
1067 break;
1068
1069 switch (option) {
1070 case 'd':
1071 daemonize = true;
1072 break;
1073 case 'c':
1074 children_max = strtoul(optarg, NULL, 0);
1075 break;
1076 case 'e':
1077 exec_delay = strtoul(optarg, NULL, 0);
1078 break;
1079 case 't':
1080 event_timeout = strtoul(optarg, NULL, 0);
1081 break;
1082 case 'D':
1083 debug = true;
1084 log_set_max_level(LOG_DEBUG);
1085 udev_set_log_priority(udev, LOG_DEBUG);
1086 break;
1087 case 'N':
1088 if (streq(optarg, "early")) {
1089 resolve_names = 1;
1090 } else if (streq(optarg, "late")) {
1091 resolve_names = 0;
1092 } else if (streq(optarg, "never")) {
1093 resolve_names = -1;
1094 } else {
1095 fprintf(stderr, "resolve-names must be early, late or never\n");
1096 log_error("resolve-names must be early, late or never");
1097 goto exit;
1098 }
1099 break;
1100 case 'h':
1101 printf("Usage: udevd OPTIONS\n"
1102 " --daemon\n"
1103 " --debug\n"
1104 " --children-max=<maximum number of workers>\n"
1105 " --exec-delay=<seconds to wait before executing RUN=>\n"
1106 " --resolve-names=early|late|never\n"
1107 " --version\n"
1108 " --help\n"
1109 "\n");
1110 goto exit;
1111 case 'V':
1112 printf("%s\n", VERSION);
1113 goto exit;
1114 default:
1115 goto exit;
1116 }
1117 }
1118
1119 kernel_cmdline_options(udev);
1120
1121 if (getuid() != 0) {
1122 fprintf(stderr, "root privileges required\n");
1123 log_error("root privileges required");
1124 goto exit;
1125 }
1126
1127 /* set umask before creating any file/directory */
1128 chdir("/");
1129 umask(022);
1130
1131 mkdir("/run/udev", 0755);
1132
1133 dev_setup(NULL);
1134
1135 /* before opening new files, make sure std{in,out,err} fds are in a sane state */
1136 if (daemonize) {
1137 int fd;
1138
1139 fd = open("/dev/null", O_RDWR);
1140 if (fd >= 0) {
1141 if (write(STDOUT_FILENO, 0, 0) < 0)
1142 dup2(fd, STDOUT_FILENO);
1143 if (write(STDERR_FILENO, 0, 0) < 0)
1144 dup2(fd, STDERR_FILENO);
1145 if (fd > STDERR_FILENO)
1146 close(fd);
1147 } else {
1148 fprintf(stderr, "cannot open /dev/null\n");
1149 log_error("cannot open /dev/null");
1150 }
1151 }
1152
1153 if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
1154 /* get control and netlink socket from systemd */
1155 udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
1156 if (udev_ctrl == NULL) {
1157 log_error("error taking over udev control socket");
1158 rc = 1;
1159 goto exit;
1160 }
1161
1162 monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink);
1163 if (monitor == NULL) {
1164 log_error("error taking over netlink socket");
1165 rc = 3;
1166 goto exit;
1167 }
1168
1169 /* get our own cgroup, we regularly kill everything udev has left behind */
1170 if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0)
1171 udev_cgroup = NULL;
1172 } else {
1173 /* open control and netlink socket */
1174 udev_ctrl = udev_ctrl_new(udev);
1175 if (udev_ctrl == NULL) {
1176 fprintf(stderr, "error initializing udev control socket");
1177 log_error("error initializing udev control socket");
1178 rc = 1;
1179 goto exit;
1180 }
1181 fd_ctrl = udev_ctrl_get_fd(udev_ctrl);
1182
1183 monitor = udev_monitor_new_from_netlink(udev, "kernel");
1184 if (monitor == NULL) {
1185 fprintf(stderr, "error initializing netlink socket\n");
1186 log_error("error initializing netlink socket");
1187 rc = 3;
1188 goto exit;
1189 }
1190 fd_netlink = udev_monitor_get_fd(monitor);
1191 }
1192
1193 if (udev_monitor_enable_receiving(monitor) < 0) {
1194 fprintf(stderr, "error binding netlink socket\n");
1195 log_error("error binding netlink socket");
1196 rc = 3;
1197 goto exit;
1198 }
1199
1200 if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
1201 fprintf(stderr, "error binding udev control socket\n");
1202 log_error("error binding udev control socket");
1203 rc = 1;
1204 goto exit;
1205 }
1206
1207 udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024);
1208
1209 if (daemonize) {
1210 pid_t pid;
1211
1212 pid = fork();
1213 switch (pid) {
1214 case 0:
1215 break;
1216 case -1:
1217 log_error("fork of daemon failed: %m");
1218 rc = 4;
1219 goto exit;
1220 default:
1221 rc = EXIT_SUCCESS;
1222 goto exit_daemonize;
1223 }
1224
1225 setsid();
1226
1227 write_string_file("/proc/self/oom_score_adj", "-1000");
1228 } else {
1229 sd_notify(1, "READY=1");
1230 }
1231
1232 print_kmsg("starting version " VERSION "\n");
1233
1234 if (!debug) {
1235 int fd;
1236
1237 fd = open("/dev/null", O_RDWR);
1238 if (fd >= 0) {
1239 dup2(fd, STDIN_FILENO);
1240 dup2(fd, STDOUT_FILENO);
1241 dup2(fd, STDERR_FILENO);
1242 close(fd);
1243 }
1244 }
1245
1246 fd_inotify = udev_watch_init(udev);
1247 if (fd_inotify < 0) {
1248 fprintf(stderr, "error initializing inotify\n");
1249 log_error("error initializing inotify");
1250 rc = 4;
1251 goto exit;
1252 }
1253 udev_watch_restore(udev);
1254
1255 /* block and listen to all signals on signalfd */
1256 sigfillset(&mask);
1257 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
1258 fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
1259 if (fd_signal < 0) {
1260 fprintf(stderr, "error creating signalfd\n");
1261 log_error("error creating signalfd");
1262 rc = 5;
1263 goto exit;
1264 }
1265
1266 /* unnamed socket from workers to the main daemon */
1267 if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
1268 fprintf(stderr, "error creating socketpair\n");
1269 log_error("error creating socketpair");
1270 rc = 6;
1271 goto exit;
1272 }
1273 fd_worker = worker_watch[READ_END];
1274
1275 udev_builtin_init(udev);
1276
1277 rules = udev_rules_new(udev, resolve_names);
1278 if (rules == NULL) {
1279 log_error("error reading rules");
1280 goto exit;
1281 }
1282
1283 memzero(&ep_ctrl, sizeof(struct epoll_event));
1284 ep_ctrl.events = EPOLLIN;
1285 ep_ctrl.data.fd = fd_ctrl;
1286
1287 memzero(&ep_inotify, sizeof(struct epoll_event));
1288 ep_inotify.events = EPOLLIN;
1289 ep_inotify.data.fd = fd_inotify;
1290
1291 memzero(&ep_signal, sizeof(struct epoll_event));
1292 ep_signal.events = EPOLLIN;
1293 ep_signal.data.fd = fd_signal;
1294
1295 memzero(&ep_netlink, sizeof(struct epoll_event));
1296 ep_netlink.events = EPOLLIN;
1297 ep_netlink.data.fd = fd_netlink;
1298
1299 memzero(&ep_worker, sizeof(struct epoll_event));
1300 ep_worker.events = EPOLLIN;
1301 ep_worker.data.fd = fd_worker;
1302
1303 fd_ep = epoll_create1(EPOLL_CLOEXEC);
1304 if (fd_ep < 0) {
1305 log_error("error creating epoll fd: %m");
1306 goto exit;
1307 }
1308 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 ||
1309 epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_inotify, &ep_inotify) < 0 ||
1310 epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
1311 epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 ||
1312 epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) {
1313 log_error("fail to add fds to epoll: %m");
1314 goto exit;
1315 }
1316
1317 if (children_max <= 0) {
1318 cpu_set_t cpu_set;
1319
1320 children_max = 8;
1321
1322 if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) {
1323 children_max += CPU_COUNT(&cpu_set) * 2;
1324 }
1325 }
1326 log_debug("set children_max to %u", children_max);
1327
1328 rc = udev_rules_apply_static_dev_perms(rules);
1329 if (rc < 0)
1330 log_error("failed to apply permissions on static device nodes - %s", strerror(-rc));
1331
1332 udev_list_node_init(&event_list);
1333 udev_list_node_init(&worker_list);
1334
1335 for (;;) {
1336 static usec_t last_usec;
1337 struct epoll_event ev[8];
1338 int fdcount;
1339 int timeout;
1340 bool is_worker, is_signal, is_inotify, is_netlink, is_ctrl;
1341 int i;
1342
1343 if (udev_exit) {
1344 /* close sources of new events and discard buffered events */
1345 if (fd_ctrl >= 0) {
1346 epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_ctrl, NULL);
1347 fd_ctrl = -1;
1348 }
1349 if (monitor != NULL) {
1350 epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_netlink, NULL);
1351 udev_monitor_unref(monitor);
1352 monitor = NULL;
1353 }
1354 if (fd_inotify >= 0) {
1355 epoll_ctl(fd_ep, EPOLL_CTL_DEL, fd_inotify, NULL);
1356 close(fd_inotify);
1357 fd_inotify = -1;
1358 }
1359
1360 /* discard queued events and kill workers */
1361 event_queue_cleanup(udev, EVENT_QUEUED);
1362 worker_kill(udev);
1363
1364 /* exit after all has cleaned up */
1365 if (udev_list_node_is_empty(&event_list) && children == 0)
1366 break;
1367
1368 /* timeout at exit for workers to finish */
1369 timeout = 30 * MSEC_PER_SEC;
1370 } else if (udev_list_node_is_empty(&event_list) && children == 0) {
1371 /* we are idle */
1372 timeout = -1;
1373
1374 /* cleanup possible left-over processes in our cgroup */
1375 if (udev_cgroup)
1376 cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL);
1377 } else {
1378 /* kill idle or hanging workers */
1379 timeout = 3 * MSEC_PER_SEC;
1380 }
1381
1382 /* tell settle that we are busy or idle */
1383 if (!udev_list_node_is_empty(&event_list)) {
1384 int fd;
1385
1386 fd = open("/run/udev/queue", O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
1387 if (fd >= 0)
1388 close(fd);
1389 } else {
1390 unlink("/run/udev/queue");
1391 }
1392
1393 fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
1394 if (fdcount < 0)
1395 continue;
1396
1397 if (fdcount == 0) {
1398 struct udev_list_node *loop;
1399
1400 /* timeout */
1401 if (udev_exit) {
1402 log_error("timeout, giving up waiting for workers to finish");
1403 break;
1404 }
1405
1406 /* kill idle workers */
1407 if (udev_list_node_is_empty(&event_list)) {
1408 log_debug("cleanup idle workers");
1409 worker_kill(udev);
1410 }
1411
1412 /* check for hanging events */
1413 udev_list_node_foreach(loop, &worker_list) {
1414 struct worker *worker = node_to_worker(loop);
1415
1416 if (worker->state != WORKER_RUNNING)
1417 continue;
1418
1419 if ((now(CLOCK_MONOTONIC) - worker->event_start_usec) > event_timeout * USEC_PER_SEC) {
1420 log_error("worker [%u] %s timeout; kill it", worker->pid,
1421 worker->event ? worker->event->devpath : "<idle>");
1422 kill(worker->pid, SIGKILL);
1423 worker->state = WORKER_KILLED;
1424
1425 /* drop reference taken for state 'running' */
1426 worker_unref(worker);
1427 if (worker->event) {
1428 log_error("seq %llu '%s' killed", udev_device_get_seqnum(worker->event->dev), worker->event->devpath);
1429 worker->event->exitcode = -64;
1430 event_queue_delete(worker->event);
1431 worker->event = NULL;
1432 }
1433 }
1434 }
1435
1436 }
1437
1438 is_worker = is_signal = is_inotify = is_netlink = is_ctrl = false;
1439 for (i = 0; i < fdcount; i++) {
1440 if (ev[i].data.fd == fd_worker && ev[i].events & EPOLLIN)
1441 is_worker = true;
1442 else if (ev[i].data.fd == fd_netlink && ev[i].events & EPOLLIN)
1443 is_netlink = true;
1444 else if (ev[i].data.fd == fd_signal && ev[i].events & EPOLLIN)
1445 is_signal = true;
1446 else if (ev[i].data.fd == fd_inotify && ev[i].events & EPOLLIN)
1447 is_inotify = true;
1448 else if (ev[i].data.fd == fd_ctrl && ev[i].events & EPOLLIN)
1449 is_ctrl = true;
1450 }
1451
1452 /* check for changed config, every 3 seconds at most */
1453 if ((now(CLOCK_MONOTONIC) - last_usec) > 3 * USEC_PER_SEC) {
1454 if (udev_rules_check_timestamp(rules))
1455 reload = true;
1456 if (udev_builtin_validate(udev))
1457 reload = true;
1458
1459 last_usec = now(CLOCK_MONOTONIC);
1460 }
1461
1462 /* reload requested, HUP signal received, rules changed, builtin changed */
1463 if (reload) {
1464 worker_kill(udev);
1465 rules = udev_rules_unref(rules);
1466 udev_builtin_exit(udev);
1467 reload = false;
1468 }
1469
1470 /* event has finished */
1471 if (is_worker)
1472 worker_returned(fd_worker);
1473
1474 if (is_netlink) {
1475 struct udev_device *dev;
1476
1477 dev = udev_monitor_receive_device(monitor);
1478 if (dev != NULL) {
1479 udev_device_set_usec_initialized(dev, now(CLOCK_MONOTONIC));
1480 if (event_queue_insert(dev) < 0)
1481 udev_device_unref(dev);
1482 }
1483 }
1484
1485 /* start new events */
1486 if (!udev_list_node_is_empty(&event_list) && !udev_exit && !stop_exec_queue) {
1487 udev_builtin_init(udev);
1488 if (rules == NULL)
1489 rules = udev_rules_new(udev, resolve_names);
1490 if (rules != NULL)
1491 event_queue_start(udev);
1492 }
1493
1494 if (is_signal) {
1495 struct signalfd_siginfo fdsi;
1496 ssize_t size;
1497
1498 size = read(fd_signal, &fdsi, sizeof(struct signalfd_siginfo));
1499 if (size == sizeof(struct signalfd_siginfo))
1500 handle_signal(udev, fdsi.ssi_signo);
1501 }
1502
1503 /* we are shutting down, the events below are not handled anymore */
1504 if (udev_exit)
1505 continue;
1506
1507 /* device node watch */
1508 if (is_inotify)
1509 handle_inotify(udev);
1510
1511 /*
1512 * This needs to be after the inotify handling, to make sure,
1513 * that the ping is send back after the possibly generated
1514 * "change" events by the inotify device node watch.
1515 *
1516 * A single time we may receive a client connection which we need to
1517 * keep open to block the client. It will be closed right before we
1518 * exit.
1519 */
1520 if (is_ctrl)
1521 ctrl_conn = handle_ctrl_msg(udev_ctrl);
1522 }
1523
1524 rc = EXIT_SUCCESS;
1525 exit:
1526 udev_ctrl_cleanup(udev_ctrl);
1527 unlink("/run/udev/queue");
1528 exit_daemonize:
1529 if (fd_ep >= 0)
1530 close(fd_ep);
1531 worker_list_cleanup(udev);
1532 event_queue_cleanup(udev, EVENT_UNDEF);
1533 udev_rules_unref(rules);
1534 udev_builtin_exit(udev);
1535 if (fd_signal >= 0)
1536 close(fd_signal);
1537 if (worker_watch[READ_END] >= 0)
1538 close(worker_watch[READ_END]);
1539 if (worker_watch[WRITE_END] >= 0)
1540 close(worker_watch[WRITE_END]);
1541 udev_monitor_unref(monitor);
1542 udev_ctrl_connection_unref(ctrl_conn);
1543 udev_ctrl_unref(udev_ctrl);
1544 label_finish();
1545 udev_unref(udev);
1546 log_close();
1547 return rc;
1548 }