]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevd.c
[PATCH] add dbus.dev, pam_console.dev and selinux.dev files for /etc/dev.d/default...
[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);
7d855c31 63char *udev_bin;
7fafc032 64
d026a35d 65#ifdef LOG
d00bd172 66unsigned char logname[LOGNAME_SIZE];
d026a35d 67void log_message (int level, const char *format, ...)
51a8bb2f 68{
d026a35d
GKH
69 va_list args;
70
71 va_start(args, format);
72 vsyslog(level, format, args);
73 va_end(args);
51a8bb2f 74}
d026a35d 75#endif
51a8bb2f 76
b6bf0b12
KS
77#define msg_dump(msg) \
78 dbg("msg_dump: sequence %d, '%s', '%s', '%s'", \
79 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
80
53921bfa 81static void msg_dump_queue(void)
7fafc032 82{
f27125f9 83#ifdef DEBUG
53921bfa 84 struct hotplug_msg *msg;
7fafc032 85
53921bfa
KS
86 list_for_each_entry(msg, &msg_list, list)
87 dbg("sequence %d in queue", msg->seqnum);
f27125f9 88#endif
35b7d88c
KS
89}
90
53921bfa 91static struct hotplug_msg *msg_create(void)
35b7d88c 92{
53921bfa 93 struct hotplug_msg *new_msg;
35b7d88c 94
53921bfa 95 new_msg = malloc(sizeof(struct hotplug_msg));
2f6cbd19 96 if (new_msg == NULL)
53921bfa 97 dbg("error malloc");
53921bfa 98 return new_msg;
35b7d88c
KS
99}
100
2f6cbd19 101static void run_queue_delete(struct hotplug_msg *msg)
8e2229c4 102{
2f6cbd19
KS
103 list_del(&msg->list);
104 free(msg);
8e2229c4
KS
105}
106
53921bfa
KS
107/* orders the message in the queue by sequence number */
108static void msg_queue_insert(struct hotplug_msg *msg)
35b7d88c 109{
53921bfa 110 struct hotplug_msg *loop_msg;
e5a2989e 111 struct sysinfo info;
35b7d88c 112
f27125f9 113 /* sort message by sequence number into list. events
114 * will tend to come in order, so scan the list backwards
115 */
116 list_for_each_entry_reverse(loop_msg, &msg_list, list)
117 if (loop_msg->seqnum < msg->seqnum)
53921bfa 118 break;
35b7d88c 119
53921bfa 120 /* store timestamp of queuing */
e5a2989e
KS
121 sysinfo(&info);
122 msg->queue_time = info.uptime;
123
124 list_add(&msg->list, &loop_msg->list);
125 dbg("queued message seq %d", msg->seqnum);
35b7d88c 126
2f6cbd19 127 /* run msg queue manager */
f27125f9 128 run_msg_q = 1;
7fafc032 129
53921bfa 130 return ;
7fafc032
KS
131}
132
53921bfa 133/* forks event and removes event from run queue when finished */
2f6cbd19 134static void udev_run(struct hotplug_msg *msg)
7fafc032 135{
90c210eb 136 pid_t pid;
e964c2c0
KS
137 char action[ACTION_SIZE];
138 char devpath[DEVPATH_SIZE];
f8911dbb
KS
139 char *env[] = { action, devpath, NULL };
140
9b28a52a
KS
141 strcpy(action, "ACTION=");
142 strfieldcat(action, msg->action);
143 strcpy(devpath, "DEVPATH=");
144 strfieldcat(devpath, msg->devpath);
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
KS
162 /* get SIGCHLD in main loop */
163 dbg("==> exec seq %d [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
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);
191 dbg("moved seq %d to running list", loop_msg->seqnum);
192 } else {
193 dbg("delay seq %d, cause seq %d already working on '%s'",
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
KS
203 expected_seqnum = msg->seqnum+1;
204 dbg("moved seq %d to exec, next expected is %d",
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
2f6cbd19 216 dbg("msg queue manager, next expected is %d", 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;
228 dbg("seq %d 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 */
2f6cbd19 298 if (msg->seqnum == -1) {
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
53921bfa 311static void sig_handler(int signum)
7fafc032 312{
f27125f9 313 int rc;
53921bfa
KS
314 switch (signum) {
315 case SIGINT:
316 case SIGTERM:
53921bfa
KS
317 exit(20 + signum);
318 break;
2f6cbd19 319 case SIGALRM:
f27125f9 320 /* set flag, then write to pipe if needed */
321 run_msg_q = 1;
322 goto do_write;
2f6cbd19
KS
323 break;
324 case SIGCHLD:
f27125f9 325 /* set flag, then write to pipe if needed */
2f6cbd19 326 children_waiting = 1;
f27125f9 327 goto do_write;
2f6cbd19 328 break;
53921bfa
KS
329 default:
330 dbg("unhandled signal");
f27125f9 331 return;
332 }
333
334do_write:
335 /* if pipe is empty, write to pipe to force select to return
336 * immediately when it gets called
337 */
338 if (!sig_flag) {
339 rc = write(pipefds[1],&signum,sizeof(signum));
340 if (rc < 0)
341 dbg("unable to write to pipe");
342 else
343 sig_flag = 1;
7fafc032 344 }
33db4b8d 345}
7fafc032 346
2f6cbd19
KS
347static void udev_done(int pid)
348{
349 /* find msg associated with pid and delete it */
350 struct hotplug_msg *msg;
351
352 list_for_each_entry(msg, &running_list, list) {
353 if (msg->pid == pid) {
354 dbg("<== exec seq %d came back", msg->seqnum);
355 run_queue_delete(msg);
f27125f9 356
357 /* we want to run the exec queue manager since there may
358 * be events waiting with the devpath of the one that
359 * just finished
360 */
361 run_exec_q = 1;
2f6cbd19
KS
362 return;
363 }
364 }
365}
366
f27125f9 367static void reap_kids()
368{
369 /* reap all dead children */
370 while(1) {
371 int pid = waitpid(-1, 0, WNOHANG);
372 if ((pid == -1) || (pid == 0))
373 break;
374 udev_done(pid);
375 }
376}
377
378/* just read everything from the pipe and clear the flag,
379 * the useful flags were set in the signal handler
380 */
381static void user_sighandler()
382{
383 int sig;
384 while(1) {
385 int rc = read(pipefds[0],&sig,sizeof(sig));
386 if (rc < 0)
387 break;
388
389 sig_flag = 0;
390 }
391}
392
393
33db4b8d
KS
394int main(int argc, char *argv[])
395{
f27125f9 396 int ssock, maxsockplus;
53921bfa 397 struct sockaddr_un saddr;
1dadabd7 398 socklen_t addrlen;
53921bfa 399 int retval;
0028653c 400 const int on = 1;
f8911dbb 401 struct sigaction act;
f27125f9 402 fd_set readfds;
53921bfa 403
95a6f4c8 404 init_logging("udevd");
896e5aa9 405 dbg("version %s", UDEV_VERSION);
95a6f4c8 406
7b1cbec9
KS
407 if (getuid() != 0) {
408 dbg("need to be root, exit");
409 exit(1);
410 }
e5a2989e 411
f27125f9 412 /* setup signal handler pipe */
e5a2989e
KS
413 retval = pipe(pipefds);
414 if (retval < 0) {
415 dbg("error getting pipes: %s", strerror(errno));
416 exit(1);
417 }
418
419 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
420 if (retval < 0) {
421 dbg("error fcntl on read pipe: %s", strerror(errno));
422 exit(1);
423 }
424
425 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
426 if (retval < 0) {
427 dbg("error fcntl on write pipe: %s", strerror(errno));
428 exit(1);
429 }
f27125f9 430
431 /* set signal handlers */
f8911dbb 432 act.sa_handler = sig_handler;
f27125f9 433 sigemptyset(&act.sa_mask);
f8911dbb
KS
434 act.sa_flags = SA_RESTART;
435 sigaction(SIGINT, &act, NULL);
436 sigaction(SIGTERM, &act, NULL);
f8911dbb
KS
437 sigaction(SIGALRM, &act, NULL);
438 sigaction(SIGCHLD, &act, NULL);
7fafc032 439
53921bfa
KS
440 memset(&saddr, 0x00, sizeof(saddr));
441 saddr.sun_family = AF_LOCAL;
872344c4
KS
442 /* use abstract namespace for socket path */
443 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
1dadabd7 444 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
53921bfa 445
2f6cbd19 446 ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
53921bfa 447 if (ssock == -1) {
7b1cbec9 448 dbg("error getting socket, exit");
53921bfa
KS
449 exit(1);
450 }
451
2f6cbd19 452 /* the bind takes care of ensuring only one copy running */
f8911dbb 453 retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
53921bfa 454 if (retval < 0) {
7b1cbec9 455 dbg("bind failed, exit");
53921bfa
KS
456 goto exit;
457 }
458
0028653c
KS
459 /* enable receiving of the sender credentials */
460 setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
7d855c31
KS
461
462 /* possible override of udev binary, used for testing */
463 udev_bin = getenv("UDEV_BIN");
464 if (udev_bin != NULL)
465 dbg("udev binary is set to '%s'", udev_bin);
466 else
467 udev_bin = UDEV_BIN;
0028653c 468
e5a2989e
KS
469 FD_ZERO(&readfds);
470 FD_SET(ssock, &readfds);
471 FD_SET(pipefds[0], &readfds);
f27125f9 472 maxsockplus = ssock+1;
2f6cbd19 473 while (1) {
f27125f9 474 fd_set workreadfds = readfds;
475 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
e5a2989e 476
f27125f9 477 if (retval < 0) {
e5a2989e
KS
478 if (errno != EINTR)
479 dbg("error in select: %s", strerror(errno));
f27125f9 480 continue;
2f6cbd19 481 }
e5a2989e 482
f27125f9 483 if (FD_ISSET(ssock, &workreadfds))
484 handle_msg(ssock);
e5a2989e 485
f27125f9 486 if (FD_ISSET(pipefds[0], &workreadfds))
487 user_sighandler();
e5a2989e 488
f27125f9 489 if (children_waiting) {
2f6cbd19 490 children_waiting = 0;
f27125f9 491 reap_kids();
492 }
e5a2989e 493
f27125f9 494 if (run_msg_q) {
495 run_msg_q = 0;
496 msg_queue_manager();
497 }
e5a2989e 498
f27125f9 499 if (run_exec_q) {
f27125f9 500 /* this is tricky. exec_queue_manager() loops over exec_list, and
501 * calls running_with_devpath(), which loops over running_list. This gives
502 * O(N*M), which can get *nasty*. Clean up running_list before
503 * calling exec_queue_manager().
504 */
f27125f9 505 if (children_waiting) {
506 children_waiting = 0;
507 reap_kids();
2f6cbd19 508 }
f27125f9 509
510 run_exec_q = 0;
511 exec_queue_manager();
53921bfa 512 }
53921bfa
KS
513 }
514exit:
515 close(ssock);
53921bfa 516 exit(1);
7fafc032 517}