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 failed = udev_event_execute_run(udev_event, &orig_sigmask);
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 udevd 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);
411 event->devnum = udev_device_get_devnum(dev);
412 event->is_block = (strcmp("block", udev_device_get_subsystem(dev)) == 0);
414 udev_queue_export_device_queued(udev_queue_export, dev);
415 info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
416 udev_device_get_action(dev), udev_device_get_subsystem(dev));
418 event->state = EVENT_QUEUED;
419 udev_list_node_append(&event->node, &event_list);
421 /* run all events with a timeout set immediately */
422 if (udev_device_get_timeout(dev) > 0) {
428 static void worker_kill(struct udev *udev, int retain)
430 struct udev_list_node *loop;
433 if (childs <= retain)
436 max = childs - retain;
438 udev_list_node_foreach(loop, &worker_list) {
439 struct worker *worker = node_to_worker(loop);
444 if (worker->state == WORKER_KILLED)
447 worker->state = WORKER_KILLED;
448 kill(worker->pid, SIGTERM);
452 static int mem_size_mb(void)
456 long int memsize = -1;
458 f = fopen("/proc/meminfo", "r");
462 while (fgets(buf, sizeof(buf), f) != NULL) {
465 if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
466 memsize = value / 1024;
475 /* lookup event for identical, parent, child device */
476 static bool is_devpath_busy(struct event *event)
478 struct udev_list_node *loop;
481 /* check if queue contains events we depend on */
482 udev_list_node_foreach(loop, &event_list) {
483 struct event *loop_event = node_to_event(loop);
485 /* we already found a later event, earlier can not block us, no need to check again */
486 if (loop_event->seqnum < event->delaying_seqnum)
489 /* event we checked earlier still exists, no need to check again */
490 if (loop_event->seqnum == event->delaying_seqnum)
493 /* found ourself, no later event can block us */
494 if (loop_event->seqnum >= event->seqnum)
497 /* check major/minor */
498 if (major(event->devnum) != 0 && event->devnum == loop_event->devnum && event->is_block == loop_event->is_block)
501 /* check our old name */
502 if (event->devpath_old != NULL && strcmp(loop_event->devpath, event->devpath_old) == 0) {
503 event->delaying_seqnum = loop_event->seqnum;
507 /* compare devpath */
508 common = MIN(loop_event->devpath_len, event->devpath_len);
510 /* one devpath is contained in the other? */
511 if (memcmp(loop_event->devpath, event->devpath, common) != 0)
514 /* identical device event found */
515 if (loop_event->devpath_len == event->devpath_len) {
516 event->delaying_seqnum = loop_event->seqnum;
520 /* parent device event found */
521 if (event->devpath[common] == '/') {
522 event->delaying_seqnum = loop_event->seqnum;
526 /* child device event found */
527 if (loop_event->devpath[common] == '/') {
528 event->delaying_seqnum = loop_event->seqnum;
532 /* no matching device */
539 static void events_start(struct udev *udev)
541 struct udev_list_node *loop;
543 udev_list_node_foreach(loop, &event_list) {
544 struct event *event = node_to_event(loop);
546 if (event->state != EVENT_QUEUED)
549 /* do not start event if parent or child event is still running */
550 if (is_devpath_busy(event)) {
551 dbg(udev, "delay seq %llu (%s)\n", event->seqnum, event->devpath);
559 static void worker_returned(void)
562 struct worker_message msg;
564 struct udev_list_node *loop;
566 size = recv(pfd[FD_WORKER].fd, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
567 if (size != sizeof(struct worker_message))
570 /* lookup worker who sent the signal */
571 udev_list_node_foreach(loop, &worker_list) {
572 struct worker *worker = node_to_worker(loop);
574 if (worker->pid != msg.pid)
577 /* worker returned */
578 worker->event->exitcode = msg.exitcode;
579 event_queue_delete(worker->event);
580 worker->event = NULL;
581 if (worker->state != WORKER_KILLED)
582 worker->state = WORKER_IDLE;
583 worker_unref(worker);
589 /* receive the udevd message from userspace */
590 static void handle_ctrl_msg(struct udev_ctrl *uctrl)
592 struct udev *udev = udev_ctrl_get_udev(uctrl);
593 struct udev_ctrl_msg *ctrl_msg;
597 ctrl_msg = udev_ctrl_receive_msg(uctrl);
598 if (ctrl_msg == NULL)
601 i = udev_ctrl_get_set_log_level(ctrl_msg);
603 info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
604 udev_set_log_priority(udev, i);
605 worker_kill(udev, 0);
608 if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
609 info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
610 stop_exec_queue = true;
613 if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
614 info(udev, "udevd message (START_EXEC_QUEUE) received\n");
615 stop_exec_queue = false;
618 if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
619 info(udev, "udevd message (RELOAD_RULES) received\n");
620 reload_config = true;
623 str = udev_ctrl_get_set_env(ctrl_msg);
631 val = strchr(key, '=');
635 if (val[0] == '\0') {
636 info(udev, "udevd message (ENV) received, unset '%s'\n", key);
637 udev_add_property(udev, key, NULL);
639 info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
640 udev_add_property(udev, key, val);
643 err(udev, "wrong key format '%s'\n", key);
647 worker_kill(udev, 0);
650 i = udev_ctrl_get_set_max_childs(ctrl_msg);
652 info(udev, "udevd message (SET_MAX_CHILDS) received, max_childs=%i\n", i);
656 settle_pid = udev_ctrl_get_settle(ctrl_msg);
657 if (settle_pid > 0) {
658 info(udev, "udevd message (SETTLE) received\n");
659 kill(settle_pid, SIGUSR1);
662 udev_ctrl_msg_unref(ctrl_msg);
665 /* read inotify messages */
666 static int handle_inotify(struct udev *udev)
670 struct inotify_event *ev;
672 if ((ioctl(pfd[FD_INOTIFY].fd, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
675 buf = malloc(nbytes);
677 err(udev, "error getting buffer for inotify\n");
681 nbytes = read(pfd[FD_INOTIFY].fd, buf, nbytes);
683 for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
684 struct udev_device *dev;
686 ev = (struct inotify_event *)(buf + pos);
688 dbg(udev, "inotify event: %x for %s\n", ev->mask, ev->name);
689 reload_config = true;
693 dev = udev_watch_lookup(udev, ev->wd);
695 dbg(udev, "inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
696 if (ev->mask & IN_CLOSE_WRITE) {
697 char filename[UTIL_PATH_SIZE];
700 info(udev, "device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
701 util_strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
702 fd = open(filename, O_WRONLY);
703 if (fd < 0 || write(fd, "change", 6) < 0)
704 info(udev, "error writing uevent: %m\n");
707 if (ev->mask & IN_IGNORED)
708 udev_watch_end(udev, dev);
710 udev_device_unref(dev);
719 static void handle_signal(struct udev *udev, int signo)
730 struct udev_list_node *loop, *tmp;
732 pid = waitpid(-1, &status, WNOHANG);
736 udev_list_node_foreach_safe(loop, tmp, &worker_list) {
737 struct worker *worker = node_to_worker(loop);
739 if (worker->pid != pid)
742 info(udev, "worker [%u] exit\n", pid);
743 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
744 err(udev, "worker [%u] unexpectedly returned with status 0x%04x\n", pid, status);
745 if (worker->event != NULL) {
746 err(udev, "worker [%u] failed while handling '%s'\n", pid, worker->event->devpath);
747 worker->event->exitcode = -32;
748 event_queue_delete(worker->event);
749 /* drop reference from running event */
750 worker_unref(worker);
753 worker_unref(worker);
759 reload_config = true;
764 int main(int argc, char *argv[])
771 int daemonize = false;
772 int resolve_names = 1;
773 static const struct option options[] = {
774 { "daemon", no_argument, NULL, 'd' },
775 { "debug-trace", no_argument, NULL, 't' },
776 { "debug", no_argument, NULL, 'D' },
777 { "help", no_argument, NULL, 'h' },
778 { "version", no_argument, NULL, 'V' },
779 { "resolve-names", required_argument, NULL, 'N' },
788 udev_log_init("udevd");
789 udev_set_log_fn(udev, log_fn);
790 info(udev, "version %s\n", VERSION);
791 udev_selinux_init(udev);
796 option = getopt_long(argc, argv, "dDthV", options, NULL);
809 if (udev_get_log_priority(udev) < LOG_INFO)
810 udev_set_log_priority(udev, LOG_INFO);
813 if (strcmp (optarg, "early") == 0) {
815 } else if (strcmp (optarg, "late") == 0) {
817 } else if (strcmp (optarg, "never") == 0) {
820 fprintf(stderr, "resolve-names must be early, late or never\n");
821 err(udev, "resolve-names must be early, late or never\n");
826 printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--debug] "
827 "[--resolve-names=early|late|never] [--version]\n");
830 printf("%s\n", VERSION);
838 fprintf(stderr, "root privileges required\n");
839 err(udev, "root privileges required\n");
843 /* make sure std{in,out,err} fd's are in a sane state */
844 fd = open("/dev/null", O_RDWR);
846 fprintf(stderr, "cannot open /dev/null\n");
847 err(udev, "cannot open /dev/null\n");
849 if (write(STDOUT_FILENO, 0, 0) < 0)
850 dup2(fd, STDOUT_FILENO);
851 if (write(STDERR_FILENO, 0, 0) < 0)
852 dup2(fd, STDERR_FILENO);
854 /* init control socket, bind() ensures, that only one udevd instance is running */
855 udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
856 if (udev_ctrl == NULL) {
857 fprintf(stderr, "error initializing control socket");
858 err(udev, "error initializing udevd socket");
862 if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
863 fprintf(stderr, "error binding control socket, seems udevd is already running\n");
864 err(udev, "error binding control socket, seems udevd is already running\n");
868 pfd[FD_CONTROL].fd = udev_ctrl_get_fd(udev_ctrl);
870 monitor = udev_monitor_new_from_netlink(udev, "kernel");
871 if (monitor == NULL || udev_monitor_enable_receiving(monitor) < 0) {
872 fprintf(stderr, "error initializing netlink socket\n");
873 err(udev, "error initializing netlink socket\n");
877 udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
878 pfd[FD_NETLINK].fd = udev_monitor_get_fd(monitor);
880 pfd[FD_INOTIFY].fd = udev_watch_init(udev);
881 if (pfd[FD_INOTIFY].fd < 0) {
882 fprintf(stderr, "error initializing inotify\n");
883 err(udev, "error initializing inotify\n");
888 if (udev_get_rules_path(udev) != NULL) {
889 inotify_add_watch(pfd[FD_INOTIFY].fd, udev_get_rules_path(udev),
890 IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
892 char filename[UTIL_PATH_SIZE];
895 inotify_add_watch(pfd[FD_INOTIFY].fd, LIBEXECDIR "/rules.d",
896 IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
897 inotify_add_watch(pfd[FD_INOTIFY].fd, SYSCONFDIR "/udev/rules.d",
898 IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
900 /* watch dynamic rules directory */
901 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL);
902 if (stat(filename, &statbuf) != 0) {
903 util_create_path(udev, filename);
904 udev_selinux_setfscreatecon(udev, filename, S_IFDIR|0755);
905 mkdir(filename, 0755);
906 udev_selinux_resetfscreatecon(udev);
908 inotify_add_watch(pfd[FD_INOTIFY].fd, filename,
909 IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
911 udev_watch_restore(udev);
913 /* block and listen to all signals on signalfd */
915 sigprocmask(SIG_SETMASK, &mask, &orig_sigmask);
916 pfd[FD_SIGNAL].fd = signalfd(-1, &mask, 0);
917 if (pfd[FD_SIGNAL].fd < 0) {
918 fprintf(stderr, "error getting signalfd\n");
919 err(udev, "error getting signalfd\n");
924 /* unnamed socket from workers to the main daemon */
925 if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
926 fprintf(stderr, "error getting socketpair\n");
927 err(udev, "error getting socketpair\n");
931 pfd[FD_WORKER].fd = worker_watch[READ_END];
933 rules = udev_rules_new(udev, resolve_names);
935 err(udev, "error reading rules\n");
939 udev_queue_export = udev_queue_export_new(udev);
940 if (udev_queue_export == NULL) {
941 err(udev, "error creating queue file\n");
953 err(udev, "fork of daemon failed: %m\n");
962 f = fopen("/dev/kmsg", "w");
964 fprintf(f, "<6>udev: starting version " VERSION "\n");
968 /* redirect std{out,err} */
969 if (!debug && !debug_trace) {
970 dup2(fd, STDIN_FILENO);
971 dup2(fd, STDOUT_FILENO);
972 dup2(fd, STDERR_FILENO);
974 if (fd > STDERR_FILENO)
977 /* set scheduling priority for the daemon */
978 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
984 /* OOM_DISABLE == -17 */
985 fd = open("/proc/self/oom_adj", O_RDWR);
987 err(udev, "error disabling OOM: %m\n");
993 /* in trace mode run one event after the other */
997 int memsize = mem_size_mb();
1000 max_childs = 128 + (memsize / 8);
1005 /* possibly overwrite maximum limit of executed events */
1006 value = getenv("UDEVD_MAX_CHILDS");
1008 max_childs = strtoul(value, NULL, 10);
1009 info(udev, "initialize max_childs to %u\n", max_childs);
1011 udev_list_init(&event_list);
1012 udev_list_init(&worker_list);
1014 while (!udev_exit) {
1018 /* set timeout to kill idle workers */
1019 if (udev_list_is_empty(&event_list) && childs > 2)
1023 /* wait for events */
1024 fdcount = poll(pfd, ARRAY_SIZE(pfd), timeout);
1028 /* timeout - kill idle workers */
1030 worker_kill(udev, 2);
1032 /* event has finished */
1033 if (pfd[FD_WORKER].revents & POLLIN)
1036 /* get kernel uevent */
1037 if (pfd[FD_NETLINK].revents & POLLIN) {
1038 struct udev_device *dev;
1040 dev = udev_monitor_receive_device(monitor);
1042 event_queue_insert(dev);
1044 udev_device_unref(dev);
1047 /* start new events */
1048 if (!udev_list_is_empty(&event_list) && !stop_exec_queue)
1052 if (pfd[FD_SIGNAL].revents & POLLIN) {
1053 struct signalfd_siginfo fdsi;
1056 size = read(pfd[FD_SIGNAL].fd, &fdsi, sizeof(struct signalfd_siginfo));
1057 if (size == sizeof(struct signalfd_siginfo))
1058 handle_signal(udev, fdsi.ssi_signo);
1061 /* device node and rules directory inotify watch */
1062 if (pfd[FD_INOTIFY].revents & POLLIN)
1063 handle_inotify(udev);
1066 * get control message
1068 * This needs to be after the inotify handling, to make sure,
1069 * that the settle signal is send back after the possibly generated
1070 * "change" events by the inotify device node watch.
1072 if (pfd[FD_CONTROL].revents & POLLIN)
1073 handle_ctrl_msg(udev_ctrl);
1075 /* rules changed, set by inotify or a HUP signal */
1076 if (reload_config) {
1077 struct udev_rules *rules_new;
1079 worker_kill(udev, 0);
1080 rules_new = udev_rules_new(udev, resolve_names);
1081 if (rules_new != NULL) {
1082 udev_rules_unref(rules);
1089 udev_queue_export_cleanup(udev_queue_export);
1092 udev_queue_export_unref(udev_queue_export);
1093 udev_rules_unref(rules);
1094 udev_ctrl_unref(udev_ctrl);
1095 if (pfd[FD_SIGNAL].fd >= 0)
1096 close(pfd[FD_SIGNAL].fd);
1097 if (worker_watch[READ_END] >= 0)
1098 close(worker_watch[READ_END]);
1099 if (worker_watch[WRITE_END] >= 0)
1100 close(worker_watch[WRITE_END]);
1101 udev_monitor_unref(monitor);
1102 udev_selinux_exit(udev);