]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevd.c
[PATCH] make udev-test.pl test for root permissions before running
[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 <sys/sysinfo.h>
dc117daa 37#include <sys/stat.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;
5cab7caa 48static volatile int sigchilds_waiting;
c8fa2d8b
HH
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);
5cab7caa 60static void reap_sigchilds(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 151 dbg("exec of child failed");
7257cb18 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 178/* exec queue management routine executes the events and delays events for the same devpath */
1ceba936 179static void exec_queue_manager(void)
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 208/* msg queue management routine handles the timeouts and dispatches the events */
1ceba936 209static void msg_queue_manager(void)
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);
1ceba936 243 setitimer(ITIMER_REAL, &itv, NULL);
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
e5a5b54a 311static void asmlinkage 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 */
5cab7caa 327 sigchilds_waiting = 1;
f27125f9 328 goto do_write;
2f6cbd19 329 break;
f27125f9 330 }
5a73b25f 331
f27125f9 332do_write:
333 /* if pipe is empty, write to pipe to force select to return
334 * immediately when it gets called
335 */
336 if (!sig_flag) {
337 rc = write(pipefds[1],&signum,sizeof(signum));
5a73b25f 338 if (rc >= 0)
f27125f9 339 sig_flag = 1;
7fafc032 340 }
33db4b8d 341}
7fafc032 342
2f6cbd19
KS
343static void udev_done(int pid)
344{
345 /* find msg associated with pid and delete it */
346 struct hotplug_msg *msg;
347
348 list_for_each_entry(msg, &running_list, list) {
349 if (msg->pid == pid) {
cdc60e8a 350 dbg("<== exec seq %llu came back", msg->seqnum);
2f6cbd19 351 run_queue_delete(msg);
3169e8d1 352
f27125f9 353 /* we want to run the exec queue manager since there may
354 * be events waiting with the devpath of the one that
355 * just finished
356 */
357 run_exec_q = 1;
2f6cbd19
KS
358 return;
359 }
360 }
361}
362
5cab7caa 363static void reap_sigchilds(void)
f27125f9 364{
f27125f9 365 while(1) {
1ceba936 366 int pid = waitpid(-1, NULL, WNOHANG);
f27125f9 367 if ((pid == -1) || (pid == 0))
368 break;
369 udev_done(pid);
370 }
371}
372
373/* just read everything from the pipe and clear the flag,
5cab7caa 374 * the flags was set in the signal handler
f27125f9 375 */
1ceba936 376static void user_sighandler(void)
f27125f9 377{
378 int sig;
379 while(1) {
5cab7caa 380 int rc = read(pipefds[0], &sig, sizeof(sig));
f27125f9 381 if (rc < 0)
382 break;
383
384 sig_flag = 0;
385 }
386}
387
5cab7caa 388int main(int argc, char *argv[], char *envp[])
33db4b8d 389{
f27125f9 390 int ssock, maxsockplus;
53921bfa 391 struct sockaddr_un saddr;
1dadabd7 392 socklen_t addrlen;
c8fa2d8b 393 int retval, fd;
5cab7caa 394 const int feature_on = 1;
f8911dbb 395 struct sigaction act;
f27125f9 396 fd_set readfds;
53921bfa 397
7257cb18 398 logging_init("udevd");
896e5aa9 399 dbg("version %s", UDEV_VERSION);
95a6f4c8 400
7b1cbec9
KS
401 if (getuid() != 0) {
402 dbg("need to be root, exit");
5cab7caa 403 _exit(1);
7b1cbec9 404 }
5cab7caa
KS
405
406 /* make sure we don't lock any path */
c8fa2d8b 407 chdir("/");
5cab7caa
KS
408 umask(umask(077) | 022);
409
c8fa2d8b
HH
410 /* Set fds to dev/null */
411 fd = open( "/dev/null", O_RDWR );
412 if ( fd < 0 ) {
413 dbg("error opening /dev/null %s", strerror(errno));
414 exit(1);
415 }
416 dup2(fd, 0);
417 dup2(fd, 1);
418 dup2(fd, 2);
419 if (fd > 2)
420 close(fd);
5cab7caa
KS
421
422 /* become session leader */
c8fa2d8b 423 setsid();
e5a2989e 424
f27125f9 425 /* setup signal handler pipe */
e5a2989e
KS
426 retval = pipe(pipefds);
427 if (retval < 0) {
428 dbg("error getting pipes: %s", strerror(errno));
429 exit(1);
430 }
431
432 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
c8fa2d8b
HH
433 if (retval < 0) {
434 dbg("error fcntl on read pipe: %s", strerror(errno));
435 exit(1);
436 }
437 retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC);
438 if (retval < 0) {
e5a2989e
KS
439 dbg("error fcntl on read pipe: %s", strerror(errno));
440 exit(1);
441 }
442
443 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
444 if (retval < 0) {
445 dbg("error fcntl on write pipe: %s", strerror(errno));
446 exit(1);
447 }
c8fa2d8b
HH
448 retval = fcntl(pipefds[1], F_SETFD, FD_CLOEXEC);
449 if (retval < 0) {
450 dbg("error fcntl on write pipe: %s", strerror(errno));
451 exit(1);
452 }
f27125f9 453
454 /* set signal handlers */
dc117daa 455 act.sa_handler = (void (*) (int))sig_handler;
f27125f9 456 sigemptyset(&act.sa_mask);
f8911dbb
KS
457 act.sa_flags = SA_RESTART;
458 sigaction(SIGINT, &act, NULL);
459 sigaction(SIGTERM, &act, NULL);
f8911dbb
KS
460 sigaction(SIGALRM, &act, NULL);
461 sigaction(SIGCHLD, &act, NULL);
7fafc032 462
53921bfa
KS
463 memset(&saddr, 0x00, sizeof(saddr));
464 saddr.sun_family = AF_LOCAL;
872344c4
KS
465 /* use abstract namespace for socket path */
466 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
1dadabd7 467 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
53921bfa 468
2f6cbd19 469 ssock = socket(AF_LOCAL, SOCK_DGRAM, 0);
53921bfa 470 if (ssock == -1) {
7b1cbec9 471 dbg("error getting socket, exit");
53921bfa
KS
472 exit(1);
473 }
474
6e3e3c34
HH
475 set_cloexec_flag(ssock, 1);
476
2f6cbd19 477 /* the bind takes care of ensuring only one copy running */
f8911dbb 478 retval = bind(ssock, (struct sockaddr *) &saddr, addrlen);
53921bfa 479 if (retval < 0) {
7b1cbec9 480 dbg("bind failed, exit");
53921bfa
KS
481 goto exit;
482 }
c8fa2d8b
HH
483 retval = fcntl(ssock, F_SETFD, FD_CLOEXEC);
484 if (retval < 0) {
485 dbg("error fcntl on ssock: %s", strerror(errno));
486 exit(1);
487 }
53921bfa 488
0028653c 489 /* enable receiving of the sender credentials */
5cab7caa 490 setsockopt(ssock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
7d855c31
KS
491
492 /* possible override of udev binary, used for testing */
493 udev_bin = getenv("UDEV_BIN");
494 if (udev_bin != NULL)
495 dbg("udev binary is set to '%s'", udev_bin);
496 else
497 udev_bin = UDEV_BIN;
0028653c 498
e5a2989e
KS
499 FD_ZERO(&readfds);
500 FD_SET(ssock, &readfds);
501 FD_SET(pipefds[0], &readfds);
f27125f9 502 maxsockplus = ssock+1;
2f6cbd19 503 while (1) {
f27125f9 504 fd_set workreadfds = readfds;
505 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
e5a2989e 506
f27125f9 507 if (retval < 0) {
e5a2989e
KS
508 if (errno != EINTR)
509 dbg("error in select: %s", strerror(errno));
f27125f9 510 continue;
2f6cbd19 511 }
e5a2989e 512
f27125f9 513 if (FD_ISSET(ssock, &workreadfds))
514 handle_msg(ssock);
e5a2989e 515
f27125f9 516 if (FD_ISSET(pipefds[0], &workreadfds))
517 user_sighandler();
e5a2989e 518
5cab7caa
KS
519 if (sigchilds_waiting) {
520 sigchilds_waiting = 0;
521 reap_sigchilds();
f27125f9 522 }
e5a2989e 523
f27125f9 524 if (run_msg_q) {
525 run_msg_q = 0;
526 msg_queue_manager();
527 }
e5a2989e 528
f27125f9 529 if (run_exec_q) {
5cab7caa
KS
530 /* clean up running_list before calling exec_queue_manager() */
531 if (sigchilds_waiting) {
532 sigchilds_waiting = 0;
533 reap_sigchilds();
2f6cbd19 534 }
f27125f9 535
536 run_exec_q = 0;
537 exec_queue_manager();
53921bfa 538 }
53921bfa
KS
539 }
540exit:
541 close(ssock);
7257cb18 542 logging_close();
5cab7caa 543 return 1;
7fafc032 544}