]>
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 <sys/sysinfo.h>
42 #include "udev_version.h"
46 static int pipefds
[2];
47 static unsigned long long expected_seqnum
= 0;
48 static volatile int sigchilds_waiting
;
49 static volatile int run_msg_q
;
50 static volatile 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_sigchilds(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 %llu, '%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 %llu 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 %llu", 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 seqnum
[SEQNUM_SIZE
];
138 char *env
[] = { action
, devpath
, seqnum
, NULL
};
140 snprintf(action
, ACTION_SIZE
-1, "ACTION=%s", msg
->action
);
141 action
[ACTION_SIZE
-1] = '\0';
142 snprintf(devpath
, DEVPATH_SIZE
-1, "DEVPATH=%s", msg
->devpath
);
143 devpath
[DEVPATH_SIZE
-1] = '\0';
144 sprintf(seqnum
, "SEQNUM=%llu", msg
->seqnum
);
150 execle(udev_bin
, "udev", msg
->subsystem
, NULL
, env
);
151 dbg("exec of child failed");
155 dbg("fork of child failed");
156 run_queue_delete(msg
);
157 /* note: we never managed to run, so we had no impact on
158 * running_with_devpath(), so don't bother setting run_exec_q
162 /* get SIGCHLD in main loop */
163 dbg("==> exec seq %llu [%d] working at '%s'", msg
->seqnum
, pid
, msg
->devpath
);
168 /* returns already running task with devpath */
169 static struct hotplug_msg
*running_with_devpath(struct hotplug_msg
*msg
)
171 struct hotplug_msg
*loop_msg
;
172 list_for_each_entry(loop_msg
, &running_list
, list
)
173 if (strncmp(loop_msg
->devpath
, msg
->devpath
, sizeof(loop_msg
->devpath
)) == 0)
178 /* exec queue management routine executes the events and delays events for the same devpath */
179 static void exec_queue_manager(void)
181 struct hotplug_msg
*loop_msg
;
182 struct hotplug_msg
*tmp_msg
;
183 struct hotplug_msg
*msg
;
185 list_for_each_entry_safe(loop_msg
, tmp_msg
, &exec_list
, list
) {
186 msg
= running_with_devpath(loop_msg
);
188 /* move event to run list */
189 list_move_tail(&loop_msg
->list
, &running_list
);
191 dbg("moved seq %llu to running list", loop_msg
->seqnum
);
193 dbg("delay seq %llu, cause seq %llu already working on '%s'",
194 loop_msg
->seqnum
, msg
->seqnum
, msg
->devpath
);
199 static void msg_move_exec(struct hotplug_msg
*msg
)
201 list_move_tail(&msg
->list
, &exec_list
);
203 expected_seqnum
= msg
->seqnum
+1;
204 dbg("moved seq %llu to exec, next expected is %llu",
205 msg
->seqnum
, expected_seqnum
);
208 /* msg queue management routine handles the timeouts and dispatches the events */
209 static void msg_queue_manager(void)
211 struct hotplug_msg
*loop_msg
;
212 struct hotplug_msg
*tmp_msg
;
216 dbg("msg queue manager, next expected is %llu", expected_seqnum
);
218 list_for_each_entry_safe(loop_msg
, tmp_msg
, &msg_list
, list
) {
219 /* move event with expected sequence to the exec list */
220 if (loop_msg
->seqnum
== expected_seqnum
) {
221 msg_move_exec(loop_msg
);
225 /* move event with expired timeout to the exec list */
227 msg_age
= info
.uptime
- loop_msg
->queue_time
;
228 dbg("seq %llu is %li seconds old", loop_msg
->seqnum
, msg_age
);
229 if (msg_age
> EVENT_TIMEOUT_SEC
-1) {
230 msg_move_exec(loop_msg
);
239 /* set timeout for remaining queued events */
240 if (list_empty(&msg_list
) == 0) {
241 struct itimerval itv
= {{0, 0}, {EVENT_TIMEOUT_SEC
- msg_age
, 0}};
242 dbg("next event expires in %li seconds", EVENT_TIMEOUT_SEC
- msg_age
);
243 setitimer(ITIMER_REAL
, &itv
, NULL
);
247 /* receive the msg, do some basic sanity checks, and queue it */
248 static void handle_msg(int sock
)
250 struct hotplug_msg
*msg
;
253 struct cmsghdr
*cmsg
;
256 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
260 dbg("unable to store message");
265 iov
.iov_len
= sizeof(struct hotplug_msg
);
267 memset(&smsg
, 0x00, sizeof(struct msghdr
));
270 smsg
.msg_control
= cred_msg
;
271 smsg
.msg_controllen
= sizeof(cred_msg
);
273 retval
= recvmsg(sock
, &smsg
, 0);
276 dbg("unable to receive message");
279 cmsg
= CMSG_FIRSTHDR(&smsg
);
280 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
282 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
283 dbg("no sender credentials received, message ignored");
287 if (cred
->uid
!= 0) {
288 dbg("sender uid=%i, message ignored", cred
->uid
);
292 if (strncmp(msg
->magic
, UDEV_MAGIC
, sizeof(UDEV_MAGIC
)) != 0 ) {
293 dbg("message magic '%s' doesn't match, ignore it", msg
->magic
);
297 /* if no seqnum is given, we move straight to exec queue */
298 if (msg
->seqnum
== 0) {
299 list_add(&msg
->list
, &exec_list
);
302 msg_queue_insert(msg
);
311 static void asmlinkage
sig_handler(int signum
)
321 /* set flag, then write to pipe if needed */
326 /* set flag, then write to pipe if needed */
327 sigchilds_waiting
= 1;
333 /* if pipe is empty, write to pipe to force select to return
334 * immediately when it gets called
337 rc
= write(pipefds
[1],&signum
,sizeof(signum
));
343 static void udev_done(int pid
)
345 /* find msg associated with pid and delete it */
346 struct hotplug_msg
*msg
;
348 list_for_each_entry(msg
, &running_list
, list
) {
349 if (msg
->pid
== pid
) {
350 dbg("<== exec seq %llu came back", msg
->seqnum
);
351 run_queue_delete(msg
);
353 /* we want to run the exec queue manager since there may
354 * be events waiting with the devpath of the one that
363 static void reap_sigchilds(void)
366 int pid
= waitpid(-1, NULL
, WNOHANG
);
367 if ((pid
== -1) || (pid
== 0))
373 /* just read everything from the pipe and clear the flag,
374 * the flags was set in the signal handler
376 static void user_sighandler(void)
380 int rc
= read(pipefds
[0], &sig
, sizeof(sig
));
388 int main(int argc
, char *argv
[], char *envp
[])
390 int ssock
, maxsockplus
;
391 struct sockaddr_un saddr
;
394 const int feature_on
= 1;
395 struct sigaction act
;
398 logging_init("udevd");
399 dbg("version %s", UDEV_VERSION
);
402 dbg("need to be root, exit");
406 /* make sure we don't lock any path */
408 umask(umask(077) | 022);
410 /* Set fds to dev/null */
411 fd
= open( "/dev/null", O_RDWR
);
413 dbg("error opening /dev/null %s", strerror(errno
));
422 /* become session leader */
425 /* setup signal handler pipe */
426 retval
= pipe(pipefds
);
428 dbg("error getting pipes: %s", strerror(errno
));
432 retval
= fcntl(pipefds
[0], F_SETFL
, O_NONBLOCK
);
434 dbg("error fcntl on read pipe: %s", strerror(errno
));
437 retval
= fcntl(pipefds
[0], F_SETFD
, FD_CLOEXEC
);
439 dbg("error fcntl on read pipe: %s", strerror(errno
));
443 retval
= fcntl(pipefds
[1], F_SETFL
, O_NONBLOCK
);
445 dbg("error fcntl on write pipe: %s", strerror(errno
));
448 retval
= fcntl(pipefds
[1], F_SETFD
, FD_CLOEXEC
);
450 dbg("error fcntl on write pipe: %s", strerror(errno
));
454 /* set signal handlers */
455 act
.sa_handler
= (void (*) (int))sig_handler
;
456 sigemptyset(&act
.sa_mask
);
457 act
.sa_flags
= SA_RESTART
;
458 sigaction(SIGINT
, &act
, NULL
);
459 sigaction(SIGTERM
, &act
, NULL
);
460 sigaction(SIGALRM
, &act
, NULL
);
461 sigaction(SIGCHLD
, &act
, NULL
);
463 memset(&saddr
, 0x00, sizeof(saddr
));
464 saddr
.sun_family
= AF_LOCAL
;
465 /* use abstract namespace for socket path */
466 strcpy(&saddr
.sun_path
[1], UDEVD_SOCK_PATH
);
467 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(saddr
.sun_path
+1) + 1;
469 ssock
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
471 dbg("error getting socket, exit");
475 set_cloexec_flag(ssock
, 1);
477 /* the bind takes care of ensuring only one copy running */
478 retval
= bind(ssock
, (struct sockaddr
*) &saddr
, addrlen
);
480 dbg("bind failed, exit");
483 retval
= fcntl(ssock
, F_SETFD
, FD_CLOEXEC
);
485 dbg("error fcntl on ssock: %s", strerror(errno
));
489 /* enable receiving of the sender credentials */
490 setsockopt(ssock
, SOL_SOCKET
, SO_PASSCRED
, &feature_on
, sizeof(feature_on
));
492 /* possible override of udev binary, used for testing */
493 udev_bin
= getenv("UDEV_BIN");
494 if (udev_bin
!= NULL
)
495 dbg("udev binary is set to '%s'", udev_bin
);
500 FD_SET(ssock
, &readfds
);
501 FD_SET(pipefds
[0], &readfds
);
502 maxsockplus
= ssock
+1;
504 fd_set workreadfds
= readfds
;
505 retval
= select(maxsockplus
, &workreadfds
, NULL
, NULL
, NULL
);
509 dbg("error in select: %s", strerror(errno
));
513 if (FD_ISSET(ssock
, &workreadfds
))
516 if (FD_ISSET(pipefds
[0], &workreadfds
))
519 if (sigchilds_waiting
) {
520 sigchilds_waiting
= 0;
530 /* clean up running_list before calling exec_queue_manager() */
531 if (sigchilds_waiting
) {
532 sigchilds_waiting
= 0;
537 exec_queue_manager();