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 "klibc_fixups.h"
38 #include <sys/sysinfo.h>
44 #include "udev_version.h"
48 static int pipefds[2];
49 static int expected_seqnum = 0;
50 volatile static int children_waiting;
51 volatile static int run_msg_q;
52 volatile static int sig_flag;
53 static int run_exec_q;
55 static LIST_HEAD(msg_list);
56 static LIST_HEAD(exec_list);
57 static LIST_HEAD(running_list);
59 static void exec_queue_manager(void);
60 static void msg_queue_manager(void);
61 static void user_sighandler(void);
62 static void reap_kids(void);
65 unsigned char logname[LOGNAME_SIZE];
66 void log_message (int level, const char *format, ...)
70 va_start(args, format);
71 vsyslog(level, format, args);
76 static void msg_dump_queue(void)
79 struct hotplug_msg *msg;
81 list_for_each_entry(msg, &msg_list, list)
82 dbg("sequence %d in queue", msg->seqnum);
87 static void msg_dump(struct hotplug_msg *msg)
89 dbg("sequence %d, '%s', '%s', '%s'",
90 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
94 static struct hotplug_msg *msg_create(void)
96 struct hotplug_msg *new_msg;
98 new_msg = malloc(sizeof(struct hotplug_msg));
104 static void run_queue_delete(struct hotplug_msg *msg)
106 list_del(&msg->list);
110 /* orders the message in the queue by sequence number */
111 static void msg_queue_insert(struct hotplug_msg *msg)
113 struct hotplug_msg *loop_msg;
116 /* sort message by sequence number into list. events
117 * will tend to come in order, so scan the list backwards
119 list_for_each_entry_reverse(loop_msg, &msg_list, list)
120 if (loop_msg->seqnum < msg->seqnum)
123 /* store timestamp of queuing */
125 msg->queue_time = info.uptime;
127 list_add(&msg->list, &loop_msg->list);
128 dbg("queued message seq %d", msg->seqnum);
130 /* run msg queue manager */
136 /* forks event and removes event from run queue when finished */
137 static void udev_run(struct hotplug_msg *msg)
140 char action[ACTION_SIZE];
141 char devpath[DEVPATH_SIZE];
142 char *env[] = { action, devpath, NULL };
144 strcpy(action, "ACTION=");
145 strfieldcat(action, msg->action);
146 strcpy(devpath, "DEVPATH=");
147 strfieldcat(devpath, msg->devpath);
153 execle(UDEV_BIN, "udev", msg->subsystem, NULL, env);
154 dbg("exec of child failed");
158 dbg("fork of child failed");
159 run_queue_delete(msg);
160 /* note: we never managed to run, so we had no impact on
161 * running_with_devpath(), so don't bother setting run_exec_q
165 /* get SIGCHLD in main loop */
166 dbg("==> exec seq %d [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
171 /* returns already running task with devpath */
172 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
174 struct hotplug_msg *loop_msg;
175 list_for_each_entry(loop_msg, &running_list, list)
176 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
181 /* exec queue management routine executes the events and delays events for the same devpath */
182 static void exec_queue_manager()
184 struct hotplug_msg *loop_msg;
185 struct hotplug_msg *tmp_msg;
186 struct hotplug_msg *msg;
188 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
189 msg = running_with_devpath(loop_msg);
191 /* move event to run list */
192 list_move_tail(&loop_msg->list, &running_list);
194 dbg("moved seq %d to running list", loop_msg->seqnum);
196 dbg("delay seq %d, cause seq %d already working on '%s'",
197 loop_msg->seqnum, msg->seqnum, msg->devpath);
202 static void msg_move_exec(struct hotplug_msg *msg)
204 list_move_tail(&msg->list, &exec_list);
206 expected_seqnum = msg->seqnum+1;
207 dbg("moved seq %d to exec, next expected is %d",
208 msg->seqnum, expected_seqnum);
211 /* msg queue management routine handles the timeouts and dispatches the events */
212 static void msg_queue_manager()
214 struct hotplug_msg *loop_msg;
215 struct hotplug_msg *tmp_msg;
219 dbg("msg queue manager, next expected is %d", expected_seqnum);
221 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
222 /* move event with expected sequence to the exec list */
223 if (loop_msg->seqnum == expected_seqnum) {
224 msg_move_exec(loop_msg);
228 /* move event with expired timeout to the exec list */
230 msg_age = info.uptime - loop_msg->queue_time;
231 dbg("seq %d is %li seconds old", loop_msg->seqnum, msg_age);
232 if (msg_age > EVENT_TIMEOUT_SEC-1) {
233 msg_move_exec(loop_msg);
242 /* set timeout for remaining queued events */
243 if (list_empty(&msg_list) == 0) {
244 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
245 dbg("next event expires in %li seconds", EVENT_TIMEOUT_SEC - msg_age);
246 setitimer(ITIMER_REAL, &itv, 0);
250 /* receive the msg, do some basic sanity checks, and queue it */
251 static void handle_msg(int sock)
253 struct hotplug_msg *msg;
256 struct cmsghdr *cmsg;
259 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
263 dbg("unable to store message");
268 iov.iov_len = sizeof(struct hotplug_msg);
270 memset(&smsg, 0x00, sizeof(struct msghdr));
273 smsg.msg_control = cred_msg;
274 smsg.msg_controllen = sizeof(cred_msg);
276 retval = recvmsg(sock, &smsg, 0);
279 dbg("unable to receive message");
282 cmsg = CMSG_FIRSTHDR(&smsg);
283 cred = (struct ucred *) CMSG_DATA(cmsg);
285 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
286 dbg("no sender credentials received, message ignored");
290 if (cred->uid != 0) {
291 dbg("sender uid=%i, message ignored", cred->uid);
295 if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
296 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
300 /* if no seqnum is given, we move straight to exec queue */
301 if (msg->seqnum == -1) {
302 list_add(&msg->list, &exec_list);
305 msg_queue_insert(msg);
314 static void sig_handler(int signum)
323 /* set flag, then write to pipe if needed */
328 /* set flag, then write to pipe if needed */
329 children_waiting = 1;
333 dbg("unhandled signal");
338 /* if pipe is empty, write to pipe to force select to return
339 * immediately when it gets called
342 rc = write(pipefds[1],&signum,sizeof(signum));
344 dbg("unable to write to pipe");
350 static void udev_done(int pid)
352 /* find msg associated with pid and delete it */
353 struct hotplug_msg *msg;
355 list_for_each_entry(msg, &running_list, list) {
356 if (msg->pid == pid) {
357 dbg("<== exec seq %d came back", msg->seqnum);
358 run_queue_delete(msg);
360 /* we want to run the exec queue manager since there may
361 * be events waiting with the devpath of the one that
370 static void reap_kids()
372 /* reap all dead children */
374 int pid = waitpid(-1, 0, WNOHANG);
375 if ((pid == -1) || (pid == 0))
381 /* just read everything from the pipe and clear the flag,
382 * the useful flags were set in the signal handler
384 static void user_sighandler()
388 int rc = read(pipefds[0],&sig,sizeof(sig));
397 int main(int argc, char *argv[])
399 int ssock, maxsockplus;
400 struct sockaddr_un saddr;
404 struct sigaction act;
407 init_logging("udevd");
408 dbg("version %s", UDEV_VERSION);
411 dbg("need to be root, exit");
415 /* setup signal handler pipe */
416 retval = pipe(pipefds);
418 dbg("error getting pipes: %s", strerror(errno));
422 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
424 dbg("error fcntl on read pipe: %s", strerror(errno));
428 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
430 dbg("error fcntl on write pipe: %s", strerror(errno));
434 /* set signal handlers */
435 act.sa_handler = sig_handler;
436 sigemptyset(&act.sa_mask);
437 act.sa_flags = SA_RESTART;
438 sigaction(SIGINT, &act, NULL);
439 sigaction(SIGTERM, &act, NULL);
440 sigaction(SIGALRM, &act, NULL);
441 sigaction(SIGCHLD, &act, NULL);
443 memset(&saddr, 0x00, sizeof(saddr));
444 saddr.sun_family = AF_LOCAL;
445 /* use abstract namespace for socket path */
446 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
447 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
449 ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
451 dbg("error getting socket, exit");
455 /* the bind takes care of ensuring only one copy running */
456 retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
458 dbg("bind failed, exit");
462 /* enable receiving of the sender credentials */
463 setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
466 FD_SET(ssock, &readfds);
467 FD_SET(pipefds[0], &readfds);
468 maxsockplus = ssock+1;
470 fd_set workreadfds = readfds;
471 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
475 dbg("error in select: %s", strerror(errno));
479 if (FD_ISSET(ssock, &workreadfds))
482 if (FD_ISSET(pipefds[0], &workreadfds))
485 if (children_waiting) {
486 children_waiting = 0;
496 /* this is tricky. exec_queue_manager() loops over exec_list, and
497 * calls running_with_devpath(), which loops over running_list. This gives
498 * O(N*M), which can get *nasty*. Clean up running_list before
499 * calling exec_queue_manager().
501 if (children_waiting) {
502 children_waiting = 0;
507 exec_queue_manager();