]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udevd.c
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>
43 #include "udev_version.h"
47 static int pipefds
[2];
48 static unsigned long long expected_seqnum
= 0;
49 static volatile int children_waiting
;
50 static volatile int run_msg_q
;
51 static volatile int sig_flag
;
52 static int run_exec_q
;
54 static LIST_HEAD(msg_list
);
55 static LIST_HEAD(exec_list
);
56 static LIST_HEAD(running_list
);
58 static void exec_queue_manager(void);
59 static void msg_queue_manager(void);
60 static void user_sighandler(void);
61 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 #define msg_dump(msg) \
77 dbg("msg_dump: sequence %llu, '%s', '%s', '%s'", \
78 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
80 static void msg_dump_queue(void)
83 struct hotplug_msg
*msg
;
85 list_for_each_entry(msg
, &msg_list
, list
)
86 dbg("sequence %llu in queue", msg
->seqnum
);
90 static struct hotplug_msg
*msg_create(void)
92 struct hotplug_msg
*new_msg
;
94 new_msg
= malloc(sizeof(struct hotplug_msg
));
100 static void run_queue_delete(struct hotplug_msg
*msg
)
102 list_del(&msg
->list
);
106 /* orders the message in the queue by sequence number */
107 static void msg_queue_insert(struct hotplug_msg
*msg
)
109 struct hotplug_msg
*loop_msg
;
112 /* sort message by sequence number into list. events
113 * will tend to come in order, so scan the list backwards
115 list_for_each_entry_reverse(loop_msg
, &msg_list
, list
)
116 if (loop_msg
->seqnum
< msg
->seqnum
)
119 /* store timestamp of queuing */
121 msg
->queue_time
= info
.uptime
;
123 list_add(&msg
->list
, &loop_msg
->list
);
124 dbg("queued message seq %llu", msg
->seqnum
);
126 /* run msg queue manager */
132 /* forks event and removes event from run queue when finished */
133 static void udev_run(struct hotplug_msg
*msg
)
136 char action
[ACTION_SIZE
];
137 char devpath
[DEVPATH_SIZE
];
138 char seqnum
[SEQNUM_SIZE
];
139 char *env
[] = { action
, devpath
, seqnum
, NULL
};
141 snprintf(action
, ACTION_SIZE
-1, "ACTION=%s", msg
->action
);
142 action
[ACTION_SIZE
-1] = '\0';
143 snprintf(devpath
, DEVPATH_SIZE
-1, "DEVPATH=%s", msg
->devpath
);
144 devpath
[DEVPATH_SIZE
-1] = '\0';
145 sprintf(seqnum
, "SEQNUM=%llu", msg
->seqnum
);
151 execle(udev_bin
, "udev", msg
->subsystem
, NULL
, env
);
152 dbg("exec of child failed");
156 dbg("fork of child failed");
157 run_queue_delete(msg
);
158 /* note: we never managed to run, so we had no impact on
159 * running_with_devpath(), so don't bother setting run_exec_q
163 /* get SIGCHLD in main loop */
164 dbg("==> exec seq %llu [%d] working at '%s'", msg
->seqnum
, pid
, msg
->devpath
);
169 /* returns already running task with devpath */
170 static struct hotplug_msg
*running_with_devpath(struct hotplug_msg
*msg
)
172 struct hotplug_msg
*loop_msg
;
173 list_for_each_entry(loop_msg
, &running_list
, list
)
174 if (strncmp(loop_msg
->devpath
, msg
->devpath
, sizeof(loop_msg
->devpath
)) == 0)
179 /* exec queue management routine executes the events and delays events for the same devpath */
180 static void exec_queue_manager()
182 struct hotplug_msg
*loop_msg
;
183 struct hotplug_msg
*tmp_msg
;
184 struct hotplug_msg
*msg
;
186 list_for_each_entry_safe(loop_msg
, tmp_msg
, &exec_list
, list
) {
187 msg
= running_with_devpath(loop_msg
);
189 /* move event to run list */
190 list_move_tail(&loop_msg
->list
, &running_list
);
192 dbg("moved seq %llu to running list", loop_msg
->seqnum
);
194 dbg("delay seq %llu, cause seq %llu already working on '%s'",
195 loop_msg
->seqnum
, msg
->seqnum
, msg
->devpath
);
200 static void msg_move_exec(struct hotplug_msg
*msg
)
202 list_move_tail(&msg
->list
, &exec_list
);
204 expected_seqnum
= msg
->seqnum
+1;
205 dbg("moved seq %llu to exec, next expected is %llu",
206 msg
->seqnum
, expected_seqnum
);
209 /* msg queue management routine handles the timeouts and dispatches the events */
210 static void msg_queue_manager()
212 struct hotplug_msg
*loop_msg
;
213 struct hotplug_msg
*tmp_msg
;
217 dbg("msg queue manager, next expected is %llu", expected_seqnum
);
219 list_for_each_entry_safe(loop_msg
, tmp_msg
, &msg_list
, list
) {
220 /* move event with expected sequence to the exec list */
221 if (loop_msg
->seqnum
== expected_seqnum
) {
222 msg_move_exec(loop_msg
);
226 /* move event with expired timeout to the exec list */
228 msg_age
= info
.uptime
- loop_msg
->queue_time
;
229 dbg("seq %llu is %li seconds old", loop_msg
->seqnum
, msg_age
);
230 if (msg_age
> EVENT_TIMEOUT_SEC
-1) {
231 msg_move_exec(loop_msg
);
240 /* set timeout for remaining queued events */
241 if (list_empty(&msg_list
) == 0) {
242 struct itimerval itv
= {{0, 0}, {EVENT_TIMEOUT_SEC
- msg_age
, 0}};
243 dbg("next event expires in %li seconds", EVENT_TIMEOUT_SEC
- msg_age
);
244 setitimer(ITIMER_REAL
, &itv
, 0);
248 /* receive the msg, do some basic sanity checks, and queue it */
249 static void handle_msg(int sock
)
251 struct hotplug_msg
*msg
;
254 struct cmsghdr
*cmsg
;
257 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
261 dbg("unable to store message");
266 iov
.iov_len
= sizeof(struct hotplug_msg
);
268 memset(&smsg
, 0x00, sizeof(struct msghdr
));
271 smsg
.msg_control
= cred_msg
;
272 smsg
.msg_controllen
= sizeof(cred_msg
);
274 retval
= recvmsg(sock
, &smsg
, 0);
277 dbg("unable to receive message");
280 cmsg
= CMSG_FIRSTHDR(&smsg
);
281 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
283 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
284 dbg("no sender credentials received, message ignored");
288 if (cred
->uid
!= 0) {
289 dbg("sender uid=%i, message ignored", cred
->uid
);
293 if (strncmp(msg
->magic
, UDEV_MAGIC
, sizeof(UDEV_MAGIC
)) != 0 ) {
294 dbg("message magic '%s' doesn't match, ignore it", msg
->magic
);
298 /* if no seqnum is given, we move straight to exec queue */
299 if (msg
->seqnum
== 0) {
300 list_add(&msg
->list
, &exec_list
);
303 msg_queue_insert(msg
);
312 static void asmlinkage
sig_handler(int signum
)
322 /* set flag, then write to pipe if needed */
327 /* set flag, then write to pipe if needed */
328 children_waiting
= 1;
332 dbg("unhandled signal %d", signum
);
337 /* if pipe is empty, write to pipe to force select to return
338 * immediately when it gets called
341 rc
= write(pipefds
[1],&signum
,sizeof(signum
));
343 dbg("unable to write to pipe");
349 static void udev_done(int pid
)
351 /* find msg associated with pid and delete it */
352 struct hotplug_msg
*msg
;
354 list_for_each_entry(msg
, &running_list
, list
) {
355 if (msg
->pid
== pid
) {
356 dbg("<== exec seq %llu came back", msg
->seqnum
);
357 run_queue_delete(msg
);
359 /* we want to run the exec queue manager since there may
360 * be events waiting with the devpath of the one that
369 static void reap_kids()
371 /* reap all dead children */
373 int pid
= waitpid(-1, 0, WNOHANG
);
374 if ((pid
== -1) || (pid
== 0))
380 /* just read everything from the pipe and clear the flag,
381 * the useful flags were set in the signal handler
383 static void user_sighandler()
387 int rc
= read(pipefds
[0],&sig
,sizeof(sig
));
396 int main(int argc
, char *argv
[])
398 int ssock
, maxsockplus
;
399 struct sockaddr_un saddr
;
403 struct sigaction act
;
406 logging_init("udevd");
407 dbg("version %s", UDEV_VERSION
);
410 dbg("need to be root, exit");
413 /* make sure we are at top of dir */
415 umask( umask( 077 ) | 022 );
416 /* Set fds to dev/null */
417 fd
= open( "/dev/null", O_RDWR
);
419 dbg("error opening /dev/null %s", strerror(errno
));
427 /* Get new session id so stray signals don't come our way. */
430 /* setup signal handler pipe */
431 retval
= pipe(pipefds
);
433 dbg("error getting pipes: %s", strerror(errno
));
437 retval
= fcntl(pipefds
[0], F_SETFL
, O_NONBLOCK
);
439 dbg("error fcntl on read pipe: %s", strerror(errno
));
442 retval
= fcntl(pipefds
[0], F_SETFD
, FD_CLOEXEC
);
444 dbg("error fcntl on read pipe: %s", strerror(errno
));
448 retval
= fcntl(pipefds
[1], F_SETFL
, O_NONBLOCK
);
450 dbg("error fcntl on write pipe: %s", strerror(errno
));
453 retval
= fcntl(pipefds
[1], F_SETFD
, FD_CLOEXEC
);
455 dbg("error fcntl on write pipe: %s", strerror(errno
));
460 /* set signal handlers */
461 act
.sa_handler
= (void (*) (int))sig_handler
;
462 sigemptyset(&act
.sa_mask
);
463 act
.sa_flags
= SA_RESTART
;
464 sigaction(SIGINT
, &act
, NULL
);
465 sigaction(SIGTERM
, &act
, NULL
);
466 sigaction(SIGALRM
, &act
, NULL
);
467 sigaction(SIGCHLD
, &act
, NULL
);
469 memset(&saddr
, 0x00, sizeof(saddr
));
470 saddr
.sun_family
= AF_LOCAL
;
471 /* use abstract namespace for socket path */
472 strcpy(&saddr
.sun_path
[1], UDEVD_SOCK_PATH
);
473 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(saddr
.sun_path
+1) + 1;
475 ssock
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
477 dbg("error getting socket, exit");
481 set_cloexec_flag(ssock
, 1);
483 /* the bind takes care of ensuring only one copy running */
484 retval
= bind(ssock
, (struct sockaddr
*) &saddr
, addrlen
);
486 dbg("bind failed, exit");
489 retval
= fcntl(ssock
, F_SETFD
, FD_CLOEXEC
);
491 dbg("error fcntl on ssock: %s", strerror(errno
));
495 /* enable receiving of the sender credentials */
496 setsockopt(ssock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
498 /* possible override of udev binary, used for testing */
499 udev_bin
= getenv("UDEV_BIN");
500 if (udev_bin
!= NULL
)
501 dbg("udev binary is set to '%s'", udev_bin
);
506 FD_SET(ssock
, &readfds
);
507 FD_SET(pipefds
[0], &readfds
);
508 maxsockplus
= ssock
+1;
510 fd_set workreadfds
= readfds
;
511 retval
= select(maxsockplus
, &workreadfds
, NULL
, NULL
, NULL
);
515 dbg("error in select: %s", strerror(errno
));
519 if (FD_ISSET(ssock
, &workreadfds
))
522 if (FD_ISSET(pipefds
[0], &workreadfds
))
525 if (children_waiting
) {
526 children_waiting
= 0;
536 /* this is tricky. exec_queue_manager() loops over exec_list, and
537 * calls running_with_devpath(), which loops over running_list. This gives
538 * O(N*M), which can get *nasty*. Clean up running_list before
539 * calling exec_queue_manager().
541 if (children_waiting
) {
542 children_waiting
= 0;
547 exec_queue_manager();