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.
34 #include <sys/select.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
40 #include <sys/sysinfo.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_utils.h"
54 static int udevd_sock;
55 static int uevent_netlink_sock;
58 static int signal_pipe[2] = {-1, -1};
59 static volatile int sigchilds_waiting;
60 static volatile int run_msg_q;
61 static volatile int udev_exit;
62 static int init_phase = 1;
63 static int run_exec_q;
64 static int stop_exec_q;
65 static char *udev_bin;
66 static int event_timeout;
67 static int max_childs;
68 static int max_childs_running;
69 static unsigned long long expected_seqnum;
72 static LIST_HEAD(msg_list);
73 static LIST_HEAD(exec_list);
74 static LIST_HEAD(running_list);
78 void log_message (int priority, const char *format, ...)
82 if (priority > udev_log_priority)
85 va_start(args, format);
86 vsyslog(priority, format, args);
91 static void msg_dump_queue(void)
94 struct uevent_msg *msg;
96 list_for_each_entry(msg, &msg_list, node)
97 dbg("sequence %llu in queue", msg->seqnum);
101 static void msg_queue_delete(struct uevent_msg *msg)
103 list_del(&msg->node);
107 /* orders the message in the queue by sequence number */
108 static void msg_queue_insert(struct uevent_msg *msg)
110 struct uevent_msg *loop_msg;
113 if (msg->seqnum == 0) {
114 dbg("no SEQNUM, move straight to the exec queue");
115 list_add_tail(&msg->node, &exec_list);
120 /* store timestamp of queuing */
122 msg->queue_time = info.uptime;
124 /* with the first event we provide a phase of shorter timeout */
126 static long init_time;
129 init_time = info.uptime;
130 if (info.uptime - init_time >= UDEVD_INIT_TIME)
134 /* sort message by sequence number into list */
135 list_for_each_entry_reverse(loop_msg, &msg_list, node) {
136 if (loop_msg->seqnum < msg->seqnum)
139 if (loop_msg->seqnum == msg->seqnum) {
140 dbg("ignoring duplicate message seq %llu", msg->seqnum);
145 list_add(&msg->node, &loop_msg->node);
146 info("seq %llu queued, '%s' '%s'", msg->seqnum, msg->action, msg->devpath);
148 /* run msg queue manager */
154 /* forks event and removes event from run queue when finished */
155 static void udev_event_run(struct uevent_msg *msg)
157 char *const argv[] = { "udev", msg->subsystem, NULL };
165 if (uevent_netlink_sock != -1)
166 close(uevent_netlink_sock);
169 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
170 execve(udev_bin, argv, msg->envp);
171 err("exec of child failed");
174 err("fork of child failed");
175 msg_queue_delete(msg);
178 /* get SIGCHLD in main loop */
180 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
181 msg->seqnum, pid, msg->action, msg->subsystem, info.uptime - msg->queue_time);
186 static int running_processes(void)
189 static char buf[4096];
194 f = open("/proc/stat", O_RDONLY);
198 len = read(f, buf, sizeof(buf)-1);
206 pos = strstr(buf, "procs_running ");
210 if (sscanf(pos, "procs_running %u", &running) != 1)
216 /* return the number of process es in our session, count only until limit */
217 static int running_processes_in_session(pid_t session, int limit)
223 dir = opendir("/proc");
227 /* read process info from /proc */
228 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
234 pid_t ppid, pgrp, sess;
237 if (!isdigit(dent->d_name[0]))
240 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
241 procdir[sizeof(procdir)-1] = '\0';
243 f = open(procdir, O_RDONLY);
247 len = read(f, line, sizeof(line)-1);
255 /* skip ugly program name */
256 pos = strrchr(line, ')') + 2;
260 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
263 /* count only processes in our session */
267 /* count only running, no sleeping processes */
272 if (limit > 0 && running >= limit)
280 static int compare_devpath(const char *running, const char *waiting)
284 for (i = 0; i < PATH_SIZE; i++) {
285 /* identical device event found */
286 if (running[i] == '\0' && waiting[i] == '\0')
289 /* parent device event found */
290 if (running[i] == '\0' && waiting[i] == '/')
293 /* child device event found */
294 if (running[i] == '/' && waiting[i] == '\0')
297 /* no matching event */
298 if (running[i] != waiting[i])
305 /* returns still running task for the same device, its parent or its physical device */
306 static int running_with_devpath(struct uevent_msg *msg, int limit)
308 struct uevent_msg *loop_msg;
309 int childs_count = 0;
311 if (msg->devpath == NULL)
314 /* skip any events with a timeout set */
315 if (msg->timeout != 0)
318 list_for_each_entry(loop_msg, &running_list, node) {
319 if (limit && childs_count++ > limit) {
320 dbg("%llu, maximum number (%i) of child reached", msg->seqnum, childs_count);
323 if (loop_msg->devpath == NULL)
326 /* return running parent/child device event */
327 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
328 dbg("%llu, child device event still running %llu (%s)",
329 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
333 /* return running physical device event */
334 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
335 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
336 dbg("%llu, physical device event still running %llu (%s)",
337 msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
345 /* exec queue management routine executes the events and serializes events in the same sequence */
346 static void exec_queue_manager(void)
348 struct uevent_msg *loop_msg;
349 struct uevent_msg *tmp_msg;
352 if (list_empty(&exec_list))
355 running = running_processes();
356 dbg("%d processes runnning on system", running);
358 running = max_childs_running;
360 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
361 /* check running processes in our session and possibly throttle */
362 if (running >= max_childs_running) {
363 running = running_processes_in_session(sid, max_childs_running+10);
364 dbg("at least %d processes running in session", running);
365 if (running >= max_childs_running) {
366 dbg("delay seq %llu, cause too many processes already running",
372 if (running_with_devpath(loop_msg, max_childs) == 0) {
373 /* move event to run list */
374 list_move_tail(&loop_msg->node, &running_list);
375 udev_event_run(loop_msg);
377 dbg("moved seq %llu to running list", loop_msg->seqnum);
379 dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
383 static void msg_move_exec(struct uevent_msg *msg)
385 list_move_tail(&msg->node, &exec_list);
387 expected_seqnum = msg->seqnum+1;
388 dbg("moved seq %llu to exec, next expected is %llu",
389 msg->seqnum, expected_seqnum);
392 /* msg queue management routine handles the timeouts and dispatches the events */
393 static void msg_queue_manager(void)
395 struct uevent_msg *loop_msg;
396 struct uevent_msg *tmp_msg;
399 int timeout = event_timeout;
401 dbg("msg queue manager, next expected is %llu", expected_seqnum);
404 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, node) {
405 /* move event with expected sequence to the exec list */
406 if (loop_msg->seqnum == expected_seqnum) {
407 msg_move_exec(loop_msg);
411 /* limit timeout during initialization phase */
413 if (timeout > UDEVD_INIT_EVENT_TIMEOUT)
414 timeout = UDEVD_INIT_EVENT_TIMEOUT;
415 dbg("initialization phase, timeout %i seconds", timeout);
418 /* move event with expired timeout to the exec list */
419 msg_age = info.uptime - loop_msg->queue_time;
420 dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age);
421 if (msg_age >= timeout) {
422 msg_move_exec(loop_msg);
431 /* set timeout for remaining queued events */
432 if (!list_empty(&msg_list)) {
433 struct itimerval itv = {{0, 0}, {timeout - msg_age, 0}};
434 info("next event expires in %li seconds", timeout - msg_age);
435 setitimer(ITIMER_REAL, &itv, NULL);
439 static struct uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
443 struct uevent_msg *msg;
447 msg = malloc(sizeof(struct uevent_msg) + buf_size);
450 memset(msg, 0x00, sizeof(struct uevent_msg) + buf_size);
452 /* copy environment buffer and reconstruct envp */
453 memcpy(msg->envbuf, buf, buf_size);
455 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
459 key = &msg->envbuf[bufpos];
460 keylen = strlen(key);
462 bufpos += keylen + 1;
463 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
465 /* remember some keys for further processing */
466 if (strncmp(key, "ACTION=", 7) == 0)
467 msg->action = &key[7];
468 else if (strncmp(key, "DEVPATH=", 8) == 0)
469 msg->devpath = &key[8];
470 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
471 msg->subsystem = &key[10];
472 else if (strncmp(key, "SEQNUM=", 7) == 0)
473 msg->seqnum = strtoull(&key[7], NULL, 10);
474 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
475 msg->physdevpath = &key[12];
476 else if (strncmp(key, "MAJOR=", 6) == 0)
477 major = strtoull(&key[6], NULL, 10);
478 else if (strncmp(key, "MINOR=", 6) == 0)
479 minor = strtoull(&key[6], NULL, 10);
480 else if (strncmp(key, "TIMEOUT=", 8) == 0)
481 msg->timeout = strtoull(&key[8], NULL, 10);
483 msg->devt = makedev(major, minor);
484 msg->envp[i++] = "UDEVD_EVENT=1";
490 /* receive the udevd message from userspace */
491 static struct uevent_msg *get_udevd_msg(void)
493 static struct udevd_msg usend_msg;
494 struct uevent_msg *msg;
497 struct cmsghdr *cmsg;
500 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
504 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
505 iov.iov_base = &usend_msg;
506 iov.iov_len = sizeof(struct udevd_msg);
508 memset(&smsg, 0x00, sizeof(struct msghdr));
511 smsg.msg_control = cred_msg;
512 smsg.msg_controllen = sizeof(cred_msg);
514 size = recvmsg(udevd_sock, &smsg, 0);
517 err("unable to receive udevd message");
520 cmsg = CMSG_FIRSTHDR(&smsg);
521 cred = (struct ucred *) CMSG_DATA(cmsg);
523 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
524 err("no sender credentials received, message ignored");
528 if (cred->uid != 0) {
529 err("sender uid=%i, message ignored", cred->uid);
533 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
534 err("message magic '%s' doesn't match, ignore it", usend_msg.magic);
538 switch (usend_msg.type) {
539 case UDEVD_UEVENT_UDEVSEND:
540 case UDEVD_UEVENT_INITSEND:
541 info("udevd event message received");
542 envbuf_size = size - offsetof(struct udevd_msg, envbuf);
543 dbg("envbuf_size=%i", envbuf_size);
544 msg = get_msg_from_envbuf(usend_msg.envbuf, envbuf_size);
547 msg->type = usend_msg.type;
549 case UDEVD_STOP_EXEC_QUEUE:
550 info("udevd message (STOP_EXEC_QUEUE) received");
553 case UDEVD_START_EXEC_QUEUE:
554 info("udevd message (START_EXEC_QUEUE) received");
556 exec_queue_manager();
558 case UDEVD_SET_LOG_LEVEL:
559 intval = (int *) usend_msg.envbuf;
560 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
561 udev_log_priority = *intval;
562 sprintf(log, "UDEV_LOG=%i", udev_log_priority);
565 case UDEVD_SET_MAX_CHILDS:
566 intval = (int *) usend_msg.envbuf;
567 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
568 max_childs = *intval;
571 dbg("unknown message type");
576 /* receive the kernel user event message and do some sanity checks */
577 static struct uevent_msg *get_netlink_msg(void)
579 struct uevent_msg *msg;
582 static char buffer[UEVENT_BUFFER_SIZE + 512];
585 size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
588 err("unable to receive udevd message");
592 if ((size_t)size > sizeof(buffer)-1)
593 size = sizeof(buffer)-1;
595 dbg("uevent_size=%zi", size);
597 /* start of event payload */
598 bufpos = strlen(buffer)+1;
599 msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
602 msg->type = UDEVD_UEVENT_NETLINK;
604 /* validate message */
605 pos = strchr(buffer, '@');
607 err("invalid uevent '%s'", buffer);
613 if (msg->action == NULL) {
614 err("no ACTION in payload found, skip event '%s'", buffer);
619 if (strcmp(msg->action, buffer) != 0) {
620 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
628 static void asmlinkage sig_handler(int signum)
636 /* set flag, then write to pipe if needed */
640 /* set flag, then write to pipe if needed */
641 sigchilds_waiting = 1;
645 /* write to pipe, which will wakeup select() in our mainloop */
646 write(signal_pipe[WRITE_END], "", 1);
649 static void udev_done(int pid)
651 /* find msg associated with pid and delete it */
652 struct uevent_msg *msg;
655 list_for_each_entry(msg, &running_list, node) {
656 if (msg->pid == pid) {
659 info("seq %llu, pid [%d] exit, %ld seconds old", msg->seqnum, msg->pid, info.uptime - msg->queue_time);
661 info("seq 0, pid [%d] exit", msg->pid);
662 msg_queue_delete(msg);
664 /* we want to run the exec queue manager since there may
665 * be events waiting with the devpath of the one that
674 static void reap_sigchilds(void)
679 pid = waitpid(-1, NULL, WNOHANG);
686 static int init_udevd_socket(void)
688 struct sockaddr_un saddr;
689 const int buffersize = 16 * 1024 * 1024;
691 const int feature_on = 1;
694 memset(&saddr, 0x00, sizeof(saddr));
695 saddr.sun_family = AF_LOCAL;
696 /* use abstract namespace for socket path */
697 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
698 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
700 udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
701 if (udevd_sock == -1) {
702 err("error getting socket, %s", strerror(errno));
706 /* set receive buffersize */
707 setsockopt(udevd_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
709 /* the bind takes care of ensuring only one copy running */
710 retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
712 err("bind failed, %s", strerror(errno));
717 /* enable receiving of the sender credentials */
718 setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
723 static int init_uevent_netlink_sock(void)
725 struct sockaddr_nl snl;
726 const int buffersize = 16 * 1024 * 1024;
729 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
730 snl.nl_family = AF_NETLINK;
731 snl.nl_pid = getpid();
732 snl.nl_groups = 0xffffffff;
734 uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
735 if (uevent_netlink_sock == -1) {
736 err("error getting socket, %s", strerror(errno));
740 /* set receive buffersize */
741 setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
743 retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
744 sizeof(struct sockaddr_nl));
746 err("bind failed, %s", strerror(errno));
747 close(uevent_netlink_sock);
748 uevent_netlink_sock = -1;
755 int main(int argc, char *argv[], char *envp[])
759 struct sigaction act;
762 int uevent_netlink_active = 0;
766 /* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
767 devnull = open("/dev/null", O_RDWR);
769 if (devnull != STDIN_FILENO)
770 dup2(devnull, STDIN_FILENO);
771 if (devnull != STDOUT_FILENO)
772 dup2(devnull, STDOUT_FILENO);
773 if (devnull != STDERR_FILENO)
774 dup2(devnull, STDERR_FILENO);
775 if (devnull > STDERR_FILENO)
779 logging_init("udevd");
781 err("fatal, could not open /dev/null");
784 dbg("version %s", UDEV_VERSION);
787 err("need to be root, exit");
791 for (i = 1 ; i < argc; i++) {
793 if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
794 info("will daemonize");
797 if (strcmp(arg, "--stop-exec-queue") == 0) {
798 info("will not execute events until START_EXEC_QUEUE is received");
809 dbg("damonized fork running");
812 err("fork of daemon failed");
820 /* become session leader */
822 dbg("our session is %d", sid);
825 umask(umask(077) | 022);
827 /* set a reasonable scheduling priority for the daemon */
828 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
830 /* setup signal handler pipe */
831 retval = pipe(signal_pipe);
833 err("error getting pipes: %s", strerror(errno));
836 retval = fcntl(signal_pipe[READ_END], F_SETFL, O_NONBLOCK);
838 err("error fcntl on read pipe: %s", strerror(errno));
841 retval = fcntl(signal_pipe[READ_END], F_SETFD, FD_CLOEXEC);
843 err("error fcntl on read pipe: %s", strerror(errno));
844 retval = fcntl(signal_pipe[WRITE_END], F_SETFL, O_NONBLOCK);
846 err("error fcntl on write pipe: %s", strerror(errno));
849 retval = fcntl(signal_pipe[WRITE_END], F_SETFD, FD_CLOEXEC);
851 err("error fcntl on write pipe: %s", strerror(errno));
853 /* set signal handlers */
854 memset(&act, 0x00, sizeof(struct sigaction));
855 act.sa_handler = (void (*)(int)) sig_handler;
856 sigemptyset(&act.sa_mask);
857 act.sa_flags = SA_RESTART;
858 sigaction(SIGINT, &act, NULL);
859 sigaction(SIGTERM, &act, NULL);
860 sigaction(SIGALRM, &act, NULL);
861 sigaction(SIGCHLD, &act, NULL);
862 sigaction(SIGHUP, &act, NULL);
864 if (init_udevd_socket() < 0) {
865 if (errno == EADDRINUSE)
866 dbg("another udevd running, exit");
868 dbg("error initialising udevd socket: %s", strerror(errno));
873 if (init_uevent_netlink_sock() < 0)
874 info("uevent socket not available");
876 /* override of forked udev binary, used for testing */
877 udev_bin = getenv("UDEV_BIN");
878 if (udev_bin != NULL)
879 info("udev binary is set to '%s'", udev_bin);
883 /* init of expected_seqnum value */
884 value = getenv("UDEVD_EXPECTED_SEQNUM");
886 expected_seqnum = strtoull(value, NULL, 10);
887 info("initialize expected_seqnum to %llu", expected_seqnum);
890 /* timeout to wait for missing events */
891 value = getenv("UDEVD_EVENT_TIMEOUT");
893 event_timeout = strtoul(value, NULL, 10);
895 event_timeout = UDEVD_EVENT_TIMEOUT;
896 info("initialize event_timeout to %u", event_timeout);
898 /* maximum limit of forked childs */
899 value = getenv("UDEVD_MAX_CHILDS");
901 max_childs = strtoul(value, NULL, 10);
903 max_childs = UDEVD_MAX_CHILDS;
904 info("initialize max_childs to %u", max_childs);
906 /* start to throttle forking if maximum number of _running_ childs is reached */
907 value = getenv("UDEVD_MAX_CHILDS_RUNNING");
909 max_childs_running = strtoull(value, NULL, 10);
911 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
912 info("initialize max_childs_running to %u", max_childs_running);
914 /* export log_priority , as called programs may want to follow that setting */
915 sprintf(log, "UDEV_LOG=%i", udev_log_priority);
919 struct uevent_msg *msg;
923 FD_SET(signal_pipe[READ_END], &readfds);
924 FD_SET(udevd_sock, &readfds);
925 if (uevent_netlink_sock > 0)
926 FD_SET(uevent_netlink_sock, &readfds);
928 fdcount = select(UDEV_MAX(udevd_sock, uevent_netlink_sock)+1, &readfds, NULL, NULL, NULL);
931 dbg("error in select: %s", strerror(errno));
935 /* get user socket message */
936 if (FD_ISSET(udevd_sock, &readfds)) {
937 msg = get_udevd_msg();
939 /* discard kernel messages if netlink is active */
940 if (uevent_netlink_active && msg->type == UDEVD_UEVENT_UDEVSEND && msg->seqnum != 0) {
941 info("skip uevent_helper message with SEQNUM, netlink is active");
944 msg_queue_insert(msg);
948 /* get kernel netlink message */
949 if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
950 msg = get_netlink_msg();
952 msg_queue_insert(msg);
953 /* disable udevsend with first netlink message */
954 if (!uevent_netlink_active) {
955 info("uevent_nl message received, disable udevsend messages");
956 uevent_netlink_active = 1;
961 /* received a signal, clear our notification pipe */
962 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
965 read(signal_pipe[READ_END], &buf, sizeof(buf));
968 /* forked child have returned */
969 if (sigchilds_waiting) {
970 sigchilds_waiting = 0;
980 /* clean up running_list before calling exec_queue_manager() */
981 if (sigchilds_waiting) {
982 sigchilds_waiting = 0;
988 exec_queue_manager();
993 if (signal_pipe[READ_END] > 0)
994 close(signal_pipe[READ_END]);
995 if (signal_pipe[WRITE_END] > 0)
996 close(signal_pipe[WRITE_END]);
1000 if (uevent_netlink_sock > 0)
1001 close(uevent_netlink_sock);