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>
40 #include "udev_version.h"
44 static int expected_seqnum = 0;
45 volatile static int children_waiting;
46 volatile static int msg_q_timeout;
50 LIST_HEAD(running_list);
52 static void exec_queue_manager(void);
53 static void msg_queue_manager(void);
56 unsigned char logname[LOGNAME_SIZE];
57 void log_message (int level, const char *format, ...)
61 va_start(args, format);
62 vsyslog(level, format, args);
67 static void msg_dump_queue(void)
69 struct hotplug_msg *msg;
71 list_for_each_entry(msg, &msg_list, list)
72 dbg("sequence %d in queue", msg->seqnum);
75 static void msg_dump(struct hotplug_msg *msg)
77 dbg("sequence %d, '%s', '%s', '%s'",
78 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
81 static struct hotplug_msg *msg_create(void)
83 struct hotplug_msg *new_msg;
85 new_msg = malloc(sizeof(struct hotplug_msg));
91 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;
103 /* sort message by sequence number into list*/
104 list_for_each_entry(loop_msg, &msg_list, list)
105 if (loop_msg->seqnum > msg->seqnum)
107 list_add_tail(&msg->list, &loop_msg->list);
108 dbg("queued message seq %d", msg->seqnum);
110 /* store timestamp of queuing */
111 msg->queue_time = time(NULL);
113 /* run msg queue manager */
119 /* forks event and removes event from run queue when finished */
120 static void udev_run(struct hotplug_msg *msg)
123 char action[ACTION_SIZE];
124 char devpath[DEVPATH_SIZE];
125 char *env[] = { action, devpath, NULL };
127 snprintf(action, sizeof(action), "ACTION=%s", msg->action);
128 snprintf(devpath, sizeof(devpath), "DEVPATH=%s", msg->devpath);
134 execle(UDEV_BIN, "udev", msg->subsystem, NULL, env);
135 dbg("exec of child failed");
139 dbg("fork of child failed");
140 run_queue_delete(msg);
143 /* get SIGCHLD in main loop */
144 dbg("==> exec seq %d [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
149 /* returns already running task with devpath */
150 static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
152 struct hotplug_msg *loop_msg;
153 list_for_each_entry(loop_msg, &running_list, list)
154 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
159 /* exec queue management routine executes the events and delays events for the same devpath */
160 static void exec_queue_manager()
162 struct hotplug_msg *loop_msg;
163 struct hotplug_msg *tmp_msg;
164 struct hotplug_msg *msg;
166 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
167 msg = running_with_devpath(loop_msg);
169 /* move event to run list */
170 list_move_tail(&loop_msg->list, &running_list);
172 dbg("moved seq %d to running list", loop_msg->seqnum);
174 dbg("delay seq %d, cause seq %d already working on '%s'",
175 loop_msg->seqnum, msg->seqnum, msg->devpath);
180 static void msg_move_exec(struct hotplug_msg *msg)
182 list_move_tail(&msg->list, &exec_list);
183 exec_queue_manager();
184 expected_seqnum = msg->seqnum+1;
185 dbg("moved seq %d to exec, next expected is %d",
186 msg->seqnum, expected_seqnum);
189 /* msg queue management routine handles the timeouts and dispatches the events */
190 static void msg_queue_manager()
192 struct hotplug_msg *loop_msg;
193 struct hotplug_msg *tmp_msg;
196 dbg("msg queue manager, next expected is %d", expected_seqnum);
198 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, list) {
199 /* move event with expected sequence to the exec list */
200 if (loop_msg->seqnum == expected_seqnum) {
201 msg_move_exec(loop_msg);
205 /* move event with expired timeout to the exec list */
206 msg_age = time(NULL) - loop_msg->queue_time;
207 if (msg_age > EVENT_TIMEOUT_SEC-1) {
208 msg_move_exec(loop_msg);
217 if (list_empty(&msg_list) == 0) {
218 /* set timeout for remaining queued events */
219 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
220 dbg("next event expires in %li seconds",
221 EVENT_TIMEOUT_SEC - msg_age);
222 setitimer(ITIMER_REAL, &itv, 0);
226 /* receive the msg, do some basic sanity checks, and queue it */
227 static void handle_msg(int sock)
229 struct hotplug_msg *msg;
232 struct cmsghdr *cmsg;
235 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
239 dbg("unable to store message");
244 iov.iov_len = sizeof(struct hotplug_msg);
246 memset(&smsg, 0x00, sizeof(struct msghdr));
249 smsg.msg_control = cred_msg;
250 smsg.msg_controllen = sizeof(cred_msg);
252 retval = recvmsg(sock, &smsg, 0);
255 dbg("unable to receive message");
258 cmsg = CMSG_FIRSTHDR(&smsg);
259 cred = (struct ucred *) CMSG_DATA(cmsg);
261 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
262 dbg("no sender credentials received, message ignored");
266 if (cred->uid != 0) {
267 dbg("sender uid=%i, message ignored", cred->uid);
271 if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
272 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
276 /* if no seqnum is given, we move straight to exec queue */
277 if (msg->seqnum == -1) {
278 list_add(&msg->list, &exec_list);
279 exec_queue_manager();
281 msg_queue_insert(msg);
290 static void sig_handler(int signum)
301 children_waiting = 1;
304 dbg("unhandled signal");
308 static void udev_done(int pid)
310 /* find msg associated with pid and delete it */
311 struct hotplug_msg *msg;
313 list_for_each_entry(msg, &running_list, list) {
314 if (msg->pid == pid) {
315 dbg("<== exec seq %d came back", msg->seqnum);
316 run_queue_delete(msg);
322 int main(int argc, char *argv[])
325 struct sockaddr_un saddr;
329 struct sigaction act;
331 init_logging("udevd");
332 dbg("version %s", UDEV_VERSION);
335 dbg("need to be root, exit");
339 /* set signal handler */
340 act.sa_handler = sig_handler;
341 sigemptyset (&act.sa_mask);
342 act.sa_flags = SA_RESTART;
343 sigaction(SIGINT, &act, NULL);
344 sigaction(SIGTERM, &act, NULL);
346 /* we want these two to interrupt system calls */
348 sigaction(SIGALRM, &act, NULL);
349 sigaction(SIGCHLD, &act, NULL);
351 memset(&saddr, 0x00, sizeof(saddr));
352 saddr.sun_family = AF_LOCAL;
353 /* use abstract namespace for socket path */
354 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
355 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
357 ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
359 dbg("error getting socket, exit");
363 /* the bind takes care of ensuring only one copy running */
364 retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
366 dbg("bind failed, exit");
370 /* enable receiving of the sender credentials */
371 setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
376 while(msg_q_timeout) {
381 while(children_waiting) {
382 children_waiting = 0;
383 /* reap all dead children */
385 int pid = waitpid(-1, 0, WNOHANG);
386 if ((pid == -1) || (pid == 0))