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>
42 #include "udev_version.h"
47 static int udevsendsock;
49 static int pipefds[2];
50 static unsigned long long expected_seqnum = 0;
51 static volatile int sigchilds_waiting;
52 static volatile int run_msg_q;
53 static volatile int sig_flag;
54 static int run_exec_q;
56 static LIST_HEAD(msg_list);
57 static LIST_HEAD(exec_list);
58 static LIST_HEAD(running_list);
60 static void exec_queue_manager(void);
61 static void msg_queue_manager(void);
62 static void user_sighandler(void);
63 static void reap_sigchilds(void);
67 unsigned char logname[LOGNAME_SIZE];
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 /* sort message by sequence number into list. events
105 * will tend to come in order, so scan the list backwards
107 list_for_each_entry_reverse(loop_msg, &msg_list, list)
108 if (loop_msg->seqnum < msg->seqnum)
111 /* store timestamp of queuing */
113 msg->queue_time = info.uptime;
115 list_add(&msg->list, &loop_msg->list);
116 dbg("queued message seq %llu", msg->seqnum);
118 /* run msg queue manager */
124 /* forks event and removes event from run queue when finished */
125 static void udev_run(struct hotplug_msg *msg)
134 execle(udev_bin, "udev", msg->subsystem, NULL, msg->envp);
135 dbg("exec of child failed");
139 dbg("fork of child failed");
140 run_queue_delete(msg);
141 /* note: we never managed to run, so we had no impact on
142 * running_with_devpath(), so don't bother setting run_exec_q
146 /* get SIGCHLD in main loop */
147 dbg("==> exec seq %llu [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
152 /* returns already running task with devpath */
153 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
155 struct hotplug_msg *loop_msg;
156 list_for_each_entry(loop_msg, &running_list, list) {
157 if (loop_msg->devpath == NULL || msg->devpath == NULL)
160 if (strcmp(loop_msg->devpath, msg->devpath) == 0)
167 /* exec queue management routine executes the events and delays events for the same devpath */
168 static void exec_queue_manager(void)
170 struct hotplug_msg *loop_msg;
171 struct hotplug_msg *tmp_msg;
172 struct hotplug_msg *msg;
174 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
175 msg = running_with_devpath(loop_msg);
177 /* move event to run list */
178 list_move_tail(&loop_msg->list, &running_list);
180 dbg("moved seq %llu to running list", loop_msg->seqnum);
182 dbg("delay seq %llu, cause seq %llu already working on '%s'",
183 loop_msg->seqnum, msg->seqnum, msg->devpath);
188 static void msg_move_exec(struct hotplug_msg *msg)
190 list_move_tail(&msg->list, &exec_list);
192 expected_seqnum = msg->seqnum+1;
193 dbg("moved seq %llu to exec, next expected is %llu",
194 msg->seqnum, expected_seqnum);
197 /* msg queue management routine handles the timeouts and dispatches the events */
198 static void msg_queue_manager(void)
200 struct hotplug_msg *loop_msg;
201 struct hotplug_msg *tmp_msg;
205 dbg("msg queue manager, next expected is %llu", expected_seqnum);
207 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
208 /* move event with expected sequence to the exec list */
209 if (loop_msg->seqnum == expected_seqnum) {
210 msg_move_exec(loop_msg);
214 /* move event with expired timeout to the exec list */
216 msg_age = info.uptime - loop_msg->queue_time;
217 dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age);
218 if (msg_age > EVENT_TIMEOUT_SEC-1) {
219 msg_move_exec(loop_msg);
228 /* set timeout for remaining queued events */
229 if (list_empty(&msg_list) == 0) {
230 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
231 dbg("next event expires in %li seconds", EVENT_TIMEOUT_SEC - msg_age);
232 setitimer(ITIMER_REAL, &itv, NULL);
236 /* receive the msg, do some basic sanity checks, and queue it */
237 static void handle_udevsend_msg(int sock)
239 static struct udevsend_msg usend_msg;
240 struct hotplug_msg *msg;
245 struct cmsghdr *cmsg;
248 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
251 memset(&usend_msg, 0x00, sizeof(struct udevsend_msg));
252 iov.iov_base = &usend_msg;
253 iov.iov_len = sizeof(struct udevsend_msg);
255 memset(&smsg, 0x00, sizeof(struct msghdr));
258 smsg.msg_control = cred_msg;
259 smsg.msg_controllen = sizeof(cred_msg);
261 size = recvmsg(sock, &smsg, 0);
264 dbg("unable to receive message");
267 cmsg = CMSG_FIRSTHDR(&smsg);
268 cred = (struct ucred *) CMSG_DATA(cmsg);
270 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
271 dbg("no sender credentials received, message ignored");
275 if (cred->uid != 0) {
276 dbg("sender uid=%i, message ignored", cred->uid);
280 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
281 dbg("message magic '%s' doesn't match, ignore it", usend_msg.magic);
285 envbuf_size = size - offsetof(struct udevsend_msg, envbuf);
286 dbg("envbuf_size=%i", envbuf_size);
287 msg = malloc(sizeof(struct hotplug_msg) + envbuf_size);
288 memset(msg, 0x00, sizeof(struct hotplug_msg) + envbuf_size);
290 /* copy environment buffer and reconstruct envp */
291 memcpy(msg->envbuf, usend_msg.envbuf, envbuf_size);
293 for (i = 0; (bufpos < envbuf_size) && (i < HOTPLUG_NUM_ENVP-1); i++) {
297 key = &msg->envbuf[bufpos];
298 keylen = strlen(key);
300 bufpos += keylen + 1;
301 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
303 /* remember some keys for further processing */
304 if (strncmp(key, "ACTION=", 7) == 0)
305 msg->action = &key[7];
307 if (strncmp(key, "DEVPATH=", 8) == 0)
308 msg->devpath = &key[8];
310 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
311 msg->subsystem = &key[10];
313 if (strncmp(key, "SEQNUM=", 7) == 0)
314 msg->seqnum = strtoull(&key[7], NULL, 10);
318 /* if no seqnum is given, we move straight to exec queue */
319 if (msg->seqnum == 0) {
320 list_add(&msg->list, &exec_list);
323 msg_queue_insert(msg);
330 static void asmlinkage sig_handler(int signum)
340 /* set flag, then write to pipe if needed */
345 /* set flag, then write to pipe if needed */
346 sigchilds_waiting = 1;
352 /* if pipe is empty, write to pipe to force select to return
353 * immediately when it gets called
356 rc = write(pipefds[1],&signum,sizeof(signum));
362 static void udev_done(int pid)
364 /* find msg associated with pid and delete it */
365 struct hotplug_msg *msg;
367 list_for_each_entry(msg, &running_list, list) {
368 if (msg->pid == pid) {
369 dbg("<== exec seq %llu came back", msg->seqnum);
370 run_queue_delete(msg);
372 /* we want to run the exec queue manager since there may
373 * be events waiting with the devpath of the one that
382 static void reap_sigchilds(void)
385 int pid = waitpid(-1, NULL, WNOHANG);
386 if ((pid == -1) || (pid == 0))
392 /* just read everything from the pipe and clear the flag,
393 * the flags was set in the signal handler
395 static void user_sighandler(void)
399 int rc = read(pipefds[0], &sig, sizeof(sig));
407 int main(int argc, char *argv[], char *envp[])
410 struct sockaddr_un saddr;
413 const int feature_on = 1;
414 struct sigaction act;
417 logging_init("udevd");
418 dbg("version %s", UDEV_VERSION);
421 dbg("need to be root, exit");
425 /* make sure we don't lock any path */
427 umask(umask(077) | 022);
429 /* Set fds to dev/null */
430 fd = open( "/dev/null", O_RDWR );
432 dbg("error opening /dev/null %s", strerror(errno));
441 /* become session leader */
444 /* setup signal handler pipe */
445 retval = pipe(pipefds);
447 dbg("error getting pipes: %s", strerror(errno));
451 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
453 dbg("error fcntl on read pipe: %s", strerror(errno));
456 retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC);
458 dbg("error fcntl on read pipe: %s", strerror(errno));
462 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
464 dbg("error fcntl on write pipe: %s", strerror(errno));
467 retval = fcntl(pipefds[1], F_SETFD, FD_CLOEXEC);
469 dbg("error fcntl on write pipe: %s", strerror(errno));
473 /* set signal handlers */
474 act.sa_handler = (void (*) (int))sig_handler;
475 sigemptyset(&act.sa_mask);
476 act.sa_flags = SA_RESTART;
477 sigaction(SIGINT, &act, NULL);
478 sigaction(SIGTERM, &act, NULL);
479 sigaction(SIGALRM, &act, NULL);
480 sigaction(SIGCHLD, &act, NULL);
482 memset(&saddr, 0x00, sizeof(saddr));
483 saddr.sun_family = AF_LOCAL;
484 /* use abstract namespace for socket path */
485 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
486 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
488 udevsendsock = socket(AF_LOCAL, SOCK_DGRAM, 0);
489 if (udevsendsock == -1) {
490 dbg("error getting socket, exit");
494 /* the bind takes care of ensuring only one copy running */
495 retval = bind(udevsendsock, (struct sockaddr *) &saddr, addrlen);
497 dbg("bind failed, exit");
501 /* enable receiving of the sender credentials */
502 setsockopt(udevsendsock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
504 /* possible override of udev binary, used for testing */
505 udev_bin = getenv("UDEV_BIN");
506 if (udev_bin != NULL)
507 dbg("udev binary is set to '%s'", udev_bin);
512 FD_SET(udevsendsock, &readfds);
513 FD_SET(pipefds[0], &readfds);
514 maxsockplus = udevsendsock+1;
516 fd_set workreadfds = readfds;
517 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
521 dbg("error in select: %s", strerror(errno));
525 if (FD_ISSET(udevsendsock, &workreadfds))
526 handle_udevsend_msg(udevsendsock);
528 if (FD_ISSET(pipefds[0], &workreadfds))
531 if (sigchilds_waiting) {
532 sigchilds_waiting = 0;
542 /* clean up running_list before calling exec_queue_manager() */
543 if (sigchilds_waiting) {
544 sigchilds_waiting = 0;
549 exec_queue_manager();