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 /* reset to default */
104 act.sa_handler = SIG_DFL;
105 sigaction(SIGINT, &act, NULL);
106 sigaction(SIGTERM, &act, NULL);
107 sigaction(SIGCHLD, &act, NULL);
108 sigaction(SIGHUP, &act, NULL);
110 /* trigger timeout to prevent hanging processes */
111 alarm(UDEV_ALARM_TIMEOUT);
113 /* reconstruct event environment from message */
114 for (i = 0; msg->envp[i]; i++)
115 putenv(msg->envp[i]);
117 udev = udev_device_init();
120 strlcpy(udev->action, msg->action, sizeof(udev->action));
121 sysfs_device_set_values(udev->dev, msg->devpath, msg->subsystem);
122 udev->devt = msg->devt;
124 retval = udev_device_event(&rules, udev);
126 /* run programs collected by RUN-key*/
127 if (retval == 0 && !udev->ignore_device && udev_run) {
128 struct name_entry *name_loop;
130 dbg("executing run list");
131 list_for_each_entry(name_loop, &udev->run_list, node) {
132 if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
133 pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, udev->action);
135 char program[PATH_SIZE];
137 strlcpy(program, name_loop->name, sizeof(program));
138 udev_rules_apply_format(udev, program, sizeof(program));
139 if (run_program(program, udev->dev->subsystem, NULL, 0, NULL,
140 (udev_log_priority >= LOG_INFO)))
146 udev_device_cleanup(udev);
156 static void export_event_state(struct uevent_msg *msg, enum event_state state)
158 char filename[PATH_SIZE];
159 char filename_failed[PATH_SIZE];
160 char target[PATH_SIZE];
161 size_t start, end, i;
162 struct uevent_msg *loop_msg;
164 /* add location of queue files */
165 strlcpy(filename, udev_root, sizeof(filename));
166 strlcat(filename, "/", sizeof(filename));
167 start = strlcat(filename, EVENT_QUEUE_DIR, sizeof(filename));
168 end = strlcat(filename, msg->devpath, sizeof(filename));
169 if (end > sizeof(filename))
170 end = sizeof(filename);
172 /* replace '/' to transform path into a filename */
173 for (i = start+1; i < end; i++)
174 if (filename[i] == '/')
175 filename[i] = PATH_TO_NAME_CHAR;
177 /* add location of failed files */
178 strlcpy(filename_failed, udev_root, sizeof(filename_failed));
179 strlcat(filename_failed, "/", sizeof(filename_failed));
180 start = strlcat(filename_failed, EVENT_FAILED_DIR, sizeof(filename_failed));
181 end = strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
182 if (end > sizeof(filename_failed))
183 end = sizeof(filename_failed);
185 /* replace '/' to transform path into a filename */
186 for (i = start+1; i < end; i++)
187 if (filename_failed[i] == '/')
188 filename_failed[i] = PATH_TO_NAME_CHAR;
192 unlink(filename_failed);
193 delete_path(filename_failed);
195 strlcpy(target, sysfs_path, sizeof(target));
196 strlcat(target, msg->devpath, sizeof(target));
197 create_path(filename);
198 symlink(target, filename);
202 unlink(filename_failed);
203 delete_path(filename_failed);
205 /* don't remove, if events for the same path are still pending */
206 list_for_each_entry(loop_msg, &running_list, node)
207 if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
210 list_for_each_entry(loop_msg, &exec_list, node)
211 if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
214 /* move failed events to the failed directory */
215 if (state == EVENT_FAILED) {
216 create_path(filename_failed);
217 rename(filename, filename_failed);
222 /* clean up the queue directory */
223 delete_path(filename);
229 static void msg_queue_delete(struct uevent_msg *msg)
231 list_del(&msg->node);
233 /* mark as failed, if add event returns non-zero */
234 if (msg->exitstatus && strcmp(msg->action, "add") == 0)
235 export_event_state(msg, EVENT_FAILED);
237 export_event_state(msg, EVENT_FINISHED);
242 static void udev_event_run(struct uevent_msg *msg)
251 close(uevent_netlink_sock);
255 close(signal_pipe[READ_END]);
256 close(signal_pipe[WRITE_END]);
259 logging_init("udevd-event");
260 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
262 retval = udev_event_process(msg);
263 info("seq %llu finished", msg->seqnum);
270 err("fork of child failed: %s", strerror(errno));
271 msg_queue_delete(msg);
274 /* get SIGCHLD in main loop */
275 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
276 msg->seqnum, pid, msg->action, msg->subsystem, time(NULL) - msg->queue_time);
281 static void msg_queue_insert(struct uevent_msg *msg)
283 char filename[PATH_SIZE];
286 msg->queue_time = time(NULL);
288 strlcpy(filename, udev_root, sizeof(filename));
289 strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
290 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
295 len = sprintf(str, "%llu\n", msg->seqnum);
300 export_event_state(msg, EVENT_QUEUED);
302 /* run all events with a timeout set immediately */
303 if (msg->timeout != 0) {
304 list_add_tail(&msg->node, &running_list);
309 list_add_tail(&msg->node, &exec_list);
313 /* runs event and removes event from run queue when finished */
314 static int running_processes(void)
322 f = open("/proc/stat", O_RDONLY);
326 len = read(f, buf, sizeof(buf)-1);
334 pos = strstr(buf, "procs_running ");
338 if (sscanf(pos, "procs_running %u", &running) != 1)
344 /* return the number of process es in our session, count only until limit */
345 static int running_processes_in_session(pid_t session, int limit)
351 dir = opendir("/proc");
355 /* read process info from /proc */
356 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
362 pid_t ppid, pgrp, sess;
365 if (!isdigit(dent->d_name[0]))
368 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
369 procdir[sizeof(procdir)-1] = '\0';
371 f = open(procdir, O_RDONLY);
375 len = read(f, line, sizeof(line)-1);
383 /* skip ugly program name */
384 pos = strrchr(line, ')') + 2;
388 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
391 /* count only processes in our session */
395 /* count only running, no sleeping processes */
400 if (limit > 0 && running >= limit)
408 static int compare_devpath(const char *running, const char *waiting)
412 for (i = 0; i < PATH_SIZE; i++) {
413 /* identical device event found */
414 if (running[i] == '\0' && waiting[i] == '\0')
417 /* parent device event found */
418 if (running[i] == '\0' && waiting[i] == '/')
421 /* child device event found */
422 if (running[i] == '/' && waiting[i] == '\0')
425 /* no matching event */
426 if (running[i] != waiting[i])
433 /* returns still running task for the same device, its parent or its physical device */
434 static int running_with_devpath(struct uevent_msg *msg, int limit)
436 struct uevent_msg *loop_msg;
437 int childs_count = 0;
439 list_for_each_entry(loop_msg, &running_list, node) {
440 if (limit && childs_count++ > limit) {
441 dbg("%llu, maximum number (%i) of child reached", msg->seqnum, childs_count);
445 /* return running parent/child device event */
446 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
447 dbg("%llu, child device event still running %llu (%s)",
448 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
452 /* return running physical device event */
453 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
454 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
455 dbg("%llu, physical device event still running %llu (%s)",
456 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
464 /* exec queue management routine executes the events and serializes events in the same sequence */
465 static void msg_queue_manager(void)
467 struct uevent_msg *loop_msg;
468 struct uevent_msg *tmp_msg;
471 if (list_empty(&exec_list))
474 running = running_processes();
475 dbg("%d processes runnning on system", running);
477 running = max_childs_running;
479 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
480 /* check running processes in our session and possibly throttle */
481 if (running >= max_childs_running) {
482 running = running_processes_in_session(sid, max_childs_running+10);
483 dbg("at least %d processes running in session", running);
484 if (running >= max_childs_running) {
485 dbg("delay seq %llu, too many processes already running", loop_msg->seqnum);
490 /* don't run two processes for the same devpath and wait for the parent*/
491 if (running_with_devpath(loop_msg, max_childs)) {
492 dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
496 /* move event to run list */
497 list_move_tail(&loop_msg->node, &running_list);
498 udev_event_run(loop_msg);
500 dbg("moved seq %llu to running list", loop_msg->seqnum);
504 static struct uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
508 struct uevent_msg *msg;
512 msg = malloc(sizeof(struct uevent_msg) + buf_size);
515 memset(msg, 0x00, sizeof(struct uevent_msg) + buf_size);
517 /* copy environment buffer and reconstruct envp */
518 memcpy(msg->envbuf, buf, buf_size);
520 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
524 key = &msg->envbuf[bufpos];
525 keylen = strlen(key);
527 bufpos += keylen + 1;
528 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
530 /* remember some keys for further processing */
531 if (strncmp(key, "ACTION=", 7) == 0)
532 msg->action = &key[7];
533 else if (strncmp(key, "DEVPATH=", 8) == 0)
534 msg->devpath = &key[8];
535 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
536 msg->subsystem = &key[10];
537 else if (strncmp(key, "SEQNUM=", 7) == 0)
538 msg->seqnum = strtoull(&key[7], NULL, 10);
539 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
540 msg->physdevpath = &key[12];
541 else if (strncmp(key, "MAJOR=", 6) == 0)
542 major = strtoull(&key[6], NULL, 10);
543 else if (strncmp(key, "MINOR=", 6) == 0)
544 minor = strtoull(&key[6], NULL, 10);
545 else if (strncmp(key, "TIMEOUT=", 8) == 0)
546 msg->timeout = strtoull(&key[8], NULL, 10);
548 msg->devt = makedev(major, minor);
549 msg->envp[i++] = "UDEVD_EVENT=1";
552 if (msg->devpath == NULL || msg->action == NULL) {
553 info("DEVPATH or ACTION missing, ignore message");
560 /* receive the udevd message from userspace */
561 static void get_udevd_msg(void)
563 struct udevd_msg ctrl_msg;
566 struct cmsghdr *cmsg;
569 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
572 memset(&ctrl_msg, 0x00, sizeof(struct udevd_msg));
573 iov.iov_base = &ctrl_msg;
574 iov.iov_len = sizeof(struct udevd_msg);
576 memset(&smsg, 0x00, sizeof(struct msghdr));
579 smsg.msg_control = cred_msg;
580 smsg.msg_controllen = sizeof(cred_msg);
582 size = recvmsg(udevd_sock, &smsg, 0);
585 err("unable to receive user udevd message: %s", strerror(errno));
588 cmsg = CMSG_FIRSTHDR(&smsg);
589 cred = (struct ucred *) CMSG_DATA(cmsg);
591 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
592 err("no sender credentials received, message ignored");
596 if (cred->uid != 0) {
597 err("sender uid=%i, message ignored", cred->uid);
601 if (strncmp(ctrl_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
602 err("message magic '%s' doesn't match, ignore it", ctrl_msg.magic);
606 switch (ctrl_msg.type) {
607 case UDEVD_STOP_EXEC_QUEUE:
608 info("udevd message (STOP_EXEC_QUEUE) received");
611 case UDEVD_START_EXEC_QUEUE:
612 info("udevd message (START_EXEC_QUEUE) received");
616 case UDEVD_SET_LOG_LEVEL:
617 intval = (int *) ctrl_msg.envbuf;
618 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
619 udev_log_priority = *intval;
620 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
623 case UDEVD_SET_MAX_CHILDS:
624 intval = (int *) ctrl_msg.envbuf;
625 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
626 max_childs = *intval;
628 case UDEVD_RELOAD_RULES:
629 info("udevd message (RELOAD_RULES) received");
633 dbg("unknown message type");
637 /* receive the kernel user event message and do some sanity checks */
638 static struct uevent_msg *get_netlink_msg(void)
640 struct uevent_msg *msg;
643 static char buffer[UEVENT_BUFFER_SIZE+512];
646 size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
649 err("unable to receive kernel netlink message: %s", strerror(errno));
653 if ((size_t)size > sizeof(buffer)-1)
654 size = sizeof(buffer)-1;
656 dbg("uevent_size=%zi", size);
658 /* start of event payload */
659 bufpos = strlen(buffer)+1;
660 msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
663 msg->type = UDEVD_UEVENT_NETLINK;
665 /* validate message */
666 pos = strchr(buffer, '@');
668 err("invalid uevent '%s'", buffer);
674 if (msg->action == NULL) {
675 info("no ACTION in payload found, skip event '%s'", buffer);
680 if (strcmp(msg->action, buffer) != 0) {
681 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
689 static void asmlinkage sig_handler(int signum)
697 /* set flag, then write to pipe if needed */
698 sigchilds_waiting = 1;
705 /* write to pipe, which will wakeup select() in our mainloop */
706 write(signal_pipe[WRITE_END], "", 1);
709 static void udev_done(int pid, int exitstatus)
711 /* find msg associated with pid and delete it */
712 struct uevent_msg *msg;
714 list_for_each_entry(msg, &running_list, node) {
715 if (msg->pid == pid) {
716 info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg->seqnum, msg->pid,
717 exitstatus, time(NULL) - msg->queue_time);
718 msg->exitstatus = exitstatus;
719 msg_queue_delete(msg);
721 /* there may be events waiting with the same devpath */
728 static void reap_sigchilds(void)
734 pid = waitpid(-1, &status, WNOHANG);
737 if (WIFEXITED(status))
738 status = WEXITSTATUS(status);
739 else if (WIFSIGNALED(status))
740 status = WTERMSIG(status) + 128;
743 udev_done(pid, status);
747 static int init_udevd_socket(void)
749 struct sockaddr_un saddr;
750 const int buffersize = 16 * 1024 * 1024;
752 const int feature_on = 1;
755 memset(&saddr, 0x00, sizeof(saddr));
756 saddr.sun_family = AF_LOCAL;
757 /* use abstract namespace for socket path */
758 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
759 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
761 udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
762 if (udevd_sock == -1) {
763 err("error getting socket: %s", strerror(errno));
767 /* set receive buffersize */
768 setsockopt(udevd_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
770 /* the bind takes care of ensuring only one copy running */
771 retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
773 err("bind failed: %s", strerror(errno));
777 /* enable receiving of the sender credentials */
778 setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
783 static int init_uevent_netlink_sock(void)
785 struct sockaddr_nl snl;
786 const int buffersize = 16 * 1024 * 1024;
789 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
790 snl.nl_family = AF_NETLINK;
791 snl.nl_pid = getpid();
794 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
795 if (uevent_netlink_sock == -1) {
796 err("error getting socket: %s", strerror(errno));
800 /* set receive buffersize */
801 setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
803 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
805 err("bind failed: %s", strerror(errno));
806 close(uevent_netlink_sock);
807 uevent_netlink_sock = -1;
813 static void export_initial_seqnum(void)
815 char filename[PATH_SIZE];
820 strlcpy(filename, sysfs_path, sizeof(filename));
821 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
822 fd = open(filename, O_RDONLY);
824 len = read(fd, seqnum, sizeof(seqnum)-1);
828 strcpy(seqnum, "0\n");
831 strlcpy(filename, udev_root, sizeof(filename));
832 strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
833 create_path(filename);
834 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
836 write(fd, seqnum, len);
841 int main(int argc, char *argv[], char *envp[])
845 struct sigaction act;
853 /* redirect std fd's, if the kernel forks us, we don't have them at all */
854 fd = open("/dev/null", O_RDWR);
856 if (fd != STDIN_FILENO)
857 dup2(fd, STDIN_FILENO);
858 if (fd != STDOUT_FILENO)
859 dup2(fd, STDOUT_FILENO);
860 if (fd != STDERR_FILENO)
861 dup2(fd, STDERR_FILENO);
862 if (fd > STDERR_FILENO)
866 logging_init("udevd");
868 err("fatal, could not open /dev/null: %s", strerror(errno));
871 dbg("version %s", UDEV_VERSION);
874 err("need to be root, exit");
878 /* parse commandline options */
879 for (i = 1 ; i < argc; i++) {
881 if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
882 info("will daemonize");
885 if (strcmp(arg, "--stop-exec-queue") == 0) {
886 info("will not execute events until START_EXEC_QUEUE is received");
891 /* init sockets to receive events */
892 if (init_udevd_socket() < 0) {
893 if (errno == EADDRINUSE) {
894 err("another udevd running, exit");
897 err("error initializing udevd socket: %s", strerror(errno));
903 if (init_uevent_netlink_sock() < 0) {
904 err("uevent socket not available");
909 /* parse the rules and keep it in memory */
911 udev_rules_init(&rules, 1);
913 export_initial_seqnum();
921 dbg("daemonized fork running");
924 err("fork of daemon failed: %s", strerror(errno));
928 dbg("child [%u] running, parent exits", pid);
933 /* set scheduling priority for the daemon */
934 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
939 /* become session leader */
941 dbg("our session is %d", sid);
943 /* OOM_DISABLE == -17 */
944 fd = open("/proc/self/oom_adj", O_RDWR);
946 err("error disabling OOM: %s", strerror(errno));
952 /* setup signal handler pipe */
953 retval = pipe(signal_pipe);
955 err("error getting pipes: %s", strerror(errno));
959 retval = fcntl(signal_pipe[READ_END], F_GETFL, 0);
961 err("error fcntl on read pipe: %s", strerror(errno));
964 retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK);
966 err("error fcntl on read pipe: %s", strerror(errno));
970 retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0);
972 err("error fcntl on write pipe: %s", strerror(errno));
975 retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK);
977 err("error fcntl on write pipe: %s", strerror(errno));
981 /* set signal handlers */
982 memset(&act, 0x00, sizeof(struct sigaction));
983 act.sa_handler = (void (*)(int)) sig_handler;
984 sigemptyset(&act.sa_mask);
985 act.sa_flags = SA_RESTART;
986 sigaction(SIGINT, &act, NULL);
987 sigaction(SIGTERM, &act, NULL);
988 sigaction(SIGCHLD, &act, NULL);
989 sigaction(SIGHUP, &act, NULL);
991 /* watch rules directory */
992 inotify_fd = inotify_init();
994 inotify_add_watch(inotify_fd, udev_rules_filename, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
995 else if (errno == ENOSYS)
996 err("the kernel does not support inotify, udevd can't monitor configuration file changes");
998 err("inotify_init failed: %s", strerror(errno));
1000 /* maximum limit of forked childs */
1001 value = getenv("UDEVD_MAX_CHILDS");
1003 max_childs = strtoul(value, NULL, 10);
1005 max_childs = UDEVD_MAX_CHILDS;
1006 info("initialize max_childs to %u", max_childs);
1008 /* start to throttle forking if maximum number of _running_ childs is reached */
1009 value = getenv("UDEVD_MAX_CHILDS_RUNNING");
1011 max_childs_running = strtoull(value, NULL, 10);
1013 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
1014 info("initialize max_childs_running to %u", max_childs_running);
1016 /* clear environment for forked event processes */
1019 /* export log_priority , as called programs may want to follow that setting */
1020 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
1024 maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
1025 maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
1026 maxfd = UDEV_MAX(maxfd, inotify_fd);
1028 while (!udev_exit) {
1029 struct uevent_msg *msg;
1033 FD_SET(signal_pipe[READ_END], &readfds);
1034 FD_SET(udevd_sock, &readfds);
1035 FD_SET(uevent_netlink_sock, &readfds);
1036 if (inotify_fd >= 0)
1037 FD_SET(inotify_fd, &readfds);
1039 fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
1042 err("error in select: %s", strerror(errno));
1046 /* get user socket message */
1047 if (FD_ISSET(udevd_sock, &readfds))
1050 /* get kernel netlink message */
1051 if (FD_ISSET(uevent_netlink_sock, &readfds)) {
1052 msg = get_netlink_msg();
1054 msg_queue_insert(msg);
1057 /* received a signal, clear our notification pipe */
1058 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
1061 read(signal_pipe[READ_END], &buf, sizeof(buf));
1064 /* rules directory inotify watch */
1065 if ((inotify_fd >= 0) && FD_ISSET(inotify_fd, &readfds)) {
1068 /* discard all possible events, we can just reload the config */
1069 if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
1073 buf = malloc(nbytes);
1075 err("error getting buffer for inotify, disable watching");
1079 read(inotify_fd, buf, nbytes);
1084 /* rules changed, set by inotify or a signal*/
1085 if (reload_config) {
1087 udev_rules_cleanup(&rules);
1088 udev_rules_init(&rules, 1);
1091 /* forked child has returned */
1092 if (sigchilds_waiting) {
1093 sigchilds_waiting = 0;
1100 msg_queue_manager();
1105 udev_rules_cleanup(&rules);
1108 if (signal_pipe[READ_END] >= 0)
1109 close(signal_pipe[READ_END]);
1110 if (signal_pipe[WRITE_END] >= 0)
1111 close(signal_pipe[WRITE_END]);
1113 if (udevd_sock >= 0)
1115 if (inotify_fd >= 0)
1117 if (uevent_netlink_sock >= 0)
1118 close(uevent_netlink_sock);