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