2 * udevd.c - hotplug event serializer
4 * Copyright (C) 2004 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.
32 #include <sys/types.h>
33 #include <sys/socket.h>
36 #include <sys/sysinfo.h>
41 #include "udev_version.h"
42 #include "udev_utils.h"
47 static int udevsendsock;
49 static int pipefds[2];
50 static long startup_time;
51 static unsigned long long expected_seqnum = 0;
52 static volatile int sigchilds_waiting;
53 static volatile int run_msg_q;
54 static volatile int sig_flag;
55 static int run_exec_q;
57 static LIST_HEAD(msg_list);
58 static LIST_HEAD(exec_list);
59 static LIST_HEAD(running_list);
61 static void exec_queue_manager(void);
62 static void msg_queue_manager(void);
63 static void user_sighandler(void);
64 static void reap_sigchilds(void);
68 void log_message (int level, const char *format, ...)
72 va_start(args, format);
73 vsyslog(level, format, args);
78 #define msg_dump(msg) \
79 dbg("msg_dump: sequence %llu, '%s', '%s', '%s'", \
80 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
82 static void msg_dump_queue(void)
85 struct hotplug_msg *msg;
87 list_for_each_entry(msg, &msg_list, list)
88 dbg("sequence %llu in queue", msg->seqnum);
92 static void run_queue_delete(struct hotplug_msg *msg)
98 /* orders the message in the queue by sequence number */
99 static void msg_queue_insert(struct hotplug_msg *msg)
101 struct hotplug_msg *loop_msg;
104 if (msg->seqnum == 0) {
105 dbg("no SEQNUM, move straight to the exec queue");
106 list_add(&msg->list, &exec_list);
111 /* sort message by sequence number into list */
112 list_for_each_entry_reverse(loop_msg, &msg_list, list)
113 if (loop_msg->seqnum < msg->seqnum)
116 /* store timestamp of queuing */
118 msg->queue_time = info.uptime;
120 list_add(&msg->list, &loop_msg->list);
121 dbg("queued message seq %llu", msg->seqnum);
123 /* run msg queue manager */
129 /* forks event and removes event from run queue when finished */
130 static void udev_run(struct hotplug_msg *msg)
132 char *const argv[] = { "udev", msg->subsystem, NULL };
141 execve(udev_bin, argv, msg->envp);
142 dbg("exec of child failed");
146 dbg("fork of child failed");
147 run_queue_delete(msg);
148 /* note: we never managed to run, so we had no impact on
149 * running_with_devpath(), so don't bother setting run_exec_q
153 /* get SIGCHLD in main loop */
154 dbg("==> exec seq %llu [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
159 static int compare_devpath(const char *running, const char *waiting)
163 for (i = 0; i < DEVPATH_SIZE; i++) {
164 /* identical device event found */
165 if (running[i] == '\0' && waiting[i] == '\0')
168 /* parent device event found */
169 if (running[i] == '\0' && waiting[i] == '/')
172 /* child device event found */
173 if (running[i] == '/' && waiting[i] == '\0')
176 /* no matching event */
177 if (running[i] != waiting[i])
184 /* returns still running task for the same device, its parent or its physical device */
185 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
187 struct hotplug_msg *loop_msg;
189 if (msg->devpath == NULL)
192 list_for_each_entry(loop_msg, &running_list, list) {
193 if (loop_msg->devpath == NULL)
196 /* return running parent/child device event */
197 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0)
200 /* return running physical device event */
201 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
202 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0)
209 /* exec queue management routine executes the events and serializes events in the same sequence */
210 static void exec_queue_manager(void)
212 struct hotplug_msg *loop_msg;
213 struct hotplug_msg *tmp_msg;
214 struct hotplug_msg *msg;
216 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
217 msg = running_with_devpath(loop_msg);
219 /* move event to run list */
220 list_move_tail(&loop_msg->list, &running_list);
222 dbg("moved seq %llu to running list", loop_msg->seqnum);
224 dbg("delay seq %llu (%s), cause seq %llu (%s) is still running",
225 loop_msg->seqnum, loop_msg->devpath, msg->seqnum, msg->devpath);
230 static void msg_move_exec(struct hotplug_msg *msg)
232 list_move_tail(&msg->list, &exec_list);
234 expected_seqnum = msg->seqnum+1;
235 dbg("moved seq %llu to exec, next expected is %llu",
236 msg->seqnum, expected_seqnum);
239 /* msg queue management routine handles the timeouts and dispatches the events */
240 static void msg_queue_manager(void)
242 struct hotplug_msg *loop_msg;
243 struct hotplug_msg *tmp_msg;
246 static int timeout = EVENT_INIT_TIMEOUT_SEC;
249 dbg("msg queue manager, next expected is %llu", expected_seqnum);
251 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
252 /* move event with expected sequence to the exec list */
253 if (loop_msg->seqnum == expected_seqnum) {
254 msg_move_exec(loop_msg);
258 /* see if we are in the initialization phase and wait for the very first events */
259 if (init && (info.uptime - startup_time >= INIT_TIME_SEC)) {
261 timeout = EVENT_TIMEOUT_SEC;
262 dbg("initialization phase passed, set timeout to %i seconds", EVENT_TIMEOUT_SEC);
265 /* move event with expired timeout to the exec list */
267 msg_age = info.uptime - loop_msg->queue_time;
268 dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age);
269 if (msg_age >= timeout) {
270 msg_move_exec(loop_msg);
279 /* set timeout for remaining queued events */
280 if (list_empty(&msg_list) == 0) {
281 struct itimerval itv = {{0, 0}, {timeout - msg_age, 0}};
282 dbg("next event expires in %li seconds", timeout - msg_age);
283 setitimer(ITIMER_REAL, &itv, NULL);
287 /* receive the udevsend message and do some sanity checks */
288 static struct hotplug_msg *get_udevsend_msg(void)
290 static struct udevsend_msg usend_msg;
291 struct hotplug_msg *msg;
296 struct cmsghdr *cmsg;
299 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
302 memset(&usend_msg, 0x00, sizeof(struct udevsend_msg));
303 iov.iov_base = &usend_msg;
304 iov.iov_len = sizeof(struct udevsend_msg);
306 memset(&smsg, 0x00, sizeof(struct msghdr));
309 smsg.msg_control = cred_msg;
310 smsg.msg_controllen = sizeof(cred_msg);
312 size = recvmsg(udevsendsock, &smsg, 0);
315 dbg("unable to receive udevsend message");
318 cmsg = CMSG_FIRSTHDR(&smsg);
319 cred = (struct ucred *) CMSG_DATA(cmsg);
321 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
322 dbg("no sender credentials received, message ignored");
326 if (cred->uid != 0) {
327 dbg("sender uid=%i, message ignored", cred->uid);
331 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
332 dbg("message magic '%s' doesn't match, ignore it", usend_msg.magic);
336 envbuf_size = size - offsetof(struct udevsend_msg, envbuf);
337 dbg("envbuf_size=%i", envbuf_size);
338 msg = malloc(sizeof(struct hotplug_msg) + envbuf_size);
342 memset(msg, 0x00, sizeof(struct hotplug_msg) + envbuf_size);
344 /* copy environment buffer and reconstruct envp */
345 memcpy(msg->envbuf, usend_msg.envbuf, envbuf_size);
347 for (i = 0; (bufpos < envbuf_size) && (i < HOTPLUG_NUM_ENVP-2); i++) {
351 key = &msg->envbuf[bufpos];
352 keylen = strlen(key);
354 bufpos += keylen + 1;
355 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
357 /* remember some keys for further processing */
358 if (strncmp(key, "ACTION=", 7) == 0)
359 msg->action = &key[7];
361 if (strncmp(key, "DEVPATH=", 8) == 0)
362 msg->devpath = &key[8];
364 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
365 msg->subsystem = &key[10];
367 if (strncmp(key, "SEQNUM=", 7) == 0)
368 msg->seqnum = strtoull(&key[7], NULL, 10);
370 if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
371 msg->physdevpath = &key[12];
373 msg->envp[i++] = "UDEVD_EVENT=1";
379 static void asmlinkage sig_handler(int signum)
389 /* set flag, then write to pipe if needed */
394 /* set flag, then write to pipe if needed */
395 sigchilds_waiting = 1;
401 /* if pipe is empty, write to pipe to force select to return
402 * immediately when it gets called
405 rc = write(pipefds[1],&signum,sizeof(signum));
411 static void udev_done(int pid)
413 /* find msg associated with pid and delete it */
414 struct hotplug_msg *msg;
416 list_for_each_entry(msg, &running_list, list) {
417 if (msg->pid == pid) {
418 dbg("<== exec seq %llu came back", msg->seqnum);
419 run_queue_delete(msg);
421 /* we want to run the exec queue manager since there may
422 * be events waiting with the devpath of the one that
431 static void reap_sigchilds(void)
434 int pid = waitpid(-1, NULL, WNOHANG);
435 if ((pid == -1) || (pid == 0))
441 /* just read everything from the pipe and clear the flag,
442 * the flags was set in the signal handler
444 static void user_sighandler(void)
448 int rc = read(pipefds[0], &sig, sizeof(sig));
456 int main(int argc, char *argv[], char *envp[])
460 struct sockaddr_un saddr;
463 const int feature_on = 1;
464 struct sigaction act;
467 logging_init("udevd");
468 dbg("version %s", UDEV_VERSION);
471 dbg("need to be root, exit");
475 /* daemonize on request */
476 if (argc == 2 && strcmp(argv[1], "-d") == 0) {
482 dbg("damonized fork running");
485 dbg("fork of daemon failed");
493 /* make sure we don't lock any path */
495 umask(umask(077) | 022);
497 /* Set fds to dev/null */
498 fd = open( "/dev/null", O_RDWR );
500 dbg("error opening /dev/null %s", strerror(errno));
509 /* become session leader */
512 /* setup signal handler pipe */
513 retval = pipe(pipefds);
515 dbg("error getting pipes: %s", strerror(errno));
519 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
521 dbg("error fcntl on read pipe: %s", strerror(errno));
524 retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC);
526 dbg("error fcntl on read pipe: %s", strerror(errno));
530 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
532 dbg("error fcntl on write pipe: %s", strerror(errno));
535 retval = fcntl(pipefds[1], F_SETFD, FD_CLOEXEC);
537 dbg("error fcntl on write pipe: %s", strerror(errno));
541 /* set signal handlers */
542 act.sa_handler = (void (*) (int))sig_handler;
543 sigemptyset(&act.sa_mask);
544 act.sa_flags = SA_RESTART;
545 sigaction(SIGINT, &act, NULL);
546 sigaction(SIGTERM, &act, NULL);
547 sigaction(SIGALRM, &act, NULL);
548 sigaction(SIGCHLD, &act, NULL);
550 memset(&saddr, 0x00, sizeof(saddr));
551 saddr.sun_family = AF_LOCAL;
552 /* use abstract namespace for socket path */
553 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
554 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
556 udevsendsock = socket(AF_LOCAL, SOCK_DGRAM, 0);
557 if (udevsendsock == -1) {
558 dbg("error getting socket, exit");
562 /* the bind takes care of ensuring only one copy running */
563 retval = bind(udevsendsock, (struct sockaddr *) &saddr, addrlen);
565 dbg("bind failed, exit");
570 /* enable receiving of the sender credentials */
571 setsockopt(udevsendsock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
573 /* possible override of udev binary, used for testing */
574 udev_bin = getenv("UDEV_BIN");
575 if (udev_bin != NULL)
576 dbg("udev binary is set to '%s'", udev_bin);
580 /* handle special startup timeout*/
582 startup_time = info.uptime;
585 FD_SET(udevsendsock, &readfds);
586 FD_SET(pipefds[0], &readfds);
587 maxsockplus = udevsendsock+1;
589 struct hotplug_msg *msg;
591 fd_set workreadfds = readfds;
592 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
596 dbg("error in select: %s", strerror(errno));
600 if (FD_ISSET(udevsendsock, &workreadfds)) {
601 msg = get_udevsend_msg();
603 msg_queue_insert(msg);
606 if (FD_ISSET(pipefds[0], &workreadfds))
609 if (sigchilds_waiting) {
610 sigchilds_waiting = 0;
620 /* clean up running_list before calling exec_queue_manager() */
621 if (sigchilds_waiting) {
622 sigchilds_waiting = 0;
627 exec_queue_manager();