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 if (run_program(name_loop->name, udev->dev->subsystem, NULL, 0, NULL,
128 (udev_log_priority >= LOG_INFO)))
133 udev_device_cleanup(udev);
143 static void export_event_state(struct uevent_msg *msg, enum event_state state)
145 char filename[PATH_SIZE];
146 char filename_failed[PATH_SIZE];
147 char target[PATH_SIZE];
148 size_t start, end, i;
149 struct uevent_msg *loop_msg;
151 /* add location of queue files */
152 strlcpy(filename, udev_root, sizeof(filename));
153 strlcat(filename, "/", sizeof(filename));
154 start = strlcat(filename, EVENT_QUEUE_DIR, sizeof(filename));
155 end = strlcat(filename, msg->devpath, sizeof(filename));
156 if (end > sizeof(filename))
157 end = sizeof(filename);
159 /* replace '/' to transform path into a filename */
160 for (i = start+1; i < end; i++)
161 if (filename[i] == '/')
162 filename[i] = PATH_TO_NAME_CHAR;
164 /* add location of failed files */
165 strlcpy(filename_failed, udev_root, sizeof(filename_failed));
166 strlcat(filename_failed, "/", sizeof(filename_failed));
167 start = strlcat(filename_failed, EVENT_FAILED_DIR, sizeof(filename_failed));
168 end = strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
169 if (end > sizeof(filename_failed))
170 end = sizeof(filename_failed);
172 /* replace '/' to transform path into a filename */
173 for (i = start+1; i < end; i++)
174 if (filename_failed[i] == '/')
175 filename_failed[i] = PATH_TO_NAME_CHAR;
179 unlink(filename_failed);
180 delete_path(filename_failed);
182 strlcpy(target, sysfs_path, sizeof(target));
183 strlcat(target, msg->devpath, sizeof(target));
184 create_path(filename);
185 symlink(target, filename);
189 unlink(filename_failed);
190 delete_path(filename_failed);
192 /* don't remove, if events for the same path are still pending */
193 list_for_each_entry(loop_msg, &running_list, node)
194 if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
197 /* move failed events to the failed directory */
198 if (state == EVENT_FAILED) {
199 create_path(filename_failed);
200 rename(filename, filename_failed);
205 /* clean up the queue directory */
206 delete_path(filename);
212 static void msg_queue_delete(struct uevent_msg *msg)
214 list_del(&msg->node);
216 /* mark as failed, if add event returns non-zero */
217 if (msg->exitstatus && strcmp(msg->action, "add") == 0)
218 export_event_state(msg, EVENT_FAILED);
220 export_event_state(msg, EVENT_FINISHED);
225 static void udev_event_run(struct uevent_msg *msg)
234 close(uevent_netlink_sock);
238 close(signal_pipe[READ_END]);
239 close(signal_pipe[WRITE_END]);
242 logging_init("udevd-event");
243 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
244 retval = udev_event_process(msg);
245 info("seq %llu finished", msg->seqnum);
252 err("fork of child failed: %s", strerror(errno));
253 msg_queue_delete(msg);
256 /* get SIGCHLD in main loop */
257 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
258 msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time);
263 static void msg_queue_insert(struct uevent_msg *msg)
265 msg->queue_time = time(NULL);
267 export_event_state(msg, EVENT_QUEUED);
269 /* run all events with a timeout set immediately */
270 if (msg->timeout != 0) {
271 list_add_tail(&msg->node, &running_list);
276 list_add_tail(&msg->node, &exec_list);
280 /* runs event and removes event from run queue when finished */
281 static int running_processes(void)
284 static char buf[4096];
289 f = open("/proc/stat", O_RDONLY);
293 len = read(f, buf, sizeof(buf)-1);
301 pos = strstr(buf, "procs_running ");
305 if (sscanf(pos, "procs_running %u", &running) != 1)
311 /* return the number of process es in our session, count only until limit */
312 static int running_processes_in_session(pid_t session, int limit)
318 dir = opendir("/proc");
322 /* read process info from /proc */
323 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
329 pid_t ppid, pgrp, sess;
332 if (!isdigit(dent->d_name[0]))
335 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
336 procdir[sizeof(procdir)-1] = '\0';
338 f = open(procdir, O_RDONLY);
342 len = read(f, line, sizeof(line)-1);
350 /* skip ugly program name */
351 pos = strrchr(line, ')') + 2;
355 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
358 /* count only processes in our session */
362 /* count only running, no sleeping processes */
367 if (limit > 0 && running >= limit)
375 static int compare_devpath(const char *running, const char *waiting)
379 for (i = 0; i < PATH_SIZE; i++) {
380 /* identical device event found */
381 if (running[i] == '\0' && waiting[i] == '\0')
384 /* parent device event found */
385 if (running[i] == '\0' && waiting[i] == '/')
388 /* child device event found */
389 if (running[i] == '/' && waiting[i] == '\0')
392 /* no matching event */
393 if (running[i] != waiting[i])
400 /* returns still running task for the same device, its parent or its physical device */
401 static int running_with_devpath(struct uevent_msg *msg, int limit)
403 struct uevent_msg *loop_msg;
404 int childs_count = 0;
406 list_for_each_entry(loop_msg, &running_list, node) {
407 if (limit && childs_count++ > limit) {
408 dbg("%llu, maximum number (%i) of child reached", msg->seqnum, childs_count);
412 /* return running parent/child device event */
413 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
414 dbg("%llu, child device event still running %llu (%s)",
415 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
419 /* return running physical device event */
420 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
421 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
422 dbg("%llu, physical device event still running %llu (%s)",
423 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
431 /* exec queue management routine executes the events and serializes events in the same sequence */
432 static void msg_queue_manager(void)
434 struct uevent_msg *loop_msg;
435 struct uevent_msg *tmp_msg;
438 if (list_empty(&exec_list))
441 running = running_processes();
442 dbg("%d processes runnning on system", running);
444 running = max_childs_running;
446 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
447 /* check running processes in our session and possibly throttle */
448 if (running >= max_childs_running) {
449 running = running_processes_in_session(sid, max_childs_running+10);
450 dbg("at least %d processes running in session", running);
451 if (running >= max_childs_running) {
452 dbg("delay seq %llu, too many processes already running", loop_msg->seqnum);
457 /* don't run two processes for the same devpath and wait for the parent*/
458 if (running_with_devpath(loop_msg, max_childs)) {
459 dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
463 /* move event to run list */
464 list_move_tail(&loop_msg->node, &running_list);
465 udev_event_run(loop_msg);
467 dbg("moved seq %llu to running list", loop_msg->seqnum);
471 static struct uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
475 struct uevent_msg *msg;
479 msg = malloc(sizeof(struct uevent_msg) + buf_size);
482 memset(msg, 0x00, sizeof(struct uevent_msg) + buf_size);
484 /* copy environment buffer and reconstruct envp */
485 memcpy(msg->envbuf, buf, buf_size);
487 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
491 key = &msg->envbuf[bufpos];
492 keylen = strlen(key);
494 bufpos += keylen + 1;
495 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
497 /* remember some keys for further processing */
498 if (strncmp(key, "ACTION=", 7) == 0)
499 msg->action = &key[7];
500 else if (strncmp(key, "DEVPATH=", 8) == 0)
501 msg->devpath = &key[8];
502 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
503 msg->subsystem = &key[10];
504 else if (strncmp(key, "SEQNUM=", 7) == 0)
505 msg->seqnum = strtoull(&key[7], NULL, 10);
506 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
507 msg->physdevpath = &key[12];
508 else if (strncmp(key, "MAJOR=", 6) == 0)
509 major = strtoull(&key[6], NULL, 10);
510 else if (strncmp(key, "MINOR=", 6) == 0)
511 minor = strtoull(&key[6], NULL, 10);
512 else if (strncmp(key, "TIMEOUT=", 8) == 0)
513 msg->timeout = strtoull(&key[8], NULL, 10);
515 msg->devt = makedev(major, minor);
516 msg->envp[i++] = "UDEVD_EVENT=1";
520 info("DEVPATH missing, ignore message");
528 /* receive the udevd message from userspace */
529 static struct uevent_msg *get_udevd_msg(void)
531 static struct udevd_msg usend_msg;
532 struct uevent_msg *msg;
535 struct cmsghdr *cmsg;
538 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
542 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
543 iov.iov_base = &usend_msg;
544 iov.iov_len = sizeof(struct udevd_msg);
546 memset(&smsg, 0x00, sizeof(struct msghdr));
549 smsg.msg_control = cred_msg;
550 smsg.msg_controllen = sizeof(cred_msg);
552 size = recvmsg(udevd_sock, &smsg, 0);
555 err("unable to receive user udevd message: %s", strerror(errno));
558 cmsg = CMSG_FIRSTHDR(&smsg);
559 cred = (struct ucred *) CMSG_DATA(cmsg);
561 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
562 err("no sender credentials received, message ignored");
566 if (cred->uid != 0) {
567 err("sender uid=%i, message ignored", cred->uid);
571 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
572 err("message magic '%s' doesn't match, ignore it", usend_msg.magic);
576 switch (usend_msg.type) {
577 case UDEVD_UEVENT_UDEVSEND:
578 case UDEVD_UEVENT_INITSEND:
579 info("udevd event message received");
580 envbuf_size = size - offsetof(struct udevd_msg, envbuf);
581 dbg("envbuf_size=%i", envbuf_size);
582 msg = get_msg_from_envbuf(usend_msg.envbuf, envbuf_size);
585 msg->type = usend_msg.type;
587 case UDEVD_STOP_EXEC_QUEUE:
588 info("udevd message (STOP_EXEC_QUEUE) received");
591 case UDEVD_START_EXEC_QUEUE:
592 info("udevd message (START_EXEC_QUEUE) received");
596 case UDEVD_SET_LOG_LEVEL:
597 intval = (int *) usend_msg.envbuf;
598 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
599 udev_log_priority = *intval;
600 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
603 case UDEVD_SET_MAX_CHILDS:
604 intval = (int *) usend_msg.envbuf;
605 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
606 max_childs = *intval;
608 case UDEVD_RELOAD_RULES:
609 info("udevd message (RELOAD_RULES) received");
613 dbg("unknown message type");
618 /* receive the kernel user event message and do some sanity checks */
619 static struct uevent_msg *get_netlink_msg(void)
621 struct uevent_msg *msg;
624 static char buffer[UEVENT_BUFFER_SIZE + 512];
627 size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
630 err("unable to receive kernel netlink message: %s", strerror(errno));
634 if ((size_t)size > sizeof(buffer)-1)
635 size = sizeof(buffer)-1;
637 dbg("uevent_size=%zi", size);
639 /* start of event payload */
640 bufpos = strlen(buffer)+1;
641 msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
644 msg->type = UDEVD_UEVENT_NETLINK;
646 /* validate message */
647 pos = strchr(buffer, '@');
649 err("invalid uevent '%s'", buffer);
655 if (msg->action == NULL) {
656 info("no ACTION in payload found, skip event '%s'", buffer);
661 if (strcmp(msg->action, buffer) != 0) {
662 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
670 static void asmlinkage sig_handler(int signum)
678 /* set flag, then write to pipe if needed */
679 sigchilds_waiting = 1;
686 /* write to pipe, which will wakeup select() in our mainloop */
687 write(signal_pipe[WRITE_END], "", 1);
690 static void udev_done(int pid, int exitstatus)
692 /* find msg associated with pid and delete it */
693 struct uevent_msg *msg;
695 list_for_each_entry(msg, &running_list, node) {
696 if (msg->pid == pid) {
697 info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg->seqnum, msg->pid,
698 exitstatus, time(NULL) - msg->queue_time);
699 msg->exitstatus = exitstatus;
700 msg_queue_delete(msg);
702 /* there may be events waiting with the same devpath */
709 static void reap_sigchilds(void)
715 pid = waitpid(-1, &status, WNOHANG);
718 if (WIFEXITED(status))
719 status = WEXITSTATUS(status);
720 else if (WIFSIGNALED(status))
721 status = WTERMSIG(status) + 128;
724 udev_done(pid, status);
728 static int init_udevd_socket(void)
730 struct sockaddr_un saddr;
731 const int buffersize = 16 * 1024 * 1024;
733 const int feature_on = 1;
736 memset(&saddr, 0x00, sizeof(saddr));
737 saddr.sun_family = AF_LOCAL;
738 /* use abstract namespace for socket path */
739 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
740 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
742 udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
743 if (udevd_sock == -1) {
744 err("error getting socket: %s", strerror(errno));
748 /* set receive buffersize */
749 setsockopt(udevd_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
751 /* the bind takes care of ensuring only one copy running */
752 retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
754 err("bind failed: %s", strerror(errno));
758 /* enable receiving of the sender credentials */
759 setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
764 static int init_uevent_netlink_sock(void)
766 struct sockaddr_nl snl;
767 const int buffersize = 16 * 1024 * 1024;
770 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
771 snl.nl_family = AF_NETLINK;
772 snl.nl_pid = getpid();
773 snl.nl_groups = 0xffffffff;
775 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
776 if (uevent_netlink_sock == -1) {
777 err("error getting socket: %s", strerror(errno));
781 /* set receive buffersize */
782 setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
784 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
786 err("bind failed: %s", strerror(errno));
787 close(uevent_netlink_sock);
788 uevent_netlink_sock = -1;
794 int main(int argc, char *argv[], char *envp[])
798 struct sigaction act;
806 /* redirect std fd's, if the kernel forks us, we don't have them at all */
807 fd = open("/dev/null", O_RDWR);
809 if (fd != STDIN_FILENO)
810 dup2(fd, STDIN_FILENO);
811 if (fd != STDOUT_FILENO)
812 dup2(fd, STDOUT_FILENO);
813 if (fd != STDERR_FILENO)
814 dup2(fd, STDERR_FILENO);
815 if (fd > STDERR_FILENO)
819 logging_init("udevd");
821 err("fatal, could not open /dev/null: %s", strerror(errno));
824 dbg("version %s", UDEV_VERSION);
827 err("need to be root, exit");
831 /* parse commandline options */
832 for (i = 1 ; i < argc; i++) {
834 if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
835 info("will daemonize");
838 if (strcmp(arg, "--stop-exec-queue") == 0) {
839 info("will not execute events until START_EXEC_QUEUE is received");
844 /* init sockets to receive events */
845 if (init_udevd_socket() < 0) {
846 if (errno == EADDRINUSE) {
847 err("another udevd running, exit");
850 err("error initializing udevd socket: %s", strerror(errno));
856 if (init_uevent_netlink_sock() < 0) {
857 err("uevent socket not available");
862 /* parse the rules and keep it in memory */
864 udev_rules_init(&rules, 1);
872 dbg("daemonized fork running");
875 err("fork of daemon failed: %s", strerror(errno));
879 dbg("child [%u] running, parent exits", pid);
884 /* set scheduling priority for the daemon */
885 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
890 /* become session leader */
892 dbg("our session is %d", sid);
894 /* OOM_DISABLE == -17 */
895 fd = open("/proc/self/oom_adj", O_RDWR);
897 err("error disabling OOM: %s", strerror(errno));
903 /* setup signal handler pipe */
904 retval = pipe(signal_pipe);
906 err("error getting pipes: %s", strerror(errno));
909 retval = fcntl(signal_pipe[READ_END], F_SETFL, O_NONBLOCK);
911 err("error fcntl on read pipe: %s", strerror(errno));
914 retval = fcntl(signal_pipe[WRITE_END], F_SETFL, O_NONBLOCK);
916 err("error fcntl on write pipe: %s", strerror(errno));
920 /* set signal handlers */
921 memset(&act, 0x00, sizeof(struct sigaction));
922 act.sa_handler = (void (*)(int)) sig_handler;
923 sigemptyset(&act.sa_mask);
924 act.sa_flags = SA_RESTART;
925 sigaction(SIGINT, &act, NULL);
926 sigaction(SIGTERM, &act, NULL);
927 sigaction(SIGCHLD, &act, NULL);
928 sigaction(SIGHUP, &act, NULL);
930 /* watch rules directory */
931 inotify_fd = inotify_init();
933 inotify_add_watch(inotify_fd, udev_rules_filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
935 /* maximum limit of forked childs */
936 value = getenv("UDEVD_MAX_CHILDS");
938 max_childs = strtoul(value, NULL, 10);
940 max_childs = UDEVD_MAX_CHILDS;
941 info("initialize max_childs to %u", max_childs);
943 /* start to throttle forking if maximum number of _running_ childs is reached */
944 value = getenv("UDEVD_MAX_CHILDS_RUNNING");
946 max_childs_running = strtoull(value, NULL, 10);
948 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
949 info("initialize max_childs_running to %u", max_childs_running);
951 /* clear environment for forked event processes */
954 /* export log_priority , as called programs may want to follow that setting */
955 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
959 maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
960 maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
961 maxfd = UDEV_MAX(maxfd, inotify_fd);
964 struct uevent_msg *msg;
968 FD_SET(signal_pipe[READ_END], &readfds);
969 FD_SET(udevd_sock, &readfds);
970 FD_SET(uevent_netlink_sock, &readfds);
972 FD_SET(inotify_fd, &readfds);
974 fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
977 err("error in select: %s", strerror(errno));
981 /* get user socket message */
982 if (FD_ISSET(udevd_sock, &readfds)) {
983 msg = get_udevd_msg();
985 msg_queue_insert(msg);
988 /* get kernel netlink message */
989 if (FD_ISSET(uevent_netlink_sock, &readfds)) {
990 msg = get_netlink_msg();
992 msg_queue_insert(msg);
995 /* received a signal, clear our notification pipe */
996 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
999 read(signal_pipe[READ_END], &buf, sizeof(buf));
1002 /* rules directory inotify watch */
1003 if ((inotify_fd > 0) && FD_ISSET(inotify_fd, &readfds)) {
1006 /* discard all possible events, we can just reload the config */
1007 if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
1011 buf = malloc(nbytes);
1013 err("error getting buffer for inotify, disable watching");
1017 read(inotify_fd, buf, nbytes);
1022 /* rules changed, set by inotify or a signal*/
1023 if (reload_config) {
1025 udev_rules_cleanup(&rules);
1026 udev_rules_init(&rules, 1);
1029 /* forked child has returned */
1030 if (sigchilds_waiting) {
1031 sigchilds_waiting = 0;
1038 msg_queue_manager();
1043 udev_rules_cleanup(&rules);
1046 if (signal_pipe[READ_END] > 0)
1047 close(signal_pipe[READ_END]);
1048 if (signal_pipe[WRITE_END] > 0)
1049 close(signal_pipe[WRITE_END]);
1055 if (uevent_netlink_sock > 0)
1056 close(uevent_netlink_sock);