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.
35 #include <sys/select.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include <linux/types.h>
43 #include <linux/netlink.h>
46 #include "udev_rules.h"
49 static struct udev_rules rules;
50 static int udevd_sock;
51 static int uevent_netlink_sock;
52 static int inotify_fd;
55 static int signal_pipe[2] = {-1, -1};
56 static volatile int sigchilds_waiting;
57 static volatile int udev_exit;
58 static volatile int reload_config;
59 static int run_exec_q;
60 static int stop_exec_q;
61 static int max_childs;
62 static int max_childs_running;
63 static char udev_log[32];
65 static LIST_HEAD(exec_list);
66 static LIST_HEAD(running_list);
70 void log_message(int priority, const char *format, ...)
74 if (priority > udev_log_priority)
77 va_start(args, format);
78 vsyslog(priority, format, args);
83 static void asmlinkage udev_event_sig_handler(int signum)
85 if (signum == SIGALRM)
89 static int udev_event_process(struct uevent_msg *msg)
96 /* set signal handlers */
97 memset(&act, 0x00, sizeof(act));
98 act.sa_handler = (void (*)(int)) udev_event_sig_handler;
99 sigemptyset (&act.sa_mask);
101 sigaction(SIGALRM, &act, NULL);
103 /* trigger timeout to prevent hanging processes */
104 alarm(UDEV_ALARM_TIMEOUT);
106 /* reconstruct event environment from message */
107 for (i = 0; msg->envp[i]; i++)
108 putenv(msg->envp[i]);
110 udev = udev_device_init();
113 strlcpy(udev->action, msg->action, sizeof(udev->action));
114 sysfs_device_set_values(udev->dev, msg->devpath, msg->subsystem);
115 udev->devt = msg->devt;
117 retval = udev_device_event(&rules, udev);
119 /* run programs collected by RUN-key*/
121 struct name_entry *name_loop;
123 list_for_each_entry(name_loop, &udev->run_list, node) {
124 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
125 pass_env_to_socket(&name_loop->name[strlen("socket:")], msg->devpath, msg->action);
127 char program[PATH_SIZE];
129 strlcpy(program, name_loop->name, sizeof(program));
130 apply_format(udev, program, sizeof(program));
131 if (run_program(program, udev->dev->subsystem, NULL, 0, NULL,
132 (udev_log_priority >= LOG_INFO)))
138 udev_device_cleanup(udev);
148 static void export_event_state(struct uevent_msg *msg, enum event_state state)
150 char filename[PATH_SIZE];
151 char filename_failed[PATH_SIZE];
152 char target[PATH_SIZE];
153 size_t start, end, i;
154 struct uevent_msg *loop_msg;
156 /* add location of queue files */
157 strlcpy(filename, udev_root, sizeof(filename));
158 strlcat(filename, "/", sizeof(filename));
159 start = strlcat(filename, EVENT_QUEUE_DIR, sizeof(filename));
160 end = strlcat(filename, msg->devpath, sizeof(filename));
161 if (end > sizeof(filename))
162 end = sizeof(filename);
164 /* replace '/' to transform path into a filename */
165 for (i = start+1; i < end; i++)
166 if (filename[i] == '/')
167 filename[i] = PATH_TO_NAME_CHAR;
169 /* add location of failed files */
170 strlcpy(filename_failed, udev_root, sizeof(filename_failed));
171 strlcat(filename_failed, "/", sizeof(filename_failed));
172 start = strlcat(filename_failed, EVENT_FAILED_DIR, sizeof(filename_failed));
173 end = strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
174 if (end > sizeof(filename_failed))
175 end = sizeof(filename_failed);
177 /* replace '/' to transform path into a filename */
178 for (i = start+1; i < end; i++)
179 if (filename_failed[i] == '/')
180 filename_failed[i] = PATH_TO_NAME_CHAR;
184 unlink(filename_failed);
185 delete_path(filename_failed);
187 strlcpy(target, sysfs_path, sizeof(target));
188 strlcat(target, msg->devpath, sizeof(target));
189 create_path(filename);
190 symlink(target, filename);
194 unlink(filename_failed);
195 delete_path(filename_failed);
197 /* don't remove, if events for the same path are still pending */
198 list_for_each_entry(loop_msg, &running_list, node)
199 if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
202 /* move failed events to the failed directory */
203 if (state == EVENT_FAILED) {
204 create_path(filename_failed);
205 rename(filename, filename_failed);
210 /* clean up the queue directory */
211 delete_path(filename);
217 static void msg_queue_delete(struct uevent_msg *msg)
219 list_del(&msg->node);
221 /* mark as failed, if add event returns non-zero */
222 if (msg->exitstatus && strcmp(msg->action, "add") == 0)
223 export_event_state(msg, EVENT_FAILED);
225 export_event_state(msg, EVENT_FINISHED);
230 static void udev_event_run(struct uevent_msg *msg)
239 close(uevent_netlink_sock);
243 close(signal_pipe[READ_END]);
244 close(signal_pipe[WRITE_END]);
247 logging_init("udevd-event");
248 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
249 retval = udev_event_process(msg);
250 info("seq %llu finished", msg->seqnum);
257 err("fork of child failed: %s", strerror(errno));
258 msg_queue_delete(msg);
261 /* get SIGCHLD in main loop */
262 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
263 msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time);
268 static void msg_queue_insert(struct uevent_msg *msg)
270 msg->queue_time = time(NULL);
272 export_event_state(msg, EVENT_QUEUED);
274 /* run all events with a timeout set immediately */
275 if (msg->timeout != 0) {
276 list_add_tail(&msg->node, &running_list);
281 list_add_tail(&msg->node, &exec_list);
285 /* runs event and removes event from run queue when finished */
286 static int running_processes(void)
289 static char buf[4096];
294 f = open("/proc/stat", O_RDONLY);
298 len = read(f, buf, sizeof(buf)-1);
306 pos = strstr(buf, "procs_running ");
310 if (sscanf(pos, "procs_running %u", &running) != 1)
316 /* return the number of process es in our session, count only until limit */
317 static int running_processes_in_session(pid_t session, int limit)
323 dir = opendir("/proc");
327 /* read process info from /proc */
328 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
334 pid_t ppid, pgrp, sess;
337 if (!isdigit(dent->d_name[0]))
340 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
341 procdir[sizeof(procdir)-1] = '\0';
343 f = open(procdir, O_RDONLY);
347 len = read(f, line, sizeof(line)-1);
355 /* skip ugly program name */
356 pos = strrchr(line, ')') + 2;
360 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
363 /* count only processes in our session */
367 /* count only running, no sleeping processes */
372 if (limit > 0 && running >= limit)
380 static int compare_devpath(const char *running, const char *waiting)
384 for (i = 0; i < PATH_SIZE; i++) {
385 /* identical device event found */
386 if (running[i] == '\0' && waiting[i] == '\0')
389 /* parent device event found */
390 if (running[i] == '\0' && waiting[i] == '/')
393 /* child device event found */
394 if (running[i] == '/' && waiting[i] == '\0')
397 /* no matching event */
398 if (running[i] != waiting[i])
405 /* returns still running task for the same device, its parent or its physical device */
406 static int running_with_devpath(struct uevent_msg *msg, int limit)
408 struct uevent_msg *loop_msg;
409 int childs_count = 0;
411 list_for_each_entry(loop_msg, &running_list, node) {
412 if (limit && childs_count++ > limit) {
413 dbg("%llu, maximum number (%i) of child reached", msg->seqnum, childs_count);
417 /* return running parent/child device event */
418 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
419 dbg("%llu, child device event still running %llu (%s)",
420 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
424 /* return running physical device event */
425 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
426 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
427 dbg("%llu, physical device event still running %llu (%s)",
428 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
436 /* exec queue management routine executes the events and serializes events in the same sequence */
437 static void msg_queue_manager(void)
439 struct uevent_msg *loop_msg;
440 struct uevent_msg *tmp_msg;
443 if (list_empty(&exec_list))
446 running = running_processes();
447 dbg("%d processes runnning on system", running);
449 running = max_childs_running;
451 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
452 /* check running processes in our session and possibly throttle */
453 if (running >= max_childs_running) {
454 running = running_processes_in_session(sid, max_childs_running+10);
455 dbg("at least %d processes running in session", running);
456 if (running >= max_childs_running) {
457 dbg("delay seq %llu, too many processes already running", loop_msg->seqnum);
462 /* don't run two processes for the same devpath and wait for the parent*/
463 if (running_with_devpath(loop_msg, max_childs)) {
464 dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
468 /* move event to run list */
469 list_move_tail(&loop_msg->node, &running_list);
470 udev_event_run(loop_msg);
472 dbg("moved seq %llu to running list", loop_msg->seqnum);
476 static struct uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
480 struct uevent_msg *msg;
484 msg = malloc(sizeof(struct uevent_msg) + buf_size);
487 memset(msg, 0x00, sizeof(struct uevent_msg) + buf_size);
489 /* copy environment buffer and reconstruct envp */
490 memcpy(msg->envbuf, buf, buf_size);
492 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
496 key = &msg->envbuf[bufpos];
497 keylen = strlen(key);
499 bufpos += keylen + 1;
500 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
502 /* remember some keys for further processing */
503 if (strncmp(key, "ACTION=", 7) == 0)
504 msg->action = &key[7];
505 else if (strncmp(key, "DEVPATH=", 8) == 0)
506 msg->devpath = &key[8];
507 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
508 msg->subsystem = &key[10];
509 else if (strncmp(key, "SEQNUM=", 7) == 0)
510 msg->seqnum = strtoull(&key[7], NULL, 10);
511 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
512 msg->physdevpath = &key[12];
513 else if (strncmp(key, "MAJOR=", 6) == 0)
514 major = strtoull(&key[6], NULL, 10);
515 else if (strncmp(key, "MINOR=", 6) == 0)
516 minor = strtoull(&key[6], NULL, 10);
517 else if (strncmp(key, "TIMEOUT=", 8) == 0)
518 msg->timeout = strtoull(&key[8], NULL, 10);
520 msg->devt = makedev(major, minor);
521 msg->envp[i++] = "UDEVD_EVENT=1";
524 if (msg->devpath == NULL || msg->action == NULL) {
525 info("DEVPATH or ACTION missing, ignore message");
533 /* receive the udevd message from userspace */
534 static struct uevent_msg *get_udevd_msg(void)
536 static struct udevd_msg usend_msg;
537 struct uevent_msg *msg;
540 struct cmsghdr *cmsg;
543 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
547 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
548 iov.iov_base = &usend_msg;
549 iov.iov_len = sizeof(struct udevd_msg);
551 memset(&smsg, 0x00, sizeof(struct msghdr));
554 smsg.msg_control = cred_msg;
555 smsg.msg_controllen = sizeof(cred_msg);
557 size = recvmsg(udevd_sock, &smsg, 0);
560 err("unable to receive user udevd message: %s", strerror(errno));
563 cmsg = CMSG_FIRSTHDR(&smsg);
564 cred = (struct ucred *) CMSG_DATA(cmsg);
566 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
567 err("no sender credentials received, message ignored");
571 if (cred->uid != 0) {
572 err("sender uid=%i, message ignored", cred->uid);
576 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
577 err("message magic '%s' doesn't match, ignore it", usend_msg.magic);
581 switch (usend_msg.type) {
582 case UDEVD_UEVENT_UDEVSEND:
583 case UDEVD_UEVENT_INITSEND:
584 info("udevd event message received");
585 envbuf_size = size - offsetof(struct udevd_msg, envbuf);
586 dbg("envbuf_size=%i", envbuf_size);
587 msg = get_msg_from_envbuf(usend_msg.envbuf, envbuf_size);
590 msg->type = usend_msg.type;
592 case UDEVD_STOP_EXEC_QUEUE:
593 info("udevd message (STOP_EXEC_QUEUE) received");
596 case UDEVD_START_EXEC_QUEUE:
597 info("udevd message (START_EXEC_QUEUE) received");
601 case UDEVD_SET_LOG_LEVEL:
602 intval = (int *) usend_msg.envbuf;
603 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
604 udev_log_priority = *intval;
605 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
608 case UDEVD_SET_MAX_CHILDS:
609 intval = (int *) usend_msg.envbuf;
610 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
611 max_childs = *intval;
613 case UDEVD_RELOAD_RULES:
614 info("udevd message (RELOAD_RULES) received");
618 dbg("unknown message type");
623 /* receive the kernel user event message and do some sanity checks */
624 static struct uevent_msg *get_netlink_msg(void)
626 struct uevent_msg *msg;
629 static char buffer[UEVENT_BUFFER_SIZE+512];
632 size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
635 err("unable to receive kernel netlink message: %s", strerror(errno));
639 if ((size_t)size > sizeof(buffer)-1)
640 size = sizeof(buffer)-1;
642 dbg("uevent_size=%zi", size);
644 /* start of event payload */
645 bufpos = strlen(buffer)+1;
646 msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
649 msg->type = UDEVD_UEVENT_NETLINK;
651 /* validate message */
652 pos = strchr(buffer, '@');
654 err("invalid uevent '%s'", buffer);
660 if (msg->action == NULL) {
661 info("no ACTION in payload found, skip event '%s'", buffer);
666 if (strcmp(msg->action, buffer) != 0) {
667 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
675 static void asmlinkage sig_handler(int signum)
683 /* set flag, then write to pipe if needed */
684 sigchilds_waiting = 1;
691 /* write to pipe, which will wakeup select() in our mainloop */
692 write(signal_pipe[WRITE_END], "", 1);
695 static void udev_done(int pid, int exitstatus)
697 /* find msg associated with pid and delete it */
698 struct uevent_msg *msg;
700 list_for_each_entry(msg, &running_list, node) {
701 if (msg->pid == pid) {
702 info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg->seqnum, msg->pid,
703 exitstatus, time(NULL) - msg->queue_time);
704 msg->exitstatus = exitstatus;
705 msg_queue_delete(msg);
707 /* there may be events waiting with the same devpath */
714 static void reap_sigchilds(void)
720 pid = waitpid(-1, &status, WNOHANG);
723 if (WIFEXITED(status))
724 status = WEXITSTATUS(status);
725 else if (WIFSIGNALED(status))
726 status = WTERMSIG(status) + 128;
729 udev_done(pid, status);
733 static int init_udevd_socket(void)
735 struct sockaddr_un saddr;
736 const int buffersize = 16 * 1024 * 1024;
738 const int feature_on = 1;
741 memset(&saddr, 0x00, sizeof(saddr));
742 saddr.sun_family = AF_LOCAL;
743 /* use abstract namespace for socket path */
744 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
745 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
747 udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
748 if (udevd_sock == -1) {
749 err("error getting socket: %s", strerror(errno));
753 /* set receive buffersize */
754 setsockopt(udevd_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
756 /* the bind takes care of ensuring only one copy running */
757 retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
759 err("bind failed: %s", strerror(errno));
763 /* enable receiving of the sender credentials */
764 setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
769 static int init_uevent_netlink_sock(void)
771 struct sockaddr_nl snl;
772 const int buffersize = 16 * 1024 * 1024;
775 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
776 snl.nl_family = AF_NETLINK;
777 snl.nl_pid = getpid();
778 snl.nl_groups = 0xffffffff;
780 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
781 if (uevent_netlink_sock == -1) {
782 err("error getting socket: %s", strerror(errno));
786 /* set receive buffersize */
787 setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
789 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
791 err("bind failed: %s", strerror(errno));
792 close(uevent_netlink_sock);
793 uevent_netlink_sock = -1;
799 int main(int argc, char *argv[], char *envp[])
803 struct sigaction act;
811 /* redirect std fd's, if the kernel forks us, we don't have them at all */
812 fd = open("/dev/null", O_RDWR);
814 if (fd != STDIN_FILENO)
815 dup2(fd, STDIN_FILENO);
816 if (fd != STDOUT_FILENO)
817 dup2(fd, STDOUT_FILENO);
818 if (fd != STDERR_FILENO)
819 dup2(fd, STDERR_FILENO);
820 if (fd > STDERR_FILENO)
824 logging_init("udevd");
826 err("fatal, could not open /dev/null: %s", strerror(errno));
829 dbg("version %s", UDEV_VERSION);
832 err("need to be root, exit");
836 /* parse commandline options */
837 for (i = 1 ; i < argc; i++) {
839 if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
840 info("will daemonize");
843 if (strcmp(arg, "--stop-exec-queue") == 0) {
844 info("will not execute events until START_EXEC_QUEUE is received");
849 /* init sockets to receive events */
850 if (init_udevd_socket() < 0) {
851 if (errno == EADDRINUSE) {
852 err("another udevd running, exit");
855 err("error initializing udevd socket: %s", strerror(errno));
861 if (init_uevent_netlink_sock() < 0) {
862 err("uevent socket not available");
867 /* parse the rules and keep it in memory */
869 udev_rules_init(&rules, 1);
877 dbg("daemonized fork running");
880 err("fork of daemon failed: %s", strerror(errno));
884 dbg("child [%u] running, parent exits", pid);
889 /* set scheduling priority for the daemon */
890 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
895 /* become session leader */
897 dbg("our session is %d", sid);
899 /* OOM_DISABLE == -17 */
900 fd = open("/proc/self/oom_adj", O_RDWR);
902 err("error disabling OOM: %s", strerror(errno));
908 /* setup signal handler pipe */
909 retval = pipe(signal_pipe);
911 err("error getting pipes: %s", strerror(errno));
914 retval = fcntl(signal_pipe[READ_END], F_SETFL, O_NONBLOCK);
916 err("error fcntl on read pipe: %s", strerror(errno));
919 retval = fcntl(signal_pipe[WRITE_END], F_SETFL, O_NONBLOCK);
921 err("error fcntl on write pipe: %s", strerror(errno));
925 /* set signal handlers */
926 memset(&act, 0x00, sizeof(struct sigaction));
927 act.sa_handler = (void (*)(int)) sig_handler;
928 sigemptyset(&act.sa_mask);
929 act.sa_flags = SA_RESTART;
930 sigaction(SIGINT, &act, NULL);
931 sigaction(SIGTERM, &act, NULL);
932 sigaction(SIGCHLD, &act, NULL);
933 sigaction(SIGHUP, &act, NULL);
935 /* watch rules directory */
936 inotify_fd = inotify_init();
938 inotify_add_watch(inotify_fd, udev_rules_filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
940 /* maximum limit of forked childs */
941 value = getenv("UDEVD_MAX_CHILDS");
943 max_childs = strtoul(value, NULL, 10);
945 max_childs = UDEVD_MAX_CHILDS;
946 info("initialize max_childs to %u", max_childs);
948 /* start to throttle forking if maximum number of _running_ childs is reached */
949 value = getenv("UDEVD_MAX_CHILDS_RUNNING");
951 max_childs_running = strtoull(value, NULL, 10);
953 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
954 info("initialize max_childs_running to %u", max_childs_running);
956 /* clear environment for forked event processes */
959 /* export log_priority , as called programs may want to follow that setting */
960 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
964 maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
965 maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
966 maxfd = UDEV_MAX(maxfd, inotify_fd);
969 struct uevent_msg *msg;
973 FD_SET(signal_pipe[READ_END], &readfds);
974 FD_SET(udevd_sock, &readfds);
975 FD_SET(uevent_netlink_sock, &readfds);
977 FD_SET(inotify_fd, &readfds);
979 fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
982 err("error in select: %s", strerror(errno));
986 /* get user socket message */
987 if (FD_ISSET(udevd_sock, &readfds)) {
988 msg = get_udevd_msg();
990 msg_queue_insert(msg);
993 /* get kernel netlink message */
994 if (FD_ISSET(uevent_netlink_sock, &readfds)) {
995 msg = get_netlink_msg();
997 msg_queue_insert(msg);
1000 /* received a signal, clear our notification pipe */
1001 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
1004 read(signal_pipe[READ_END], &buf, sizeof(buf));
1007 /* rules directory inotify watch */
1008 if ((inotify_fd > 0) && FD_ISSET(inotify_fd, &readfds)) {
1011 /* discard all possible events, we can just reload the config */
1012 if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
1016 buf = malloc(nbytes);
1018 err("error getting buffer for inotify, disable watching");
1022 read(inotify_fd, buf, nbytes);
1027 /* rules changed, set by inotify or a signal*/
1028 if (reload_config) {
1030 udev_rules_cleanup(&rules);
1031 udev_rules_init(&rules, 1);
1034 /* forked child has returned */
1035 if (sigchilds_waiting) {
1036 sigchilds_waiting = 0;
1043 msg_queue_manager();
1048 udev_rules_cleanup(&rules);
1051 if (signal_pipe[READ_END] > 0)
1052 close(signal_pipe[READ_END]);
1053 if (signal_pipe[WRITE_END] > 0)
1054 close(signal_pipe[WRITE_END]);
1060 if (uevent_netlink_sock > 0)
1061 close(uevent_netlink_sock);