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>
41 #include "udev_version.h"
42 #include "udev_utils.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)
127 char *const argv[] = { "udev", msg->subsystem, NULL };
136 execve(udev_bin, argv, msg->envp);
137 dbg("exec of child failed");
141 dbg("fork of child failed");
142 run_queue_delete(msg);
143 /* note: we never managed to run, so we had no impact on
144 * running_with_devpath(), so don't bother setting run_exec_q
148 /* get SIGCHLD in main loop */
149 dbg("==> exec seq %llu [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
154 /* returns still running task for the same event sequence */
155 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
157 struct hotplug_msg *loop_msg;
160 list_for_each_entry(loop_msg, &running_list, list) {
161 if (loop_msg->devpath == NULL || msg->devpath == NULL)
164 /* is a parent or child device event still running */
165 for (i = 0; i < DEVPATH_SIZE; i++) {
166 if (loop_msg->devpath[i] == '\0' || msg->devpath[i] == '\0')
169 if (loop_msg->devpath[i] != msg->devpath[i])
173 /* is the physical device event still running on an add sequence */
174 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
175 for (i = 0; i < DEVPATH_SIZE; i++) {
176 if (loop_msg->devpath[i] == '\0' || msg->physdevpath[i] == '\0')
179 if (loop_msg->devpath[i] != msg->physdevpath[i])
187 /* exec queue management routine executes the events and serializes events in the same sequence */
188 static void exec_queue_manager(void)
190 struct hotplug_msg *loop_msg;
191 struct hotplug_msg *tmp_msg;
192 struct hotplug_msg *msg;
194 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
195 msg = running_with_devpath(loop_msg);
197 /* move event to run list */
198 list_move_tail(&loop_msg->list, &running_list);
200 dbg("moved seq %llu to running list", loop_msg->seqnum);
202 dbg("delay seq %llu (%s), cause seq %llu (%s) is still running",
203 loop_msg->seqnum, loop_msg->devpath, msg->seqnum, msg->devpath);
208 static void msg_move_exec(struct hotplug_msg *msg)
210 list_move_tail(&msg->list, &exec_list);
212 expected_seqnum = msg->seqnum+1;
213 dbg("moved seq %llu to exec, next expected is %llu",
214 msg->seqnum, expected_seqnum);
217 /* msg queue management routine handles the timeouts and dispatches the events */
218 static void msg_queue_manager(void)
220 struct hotplug_msg *loop_msg;
221 struct hotplug_msg *tmp_msg;
225 dbg("msg queue manager, next expected is %llu", expected_seqnum);
227 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
228 /* move event with expected sequence to the exec list */
229 if (loop_msg->seqnum == expected_seqnum) {
230 msg_move_exec(loop_msg);
234 /* move event with expired timeout to the exec list */
236 msg_age = info.uptime - loop_msg->queue_time;
237 dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age);
238 if (msg_age > EVENT_TIMEOUT_SEC-1) {
239 msg_move_exec(loop_msg);
248 /* set timeout for remaining queued events */
249 if (list_empty(&msg_list) == 0) {
250 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
251 dbg("next event expires in %li seconds", EVENT_TIMEOUT_SEC - msg_age);
252 setitimer(ITIMER_REAL, &itv, NULL);
256 /* receive the msg, do some basic sanity checks, and queue it */
257 static void handle_udevsend_msg(int sock)
259 static struct udevsend_msg usend_msg;
260 struct hotplug_msg *msg;
265 struct cmsghdr *cmsg;
268 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
271 memset(&usend_msg, 0x00, sizeof(struct udevsend_msg));
272 iov.iov_base = &usend_msg;
273 iov.iov_len = sizeof(struct udevsend_msg);
275 memset(&smsg, 0x00, sizeof(struct msghdr));
278 smsg.msg_control = cred_msg;
279 smsg.msg_controllen = sizeof(cred_msg);
281 size = recvmsg(sock, &smsg, 0);
284 dbg("unable to receive message");
287 cmsg = CMSG_FIRSTHDR(&smsg);
288 cred = (struct ucred *) CMSG_DATA(cmsg);
290 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
291 dbg("no sender credentials received, message ignored");
295 if (cred->uid != 0) {
296 dbg("sender uid=%i, message ignored", cred->uid);
300 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
301 dbg("message magic '%s' doesn't match, ignore it", usend_msg.magic);
305 envbuf_size = size - offsetof(struct udevsend_msg, envbuf);
306 dbg("envbuf_size=%i", envbuf_size);
307 msg = malloc(sizeof(struct hotplug_msg) + envbuf_size);
308 memset(msg, 0x00, sizeof(struct hotplug_msg) + envbuf_size);
310 /* copy environment buffer and reconstruct envp */
311 memcpy(msg->envbuf, usend_msg.envbuf, envbuf_size);
313 for (i = 0; (bufpos < envbuf_size) && (i < HOTPLUG_NUM_ENVP-2); i++) {
317 key = &msg->envbuf[bufpos];
318 keylen = strlen(key);
320 bufpos += keylen + 1;
321 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
323 /* remember some keys for further processing */
324 if (strncmp(key, "ACTION=", 7) == 0)
325 msg->action = &key[7];
327 if (strncmp(key, "DEVPATH=", 8) == 0)
328 msg->devpath = &key[8];
330 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
331 msg->subsystem = &key[10];
333 if (strncmp(key, "SEQNUM=", 7) == 0)
334 msg->seqnum = strtoull(&key[7], NULL, 10);
336 if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
337 msg->physdevpath = &key[12];
339 msg->envp[i++] = "MANAGED_EVENT=1";
342 /* if no seqnum is given, we move straight to exec queue */
343 if (msg->seqnum == 0) {
344 list_add(&msg->list, &exec_list);
347 msg_queue_insert(msg);
354 static void asmlinkage sig_handler(int signum)
364 /* set flag, then write to pipe if needed */
369 /* set flag, then write to pipe if needed */
370 sigchilds_waiting = 1;
376 /* if pipe is empty, write to pipe to force select to return
377 * immediately when it gets called
380 rc = write(pipefds[1],&signum,sizeof(signum));
386 static void udev_done(int pid)
388 /* find msg associated with pid and delete it */
389 struct hotplug_msg *msg;
391 list_for_each_entry(msg, &running_list, list) {
392 if (msg->pid == pid) {
393 dbg("<== exec seq %llu came back", msg->seqnum);
394 run_queue_delete(msg);
396 /* we want to run the exec queue manager since there may
397 * be events waiting with the devpath of the one that
406 static void reap_sigchilds(void)
409 int pid = waitpid(-1, NULL, WNOHANG);
410 if ((pid == -1) || (pid == 0))
416 /* just read everything from the pipe and clear the flag,
417 * the flags was set in the signal handler
419 static void user_sighandler(void)
423 int rc = read(pipefds[0], &sig, sizeof(sig));
431 int main(int argc, char *argv[], char *envp[])
434 struct sockaddr_un saddr;
437 const int feature_on = 1;
438 struct sigaction act;
441 logging_init("udevd");
442 dbg("version %s", UDEV_VERSION);
445 dbg("need to be root, exit");
449 /* make sure we don't lock any path */
451 umask(umask(077) | 022);
453 /* Set fds to dev/null */
454 fd = open( "/dev/null", O_RDWR );
456 dbg("error opening /dev/null %s", strerror(errno));
465 /* become session leader */
468 /* setup signal handler pipe */
469 retval = pipe(pipefds);
471 dbg("error getting pipes: %s", strerror(errno));
475 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
477 dbg("error fcntl on read pipe: %s", strerror(errno));
480 retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC);
482 dbg("error fcntl on read pipe: %s", strerror(errno));
486 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
488 dbg("error fcntl on write pipe: %s", strerror(errno));
491 retval = fcntl(pipefds[1], F_SETFD, FD_CLOEXEC);
493 dbg("error fcntl on write pipe: %s", strerror(errno));
497 /* set signal handlers */
498 act.sa_handler = (void (*) (int))sig_handler;
499 sigemptyset(&act.sa_mask);
500 act.sa_flags = SA_RESTART;
501 sigaction(SIGINT, &act, NULL);
502 sigaction(SIGTERM, &act, NULL);
503 sigaction(SIGALRM, &act, NULL);
504 sigaction(SIGCHLD, &act, NULL);
506 memset(&saddr, 0x00, sizeof(saddr));
507 saddr.sun_family = AF_LOCAL;
508 /* use abstract namespace for socket path */
509 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
510 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
512 udevsendsock = socket(AF_LOCAL, SOCK_DGRAM, 0);
513 if (udevsendsock == -1) {
514 dbg("error getting socket, exit");
518 /* the bind takes care of ensuring only one copy running */
519 retval = bind(udevsendsock, (struct sockaddr *) &saddr, addrlen);
521 dbg("bind failed, exit");
526 /* enable receiving of the sender credentials */
527 setsockopt(udevsendsock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
529 /* possible override of udev binary, used for testing */
530 udev_bin = getenv("UDEV_BIN");
531 if (udev_bin != NULL)
532 dbg("udev binary is set to '%s'", udev_bin);
537 FD_SET(udevsendsock, &readfds);
538 FD_SET(pipefds[0], &readfds);
539 maxsockplus = udevsendsock+1;
541 fd_set workreadfds = readfds;
542 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
546 dbg("error in select: %s", strerror(errno));
550 if (FD_ISSET(udevsendsock, &workreadfds))
551 handle_udevsend_msg(udevsendsock);
553 if (FD_ISSET(pipefds[0], &workreadfds))
556 if (sigchilds_waiting) {
557 sigchilds_waiting = 0;
567 /* clean up running_list before calling exec_queue_manager() */
568 if (sigchilds_waiting) {
569 sigchilds_waiting = 0;
574 exec_queue_manager();