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_libc_wrapper.h"
48 #include "udev_version.h"
49 #include "udev_rules.h"
50 #include "udev_utils.h"
54 struct udev_rules rules;
55 static int udevd_sock;
56 static int uevent_netlink_sock;
57 static int inotify_fd;
60 static int signal_pipe[2] = {-1, -1};
61 static volatile int sigchilds_waiting;
62 static volatile int udev_exit;
63 static volatile int reload_config;
64 static int run_exec_q;
65 static int stop_exec_q;
66 static int max_childs;
67 static int max_childs_running;
68 static char udev_log[32];
70 static LIST_HEAD(exec_list);
71 static LIST_HEAD(running_list);
75 void log_message(int priority, const char *format, ...)
79 if (priority > udev_log_priority)
82 va_start(args, format);
83 vsyslog(priority, format, args);
88 static void asmlinkage udev_event_sig_handler(int signum)
90 if (signum == SIGALRM)
94 static int udev_event_process(struct uevent_msg *msg)
98 struct name_entry *name_loop;
102 /* set signal handlers */
103 memset(&act, 0x00, sizeof(act));
104 act.sa_handler = (void (*)(int)) udev_event_sig_handler;
105 sigemptyset (&act.sa_mask);
107 sigaction(SIGALRM, &act, NULL);
109 /* trigger timeout to prevent hanging processes */
110 alarm(UDEV_ALARM_TIMEOUT);
112 /* reconstruct env from message */
113 for (i = 0; msg->envp[i]; i++)
114 putenv(msg->envp[i]);
116 udev_init_device(&udev, msg->devpath, msg->subsystem, msg->action);
117 retval = udev_process_event(&rules, &udev);
119 /* run programs collected by RUN-key*/
121 list_for_each_entry(name_loop, &udev.run_list, node) {
122 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
123 pass_env_to_socket(&name_loop->name[strlen("socket:")], msg->devpath, msg->action);
125 if (run_program(name_loop->name, udev.subsystem, NULL, 0, NULL,
126 (udev_log_priority >= LOG_INFO)))
131 udev_cleanup_device(&udev);
142 static void export_event_state(struct uevent_msg *msg, enum event_state state)
144 char filename[PATH_SIZE];
145 char filename_failed[PATH_SIZE];
146 char target[PATH_SIZE];
147 size_t start, end, i;
148 struct uevent_msg *loop_msg;
150 /* add location of queue files */
151 strlcpy(filename, udev_root, sizeof(filename));
152 strlcat(filename, "/", sizeof(filename));
153 start = strlcat(filename, EVENT_QUEUE_DIR, sizeof(filename));
154 end = strlcat(filename, msg->devpath, sizeof(filename));
155 if (end > sizeof(filename))
156 end = sizeof(filename);
158 /* replace '/' to transform path into a filename */
159 for (i = start+1; i < end; i++)
160 if (filename[i] == '/')
161 filename[i] = PATH_TO_NAME_CHAR;
163 /* add location of failed files */
164 strlcpy(filename_failed, udev_root, sizeof(filename_failed));
165 strlcat(filename_failed, "/", sizeof(filename_failed));
166 start = strlcat(filename_failed, EVENT_FAILED_DIR, sizeof(filename_failed));
167 end = strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
168 if (end > sizeof(filename_failed))
169 end = sizeof(filename_failed);
171 /* replace '/' to transform path into a filename */
172 for (i = start+1; i < end; i++)
173 if (filename_failed[i] == '/')
174 filename_failed[i] = PATH_TO_NAME_CHAR;
178 unlink(filename_failed);
179 delete_path(filename_failed);
181 strlcpy(target, sysfs_path, sizeof(target));
182 strlcat(target, msg->devpath, sizeof(target));
183 create_path(filename);
184 symlink(target, filename);
188 unlink(filename_failed);
189 delete_path(filename_failed);
191 /* don't remove, if events for the same path are still pending */
192 list_for_each_entry(loop_msg, &running_list, node)
193 if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
196 /* move failed events to the failed directory */
197 if (state == EVENT_FAILED) {
198 create_path(filename_failed);
199 rename(filename, filename_failed);
204 /* clean up the queue directory */
205 delete_path(filename);
211 static void msg_queue_delete(struct uevent_msg *msg)
213 list_del(&msg->node);
215 /* mark as failed, if add event returns non-zero */
216 if (msg->exitstatus && strcmp(msg->action, "add") == 0)
217 export_event_state(msg, EVENT_FAILED);
219 export_event_state(msg, EVENT_FINISHED);
224 static void udev_event_run(struct uevent_msg *msg)
233 close(uevent_netlink_sock);
237 close(signal_pipe[READ_END]);
238 close(signal_pipe[WRITE_END]);
241 logging_init("udevd-event");
242 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
243 retval = udev_event_process(msg);
244 info("seq %llu finished", msg->seqnum);
251 err("fork of child failed: %s", strerror(errno));
252 msg_queue_delete(msg);
255 /* get SIGCHLD in main loop */
256 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
257 msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time);
262 static void msg_queue_insert(struct uevent_msg *msg)
264 msg->queue_time = time(NULL);
266 export_event_state(msg, EVENT_QUEUED);
268 /* run all events with a timeout set immediately */
269 if (msg->timeout != 0) {
270 list_add_tail(&msg->node, &running_list);
275 list_add_tail(&msg->node, &exec_list);
279 /* runs event and removes event from run queue when finished */
280 static int running_processes(void)
283 static char buf[4096];
288 f = open("/proc/stat", O_RDONLY);
292 len = read(f, buf, sizeof(buf)-1);
300 pos = strstr(buf, "procs_running ");
304 if (sscanf(pos, "procs_running %u", &running) != 1)
310 /* return the number of process es in our session, count only until limit */
311 static int running_processes_in_session(pid_t session, int limit)
317 dir = opendir("/proc");
321 /* read process info from /proc */
322 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
328 pid_t ppid, pgrp, sess;
331 if (!isdigit(dent->d_name[0]))
334 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
335 procdir[sizeof(procdir)-1] = '\0';
337 f = open(procdir, O_RDONLY);
341 len = read(f, line, sizeof(line)-1);
349 /* skip ugly program name */
350 pos = strrchr(line, ')') + 2;
354 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
357 /* count only processes in our session */
361 /* count only running, no sleeping processes */
366 if (limit > 0 && running >= limit)
374 static int compare_devpath(const char *running, const char *waiting)
378 for (i = 0; i < PATH_SIZE; i++) {
379 /* identical device event found */
380 if (running[i] == '\0' && waiting[i] == '\0')
383 /* parent device event found */
384 if (running[i] == '\0' && waiting[i] == '/')
387 /* child device event found */
388 if (running[i] == '/' && waiting[i] == '\0')
391 /* no matching event */
392 if (running[i] != waiting[i])
399 /* returns still running task for the same device, its parent or its physical device */
400 static int running_with_devpath(struct uevent_msg *msg, int limit)
402 struct uevent_msg *loop_msg;
403 int childs_count = 0;
405 list_for_each_entry(loop_msg, &running_list, node) {
406 if (limit && childs_count++ > limit) {
407 dbg("%llu, maximum number (%i) of child reached", msg->seqnum, childs_count);
411 /* return running parent/child device event */
412 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
413 dbg("%llu, child device event still running %llu (%s)",
414 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
418 /* return running physical device event */
419 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
420 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
421 dbg("%llu, physical device event still running %llu (%s)",
422 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
430 /* exec queue management routine executes the events and serializes events in the same sequence */
431 static void msg_queue_manager(void)
433 struct uevent_msg *loop_msg;
434 struct uevent_msg *tmp_msg;
437 if (list_empty(&exec_list))
440 running = running_processes();
441 dbg("%d processes runnning on system", running);
443 running = max_childs_running;
445 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
446 /* check running processes in our session and possibly throttle */
447 if (running >= max_childs_running) {
448 running = running_processes_in_session(sid, max_childs_running+10);
449 dbg("at least %d processes running in session", running);
450 if (running >= max_childs_running) {
451 dbg("delay seq %llu, too many processes already running", loop_msg->seqnum);
456 /* don't run two processes for the same devpath and wait for the parent*/
457 if (running_with_devpath(loop_msg, max_childs)) {
458 dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
462 /* move event to run list */
463 list_move_tail(&loop_msg->node, &running_list);
464 udev_event_run(loop_msg);
466 dbg("moved seq %llu to running list", loop_msg->seqnum);
470 static struct uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
474 struct uevent_msg *msg;
478 msg = malloc(sizeof(struct uevent_msg) + buf_size);
481 memset(msg, 0x00, sizeof(struct uevent_msg) + buf_size);
483 /* copy environment buffer and reconstruct envp */
484 memcpy(msg->envbuf, buf, buf_size);
486 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
490 key = &msg->envbuf[bufpos];
491 keylen = strlen(key);
493 bufpos += keylen + 1;
494 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
496 /* remember some keys for further processing */
497 if (strncmp(key, "ACTION=", 7) == 0)
498 msg->action = &key[7];
499 else if (strncmp(key, "DEVPATH=", 8) == 0)
500 msg->devpath = &key[8];
501 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
502 msg->subsystem = &key[10];
503 else if (strncmp(key, "SEQNUM=", 7) == 0)
504 msg->seqnum = strtoull(&key[7], NULL, 10);
505 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
506 msg->physdevpath = &key[12];
507 else if (strncmp(key, "MAJOR=", 6) == 0)
508 major = strtoull(&key[6], NULL, 10);
509 else if (strncmp(key, "MINOR=", 6) == 0)
510 minor = strtoull(&key[6], NULL, 10);
511 else if (strncmp(key, "TIMEOUT=", 8) == 0)
512 msg->timeout = strtoull(&key[8], NULL, 10);
514 msg->devt = makedev(major, minor);
515 msg->envp[i++] = "UDEVD_EVENT=1";
519 info("DEVPATH missing, ignore message");
527 /* receive the udevd message from userspace */
528 static struct uevent_msg *get_udevd_msg(void)
530 static struct udevd_msg usend_msg;
531 struct uevent_msg *msg;
534 struct cmsghdr *cmsg;
537 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
541 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
542 iov.iov_base = &usend_msg;
543 iov.iov_len = sizeof(struct udevd_msg);
545 memset(&smsg, 0x00, sizeof(struct msghdr));
548 smsg.msg_control = cred_msg;
549 smsg.msg_controllen = sizeof(cred_msg);
551 size = recvmsg(udevd_sock, &smsg, 0);
554 err("unable to receive user udevd message: %s", strerror(errno));
557 cmsg = CMSG_FIRSTHDR(&smsg);
558 cred = (struct ucred *) CMSG_DATA(cmsg);
560 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
561 err("no sender credentials received, message ignored");
565 if (cred->uid != 0) {
566 err("sender uid=%i, message ignored", cred->uid);
570 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
571 err("message magic '%s' doesn't match, ignore it", usend_msg.magic);
575 switch (usend_msg.type) {
576 case UDEVD_UEVENT_UDEVSEND:
577 case UDEVD_UEVENT_INITSEND:
578 info("udevd event message received");
579 envbuf_size = size - offsetof(struct udevd_msg, envbuf);
580 dbg("envbuf_size=%i", envbuf_size);
581 msg = get_msg_from_envbuf(usend_msg.envbuf, envbuf_size);
584 msg->type = usend_msg.type;
586 case UDEVD_STOP_EXEC_QUEUE:
587 info("udevd message (STOP_EXEC_QUEUE) received");
590 case UDEVD_START_EXEC_QUEUE:
591 info("udevd message (START_EXEC_QUEUE) received");
595 case UDEVD_SET_LOG_LEVEL:
596 intval = (int *) usend_msg.envbuf;
597 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
598 udev_log_priority = *intval;
599 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
602 case UDEVD_SET_MAX_CHILDS:
603 intval = (int *) usend_msg.envbuf;
604 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
605 max_childs = *intval;
607 case UDEVD_RELOAD_RULES:
608 info("udevd message (RELOAD_RULES) received");
612 dbg("unknown message type");
617 /* receive the kernel user event message and do some sanity checks */
618 static struct uevent_msg *get_netlink_msg(void)
620 struct uevent_msg *msg;
623 static char buffer[UEVENT_BUFFER_SIZE + 512];
626 size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
629 err("unable to receive kernel netlink message: %s", strerror(errno));
633 if ((size_t)size > sizeof(buffer)-1)
634 size = sizeof(buffer)-1;
636 dbg("uevent_size=%zi", size);
638 /* start of event payload */
639 bufpos = strlen(buffer)+1;
640 msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
643 msg->type = UDEVD_UEVENT_NETLINK;
645 /* validate message */
646 pos = strchr(buffer, '@');
648 err("invalid uevent '%s'", buffer);
654 if (msg->action == NULL) {
655 info("no ACTION in payload found, skip event '%s'", buffer);
660 if (strcmp(msg->action, buffer) != 0) {
661 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
669 static void asmlinkage sig_handler(int signum)
677 /* set flag, then write to pipe if needed */
678 sigchilds_waiting = 1;
685 /* write to pipe, which will wakeup select() in our mainloop */
686 write(signal_pipe[WRITE_END], "", 1);
689 static void udev_done(int pid, int exitstatus)
691 /* find msg associated with pid and delete it */
692 struct uevent_msg *msg;
694 list_for_each_entry(msg, &running_list, node) {
695 if (msg->pid == pid) {
696 info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg->seqnum, msg->pid,
697 exitstatus, time(NULL) - msg->queue_time);
698 msg->exitstatus = exitstatus;
699 msg_queue_delete(msg);
701 /* there may be events waiting with the same devpath */
708 static void reap_sigchilds(void)
714 pid = waitpid(-1, &status, WNOHANG);
717 if (WIFEXITED(status))
718 status = WEXITSTATUS(status);
719 else if (WIFSIGNALED(status))
720 status = WTERMSIG(status) + 128;
723 udev_done(pid, status);
727 static int init_udevd_socket(void)
729 struct sockaddr_un saddr;
730 const int buffersize = 16 * 1024 * 1024;
732 const int feature_on = 1;
735 memset(&saddr, 0x00, sizeof(saddr));
736 saddr.sun_family = AF_LOCAL;
737 /* use abstract namespace for socket path */
738 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
739 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
741 udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
742 if (udevd_sock == -1) {
743 err("error getting socket: %s", strerror(errno));
747 /* set receive buffersize */
748 setsockopt(udevd_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
750 /* the bind takes care of ensuring only one copy running */
751 retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
753 err("bind failed: %s", strerror(errno));
757 /* enable receiving of the sender credentials */
758 setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
763 static int init_uevent_netlink_sock(void)
765 struct sockaddr_nl snl;
766 const int buffersize = 16 * 1024 * 1024;
769 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
770 snl.nl_family = AF_NETLINK;
771 snl.nl_pid = getpid();
772 snl.nl_groups = 0xffffffff;
774 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
775 if (uevent_netlink_sock == -1) {
776 err("error getting socket: %s", strerror(errno));
780 /* set receive buffersize */
781 setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
783 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
785 err("bind failed: %s", strerror(errno));
786 close(uevent_netlink_sock);
787 uevent_netlink_sock = -1;
793 int main(int argc, char *argv[], char *envp[])
797 struct sigaction act;
805 /* redirect std fd's, if the kernel forks us, we don't have them at all */
806 fd = open("/dev/null", O_RDWR);
808 if (fd != STDIN_FILENO)
809 dup2(fd, STDIN_FILENO);
810 if (fd != STDOUT_FILENO)
811 dup2(fd, STDOUT_FILENO);
812 if (fd != STDERR_FILENO)
813 dup2(fd, STDERR_FILENO);
814 if (fd > STDERR_FILENO)
818 logging_init("udevd");
820 err("fatal, could not open /dev/null: %s", strerror(errno));
823 dbg("version %s", UDEV_VERSION);
826 err("need to be root, exit");
830 /* parse commandline options */
831 for (i = 1 ; i < argc; i++) {
833 if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
834 info("will daemonize");
837 if (strcmp(arg, "--stop-exec-queue") == 0) {
838 info("will not execute events until START_EXEC_QUEUE is received");
843 /* init sockets to receive events */
844 if (init_udevd_socket() < 0) {
845 if (errno == EADDRINUSE) {
846 err("another udevd running, exit");
849 err("error initializing udevd socket: %s", strerror(errno));
855 if (init_uevent_netlink_sock() < 0) {
856 err("uevent socket not available");
861 /* parse the rules and keep it in memory */
862 udev_rules_init(&rules, 1);
870 dbg("daemonized fork running");
873 err("fork of daemon failed: %s", strerror(errno));
877 dbg("child [%u] running, parent exits", pid);
882 /* set scheduling priority for the daemon */
883 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
888 /* become session leader */
890 dbg("our session is %d", sid);
892 /* OOM_DISABLE == -17 */
893 fd = open("/proc/self/oom_adj", O_RDWR);
895 err("error disabling OOM: %s", strerror(errno));
901 /* setup signal handler pipe */
902 retval = pipe(signal_pipe);
904 err("error getting pipes: %s", strerror(errno));
907 retval = fcntl(signal_pipe[READ_END], F_SETFL, O_NONBLOCK);
909 err("error fcntl on read pipe: %s", strerror(errno));
912 retval = fcntl(signal_pipe[WRITE_END], F_SETFL, O_NONBLOCK);
914 err("error fcntl on write pipe: %s", strerror(errno));
918 /* set signal handlers */
919 memset(&act, 0x00, sizeof(struct sigaction));
920 act.sa_handler = (void (*)(int)) sig_handler;
921 sigemptyset(&act.sa_mask);
922 act.sa_flags = SA_RESTART;
923 sigaction(SIGINT, &act, NULL);
924 sigaction(SIGTERM, &act, NULL);
925 sigaction(SIGCHLD, &act, NULL);
926 sigaction(SIGHUP, &act, NULL);
928 /* watch rules directory */
929 inotify_fd = inotify_init();
931 inotify_add_watch(inotify_fd, udev_rules_filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
933 /* maximum limit of forked childs */
934 value = getenv("UDEVD_MAX_CHILDS");
936 max_childs = strtoul(value, NULL, 10);
938 max_childs = UDEVD_MAX_CHILDS;
939 info("initialize max_childs to %u", max_childs);
941 /* start to throttle forking if maximum number of _running_ childs is reached */
942 value = getenv("UDEVD_MAX_CHILDS_RUNNING");
944 max_childs_running = strtoull(value, NULL, 10);
946 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
947 info("initialize max_childs_running to %u", max_childs_running);
949 /* clear environment for forked event processes */
952 /* export log_priority , as called programs may want to follow that setting */
953 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
957 maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
958 maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
959 maxfd = UDEV_MAX(maxfd, inotify_fd);
962 struct uevent_msg *msg;
966 FD_SET(signal_pipe[READ_END], &readfds);
967 FD_SET(udevd_sock, &readfds);
968 FD_SET(uevent_netlink_sock, &readfds);
970 FD_SET(inotify_fd, &readfds);
972 fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
975 err("error in select: %s", strerror(errno));
979 /* get user socket message */
980 if (FD_ISSET(udevd_sock, &readfds)) {
981 msg = get_udevd_msg();
983 msg_queue_insert(msg);
986 /* get kernel netlink message */
987 if (FD_ISSET(uevent_netlink_sock, &readfds)) {
988 msg = get_netlink_msg();
990 msg_queue_insert(msg);
993 /* received a signal, clear our notification pipe */
994 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
997 read(signal_pipe[READ_END], &buf, sizeof(buf));
1000 /* rules directory inotify watch */
1001 if ((inotify_fd > 0) && FD_ISSET(inotify_fd, &readfds)) {
1004 /* discard all possible events, we can just reload the config */
1005 if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
1009 buf = malloc(nbytes);
1011 err("error getting buffer for inotify, disable watching");
1015 read(inotify_fd, buf, nbytes);
1020 /* rules changed, set by inotify or a signal*/
1021 if (reload_config) {
1023 udev_rules_close(&rules);
1024 udev_rules_init(&rules, 1);
1027 /* forked child has returned */
1028 if (sigchilds_waiting) {
1029 sigchilds_waiting = 0;
1036 msg_queue_manager();
1041 udev_rules_close(&rules);
1043 if (signal_pipe[READ_END] > 0)
1044 close(signal_pipe[READ_END]);
1045 if (signal_pipe[WRITE_END] > 0)
1046 close(signal_pipe[WRITE_END]);
1052 if (uevent_netlink_sock > 0)
1053 close(uevent_netlink_sock);