2 * Copyright (C) 2004-2009 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>
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.
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.
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/>.
35 #include <sys/prctl.h>
36 #include <sys/socket.h>
37 #include <sys/signalfd.h>
38 #include <sys/select.h>
42 #include <sys/ioctl.h>
43 #include <sys/inotify.h>
47 #define UDEVD_PRIORITY -4
48 #define UDEV_PRIORITY -2
52 static void log_fn(struct udev *udev, int priority,
53 const char *file, int line, const char *fn,
54 const char *format, va_list args)
61 vsnprintf(buf, sizeof(buf), format, args);
62 gettimeofday(&tv, &tz);
63 fprintf(stderr, "%llu.%06u [%u] %s: %s",
64 (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec,
65 (int) getpid(), fn, buf);
67 vsyslog(priority, format, args);
71 static bool debug_trace;
72 static struct udev_rules *rules;
73 static struct udev_queue_export *udev_queue_export;
74 static struct udev_ctrl *udev_ctrl;
75 static struct udev_monitor *monitor;
76 static int worker_watch[2];
77 static pid_t settle_pid;
78 static bool stop_exec_queue;
79 static bool reload_config;
80 static int max_childs;
82 static struct udev_list_node event_list;
83 static struct udev_list_node worker_list;
84 static bool udev_exit;
85 static volatile sig_atomic_t worker_exit;
95 static struct pollfd pfd[] = {
96 [FD_NETLINK] = { .events = POLLIN },
97 [FD_WORKER] = { .events = POLLIN },
98 [FD_SIGNAL] = { .events = POLLIN },
99 [FD_INOTIFY] = { .events = POLLIN },
100 [FD_CONTROL] = { .events = POLLIN },
110 struct udev_list_node node;
112 struct udev_device *dev;
113 enum event_state state;
115 unsigned long long int delaying_seqnum;
116 unsigned long long int seqnum;
119 const char *devpath_old;
122 static struct event *node_to_event(struct udev_list_node *node)
126 event = (char *)node;
127 event -= offsetof(struct event, node);
128 return (struct event *)event;
139 struct udev_list_node node;
143 struct udev_monitor *monitor;
144 enum worker_state state;
148 /* passed from worker to main process */
149 struct worker_message {
154 static struct worker *node_to_worker(struct udev_list_node *node)
158 worker = (char *)node;
159 worker -= offsetof(struct worker, node);
160 return (struct worker *)worker;
163 static void event_queue_delete(struct event *event)
165 udev_list_node_remove(&event->node);
167 /* mark as failed, if "add" event returns non-zero */
168 if (event->exitcode != 0 && strcmp(udev_device_get_action(event->dev), "add") == 0)
169 udev_queue_export_device_failed(udev_queue_export, event->dev);
171 udev_queue_export_device_finished(udev_queue_export, event->dev);
173 info(event->udev, "seq %llu done with %i\n", udev_device_get_seqnum(event->dev), event->exitcode);
174 udev_device_unref(event->dev);
178 static void event_sig_handler(int signum)
190 static struct worker *worker_ref(struct worker *worker)
196 static void worker_unref(struct worker *worker)
199 if (worker->refcount > 0)
202 udev_list_node_remove(&worker->node);
203 udev_monitor_unref(worker->monitor);
205 info(worker->udev, "worker [%u] cleaned up\n", worker->pid);
209 static void worker_new(struct event *event)
211 struct worker *worker;
212 struct udev_monitor *worker_monitor;
214 struct sigaction act;
216 /* listen for new events */
217 worker_monitor = udev_monitor_new_from_netlink(event->udev, NULL);
218 if (worker_monitor == NULL)
220 /* allow the main daemon netlink address to send devices to the worker */
221 udev_monitor_allow_unicast_sender(worker_monitor, monitor);
222 udev_monitor_enable_receiving(worker_monitor);
223 util_set_fd_cloexec(udev_monitor_get_fd(worker_monitor));
225 worker = calloc(1, sizeof(struct worker));
228 /* worker + event reference */
229 worker->refcount = 2;
230 worker->udev = event->udev;
236 struct udev_device *dev;
237 struct pollfd pmon = {
238 .fd = udev_monitor_get_fd(worker_monitor),
242 udev_queue_export_unref(udev_queue_export);
243 udev_monitor_unref(monitor);
244 udev_ctrl_unref(udev_ctrl);
245 close(pfd[FD_SIGNAL].fd);
246 close(worker_watch[READ_END]);
248 udev_log_init("udevd-work");
249 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
251 /* set signal handlers */
252 memset(&act, 0x00, sizeof(act));
253 act.sa_handler = event_sig_handler;
254 sigemptyset (&act.sa_mask);
256 sigaction(SIGTERM, &act, NULL);
257 sigaction(SIGALRM, &act, NULL);
259 /* unblock SIGALRM */
260 sigfillset(&sigmask);
261 sigdelset(&sigmask, SIGALRM);
262 sigprocmask(SIG_SETMASK, &sigmask, NULL);
263 /* SIGTERM is unblocked in ppoll() */
264 sigdelset(&sigmask, SIGTERM);
266 /* request TERM signal if parent exits */
267 prctl(PR_SET_PDEATHSIG, SIGTERM);
273 struct udev_event *udev_event;
274 struct worker_message msg = {};
278 info(event->udev, "seq %llu running\n", udev_device_get_seqnum(dev));
279 udev_event = udev_event_new(dev);
280 if (udev_event == NULL)
283 /* set timeout to prevent hanging processes */
284 alarm(UDEV_EVENT_TIMEOUT);
286 /* apply rules, create node, symlinks */
287 err = udev_event_execute_rules(udev_event, rules);
289 /* rules may change/disable the timeout */
290 if (udev_device_get_event_timeout(dev) >= 0)
291 alarm(udev_device_get_event_timeout(dev));
294 if (err == 0 && !udev_event->ignore_device && udev_get_run(udev_event->udev))
295 failed = udev_event_execute_run(udev_event);
300 /* apply/restore inotify watch */
301 if (err == 0 && udev_event->inotify_watch) {
302 udev_watch_begin(udev_event->udev, dev);
303 udev_device_update_db(dev);
306 /* send processed event back to libudev listeners */
307 udev_monitor_send_device(worker_monitor, NULL, dev);
309 /* send back the result of the event execution */
312 else if (failed != 0)
313 msg.exitcode = failed;
315 send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
317 info(event->udev, "seq %llu processed with %i\n", udev_device_get_seqnum(dev), err);
318 udev_event_unref(udev_event);
319 udev_device_unref(dev);
322 /* wait for more device messages or signal from udevd */
323 while (!worker_exit) {
326 fdcount = ppoll(&pmon, 1, NULL, &sigmask);
330 if (pmon.revents & POLLIN) {
331 dev = udev_monitor_receive_device(worker_monitor);
336 } while (dev != NULL);
338 udev_monitor_unref(worker_monitor);
343 udev_monitor_unref(worker_monitor);
344 event->state = EVENT_QUEUED;
346 err(event->udev, "fork of child failed: %m\n");
349 /* close monitor, but keep address around */
350 udev_monitor_disconnect(worker_monitor);
351 worker->monitor = worker_monitor;
353 worker->state = WORKER_RUNNING;
354 worker->event = event;
355 event->state = EVENT_RUNNING;
356 udev_list_node_append(&worker->node, &worker_list);
358 info(event->udev, "seq %llu forked new worker [%u]\n", udev_device_get_seqnum(event->dev), pid);
363 static void event_run(struct event *event)
365 struct udev_list_node *loop;
367 udev_list_node_foreach(loop, &worker_list) {
368 struct worker *worker = node_to_worker(loop);
371 if (worker->state != WORKER_IDLE)
374 count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
376 err(event->udev, "worker [%u] did not accept message %zi (%m), kill it\n", worker->pid, count);
377 kill(worker->pid, SIGKILL);
378 worker->state = WORKER_KILLED;
382 worker->event = event;
383 worker->state = WORKER_RUNNING;
384 event->state = EVENT_RUNNING;
388 if (childs >= max_childs) {
389 info(event->udev, "maximum number (%i) of childs reached\n", childs);
393 /* start new worker and pass initial device */
397 static void event_queue_insert(struct udev_device *dev)
401 event = calloc(1, sizeof(struct event));
405 event->udev = udev_device_get_udev(dev);
407 event->seqnum = udev_device_get_seqnum(dev);
408 event->devpath = udev_device_get_devpath(dev);
409 event->devpath_len = strlen(event->devpath);
410 event->devpath_old = udev_device_get_devpath_old(dev);
412 udev_queue_export_device_queued(udev_queue_export, dev);
413 info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
414 udev_device_get_action(dev), udev_device_get_subsystem(dev));
416 event->state = EVENT_QUEUED;
417 udev_list_node_append(&event->node, &event_list);
419 /* run all events with a timeout set immediately */
420 if (udev_device_get_timeout(dev) > 0) {
426 static void worker_kill(struct udev *udev, int retain)
428 struct udev_list_node *loop;
431 if (childs <= retain)
434 max = childs - retain;
436 udev_list_node_foreach(loop, &worker_list) {
437 struct worker *worker = node_to_worker(loop);
442 if (worker->state == WORKER_KILLED)
445 worker->state = WORKER_KILLED;
446 kill(worker->pid, SIGTERM);
450 static int mem_size_mb(void)
454 long int memsize = -1;
456 f = fopen("/proc/meminfo", "r");
460 while (fgets(buf, sizeof(buf), f) != NULL) {
463 if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
464 memsize = value / 1024;
473 /* lookup event for identical, parent, child device */
474 static int devpath_busy(struct event *event)
476 struct udev_list_node *loop;
479 /* check if queue contains events we depend on */
480 udev_list_node_foreach(loop, &event_list) {
481 struct event *loop_event = node_to_event(loop);
483 /* we already found a later event, earlier can not block us, no need to check again */
484 if (loop_event->seqnum < event->delaying_seqnum)
487 /* event we checked earlier still exists, no need to check again */
488 if (loop_event->seqnum == event->delaying_seqnum)
491 /* found ourself, no later event can block us */
492 if (loop_event->seqnum >= event->seqnum)
495 /* check our old name */
496 if (event->devpath_old != NULL)
497 if (strcmp(loop_event->devpath, event->devpath_old) == 0) {
498 event->delaying_seqnum = loop_event->seqnum;
502 /* compare devpath */
503 common = MIN(loop_event->devpath_len, event->devpath_len);
505 /* one devpath is contained in the other? */
506 if (memcmp(loop_event->devpath, event->devpath, common) != 0)
509 /* identical device event found */
510 if (loop_event->devpath_len == event->devpath_len) {
511 event->delaying_seqnum = loop_event->seqnum;
515 /* parent device event found */
516 if (event->devpath[common] == '/') {
517 event->delaying_seqnum = loop_event->seqnum;
521 /* child device event found */
522 if (loop_event->devpath[common] == '/') {
523 event->delaying_seqnum = loop_event->seqnum;
527 /* no matching device */
534 static void events_start(struct udev *udev)
536 struct udev_list_node *loop;
538 udev_list_node_foreach(loop, &event_list) {
539 struct event *event = node_to_event(loop);
541 if (event->state != EVENT_QUEUED)
544 /* do not start event if parent or child event is still running */
545 if (devpath_busy(event) != 0) {
546 dbg(udev, "delay seq %llu (%s)\n", event->seqnum, event->devpath);
554 static void worker_returned(void)
557 struct worker_message msg;
559 struct udev_list_node *loop;
561 size = recv(pfd[FD_WORKER].fd, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
562 if (size != sizeof(struct worker_message))
565 /* lookup worker who sent the signal */
566 udev_list_node_foreach(loop, &worker_list) {
567 struct worker *worker = node_to_worker(loop);
569 if (worker->pid != msg.pid)
572 /* worker returned */
573 worker->event->exitcode = msg.exitcode;
574 event_queue_delete(worker->event);
575 worker->event = NULL;
576 if (worker->state != WORKER_KILLED)
577 worker->state = WORKER_IDLE;
578 worker_unref(worker);
584 /* receive the udevd message from userspace */
585 static void handle_ctrl_msg(struct udev_ctrl *uctrl)
587 struct udev *udev = udev_ctrl_get_udev(uctrl);
588 struct udev_ctrl_msg *ctrl_msg;
592 ctrl_msg = udev_ctrl_receive_msg(uctrl);
593 if (ctrl_msg == NULL)
596 i = udev_ctrl_get_set_log_level(ctrl_msg);
598 info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
599 udev_set_log_priority(udev, i);
600 worker_kill(udev, 0);
603 if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
604 info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
605 stop_exec_queue = true;
608 if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
609 info(udev, "udevd message (START_EXEC_QUEUE) received\n");
610 stop_exec_queue = false;
613 if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
614 info(udev, "udevd message (RELOAD_RULES) received\n");
615 reload_config = true;
618 str = udev_ctrl_get_set_env(ctrl_msg);
626 val = strchr(key, '=');
630 if (val[0] == '\0') {
631 info(udev, "udevd message (ENV) received, unset '%s'\n", key);
632 udev_add_property(udev, key, NULL);
634 info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
635 udev_add_property(udev, key, val);
638 err(udev, "wrong key format '%s'\n", key);
642 worker_kill(udev, 0);
645 i = udev_ctrl_get_set_max_childs(ctrl_msg);
647 info(udev, "udevd message (SET_MAX_CHILDS) received, max_childs=%i\n", i);
651 settle_pid = udev_ctrl_get_settle(ctrl_msg);
652 if (settle_pid > 0) {
653 info(udev, "udevd message (SETTLE) received\n");
654 kill(settle_pid, SIGUSR1);
657 udev_ctrl_msg_unref(ctrl_msg);
660 /* read inotify messages */
661 static int handle_inotify(struct udev *udev)
665 struct inotify_event *ev;
667 if ((ioctl(pfd[FD_INOTIFY].fd, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
670 buf = malloc(nbytes);
672 err(udev, "error getting buffer for inotify\n");
676 nbytes = read(pfd[FD_INOTIFY].fd, buf, nbytes);
678 for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
679 struct udev_device *dev;
681 ev = (struct inotify_event *)(buf + pos);
683 dbg(udev, "inotify event: %x for %s\n", ev->mask, ev->name);
684 reload_config = true;
688 dev = udev_watch_lookup(udev, ev->wd);
690 dbg(udev, "inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
691 if (ev->mask & IN_CLOSE_WRITE) {
692 char filename[UTIL_PATH_SIZE];
695 info(udev, "device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
696 util_strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
697 fd = open(filename, O_WRONLY);
698 if (fd < 0 || write(fd, "change", 6) < 0)
699 info(udev, "error writing uevent: %m\n");
702 if (ev->mask & IN_IGNORED)
703 udev_watch_end(udev, dev);
705 udev_device_unref(dev);
714 static void handle_signal(struct udev *udev, int signo)
725 struct udev_list_node *loop, *tmp;
727 pid = waitpid(-1, &status, WNOHANG);
731 udev_list_node_foreach_safe(loop, tmp, &worker_list) {
732 struct worker *worker = node_to_worker(loop);
734 if (worker->pid != pid)
737 info(udev, "worker [%u] exit\n", pid);
738 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
739 err(udev, "worker [%u] unexpectedly returned with status 0x%04x\n", pid, status);
740 if (worker->event != NULL) {
741 err(udev, "worker [%u] failed while handling '%s'\n", pid, worker->event->devpath);
742 worker->event->exitcode = -32;
743 event_queue_delete(worker->event);
744 /* drop reference from running event */
745 worker_unref(worker);
748 worker_unref(worker);
754 reload_config = true;
759 static void startup_log(struct udev *udev)
762 char path[UTIL_PATH_SIZE];
765 f = fopen("/dev/kmsg", "w");
767 fprintf(f, "<6>udev: starting version " VERSION "\n");
769 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev), "/class/mem/null", NULL);
770 if (lstat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
771 const char *depr_str =
772 "udev: missing sysfs features; please update the kernel "
773 "or disable the kernel's CONFIG_SYSFS_DEPRECATED option; "
774 "udev may fail to work correctly";
777 fprintf(f, "<3>%s\n", depr_str);
778 err(udev, "%s\n", depr_str);
786 int main(int argc, char *argv[])
792 int daemonize = false;
793 int resolve_names = 1;
794 static const struct option options[] = {
795 { "daemon", no_argument, NULL, 'd' },
796 { "debug-trace", no_argument, NULL, 't' },
797 { "debug", no_argument, NULL, 'D' },
798 { "help", no_argument, NULL, 'h' },
799 { "version", no_argument, NULL, 'V' },
800 { "resolve-names", required_argument, NULL, 'N' },
809 udev_log_init("udevd");
810 udev_set_log_fn(udev, log_fn);
811 info(udev, "version %s\n", VERSION);
812 udev_selinux_init(udev);
817 option = getopt_long(argc, argv, "dDthV", options, NULL);
830 if (udev_get_log_priority(udev) < LOG_INFO)
831 udev_set_log_priority(udev, LOG_INFO);
834 if (strcmp (optarg, "early") == 0) {
836 } else if (strcmp (optarg, "late") == 0) {
838 } else if (strcmp (optarg, "never") == 0) {
841 fprintf(stderr, "resolve-names must be early, late or never\n");
842 err(udev, "resolve-names must be early, late or never\n");
847 printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--debug] "
848 "[--resolve-names=early|late|never] [--version]\n");
851 printf("%s\n", VERSION);
859 fprintf(stderr, "root privileges required\n");
860 err(udev, "root privileges required\n");
864 /* make sure std{in,out,err} fd's are in a sane state */
865 fd = open("/dev/null", O_RDWR);
867 fprintf(stderr, "cannot open /dev/null\n");
868 err(udev, "cannot open /dev/null\n");
870 if (write(STDOUT_FILENO, 0, 0) < 0)
871 dup2(fd, STDOUT_FILENO);
872 if (write(STDERR_FILENO, 0, 0) < 0)
873 dup2(fd, STDERR_FILENO);
875 /* init control socket, bind() ensures, that only one udevd instance is running */
876 udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
877 if (udev_ctrl == NULL) {
878 fprintf(stderr, "error initializing control socket");
879 err(udev, "error initializing udevd socket");
883 if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
884 fprintf(stderr, "error binding control socket, seems udevd is already running\n");
885 err(udev, "error binding control socket, seems udevd is already running\n");
889 pfd[FD_CONTROL].fd = udev_ctrl_get_fd(udev_ctrl);
891 monitor = udev_monitor_new_from_netlink(udev, "kernel");
892 if (monitor == NULL || udev_monitor_enable_receiving(monitor) < 0) {
893 fprintf(stderr, "error initializing netlink socket\n");
894 err(udev, "error initializing netlink socket\n");
898 udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
899 pfd[FD_NETLINK].fd = udev_monitor_get_fd(monitor);
901 pfd[FD_INOTIFY].fd = udev_watch_init(udev);
902 if (pfd[FD_INOTIFY].fd < 0) {
903 fprintf(stderr, "error initializing inotify\n");
904 err(udev, "error initializing inotify\n");
909 if (udev_get_rules_path(udev) != NULL) {
910 inotify_add_watch(pfd[FD_INOTIFY].fd, udev_get_rules_path(udev),
911 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
913 char filename[UTIL_PATH_SIZE];
915 inotify_add_watch(pfd[FD_INOTIFY].fd, LIBEXECDIR "/rules.d",
916 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
917 inotify_add_watch(pfd[FD_INOTIFY].fd, SYSCONFDIR "/udev/rules.d",
918 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
920 /* watch dynamic rules directory */
921 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL);
922 inotify_add_watch(pfd[FD_INOTIFY].fd, filename,
923 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
925 udev_watch_restore(udev);
927 /* block and listen to all signals on signalfd */
929 sigprocmask(SIG_SETMASK, &mask, NULL);
930 pfd[FD_SIGNAL].fd = signalfd(-1, &mask, 0);
931 if (pfd[FD_SIGNAL].fd < 0) {
932 fprintf(stderr, "error getting signalfd\n");
933 err(udev, "error getting signalfd\n");
938 /* unnamed socket from workers to the main daemon */
939 if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, worker_watch) < 0) {
940 fprintf(stderr, "error getting socketpair\n");
941 err(udev, "error getting socketpair\n");
945 pfd[FD_WORKER].fd = worker_watch[READ_END];
946 util_set_fd_cloexec(worker_watch[WRITE_END]);
948 rules = udev_rules_new(udev, resolve_names);
950 err(udev, "error reading rules\n");
954 udev_queue_export = udev_queue_export_new(udev);
955 if (udev_queue_export == NULL) {
956 err(udev, "error creating queue file\n");
968 err(udev, "fork of daemon failed: %m\n");
979 /* redirect std{out,err} */
980 if (!debug && !debug_trace) {
981 dup2(fd, STDIN_FILENO);
982 dup2(fd, STDOUT_FILENO);
983 dup2(fd, STDERR_FILENO);
985 if (fd > STDERR_FILENO)
988 /* set scheduling priority for the daemon */
989 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
995 /* OOM_DISABLE == -17 */
996 fd = open("/proc/self/oom_adj", O_RDWR);
998 err(udev, "error disabling OOM: %m\n");
1000 write(fd, "-17", 3);
1004 /* in trace mode run one event after the other */
1008 int memsize = mem_size_mb();
1011 max_childs = 128 + (memsize / 8);
1016 /* possibly overwrite maximum limit of executed events */
1017 value = getenv("UDEVD_MAX_CHILDS");
1019 max_childs = strtoul(value, NULL, 10);
1020 info(udev, "initialize max_childs to %u\n", max_childs);
1022 udev_list_init(&event_list);
1023 udev_list_init(&worker_list);
1025 while (!udev_exit) {
1029 /* set timeout to kill idle workers */
1030 if (udev_list_is_empty(&event_list) && childs > 2)
1034 /* wait for events */
1035 fdcount = poll(pfd, ARRAY_SIZE(pfd), timeout);
1039 /* timeout - kill idle workers */
1041 worker_kill(udev, 2);
1043 /* event has finished */
1044 if (pfd[FD_WORKER].revents & POLLIN)
1047 /* get kernel uevent */
1048 if (pfd[FD_NETLINK].revents & POLLIN) {
1049 struct udev_device *dev;
1051 dev = udev_monitor_receive_device(monitor);
1053 event_queue_insert(dev);
1055 udev_device_unref(dev);
1058 /* start new events */
1059 if (!udev_list_is_empty(&event_list) && !stop_exec_queue)
1063 if (pfd[FD_SIGNAL].revents & POLLIN) {
1064 struct signalfd_siginfo fdsi;
1067 size = read(pfd[FD_SIGNAL].fd, &fdsi, sizeof(struct signalfd_siginfo));
1068 if (size == sizeof(struct signalfd_siginfo))
1069 handle_signal(udev, fdsi.ssi_signo);
1072 /* device node and rules directory inotify watch */
1073 if (pfd[FD_INOTIFY].revents & POLLIN)
1074 handle_inotify(udev);
1077 * get control message
1079 * This needs to be after the inotify handling, to make sure,
1080 * that the settle signal is send back after the possibly generated
1081 * "change" events by the inotify device node watch.
1083 if (pfd[FD_CONTROL].revents & POLLIN)
1084 handle_ctrl_msg(udev_ctrl);
1086 /* rules changed, set by inotify or a HUP signal */
1087 if (reload_config) {
1088 struct udev_rules *rules_new;
1090 worker_kill(udev, 0);
1091 rules_new = udev_rules_new(udev, resolve_names);
1092 if (rules_new != NULL) {
1093 udev_rules_unref(rules);
1100 udev_queue_export_cleanup(udev_queue_export);
1103 udev_queue_export_unref(udev_queue_export);
1104 udev_rules_unref(rules);
1105 udev_ctrl_unref(udev_ctrl);
1106 if (pfd[FD_SIGNAL].fd >= 0)
1107 close(pfd[FD_SIGNAL].fd);
1108 if (worker_watch[READ_END] >= 0)
1109 close(worker_watch[READ_END]);
1110 if (worker_watch[WRITE_END] >= 0)
1111 close(worker_watch[WRITE_END]);
1112 udev_monitor_unref(monitor);
1113 udev_selinux_exit(udev);