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"
46 static int pipefds[2];
47 static unsigned long long expected_seqnum = 0;
48 static volatile int sigchilds_waiting;
49 static volatile int run_msg_q;
50 static volatile int sig_flag;
51 static int run_exec_q;
53 static LIST_HEAD(msg_list);
54 static LIST_HEAD(exec_list);
55 static LIST_HEAD(running_list);
57 static void exec_queue_manager(void);
58 static void msg_queue_manager(void);
59 static void user_sighandler(void);
60 static void reap_sigchilds(void);
64 unsigned char logname[LOGNAME_SIZE];
65 void log_message (int level, const char *format, ...)
69 va_start(args, format);
70 vsyslog(level, format, args);
75 #define msg_dump(msg) \
76 dbg("msg_dump: sequence %llu, '%s', '%s', '%s'", \
77 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
79 static void msg_dump_queue(void)
82 struct hotplug_msg *msg;
84 list_for_each_entry(msg, &msg_list, list)
85 dbg("sequence %llu in queue", msg->seqnum);
89 static void run_queue_delete(struct hotplug_msg *msg)
95 /* orders the message in the queue by sequence number */
96 static void msg_queue_insert(struct hotplug_msg *msg)
98 struct hotplug_msg *loop_msg;
101 /* sort message by sequence number into list. events
102 * will tend to come in order, so scan the list backwards
104 list_for_each_entry_reverse(loop_msg, &msg_list, list)
105 if (loop_msg->seqnum < msg->seqnum)
108 /* store timestamp of queuing */
110 msg->queue_time = info.uptime;
112 list_add(&msg->list, &loop_msg->list);
113 dbg("queued message seq %llu", msg->seqnum);
115 /* run msg queue manager */
121 /* forks event and removes event from run queue when finished */
122 static void udev_run(struct hotplug_msg *msg)
130 execle(udev_bin, "udev", msg->subsystem, NULL, msg->envp);
131 dbg("exec of child failed");
135 dbg("fork of child failed");
136 run_queue_delete(msg);
137 /* note: we never managed to run, so we had no impact on
138 * running_with_devpath(), so don't bother setting run_exec_q
142 /* get SIGCHLD in main loop */
143 dbg("==> exec seq %llu [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
148 /* returns already running task with devpath */
149 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
151 struct hotplug_msg *loop_msg;
152 list_for_each_entry(loop_msg, &running_list, list)
153 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
158 /* exec queue management routine executes the events and delays events for the same devpath */
159 static void exec_queue_manager(void)
161 struct hotplug_msg *loop_msg;
162 struct hotplug_msg *tmp_msg;
163 struct hotplug_msg *msg;
165 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
166 msg = running_with_devpath(loop_msg);
168 /* move event to run list */
169 list_move_tail(&loop_msg->list, &running_list);
171 dbg("moved seq %llu to running list", loop_msg->seqnum);
173 dbg("delay seq %llu, cause seq %llu already working on '%s'",
174 loop_msg->seqnum, msg->seqnum, msg->devpath);
179 static void msg_move_exec(struct hotplug_msg *msg)
181 list_move_tail(&msg->list, &exec_list);
183 expected_seqnum = msg->seqnum+1;
184 dbg("moved seq %llu to exec, next expected is %llu",
185 msg->seqnum, expected_seqnum);
188 /* msg queue management routine handles the timeouts and dispatches the events */
189 static void msg_queue_manager(void)
191 struct hotplug_msg *loop_msg;
192 struct hotplug_msg *tmp_msg;
196 dbg("msg queue manager, next expected is %llu", expected_seqnum);
198 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
199 /* move event with expected sequence to the exec list */
200 if (loop_msg->seqnum == expected_seqnum) {
201 msg_move_exec(loop_msg);
205 /* move event with expired timeout to the exec list */
207 msg_age = info.uptime - loop_msg->queue_time;
208 dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age);
209 if (msg_age > EVENT_TIMEOUT_SEC-1) {
210 msg_move_exec(loop_msg);
219 /* set timeout for remaining queued events */
220 if (list_empty(&msg_list) == 0) {
221 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
222 dbg("next event expires in %li seconds", EVENT_TIMEOUT_SEC - msg_age);
223 setitimer(ITIMER_REAL, &itv, NULL);
227 /* receive the msg, do some basic sanity checks, and queue it */
228 static void handle_udevsend_msg(int sock)
230 static struct udevsend_msg usend_msg;
231 struct hotplug_msg *msg;
236 struct cmsghdr *cmsg;
239 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
242 memset(&usend_msg, 0x00, sizeof(struct udevsend_msg));
243 iov.iov_base = &usend_msg;
244 iov.iov_len = sizeof(struct udevsend_msg);
246 memset(&smsg, 0x00, sizeof(struct msghdr));
249 smsg.msg_control = cred_msg;
250 smsg.msg_controllen = sizeof(cred_msg);
252 size = recvmsg(sock, &smsg, 0);
255 dbg("unable to receive message");
258 cmsg = CMSG_FIRSTHDR(&smsg);
259 cred = (struct ucred *) CMSG_DATA(cmsg);
261 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
262 dbg("no sender credentials received, message ignored");
266 if (cred->uid != 0) {
267 dbg("sender uid=%i, message ignored", cred->uid);
271 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
272 dbg("message magic '%s' doesn't match, ignore it", usend_msg.magic);
276 envbuf_size = size - offsetof(struct udevsend_msg, envbuf);
277 dbg("envbuf_size=%i", envbuf_size);
278 msg = malloc(sizeof(struct hotplug_msg) + envbuf_size);
279 memset(msg, 0x00, sizeof(struct hotplug_msg) + envbuf_size);
281 /* copy environment buffer and reconstruct envp */
282 memcpy(msg->envbuf, usend_msg.envbuf, envbuf_size);
284 for (i = 0; (bufpos < envbuf_size) && (i < HOTPLUG_NUM_ENVP-1); i++) {
288 key = &msg->envbuf[bufpos];
289 keylen = strlen(key);
291 bufpos += keylen + 1;
292 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
294 /* remember some keys for further processing */
295 if (strncmp(key, "ACTION=", 7) == 0)
296 msg->action = &key[7];
298 if (strncmp(key, "DEVPATH=", 8) == 0)
299 msg->devpath = &key[8];
301 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
302 msg->subsystem = &key[10];
304 if (strncmp(key, "SEQNUM=", 7) == 0)
305 msg->seqnum = strtoull(&key[7], NULL, 10);
309 /* if no seqnum is given, we move straight to exec queue */
310 if (msg->seqnum == 0) {
311 list_add(&msg->list, &exec_list);
314 msg_queue_insert(msg);
321 static void asmlinkage sig_handler(int signum)
331 /* set flag, then write to pipe if needed */
336 /* set flag, then write to pipe if needed */
337 sigchilds_waiting = 1;
343 /* if pipe is empty, write to pipe to force select to return
344 * immediately when it gets called
347 rc = write(pipefds[1],&signum,sizeof(signum));
353 static void udev_done(int pid)
355 /* find msg associated with pid and delete it */
356 struct hotplug_msg *msg;
358 list_for_each_entry(msg, &running_list, list) {
359 if (msg->pid == pid) {
360 dbg("<== exec seq %llu came back", msg->seqnum);
361 run_queue_delete(msg);
363 /* we want to run the exec queue manager since there may
364 * be events waiting with the devpath of the one that
373 static void reap_sigchilds(void)
376 int pid = waitpid(-1, NULL, WNOHANG);
377 if ((pid == -1) || (pid == 0))
383 /* just read everything from the pipe and clear the flag,
384 * the flags was set in the signal handler
386 static void user_sighandler(void)
390 int rc = read(pipefds[0], &sig, sizeof(sig));
398 int main(int argc, char *argv[], char *envp[])
400 int ssock, maxsockplus;
401 struct sockaddr_un saddr;
404 const int feature_on = 1;
405 struct sigaction act;
408 logging_init("udevd");
409 dbg("version %s", UDEV_VERSION);
412 dbg("need to be root, exit");
416 /* make sure we don't lock any path */
418 umask(umask(077) | 022);
420 /* Set fds to dev/null */
421 fd = open( "/dev/null", O_RDWR );
423 dbg("error opening /dev/null %s", strerror(errno));
432 /* become session leader */
435 /* setup signal handler pipe */
436 retval = pipe(pipefds);
438 dbg("error getting pipes: %s", strerror(errno));
442 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
444 dbg("error fcntl on read pipe: %s", strerror(errno));
447 retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC);
449 dbg("error fcntl on read pipe: %s", strerror(errno));
453 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
455 dbg("error fcntl on write pipe: %s", strerror(errno));
458 retval = fcntl(pipefds[1], F_SETFD, FD_CLOEXEC);
460 dbg("error fcntl on write pipe: %s", strerror(errno));
464 /* set signal handlers */
465 act.sa_handler = (void (*) (int))sig_handler;
466 sigemptyset(&act.sa_mask);
467 act.sa_flags = SA_RESTART;
468 sigaction(SIGINT, &act, NULL);
469 sigaction(SIGTERM, &act, NULL);
470 sigaction(SIGALRM, &act, NULL);
471 sigaction(SIGCHLD, &act, NULL);
473 memset(&saddr, 0x00, sizeof(saddr));
474 saddr.sun_family = AF_LOCAL;
475 /* use abstract namespace for socket path */
476 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
477 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
479 ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
481 dbg("error getting socket, exit");
485 set_cloexec_flag(ssock, 1);
487 /* the bind takes care of ensuring only one copy running */
488 retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
490 dbg("bind failed, exit");
493 retval = fcntl(ssock, F_SETFD, FD_CLOEXEC);
495 dbg("error fcntl on ssock: %s", strerror(errno));
499 /* enable receiving of the sender credentials */
500 setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
502 /* possible override of udev binary, used for testing */
503 udev_bin = getenv("UDEV_BIN");
504 if (udev_bin != NULL)
505 dbg("udev binary is set to '%s'", udev_bin);
510 FD_SET(ssock, &readfds);
511 FD_SET(pipefds[0], &readfds);
512 maxsockplus = ssock+1;
514 fd_set workreadfds = readfds;
515 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
519 dbg("error in select: %s", strerror(errno));
523 if (FD_ISSET(ssock, &workreadfds))
524 handle_udevsend_msg(ssock);
526 if (FD_ISSET(pipefds[0], &workreadfds))
529 if (sigchilds_waiting) {
530 sigchilds_waiting = 0;
540 /* clean up running_list before calling exec_queue_manager() */
541 if (sigchilds_waiting) {
542 sigchilds_waiting = 0;
547 exec_queue_manager();