chiark / gitweb /
FAQ: remove confusing statement about module loading
[elogind.git] / udevd.c
1 /*
2  * udevd.c - event listener and serializer
3  *
4  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
5  * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
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
23 #include <stddef.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <dirent.h>
32 #include <fcntl.h>
33 #include <syslog.h>
34 #include <sys/select.h>
35 #include <sys/wait.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/un.h>
40 #include <sys/sysinfo.h>
41 #include <sys/stat.h>
42 #include <linux/types.h>
43 #include <linux/netlink.h>
44
45 #include "list.h"
46 #include "udev_libc_wrapper.h"
47 #include "udev.h"
48 #include "udev_version.h"
49 #include "udev_utils.h"
50 #include "udevd.h"
51 #include "logging.h"
52
53 /* global variables*/
54 static int udevd_sock;
55 static int uevent_netlink_sock;
56 static pid_t sid;
57
58 static int signal_pipe[2] = {-1, -1};
59 static volatile int sigchilds_waiting;
60 static volatile int run_msg_q;
61 static volatile int udev_exit;
62 static int init_phase = 1;
63 static int run_exec_q;
64 static int stop_exec_q;
65 static char *udev_bin;
66 static int event_timeout;
67 static int max_childs;
68 static int max_childs_running;
69 static unsigned long long expected_seqnum;
70 static char log[32];
71
72 static LIST_HEAD(msg_list);
73 static LIST_HEAD(exec_list);
74 static LIST_HEAD(running_list);
75
76
77 #ifdef USE_LOG
78 void log_message (int priority, const char *format, ...)
79 {
80         va_list args;
81
82         if (priority > udev_log_priority)
83                 return;
84
85         va_start(args, format);
86         vsyslog(priority, format, args);
87         va_end(args);
88 }
89 #endif
90
91 static void msg_dump_queue(void)
92 {
93 #ifdef DEBUG
94         struct uevent_msg *msg;
95
96         list_for_each_entry(msg, &msg_list, node)
97                 dbg("sequence %llu in queue", msg->seqnum);
98 #endif
99 }
100
101 static void msg_queue_delete(struct uevent_msg *msg)
102 {
103         list_del(&msg->node);
104         free(msg);
105 }
106
107 /* orders the message in the queue by sequence number */
108 static void msg_queue_insert(struct uevent_msg *msg)
109 {
110         struct uevent_msg *loop_msg;
111         struct sysinfo info;
112
113         if (msg->seqnum == 0) {
114                 dbg("no SEQNUM, move straight to the exec queue");
115                 list_add_tail(&msg->node, &exec_list);
116                 run_exec_q = 1;
117                 return;
118         }
119
120         /* store timestamp of queuing */
121         sysinfo(&info);
122         msg->queue_time = info.uptime;
123
124         /* with the first event we provide a phase of shorter timeout */
125         if (init_phase) {
126                 static long init_time;
127
128                 if (init_time == 0)
129                         init_time = info.uptime;
130                 if (info.uptime - init_time >= UDEVD_INIT_TIME)
131                         init_phase = 0;
132         }
133
134         /* sort message by sequence number into list */
135         list_for_each_entry_reverse(loop_msg, &msg_list, node) {
136                 if (loop_msg->seqnum < msg->seqnum)
137                         break;
138
139                 if (loop_msg->seqnum == msg->seqnum) {
140                         dbg("ignoring duplicate message seq %llu", msg->seqnum);
141                         free(msg);
142                         return;
143                 }
144         }
145         list_add(&msg->node, &loop_msg->node);
146         info("seq %llu queued, '%s' '%s'", msg->seqnum, msg->action, msg->devpath);
147
148         /* run msg queue manager */
149         run_msg_q = 1;
150
151         return;
152 }
153
154 /* forks event and removes event from run queue when finished */
155 static void udev_event_run(struct uevent_msg *msg)
156 {
157         char *const argv[] = { "udev", msg->subsystem, NULL };
158         pid_t pid;
159         struct sysinfo info;
160
161         pid = fork();
162         switch (pid) {
163         case 0:
164                 /* child */
165                 if (uevent_netlink_sock != -1)
166                         close(uevent_netlink_sock);
167                 close(udevd_sock);
168                 logging_close();
169                 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
170                 execve(udev_bin, argv, msg->envp);
171                 err("exec of child failed");
172                 _exit(1);
173         case -1:
174                 err("fork of child failed");
175                 msg_queue_delete(msg);
176                 break;
177         default:
178                 /* get SIGCHLD in main loop */
179                 sysinfo(&info);
180                 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
181                      msg->seqnum, pid,  msg->action, msg->subsystem, info.uptime - msg->queue_time);
182                 msg->pid = pid;
183         }
184 }
185
186 static int running_processes(void)
187 {
188         int f;
189         static char buf[4096];
190         int len;
191         int running;
192         const char *pos;
193
194         f = open("/proc/stat", O_RDONLY);
195         if (f == -1)
196                 return -1;
197
198         len = read(f, buf, sizeof(buf)-1);
199         close(f);
200
201         if (len <= 0)
202                 return -1;
203         else
204                 buf[len] = '\0';
205
206         pos = strstr(buf, "procs_running ");
207         if (pos == NULL)
208                 return -1;
209
210         if (sscanf(pos, "procs_running %u", &running) != 1)
211                 return -1;
212
213         return running;
214 }
215
216 /* return the number of process es in our session, count only until limit */
217 static int running_processes_in_session(pid_t session, int limit)
218 {
219         DIR *dir;
220         struct dirent *dent;
221         int running = 0;
222
223         dir = opendir("/proc");
224         if (!dir)
225                 return -1;
226
227         /* read process info from /proc */
228         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
229                 int f;
230                 char procdir[64];
231                 char line[256];
232                 const char *pos;
233                 char state;
234                 pid_t ppid, pgrp, sess;
235                 int len;
236
237                 if (!isdigit(dent->d_name[0]))
238                         continue;
239
240                 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
241                 procdir[sizeof(procdir)-1] = '\0';
242
243                 f = open(procdir, O_RDONLY);
244                 if (f == -1)
245                         continue;
246
247                 len = read(f, line, sizeof(line)-1);
248                 close(f);
249
250                 if (len <= 0)
251                         continue;
252                 else
253                         line[len] = '\0';
254
255                 /* skip ugly program name */
256                 pos = strrchr(line, ')') + 2;
257                 if (pos == NULL)
258                         continue;
259
260                 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
261                         continue;
262
263                 /* count only processes in our session */
264                 if (sess != session)
265                         continue;
266
267                 /* count only running, no sleeping processes */
268                 if (state != 'R')
269                         continue;
270
271                 running++;
272                 if (limit > 0 && running >= limit)
273                         break;
274         }
275         closedir(dir);
276
277         return running;
278 }
279
280 static int compare_devpath(const char *running, const char *waiting)
281 {
282         int i;
283
284         for (i = 0; i < PATH_SIZE; i++) {
285                 /* identical device event found */
286                 if (running[i] == '\0' && waiting[i] == '\0')
287                         return 1;
288
289                 /* parent device event found */
290                 if (running[i] == '\0' && waiting[i] == '/')
291                         return 2;
292
293                 /* child device event found */
294                 if (running[i] == '/' && waiting[i] == '\0')
295                         return 3;
296
297                 /* no matching event */
298                 if (running[i] != waiting[i])
299                         break;
300         }
301
302         return 0;
303 }
304
305 /* returns still running task for the same device, its parent or its physical device */
306 static int running_with_devpath(struct uevent_msg *msg, int limit)
307 {
308         struct uevent_msg *loop_msg;
309         int childs_count = 0;
310
311         if (msg->devpath == NULL)
312                 return 0;
313
314         /* skip any events with a timeout set */
315         if (msg->timeout != 0)
316                 return 0;
317
318         list_for_each_entry(loop_msg, &running_list, node) {
319                 if (limit && childs_count++ > limit) {
320                         dbg("%llu, maximum number (%i) of child reached", msg->seqnum, childs_count);
321                         return 1;
322                 }
323                 if (loop_msg->devpath == NULL)
324                         continue;
325
326                 /* return running parent/child device event */
327                 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
328                         dbg("%llu, child device event still running %llu (%s)",
329                             msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
330                         return 2;
331                 }
332
333                 /* return running physical device event */
334                 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
335                         if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
336                                 dbg("%llu, physical device event still running %llu (%s)",
337                                     msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
338                                 return 3;
339                         }
340         }
341
342         return 0;
343 }
344
345 /* exec queue management routine executes the events and serializes events in the same sequence */
346 static void exec_queue_manager(void)
347 {
348         struct uevent_msg *loop_msg;
349         struct uevent_msg *tmp_msg;
350         int running;
351
352         if (list_empty(&exec_list))
353                 return;
354
355         running = running_processes();
356         dbg("%d processes runnning on system", running);
357         if (running < 0)
358                 running = max_childs_running;
359
360         list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
361                 /* check running processes in our session and possibly throttle */
362                 if (running >= max_childs_running) {
363                         running = running_processes_in_session(sid, max_childs_running+10);
364                         dbg("at least %d processes running in session", running);
365                         if (running >= max_childs_running) {
366                                 dbg("delay seq %llu, cause too many processes already running",
367                                     loop_msg->seqnum);
368                                 return;
369                         }
370                 }
371
372                 if (running_with_devpath(loop_msg, max_childs) == 0) {
373                         /* move event to run list */
374                         list_move_tail(&loop_msg->node, &running_list);
375                         udev_event_run(loop_msg);
376                         running++;
377                         dbg("moved seq %llu to running list", loop_msg->seqnum);
378                 } else
379                         dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
380         }
381 }
382
383 static void msg_move_exec(struct uevent_msg *msg)
384 {
385         list_move_tail(&msg->node, &exec_list);
386         run_exec_q = 1;
387         expected_seqnum = msg->seqnum+1;
388         dbg("moved seq %llu to exec, next expected is %llu",
389                 msg->seqnum, expected_seqnum);
390 }
391
392 /* msg queue management routine handles the timeouts and dispatches the events */
393 static void msg_queue_manager(void)
394 {
395         struct uevent_msg *loop_msg;
396         struct uevent_msg *tmp_msg;
397         struct sysinfo info;
398         long msg_age = 0;
399         int timeout = event_timeout;
400
401         dbg("msg queue manager, next expected is %llu", expected_seqnum);
402 recheck:
403         sysinfo(&info);
404         list_for_each_entry_safe(loop_msg, tmp_msg, &msg_list, node) {
405                 /* move event with expected sequence to the exec list */
406                 if (loop_msg->seqnum == expected_seqnum) {
407                         msg_move_exec(loop_msg);
408                         continue;
409                 }
410
411                 /* limit timeout during initialization phase */
412                 if (init_phase) {
413                         if (timeout > UDEVD_INIT_EVENT_TIMEOUT)
414                                 timeout = UDEVD_INIT_EVENT_TIMEOUT;
415                         dbg("initialization phase, timeout %i seconds", timeout);
416                 }
417
418                 /* move event with expired timeout to the exec list */
419                 msg_age = info.uptime - loop_msg->queue_time;
420                 dbg("seq %llu is %li seconds old", loop_msg->seqnum, msg_age);
421                 if (msg_age >= timeout) {
422                         msg_move_exec(loop_msg);
423                         goto recheck;
424                 } else {
425                         break;
426                 }
427         }
428
429         msg_dump_queue();
430
431         /* set timeout for remaining queued events */
432         if (!list_empty(&msg_list)) {
433                 struct itimerval itv = {{0, 0}, {timeout - msg_age, 0}};
434                 info("next event expires in %li seconds", timeout - msg_age);
435                 setitimer(ITIMER_REAL, &itv, NULL);
436         }
437 }
438
439 static struct uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
440 {
441         int bufpos;
442         int i;
443         struct uevent_msg *msg;
444         int major = 0;
445         int minor = 0;
446
447         msg = malloc(sizeof(struct uevent_msg) + buf_size);
448         if (msg == NULL)
449                 return NULL;
450         memset(msg, 0x00, sizeof(struct uevent_msg) + buf_size);
451
452         /* copy environment buffer and reconstruct envp */
453         memcpy(msg->envbuf, buf, buf_size);
454         bufpos = 0;
455         for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
456                 int keylen;
457                 char *key;
458
459                 key = &msg->envbuf[bufpos];
460                 keylen = strlen(key);
461                 msg->envp[i] = key;
462                 bufpos += keylen + 1;
463                 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
464
465                 /* remember some keys for further processing */
466                 if (strncmp(key, "ACTION=", 7) == 0)
467                         msg->action = &key[7];
468                 else if (strncmp(key, "DEVPATH=", 8) == 0)
469                         msg->devpath = &key[8];
470                 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
471                         msg->subsystem = &key[10];
472                 else if (strncmp(key, "SEQNUM=", 7) == 0)
473                         msg->seqnum = strtoull(&key[7], NULL, 10);
474                 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
475                         msg->physdevpath = &key[12];
476                 else if (strncmp(key, "MAJOR=", 6) == 0)
477                         major = strtoull(&key[6], NULL, 10);
478                 else if (strncmp(key, "MINOR=", 6) == 0)
479                         minor = strtoull(&key[6], NULL, 10);
480                 else if (strncmp(key, "TIMEOUT=", 8) == 0)
481                         msg->timeout = strtoull(&key[8], NULL, 10);
482         }
483         msg->devt = makedev(major, minor);
484         msg->envp[i++] = "UDEVD_EVENT=1";
485         msg->envp[i] = NULL;
486
487         return msg;
488 }
489
490 /* receive the udevd message from userspace */
491 static struct uevent_msg *get_udevd_msg(void)
492 {
493         static struct udevd_msg usend_msg;
494         struct uevent_msg *msg;
495         ssize_t size;
496         struct msghdr smsg;
497         struct cmsghdr *cmsg;
498         struct iovec iov;
499         struct ucred *cred;
500         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
501         int envbuf_size;
502         int *intval;
503
504         memset(&usend_msg, 0x00, sizeof(struct udevd_msg));
505         iov.iov_base = &usend_msg;
506         iov.iov_len = sizeof(struct udevd_msg);
507
508         memset(&smsg, 0x00, sizeof(struct msghdr));
509         smsg.msg_iov = &iov;
510         smsg.msg_iovlen = 1;
511         smsg.msg_control = cred_msg;
512         smsg.msg_controllen = sizeof(cred_msg);
513
514         size = recvmsg(udevd_sock, &smsg, 0);
515         if (size <  0) {
516                 if (errno != EINTR)
517                         err("unable to receive udevd message");
518                 return NULL;
519         }
520         cmsg = CMSG_FIRSTHDR(&smsg);
521         cred = (struct ucred *) CMSG_DATA(cmsg);
522
523         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
524                 err("no sender credentials received, message ignored");
525                 return NULL;
526         }
527
528         if (cred->uid != 0) {
529                 err("sender uid=%i, message ignored", cred->uid);
530                 return NULL;
531         }
532
533         if (strncmp(usend_msg.magic, UDEV_MAGIC, sizeof(UDEV_MAGIC)) != 0 ) {
534                 err("message magic '%s' doesn't match, ignore it", usend_msg.magic);
535                 return NULL;
536         }
537
538         switch (usend_msg.type) {
539         case UDEVD_UEVENT_UDEVSEND:
540         case UDEVD_UEVENT_INITSEND:
541                 info("udevd event message received");
542                 envbuf_size = size - offsetof(struct udevd_msg, envbuf);
543                 dbg("envbuf_size=%i", envbuf_size);
544                 msg = get_msg_from_envbuf(usend_msg.envbuf, envbuf_size);
545                 if (msg == NULL)
546                         return NULL;
547                 msg->type = usend_msg.type;
548                 return msg;
549         case UDEVD_STOP_EXEC_QUEUE:
550                 info("udevd message (STOP_EXEC_QUEUE) received");
551                 stop_exec_q = 1;
552                 break;
553         case UDEVD_START_EXEC_QUEUE:
554                 info("udevd message (START_EXEC_QUEUE) received");
555                 stop_exec_q = 0;
556                 exec_queue_manager();
557                 break;
558         case UDEVD_SET_LOG_LEVEL:
559                 intval = (int *) usend_msg.envbuf;
560                 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
561                 udev_log_priority = *intval;
562                 sprintf(log, "UDEV_LOG=%i", udev_log_priority);
563                 putenv(log);
564                 break;
565         case UDEVD_SET_MAX_CHILDS:
566                 intval = (int *) usend_msg.envbuf;
567                 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
568                 max_childs = *intval;
569                 break;
570         default:
571                 dbg("unknown message type");
572         }
573         return NULL;
574 }
575
576 /* receive the kernel user event message and do some sanity checks */
577 static struct uevent_msg *get_netlink_msg(void)
578 {
579         struct uevent_msg *msg;
580         int bufpos;
581         ssize_t size;
582         static char buffer[UEVENT_BUFFER_SIZE + 512];
583         char *pos;
584
585         size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
586         if (size <  0) {
587                 if (errno != EINTR)
588                         err("unable to receive udevd message");
589                 return NULL;
590         }
591
592         if ((size_t)size > sizeof(buffer)-1)
593                 size = sizeof(buffer)-1;
594         buffer[size] = '\0';
595         dbg("uevent_size=%zi", size);
596
597         /* start of event payload */
598         bufpos = strlen(buffer)+1;
599         msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
600         if (msg == NULL)
601                 return NULL;
602         msg->type = UDEVD_UEVENT_NETLINK;
603
604         /* validate message */
605         pos = strchr(buffer, '@');
606         if (pos == NULL) {
607                 err("invalid uevent '%s'", buffer);
608                 free(msg);
609                 return NULL;
610         }
611         pos[0] = '\0';
612
613         if (msg->action == NULL) {
614                 info("no ACTION in payload found, skip event '%s'", buffer);
615                 free(msg);
616                 return NULL;
617         }
618
619         if (strcmp(msg->action, buffer) != 0) {
620                 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
621                 free(msg);
622                 return NULL;
623         }
624
625         return msg;
626 }
627
628 static void asmlinkage sig_handler(int signum)
629 {
630         switch (signum) {
631                 case SIGINT:
632                 case SIGTERM:
633                         udev_exit = 1;
634                         break;
635                 case SIGALRM:
636                         /* set flag, then write to pipe if needed */
637                         run_msg_q = 1;
638                         break;
639                 case SIGCHLD:
640                         /* set flag, then write to pipe if needed */
641                         sigchilds_waiting = 1;
642                         break;
643         }
644
645         /* write to pipe, which will wakeup select() in our mainloop */
646         write(signal_pipe[WRITE_END], "", 1);
647 }
648
649 static void udev_done(int pid)
650 {
651         /* find msg associated with pid and delete it */
652         struct uevent_msg *msg;
653         struct sysinfo info;
654
655         list_for_each_entry(msg, &running_list, node) {
656                 if (msg->pid == pid) {
657                         sysinfo(&info);
658                         if (msg->queue_time)
659                                 info("seq %llu, pid [%d] exit, %ld seconds old", msg->seqnum, msg->pid, info.uptime - msg->queue_time);
660                         else
661                                 info("seq 0, pid [%d] exit", msg->pid);
662                         msg_queue_delete(msg);
663
664                         /* we want to run the exec queue manager since there may
665                          * be events waiting with the devpath of the one that
666                          * just finished
667                          */
668                         run_exec_q = 1;
669                         return;
670                 }
671         }
672 }
673
674 static void reap_sigchilds(void)
675 {
676         pid_t pid;
677
678         while (1) {
679                 pid = waitpid(-1, NULL, WNOHANG);
680                 if (pid <= 0)
681                         break;
682                 udev_done(pid);
683         }
684 }
685
686 static int init_udevd_socket(void)
687 {
688         struct sockaddr_un saddr;
689         const int buffersize = 16 * 1024 * 1024;
690         socklen_t addrlen;
691         const int feature_on = 1;
692         int retval;
693
694         memset(&saddr, 0x00, sizeof(saddr));
695         saddr.sun_family = AF_LOCAL;
696         /* use abstract namespace for socket path */
697         strcpy(&saddr.sun_path[1], UDEVD_SOCK_PATH);
698         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
699
700         udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
701         if (udevd_sock == -1) {
702                 err("error getting socket, %s", strerror(errno));
703                 return -1;
704         }
705
706         /* set receive buffersize */
707         setsockopt(udevd_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
708
709         /* the bind takes care of ensuring only one copy running */
710         retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
711         if (retval < 0) {
712                 err("bind failed, %s", strerror(errno));
713                 close(udevd_sock);
714                 return -1;
715         }
716
717         /* enable receiving of the sender credentials */
718         setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
719
720         return 0;
721 }
722
723 static int init_uevent_netlink_sock(void)
724 {
725         struct sockaddr_nl snl;
726         const int buffersize = 16 * 1024 * 1024;
727         int retval;
728
729         memset(&snl, 0x00, sizeof(struct sockaddr_nl));
730         snl.nl_family = AF_NETLINK;
731         snl.nl_pid = getpid();
732         snl.nl_groups = 0xffffffff;
733
734         uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
735         if (uevent_netlink_sock == -1) {
736                 err("error getting socket, %s", strerror(errno));
737                 return -1;
738         }
739
740         /* set receive buffersize */
741         setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
742
743         retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl,
744                       sizeof(struct sockaddr_nl));
745         if (retval < 0) {
746                 err("bind failed, %s", strerror(errno));
747                 close(uevent_netlink_sock);
748                 uevent_netlink_sock = -1;
749                 return -1;
750         }
751
752         return 0;
753 }
754
755 int main(int argc, char *argv[], char *envp[])
756 {
757         int retval;
758         int devnull;
759         struct sigaction act;
760         fd_set readfds;
761         const char *value;
762         int uevent_netlink_active = 0;
763         int daemonize = 0;
764         int i;
765
766         /* set std fd's to /dev/null, if the kernel forks us, we don't have them at all */
767         devnull = open("/dev/null", O_RDWR);
768         if (devnull >= 0)  {
769                 if (devnull != STDIN_FILENO)
770                         dup2(devnull, STDIN_FILENO);
771                 if (devnull != STDOUT_FILENO)
772                         dup2(devnull, STDOUT_FILENO);
773                 if (devnull != STDERR_FILENO)
774                         dup2(devnull, STDERR_FILENO);
775                 if (devnull > STDERR_FILENO)
776                         close(devnull);
777         }
778
779         logging_init("udevd");
780         if (devnull < 0)
781                 err("fatal, could not open /dev/null");
782
783         udev_init_config();
784         dbg("version %s", UDEV_VERSION);
785
786         if (getuid() != 0) {
787                 err("need to be root, exit");
788                 goto exit;
789         }
790
791         for (i = 1 ; i < argc; i++) {
792                 char *arg = argv[i];
793                 if (strcmp(arg, "--daemon") == 0 || strcmp(arg, "-d") == 0) {
794                         info("will daemonize");
795                         daemonize = 1;
796                 }
797                 if (strcmp(arg, "--stop-exec-queue") == 0) {
798                         info("will not execute events until START_EXEC_QUEUE is received");
799                         stop_exec_q = 1;
800                 }
801         }
802
803         if (daemonize) {
804                 pid_t pid;
805
806                 pid = fork();
807                 switch (pid) {
808                 case 0:
809                         dbg("damonized fork running");
810                         break;
811                 case -1:
812                         err("fork of daemon failed");
813                         goto exit;
814                 default:
815                         logging_close();
816                         exit(0);
817                 }
818         }
819
820         /* become session leader */
821         sid = setsid();
822         dbg("our session is %d", sid);
823
824         chdir("/");
825         umask(umask(077) | 022);
826
827         /* set a reasonable scheduling priority for the daemon */
828         setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
829
830         /* setup signal handler pipe */
831         retval = pipe(signal_pipe);
832         if (retval < 0) {
833                 err("error getting pipes: %s", strerror(errno));
834                 goto exit;
835         }
836         retval = fcntl(signal_pipe[READ_END], F_SETFL, O_NONBLOCK);
837         if (retval < 0) {
838                 err("error fcntl on read pipe: %s", strerror(errno));
839                 goto exit;
840         }
841         retval = fcntl(signal_pipe[READ_END], F_SETFD, FD_CLOEXEC);
842         if (retval < 0)
843                 err("error fcntl on read pipe: %s", strerror(errno));
844         retval = fcntl(signal_pipe[WRITE_END], F_SETFL, O_NONBLOCK);
845         if (retval < 0) {
846                 err("error fcntl on write pipe: %s", strerror(errno));
847                 goto exit;
848         }
849         retval = fcntl(signal_pipe[WRITE_END], F_SETFD, FD_CLOEXEC);
850         if (retval < 0)
851                 err("error fcntl on write pipe: %s", strerror(errno));
852
853         /* set signal handlers */
854         memset(&act, 0x00, sizeof(struct sigaction));
855         act.sa_handler = (void (*)(int)) sig_handler;
856         sigemptyset(&act.sa_mask);
857         act.sa_flags = SA_RESTART;
858         sigaction(SIGINT, &act, NULL);
859         sigaction(SIGTERM, &act, NULL);
860         sigaction(SIGALRM, &act, NULL);
861         sigaction(SIGCHLD, &act, NULL);
862         sigaction(SIGHUP, &act, NULL);
863
864         if (init_udevd_socket() < 0) {
865                 if (errno == EADDRINUSE)
866                         dbg("another udevd running, exit");
867                 else
868                         dbg("error initializing udevd socket: %s", strerror(errno));
869
870                 goto exit;
871         }
872
873         if (init_uevent_netlink_sock() < 0)
874                 info("uevent socket not available");
875
876         /* override of forked udev binary, used for testing */
877         udev_bin = getenv("UDEV_BIN");
878         if (udev_bin != NULL)
879                 info("udev binary is set to '%s'", udev_bin);
880         else
881                 udev_bin = UDEV_BIN;
882
883         /* init of expected_seqnum value */
884         value = getenv("UDEVD_EXPECTED_SEQNUM");
885         if (value) {
886                 expected_seqnum = strtoull(value, NULL, 10);
887                 info("initialize expected_seqnum to %llu", expected_seqnum);
888         }
889
890         /* timeout to wait for missing events */
891         value = getenv("UDEVD_EVENT_TIMEOUT");
892         if (value)
893                 event_timeout = strtoul(value, NULL, 10);
894         else
895                 event_timeout = UDEVD_EVENT_TIMEOUT;
896         info("initialize event_timeout to %u", event_timeout);
897
898         /* maximum limit of forked childs */
899         value = getenv("UDEVD_MAX_CHILDS");
900         if (value)
901                 max_childs = strtoul(value, NULL, 10);
902         else
903                 max_childs = UDEVD_MAX_CHILDS;
904         info("initialize max_childs to %u", max_childs);
905
906         /* start to throttle forking if maximum number of _running_ childs is reached */
907         value = getenv("UDEVD_MAX_CHILDS_RUNNING");
908         if (value)
909                 max_childs_running = strtoull(value, NULL, 10);
910         else
911                 max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
912         info("initialize max_childs_running to %u", max_childs_running);
913
914         /* export log_priority , as called programs may want to follow that setting */
915         sprintf(log, "UDEV_LOG=%i", udev_log_priority);
916         putenv(log);
917
918         while (!udev_exit) {
919                 struct uevent_msg *msg;
920                 int fdcount;
921
922                 FD_ZERO(&readfds);
923                 FD_SET(signal_pipe[READ_END], &readfds);
924                 FD_SET(udevd_sock, &readfds);
925                 if (uevent_netlink_sock > 0)
926                         FD_SET(uevent_netlink_sock, &readfds);
927
928                 fdcount = select(UDEV_MAX(udevd_sock, uevent_netlink_sock)+1, &readfds, NULL, NULL, NULL);
929                 if (fdcount < 0) {
930                         if (errno != EINTR)
931                                 dbg("error in select: %s", strerror(errno));
932                         continue;
933                 }
934
935                 /* get user socket message */
936                 if (FD_ISSET(udevd_sock, &readfds)) {
937                         msg = get_udevd_msg();
938                         if (msg) {
939                                 /* discard kernel messages if netlink is active */
940                                 if (uevent_netlink_active && msg->type == UDEVD_UEVENT_UDEVSEND && msg->seqnum != 0) {
941                                         info("skip uevent_helper message with SEQNUM, netlink is active");
942                                         free(msg);
943                                 } else
944                                         msg_queue_insert(msg);
945                         }
946                 }
947
948                 /* get kernel netlink message */
949                 if ((uevent_netlink_sock > 0) && FD_ISSET(uevent_netlink_sock, &readfds)) {
950                         msg = get_netlink_msg();
951                         if (msg) {
952                                 msg_queue_insert(msg);
953                                 /* disable udevsend with first netlink message */
954                                 if (!uevent_netlink_active) {
955                                         info("uevent_nl message received, disable udevsend messages");
956                                         uevent_netlink_active = 1;
957                                 }
958                         }
959                 }
960
961                 /* received a signal, clear our notification pipe */
962                 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
963                         char buf[256];
964
965                         read(signal_pipe[READ_END], &buf, sizeof(buf));
966                 }
967
968                 /* forked child have returned */
969                 if (sigchilds_waiting) {
970                         sigchilds_waiting = 0;
971                         reap_sigchilds();
972                 }
973
974                 if (run_msg_q) {
975                         run_msg_q = 0;
976                         msg_queue_manager();
977                 }
978
979                 if (run_exec_q) {
980                          /* clean up running_list before calling exec_queue_manager() */
981                         if (sigchilds_waiting) {
982                                 sigchilds_waiting = 0;
983                                 reap_sigchilds();
984                         }
985
986                         run_exec_q = 0;
987                         if (!stop_exec_q)
988                                 exec_queue_manager();
989                 }
990         }
991
992 exit:
993         if (signal_pipe[READ_END] > 0)
994                 close(signal_pipe[READ_END]);
995         if (signal_pipe[WRITE_END] > 0)
996                 close(signal_pipe[WRITE_END]);
997
998         if (udevd_sock > 0)
999                 close(udevd_sock);
1000         if (uevent_netlink_sock > 0)
1001                 close(uevent_netlink_sock);
1002
1003         logging_close();
1004
1005         return 0;
1006 }