]>
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);
54 unsigned char logname
[42];
61 static void msg_dump_queue(void)
63 struct hotplug_msg
*msg
;
65 list_for_each_entry(msg
, &msg_list
, list
)
66 dbg("sequence %d in queue", msg
->seqnum
);
69 static void msg_dump(struct hotplug_msg
*msg
)
71 dbg("sequence %d, '%s', '%s', '%s'",
72 msg
->seqnum
, msg
->action
, msg
->devpath
, msg
->subsystem
);
75 static struct hotplug_msg
*msg_create(void)
77 struct hotplug_msg
*new_msg
;
79 new_msg
= malloc(sizeof(struct hotplug_msg
));
85 static void run_queue_delete(struct hotplug_msg
*msg
)
92 /* orders the message in the queue by sequence number */
93 static void msg_queue_insert(struct hotplug_msg
*msg
)
95 struct hotplug_msg
*loop_msg
;
97 /* sort message by sequence number into list*/
98 list_for_each_entry(loop_msg
, &msg_list
, list
)
99 if (loop_msg
->seqnum
> msg
->seqnum
)
101 list_add_tail(&msg
->list
, &loop_msg
->list
);
102 dbg("queued message seq %d", msg
->seqnum
);
104 /* store timestamp of queuing */
105 msg
->queue_time
= time(NULL
);
107 /* run msg queue manager */
113 /* forks event and removes event from run queue when finished */
114 static void udev_run(struct hotplug_msg
*msg
)
119 char *env
[] = { action
, devpath
, NULL
};
121 snprintf(action
, sizeof(action
), "ACTION=%s", msg
->action
);
122 snprintf(devpath
, sizeof(devpath
), "DEVPATH=%s", msg
->devpath
);
128 execle(UDEV_BIN
, "udev", msg
->subsystem
, NULL
, env
);
129 dbg("exec of child failed");
133 dbg("fork of child failed");
134 run_queue_delete(msg
);
137 /* get SIGCHLD in main loop */
138 dbg("==> exec seq %d [%d] working at '%s'", msg
->seqnum
, pid
, msg
->devpath
);
143 /* returns already running task with devpath */
144 static struct hotplug_msg
*running_with_devpath(struct hotplug_msg
*msg
)
146 struct hotplug_msg
*loop_msg
;
147 list_for_each_entry(loop_msg
, &running_list
, list
)
148 if (strncmp(loop_msg
->devpath
, msg
->devpath
, sizeof(loop_msg
->devpath
)) == 0)
153 /* exec queue management routine executes the events and delays events for the same devpath */
154 static void exec_queue_manager()
156 struct hotplug_msg
*loop_msg
;
157 struct hotplug_msg
*tmp_msg
;
158 struct hotplug_msg
*msg
;
160 list_for_each_entry_safe(loop_msg
, tmp_msg
, &exec_list
, list
) {
161 msg
= running_with_devpath(loop_msg
);
163 /* move event to run list */
164 list_move_tail(&loop_msg
->list
, &running_list
);
166 dbg("moved seq %d to running list", loop_msg
->seqnum
);
168 dbg("delay seq %d, cause seq %d already working on '%s'",
169 loop_msg
->seqnum
, msg
->seqnum
, msg
->devpath
);
174 static void msg_move_exec(struct hotplug_msg
*msg
)
176 list_move_tail(&msg
->list
, &exec_list
);
177 exec_queue_manager();
178 expected_seqnum
= msg
->seqnum
+1;
179 dbg("moved seq %d to exec, next expected is %d",
180 msg
->seqnum
, expected_seqnum
);
183 /* msg queue management routine handles the timeouts and dispatches the events */
184 static void msg_queue_manager()
186 struct hotplug_msg
*loop_msg
;
187 struct hotplug_msg
*tmp_msg
;
190 dbg("msg queue manager, next expected is %d", expected_seqnum
);
192 list_for_each_entry_safe(loop_msg
, tmp_msg
, &msg_list
, list
) {
193 /* move event with expected sequence to the exec list */
194 if (loop_msg
->seqnum
== expected_seqnum
) {
195 msg_move_exec(loop_msg
);
199 /* move event with expired timeout to the exec list */
200 msg_age
= time(NULL
) - loop_msg
->queue_time
;
201 if (msg_age
> EVENT_TIMEOUT_SEC
-1) {
202 msg_move_exec(loop_msg
);
211 if (list_empty(&msg_list
) == 0) {
212 /* set timeout for remaining queued events */
213 struct itimerval itv
= {{0, 0}, {EVENT_TIMEOUT_SEC
- msg_age
, 0}};
214 dbg("next event expires in %li seconds",
215 EVENT_TIMEOUT_SEC
- msg_age
);
216 setitimer(ITIMER_REAL
, &itv
, 0);
220 /* receive the msg, do some basic sanity checks, and queue it */
221 static void handle_msg(int sock
)
223 struct hotplug_msg
*msg
;
226 struct cmsghdr
*cmsg
;
229 char cred_msg
[CMSG_SPACE(sizeof(struct ucred
))];
233 dbg("unable to store message");
238 iov
.iov_len
= sizeof(struct hotplug_msg
);
240 memset(&smsg
, 0x00, sizeof(struct msghdr
));
243 smsg
.msg_control
= cred_msg
;
244 smsg
.msg_controllen
= sizeof(cred_msg
);
246 retval
= recvmsg(sock
, &smsg
, 0);
249 dbg("unable to receive message");
252 cmsg
= CMSG_FIRSTHDR(&smsg
);
253 cred
= (struct ucred
*) CMSG_DATA(cmsg
);
255 if (cred
->uid
!= 0) {
256 dbg("sender uid=%i, message ignored", cred
->uid
);
261 if (strncmp(msg
->magic
, UDEV_MAGIC
, sizeof(UDEV_MAGIC
)) != 0 ) {
262 dbg("message magic '%s' doesn't match, ignore it", msg
->magic
);
267 /* if no seqnum is given, we move straight to exec queue */
268 if (msg
->seqnum
== -1) {
269 list_add(&msg
->list
, &exec_list
);
270 exec_queue_manager();
272 msg_queue_insert(msg
);
276 static void sig_handler(int signum
)
287 children_waiting
= 1;
290 dbg("unhandled signal");
294 static void udev_done(int pid
)
296 /* find msg associated with pid and delete it */
297 struct hotplug_msg
*msg
;
299 list_for_each_entry(msg
, &running_list
, list
) {
300 if (msg
->pid
== pid
) {
301 dbg("<== exec seq %d came back", msg
->seqnum
);
302 run_queue_delete(msg
);
308 int main(int argc
, char *argv
[])
311 struct sockaddr_un saddr
;
315 struct sigaction act
;
317 init_logging("udevd");
319 /* set signal handler */
320 act
.sa_handler
= sig_handler
;
321 sigemptyset (&act
.sa_mask
);
322 act
.sa_flags
= SA_RESTART
;
323 sigaction(SIGINT
, &act
, NULL
);
324 sigaction(SIGTERM
, &act
, NULL
);
326 /* we want these two to interrupt system calls */
328 sigaction(SIGALRM
, &act
, NULL
);
329 sigaction(SIGCHLD
, &act
, NULL
);
331 memset(&saddr
, 0x00, sizeof(saddr
));
332 saddr
.sun_family
= AF_LOCAL
;
333 /* use abstract namespace for socket path */
334 strcpy(&saddr
.sun_path
[1], UDEVD_SOCK_PATH
);
335 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + strlen(saddr
.sun_path
+1) + 1;
337 ssock
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
339 dbg("error getting socket");
343 /* the bind takes care of ensuring only one copy running */
344 retval
= bind(ssock
, (struct sockaddr
*) &saddr
, addrlen
);
346 dbg("bind failed\n");
350 /* enable receiving of the sender credentials */
351 setsockopt(ssock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof(on
));
356 while(msg_q_timeout
) {
361 while(children_waiting
) {
362 children_waiting
= 0;
363 /* reap all dead children */
365 int pid
= waitpid(-1, 0, WNOHANG
);
366 if ((pid
== -1) || (pid
== 0))