]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevd.c
[PATCH] devfs.sh-ide-floppy
[thirdparty/systemd.git] / udevd.c
CommitLineData
7fafc032 1/*
53921bfa 2 * udevd.c - hotplug event serializer
7fafc032 3 *
7fafc032 4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
2f6cbd19 5 * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
7fafc032
KS
6 *
7 *
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.
11 *
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.
16 *
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.
20 *
21 */
22
a695feae 23#include <stddef.h>
90c210eb 24#include <sys/wait.h>
7fafc032
KS
25#include <signal.h>
26#include <unistd.h>
27#include <errno.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
e5a2989e 31#include <sys/time.h>
53921bfa
KS
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <sys/un.h>
f27125f9 35#include <fcntl.h>
e5a2989e
KS
36#include "klibc_fixups.h"
37#ifndef __KLIBC__
38#include <sys/sysinfo.h>
39#endif
7fafc032 40
a695feae 41#include "list.h"
7fafc032 42#include "udev.h"
c81b35c0 43#include "udev_lib.h"
35b7d88c 44#include "udev_version.h"
7fafc032
KS
45#include "udevd.h"
46#include "logging.h"
47
f27125f9 48static int pipefds[2];
53921bfa 49static int expected_seqnum = 0;
2f6cbd19 50volatile static int children_waiting;
f27125f9 51volatile static int run_msg_q;
52volatile static int sig_flag;
53static int run_exec_q;
7fafc032 54
4a539daf
KS
55static LIST_HEAD(msg_list);
56static LIST_HEAD(exec_list);
57static LIST_HEAD(running_list);
7fafc032 58
2f6cbd19
KS
59static void exec_queue_manager(void);
60static void msg_queue_manager(void);
f27125f9 61static void user_sighandler(void);
62static void reap_kids(void);
7fafc032 63
d026a35d 64#ifdef LOG
d00bd172 65unsigned char logname[LOGNAME_SIZE];
d026a35d 66void log_message (int level, const char *format, ...)
51a8bb2f 67{
d026a35d
GKH
68 va_list args;
69
70 va_start(args, format);
71 vsyslog(level, format, args);
72 va_end(args);
51a8bb2f 73}
d026a35d 74#endif
51a8bb2f 75
b6bf0b12
KS
76#define msg_dump(msg) \
77 dbg("msg_dump: sequence %d, '%s', '%s', '%s'", \
78 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
79
53921bfa 80static void msg_dump_queue(void)
7fafc032 81{
f27125f9 82#ifdef DEBUG
53921bfa 83 struct hotplug_msg *msg;
7fafc032 84
53921bfa
KS
85 list_for_each_entry(msg, &msg_list, list)
86 dbg("sequence %d in queue", msg->seqnum);
f27125f9 87#endif
35b7d88c
KS
88}
89
53921bfa 90static struct hotplug_msg *msg_create(void)
35b7d88c 91{
53921bfa 92 struct hotplug_msg *new_msg;
35b7d88c 93
53921bfa 94 new_msg = malloc(sizeof(struct hotplug_msg));
2f6cbd19 95 if (new_msg == NULL)
53921bfa 96 dbg("error malloc");
53921bfa 97 return new_msg;
35b7d88c
KS
98}
99
2f6cbd19 100static void run_queue_delete(struct hotplug_msg *msg)
8e2229c4 101{
2f6cbd19
KS
102 list_del(&msg->list);
103 free(msg);
8e2229c4
KS
104}
105
53921bfa
KS
106/* orders the message in the queue by sequence number */
107static void msg_queue_insert(struct hotplug_msg *msg)
35b7d88c 108{
53921bfa 109 struct hotplug_msg *loop_msg;
e5a2989e 110 struct sysinfo info;
35b7d88c 111
f27125f9 112 /* sort message by sequence number into list. events
113 * will tend to come in order, so scan the list backwards
114 */
115 list_for_each_entry_reverse(loop_msg, &msg_list, list)
116 if (loop_msg->seqnum < msg->seqnum)
53921bfa 117 break;
35b7d88c 118
53921bfa 119 /* store timestamp of queuing */
e5a2989e
KS
120 sysinfo(&info);
121 msg->queue_time = info.uptime;
122
123 list_add(&msg->list, &loop_msg->list);
124 dbg("queued message seq %d", msg->seqnum);
35b7d88c 125
2f6cbd19 126 /* run msg queue manager */
f27125f9 127 run_msg_q = 1;
7fafc032 128
53921bfa 129 return ;
7fafc032
KS
130}
131
53921bfa 132/* forks event and removes event from run queue when finished */
2f6cbd19 133static void udev_run(struct hotplug_msg *msg)
7fafc032 134{
90c210eb 135 pid_t pid;
e964c2c0
KS
136 char action[ACTION_SIZE];
137 char devpath[DEVPATH_SIZE];
f8911dbb
KS
138 char *env[] = { action, devpath, NULL };
139
9b28a52a
KS
140 strcpy(action, "ACTION=");
141 strfieldcat(action, msg->action);
142 strcpy(devpath, "DEVPATH=");
143 strfieldcat(devpath, msg->devpath);
90c210eb
KS
144
145 pid = fork();
146 switch (pid) {
147 case 0:
33db4b8d 148 /* child */
f8911dbb 149 execle(UDEV_BIN, "udev", msg->subsystem, NULL, env);
33db4b8d
KS
150 dbg("exec of child failed");
151 exit(1);
90c210eb
KS
152 break;
153 case -1:
33db4b8d 154 dbg("fork of child failed");
2f6cbd19 155 run_queue_delete(msg);
f27125f9 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
158 */
2f6cbd19 159 break;
90c210eb 160 default:
2f6cbd19
KS
161 /* get SIGCHLD in main loop */
162 dbg("==> exec seq %d [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
163 msg->pid = pid;
90c210eb 164 }
7fafc032
KS
165}
166
53921bfa
KS
167/* returns already running task with devpath */
168static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
7fafc032 169{
53921bfa 170 struct hotplug_msg *loop_msg;
2f6cbd19 171 list_for_each_entry(loop_msg, &running_list, list)
53921bfa
KS
172 if (strncmp(loop_msg->devpath, msg->devpath, sizeof(loop_msg->devpath)) == 0)
173 return loop_msg;
174 return NULL;
7fafc032
KS
175}
176
2f6cbd19
KS
177/* exec queue management routine executes the events and delays events for the same devpath */
178static void exec_queue_manager()
7fafc032 179{
53921bfa
KS
180 struct hotplug_msg *loop_msg;
181 struct hotplug_msg *tmp_msg;
a695feae 182 struct hotplug_msg *msg;
53921bfa 183
2f6cbd19
KS
184 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, list) {
185 msg = running_with_devpath(loop_msg);
186 if (!msg) {
187 /* move event to run list */
188 list_move_tail(&loop_msg->list, &running_list);
189 udev_run(loop_msg);
190 dbg("moved seq %d to running list", loop_msg->seqnum);
191 } else {
192 dbg("delay seq %d, cause seq %d already working on '%s'",
193 loop_msg->seqnum, msg->seqnum, msg->devpath);
53921bfa 194 }
53921bfa
KS
195 }
196}
197
2f6cbd19 198static void msg_move_exec(struct hotplug_msg *msg)
86590cd5 199{
2f6cbd19 200 list_move_tail(&msg->list, &exec_list);
f27125f9 201 run_exec_q = 1;
2f6cbd19
KS
202 expected_seqnum = msg->seqnum+1;
203 dbg("moved seq %d to exec, next expected is %d",
204 msg->seqnum, expected_seqnum);
86590cd5
KS
205}
206
2f6cbd19
KS
207/* msg queue management routine handles the timeouts and dispatches the events */
208static void msg_queue_manager()
53921bfa
KS
209{
210 struct hotplug_msg *loop_msg;
a695feae 211 struct hotplug_msg *tmp_msg;
e5a2989e
KS
212 struct sysinfo info;
213 long msg_age = 0;
a695feae 214
2f6cbd19 215 dbg("msg queue manager, next expected is %d", expected_seqnum);
a695feae 216recheck:
2f6cbd19
KS
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);
221 continue;
a695feae 222 }
35b7d88c 223
2f6cbd19 224 /* move event with expired timeout to the exec list */
e5a2989e
KS
225 sysinfo(&info);
226 msg_age = info.uptime - loop_msg->queue_time;
227 dbg("seq %d is %li seconds old", loop_msg->seqnum, msg_age);
2f6cbd19
KS
228 if (msg_age > EVENT_TIMEOUT_SEC-1) {
229 msg_move_exec(loop_msg);
230 goto recheck;
53921bfa 231 } else {
2f6cbd19 232 break;
53921bfa 233 }
2f6cbd19
KS
234 }
235
236 msg_dump_queue();
237
e5a2989e 238 /* set timeout for remaining queued events */
2f6cbd19 239 if (list_empty(&msg_list) == 0) {
2f6cbd19 240 struct itimerval itv = {{0, 0}, {EVENT_TIMEOUT_SEC - msg_age, 0}};
e5a2989e 241 dbg("next event expires in %li seconds", EVENT_TIMEOUT_SEC - msg_age);
2f6cbd19 242 setitimer(ITIMER_REAL, &itv, 0);
7fafc032 243 }
7fafc032
KS
244}
245
2f6cbd19
KS
246/* receive the msg, do some basic sanity checks, and queue it */
247static void handle_msg(int sock)
7fafc032 248{
53921bfa
KS
249 struct hotplug_msg *msg;
250 int retval;
0028653c
KS
251 struct msghdr smsg;
252 struct cmsghdr *cmsg;
253 struct iovec iov;
254 struct ucred *cred;
255 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
a695feae 256
53921bfa
KS
257 msg = msg_create();
258 if (msg == NULL) {
259 dbg("unable to store message");
2f6cbd19 260 return;
7fafc032 261 }
7fafc032 262
0028653c
KS
263 iov.iov_base = msg;
264 iov.iov_len = sizeof(struct hotplug_msg);
265
266 memset(&smsg, 0x00, sizeof(struct msghdr));
267 smsg.msg_iov = &iov;
268 smsg.msg_iovlen = 1;
269 smsg.msg_control = cred_msg;
270 smsg.msg_controllen = sizeof(cred_msg);
271
272 retval = recvmsg(sock, &smsg, 0);
53921bfa 273 if (retval < 0) {
2f6cbd19
KS
274 if (errno != EINTR)
275 dbg("unable to receive message");
276 return;
53921bfa 277 }
0028653c
KS
278 cmsg = CMSG_FIRSTHDR(&smsg);
279 cred = (struct ucred *) CMSG_DATA(cmsg);
280
7b1cbec9
KS
281 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
282 dbg("no sender credentials received, message ignored");
283 goto skip;
284 }
285
0028653c
KS
286 if (cred->uid != 0) {
287 dbg("sender uid=%i, message ignored", cred->uid);
7b1cbec9 288 goto skip;
0028653c
KS
289 }
290
53921bfa
KS
291 if (strncmp(msg->magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
292 dbg("message magic '%s' doesn't match, ignore it", msg->magic);
7b1cbec9 293 goto skip;
53921bfa 294 }
a695feae 295
86590cd5 296 /* if no seqnum is given, we move straight to exec queue */
2f6cbd19 297 if (msg->seqnum == -1) {
86590cd5 298 list_add(&msg->list, &exec_list);
f27125f9 299 run_exec_q = 1;
86590cd5 300 } else {
86590cd5 301 msg_queue_insert(msg);
86590cd5 302 }
7b1cbec9
KS
303 return;
304
305skip:
306 free(msg);
307 return;
a695feae 308}
1c5c245e 309
53921bfa 310static void sig_handler(int signum)
7fafc032 311{
f27125f9 312 int rc;
53921bfa
KS
313 switch (signum) {
314 case SIGINT:
315 case SIGTERM:
53921bfa
KS
316 exit(20 + signum);
317 break;
2f6cbd19 318 case SIGALRM:
f27125f9 319 /* set flag, then write to pipe if needed */
320 run_msg_q = 1;
321 goto do_write;
2f6cbd19
KS
322 break;
323 case SIGCHLD:
f27125f9 324 /* set flag, then write to pipe if needed */
2f6cbd19 325 children_waiting = 1;
f27125f9 326 goto do_write;
2f6cbd19 327 break;
53921bfa
KS
328 default:
329 dbg("unhandled signal");
f27125f9 330 return;
331 }
332
333do_write:
334 /* if pipe is empty, write to pipe to force select to return
335 * immediately when it gets called
336 */
337 if (!sig_flag) {
338 rc = write(pipefds[1],&signum,sizeof(signum));
339 if (rc < 0)
340 dbg("unable to write to pipe");
341 else
342 sig_flag = 1;
7fafc032 343 }
33db4b8d 344}
7fafc032 345
2f6cbd19
KS
346static void udev_done(int pid)
347{
348 /* find msg associated with pid and delete it */
349 struct hotplug_msg *msg;
350
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);
f27125f9 355
356 /* we want to run the exec queue manager since there may
357 * be events waiting with the devpath of the one that
358 * just finished
359 */
360 run_exec_q = 1;
2f6cbd19
KS
361 return;
362 }
363 }
364}
365
f27125f9 366static void reap_kids()
367{
368 /* reap all dead children */
369 while(1) {
370 int pid = waitpid(-1, 0, WNOHANG);
371 if ((pid == -1) || (pid == 0))
372 break;
373 udev_done(pid);
374 }
375}
376
377/* just read everything from the pipe and clear the flag,
378 * the useful flags were set in the signal handler
379 */
380static void user_sighandler()
381{
382 int sig;
383 while(1) {
384 int rc = read(pipefds[0],&sig,sizeof(sig));
385 if (rc < 0)
386 break;
387
388 sig_flag = 0;
389 }
390}
391
392
33db4b8d
KS
393int main(int argc, char *argv[])
394{
f27125f9 395 int ssock, maxsockplus;
53921bfa 396 struct sockaddr_un saddr;
1dadabd7 397 socklen_t addrlen;
53921bfa 398 int retval;
0028653c 399 const int on = 1;
f8911dbb 400 struct sigaction act;
f27125f9 401 fd_set readfds;
53921bfa 402
95a6f4c8 403 init_logging("udevd");
896e5aa9 404 dbg("version %s", UDEV_VERSION);
95a6f4c8 405
7b1cbec9
KS
406 if (getuid() != 0) {
407 dbg("need to be root, exit");
408 exit(1);
409 }
e5a2989e 410
f27125f9 411 /* setup signal handler pipe */
e5a2989e
KS
412 retval = pipe(pipefds);
413 if (retval < 0) {
414 dbg("error getting pipes: %s", strerror(errno));
415 exit(1);
416 }
417
418 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
419 if (retval < 0) {
420 dbg("error fcntl on read pipe: %s", strerror(errno));
421 exit(1);
422 }
423
424 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
425 if (retval < 0) {
426 dbg("error fcntl on write pipe: %s", strerror(errno));
427 exit(1);
428 }
f27125f9 429
430 /* set signal handlers */
f8911dbb 431 act.sa_handler = sig_handler;
f27125f9 432 sigemptyset(&act.sa_mask);
f8911dbb
KS
433 act.sa_flags = SA_RESTART;
434 sigaction(SIGINT, &act, NULL);
435 sigaction(SIGTERM, &act, NULL);
f8911dbb
KS
436 sigaction(SIGALRM, &act, NULL);
437 sigaction(SIGCHLD, &act, NULL);
7fafc032 438
53921bfa
KS
439 memset(&saddr, 0x00, sizeof(saddr));
440 saddr.sun_family = AF_LOCAL;
872344c4
KS
441 /* use abstract namespace for socket path */
442 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
1dadabd7 443 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
53921bfa 444
2f6cbd19 445 ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
53921bfa 446 if (ssock == -1) {
7b1cbec9 447 dbg("error getting socket, exit");
53921bfa
KS
448 exit(1);
449 }
450
2f6cbd19 451 /* the bind takes care of ensuring only one copy running */
f8911dbb 452 retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
53921bfa 453 if (retval < 0) {
7b1cbec9 454 dbg("bind failed, exit");
53921bfa
KS
455 goto exit;
456 }
457
0028653c
KS
458 /* enable receiving of the sender credentials */
459 setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
460
e5a2989e
KS
461 FD_ZERO(&readfds);
462 FD_SET(ssock, &readfds);
463 FD_SET(pipefds[0], &readfds);
f27125f9 464 maxsockplus = ssock+1;
2f6cbd19 465 while (1) {
f27125f9 466 fd_set workreadfds = readfds;
467 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
e5a2989e 468
f27125f9 469 if (retval < 0) {
e5a2989e
KS
470 if (errno != EINTR)
471 dbg("error in select: %s", strerror(errno));
f27125f9 472 continue;
2f6cbd19 473 }
e5a2989e 474
f27125f9 475 if (FD_ISSET(ssock, &workreadfds))
476 handle_msg(ssock);
e5a2989e 477
f27125f9 478 if (FD_ISSET(pipefds[0], &workreadfds))
479 user_sighandler();
e5a2989e 480
f27125f9 481 if (children_waiting) {
2f6cbd19 482 children_waiting = 0;
f27125f9 483 reap_kids();
484 }
e5a2989e 485
f27125f9 486 if (run_msg_q) {
487 run_msg_q = 0;
488 msg_queue_manager();
489 }
e5a2989e 490
f27125f9 491 if (run_exec_q) {
f27125f9 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().
496 */
f27125f9 497 if (children_waiting) {
498 children_waiting = 0;
499 reap_kids();
2f6cbd19 500 }
f27125f9 501
502 run_exec_q = 0;
503 exec_queue_manager();
53921bfa 504 }
53921bfa
KS
505 }
506exit:
507 close(ssock);
53921bfa 508 exit(1);
7fafc032 509}