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*/
120 if (retval == 0 && !udev->ignore_device && udev_run) {
121 struct name_entry *name_loop;
123 dbg("executing run list");
124 list_for_each_entry(name_loop, &udev->run_list, node) {
125 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
126 pass_env_to_socket(&name_loop->name[strlen("socket:")], msg->devpath, msg->action);
128 char program[PATH_SIZE];
130 strlcpy(program, name_loop->name, sizeof(program));
131 udev_rules_apply_format(udev, program, sizeof(program));
132 if (run_program(program, udev->dev->subsystem, NULL, 0, NULL,
133 (udev_log_priority >= LOG_INFO)))
139 udev_device_cleanup(udev);
149 static void export_event_state(struct uevent_msg *msg, enum event_state state)
151 char filename[PATH_SIZE];
152 char filename_failed[PATH_SIZE];
153 char target[PATH_SIZE];
154 size_t start, end, i;
155 struct uevent_msg *loop_msg;
157 /* add location of queue files */
158 strlcpy(filename, udev_root, sizeof(filename));
159 strlcat(filename, "/", sizeof(filename));
160 start = strlcat(filename, EVENT_QUEUE_DIR, sizeof(filename));
161 end = strlcat(filename, msg->devpath, sizeof(filename));
162 if (end > sizeof(filename))
163 end = sizeof(filename);
165 /* replace '/' to transform path into a filename */
166 for (i = start+1; i < end; i++)
167 if (filename[i] == '/')
168 filename[i] = PATH_TO_NAME_CHAR;
170 /* add location of failed files */
171 strlcpy(filename_failed, udev_root, sizeof(filename_failed));
172 strlcat(filename_failed, "/", sizeof(filename_failed));
173 start = strlcat(filename_failed, EVENT_FAILED_DIR, sizeof(filename_failed));
174 end = strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
175 if (end > sizeof(filename_failed))
176 end = sizeof(filename_failed);
178 /* replace '/' to transform path into a filename */
179 for (i = start+1; i < end; i++)
180 if (filename_failed[i] == '/')
181 filename_failed[i] = PATH_TO_NAME_CHAR;
185 unlink(filename_failed);
186 delete_path(filename_failed);
188 strlcpy(target, sysfs_path, sizeof(target));
189 strlcat(target, msg->devpath, sizeof(target));
190 create_path(filename);
191 symlink(target, filename);
195 unlink(filename_failed);
196 delete_path(filename_failed);
198 /* don't remove, if events for the same path are still pending */
199 list_for_each_entry(loop_msg, &running_list, node)
200 if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
203 /* move failed events to the failed directory */
204 if (state == EVENT_FAILED) {
205 create_path(filename_failed);
206 rename(filename, filename_failed);
211 /* clean up the queue directory */
212 delete_path(filename);
218 static void msg_queue_delete(struct uevent_msg *msg)
220 list_del(&msg->node);
222 /* mark as failed, if add event returns non-zero */
223 if (msg->exitstatus && strcmp(msg->action, "add") == 0)
224 export_event_state(msg, EVENT_FAILED);
226 export_event_state(msg, EVENT_FINISHED);
231 static void udev_event_run(struct uevent_msg *msg)
240 close(uevent_netlink_sock);
244 close(signal_pipe[READ_END]);
245 close(signal_pipe[WRITE_END]);
248 logging_init("udevd-event");
249 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
250 retval = udev_event_process(msg);
251 info("seq %llu finished", msg->seqnum);
258 err("fork of child failed: %s", strerror(errno));
259 msg_queue_delete(msg);
262 /* get SIGCHLD in main loop */
263 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
264 msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time);
269 static void msg_queue_insert(struct uevent_msg *msg)
271 msg->queue_time = time(NULL);
273 export_event_state(msg, EVENT_QUEUED);
275 /* run all events with a timeout set immediately */
276 if (msg->timeout != 0) {
277 list_add_tail(&msg->node, &running_list);
282 list_add_tail(&msg->node, &exec_list);
286 /* runs event and removes event from run queue when finished */
287 static int running_processes(void)
290 static char buf[4096];
295 f = open("/proc/stat", O_RDONLY);
299 len = read(f, buf, sizeof(buf)-1);
307 pos = strstr(buf, "procs_running ");
311 if (sscanf(pos, "procs_running %u", &running) != 1)
317 /* return the number of process es in our session, count only until limit */
318 static int running_processes_in_session(pid_t session, int limit)
324 dir = opendir("/proc");
328 /* read process info from /proc */
329 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
335 pid_t ppid, pgrp, sess;
338 if (!isdigit(dent->d_name[0]))
341 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
342 procdir[sizeof(procdir)-1] = '\0';
344 f = open(procdir, O_RDONLY);
348 len = read(f, line, sizeof(line)-1);
356 /* skip ugly program name */
357 pos = strrchr(line, ')') + 2;
361 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
364 /* count only processes in our session */
368 /* count only running, no sleeping processes */
373 if (limit > 0 && running >= limit)
381 static int compare_devpath(const char *running, const char *waiting)
385 for (i = 0; i < PATH_SIZE; i++) {
386 /* identical device event found */
387 if (running[i] == '\0' && waiting[i] == '\0')
390 /* parent device event found */
391 if (running[i] == '\0' && waiting[i] == '/')
394 /* child device event found */
395 if (running[i] == '/' && waiting[i] == '\0')
398 /* no matching event */
399 if (running[i] != waiting[i])
406 /* returns still running task for the same device, its parent or its physical device */
407 static int running_with_devpath(struct uevent_msg *msg, int limit)
409 struct uevent_msg *loop_msg;
410 int childs_count = 0;
412 list_for_each_entry(loop_msg, &running_list, node) {
413 if (limit && childs_count++ > limit) {
414 dbg("%llu, maximum number (%i) of child reached", msg->seqnum, childs_count);
418 /* return running parent/child device event */
419 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
420 dbg("%llu, child device event still running %llu (%s)",
421 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
425 /* return running physical device event */
426 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
427 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
428 dbg("%llu, physical device event still running %llu (%s)",
429 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
437 /* exec queue management routine executes the events and serializes events in the same sequence */
438 static void msg_queue_manager(void)
440 struct uevent_msg *loop_msg;
441 struct uevent_msg *tmp_msg;
444 if (list_empty(&exec_list))
447 running = running_processes();
448 dbg("%d processes runnning on system", running);
450 running = max_childs_running;
452 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
453 /* check running processes in our session and possibly throttle */
454 if (running >= max_childs_running) {
455 running = running_processes_in_session(sid, max_childs_running+10);
456 dbg("at least %d processes running in session", running);
457 if (running >= max_childs_running) {
458 dbg("delay seq %llu, too many processes already running", loop_msg->seqnum);
463 /* don't run two processes for the same devpath and wait for the parent*/
464 if (running_with_devpath(loop_msg, max_childs)) {
465 dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
469 /* move event to run list */
470 list_move_tail(&loop_msg->node, &running_list);
471 udev_event_run(loop_msg);
473 dbg("moved seq %llu to running list", loop_msg->seqnum);
477 static struct uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
481 struct uevent_msg *msg;
485 msg = malloc(sizeof(struct uevent_msg) + buf_size);
488 memset(msg, 0x00, sizeof(struct uevent_msg) + buf_size);
490 /* copy environment buffer and reconstruct envp */
491 memcpy(msg->envbuf, buf, buf_size);
493 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
497 key = &msg->envbuf[bufpos];
498 keylen = strlen(key);
500 bufpos += keylen + 1;
501 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
503 /* remember some keys for further processing */
504 if (strncmp(key, "ACTION=", 7) == 0)
505 msg->action = &key[7];
506 else if (strncmp(key, "DEVPATH=", 8) == 0)
507 msg->devpath = &key[8];
508 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
509 msg->subsystem = &key[10];
510 else if (strncmp(key, "SEQNUM=", 7) == 0)
511 msg->seqnum = strtoull(&key[7], NULL, 10);
512 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
513 msg->physdevpath = &key[12];
514 else if (strncmp(key, "MAJOR=", 6) == 0)
515 major = strtoull(&key[6], NULL, 10);
516 else if (strncmp(key, "MINOR=", 6) == 0)
517 minor = strtoull(&key[6], NULL, 10);
518 else if (strncmp(key, "TIMEOUT=", 8) == 0)
519 msg->timeout = strtoull(&key[8], NULL, 10);
521 msg->devt = makedev(major, minor);
522 msg->envp[i++] = "UDEVD_EVENT=1";
525 if (msg->devpath == NULL || msg->action == NULL) {
526 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 info("udevd event message received");
584 envbuf_size = size - offsetof(struct udevd_msg, envbuf);
585 dbg("envbuf_size=%i", envbuf_size);
586 msg = get_msg_from_envbuf(usend_msg.envbuf, envbuf_size);
589 msg->type = usend_msg.type;
591 case UDEVD_STOP_EXEC_QUEUE:
592 info("udevd message (STOP_EXEC_QUEUE) received");
595 case UDEVD_START_EXEC_QUEUE:
596 info("udevd message (START_EXEC_QUEUE) received");
600 case UDEVD_SET_LOG_LEVEL:
601 intval = (int *) usend_msg.envbuf;
602 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
603 udev_log_priority = *intval;
604 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
607 case UDEVD_SET_MAX_CHILDS:
608 intval = (int *) usend_msg.envbuf;
609 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
610 max_childs = *intval;
612 case UDEVD_RELOAD_RULES:
613 info("udevd message (RELOAD_RULES) received");
617 dbg("unknown message type");
622 /* receive the kernel user event message and do some sanity checks */
623 static struct uevent_msg *get_netlink_msg(void)
625 struct uevent_msg *msg;
628 static char buffer[UEVENT_BUFFER_SIZE+512];
631 size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
634 err("unable to receive kernel netlink message: %s", strerror(errno));
638 if ((size_t)size > sizeof(buffer)-1)
639 size = sizeof(buffer)-1;
641 dbg("uevent_size=%zi", size);
643 /* start of event payload */
644 bufpos = strlen(buffer)+1;
645 msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
648 msg->type = UDEVD_UEVENT_NETLINK;
650 /* validate message */
651 pos = strchr(buffer, '@');
653 err("invalid uevent '%s'", buffer);
659 if (msg->action == NULL) {
660 info("no ACTION in payload found, skip event '%s'", buffer);
665 if (strcmp(msg->action, buffer) != 0) {
666 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
674 static void asmlinkage sig_handler(int signum)
682 /* set flag, then write to pipe if needed */
683 sigchilds_waiting = 1;
690 /* write to pipe, which will wakeup select() in our mainloop */
691 write(signal_pipe[WRITE_END], "", 1);
694 static void udev_done(int pid, int exitstatus)
696 /* find msg associated with pid and delete it */
697 struct uevent_msg *msg;
699 list_for_each_entry(msg, &running_list, node) {
700 if (msg->pid == pid) {
701 info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg->seqnum, msg->pid,
702 exitstatus, time(NULL) - msg->queue_time);
703 msg->exitstatus = exitstatus;
704 msg_queue_delete(msg);
706 /* there may be events waiting with the same devpath */
713 static void reap_sigchilds(void)
719 pid = waitpid(-1, &status, WNOHANG);
722 if (WIFEXITED(status))
723 status = WEXITSTATUS(status);
724 else if (WIFSIGNALED(status))
725 status = WTERMSIG(status) + 128;
728 udev_done(pid, status);
732 static int init_udevd_socket(void)
734 struct sockaddr_un saddr;
735 const int buffersize = 16 * 1024 * 1024;
737 const int feature_on = 1;
740 memset(&saddr, 0x00, sizeof(saddr));
741 saddr.sun_family = AF_LOCAL;
742 /* use abstract namespace for socket path */
743 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
744 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
746 udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
747 if (udevd_sock == -1) {
748 err("error getting socket: %s", strerror(errno));
752 /* set receive buffersize */
753 setsockopt(udevd_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
755 /* the bind takes care of ensuring only one copy running */
756 retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
758 err("bind failed: %s", strerror(errno));
762 /* enable receiving of the sender credentials */
763 setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
768 static int init_uevent_netlink_sock(void)
770 struct sockaddr_nl snl;
771 const int buffersize = 16 * 1024 * 1024;
774 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
775 snl.nl_family = AF_NETLINK;
776 snl.nl_pid = getpid();
777 snl.nl_groups = 0xffffffff;
779 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
780 if (uevent_netlink_sock == -1) {
781 err("error getting socket: %s", strerror(errno));
785 /* set receive buffersize */
786 setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
788 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
790 err("bind failed: %s", strerror(errno));
791 close(uevent_netlink_sock);
792 uevent_netlink_sock = -1;
798 int main(int argc, char *argv[], char *envp[])
802 struct sigaction act;
810 /* redirect std fd's, if the kernel forks us, we don't have them at all */
811 fd = open("/dev/null", O_RDWR);
813 if (fd != STDIN_FILENO)
814 dup2(fd, STDIN_FILENO);
815 if (fd != STDOUT_FILENO)
816 dup2(fd, STDOUT_FILENO);
817 if (fd != STDERR_FILENO)
818 dup2(fd, STDERR_FILENO);
819 if (fd > STDERR_FILENO)
823 logging_init("udevd");
825 err("fatal, could not open /dev/null: %s", strerror(errno));
828 dbg("version %s", UDEV_VERSION);
831 err("need to be root, exit");
835 /* parse commandline options */
836 for (i = 1 ; i < argc; i++) {
838 if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
839 info("will daemonize");
842 if (strcmp(arg, "--stop-exec-queue") == 0) {
843 info("will not execute events until START_EXEC_QUEUE is received");
848 /* init sockets to receive events */
849 if (init_udevd_socket() < 0) {
850 if (errno == EADDRINUSE) {
851 err("another udevd running, exit");
854 err("error initializing udevd socket: %s", strerror(errno));
860 if (init_uevent_netlink_sock() < 0) {
861 err("uevent socket not available");
866 /* parse the rules and keep it in memory */
868 udev_rules_init(&rules, 1);
876 dbg("daemonized fork running");
879 err("fork of daemon failed: %s", strerror(errno));
883 dbg("child [%u] running, parent exits", pid);
888 /* set scheduling priority for the daemon */
889 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
894 /* become session leader */
896 dbg("our session is %d", sid);
898 /* OOM_DISABLE == -17 */
899 fd = open("/proc/self/oom_adj", O_RDWR);
901 err("error disabling OOM: %s", strerror(errno));
907 /* setup signal handler pipe */
908 retval = pipe(signal_pipe);
910 err("error getting pipes: %s", strerror(errno));
913 retval = fcntl(signal_pipe[READ_END], F_SETFL, O_NONBLOCK);
915 err("error fcntl on read pipe: %s", strerror(errno));
918 retval = fcntl(signal_pipe[WRITE_END], F_SETFL, O_NONBLOCK);
920 err("error fcntl on write pipe: %s", strerror(errno));
924 /* set signal handlers */
925 memset(&act, 0x00, sizeof(struct sigaction));
926 act.sa_handler = (void (*)(int)) sig_handler;
927 sigemptyset(&act.sa_mask);
928 act.sa_flags = SA_RESTART;
929 sigaction(SIGINT, &act, NULL);
930 sigaction(SIGTERM, &act, NULL);
931 sigaction(SIGCHLD, &act, NULL);
932 sigaction(SIGHUP, &act, NULL);
934 /* watch rules directory */
935 inotify_fd = inotify_init();
937 inotify_add_watch(inotify_fd, udev_rules_filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
939 /* maximum limit of forked childs */
940 value = getenv("UDEVD_MAX_CHILDS");
942 max_childs = strtoul(value, NULL, 10);
944 max_childs = UDEVD_MAX_CHILDS;
945 info("initialize max_childs to %u", max_childs);
947 /* start to throttle forking if maximum number of _running_ childs is reached */
948 value = getenv("UDEVD_MAX_CHILDS_RUNNING");
950 max_childs_running = strtoull(value, NULL, 10);
952 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
953 info("initialize max_childs_running to %u", max_childs_running);
955 /* clear environment for forked event processes */
958 /* export log_priority , as called programs may want to follow that setting */
959 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
963 maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
964 maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
965 maxfd = UDEV_MAX(maxfd, inotify_fd);
968 struct uevent_msg *msg;
972 FD_SET(signal_pipe[READ_END], &readfds);
973 FD_SET(udevd_sock, &readfds);
974 FD_SET(uevent_netlink_sock, &readfds);
976 FD_SET(inotify_fd, &readfds);
978 fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
981 err("error in select: %s", strerror(errno));
985 /* get user socket message */
986 if (FD_ISSET(udevd_sock, &readfds)) {
987 msg = get_udevd_msg();
989 msg_queue_insert(msg);
992 /* get kernel netlink message */
993 if (FD_ISSET(uevent_netlink_sock, &readfds)) {
994 msg = get_netlink_msg();
996 msg_queue_insert(msg);
999 /* received a signal, clear our notification pipe */
1000 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
1003 read(signal_pipe[READ_END], &buf, sizeof(buf));
1006 /* rules directory inotify watch */
1007 if ((inotify_fd > 0) && FD_ISSET(inotify_fd, &readfds)) {
1010 /* discard all possible events, we can just reload the config */
1011 if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
1015 buf = malloc(nbytes);
1017 err("error getting buffer for inotify, disable watching");
1021 read(inotify_fd, buf, nbytes);
1026 /* rules changed, set by inotify or a signal*/
1027 if (reload_config) {
1029 udev_rules_cleanup(&rules);
1030 udev_rules_init(&rules, 1);
1033 /* forked child has returned */
1034 if (sigchilds_waiting) {
1035 sigchilds_waiting = 0;
1042 msg_queue_manager();
1047 udev_rules_cleanup(&rules);
1050 if (signal_pipe[READ_END] > 0)
1051 close(signal_pipe[READ_END]);
1052 if (signal_pipe[WRITE_END] > 0)
1053 close(signal_pipe[WRITE_END]);
1059 if (uevent_netlink_sock > 0)
1060 close(uevent_netlink_sock);