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