2 * udevd.c - event listener and serializer
4 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
5 * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <sys/select.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
40 #include <sys/sysinfo.h>
42 #include <sys/ioctl.h>
43 #include <linux/types.h>
44 #include <linux/netlink.h>
47 #include "udev_libc_wrapper.h"
49 #include "udev_version.h"
50 #include "udev_rules.h"
51 #include "udev_utils.h"
56 struct udev_rules rules;
57 static int udevd_sock;
58 static int uevent_netlink_sock;
59 static int inotify_fd;
62 static int signal_pipe[2] = {-1, -1};
63 static volatile int sigchilds_waiting;
64 static volatile int run_msg_q;
65 static volatile int udev_exit;
66 static volatile int reload_config;
67 static int init_phase = 1;
68 static int run_exec_q;
69 static int stop_exec_q;
70 static int event_timeout;
71 static int max_childs;
72 static int max_childs_running;
73 static unsigned long long expected_seqnum;
74 static char udev_log[32];
76 static LIST_HEAD(msg_list);
77 static LIST_HEAD(exec_list);
78 static LIST_HEAD(running_list);
82 void log_message(int priority, const char *format, ...)
86 if (priority > udev_log_priority)
89 va_start(args, format);
90 vsyslog(priority, format, args);
95 static void msg_dump_queue(void)
98 struct uevent_msg *msg;
100 list_for_each_entry(msg, &msg_list, node)
101 dbg("sequence %llu in queue", msg->seqnum);
105 static void msg_queue_delete(struct uevent_msg *msg)
107 list_del(&msg->node);
111 /* orders the message in the queue by sequence number */
112 static void msg_queue_insert(struct uevent_msg *msg)
114 struct uevent_msg *loop_msg;
117 if (msg->seqnum == 0) {
118 dbg("no SEQNUM, move straight to the exec queue");
119 list_add_tail(&msg->node, &exec_list);
124 /* store timestamp of queuing */
126 msg->queue_time = info.uptime;
128 /* with the first event we provide a phase of shorter timeout */
130 static long init_time;
133 init_time = info.uptime;
134 if (info.uptime - init_time >= UDEVD_INIT_TIME)
138 /* sort message by sequence number into list */
139 list_for_each_entry_reverse(loop_msg, &msg_list, node) {
140 if (loop_msg->seqnum < msg->seqnum)
143 if (loop_msg->seqnum == msg->seqnum) {
144 dbg("ignoring duplicate message seq %llu", msg->seqnum);
149 list_add(&msg->node, &loop_msg->node);
150 info("seq %llu queued, '%s' '%s'", msg->seqnum, msg->action, msg->devpath);
152 /* run msg queue manager */
158 static void asmlinkage udev_event_sig_handler(int signum)
160 if (signum == SIGALRM)
164 static int udev_event_process(struct uevent_msg *msg)
166 struct sigaction act;
168 struct name_entry *name_loop;
172 /* set signal handlers */
173 memset(&act, 0x00, sizeof(act));
174 act.sa_handler = (void (*)(int)) udev_event_sig_handler;
175 sigemptyset (&act.sa_mask);
177 sigaction(SIGALRM, &act, NULL);
179 /* trigger timeout to prevent hanging processes */
180 alarm(UDEV_ALARM_TIMEOUT);
182 /* reconstruct env from message */
183 for (i = 0; msg->envp[i]; i++)
184 putenv(msg->envp[i]);
186 udev_init_device(&udev, msg->devpath, msg->subsystem, msg->action);
187 retval = udev_process_event(&rules, &udev);
189 /* run programs collected by RUN-key*/
191 list_for_each_entry(name_loop, &udev.run_list, node) {
192 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
193 pass_env_to_socket(&name_loop->name[strlen("socket:")], msg->devpath, msg->action);
195 run_program(name_loop->name, udev.subsystem, NULL, 0, NULL, (udev_log_priority >= LOG_INFO));
199 udev_cleanup_device(&udev);
204 /* runs event and removes event from run queue when finished */
205 static void udev_event_run(struct uevent_msg *msg)
214 close(uevent_netlink_sock);
218 close(signal_pipe[READ_END]);
219 close(signal_pipe[WRITE_END]);
222 logging_init("udevd-event");
223 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
224 udev_event_process(msg);
225 info("seq %llu finished", msg->seqnum);
230 err("fork of child failed");
231 msg_queue_delete(msg);
234 /* get SIGCHLD in main loop */
236 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
237 msg->seqnum, pid, msg->action, msg->subsystem, info.uptime - msg->queue_time);
242 static int running_processes(void)
245 static char buf[4096];
250 f = open("/proc/stat", O_RDONLY);
254 len = read(f, buf, sizeof(buf)-1);
262 pos = strstr(buf, "procs_running ");
266 if (sscanf(pos, "procs_running %u", &running) != 1)
272 /* return the number of process es in our session, count only until limit */
273 static int running_processes_in_session(pid_t session, int limit)
279 dir = opendir("/proc");
283 /* read process info from /proc */
284 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
290 pid_t ppid, pgrp, sess;
293 if (!isdigit(dent->d_name[0]))
296 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
297 procdir[sizeof(procdir)-1] = '\0';
299 f = open(procdir, O_RDONLY);
303 len = read(f, line, sizeof(line)-1);
311 /* skip ugly program name */
312 pos = strrchr(line, ')') + 2;
316 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
319 /* count only processes in our session */
323 /* count only running, no sleeping processes */
328 if (limit > 0 && running >= limit)
336 static int compare_devpath(const char *running, const char *waiting)
340 for (i = 0; i < PATH_SIZE; i++) {
341 /* identical device event found */
342 if (running[i] == '\0' && waiting[i] == '\0')
345 /* parent device event found */
346 if (running[i] == '\0' && waiting[i] == '/')
349 /* child device event found */
350 if (running[i] == '/' && waiting[i] == '\0')
353 /* no matching event */
354 if (running[i] != waiting[i])
361 /* returns still running task for the same device, its parent or its physical device */
362 static int running_with_devpath(struct uevent_msg *msg, int limit)
364 struct uevent_msg *loop_msg;
365 int childs_count = 0;
367 if (msg->devpath == NULL)
370 /* skip any events with a timeout set */
371 if (msg->timeout != 0)
374 list_for_each_entry(loop_msg, &running_list, node) {
375 if (limit && childs_count++ > limit) {
376 dbg("%llu, maximum number (%i) of child reached", msg->seqnum, childs_count);
379 if (loop_msg->devpath == NULL)
382 /* return running parent/child device event */
383 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
384 dbg("%llu, child device event still running %llu (%s)",
385 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
389 /* return running physical device event */
390 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
391 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
392 dbg("%llu, physical device event still running %llu (%s)",
393 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
401 /* exec queue management routine executes the events and serializes events in the same sequence */
402 static void exec_queue_manager(void)
404 struct uevent_msg *loop_msg;
405 struct uevent_msg *tmp_msg;
408 if (list_empty(&exec_list))
411 running = running_processes();
412 dbg("%d processes runnning on system", running);
414 running = max_childs_running;
416 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
417 /* check running processes in our session and possibly throttle */
418 if (running >= max_childs_running) {
419 running = running_processes_in_session(sid, max_childs_running+10);
420 dbg("at least %d processes running in session", running);
421 if (running >= max_childs_running) {
422 dbg("delay seq %llu, cause too many processes already running",
428 if (running_with_devpath(loop_msg, max_childs) == 0) {
429 /* move event to run list */
430 list_move_tail(&loop_msg->node, &running_list);
431 udev_event_run(loop_msg);
433 dbg("moved seq %llu to running list", loop_msg->seqnum);
435 dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
439 static void msg_move_exec(struct uevent_msg *msg)
441 list_move_tail(&msg->node, &exec_list);
443 expected_seqnum = msg->seqnum+1;
444 dbg("moved seq %llu to exec, next expected is %llu",
445 msg->seqnum, expected_seqnum);
448 /* msg queue management routine handles the timeouts and dispatches the events */
449 static void msg_queue_manager(void)
451 struct uevent_msg *loop_msg;
452 struct uevent_msg *tmp_msg;
455 int timeout = event_timeout;
457 dbg("msg queue manager, next expected is %llu", expected_seqnum);
460 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, node) {
461 /* move event with expected sequence to the exec list */
462 if (loop_msg->seqnum == expected_seqnum) {
463 msg_move_exec(loop_msg);
467 /* limit timeout during initialization phase */
469 if (timeout > UDEVD_INIT_EVENT_TIMEOUT)
470 timeout = UDEVD_INIT_EVENT_TIMEOUT;
471 dbg("initialization phase, timeout %i seconds", timeout);
474 /* move event with expired timeout to the exec list */
475 msg_age = info.uptime - loop_msg->queue_time;
476 dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age);
477 if (msg_age >= timeout) {
478 msg_move_exec(loop_msg);
486 /* set timeout for remaining queued events */
487 if (!list_empty(&msg_list)) {
488 struct itimerval itv = {{0, 0}, {timeout - msg_age, 0}};
489 info("next event expires in %li seconds", timeout - msg_age);
490 setitimer(ITIMER_REAL, &itv, NULL);
494 static struct uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
498 struct uevent_msg *msg;
502 msg = malloc(sizeof(struct uevent_msg) + buf_size);
505 memset(msg, 0x00, sizeof(struct uevent_msg) + buf_size);
507 /* copy environment buffer and reconstruct envp */
508 memcpy(msg->envbuf, buf, buf_size);
510 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
514 key = &msg->envbuf[bufpos];
515 keylen = strlen(key);
517 bufpos += keylen + 1;
518 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
520 /* remember some keys for further processing */
521 if (strncmp(key, "ACTION=", 7) == 0)
522 msg->action = &key[7];
523 else if (strncmp(key, "DEVPATH=", 8) == 0)
524 msg->devpath = &key[8];
525 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
526 msg->subsystem = &key[10];
527 else if (strncmp(key, "SEQNUM=", 7) == 0)
528 msg->seqnum = strtoull(&key[7], NULL, 10);
529 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
530 msg->physdevpath = &key[12];
531 else if (strncmp(key, "MAJOR=", 6) == 0)
532 major = strtoull(&key[6], NULL, 10);
533 else if (strncmp(key, "MINOR=", 6) == 0)
534 minor = strtoull(&key[6], NULL, 10);
535 else if (strncmp(key, "TIMEOUT=", 8) == 0)
536 msg->timeout = strtoull(&key[8], NULL, 10);
538 msg->devt = makedev(major, minor);
539 msg->envp[i++] = "UDEVD_EVENT=1";
545 /* receive the udevd message from userspace */
546 static struct uevent_msg *get_udevd_msg(void)
548 static struct udevd_msg usend_msg;
549 struct uevent_msg *msg;
552 struct cmsghdr *cmsg;
555 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
559 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
560 iov.iov_base = &usend_msg;
561 iov.iov_len = sizeof(struct udevd_msg);
563 memset(&smsg, 0x00, sizeof(struct msghdr));
566 smsg.msg_control = cred_msg;
567 smsg.msg_controllen = sizeof(cred_msg);
569 size = recvmsg(udevd_sock, &smsg, 0);
572 err("unable to receive udevd message");
575 cmsg = CMSG_FIRSTHDR(&smsg);
576 cred = (struct ucred *) CMSG_DATA(cmsg);
578 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
579 err("no sender credentials received, message ignored");
583 if (cred->uid != 0) {
584 err("sender uid=%i, message ignored", cred->uid);
588 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
589 err("message magic '%s' doesn't match, ignore it", usend_msg.magic);
593 switch (usend_msg.type) {
594 case UDEVD_UEVENT_UDEVSEND:
595 case UDEVD_UEVENT_INITSEND:
596 info("udevd event message received");
597 envbuf_size = size - offsetof(struct udevd_msg, envbuf);
598 dbg("envbuf_size=%i", envbuf_size);
599 msg = get_msg_from_envbuf(usend_msg.envbuf, envbuf_size);
602 msg->type = usend_msg.type;
604 case UDEVD_STOP_EXEC_QUEUE:
605 info("udevd message (STOP_EXEC_QUEUE) received");
608 case UDEVD_START_EXEC_QUEUE:
609 info("udevd message (START_EXEC_QUEUE) received");
611 exec_queue_manager();
613 case UDEVD_SET_LOG_LEVEL:
614 intval = (int *) usend_msg.envbuf;
615 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
616 udev_log_priority = *intval;
617 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
620 case UDEVD_SET_MAX_CHILDS:
621 intval = (int *) usend_msg.envbuf;
622 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
623 max_childs = *intval;
625 case UDEVD_RELOAD_RULES:
626 info("udevd message (RELOAD_RULES) received");
630 dbg("unknown message type");
635 /* receive the kernel user event message and do some sanity checks */
636 static struct uevent_msg *get_netlink_msg(void)
638 struct uevent_msg *msg;
641 static char buffer[UEVENT_BUFFER_SIZE + 512];
644 size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
647 err("unable to receive udevd message");
651 if ((size_t)size > sizeof(buffer)-1)
652 size = sizeof(buffer)-1;
654 dbg("uevent_size=%zi", size);
656 /* start of event payload */
657 bufpos = strlen(buffer)+1;
658 msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
661 msg->type = UDEVD_UEVENT_NETLINK;
663 /* validate message */
664 pos = strchr(buffer, '@');
666 err("invalid uevent '%s'", buffer);
672 if (msg->action == NULL) {
673 info("no ACTION in payload found, skip event '%s'", buffer);
678 if (strcmp(msg->action, buffer) != 0) {
679 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
687 static void asmlinkage sig_handler(int signum)
695 /* set flag, then write to pipe if needed */
699 /* set flag, then write to pipe if needed */
700 sigchilds_waiting = 1;
707 /* write to pipe, which will wakeup select() in our mainloop */
708 write(signal_pipe[WRITE_END], "", 1);
711 static void udev_done(int pid)
713 /* find msg associated with pid and delete it */
714 struct uevent_msg *msg;
717 list_for_each_entry(msg, &running_list, node) {
718 if (msg->pid == pid) {
721 info("seq %llu, pid [%d] exit, %ld seconds old", msg->seqnum, msg->pid, info.uptime - msg->queue_time);
723 info("seq 0, pid [%d] exit", msg->pid);
724 msg_queue_delete(msg);
726 /* we want to run the exec queue manager since there may
727 * be events waiting with the devpath of the one that
736 static void reap_sigchilds(void)
741 pid = waitpid(-1, NULL, WNOHANG);
748 static int init_udevd_socket(void)
750 struct sockaddr_un saddr;
751 const int buffersize = 16 * 1024 * 1024;
753 const int feature_on = 1;
756 memset(&saddr, 0x00, sizeof(saddr));
757 saddr.sun_family = AF_LOCAL;
758 /* use abstract namespace for socket path */
759 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
760 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
762 udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
763 if (udevd_sock == -1) {
764 err("error getting socket, %s", strerror(errno));
768 /* set receive buffersize */
769 setsockopt(udevd_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
771 /* the bind takes care of ensuring only one copy running */
772 retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
774 err("bind failed, %s", strerror(errno));
778 /* enable receiving of the sender credentials */
779 setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
784 static int init_uevent_netlink_sock(void)
786 struct sockaddr_nl snl;
787 const int buffersize = 16 * 1024 * 1024;
790 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
791 snl.nl_family = AF_NETLINK;
792 snl.nl_pid = getpid();
793 snl.nl_groups = 0xffffffff;
795 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
796 if (uevent_netlink_sock == -1) {
797 err("error getting socket, %s", strerror(errno));
801 /* set receive buffersize */
802 setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
804 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
806 err("bind failed, %s", strerror(errno));
807 close(uevent_netlink_sock);
808 uevent_netlink_sock = -1;
814 int main(int argc, char *argv[], char *envp[])
818 struct sigaction act;
821 int uevent_netlink_active = 0;
826 /* redirect std fd's, if the kernel forks us, we don't have them at all */
827 devnull = open("/dev/null", O_RDWR);
829 if (devnull != STDIN_FILENO)
830 dup2(devnull, STDIN_FILENO);
831 if (devnull != STDOUT_FILENO)
832 dup2(devnull, STDOUT_FILENO);
833 if (devnull != STDERR_FILENO)
834 dup2(devnull, STDERR_FILENO);
835 if (devnull > STDERR_FILENO)
839 logging_init("udevd");
841 err("fatal, could not open /dev/null");
844 dbg("version %s", UDEV_VERSION);
847 err("need to be root, exit");
851 /* parse commandline options */
852 for (i = 1 ; i < argc; i++) {
854 if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
855 info("will daemonize");
858 if (strcmp(arg, "--stop-exec-queue") == 0) {
859 info("will not execute events until START_EXEC_QUEUE is received");
864 /* init sockets to receive events */
865 if (init_udevd_socket() < 0) {
866 if (errno == EADDRINUSE) {
867 dbg("another udevd running, exit");
870 dbg("error initializing udevd socket: %s", strerror(errno));
876 if (init_uevent_netlink_sock() < 0) {
877 err("uevent socket not available");
882 /* parse the rules and keep it in memory */
883 udev_rules_init(&rules, 0, 1);
891 dbg("daemonized fork running");
893 /* become session leader */
895 dbg("our session is %d", sid);
898 umask(umask(077) | 022);
901 err("fork of daemon failed");
905 dbg("child [%u] running, parent exits", pid);
910 /* set a reasonable scheduling priority for the daemon */
911 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
913 /* setup signal handler pipe */
914 retval = pipe(signal_pipe);
916 err("error getting pipes: %s", strerror(errno));
919 retval = fcntl(signal_pipe[READ_END], F_SETFL, O_NONBLOCK);
921 err("error fcntl on read pipe: %s", strerror(errno));
924 retval = fcntl(signal_pipe[WRITE_END], F_SETFL, O_NONBLOCK);
926 err("error fcntl on write pipe: %s", strerror(errno));
930 /* set signal handlers */
931 memset(&act, 0x00, sizeof(struct sigaction));
932 act.sa_handler = (void (*)(int)) sig_handler;
933 sigemptyset(&act.sa_mask);
934 act.sa_flags = SA_RESTART;
935 sigaction(SIGINT, &act, NULL);
936 sigaction(SIGTERM, &act, NULL);
937 sigaction(SIGALRM, &act, NULL);
938 sigaction(SIGCHLD, &act, NULL);
939 sigaction(SIGHUP, &act, NULL);
941 /* watch rules directory */
942 inotify_fd = inotify_init();
944 inotify_add_watch(inotify_fd, udev_rules_filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
946 /* init of expected_seqnum value */
947 value = getenv("UDEVD_EXPECTED_SEQNUM");
949 expected_seqnum = strtoull(value, NULL, 10);
950 info("initialize expected_seqnum to %llu", expected_seqnum);
953 /* timeout to wait for missing events */
954 value = getenv("UDEVD_EVENT_TIMEOUT");
956 event_timeout = strtoul(value, NULL, 10);
958 event_timeout = UDEVD_EVENT_TIMEOUT;
959 info("initialize event_timeout to %u", event_timeout);
961 /* maximum limit of forked childs */
962 value = getenv("UDEVD_MAX_CHILDS");
964 max_childs = strtoul(value, NULL, 10);
966 max_childs = UDEVD_MAX_CHILDS;
967 info("initialize max_childs to %u", max_childs);
969 /* start to throttle forking if maximum number of _running_ childs is reached */
970 value = getenv("UDEVD_MAX_CHILDS_RUNNING");
972 max_childs_running = strtoull(value, NULL, 10);
974 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
975 info("initialize max_childs_running to %u", max_childs_running);
977 /* clear environment for forked event processes */
980 /* export log_priority , as called programs may want to follow that setting */
981 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
985 struct uevent_msg *msg;
989 FD_SET(signal_pipe[READ_END], &readfds);
990 FD_SET(udevd_sock, &readfds);
991 FD_SET(uevent_netlink_sock, &readfds);
993 FD_SET(inotify_fd, &readfds);
995 fdcount = select(UDEV_MAX(uevent_netlink_sock, inotify_fd)+1, &readfds, NULL, NULL, NULL);
998 dbg("error in select: %s", strerror(errno));
1002 /* get user socket message */
1003 if (FD_ISSET(udevd_sock, &readfds)) {
1004 msg = get_udevd_msg();
1006 /* discard kernel messages if netlink is active */
1007 if (uevent_netlink_active && msg->type == UDEVD_UEVENT_UDEVSEND && msg->seqnum != 0) {
1008 info("skip uevent_helper message with SEQNUM, netlink is active");
1011 msg_queue_insert(msg);
1015 /* get kernel netlink message */
1016 if (FD_ISSET(uevent_netlink_sock, &readfds)) {
1017 msg = get_netlink_msg();
1019 msg_queue_insert(msg);
1020 /* disable udevsend with first netlink message */
1021 if (!uevent_netlink_active) {
1022 info("uevent_nl message received, disable udevsend messages");
1023 uevent_netlink_active = 1;
1028 /* received a signal, clear our notification pipe */
1029 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
1032 read(signal_pipe[READ_END], &buf, sizeof(buf));
1035 /* rules directory inotify watch */
1036 if ((inotify_fd > 0) && FD_ISSET(inotify_fd, &readfds)) {
1039 /* discard all possible events, we can just reload the config */
1040 if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
1044 buf = malloc(nbytes);
1046 err("error getting buffer for inotify, disable watching");
1050 read(inotify_fd, buf, nbytes);
1055 /* rules changed, set by inotify or a signal*/
1056 if (reload_config) {
1058 udev_rules_close(&rules);
1059 udev_rules_init(&rules, 0, 1);
1062 /* forked child have returned */
1063 if (sigchilds_waiting) {
1064 sigchilds_waiting = 0;
1070 msg_queue_manager();
1074 /* clean up running_list before calling exec_queue_manager() */
1075 if (sigchilds_waiting) {
1076 sigchilds_waiting = 0;
1082 exec_queue_manager();
1087 udev_rules_close(&rules);
1089 if (signal_pipe[READ_END] > 0)
1090 close(signal_pipe[READ_END]);
1091 if (signal_pipe[WRITE_END] > 0)
1092 close(signal_pipe[WRITE_END]);
1098 if (uevent_netlink_sock > 0)
1099 close(uevent_netlink_sock);