]>
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>
39 #include "udev_version.h"
43 static int expected_seqnum
= 0;
44 volatile static int children_waiting
;
45 volatile static int msg_q_timeout
;
49 LIST_HEAD(running_list
);
51 static void exec_queue_manager(void);
52 static void msg_queue_manager(void);
55 unsigned char logname
[42];
56 void log_message (int level
, const char *format
, ...)
60 va_start(args
, format
);
61 vsyslog(level
, format
, args
);
66 static void msg_dump_queue(void)
68 struct hotplug_msg
*msg
;
70 list_for_each_entry(msg
, &msg_list
, list
)
71 dbg("sequence %d in queue", msg
->seqnum
);
74 static void msg_dump(struct hotplug_msg
*msg
)
76 dbg("sequence %d, '%s', '%s', '%s'",
77 msg
->seqnum
, msg
->action
, msg
->devpath
, msg
->subsystem
);
80 static struct hotplug_msg
*msg_create(void)
82 struct hotplug_msg
*new_msg
;
84 new_msg
= malloc(sizeof(struct hotplug_msg
));
90 static void run_queue_delete(struct hotplug_msg
*msg
)
97 /* orders the message in the queue by sequence number */
98 static void msg_queue_insert(struct hotplug_msg
*msg
)
100 struct hotplug_msg
*loop_msg
;
102 /* sort message by sequence number into list*/
103 list_for_each_entry(loop_msg
, &msg_list
, list
)
104 if (loop_msg
->seqnum
> msg
->seqnum
)
106 list_add_tail(&msg
->list
, &loop_msg
->list
);
107 dbg("queued message seq %d", msg
->seqnum
);
109 /* store timestamp of queuing */
110 msg
->queue_time
= time(NULL
);
112 /* run msg queue manager */
118 /* forks event and removes event from run queue when finished */
119 static void udev_run(struct hotplug_msg
*msg
)
124 char *env
[] = { action
, devpath
, NULL
};
126 snprintf(action
, sizeof(action
), "ACTION=%s", msg
->action
);
127 snprintf(devpath
, sizeof(devpath
), "DEVPATH=%s", msg
->devpath
);
133 execle(UDEV_BIN
, "udev", msg
->subsystem
, NULL
, env
);
134 dbg("exec of child failed");
138 dbg("fork of child failed");
139 run_queue_delete(msg
);
142 /* get SIGCHLD in main loop */
143 dbg("==> exec seq %d [%d] working at '%s'", msg
->seqnum
, pid
, msg
->devpath
);
148 /* returns already running task with devpath */
149 static struct hotplug_msg
*running_with_devpath(struct hotplug_msg
*msg
)
151 struct hotplug_msg
*loop_msg
;
152 list_for_each_entry(loop_msg
, &running_list
, list
)
153 if (strncmp(loop_msg
->devpath
, msg
->devpath
, sizeof(loop_msg
->devpath
)) == 0)
158 /* exec queue management routine executes the events and delays events for the same devpath */
159 static void exec_queue_manager()
161 struct hotplug_msg
*loop_msg
;
162 struct hotplug_msg
*tmp_msg
;
163 struct hotplug_msg
*msg
;
165 list_for_each_entry_safe(loop_msg
, tmp_msg
, &exec_list
, list
) {
166 msg
= running_with_devpath(loop_msg
);
168 /* move event to run list */
169 list_move_tail(&loop_msg
->list
, &running_list
);
171 dbg("moved seq %d to running list", loop_msg
->seqnum
);
173 dbg("delay seq %d, cause seq %d already working on '%s'",
174 loop_msg
->seqnum
, msg
->seqnum
, msg
->devpath
);
179 static void msg_move_exec(struct hotplug_msg
*msg
)
181 list_move_tail(&msg
->list
, &exec_list
);
182 exec_queue_manager();
183 expected_seqnum
= msg
->seqnum
+1;
184 dbg("moved seq %d to exec, next expected is %d",
185 msg
->seqnum
, expected_seqnum
);
188 /* msg queue management routine handles the timeouts and dispatches the events */
189 static void msg_queue_manager()
191 struct hotplug_msg
*loop_msg
;
192 struct hotplug_msg
*tmp_msg
;
195 dbg("msg queue manager, next expected is %d", expected_seqnum
);
197 list_for_each_entry_safe(loop_msg
, tmp_msg
, &msg_list
, list
) {
198 /* move event with expected sequence to the exec list */
199 if (loop_msg
->seqnum
== expected_seqnum
) {
200 msg_move_exec(loop_msg
);
204 /* move event with expired timeout to the exec list */
205 msg_age
= time(NULL
) - loop_msg
->queue_time
;
206 if (msg_age
> EVENT_TIMEOUT_SEC
-1) {
207 msg_move_exec(loop_msg
);
216 if (list_empty(&msg_list
) == 0) {
217 /* set timeout for remaining queued events */
218 struct itimerval itv
= {{0, 0}, {EVENT_TIMEOUT_SEC
- msg_age
, 0}};
219 dbg("next event expires in %li seconds",
220 EVENT_TIMEOUT_SEC
- msg_age
);
221 setitimer(ITIMER_REAL
, &itv
, 0);
225 /* receive the msg, do some basic sanity checks, and queue it */
226 static void handle_msg(int sock
)
228 struct hotplug_msg
*msg
;
231 struct cmsghdr
*cmsg
;
234 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
238 dbg("unable to store message");
243 iov
.iov_len
= sizeof(struct hotplug_msg
);
245 memset(&smsg
, 0x00, sizeof(struct msghdr
));
248 smsg
.msg_control
= cred_msg
;
249 smsg
.msg_controllen
= sizeof(cred_msg
);
251 retval
= recvmsg(sock
, &smsg
, 0);
254 dbg("unable to receive message");
257 cmsg
= CMSG_FIRSTHDR(&smsg
);
258 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
260 if (cmsg
== NULL
|| cmsg
->cmsg_type
!= SCM_CREDENTIALS
) {
261 dbg("no sender credentials received, message ignored");
265 if (cred
->uid
!= 0) {
266 dbg("sender uid=%i, message ignored", cred
->uid
);
270 if (strncmp(msg
->magic
, UDEV_MAGIC
, sizeof(UDEV_MAGIC
)) != 0 ) {
271 dbg("message magic '%s' doesn't match, ignore it", msg
->magic
);
275 /* if no seqnum is given, we move straight to exec queue */
276 if (msg
->seqnum
== -1) {
277 list_add(&msg
->list
, &exec_list
);
278 exec_queue_manager();
280 msg_queue_insert(msg
);
289 static void sig_handler(int signum
)
300 children_waiting
= 1;
303 dbg("unhandled signal");
307 static void udev_done(int pid
)
309 /* find msg associated with pid and delete it */
310 struct hotplug_msg
*msg
;
312 list_for_each_entry(msg
, &running_list
, list
) {
313 if (msg
->pid
== pid
) {
314 dbg("<== exec seq %d came back", msg
->seqnum
);
315 run_queue_delete(msg
);
321 int main(int argc
, char *argv
[])
324 struct sockaddr_un saddr
;
328 struct sigaction act
;
330 init_logging("udevd");
331 dbg("version %s", UDEV_VERSION
);
334 dbg("need to be root, exit");
338 /* set signal handler */
339 act
.sa_handler
= sig_handler
;
340 sigemptyset (&act
.sa_mask
);
341 act
.sa_flags
= SA_RESTART
;
342 sigaction(SIGINT
, &act
, NULL
);
343 sigaction(SIGTERM
, &act
, NULL
);
345 /* we want these two to interrupt system calls */
347 sigaction(SIGALRM
, &act
, NULL
);
348 sigaction(SIGCHLD
, &act
, NULL
);
350 memset(&saddr
, 0x00, sizeof(saddr
));
351 saddr
.sun_family
= AF_LOCAL
;
352 /* use abstract namespace for socket path */
353 strcpy(&saddr
.sun_path
[1], UDEVD_SOCK_PATH
);
354 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(saddr
.sun_path
+1) + 1;
356 ssock
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
358 dbg("error getting socket, exit");
362 /* the bind takes care of ensuring only one copy running */
363 retval
= bind(ssock
, (struct sockaddr
*) &saddr
, addrlen
);
365 dbg("bind failed, exit");
369 /* enable receiving of the sender credentials */
370 setsockopt(ssock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
375 while(msg_q_timeout
) {
380 while(children_waiting
) {
381 children_waiting
= 0;
382 /* reap all dead children */
384 int pid
= waitpid(-1, 0, WNOHANG
);
385 if ((pid
== -1) || (pid
== 0))