2 * udevd.c - hotplug event 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.
33 #include <sys/select.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <sys/sysinfo.h>
41 #include <linux/netlink.h>
44 #include "udev_libc_wrapper.h"
46 #include "udev_version.h"
47 #include "udev_utils.h"
52 static int udevd_sock;
53 static int uevent_nl_sock;
56 static int pipefds[2];
57 static long startup_time;
58 static unsigned long long expected_seqnum;
59 static volatile int sigchilds_waiting;
60 static volatile int run_msg_q;
61 static volatile int sig_flag;
62 static int run_exec_q;
63 static int stop_exec_q;
65 static LIST_HEAD(msg_list);
66 static LIST_HEAD(exec_list);
67 static LIST_HEAD(running_list);
69 static void exec_queue_manager(void);
70 static void msg_queue_manager(void);
71 static void user_sighandler(void);
72 static void reap_sigchilds(void);
76 void log_message (int priority, const char *format, ...)
80 if (priority > udev_log_priority)
83 va_start(args, format);
84 vsyslog(priority, format, args);
89 static void msg_dump_queue(void)
92 struct uevent_msg *msg;
94 list_for_each_entry(msg, &msg_list, node)
95 dbg("sequence %llu in queue", msg->seqnum);
99 static void run_queue_delete(struct uevent_msg *msg)
101 list_del(&msg->node);
105 /* orders the message in the queue by sequence number */
106 static void msg_queue_insert(struct uevent_msg *msg)
108 struct uevent_msg *loop_msg;
111 if (msg->seqnum == 0) {
112 dbg("no SEQNUM, move straight to the exec queue");
113 list_add(&msg->node, &exec_list);
118 /* don't delay messages with timeout set */
120 dbg("move seq %llu with timeout %u to exec queue", msg->seqnum, msg->timeout);
121 list_add(&msg->node, &exec_list);
126 /* sort message by sequence number into list */
127 list_for_each_entry_reverse(loop_msg, &msg_list, node) {
128 if (loop_msg->seqnum < msg->seqnum)
131 if (loop_msg->seqnum == msg->seqnum) {
132 dbg("ignoring duplicate message seq %llu", msg->seqnum);
138 /* store timestamp of queuing */
140 msg->queue_time = info.uptime;
142 list_add(&msg->node, &loop_msg->node);
143 dbg("queued message seq %llu", msg->seqnum);
145 /* run msg queue manager */
151 /* forks event and removes event from run queue when finished */
152 static void execute_udev(struct uevent_msg *msg)
154 char *const argv[] = { "udev", msg->subsystem, NULL };
161 if (uevent_nl_sock != -1)
162 close(uevent_nl_sock);
166 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
167 execve(udev_bin, argv, msg->envp);
168 err("exec of child failed");
172 err("fork of child failed");
173 run_queue_delete(msg);
176 /* get SIGCHLD in main loop */
177 dbg("==> exec seq %llu [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
182 static int running_processes(void)
185 static char buf[4096];
190 f = open("/proc/stat", O_RDONLY);
194 len = read(f, buf, sizeof(buf));
202 pos = strstr(buf, "procs_running ");
206 if (sscanf(pos, "procs_running %u", &running) != 1)
212 /* return the number of process es in our session, count only until limit */
213 static int running_processes_in_session(pid_t session, int limit)
219 dir = opendir("/proc");
223 /* read process info from /proc */
224 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
230 pid_t ppid, pgrp, sess;
233 if (!isdigit(dent->d_name[0]))
236 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
237 procdir[sizeof(procdir)-1] = '\0';
239 f = open(procdir, O_RDONLY);
243 len = read(f, line, sizeof(line));
251 /* skip ugly program name */
252 pos = strrchr(line, ')') + 2;
256 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
259 /* count only processes in our session */
263 /* count only running, no sleeping processes */
268 if (limit > 0 && running >= limit)
276 static int compare_devpath(const char *running, const char *waiting)
280 for (i = 0; i < PATH_SIZE; i++) {
281 /* identical device event found */
282 if (running[i] == '\0' && waiting[i] == '\0')
285 /* parent device event found */
286 if (running[i] == '\0' && waiting[i] == '/')
289 /* child device event found */
290 if (running[i] == '/' && waiting[i] == '\0')
293 /* no matching event */
294 if (running[i] != waiting[i])
301 /* returns still running task for the same device, its parent or its physical device */
302 static struct uevent_msg *running_with_devpath(struct uevent_msg *msg)
304 struct uevent_msg *loop_msg;
306 if (msg->devpath == NULL)
309 /* skip any events with a timeout set */
313 list_for_each_entry(loop_msg, &running_list, node) {
314 if (loop_msg->devpath == NULL)
317 /* return running parent/child device event */
318 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0)
321 /* return running physical device event */
322 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
323 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0)
330 /* exec queue management routine executes the events and serializes events in the same sequence */
331 static void exec_queue_manager(void)
333 struct uevent_msg *loop_msg;
334 struct uevent_msg *tmp_msg;
335 struct uevent_msg *msg;
338 running = running_processes();
339 dbg("%d processes runnning on system", running);
341 running = THROTTLE_MAX_RUNNING_CHILDS;
343 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
344 /* check running processes in our session and possibly throttle */
345 if (running >= THROTTLE_MAX_RUNNING_CHILDS) {
346 running = running_processes_in_session(sid, THROTTLE_MAX_RUNNING_CHILDS+10);
347 dbg("%d processes running in session", running);
348 if (running >= THROTTLE_MAX_RUNNING_CHILDS) {
349 dbg("delay seq %llu, cause too many processes already running", loop_msg->seqnum);
354 msg = running_with_devpath(loop_msg);
356 /* move event to run list */
357 list_move_tail(&loop_msg->node, &running_list);
358 execute_udev(loop_msg);
360 dbg("moved seq %llu to running list", loop_msg->seqnum);
362 dbg("delay seq %llu (%s), cause seq %llu (%s) is still running",
363 loop_msg->seqnum, loop_msg->devpath, msg->seqnum, msg->devpath);
368 static void msg_move_exec(struct uevent_msg *msg)
370 list_move_tail(&msg->node, &exec_list);
372 expected_seqnum = msg->seqnum+1;
373 dbg("moved seq %llu to exec, next expected is %llu",
374 msg->seqnum, expected_seqnum);
377 /* msg queue management routine handles the timeouts and dispatches the events */
378 static void msg_queue_manager(void)
380 struct uevent_msg *loop_msg;
381 struct uevent_msg *tmp_msg;
384 static int timeout = EVENT_INIT_TIMEOUT_SEC;
387 dbg("msg queue manager, next expected is %llu", expected_seqnum);
389 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, node) {
390 /* move event with expected sequence to the exec list */
391 if (loop_msg->seqnum == expected_seqnum) {
392 msg_move_exec(loop_msg);
396 /* see if we are in the initialization phase and wait for the very first events */
397 if (init && (info.uptime - startup_time >= INIT_TIME_SEC)) {
399 timeout = EVENT_TIMEOUT_SEC;
400 dbg("initialization phase passed, set timeout to %i seconds", EVENT_TIMEOUT_SEC);
403 /* move event with expired timeout to the exec list */
405 msg_age = info.uptime - loop_msg->queue_time;
406 dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age);
407 if (msg_age >= timeout) {
408 msg_move_exec(loop_msg);
417 /* set timeout for remaining queued events */
418 if (list_empty(&msg_list) == 0) {
419 struct itimerval itv = {{0, 0}, {timeout - msg_age, 0}};
420 dbg("next event expires in %li seconds", timeout - msg_age);
421 setitimer(ITIMER_REAL, &itv, NULL);
425 static struct uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
429 struct uevent_msg *msg;
431 msg = malloc(sizeof(struct uevent_msg) + buf_size);
434 memset(msg, 0x00, sizeof(struct uevent_msg) + buf_size);
436 /* copy environment buffer and reconstruct envp */
437 memcpy(msg->envbuf, buf, buf_size);
439 for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
443 key = &msg->envbuf[bufpos];
444 keylen = strlen(key);
446 bufpos += keylen + 1;
447 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
449 /* remember some keys for further processing */
450 if (strncmp(key, "ACTION=", 7) == 0)
451 msg->action = &key[7];
453 if (strncmp(key, "DEVPATH=", 8) == 0)
454 msg->devpath = &key[8];
456 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
457 msg->subsystem = &key[10];
459 if (strncmp(key, "SEQNUM=", 7) == 0)
460 msg->seqnum = strtoull(&key[7], NULL, 10);
462 if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
463 msg->physdevpath = &key[12];
465 if (strncmp(key, "TIMEOUT=", 8) == 0)
466 msg->timeout = strtoull(&key[8], NULL, 10);
468 msg->envp[i++] = "UDEVD_EVENT=1";
474 /* receive the udevd message from userspace */
475 static struct uevent_msg *get_udevd_msg(void)
477 static struct udevd_msg usend_msg;
478 struct uevent_msg *msg;
481 struct cmsghdr *cmsg;
484 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
487 memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
488 iov.iov_base = &usend_msg;
489 iov.iov_len = sizeof(struct udevd_msg);
491 memset(&smsg, 0x00, sizeof(struct msghdr));
494 smsg.msg_control = cred_msg;
495 smsg.msg_controllen = sizeof(cred_msg);
497 size = recvmsg(udevd_sock, &smsg, 0);
500 dbg("unable to receive udevd message");
503 cmsg = CMSG_FIRSTHDR(&smsg);
504 cred = (struct ucred *) CMSG_DATA(cmsg);
506 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
507 info("no sender credentials received, message ignored");
511 if (cred->uid != 0) {
512 info("sender uid=%i, message ignored", cred->uid);
516 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
517 info("message magic '%s' doesn't match, ignore it", usend_msg.magic);
521 switch (usend_msg.type) {
523 dbg("udevd message (UEVENT) received");
524 envbuf_size = size - offsetof(struct udevd_msg, envbuf);
525 dbg("envbuf_size=%i", envbuf_size);
526 msg = get_msg_from_envbuf(usend_msg.envbuf, envbuf_size);
530 case UDEVD_STOP_EXEC_QUEUE:
531 dbg("udevd message (STOP_EXEC_QUEUE) received");
534 case UDEVD_START_EXEC_QUEUE:
535 dbg("udevd message (START_EXEC_QUEUE) received");
537 exec_queue_manager();
540 dbg("unknown message type");
545 /* receive the kernel user event message and do some sanity checks */
546 static struct uevent_msg *get_uevent_msg(void)
548 struct uevent_msg *msg;
551 static char buffer[UEVENT_BUFFER_SIZE + 512];
554 size = recv(uevent_nl_sock, &buffer, sizeof(buffer), 0);
557 dbg("unable to receive udevd message");
561 if ((size_t)size > sizeof(buffer)-1)
562 size = sizeof(buffer)-1;
564 dbg("uevent_size=%i", size);
566 /* start of event payload */
567 bufpos = strlen(buffer)+1;
568 msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
572 /* validate message */
573 pos = strchr(buffer, '@');
575 dbg("invalid uevent '%s'", buffer);
581 if (msg->action == NULL) {
582 dbg("no ACTION in payload found, skip event '%s'", buffer);
587 if (strcmp(msg->action, buffer) != 0) {
588 dbg("ACTION in payload does not match uevent, skip event '%s'", buffer);
596 static void asmlinkage sig_handler(int signum)
606 /* set flag, then write to pipe if needed */
611 /* set flag, then write to pipe if needed */
612 sigchilds_waiting = 1;
618 /* if pipe is empty, write to pipe to force select to return
619 * immediately when it gets called
622 rc = write(pipefds[1],&signum,sizeof(signum));
628 static void udev_done(int pid)
630 /* find msg associated with pid and delete it */
631 struct uevent_msg *msg;
633 list_for_each_entry(msg, &running_list, node) {
634 if (msg->pid == pid) {
635 dbg("<== exec seq %llu came back", msg->seqnum);
636 run_queue_delete(msg);
638 /* we want to run the exec queue manager since there may
639 * be events waiting with the devpath of the one that
648 static void reap_sigchilds(void)
651 int pid = waitpid(-1, NULL, WNOHANG);
652 if ((pid == -1) || (pid == 0))
658 /* just read everything from the pipe and clear the flag,
659 * the flags was set in the signal handler
661 static void user_sighandler(void)
666 int rc = read(pipefds[0], &sig, sizeof(sig));
674 static int init_udevd_socket(void)
676 struct sockaddr_un saddr;
678 const int feature_on = 1;
681 memset(&saddr, 0x00, sizeof(saddr));
682 saddr.sun_family = AF_LOCAL;
683 /* use abstract namespace for socket path */
684 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
685 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
687 udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
688 if (udevd_sock == -1) {
689 err("error getting socket, %s", strerror(errno));
693 /* the bind takes care of ensuring only one copy running */
694 retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
696 err("bind failed, %s", strerror(errno));
701 /* enable receiving of the sender credentials */
702 setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
707 static int init_uevent_nl_sock(void)
709 struct sockaddr_nl snl;
712 memset(&snl, 0x00, sizeof(struct sockaddr_nl));
713 snl.nl_family = AF_NETLINK;
714 snl.nl_pid = getpid();
715 snl.nl_groups = 0xffffffff;
717 uevent_nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
718 if (uevent_nl_sock == -1) {
719 dbg("error getting socket, %s", strerror(errno));
723 retval = bind(uevent_nl_sock, (struct sockaddr *) &snl,
724 sizeof(struct sockaddr_nl));
726 dbg("bind failed, %s", strerror(errno));
727 close(uevent_nl_sock);
735 int main(int argc, char *argv[], char *envp[])
741 struct sigaction act;
743 const char *udevd_expected_seqnum;
744 int uevent_nl_active = 0;
746 logging_init("udevd");
748 dbg("version %s", UDEV_VERSION);
751 err("need to be root, exit");
755 /* daemonize on request */
756 if (argc == 2 && strcmp(argv[1], "-d") == 0) {
762 dbg("damonized fork running");
765 err("fork of daemon failed");
773 /* become session leader */
775 dbg("our session is %d", sid);
777 /* make sure we don't lock any path */
779 umask(umask(077) | 022);
781 /*set a reasonable scheduling priority for the daemon */
782 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
784 /* Set fds to dev/null */
785 fd = open( "/dev/null", O_RDWR );
793 err("error opening /dev/null %s", strerror(errno));
795 /* setup signal handler pipe */
796 retval = pipe(pipefds);
798 err("error getting pipes: %s", strerror(errno));
802 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
804 err("error fcntl on read pipe: %s", strerror(errno));
807 retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC);
809 err("error fcntl on read pipe: %s", strerror(errno));
811 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
813 err("error fcntl on write pipe: %s", strerror(errno));
816 retval = fcntl(pipefds[1], F_SETFD, FD_CLOEXEC);
818 err("error fcntl on write pipe: %s", strerror(errno));
820 /* set signal handlers */
821 memset(&act, 0x00, sizeof(struct sigaction));
822 act.sa_handler = (void (*)(int)) sig_handler;
823 sigemptyset(&act.sa_mask);
824 act.sa_flags = SA_RESTART;
825 sigaction(SIGINT, &act, NULL);
826 sigaction(SIGTERM, &act, NULL);
827 sigaction(SIGALRM, &act, NULL);
828 sigaction(SIGCHLD, &act, NULL);
830 if (init_uevent_nl_sock() < 0) {
831 dbg("uevent socket not available");
834 if (init_udevd_socket() < 0) {
835 if (errno == EADDRINUSE)
836 dbg("another udevd running, exit");
838 dbg("error initialising udevd socket: %s", strerror(errno));
843 /* possible override of udev binary, used for testing */
844 udev_bin = getenv("UDEV_BIN");
845 if (udev_bin != NULL)
846 info("udev binary is set to '%s'", udev_bin);
850 /* possible init of expected_seqnum value */
851 udevd_expected_seqnum = getenv("UDEVD_EXPECTED_SEQNUM");
852 if (udevd_expected_seqnum != NULL) {
853 expected_seqnum = strtoull(udevd_expected_seqnum, NULL, 10);
854 info("initialize expected_seqnum to %llu", expected_seqnum);
857 /* get current time to provide shorter timeout on startup */
859 startup_time = info.uptime;
862 FD_SET(udevd_sock, &readfds);
863 if (uevent_nl_sock != -1)
864 FD_SET(uevent_nl_sock, &readfds);
865 FD_SET(pipefds[0], &readfds);
866 maxsockplus = udevd_sock+1;
868 struct uevent_msg *msg;
870 fd_set workreadfds = readfds;
871 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
875 dbg("error in select: %s", strerror(errno));
879 if (FD_ISSET(udevd_sock, &workreadfds)) {
880 msg = get_udevd_msg();
882 /* discard kernel messages if netlink is active */
883 if (uevent_nl_active && msg->seqnum != 0) {
884 dbg("skip uevent_helper message, netlink is active");
888 msg_queue_insert(msg);
892 if (FD_ISSET(uevent_nl_sock, &workreadfds)) {
893 msg = get_uevent_msg();
895 msg_queue_insert(msg);
896 /* disable udevsend with first netlink message */
897 if (!uevent_nl_active) {
898 info("uevent_nl message received, disable uevent_helper messages");
899 uevent_nl_active = 1;
904 if (FD_ISSET(pipefds[0], &workreadfds))
907 if (sigchilds_waiting) {
908 sigchilds_waiting = 0;
918 /* clean up running_list before calling exec_queue_manager() */
919 if (sigchilds_waiting) {
920 sigchilds_waiting = 0;
926 exec_queue_manager();