]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevd.c
[PATCH] udevd: don't delay events with TIMEOUT in the environment
[thirdparty/systemd.git] / udevd.c
CommitLineData
7fafc032 1/*
53921bfa 2 * udevd.c - hotplug event serializer
7fafc032 3 *
6c18b1fb 4 * Copyright (C) 2004-2005 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>
7fafc032
KS
24#include <signal.h>
25#include <unistd.h>
26#include <errno.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
085cce37
KS
30#include <ctype.h>
31#include <dirent.h>
32#include <fcntl.h>
138068d6
KS
33#include <sys/select.h>
34#include <sys/wait.h>
e5a2989e 35#include <sys/time.h>
53921bfa
KS
36#include <sys/types.h>
37#include <sys/socket.h>
38#include <sys/un.h>
e5a2989e 39#include <sys/sysinfo.h>
dc117daa 40#include <sys/stat.h>
7fafc032 41
a695feae 42#include "list.h"
63f61c5c 43#include "udev_libc_wrapper.h"
7fafc032 44#include "udev.h"
35b7d88c 45#include "udev_version.h"
9af5bb2f 46#include "udev_utils.h"
7fafc032
KS
47#include "udevd.h"
48#include "logging.h"
49
13f24d59
KS
50/* global variables*/
51static int udevsendsock;
085cce37 52static pid_t sid;
13f24d59 53
f27125f9 54static int pipefds[2];
8b726878 55static long startup_time;
cdc60e8a 56static unsigned long long expected_seqnum = 0;
5cab7caa 57static volatile int sigchilds_waiting;
c8fa2d8b
HH
58static volatile int run_msg_q;
59static volatile int sig_flag;
f27125f9 60static int run_exec_q;
7fafc032 61
4a539daf
KS
62static LIST_HEAD(msg_list);
63static LIST_HEAD(exec_list);
64static LIST_HEAD(running_list);
7fafc032 65
2f6cbd19
KS
66static void exec_queue_manager(void);
67static void msg_queue_manager(void);
f27125f9 68static void user_sighandler(void);
5cab7caa 69static void reap_sigchilds(void);
7d855c31 70char *udev_bin;
7fafc032 71
6c18b1fb 72#ifdef USE_LOG
d026a35d 73void log_message (int level, const char *format, ...)
51a8bb2f 74{
d026a35d
GKH
75 va_list args;
76
77 va_start(args, format);
78 vsyslog(level, format, args);
79 va_end(args);
51a8bb2f 80}
d026a35d 81#endif
51a8bb2f 82
b6bf0b12 83#define msg_dump(msg) \
cdc60e8a 84 dbg("msg_dump: sequence %llu, '%s', '%s', '%s'", \
b6bf0b12
KS
85 msg->seqnum, msg->action, msg->devpath, msg->subsystem);
86
53921bfa 87static void msg_dump_queue(void)
7fafc032 88{
f27125f9 89#ifdef DEBUG
53921bfa 90 struct hotplug_msg *msg;
7fafc032 91
f8a178a3 92 list_for_each_entry(msg, &msg_list, node)
cdc60e8a 93 dbg("sequence %llu in queue", msg->seqnum);
f27125f9 94#endif
35b7d88c
KS
95}
96
2f6cbd19 97static void run_queue_delete(struct hotplug_msg *msg)
8e2229c4 98{
f8a178a3 99 list_del(&msg->node);
2f6cbd19 100 free(msg);
8e2229c4
KS
101}
102
53921bfa
KS
103/* orders the message in the queue by sequence number */
104static void msg_queue_insert(struct hotplug_msg *msg)
35b7d88c 105{
53921bfa 106 struct hotplug_msg *loop_msg;
e5a2989e 107 struct sysinfo info;
35b7d88c 108
021a294c
KS
109 if (msg->seqnum == 0) {
110 dbg("no SEQNUM, move straight to the exec queue");
f8a178a3 111 list_add(&msg->node, &exec_list);
021a294c
KS
112 run_exec_q = 1;
113 return;
114 }
115
7f7ae03a
KS
116 /* don't delay messages with timeout set */
117 if (msg->timeout) {
118 dbg("move seq %llu with timeout %u to exec queue", msg->seqnum, msg->timeout);
119 list_add(&msg->node, &exec_list);
120 run_exec_q = 1;
121 return;
122 }
123
021a294c 124 /* sort message by sequence number into list */
f8a178a3 125 list_for_each_entry_reverse(loop_msg, &msg_list, node) {
f27125f9 126 if (loop_msg->seqnum < msg->seqnum)
53921bfa 127 break;
35b7d88c 128
c2cf4012
KS
129 if (loop_msg->seqnum == msg->seqnum) {
130 dbg("ignoring duplicate message seq %llu", msg->seqnum);
131 return;
132 }
133 }
134
53921bfa 135 /* store timestamp of queuing */
e5a2989e
KS
136 sysinfo(&info);
137 msg->queue_time = info.uptime;
138
f8a178a3 139 list_add(&msg->node, &loop_msg->node);
cdc60e8a 140 dbg("queued message seq %llu", msg->seqnum);
35b7d88c 141
2f6cbd19 142 /* run msg queue manager */
f27125f9 143 run_msg_q = 1;
7fafc032 144
c2cf4012 145 return;
7fafc032
KS
146}
147
53921bfa 148/* forks event and removes event from run queue when finished */
2f6cbd19 149static void udev_run(struct hotplug_msg *msg)
7fafc032 150{
4497fcbf 151 char *const argv[] = { "udev", msg->subsystem, NULL };
90c210eb 152 pid_t pid;
90c210eb
KS
153
154 pid = fork();
155 switch (pid) {
156 case 0:
33db4b8d 157 /* child */
13f24d59 158 close(udevsendsock);
f602ccf0 159 logging_close();
085cce37
KS
160
161 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
4497fcbf 162 execve(udev_bin, argv, msg->envp);
33db4b8d 163 dbg("exec of child failed");
7257cb18 164 _exit(1);
90c210eb
KS
165 break;
166 case -1:
33db4b8d 167 dbg("fork of child failed");
2f6cbd19
KS
168 run_queue_delete(msg);
169 break;
90c210eb 170 default:
2f6cbd19 171 /* get SIGCHLD in main loop */
cdc60e8a 172 dbg("==> exec seq %llu [%d] working at '%s'", msg->seqnum, pid, msg->devpath);
2f6cbd19 173 msg->pid = pid;
90c210eb 174 }
7fafc032
KS
175}
176
085cce37
KS
177static int running_processes(void)
178{
179 int f;
180 static char buf[4096];
181 int len;
182 int running;
183 const char *pos;
184
185 f = open("/proc/stat", O_RDONLY);
186 if (f == -1)
187 return -1;
188
189 len = read(f, buf, sizeof(buf));
190 close(f);
191
192 if (len <= 0)
193 return -1;
194 else
195 buf[len] = '\0';
196
197 pos = strstr(buf, "procs_running ");
198 if (pos == NULL)
199 return -1;
200
201 if (sscanf(pos, "procs_running %u", &running) != 1)
202 return -1;
203
204 return running;
205}
206
207/* return the number of process es in our session, count only until limit */
208static int running_processes_in_session(pid_t session, int limit)
209{
210 DIR *dir;
211 struct dirent *dent;
212 int running = 0;
213
214 dir = opendir("/proc");
215 if (!dir)
216 return -1;
217
218 /* read process info from /proc */
219 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
220 int f;
221 char procdir[64];
222 char line[256];
223 const char *pos;
224 char state;
225 pid_t ppid, pgrp, sess;
226 int len;
227
228 if (!isdigit(dent->d_name[0]))
229 continue;
230
231 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
232 procdir[sizeof(procdir)-1] = '\0';
233
234 f = open(procdir, O_RDONLY);
235 if (f == -1)
236 continue;
237
238 len = read(f, line, sizeof(line));
239 close(f);
240
241 if (len <= 0)
242 continue;
243 else
244 line[len] = '\0';
245
246 /* skip ugly program name */
247 pos = strrchr(line, ')') + 2;
248 if (pos == NULL)
249 continue;
250
251 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
252 continue;
253
254 /* count only processes in our session */
255 if (sess != session)
256 continue;
257
258 /* count only running, no sleeping processes */
259 if (state != 'R')
260 continue;
261
262 running++;
263 if (limit > 0 && running >= limit)
264 break;
265 }
266 closedir(dir);
267
268 return running;
269}
270
7b6571a9
KS
271static int compare_devpath(const char *running, const char *waiting)
272{
273 int i;
274
63f61c5c 275 for (i = 0; i < PATH_SIZE; i++) {
7b6571a9
KS
276 /* identical device event found */
277 if (running[i] == '\0' && waiting[i] == '\0')
278 return 1;
279
280 /* parent device event found */
281 if (running[i] == '\0' && waiting[i] == '/')
282 return 2;
283
284 /* child device event found */
285 if (running[i] == '/' && waiting[i] == '\0')
286 return 3;
287
288 /* no matching event */
289 if (running[i] != waiting[i])
290 break;
291 }
292
293 return 0;
294}
295
296/* returns still running task for the same device, its parent or its physical device */
53921bfa 297static struct hotplug_msg *running_with_devpath(struct hotplug_msg *msg)
7fafc032 298{
53921bfa 299 struct hotplug_msg *loop_msg;
7b6571a9
KS
300
301 if (msg->devpath == NULL)
302 return NULL;
79721e0a 303
7f7ae03a
KS
304 /* skip any events with a timeout set */
305 if (msg->timeout)
306 return NULL;
307
f8a178a3 308 list_for_each_entry(loop_msg, &running_list, node) {
7b6571a9 309 if (loop_msg->devpath == NULL)
80513ea3
KS
310 continue;
311
7b6571a9
KS
312 /* return running parent/child device event */
313 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0)
314 return loop_msg;
79721e0a 315
7b6571a9 316 /* return running physical device event */
79721e0a 317 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
7b6571a9
KS
318 if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0)
319 return loop_msg;
80513ea3
KS
320 }
321
53921bfa 322 return NULL;
7fafc032
KS
323}
324
79721e0a 325/* exec queue management routine executes the events and serializes events in the same sequence */
1ceba936 326static void exec_queue_manager(void)
7fafc032 327{
53921bfa
KS
328 struct hotplug_msg *loop_msg;
329 struct hotplug_msg *tmp_msg;
a695feae 330 struct hotplug_msg *msg;
085cce37
KS
331 int running;
332
333 running = running_processes();
334 dbg("%d processes runnning on system", running);
335 if (running < 0)
336 running = THROTTLE_MAX_RUNNING_CHILDS;
53921bfa 337
f8a178a3 338 list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
085cce37
KS
339 /* check running processes in our session and possibly throttle */
340 if (running >= THROTTLE_MAX_RUNNING_CHILDS) {
341 running = running_processes_in_session(sid, THROTTLE_MAX_RUNNING_CHILDS+10);
342 dbg("%d processes running in session", running);
343 if (running >= THROTTLE_MAX_RUNNING_CHILDS) {
344 dbg("delay seq %llu, cause too many processes already running", loop_msg->seqnum);
345 return;
346 }
347 }
348
2f6cbd19
KS
349 msg = running_with_devpath(loop_msg);
350 if (!msg) {
351 /* move event to run list */
f8a178a3 352 list_move_tail(&loop_msg->node, &running_list);
2f6cbd19 353 udev_run(loop_msg);
085cce37 354 running++;
cdc60e8a 355 dbg("moved seq %llu to running list", loop_msg->seqnum);
2f6cbd19 356 } else {
79721e0a
KS
357 dbg("delay seq %llu (%s), cause seq %llu (%s) is still running",
358 loop_msg->seqnum, loop_msg->devpath, msg->seqnum, msg->devpath);
53921bfa 359 }
53921bfa
KS
360 }
361}
362
2f6cbd19 363static void msg_move_exec(struct hotplug_msg *msg)
86590cd5 364{
f8a178a3 365 list_move_tail(&msg->node, &exec_list);
f27125f9 366 run_exec_q = 1;
2f6cbd19 367 expected_seqnum = msg->seqnum+1;
cdc60e8a 368 dbg("moved seq %llu to exec, next expected is %llu",
2f6cbd19 369 msg->seqnum, expected_seqnum);
86590cd5
KS
370}
371
2f6cbd19 372/* msg queue management routine handles the timeouts and dispatches the events */
1ceba936 373static void msg_queue_manager(void)
53921bfa
KS
374{
375 struct hotplug_msg *loop_msg;
a695feae 376 struct hotplug_msg *tmp_msg;
e5a2989e
KS
377 struct sysinfo info;
378 long msg_age = 0;
8b726878
KS
379 static int timeout = EVENT_INIT_TIMEOUT_SEC;
380 static int init = 1;
a695feae 381
cdc60e8a 382 dbg("msg queue manager, next expected is %llu", expected_seqnum);
a695feae 383recheck:
f8a178a3 384 list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, node) {
2f6cbd19
KS
385 /* move event with expected sequence to the exec list */
386 if (loop_msg->seqnum == expected_seqnum) {
387 msg_move_exec(loop_msg);
388 continue;
a695feae 389 }
35b7d88c 390
8b726878
KS
391 /* see if we are in the initialization phase and wait for the very first events */
392 if (init && (info.uptime - startup_time >= INIT_TIME_SEC)) {
393 init = 0;
394 timeout = EVENT_TIMEOUT_SEC;
395 dbg("initialization phase passed, set timeout to %i seconds", EVENT_TIMEOUT_SEC);
396 }
397
2f6cbd19 398 /* move event with expired timeout to the exec list */
e5a2989e
KS
399 sysinfo(&info);
400 msg_age = info.uptime - loop_msg->queue_time;
cdc60e8a 401 dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age);
8b726878 402 if (msg_age >= timeout) {
2f6cbd19
KS
403 msg_move_exec(loop_msg);
404 goto recheck;
53921bfa 405 } else {
2f6cbd19 406 break;
53921bfa 407 }
2f6cbd19
KS
408 }
409
410 msg_dump_queue();
411
e5a2989e 412 /* set timeout for remaining queued events */
2f6cbd19 413 if (list_empty(&msg_list) == 0) {
8b726878
KS
414 struct itimerval itv = {{0, 0}, {timeout - msg_age, 0}};
415 dbg("next event expires in %li seconds", timeout - msg_age);
1ceba936 416 setitimer(ITIMER_REAL, &itv, NULL);
7fafc032 417 }
7fafc032
KS
418}
419
021a294c
KS
420/* receive the udevsend message and do some sanity checks */
421static struct hotplug_msg *get_udevsend_msg(void)
7fafc032 422{
4a231017 423 static struct udevsend_msg usend_msg;
53921bfa 424 struct hotplug_msg *msg;
4a231017
KS
425 int bufpos;
426 int i;
427 ssize_t size;
0028653c
KS
428 struct msghdr smsg;
429 struct cmsghdr *cmsg;
430 struct iovec iov;
431 struct ucred *cred;
432 char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
4a231017 433 int envbuf_size;
a695feae 434
4a231017
KS
435 memset(&usend_msg, 0x00, sizeof(struct udevsend_msg));
436 iov.iov_base = &usend_msg;
437 iov.iov_len = sizeof(struct udevsend_msg);
0028653c
KS
438
439 memset(&smsg, 0x00, sizeof(struct msghdr));
440 smsg.msg_iov = &iov;
441 smsg.msg_iovlen = 1;
442 smsg.msg_control = cred_msg;
443 smsg.msg_controllen = sizeof(cred_msg);
444
021a294c 445 size = recvmsg(udevsendsock, &smsg, 0);
4a231017 446 if (size < 0) {
2f6cbd19 447 if (errno != EINTR)
021a294c
KS
448 dbg("unable to receive udevsend message");
449 return NULL;
53921bfa 450 }
0028653c
KS
451 cmsg = CMSG_FIRSTHDR(&smsg);
452 cred = (struct ucred *) CMSG_DATA(cmsg);
453
7b1cbec9
KS
454 if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
455 dbg("no sender credentials received, message ignored");
021a294c 456 return NULL;
7b1cbec9
KS
457 }
458
0028653c
KS
459 if (cred->uid != 0) {
460 dbg("sender uid=%i, message ignored", cred->uid);
021a294c 461 return NULL;
4a231017
KS
462 }
463
464 if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
465 dbg("message magic '%s' doesn't match, ignore it", usend_msg.magic);
021a294c 466 return NULL;
0028653c
KS
467 }
468
4a231017
KS
469 envbuf_size = size - offsetof(struct udevsend_msg, envbuf);
470 dbg("envbuf_size=%i", envbuf_size);
471 msg = malloc(sizeof(struct hotplug_msg) + envbuf_size);
021a294c
KS
472 if (msg == NULL)
473 return NULL;
474
4a231017
KS
475 memset(msg, 0x00, sizeof(struct hotplug_msg) + envbuf_size);
476
477 /* copy environment buffer and reconstruct envp */
478 memcpy(msg->envbuf, usend_msg.envbuf, envbuf_size);
479 bufpos = 0;
6f59ed55 480 for (i = 0; (bufpos < envbuf_size) && (i < HOTPLUG_NUM_ENVP-2); i++) {
4a231017
KS
481 int keylen;
482 char *key;
483
484 key = &msg->envbuf[bufpos];
485 keylen = strlen(key);
486 msg->envp[i] = key;
487 bufpos += keylen + 1;
488 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
489
490 /* remember some keys for further processing */
491 if (strncmp(key, "ACTION=", 7) == 0)
492 msg->action = &key[7];
493
494 if (strncmp(key, "DEVPATH=", 8) == 0)
495 msg->devpath = &key[8];
496
497 if (strncmp(key, "SUBSYSTEM=", 10) == 0)
498 msg->subsystem = &key[10];
499
500 if (strncmp(key, "SEQNUM=", 7) == 0)
501 msg->seqnum = strtoull(&key[7], NULL, 10);
79721e0a
KS
502
503 if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
504 msg->physdevpath = &key[12];
7f7ae03a
KS
505
506 if (strncmp(key, "TIMEOUT=", 8) == 0)
507 msg->timeout = strtoull(&key[8], NULL, 10);
53921bfa 508 }
eabfc973 509 msg->envp[i++] = "UDEVD_EVENT=1";
4a231017 510 msg->envp[i] = NULL;
a695feae 511
021a294c 512 return msg;
a695feae 513}
1c5c245e 514
e5a5b54a 515static void asmlinkage sig_handler(int signum)
7fafc032 516{
f27125f9 517 int rc;
47bf9196 518
53921bfa
KS
519 switch (signum) {
520 case SIGINT:
521 case SIGTERM:
53921bfa
KS
522 exit(20 + signum);
523 break;
2f6cbd19 524 case SIGALRM:
f27125f9 525 /* set flag, then write to pipe if needed */
526 run_msg_q = 1;
527 goto do_write;
2f6cbd19
KS
528 break;
529 case SIGCHLD:
f27125f9 530 /* set flag, then write to pipe if needed */
5cab7caa 531 sigchilds_waiting = 1;
f27125f9 532 goto do_write;
2f6cbd19 533 break;
f27125f9 534 }
5a73b25f 535
f27125f9 536do_write:
537 /* if pipe is empty, write to pipe to force select to return
538 * immediately when it gets called
539 */
540 if (!sig_flag) {
541 rc = write(pipefds[1],&signum,sizeof(signum));
5a73b25f 542 if (rc >= 0)
f27125f9 543 sig_flag = 1;
7fafc032 544 }
33db4b8d 545}
7fafc032 546
2f6cbd19
KS
547static void udev_done(int pid)
548{
549 /* find msg associated with pid and delete it */
550 struct hotplug_msg *msg;
551
f8a178a3 552 list_for_each_entry(msg, &running_list, node) {
2f6cbd19 553 if (msg->pid == pid) {
cdc60e8a 554 dbg("<== exec seq %llu came back", msg->seqnum);
2f6cbd19 555 run_queue_delete(msg);
3169e8d1 556
f27125f9 557 /* we want to run the exec queue manager since there may
558 * be events waiting with the devpath of the one that
559 * just finished
560 */
561 run_exec_q = 1;
2f6cbd19
KS
562 return;
563 }
564 }
565}
566
5cab7caa 567static void reap_sigchilds(void)
f27125f9 568{
f27125f9 569 while(1) {
1ceba936 570 int pid = waitpid(-1, NULL, WNOHANG);
f27125f9 571 if ((pid == -1) || (pid == 0))
572 break;
573 udev_done(pid);
574 }
575}
576
577/* just read everything from the pipe and clear the flag,
5cab7caa 578 * the flags was set in the signal handler
f27125f9 579 */
1ceba936 580static void user_sighandler(void)
f27125f9 581{
582 int sig;
ce043f85 583
f27125f9 584 while(1) {
5cab7caa 585 int rc = read(pipefds[0], &sig, sizeof(sig));
f27125f9 586 if (rc < 0)
587 break;
588
589 sig_flag = 0;
590 }
591}
592
c2cf4012 593static int init_udevsend_socket(void)
33db4b8d 594{
53921bfa 595 struct sockaddr_un saddr;
1dadabd7 596 socklen_t addrlen;
5cab7caa 597 const int feature_on = 1;
c2cf4012
KS
598 int retval;
599
600 memset(&saddr, 0x00, sizeof(saddr));
601 saddr.sun_family = AF_LOCAL;
602 /* use abstract namespace for socket path */
603 strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
604 addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
605
606 udevsendsock = socket(AF_LOCAL, SOCK_DGRAM, 0);
607 if (udevsendsock == -1) {
608 dbg("error getting socket, %s", strerror(errno));
609 return -1;
610 }
611
612 /* the bind takes care of ensuring only one copy running */
613 retval = bind(udevsendsock, (struct sockaddr *) &saddr, addrlen);
614 if (retval < 0) {
615 dbg("bind failed, %s", strerror(errno));
616 close(udevsendsock);
617 return -1;
618 }
619
620 /* enable receiving of the sender credentials */
621 setsockopt(udevsendsock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
622
623 return 0;
624}
625
626int main(int argc, char *argv[], char *envp[])
627{
628 struct sysinfo info;
629 int maxsockplus;
630 int retval;
631 int fd;
f8911dbb 632 struct sigaction act;
f27125f9 633 fd_set readfds;
1e266989 634 const char *udevd_expected_seqnum;
53921bfa 635
7257cb18 636 logging_init("udevd");
896e5aa9 637 dbg("version %s", UDEV_VERSION);
95a6f4c8 638
7b1cbec9
KS
639 if (getuid() != 0) {
640 dbg("need to be root, exit");
ec9cc012 641 goto exit;
7b1cbec9 642 }
5cab7caa 643
f15515b5
KS
644 /* daemonize on request */
645 if (argc == 2 && strcmp(argv[1], "-d") == 0) {
646 pid_t pid;
647
648 pid = fork();
649 switch (pid) {
650 case 0:
651 dbg("damonized fork running");
652 break;
653 case -1:
654 dbg("fork of daemon failed");
655 goto exit;
656 default:
657 logging_close();
658 exit(0);
659 }
660 }
661
085cce37
KS
662 /* become session leader */
663 sid = setsid();
664 dbg("our session is %d", sid);
665
5cab7caa 666 /* make sure we don't lock any path */
c8fa2d8b 667 chdir("/");
5cab7caa
KS
668 umask(umask(077) | 022);
669
085cce37
KS
670 /*set a reasonable scheduling priority for the daemon */
671 setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
672
c8fa2d8b
HH
673 /* Set fds to dev/null */
674 fd = open( "/dev/null", O_RDWR );
ce043f85
KS
675 if (fd >= 0) {
676 dup2(fd, 0);
677 dup2(fd, 1);
678 dup2(fd, 2);
679 if (fd > 2)
680 close(fd);
681 } else
c8fa2d8b 682 dbg("error opening /dev/null %s", strerror(errno));
5cab7caa 683
f27125f9 684 /* setup signal handler pipe */
e5a2989e
KS
685 retval = pipe(pipefds);
686 if (retval < 0) {
687 dbg("error getting pipes: %s", strerror(errno));
ec9cc012 688 goto exit;
e5a2989e
KS
689 }
690
691 retval = fcntl(pipefds[0], F_SETFL, O_NONBLOCK);
c8fa2d8b
HH
692 if (retval < 0) {
693 dbg("error fcntl on read pipe: %s", strerror(errno));
ec9cc012 694 goto exit;
c8fa2d8b
HH
695 }
696 retval = fcntl(pipefds[0], F_SETFD, FD_CLOEXEC);
ce043f85 697 if (retval < 0)
e5a2989e 698 dbg("error fcntl on read pipe: %s", strerror(errno));
e5a2989e
KS
699
700 retval = fcntl(pipefds[1], F_SETFL, O_NONBLOCK);
701 if (retval < 0) {
702 dbg("error fcntl on write pipe: %s", strerror(errno));
ec9cc012 703 goto exit;
e5a2989e 704 }
c8fa2d8b 705 retval = fcntl(pipefds[1], F_SETFD, FD_CLOEXEC);
ce043f85 706 if (retval < 0)
c8fa2d8b 707 dbg("error fcntl on write pipe: %s", strerror(errno));
f27125f9 708
709 /* set signal handlers */
0786e8e5 710 memset(&act, 0x00, sizeof(struct sigaction));
dc117daa 711 act.sa_handler = (void (*) (int))sig_handler;
f27125f9 712 sigemptyset(&act.sa_mask);
f8911dbb
KS
713 act.sa_flags = SA_RESTART;
714 sigaction(SIGINT, &act, NULL);
715 sigaction(SIGTERM, &act, NULL);
f8911dbb
KS
716 sigaction(SIGALRM, &act, NULL);
717 sigaction(SIGCHLD, &act, NULL);
7fafc032 718
c2cf4012
KS
719 if (init_udevsend_socket() < 0) {
720 if (errno == EADDRINUSE)
ce043f85 721 dbg("another udevd running, exit");
c2cf4012
KS
722 else
723 dbg("error initialising udevsend socket: %s", strerror(errno));
53921bfa 724
53921bfa
KS
725 goto exit;
726 }
727
7d855c31
KS
728 /* possible override of udev binary, used for testing */
729 udev_bin = getenv("UDEV_BIN");
730 if (udev_bin != NULL)
731 dbg("udev binary is set to '%s'", udev_bin);
732 else
733 udev_bin = UDEV_BIN;
0028653c 734
085cce37 735 /* possible init of expected_seqnum value */
1e266989
KS
736 udevd_expected_seqnum = getenv("UDEVD_EXPECTED_SEQNUM");
737 if (udevd_expected_seqnum != NULL) {
738 expected_seqnum = strtoull(udevd_expected_seqnum, NULL, 10);
739 dbg("initialize expected_seqnum to %llu", expected_seqnum);
740 }
741
085cce37 742 /* get current time to provide shorter timeout on startup */
8b726878
KS
743 sysinfo(&info);
744 startup_time = info.uptime;
745
e5a2989e 746 FD_ZERO(&readfds);
13f24d59 747 FD_SET(udevsendsock, &readfds);
e5a2989e 748 FD_SET(pipefds[0], &readfds);
13f24d59 749 maxsockplus = udevsendsock+1;
2f6cbd19 750 while (1) {
021a294c
KS
751 struct hotplug_msg *msg;
752
f27125f9 753 fd_set workreadfds = readfds;
754 retval = select(maxsockplus, &workreadfds, NULL, NULL, NULL);
e5a2989e 755
f27125f9 756 if (retval < 0) {
e5a2989e
KS
757 if (errno != EINTR)
758 dbg("error in select: %s", strerror(errno));
f27125f9 759 continue;
2f6cbd19 760 }
e5a2989e 761
021a294c
KS
762 if (FD_ISSET(udevsendsock, &workreadfds)) {
763 msg = get_udevsend_msg();
764 if (msg)
765 msg_queue_insert(msg);
766 }
e5a2989e 767
f27125f9 768 if (FD_ISSET(pipefds[0], &workreadfds))
769 user_sighandler();
e5a2989e 770
5cab7caa
KS
771 if (sigchilds_waiting) {
772 sigchilds_waiting = 0;
773 reap_sigchilds();
f27125f9 774 }
e5a2989e 775
f27125f9 776 if (run_msg_q) {
777 run_msg_q = 0;
778 msg_queue_manager();
779 }
e5a2989e 780
f27125f9 781 if (run_exec_q) {
5cab7caa
KS
782 /* clean up running_list before calling exec_queue_manager() */
783 if (sigchilds_waiting) {
784 sigchilds_waiting = 0;
785 reap_sigchilds();
2f6cbd19 786 }
f27125f9 787
788 run_exec_q = 0;
789 exec_queue_manager();
53921bfa 790 }
53921bfa 791 }
ec9cc012 792
53921bfa 793exit:
7257cb18 794 logging_close();
5cab7caa 795 return 1;
7fafc032 796}