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 sigset_t orig_sigmask;
83 static struct udev_list_node event_list;
84 static struct udev_list_node worker_list;
85 static bool udev_exit;
86 static volatile sig_atomic_t worker_exit;
96 static struct pollfd pfd[] = {
97 [FD_NETLINK] = { .events = POLLIN },
98 [FD_WORKER] = { .events = POLLIN },
99 [FD_SIGNAL] = { .events = POLLIN },
100 [FD_INOTIFY] = { .events = POLLIN },
101 [FD_CONTROL] = { .events = POLLIN },
111 struct udev_list_node node;
113 struct udev_device *dev;
114 enum event_state state;
116 unsigned long long int delaying_seqnum;
117 unsigned long long int seqnum;
120 const char *devpath_old;
125 static struct event *node_to_event(struct udev_list_node *node)
129 event = (char *)node;
130 event -= offsetof(struct event, node);
131 return (struct event *)event;
142 struct udev_list_node node;
146 struct udev_monitor *monitor;
147 enum worker_state state;
151 /* passed from worker to main process */
152 struct worker_message {
157 static struct worker *node_to_worker(struct udev_list_node *node)
161 worker = (char *)node;
162 worker -= offsetof(struct worker, node);
163 return (struct worker *)worker;
166 static void event_queue_delete(struct event *event)
168 udev_list_node_remove(&event->node);
170 /* mark as failed, if "add" event returns non-zero */
171 if (event->exitcode != 0 && strcmp(udev_device_get_action(event->dev), "add") == 0)
172 udev_queue_export_device_failed(udev_queue_export, event->dev);
174 udev_queue_export_device_finished(udev_queue_export, event->dev);
176 info(event->udev, "seq %llu done with %i\n", udev_device_get_seqnum(event->dev), event->exitcode);
177 udev_device_unref(event->dev);
181 static void event_sig_handler(int signum)
193 static struct worker *worker_ref(struct worker *worker)
199 static void worker_unref(struct worker *worker)
202 if (worker->refcount > 0)
205 udev_list_node_remove(&worker->node);
206 udev_monitor_unref(worker->monitor);
208 info(worker->udev, "worker [%u] cleaned up\n", worker->pid);
212 static void worker_new(struct event *event)
214 struct worker *worker;
215 struct udev_monitor *worker_monitor;
217 struct sigaction act;
219 /* listen for new events */
220 worker_monitor = udev_monitor_new_from_netlink(event->udev, NULL);
221 if (worker_monitor == NULL)
223 /* allow the main daemon netlink address to send devices to the worker */
224 udev_monitor_allow_unicast_sender(worker_monitor, monitor);
225 udev_monitor_enable_receiving(worker_monitor);
227 worker = calloc(1, sizeof(struct worker));
230 /* worker + event reference */
231 worker->refcount = 2;
232 worker->udev = event->udev;
238 struct udev_device *dev;
239 struct pollfd pmon = {
240 .fd = udev_monitor_get_fd(worker_monitor),
244 udev_queue_export_unref(udev_queue_export);
245 udev_monitor_unref(monitor);
246 udev_ctrl_unref(udev_ctrl);
247 close(pfd[FD_SIGNAL].fd);
248 close(worker_watch[READ_END]);
250 udev_log_init("udevd-work");
251 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
253 /* set signal handlers */
254 memset(&act, 0x00, sizeof(act));
255 act.sa_handler = event_sig_handler;
256 sigemptyset (&act.sa_mask);
258 sigaction(SIGTERM, &act, NULL);
259 sigaction(SIGALRM, &act, NULL);
261 /* unblock SIGALRM */
262 sigfillset(&sigmask);
263 sigdelset(&sigmask, SIGALRM);
264 sigprocmask(SIG_SETMASK, &sigmask, NULL);
265 /* SIGTERM is unblocked in ppoll() */
266 sigdelset(&sigmask, SIGTERM);
268 /* request TERM signal if parent exits */
269 prctl(PR_SET_PDEATHSIG, SIGTERM);
275 struct udev_event *udev_event;
276 struct worker_message msg = {};
280 info(event->udev, "seq %llu running\n", udev_device_get_seqnum(dev));
281 udev_event = udev_event_new(dev);
282 if (udev_event == NULL)
285 /* set timeout to prevent hanging processes */
286 alarm(UDEV_EVENT_TIMEOUT);
288 /* apply rules, create node, symlinks */
289 err = udev_event_execute_rules(udev_event, rules);
291 /* rules may change/disable the timeout */
292 if (udev_device_get_event_timeout(dev) >= 0)
293 alarm(udev_device_get_event_timeout(dev));
296 if (err == 0 && udev_get_run(udev_event->udev))
297 failed = udev_event_execute_run(udev_event,
303 /* apply/restore inotify watch */
304 if (err == 0 && udev_event->inotify_watch) {
305 udev_watch_begin(udev_event->udev, dev);
306 udev_device_update_db(dev);
309 /* send processed event back to libudev listeners */
310 udev_monitor_send_device(worker_monitor, NULL, dev);
312 /* send back the result of the event execution */
315 else if (failed != 0)
316 msg.exitcode = failed;
318 send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
320 info(event->udev, "seq %llu processed with %i\n", udev_device_get_seqnum(dev), err);
321 udev_event_unref(udev_event);
322 udev_device_unref(dev);
325 /* wait for more device messages or signal from udevd */
326 while (!worker_exit) {
329 fdcount = ppoll(&pmon, 1, NULL, &sigmask);
333 if (pmon.revents & POLLIN) {
334 dev = udev_monitor_receive_device(worker_monitor);
339 } while (dev != NULL);
341 udev_monitor_unref(worker_monitor);
346 udev_monitor_unref(worker_monitor);
347 event->state = EVENT_QUEUED;
349 err(event->udev, "fork of child failed: %m\n");
352 /* close monitor, but keep address around */
353 udev_monitor_disconnect(worker_monitor);
354 worker->monitor = worker_monitor;
356 worker->state = WORKER_RUNNING;
357 worker->event = event;
358 event->state = EVENT_RUNNING;
359 udev_list_node_append(&worker->node, &worker_list);
361 info(event->udev, "seq %llu forked new worker [%u]\n", udev_device_get_seqnum(event->dev), pid);
366 static void event_run(struct event *event)
368 struct udev_list_node *loop;
370 udev_list_node_foreach(loop, &worker_list) {
371 struct worker *worker = node_to_worker(loop);
374 if (worker->state != WORKER_IDLE)
377 count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
379 err(event->udev, "worker [%u] did not accept message %zi (%m), kill it\n", worker->pid, count);
380 kill(worker->pid, SIGKILL);
381 worker->state = WORKER_KILLED;
385 worker->event = event;
386 worker->state = WORKER_RUNNING;
387 event->state = EVENT_RUNNING;
391 if (childs >= max_childs) {
392 info(event->udev, "maximum number (%i) of childs reached\n", childs);
396 /* start new worker and pass initial device */
400 static void event_queue_insert(struct udev_device *dev)
404 event = calloc(1, sizeof(struct event));
408 event->udev = udev_device_get_udev(dev);
410 event->seqnum = udev_device_get_seqnum(dev);
411 event->devpath = udev_device_get_devpath(dev);
412 event->devpath_len = strlen(event->devpath);
413 event->devpath_old = udev_device_get_devpath_old(dev);
414 event->devnum = udev_device_get_devnum(dev);
415 event->is_block = (strcmp("block", udev_device_get_subsystem(dev)) == 0);
417 udev_queue_export_device_queued(udev_queue_export, dev);
418 info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
419 udev_device_get_action(dev), udev_device_get_subsystem(dev));
421 event->state = EVENT_QUEUED;
422 udev_list_node_append(&event->node, &event_list);
424 /* run all events with a timeout set immediately */
425 if (udev_device_get_timeout(dev) > 0) {
431 static void worker_kill(struct udev *udev, int retain)
433 struct udev_list_node *loop;
436 if (childs <= retain)
439 max = childs - retain;
441 udev_list_node_foreach(loop, &worker_list) {
442 struct worker *worker = node_to_worker(loop);
447 if (worker->state == WORKER_KILLED)
450 worker->state = WORKER_KILLED;
451 kill(worker->pid, SIGTERM);
455 static int mem_size_mb(void)
459 long int memsize = -1;
461 f = fopen("/proc/meminfo", "r");
465 while (fgets(buf, sizeof(buf), f) != NULL) {
468 if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
469 memsize = value / 1024;
478 /* lookup event for identical, parent, child device */
479 static bool is_devpath_busy(struct event *event)
481 struct udev_list_node *loop;
484 /* check if queue contains events we depend on */
485 udev_list_node_foreach(loop, &event_list) {
486 struct event *loop_event = node_to_event(loop);
488 /* we already found a later event, earlier can not block us, no need to check again */
489 if (loop_event->seqnum < event->delaying_seqnum)
492 /* event we checked earlier still exists, no need to check again */
493 if (loop_event->seqnum == event->delaying_seqnum)
496 /* found ourself, no later event can block us */
497 if (loop_event->seqnum >= event->seqnum)
500 /* check major/minor */
501 if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
504 /* check our old name */
505 if (event->devpath_old != NULL && strcmp(loop_event->devpath, event->devpath_old) == 0) {
506 event->delaying_seqnum = loop_event->seqnum;
510 /* compare devpath */
511 common = MIN(loop_event->devpath_len, event->devpath_len);
513 /* one devpath is contained in the other? */
514 if (memcmp(loop_event->devpath, event->devpath, common) != 0)
517 /* identical device event found */
518 if (loop_event->devpath_len == event->devpath_len) {
519 event->delaying_seqnum = loop_event->seqnum;
523 /* parent device event found */
524 if (event->devpath[common] == '/') {
525 event->delaying_seqnum = loop_event->seqnum;
529 /* child device event found */
530 if (loop_event->devpath[common] == '/') {
531 event->delaying_seqnum = loop_event->seqnum;
535 /* no matching device */
542 static void events_start(struct udev *udev)
544 struct udev_list_node *loop;
546 udev_list_node_foreach(loop, &event_list) {
547 struct event *event = node_to_event(loop);
549 if (event->state != EVENT_QUEUED)
552 /* do not start event if parent or child event is still running */
553 if (is_devpath_busy(event)) {
554 dbg(udev, "delay seq %llu (%s)\n", event->seqnum, event->devpath);
562 static void worker_returned(void)
565 struct worker_message msg;
567 struct udev_list_node *loop;
569 size = recv(pfd[FD_WORKER].fd, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
570 if (size != sizeof(struct worker_message))
573 /* lookup worker who sent the signal */
574 udev_list_node_foreach(loop, &worker_list) {
575 struct worker *worker = node_to_worker(loop);
577 if (worker->pid != msg.pid)
580 /* worker returned */
581 worker->event->exitcode = msg.exitcode;
582 event_queue_delete(worker->event);
583 worker->event = NULL;
584 if (worker->state != WORKER_KILLED)
585 worker->state = WORKER_IDLE;
586 worker_unref(worker);
592 /* receive the udevd message from userspace */
593 static void handle_ctrl_msg(struct udev_ctrl *uctrl)
595 struct udev *udev = udev_ctrl_get_udev(uctrl);
596 struct udev_ctrl_msg *ctrl_msg;
600 ctrl_msg = udev_ctrl_receive_msg(uctrl);
601 if (ctrl_msg == NULL)
604 i = udev_ctrl_get_set_log_level(ctrl_msg);
606 info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
607 udev_set_log_priority(udev, i);
608 worker_kill(udev, 0);
611 if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
612 info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
613 stop_exec_queue = true;
616 if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
617 info(udev, "udevd message (START_EXEC_QUEUE) received\n");
618 stop_exec_queue = false;
621 if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
622 info(udev, "udevd message (RELOAD_RULES) received\n");
623 reload_config = true;
626 str = udev_ctrl_get_set_env(ctrl_msg);
634 val = strchr(key, '=');
638 if (val[0] == '\0') {
639 info(udev, "udevd message (ENV) received, unset '%s'\n", key);
640 udev_add_property(udev, key, NULL);
642 info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
643 udev_add_property(udev, key, val);
646 err(udev, "wrong key format '%s'\n", key);
650 worker_kill(udev, 0);
653 i = udev_ctrl_get_set_max_childs(ctrl_msg);
655 info(udev, "udevd message (SET_MAX_CHILDS) received, max_childs=%i\n", i);
659 settle_pid = udev_ctrl_get_settle(ctrl_msg);
660 if (settle_pid > 0) {
661 info(udev, "udevd message (SETTLE) received\n");
662 kill(settle_pid, SIGUSR1);
665 udev_ctrl_msg_unref(ctrl_msg);
668 /* read inotify messages */
669 static int handle_inotify(struct udev *udev)
673 struct inotify_event *ev;
675 if ((ioctl(pfd[FD_INOTIFY].fd, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
678 buf = malloc(nbytes);
680 err(udev, "error getting buffer for inotify\n");
684 nbytes = read(pfd[FD_INOTIFY].fd, buf, nbytes);
686 for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
687 struct udev_device *dev;
689 ev = (struct inotify_event *)(buf + pos);
691 dbg(udev, "inotify event: %x for %s\n", ev->mask, ev->name);
692 reload_config = true;
696 dev = udev_watch_lookup(udev, ev->wd);
698 dbg(udev, "inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
699 if (ev->mask & IN_CLOSE_WRITE) {
700 char filename[UTIL_PATH_SIZE];
703 info(udev, "device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
704 util_strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
705 fd = open(filename, O_WRONLY);
706 if (fd < 0 || write(fd, "change", 6) < 0)
707 info(udev, "error writing uevent: %m\n");
710 if (ev->mask & IN_IGNORED)
711 udev_watch_end(udev, dev);
713 udev_device_unref(dev);
722 static void handle_signal(struct udev *udev, int signo)
733 struct udev_list_node *loop, *tmp;
735 pid = waitpid(-1, &status, WNOHANG);
739 udev_list_node_foreach_safe(loop, tmp, &worker_list) {
740 struct worker *worker = node_to_worker(loop);
742 if (worker->pid != pid)
745 info(udev, "worker [%u] exit\n", pid);
746 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
747 err(udev, "worker [%u] unexpectedly returned with status 0x%04x\n", pid, status);
748 if (worker->event != NULL) {
749 err(udev, "worker [%u] failed while handling '%s'\n", pid, worker->event->devpath);
750 worker->event->exitcode = -32;
751 event_queue_delete(worker->event);
752 /* drop reference from running event */
753 worker_unref(worker);
756 worker_unref(worker);
762 reload_config = true;
767 int main(int argc, char *argv[])
774 int daemonize = false;
775 int resolve_names = 1;
776 static const struct option options[] = {
777 { "daemon", no_argument, NULL, 'd' },
778 { "debug-trace", no_argument, NULL, 't' },
779 { "debug", no_argument, NULL, 'D' },
780 { "help", no_argument, NULL, 'h' },
781 { "version", no_argument, NULL, 'V' },
782 { "resolve-names", required_argument, NULL, 'N' },
791 udev_log_init("udevd");
792 udev_set_log_fn(udev, log_fn);
793 info(udev, "version %s\n", VERSION);
794 udev_selinux_init(udev);
799 option = getopt_long(argc, argv, "dDthV", options, NULL);
812 if (udev_get_log_priority(udev) < LOG_INFO)
813 udev_set_log_priority(udev, LOG_INFO);
816 if (strcmp (optarg, "early") == 0) {
818 } else if (strcmp (optarg, "late") == 0) {
820 } else if (strcmp (optarg, "never") == 0) {
823 fprintf(stderr, "resolve-names must be early, late or never\n");
824 err(udev, "resolve-names must be early, late or never\n");
829 printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--debug] "
830 "[--resolve-names=early|late|never] [--version]\n");
833 printf("%s\n", VERSION);
841 fprintf(stderr, "root privileges required\n");
842 err(udev, "root privileges required\n");
846 /* make sure std{in,out,err} fd's are in a sane state */
847 fd = open("/dev/null", O_RDWR);
849 fprintf(stderr, "cannot open /dev/null\n");
850 err(udev, "cannot open /dev/null\n");
852 if (write(STDOUT_FILENO, 0, 0) < 0)
853 dup2(fd, STDOUT_FILENO);
854 if (write(STDERR_FILENO, 0, 0) < 0)
855 dup2(fd, STDERR_FILENO);
857 /* init control socket, bind() ensures, that only one udevd instance is running */
858 udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
859 if (udev_ctrl == NULL) {
860 fprintf(stderr, "error initializing control socket");
861 err(udev, "error initializing udevd socket");
865 if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
866 fprintf(stderr, "error binding control socket, seems udevd is already running\n");
867 err(udev, "error binding control socket, seems udevd is already running\n");
871 pfd[FD_CONTROL].fd = udev_ctrl_get_fd(udev_ctrl);
873 monitor = udev_monitor_new_from_netlink(udev, "kernel");
874 if (monitor == NULL || udev_monitor_enable_receiving(monitor) < 0) {
875 fprintf(stderr, "error initializing netlink socket\n");
876 err(udev, "error initializing netlink socket\n");
880 udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
881 pfd[FD_NETLINK].fd = udev_monitor_get_fd(monitor);
883 pfd[FD_INOTIFY].fd = udev_watch_init(udev);
884 if (pfd[FD_INOTIFY].fd < 0) {
885 fprintf(stderr, "error initializing inotify\n");
886 err(udev, "error initializing inotify\n");
891 if (udev_get_rules_path(udev) != NULL) {
892 inotify_add_watch(pfd[FD_INOTIFY].fd, udev_get_rules_path(udev),
893 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
895 char filename[UTIL_PATH_SIZE];
898 inotify_add_watch(pfd[FD_INOTIFY].fd, LIBEXECDIR "/rules.d",
899 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
900 inotify_add_watch(pfd[FD_INOTIFY].fd, SYSCONFDIR "/udev/rules.d",
901 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
903 /* watch dynamic rules directory */
904 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL);
905 if (stat(filename, &statbuf) != 0) {
906 util_create_path(udev, filename);
907 udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
908 mkdir(filename, 0755);
909 udev_selinux_resetfscreatecon(udev);
911 inotify_add_watch(pfd[FD_INOTIFY].fd, filename,
912 IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
914 udev_watch_restore(udev);
916 /* block and listen to all signals on signalfd */
918 sigprocmask(SIG_SETMASK, &mask, &orig_sigmask);
919 pfd[FD_SIGNAL].fd = signalfd(-1, &mask, 0);
920 if (pfd[FD_SIGNAL].fd < 0) {
921 fprintf(stderr, "error getting signalfd\n");
922 err(udev, "error getting signalfd\n");
927 /* unnamed socket from workers to the main daemon */
928 if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
929 fprintf(stderr, "error getting socketpair\n");
930 err(udev, "error getting socketpair\n");
934 pfd[FD_WORKER].fd = worker_watch[READ_END];
936 rules = udev_rules_new(udev, resolve_names);
938 err(udev, "error reading rules\n");
942 udev_queue_export = udev_queue_export_new(udev);
943 if (udev_queue_export == NULL) {
944 err(udev, "error creating queue file\n");
956 err(udev, "fork of daemon failed: %m\n");
965 f = fopen("/dev/kmsg", "w");
967 fprintf(f, "<6>udev: starting version " VERSION "\n");
971 /* redirect std{out,err} */
972 if (!debug && !debug_trace) {
973 dup2(fd, STDIN_FILENO);
974 dup2(fd, STDOUT_FILENO);
975 dup2(fd, STDERR_FILENO);
977 if (fd > STDERR_FILENO)
980 /* set scheduling priority for the daemon */
981 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
987 /* OOM_DISABLE == -17 */
988 fd = open("/proc/self/oom_adj", O_RDWR);
990 err(udev, "error disabling OOM: %m\n");
996 /* in trace mode run one event after the other */
1000 int memsize = mem_size_mb();
1003 max_childs = 128 + (memsize / 8);
1008 /* possibly overwrite maximum limit of executed events */
1009 value = getenv("UDEVD_MAX_CHILDS");
1011 max_childs = strtoul(value, NULL, 10);
1012 info(udev, "initialize max_childs to %u\n", max_childs);
1014 udev_list_init(&event_list);
1015 udev_list_init(&worker_list);
1017 while (!udev_exit) {
1021 /* set timeout to kill idle workers */
1022 if (udev_list_is_empty(&event_list) && childs > 2)
1026 /* wait for events */
1027 fdcount = poll(pfd, ARRAY_SIZE(pfd), timeout);
1031 /* timeout - kill idle workers */
1033 worker_kill(udev, 2);
1035 /* event has finished */
1036 if (pfd[FD_WORKER].revents & POLLIN)
1039 /* get kernel uevent */
1040 if (pfd[FD_NETLINK].revents & POLLIN) {
1041 struct udev_device *dev;
1043 dev = udev_monitor_receive_device(monitor);
1045 event_queue_insert(dev);
1047 udev_device_unref(dev);
1050 /* start new events */
1051 if (!udev_list_is_empty(&event_list) && !stop_exec_queue)
1055 if (pfd[FD_SIGNAL].revents & POLLIN) {
1056 struct signalfd_siginfo fdsi;
1059 size = read(pfd[FD_SIGNAL].fd, &fdsi, sizeof(struct signalfd_siginfo));
1060 if (size == sizeof(struct signalfd_siginfo))
1061 handle_signal(udev, fdsi.ssi_signo);
1064 /* device node and rules directory inotify watch */
1065 if (pfd[FD_INOTIFY].revents & POLLIN)
1066 handle_inotify(udev);
1069 * get control message
1071 * This needs to be after the inotify handling, to make sure,
1072 * that the settle signal is send back after the possibly generated
1073 * "change" events by the inotify device node watch.
1075 if (pfd[FD_CONTROL].revents & POLLIN)
1076 handle_ctrl_msg(udev_ctrl);
1078 /* rules changed, set by inotify or a HUP signal */
1079 if (reload_config) {
1080 struct udev_rules *rules_new;
1082 worker_kill(udev, 0);
1083 rules_new = udev_rules_new(udev, resolve_names);
1084 if (rules_new != NULL) {
1085 udev_rules_unref(rules);
1092 udev_queue_export_cleanup(udev_queue_export);
1095 udev_queue_export_unref(udev_queue_export);
1096 udev_rules_unref(rules);
1097 udev_ctrl_unref(udev_ctrl);
1098 if (pfd[FD_SIGNAL].fd >= 0)
1099 close(pfd[FD_SIGNAL].fd);
1100 if (worker_watch[READ_END] >= 0)
1101 close(worker_watch[READ_END]);
1102 if (worker_watch[WRITE_END] >= 0)
1103 close(worker_watch[WRITE_END]);
1104 udev_monitor_unref(monitor);
1105 udev_selinux_exit(udev);