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>
41 #include "udev_version.h"
45 static int pipefds[2];
46 static int expected_seqnum = 0;
47 volatile static int children_waiting;
48 volatile static int run_msg_q;
49 volatile static int sig_flag;
50 static int run_exec_q;
52 static LIST_HEAD(msg_list);
53 static LIST_HEAD(exec_list);
54 static LIST_HEAD(running_list);
56 static void exec_queue_manager(void);
57 static void msg_queue_manager(void);
58 static void user_sighandler(void);
59 static void reap_kids(void);
62 unsigned char logname[LOGNAME_SIZE];
63 void log_message (int level, const char *format, ...)
67 va_start(args, format);
68 vsyslog(level, format, args);
73 static void msg_dump_queue(void)
76 struct hotplug_msg *msg;
78 list_for_each_entry(msg, &msg_list, list)
79 dbg("sequence %d in queue", msg->seqnum);
83 static void msg_dump(struct hotplug_msg *msg)
85 dbg("sequence %d, '%s', '%s', '%s'",
86 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
89 static struct hotplug_msg *msg_create(void)
91 struct hotplug_msg *new_msg;
93 new_msg = malloc(sizeof(struct hotplug_msg));
99 static void run_queue_delete(struct hotplug_msg *msg)
101 list_del(&msg->list);
105 /* orders the message in the queue by sequence number */
106 static void msg_queue_insert(struct hotplug_msg *msg)
108 struct hotplug_msg *loop_msg;
110 /* sort message by sequence number into list. events
111 * will tend to come in order, so scan the list backwards
113 list_for_each_entry_reverse(loop_msg, &msg_list, list)
114 if (loop_msg->seqnum < msg->seqnum)
116 list_add(&msg->list, &loop_msg->list);
117 dbg("queued message seq %d", msg->seqnum);
119 /* store timestamp of queuing */
120 msg->queue_time = time(NULL);
122 /* run msg queue manager */
128 /* forks event and removes event from run queue when finished */
129 static void udev_run(struct hotplug_msg *msg)
132 char action[ACTION_SIZE];
133 char devpath[DEVPATH_SIZE];
134 char *env[] = { action, devpath, NULL };
136 snprintf(action, sizeof(action), "ACTION=%s", msg->action);
137 snprintf(devpath, sizeof(devpath), "DEVPATH=%s", msg->devpath);
143 execle(UDEV_BIN, "udev", msg->subsystem, NULL, env);
144 dbg("exec of child failed");
148 dbg("fork of child failed");
149 run_queue_delete(msg);
150 /* note: we never managed to run, so we had no impact on
151 * running_with_devpath(), so don't bother setting run_exec_q
155 /* get SIGCHLD in main loop */
156 dbg("==> exec seq %d [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
161 /* returns already running task with devpath */
162 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
164 struct hotplug_msg *loop_msg;
165 list_for_each_entry(loop_msg, &running_list, list)
166 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
171 /* exec queue management routine executes the events and delays events for the same devpath */
172 static void exec_queue_manager()
174 struct hotplug_msg *loop_msg;
175 struct hotplug_msg *tmp_msg;
176 struct hotplug_msg *msg;
178 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
179 msg = running_with_devpath(loop_msg);
181 /* move event to run list */
182 list_move_tail(&loop_msg->list, &running_list);
184 dbg("moved seq %d to running list", loop_msg->seqnum);
186 dbg("delay seq %d, cause seq %d already working on '%s'",
187 loop_msg->seqnum, msg->seqnum, msg->devpath);
192 static void msg_move_exec(struct hotplug_msg *msg)
194 list_move_tail(&msg->list, &exec_list);
196 expected_seqnum = msg->seqnum+1;
197 dbg("moved seq %d to exec, next expected is %d",
198 msg->seqnum, expected_seqnum);
201 /* msg queue management routine handles the timeouts and dispatches the events */
202 static void msg_queue_manager()
204 struct hotplug_msg *loop_msg;
205 struct hotplug_msg *tmp_msg;
208 dbg("msg queue manager, next expected is %d", expected_seqnum);
210 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
211 /* move event with expected sequence to the exec list */
212 if (loop_msg->seqnum == expected_seqnum) {
213 msg_move_exec(loop_msg);
217 /* move event with expired timeout to the exec list */
218 msg_age = time(NULL) - loop_msg->queue_time;
219 if (msg_age > EVENT_TIMEOUT_SEC-1) {
220 msg_move_exec(loop_msg);
229 if (list_empty(&msg_list) == 0) {
230 /* set timeout for remaining queued events */
231 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
232 dbg("next event expires in %li seconds",
233 EVENT_TIMEOUT_SEC - msg_age);
234 setitimer(ITIMER_REAL, &itv, 0);
238 /* receive the msg, do some basic sanity checks, and queue it */
239 static void handle_msg(int sock)
241 struct hotplug_msg *msg;
244 struct cmsghdr *cmsg;
247 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
251 dbg("unable to store message");
256 iov.iov_len = sizeof(struct hotplug_msg);
258 memset(&smsg, 0x00, sizeof(struct msghdr));
261 smsg.msg_control = cred_msg;
262 smsg.msg_controllen = sizeof(cred_msg);
264 retval = recvmsg(sock, &smsg, 0);
267 dbg("unable to receive message");
270 cmsg = CMSG_FIRSTHDR(&smsg);
271 cred = (struct ucred *) CMSG_DATA(cmsg);
273 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
274 dbg("no sender credentials received, message ignored");
278 if (cred->uid != 0) {
279 dbg("sender uid=%i, message ignored", cred->uid);
283 if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
284 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
288 /* if no seqnum is given, we move straight to exec queue */
289 if (msg->seqnum == -1) {
290 list_add(&msg->list, &exec_list);
293 msg_queue_insert(msg);
302 static void sig_handler(int signum)
311 /* set flag, then write to pipe if needed */
316 /* set flag, then write to pipe if needed */
317 children_waiting = 1;
321 dbg("unhandled signal");
326 /* if pipe is empty, write to pipe to force select to return
327 * immediately when it gets called
330 rc = write(pipefds[1],&signum,sizeof(signum));
332 dbg("unable to write to pipe");
338 static void udev_done(int pid)
340 /* find msg associated with pid and delete it */
341 struct hotplug_msg *msg;
343 list_for_each_entry(msg, &running_list, list) {
344 if (msg->pid == pid) {
345 dbg("<== exec seq %d came back", msg->seqnum);
346 run_queue_delete(msg);
348 /* we want to run the exec queue manager since there may
349 * be events waiting with the devpath of the one that
358 static void reap_kids()
360 /* reap all dead children */
362 int pid = waitpid(-1, 0, WNOHANG);
363 if ((pid == -1) || (pid == 0))
369 /* just read everything from the pipe and clear the flag,
370 * the useful flags were set in the signal handler
372 static void user_sighandler()
376 int rc = read(pipefds[0],&sig,sizeof(sig));
385 int main(int argc, char *argv[])
387 int ssock, maxsockplus;
388 struct sockaddr_un saddr;
392 struct sigaction act;
395 init_logging("udevd");
396 dbg("version %s", UDEV_VERSION);
399 dbg("need to be root, exit");
403 /* setup signal handler pipe */
404 retval = pipe(pipefds);
406 dbg("error getting pipes: %s", strerror(errno));
410 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
412 dbg("fcntl on read pipe: %s", strerror(errno));
416 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
418 dbg("fcntl on write pipe: %s", strerror(errno));
422 /* set signal handlers */
423 act.sa_handler = sig_handler;
424 sigemptyset(&act.sa_mask);
425 act.sa_flags = SA_RESTART;
426 sigaction(SIGINT, &act, NULL);
427 sigaction(SIGTERM, &act, NULL);
428 sigaction(SIGALRM, &act, NULL);
429 sigaction(SIGCHLD, &act, NULL);
431 memset(&saddr, 0x00, sizeof(saddr));
432 saddr.sun_family = AF_LOCAL;
433 /* use abstract namespace for socket path */
434 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
435 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
437 ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
439 dbg("error getting socket, exit");
443 /* the bind takes care of ensuring only one copy running */
444 retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
446 dbg("bind failed, exit");
450 /* enable receiving of the sender credentials */
451 setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
454 FD_SET(ssock, &readfds);
455 FD_SET(pipefds[0], &readfds);
456 maxsockplus = ssock+1;
458 fd_set workreadfds = readfds;
459 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
462 dbg("error in select: %s", strerror(errno));
466 if (FD_ISSET(ssock, &workreadfds))
469 if (FD_ISSET(pipefds[0], &workreadfds))
472 if (children_waiting) {
473 children_waiting = 0;
484 /* this is tricky. exec_queue_manager() loops over exec_list, and
485 * calls running_with_devpath(), which loops over running_list. This gives
486 * O(N*M), which can get *nasty*. Clean up running_list before
487 * calling exec_queue_manager().
490 if (children_waiting) {
491 children_waiting = 0;
496 exec_queue_manager();