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