]>
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"
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 #define msg_dump(msg) \
77 dbg("msg_dump: sequence %d, '%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 %d 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 %d", 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 *env
[] = { action
, devpath
, NULL
};
140 strcpy(action
, "ACTION=");
141 strfieldcat(action
, msg
->action
);
142 strcpy(devpath
, "DEVPATH=");
143 strfieldcat(devpath
, msg
->devpath
);
149 execle(UDEV_BIN
, "udev", msg
->subsystem
, NULL
, env
);
150 dbg("exec of child failed");
154 dbg("fork of child failed");
155 run_queue_delete(msg
);
156 /* note: we never managed to run, so we had no impact on
157 * running_with_devpath(), so don't bother setting run_exec_q
161 /* get SIGCHLD in main loop */
162 dbg("==> exec seq %d [%d] working at '%s'", msg
->seqnum
, pid
, msg
->devpath
);
167 /* returns already running task with devpath */
168 static struct hotplug_msg
*running_with_devpath(struct hotplug_msg
*msg
)
170 struct hotplug_msg
*loop_msg
;
171 list_for_each_entry(loop_msg
, &running_list
, list
)
172 if (strncmp(loop_msg
->devpath
, msg
->devpath
, sizeof(loop_msg
->devpath
)) == 0)
177 /* exec queue management routine executes the events and delays events for the same devpath */
178 static void exec_queue_manager()
180 struct hotplug_msg
*loop_msg
;
181 struct hotplug_msg
*tmp_msg
;
182 struct hotplug_msg
*msg
;
184 list_for_each_entry_safe(loop_msg
, tmp_msg
, &exec_list
, list
) {
185 msg
= running_with_devpath(loop_msg
);
187 /* move event to run list */
188 list_move_tail(&loop_msg
->list
, &running_list
);
190 dbg("moved seq %d to running list", loop_msg
->seqnum
);
192 dbg("delay seq %d, cause seq %d already working on '%s'",
193 loop_msg
->seqnum
, msg
->seqnum
, msg
->devpath
);
198 static void msg_move_exec(struct hotplug_msg
*msg
)
200 list_move_tail(&msg
->list
, &exec_list
);
202 expected_seqnum
= msg
->seqnum
+1;
203 dbg("moved seq %d to exec, next expected is %d",
204 msg
->seqnum
, expected_seqnum
);
207 /* msg queue management routine handles the timeouts and dispatches the events */
208 static void msg_queue_manager()
210 struct hotplug_msg
*loop_msg
;
211 struct hotplug_msg
*tmp_msg
;
215 dbg("msg queue manager, next expected is %d", expected_seqnum
);
217 list_for_each_entry_safe(loop_msg
, tmp_msg
, &msg_list
, list
) {
218 /* move event with expected sequence to the exec list */
219 if (loop_msg
->seqnum
== expected_seqnum
) {
220 msg_move_exec(loop_msg
);
224 /* move event with expired timeout to the exec list */
226 msg_age
= info
.uptime
- loop_msg
->queue_time
;
227 dbg("seq %d is %li seconds old", loop_msg
->seqnum
, msg_age
);
228 if (msg_age
> EVENT_TIMEOUT_SEC
-1) {
229 msg_move_exec(loop_msg
);
238 /* set timeout for remaining queued events */
239 if (list_empty(&msg_list
) == 0) {
240 struct itimerval itv
= {{0, 0}, {EVENT_TIMEOUT_SEC
- msg_age
, 0}};
241 dbg("next event expires in %li seconds", EVENT_TIMEOUT_SEC
- msg_age
);
242 setitimer(ITIMER_REAL
, &itv
, 0);
246 /* receive the msg, do some basic sanity checks, and queue it */
247 static void handle_msg(int sock
)
249 struct hotplug_msg
*msg
;
252 struct cmsghdr
*cmsg
;
255 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
259 dbg("unable to store message");
264 iov
.iov_len
= sizeof(struct hotplug_msg
);
266 memset(&smsg
, 0x00, sizeof(struct msghdr
));
269 smsg
.msg_control
= cred_msg
;
270 smsg
.msg_controllen
= sizeof(cred_msg
);
272 retval
= recvmsg(sock
, &smsg
, 0);
275 dbg("unable to receive message");
278 cmsg
= CMSG_FIRSTHDR(&smsg
);
279 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
281 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
282 dbg("no sender credentials received, message ignored");
286 if (cred
->uid
!= 0) {
287 dbg("sender uid=%i, message ignored", cred
->uid
);
291 if (strncmp(msg
->magic
, UDEV_MAGIC
, sizeof(UDEV_MAGIC
)) != 0 ) {
292 dbg("message magic '%s' doesn't match, ignore it", msg
->magic
);
296 /* if no seqnum is given, we move straight to exec queue */
297 if (msg
->seqnum
== -1) {
298 list_add(&msg
->list
, &exec_list
);
301 msg_queue_insert(msg
);
310 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");
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
));
462 FD_SET(ssock
, &readfds
);
463 FD_SET(pipefds
[0], &readfds
);
464 maxsockplus
= ssock
+1;
466 fd_set workreadfds
= readfds
;
467 retval
= select(maxsockplus
, &workreadfds
, NULL
, NULL
, NULL
);
471 dbg("error in select: %s", strerror(errno
));
475 if (FD_ISSET(ssock
, &workreadfds
))
478 if (FD_ISSET(pipefds
[0], &workreadfds
))
481 if (children_waiting
) {
482 children_waiting
= 0;
492 /* this is tricky. exec_queue_manager() loops over exec_list, and
493 * calls running_with_devpath(), which loops over running_list. This gives
494 * O(N*M), which can get *nasty*. Clean up running_list before
495 * calling exec_queue_manager().
497 if (children_waiting
) {
498 children_waiting
= 0;
503 exec_queue_manager();