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"
37 #include <sys/sysinfo.h>
42 #include "udev_version.h"
46 static int pipefds[2];
47 static int expected_seqnum = 0;
48 volatile static int children_waiting;
49 volatile static int run_msg_q;
50 volatile static 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_kids(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 %d, '%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 %d in queue", msg->seqnum);
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;
111 /* sort message by sequence number into list. events
112 * will tend to come in order, so scan the list backwards
114 list_for_each_entry_reverse(loop_msg, &msg_list, list)
115 if (loop_msg->seqnum < msg->seqnum)
118 /* store timestamp of queuing */
120 msg->queue_time = info.uptime;
122 list_add(&msg->list, &loop_msg->list);
123 dbg("queued message seq %d", msg->seqnum);
125 /* run msg queue manager */
131 /* forks event and removes event from run queue when finished */
132 static void udev_run(struct hotplug_msg *msg)
135 char action[ACTION_SIZE];
136 char devpath[DEVPATH_SIZE];
137 char *env[] = { action, devpath, NULL };
139 strcpy(action, "ACTION=");
140 strfieldcat(action, msg->action);
141 strcpy(devpath, "DEVPATH=");
142 strfieldcat(devpath, msg->devpath);
148 execle(udev_bin, "udev", msg->subsystem, NULL, env);
149 dbg("exec of child failed");
153 dbg("fork of child failed");
154 run_queue_delete(msg);
155 /* note: we never managed to run, so we had no impact on
156 * running_with_devpath(), so don't bother setting run_exec_q
160 /* get SIGCHLD in main loop */
161 dbg("==> exec seq %d [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
166 /* returns already running task with devpath */
167 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
169 struct hotplug_msg *loop_msg;
170 list_for_each_entry(loop_msg, &running_list, list)
171 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
176 /* exec queue management routine executes the events and delays events for the same devpath */
177 static void exec_queue_manager()
179 struct hotplug_msg *loop_msg;
180 struct hotplug_msg *tmp_msg;
181 struct hotplug_msg *msg;
183 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
184 msg = running_with_devpath(loop_msg);
186 /* move event to run list */
187 list_move_tail(&loop_msg->list, &running_list);
189 dbg("moved seq %d to running list", loop_msg->seqnum);
191 dbg("delay seq %d, cause seq %d already working on '%s'",
192 loop_msg->seqnum, msg->seqnum, msg->devpath);
197 static void msg_move_exec(struct hotplug_msg *msg)
199 list_move_tail(&msg->list, &exec_list);
201 expected_seqnum = msg->seqnum+1;
202 dbg("moved seq %d to exec, next expected is %d",
203 msg->seqnum, expected_seqnum);
206 /* msg queue management routine handles the timeouts and dispatches the events */
207 static void msg_queue_manager()
209 struct hotplug_msg *loop_msg;
210 struct hotplug_msg *tmp_msg;
214 dbg("msg queue manager, next expected is %d", expected_seqnum);
216 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
217 /* move event with expected sequence to the exec list */
218 if (loop_msg->seqnum == expected_seqnum) {
219 msg_move_exec(loop_msg);
223 /* move event with expired timeout to the exec list */
225 msg_age = info.uptime - loop_msg->queue_time;
226 dbg("seq %d is %li seconds old", loop_msg->seqnum, msg_age);
227 if (msg_age > EVENT_TIMEOUT_SEC-1) {
228 msg_move_exec(loop_msg);
237 /* set timeout for remaining queued events */
238 if (list_empty(&msg_list) == 0) {
239 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
240 dbg("next event expires in %li seconds", EVENT_TIMEOUT_SEC - msg_age);
241 setitimer(ITIMER_REAL, &itv, 0);
245 /* receive the msg, do some basic sanity checks, and queue it */
246 static void handle_msg(int sock)
248 struct hotplug_msg *msg;
251 struct cmsghdr *cmsg;
254 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
258 dbg("unable to store message");
263 iov.iov_len = sizeof(struct hotplug_msg);
265 memset(&smsg, 0x00, sizeof(struct msghdr));
268 smsg.msg_control = cred_msg;
269 smsg.msg_controllen = sizeof(cred_msg);
271 retval = recvmsg(sock, &smsg, 0);
274 dbg("unable to receive message");
277 cmsg = CMSG_FIRSTHDR(&smsg);
278 cred = (struct ucred *) CMSG_DATA(cmsg);
280 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
281 dbg("no sender credentials received, message ignored");
285 if (cred->uid != 0) {
286 dbg("sender uid=%i, message ignored", cred->uid);
290 if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
291 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
295 /* if no seqnum is given, we move straight to exec queue */
296 if (msg->seqnum == -1) {
297 list_add(&msg->list, &exec_list);
300 msg_queue_insert(msg);
309 __attribute__((regparm(0))) static void sig_handler(int signum)
319 /* set flag, then write to pipe if needed */
324 /* set flag, then write to pipe if needed */
325 children_waiting = 1;
329 dbg("unhandled signal %d", signum);
334 /* if pipe is empty, write to pipe to force select to return
335 * immediately when it gets called
338 rc = write(pipefds[1],&signum,sizeof(signum));
340 dbg("unable to write to pipe");
346 static void udev_done(int pid)
348 /* find msg associated with pid and delete it */
349 struct hotplug_msg *msg;
351 list_for_each_entry(msg, &running_list, list) {
352 if (msg->pid == pid) {
353 dbg("<== exec seq %d came back", msg->seqnum);
354 run_queue_delete(msg);
356 /* we want to run the exec queue manager since there may
357 * be events waiting with the devpath of the one that
366 static void reap_kids()
368 /* reap all dead children */
370 int pid = waitpid(-1, 0, WNOHANG);
371 if ((pid == -1) || (pid == 0))
377 /* just read everything from the pipe and clear the flag,
378 * the useful flags were set in the signal handler
380 static void user_sighandler()
384 int rc = read(pipefds[0],&sig,sizeof(sig));
393 int main(int argc, char *argv[])
395 int ssock, maxsockplus;
396 struct sockaddr_un saddr;
400 struct sigaction act;
403 init_logging("udevd");
404 dbg("version %s", UDEV_VERSION);
407 dbg("need to be root, exit");
411 /* setup signal handler pipe */
412 retval = pipe(pipefds);
414 dbg("error getting pipes: %s", strerror(errno));
418 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
420 dbg("error fcntl on read pipe: %s", strerror(errno));
424 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
426 dbg("error fcntl on write pipe: %s", strerror(errno));
430 /* set signal handlers */
431 act.sa_handler = sig_handler;
432 sigemptyset(&act.sa_mask);
433 act.sa_flags = SA_RESTART;
434 sigaction(SIGINT, &act, NULL);
435 sigaction(SIGTERM, &act, NULL);
436 sigaction(SIGALRM, &act, NULL);
437 sigaction(SIGCHLD, &act, NULL);
439 memset(&saddr, 0x00, sizeof(saddr));
440 saddr.sun_family = AF_LOCAL;
441 /* use abstract namespace for socket path */
442 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
443 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
445 ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
447 dbg("error getting socket, exit");
451 /* the bind takes care of ensuring only one copy running */
452 retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
454 dbg("bind failed, exit");
458 /* enable receiving of the sender credentials */
459 setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
461 /* possible override of udev binary, used for testing */
462 udev_bin = getenv("UDEV_BIN");
463 if (udev_bin != NULL)
464 dbg("udev binary is set to '%s'", udev_bin);
469 FD_SET(ssock, &readfds);
470 FD_SET(pipefds[0], &readfds);
471 maxsockplus = ssock+1;
473 fd_set workreadfds = readfds;
474 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
478 dbg("error in select: %s", strerror(errno));
482 if (FD_ISSET(ssock, &workreadfds))
485 if (FD_ISSET(pipefds[0], &workreadfds))
488 if (children_waiting) {
489 children_waiting = 0;
499 /* this is tricky. exec_queue_manager() loops over exec_list, and
500 * calls running_with_devpath(), which loops over running_list. This gives
501 * O(N*M), which can get *nasty*. Clean up running_list before
502 * calling exec_queue_manager().
504 if (children_waiting) {
505 children_waiting = 0;
510 exec_queue_manager();