chiark / gitweb /
volume_id: add md metadata 1.0, 1.1, 1.2 support
[elogind.git] / udevd.c
1 /*
2  * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
3  * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  *
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  *
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 #include <stddef.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <fcntl.h>
30 #include <syslog.h>
31 #include <time.h>
32 #include <getopt.h>
33 #include <sys/select.h>
34 #include <sys/wait.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <sys/stat.h>
39 #include <sys/ioctl.h>
40 #include <linux/types.h>
41 #include <linux/netlink.h>
42
43 #include "udev.h"
44 #include "udev_rules.h"
45 #include "udevd.h"
46 #include "udev_selinux.h"
47
48 static int debug_trace;
49 static int verbose;
50
51 static struct udev_rules rules;
52 static int udevd_sock = -1;
53 static int uevent_netlink_sock = -1;
54 static int inotify_fd = -1;
55 static pid_t sid;
56
57 static int signal_pipe[2] = {-1, -1};
58 static volatile int sigchilds_waiting;
59 static volatile int udev_exit;
60 static volatile int reload_config;
61 static int run_exec_q;
62 static int stop_exec_q;
63 static int max_childs;
64 static int max_childs_running;
65 static char udev_log[32];
66
67 static LIST_HEAD(exec_list);
68 static LIST_HEAD(running_list);
69
70
71 #ifdef USE_LOG
72 void log_message(int priority, const char *format, ...)
73 {
74         va_list args;
75
76         if (priority > udev_log_priority)
77                 return;
78
79         va_start(args, format);
80         vsyslog(priority, format, args);
81         va_end(args);
82
83         if (verbose) {
84                 va_start(args, format);
85                 vprintf(format, args);
86                 va_end(args);
87                 printf("\n");
88         }
89 }
90
91 #endif
92
93 static void asmlinkage udev_event_sig_handler(int signum)
94 {
95         if (signum == SIGALRM)
96                 exit(1);
97 }
98
99 static int udev_event_process(struct udevd_uevent_msg *msg)
100 {
101         struct sigaction act;
102         struct udevice *udev;
103         int i;
104         int retval;
105
106         /* set signal handlers */
107         memset(&act, 0x00, sizeof(act));
108         act.sa_handler = (void (*)(int)) udev_event_sig_handler;
109         sigemptyset (&act.sa_mask);
110         act.sa_flags = 0;
111         sigaction(SIGALRM, &act, NULL);
112
113         /* reset to default */
114         act.sa_handler = SIG_DFL;
115         sigaction(SIGINT, &act, NULL);
116         sigaction(SIGTERM, &act, NULL);
117         sigaction(SIGCHLD, &act, NULL);
118         sigaction(SIGHUP, &act, NULL);
119
120         /* trigger timeout to prevent hanging processes */
121         alarm(UDEV_ALARM_TIMEOUT);
122
123         /* reconstruct event environment from message */
124         for (i = 0; msg->envp[i]; i++)
125                 putenv(msg->envp[i]);
126
127         udev = udev_device_init(NULL);
128         if (udev == NULL)
129                 return -1;
130         strlcpy(udev->action, msg->action, sizeof(udev->action));
131         sysfs_device_set_values(udev->dev, msg->devpath, msg->subsystem, msg->driver);
132         udev->devt = msg->devt;
133
134         retval = udev_device_event(&rules, udev);
135
136         /* run programs collected by RUN-key*/
137         if (retval == 0 && !udev->ignore_device && udev_run) {
138                 struct name_entry *name_loop;
139
140                 dbg("executing run list");
141                 list_for_each_entry(name_loop, &udev->run_list, node) {
142                         if (strncmp(name_loop->name, "socket:", strlen("socket:")) == 0)
143                                 pass_env_to_socket(&name_loop->name[strlen("socket:")], udev->dev->devpath, udev->action);
144                         else {
145                                 char program[PATH_SIZE];
146
147                                 strlcpy(program, name_loop->name, sizeof(program));
148                                 udev_rules_apply_format(udev, program, sizeof(program));
149                                 if (run_program(program, udev->dev->subsystem, NULL, 0, NULL,
150                                                 (udev_log_priority >= LOG_INFO)))
151                                         retval = -1;
152                         }
153                 }
154         }
155
156         udev_device_cleanup(udev);
157         return retval;
158 }
159
160 enum event_state {
161         EVENT_QUEUED,
162         EVENT_FINISHED,
163         EVENT_FAILED,
164 };
165
166 static void export_event_state(struct udevd_uevent_msg *msg, enum event_state state)
167 {
168         char filename[PATH_SIZE];
169         char filename_failed[PATH_SIZE];
170         size_t start;
171         struct udevd_uevent_msg *loop_msg;
172         int fd;
173
174         /* add location of queue files */
175         strlcpy(filename, udev_root, sizeof(filename));
176         strlcat(filename, "/", sizeof(filename));
177         start = strlcat(filename, EVENT_QUEUE_DIR"/", sizeof(filename));
178         strlcat(filename, msg->devpath, sizeof(filename));
179         path_encode(&filename[start], sizeof(filename) - start);
180
181         /* add location of failed files */
182         strlcpy(filename_failed, udev_root, sizeof(filename_failed));
183         strlcat(filename_failed, "/", sizeof(filename_failed));
184         start = strlcat(filename_failed, EVENT_FAILED_DIR"/", sizeof(filename_failed));
185         strlcat(filename_failed, msg->devpath, sizeof(filename_failed));
186         path_encode(&filename_failed[start], sizeof(filename) - start);
187
188         switch (state) {
189         case EVENT_QUEUED:
190                 unlink(filename_failed);
191                 delete_path(filename_failed);
192                 create_path(filename);
193                 fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
194                 if (fd > 0)
195                         close(fd);
196                 return;
197         case EVENT_FINISHED:
198         case EVENT_FAILED:
199                 unlink(filename_failed);
200                 delete_path(filename_failed);
201
202                 /* don't remove, if events for the same path are still pending */
203                 list_for_each_entry(loop_msg, &running_list, node)
204                         if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
205                                 return;
206
207                 list_for_each_entry(loop_msg, &exec_list, node)
208                         if (loop_msg->devpath && strcmp(loop_msg->devpath, msg->devpath) == 0)
209                                 return;
210
211                 /* move failed events to the failed directory */
212                 if (state == EVENT_FAILED) {
213                         create_path(filename_failed);
214                         rename(filename, filename_failed);
215                 } else {
216                         unlink(filename);
217                 }
218
219                 /* clean up the queue directory */
220                 delete_path(filename);
221
222                 return;
223         }
224 }
225
226 static void msg_queue_delete(struct udevd_uevent_msg *msg)
227 {
228         list_del(&msg->node);
229
230         /* mark as failed, if add event returns non-zero */
231         if (msg->exitstatus && strcmp(msg->action, "add") == 0)
232                 export_event_state(msg, EVENT_FAILED);
233         else
234                 export_event_state(msg, EVENT_FINISHED);
235
236         free(msg);
237 }
238
239 static void udev_event_run(struct udevd_uevent_msg *msg)
240 {
241         pid_t pid;
242         int retval;
243
244         pid = fork();
245         switch (pid) {
246         case 0:
247                 /* child */
248                 close(uevent_netlink_sock);
249                 close(udevd_sock);
250                 if (inotify_fd >= 0)
251                         close(inotify_fd);
252                 close(signal_pipe[READ_END]);
253                 close(signal_pipe[WRITE_END]);
254                 logging_close();
255
256                 logging_init("udevd-event");
257                 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
258
259                 retval = udev_event_process(msg);
260                 info("seq %llu finished", msg->seqnum);
261
262                 logging_close();
263                 if (retval)
264                         exit(1);
265                 exit(0);
266         case -1:
267                 err("fork of child failed: %s", strerror(errno));
268                 msg_queue_delete(msg);
269                 break;
270         default:
271                 /* get SIGCHLD in main loop */
272                 info("seq %llu forked, pid [%d], '%s' '%s', %ld seconds old",
273                      msg->seqnum, pid,  msg->action, msg->subsystem, time(NULL) - msg->queue_time);
274                 msg->pid = pid;
275         }
276 }
277
278 static void msg_queue_insert(struct udevd_uevent_msg *msg)
279 {
280         char filename[PATH_SIZE];
281         int fd;
282
283         msg->queue_time = time(NULL);
284
285         strlcpy(filename, udev_root, sizeof(filename));
286         strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
287         fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
288         if (fd >= 0) {
289                 char str[32];
290                 int len;
291
292                 len = sprintf(str, "%llu\n", msg->seqnum);
293                 write(fd, str, len);
294                 close(fd);
295         }
296
297         export_event_state(msg, EVENT_QUEUED);
298
299         /* run one event after the other in debug mode */
300         if (debug_trace) {
301                 list_add_tail(&msg->node, &running_list);
302                 udev_event_run(msg);
303                 waitpid(msg->pid, NULL, 0);
304                 msg_queue_delete(msg);
305                 return;
306         }
307
308         /* run all events with a timeout set immediately */
309         if (msg->timeout != 0) {
310                 list_add_tail(&msg->node, &running_list);
311                 udev_event_run(msg);
312                 return;
313         }
314
315         list_add_tail(&msg->node, &exec_list);
316         run_exec_q = 1;
317 }
318
319 static int mem_size_mb(void)
320 {
321         int f;
322         char buf[8192];
323         long int len;
324         const char *pos;
325         long int memsize;
326
327         f = open("/proc/meminfo", O_RDONLY);
328         if (f == -1)
329                 return -1;
330
331         len = read(f, buf, sizeof(buf)-1);
332         close(f);
333
334         if (len <= 0)
335                 return -1;
336         buf[len] = '\0';
337
338         pos = strstr(buf, "MemTotal: ");
339         if (pos == NULL)
340                 return -1;
341
342         if (sscanf(pos, "MemTotal: %ld kB", &memsize) != 1)
343                 return -1;
344
345         return memsize / 1024;
346 }
347
348 static int cpu_count(void)
349 {
350         int f;
351         char buf[65536];
352         int len;
353         const char *pos;
354         int count = 0;
355
356         f = open("/proc/stat", O_RDONLY);
357         if (f == -1)
358                 return -1;
359
360         len = read(f, buf, sizeof(buf)-1);
361         close(f);
362         if (len <= 0)
363                 return -1;
364         buf[len] = '\0';
365
366         pos = strstr(buf, "cpu");
367         if (pos == NULL)
368                 return -1;
369
370         while (pos != NULL) {
371                 if (strncmp(pos, "cpu", 3) == 0 &&isdigit(pos[3]))
372                         count++;
373                 pos = strstr(&pos[3], "cpu");
374         }
375
376         if (count == 0)
377                 return -1;
378         return count;
379 }
380
381 static int running_processes(void)
382 {
383         int f;
384         char buf[32768];
385         int len;
386         int running;
387         const char *pos;
388
389         f = open("/proc/stat", O_RDONLY);
390         if (f == -1)
391                 return -1;
392
393         len = read(f, buf, sizeof(buf)-1);
394         close(f);
395         if (len <= 0)
396                 return -1;
397         buf[len] = '\0';
398
399         pos = strstr(buf, "procs_running ");
400         if (pos == NULL)
401                 return -1;
402
403         if (sscanf(pos, "procs_running %u", &running) != 1)
404                 return -1;
405
406         return running;
407 }
408
409 /* return the number of process es in our session, count only until limit */
410 static int running_processes_in_session(pid_t session, int limit)
411 {
412         DIR *dir;
413         struct dirent *dent;
414         int running = 0;
415
416         dir = opendir("/proc");
417         if (!dir)
418                 return -1;
419
420         /* read process info from /proc */
421         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
422                 int f;
423                 char procdir[64];
424                 char line[256];
425                 const char *pos;
426                 char state;
427                 pid_t ppid, pgrp, sess;
428                 int len;
429
430                 if (!isdigit(dent->d_name[0]))
431                         continue;
432
433                 snprintf(procdir, sizeof(procdir), "/proc/%s/stat", dent->d_name);
434                 procdir[sizeof(procdir)-1] = '\0';
435
436                 f = open(procdir, O_RDONLY);
437                 if (f == -1)
438                         continue;
439
440                 len = read(f, line, sizeof(line)-1);
441                 close(f);
442
443                 if (len <= 0)
444                         continue;
445                 else
446                         line[len] = '\0';
447
448                 /* skip ugly program name */
449                 pos = strrchr(line, ')') + 2;
450                 if (pos == NULL)
451                         continue;
452
453                 if (sscanf(pos, "%c %d %d %d ", &state, &ppid, &pgrp, &sess) != 4)
454                         continue;
455
456                 /* count only processes in our session */
457                 if (sess != session)
458                         continue;
459
460                 /* count only running, no sleeping processes */
461                 if (state != 'R')
462                         continue;
463
464                 running++;
465                 if (limit > 0 && running >= limit)
466                         break;
467         }
468         closedir(dir);
469
470         return running;
471 }
472
473 static int compare_devpath(const char *running, const char *waiting)
474 {
475         int i;
476
477         for (i = 0; i < PATH_SIZE; i++) {
478                 /* identical device event found */
479                 if (running[i] == '\0' && waiting[i] == '\0')
480                         return 1;
481
482                 /* parent device event found */
483                 if (running[i] == '\0' && waiting[i] == '/')
484                         return 2;
485
486                 /* child device event found */
487                 if (running[i] == '/' && waiting[i] == '\0')
488                         return 3;
489
490                 /* no matching event */
491                 if (running[i] != waiting[i])
492                         break;
493         }
494
495         return 0;
496 }
497
498 /* lookup event for identical, parent, child, or physical device */
499 static int devpath_busy(struct udevd_uevent_msg *msg, int limit)
500 {
501         struct udevd_uevent_msg *loop_msg;
502         int childs_count = 0;
503
504         /* check exec-queue which may still contain delayed events we depend on */
505         list_for_each_entry(loop_msg, &exec_list, node) {
506                 /* skip ourself and all later events */
507                 if (loop_msg->seqnum >= msg->seqnum)
508                         break;
509
510                 /* check identical, parent, or child device event */
511                 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
512                         dbg("%llu, device event still pending %llu (%s)",
513                             msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
514                         return 2;
515                 }
516
517                 /* check physical device event (special case of parent) */
518                 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
519                         if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
520                                 dbg("%llu, physical device event still pending %llu (%s)",
521                                     msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
522                                 return 3;
523                         }
524         }
525
526         /* check runing-queue for still running events */
527         list_for_each_entry(loop_msg, &running_list, node) {
528                 if (limit && childs_count++ > limit) {
529                         dbg("%llu, maximum number (%i) of childs reached", msg->seqnum, childs_count);
530                         return 1;
531                 }
532
533                 /* check identical, parent, or child device event */
534                 if (compare_devpath(loop_msg->devpath, msg->devpath) != 0) {
535                         dbg("%llu, device event still running %llu (%s)",
536                             msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
537                         return 2;
538                 }
539
540                 /* check physical device event (special case of parent) */
541                 if (msg->physdevpath && msg->action && strcmp(msg->action, "add") == 0)
542                         if (compare_devpath(loop_msg->devpath, msg->physdevpath) != 0) {
543                                 dbg("%llu, physical device event still running %llu (%s)",
544                                     msg->seqnum, loop_msg->seqnum, loop_msg->devpath);
545                                 return 3;
546                         }
547         }
548         return 0;
549 }
550
551 /* serializes events for the identical and parent and child devices */
552 static void msg_queue_manager(void)
553 {
554         struct udevd_uevent_msg *loop_msg;
555         struct udevd_uevent_msg *tmp_msg;
556         int running;
557
558         if (list_empty(&exec_list))
559                 return;
560
561         running = running_processes();
562         dbg("%d processes runnning on system", running);
563         if (running < 0)
564                 running = max_childs_running;
565
566         list_for_each_entry_safe(loop_msg, tmp_msg, &exec_list, node) {
567                 /* check running processes in our session and possibly throttle */
568                 if (running >= max_childs_running) {
569                         running = running_processes_in_session(sid, max_childs_running+10);
570                         dbg("at least %d processes running in session", running);
571                         if (running >= max_childs_running) {
572                                 dbg("delay seq %llu, too many processes already running", loop_msg->seqnum);
573                                 return;
574                         }
575                 }
576
577                 /* serialize and wait for parent or child events */
578                 if (devpath_busy(loop_msg, max_childs) != 0) {
579                         dbg("delay seq %llu (%s)", loop_msg->seqnum, loop_msg->devpath);
580                         continue;
581                 }
582
583                 /* move event to run list */
584                 list_move_tail(&loop_msg->node, &running_list);
585                 udev_event_run(loop_msg);
586                 running++;
587                 dbg("moved seq %llu to running list", loop_msg->seqnum);
588         }
589 }
590
591 static struct udevd_uevent_msg *get_msg_from_envbuf(const char *buf, int buf_size)
592 {
593         int bufpos;
594         int i;
595         struct udevd_uevent_msg *msg;
596         char *physdevdriver_key = NULL;
597         int maj = 0;
598         int min = 0;
599
600         msg = malloc(sizeof(struct udevd_uevent_msg) + buf_size);
601         if (msg == NULL)
602                 return NULL;
603         memset(msg, 0x00, sizeof(struct udevd_uevent_msg) + buf_size);
604
605         /* copy environment buffer and reconstruct envp */
606         memcpy(msg->envbuf, buf, buf_size);
607         bufpos = 0;
608         for (i = 0; (bufpos < buf_size) && (i < UEVENT_NUM_ENVP-2); i++) {
609                 int keylen;
610                 char *key;
611
612                 key = &msg->envbuf[bufpos];
613                 keylen = strlen(key);
614                 msg->envp[i] = key;
615                 bufpos += keylen + 1;
616                 dbg("add '%s' to msg.envp[%i]", msg->envp[i], i);
617
618                 /* remember some keys for further processing */
619                 if (strncmp(key, "ACTION=", 7) == 0)
620                         msg->action = &key[7];
621                 else if (strncmp(key, "DEVPATH=", 8) == 0)
622                         msg->devpath = &key[8];
623                 else if (strncmp(key, "SUBSYSTEM=", 10) == 0)
624                         msg->subsystem = &key[10];
625                 else if (strncmp(key, "DRIVER=", 7) == 0)
626                         msg->driver = &key[7];
627                 else if (strncmp(key, "SEQNUM=", 7) == 0)
628                         msg->seqnum = strtoull(&key[7], NULL, 10);
629                 else if (strncmp(key, "PHYSDEVPATH=", 12) == 0)
630                         msg->physdevpath = &key[12];
631                 else if (strncmp(key, "PHYSDEVDRIVER=", 14) == 0)
632                         physdevdriver_key = key;
633                 else if (strncmp(key, "MAJOR=", 6) == 0)
634                         maj = strtoull(&key[6], NULL, 10);
635                 else if (strncmp(key, "MINOR=", 6) == 0)
636                         min = strtoull(&key[6], NULL, 10);
637                 else if (strncmp(key, "TIMEOUT=", 8) == 0)
638                         msg->timeout = strtoull(&key[8], NULL, 10);
639         }
640         msg->devt = makedev(maj, min);
641         msg->envp[i++] = "UDEVD_EVENT=1";
642
643         if (msg->driver == NULL && msg->physdevpath == NULL && physdevdriver_key != NULL) {
644                 /* for older kernels DRIVER is empty for a bus device, export PHYSDEVDRIVER as DRIVER */
645                 msg->envp[i++] = &physdevdriver_key[7];
646                 msg->driver = &physdevdriver_key[14];
647         }
648
649         msg->envp[i] = NULL;
650
651         if (msg->devpath == NULL || msg->action == NULL) {
652                 info("DEVPATH or ACTION missing, ignore message");
653                 free(msg);
654                 return NULL;
655         }
656         return msg;
657 }
658
659 /* receive the udevd message from userspace */
660 static void get_ctrl_msg(void)
661 {
662         struct udevd_ctrl_msg ctrl_msg;
663         ssize_t size;
664         struct msghdr smsg;
665         struct cmsghdr *cmsg;
666         struct iovec iov;
667         struct ucred *cred;
668         char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
669         int *intval;
670
671         memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
672         iov.iov_base = &ctrl_msg;
673         iov.iov_len = sizeof(struct udevd_ctrl_msg);
674
675         memset(&smsg, 0x00, sizeof(struct msghdr));
676         smsg.msg_iov = &iov;
677         smsg.msg_iovlen = 1;
678         smsg.msg_control = cred_msg;
679         smsg.msg_controllen = sizeof(cred_msg);
680
681         size = recvmsg(udevd_sock, &smsg, 0);
682         if (size <  0) {
683                 if (errno != EINTR)
684                         err("unable to receive user udevd message: %s", strerror(errno));
685                 return;
686         }
687         cmsg = CMSG_FIRSTHDR(&smsg);
688         cred = (struct ucred *) CMSG_DATA(cmsg);
689
690         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
691                 err("no sender credentials received, message ignored");
692                 return;
693         }
694
695         if (cred->uid != 0) {
696                 err("sender uid=%i, message ignored", cred->uid);
697                 return;
698         }
699
700         if (strncmp(ctrl_msg.magic, UDEVD_CTRL_MAGIC, sizeof(UDEVD_CTRL_MAGIC)) != 0 ) {
701                 err("message magic '%s' doesn't match, ignore it", ctrl_msg.magic);
702                 return;
703         }
704
705         switch (ctrl_msg.type) {
706         case UDEVD_CTRL_STOP_EXEC_QUEUE:
707                 info("udevd message (STOP_EXEC_QUEUE) received");
708                 stop_exec_q = 1;
709                 break;
710         case UDEVD_CTRL_START_EXEC_QUEUE:
711                 info("udevd message (START_EXEC_QUEUE) received");
712                 stop_exec_q = 0;
713                 msg_queue_manager();
714                 break;
715         case UDEVD_CTRL_SET_LOG_LEVEL:
716                 intval = (int *) ctrl_msg.buf;
717                 info("udevd message (SET_LOG_PRIORITY) received, udev_log_priority=%i", *intval);
718                 udev_log_priority = *intval;
719                 sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
720                 putenv(udev_log);
721                 break;
722         case UDEVD_CTRL_SET_MAX_CHILDS:
723                 intval = (int *) ctrl_msg.buf;
724                 info("udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i", *intval);
725                 max_childs = *intval;
726                 break;
727         case UDEVD_CTRL_SET_MAX_CHILDS_RUNNING:
728                 intval = (int *) ctrl_msg.buf;
729                 info("udevd message (UDEVD_SET_MAX_CHILDS_RUNNING) received, max_childs=%i", *intval);
730                 max_childs_running = *intval;
731                 break;
732         case UDEVD_CTRL_RELOAD_RULES:
733                 info("udevd message (RELOAD_RULES) received");
734                 reload_config = 1;
735                 break;
736         default:
737                 err("unknown control message type");
738         }
739 }
740
741 /* receive the kernel user event message and do some sanity checks */
742 static struct udevd_uevent_msg *get_netlink_msg(void)
743 {
744         struct udevd_uevent_msg *msg;
745         int bufpos;
746         ssize_t size;
747         static char buffer[UEVENT_BUFFER_SIZE+512];
748         char *pos;
749
750         size = recv(uevent_netlink_sock, &buffer, sizeof(buffer), 0);
751         if (size <  0) {
752                 if (errno != EINTR)
753                         err("unable to receive kernel netlink message: %s", strerror(errno));
754                 return NULL;
755         }
756
757         if ((size_t)size > sizeof(buffer)-1)
758                 size = sizeof(buffer)-1;
759         buffer[size] = '\0';
760         dbg("uevent_size=%zi", size);
761
762         /* start of event payload */
763         bufpos = strlen(buffer)+1;
764         msg = get_msg_from_envbuf(&buffer[bufpos], size-bufpos);
765         if (msg == NULL)
766                 return NULL;
767
768         /* validate message */
769         pos = strchr(buffer, '@');
770         if (pos == NULL) {
771                 err("invalid uevent '%s'", buffer);
772                 free(msg);
773                 return NULL;
774         }
775         pos[0] = '\0';
776
777         if (msg->action == NULL) {
778                 info("no ACTION in payload found, skip event '%s'", buffer);
779                 free(msg);
780                 return NULL;
781         }
782
783         if (strcmp(msg->action, buffer) != 0) {
784                 err("ACTION in payload does not match uevent, skip event '%s'", buffer);
785                 free(msg);
786                 return NULL;
787         }
788
789         return msg;
790 }
791
792 static void asmlinkage sig_handler(int signum)
793 {
794         switch (signum) {
795                 case SIGINT:
796                 case SIGTERM:
797                         udev_exit = 1;
798                         break;
799                 case SIGCHLD:
800                         /* set flag, then write to pipe if needed */
801                         sigchilds_waiting = 1;
802                         break;
803                 case SIGHUP:
804                         reload_config = 1;
805                         break;
806         }
807
808         /* write to pipe, which will wakeup select() in our mainloop */
809         write(signal_pipe[WRITE_END], "", 1);
810 }
811
812 static void udev_done(int pid, int exitstatus)
813 {
814         /* find msg associated with pid and delete it */
815         struct udevd_uevent_msg *msg;
816
817         list_for_each_entry(msg, &running_list, node) {
818                 if (msg->pid == pid) {
819                         info("seq %llu, pid [%d] exit with %i, %ld seconds old", msg->seqnum, msg->pid,
820                              exitstatus, time(NULL) - msg->queue_time);
821                         msg->exitstatus = exitstatus;
822                         msg_queue_delete(msg);
823
824                         /* there may be events waiting with the same devpath */
825                         run_exec_q = 1;
826                         return;
827                 }
828         }
829 }
830
831 static void reap_sigchilds(void)
832 {
833         pid_t pid;
834         int status;
835
836         while (1) {
837                 pid = waitpid(-1, &status, WNOHANG);
838                 if (pid <= 0)
839                         break;
840                 if (WIFEXITED(status))
841                         status = WEXITSTATUS(status);
842                 else if (WIFSIGNALED(status))
843                         status = WTERMSIG(status) + 128;
844                 else
845                         status = 0;
846                 udev_done(pid, status);
847         }
848 }
849
850 static int init_udevd_socket(void)
851 {
852         struct sockaddr_un saddr;
853         socklen_t addrlen;
854         const int feature_on = 1;
855         int retval;
856
857         memset(&saddr, 0x00, sizeof(saddr));
858         saddr.sun_family = AF_LOCAL;
859         /* use abstract namespace for socket path */
860         strcpy(&saddr.sun_path[1], UDEVD_CTRL_SOCK_PATH);
861         addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
862
863         udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
864         if (udevd_sock == -1) {
865                 err("error getting socket: %s", strerror(errno));
866                 return -1;
867         }
868
869         /* the bind takes care of ensuring only one copy running */
870         retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
871         if (retval < 0) {
872                 err("bind failed: %s", strerror(errno));
873                 close(udevd_sock);
874                 udevd_sock = -1;
875                 return -1;
876         }
877
878         /* enable receiving of the sender credentials */
879         setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
880
881         return 0;
882 }
883
884 static int init_uevent_netlink_sock(void)
885 {
886         struct sockaddr_nl snl;
887         const int buffersize = 16 * 1024 * 1024;
888         int retval;
889
890         memset(&snl, 0x00, sizeof(struct sockaddr_nl));
891         snl.nl_family = AF_NETLINK;
892         snl.nl_pid = getpid();
893         snl.nl_groups = 1;
894
895         uevent_netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
896         if (uevent_netlink_sock == -1) {
897                 err("error getting socket: %s", strerror(errno));
898                 return -1;
899         }
900
901         /* set receive buffersize */
902         setsockopt(uevent_netlink_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));
903
904         retval = bind(uevent_netlink_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));
905         if (retval < 0) {
906                 err("bind failed: %s", strerror(errno));
907                 close(uevent_netlink_sock);
908                 uevent_netlink_sock = -1;
909                 return -1;
910         }
911         return 0;
912 }
913
914 static void export_initial_seqnum(void)
915 {
916         char filename[PATH_SIZE];
917         int fd;
918         char seqnum[32];
919         ssize_t len = 0;
920
921         strlcpy(filename, sysfs_path, sizeof(filename));
922         strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
923         fd = open(filename, O_RDONLY);
924         if (fd >= 0) {
925                 len = read(fd, seqnum, sizeof(seqnum)-1);
926                 close(fd);
927         }
928         if (len <= 0) {
929                 strcpy(seqnum, "0\n");
930                 len = 3;
931         }
932         strlcpy(filename, udev_root, sizeof(filename));
933         strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
934         create_path(filename);
935         fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
936         if (fd >= 0) {
937                 write(fd, seqnum, len);
938                 close(fd);
939         }
940 }
941
942 int main(int argc, char *argv[], char *envp[])
943 {
944         int retval;
945         int fd;
946         struct sigaction act;
947         fd_set readfds;
948         const char *value;
949         int daemonize = 0;
950         int option;
951         static const struct option options[] = {
952                 { "daemon", 0, NULL, 'd' },
953                 { "debug-trace", 0, NULL, 't' },
954                 { "verbose", 0, NULL, 'v' },
955                 { "help", 0, NULL, 'h' },
956                 { "version", 0, NULL, 'V' },
957                 {}
958         };
959         int rc = 1;
960         int maxfd;
961
962         logging_init("udevd");
963         udev_config_init();
964         selinux_init();
965         dbg("version %s", UDEV_VERSION);
966
967         /* parse commandline options */
968         while (1) {
969                 option = getopt_long(argc, argv, "dtvhV", options, NULL);
970                 if (option == -1)
971                         break;
972
973                 switch (option) {
974                 case 'd':
975                         daemonize = 1;
976                         break;
977                 case 't':
978                         debug_trace = 1;
979                         break;
980                 case 'v':
981                         verbose = 1;
982                         if (udev_log_priority < LOG_INFO)
983                                 udev_log_priority = LOG_INFO;
984                         break;
985                 case 'h':
986                         printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--verbose] [--version]\n");
987                         goto exit;
988                 case 'V':
989                         printf("%s\n", UDEV_VERSION);
990                         goto exit;
991                 default:
992                         goto exit;
993                 }
994         }
995
996         if (getuid() != 0) {
997                 fprintf(stderr, "root privileges required\n");
998                 err("root privileges required");
999                 goto exit;
1000         }
1001
1002         /* make sure std{in,out,err} fd's are in a sane state */
1003         fd = open("/dev/null", O_RDWR);
1004         if (fd < 0) {
1005                 fprintf(stderr, "cannot open /dev/null\n");
1006                 err("cannot open /dev/null");
1007         }
1008         if (fd > STDIN_FILENO)
1009                 dup2(fd, STDIN_FILENO);
1010         if (write(STDOUT_FILENO, 0, 0) < 0)
1011                 dup2(fd, STDOUT_FILENO);
1012         if (write(STDERR_FILENO, 0, 0) < 0)
1013                 dup2(fd, STDERR_FILENO);
1014
1015         /* init sockets to receive events */
1016         if (init_udevd_socket() < 0) {
1017                 if (errno == EADDRINUSE) {
1018                         fprintf(stderr, "another udev daemon already running\n");
1019                         err("another udev daemon already running");
1020                         rc = 1;
1021                 } else {
1022                         fprintf(stderr, "error initializing udevd socket\n");
1023                         err("error initializing udevd socket");
1024                         rc = 2;
1025                 }
1026                 goto exit;
1027         }
1028
1029         if (init_uevent_netlink_sock() < 0) {
1030                 fprintf(stderr, "error initializing netlink socket\n");
1031                 err("error initializing netlink socket");
1032                 rc = 3;
1033                 goto exit;
1034         }
1035
1036         /* setup signal handler pipe */
1037         retval = pipe(signal_pipe);
1038         if (retval < 0) {
1039                 err("error getting pipes: %s", strerror(errno));
1040                 goto exit;
1041         }
1042
1043         retval = fcntl(signal_pipe[READ_END], F_GETFL, 0);
1044         if (retval < 0) {
1045                 err("error fcntl on read pipe: %s", strerror(errno));
1046                 goto exit;
1047         }
1048         retval = fcntl(signal_pipe[READ_END], F_SETFL, retval | O_NONBLOCK);
1049         if (retval < 0) {
1050                 err("error fcntl on read pipe: %s", strerror(errno));
1051                 goto exit;
1052         }
1053
1054         retval = fcntl(signal_pipe[WRITE_END], F_GETFL, 0);
1055         if (retval < 0) {
1056                 err("error fcntl on write pipe: %s", strerror(errno));
1057                 goto exit;
1058         }
1059         retval = fcntl(signal_pipe[WRITE_END], F_SETFL, retval | O_NONBLOCK);
1060         if (retval < 0) {
1061                 err("error fcntl on write pipe: %s", strerror(errno));
1062                 goto exit;
1063         }
1064
1065         /* parse the rules and keep them in memory */
1066         sysfs_init();
1067         udev_rules_init(&rules, 1);
1068
1069         export_initial_seqnum();
1070
1071         if (daemonize) {
1072                 pid_t pid;
1073
1074                 pid = fork();
1075                 switch (pid) {
1076                 case 0:
1077                         dbg("daemonized fork running");
1078                         break;
1079                 case -1:
1080                         err("fork of daemon failed: %s", strerror(errno));
1081                         rc = 4;
1082                         goto exit;
1083                 default:
1084                         dbg("child [%u] running, parent exits", pid);
1085                         rc = 0;
1086                         goto exit;
1087                 }
1088         }
1089
1090         /* redirect std{out,err} fd's */
1091         if (!verbose)
1092                 dup2(fd, STDOUT_FILENO);
1093         dup2(fd, STDERR_FILENO);
1094         if (fd > STDERR_FILENO)
1095                 close(fd);
1096
1097         /* set scheduling priority for the daemon */
1098         setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
1099
1100         chdir("/");
1101         umask(022);
1102
1103         /* become session leader */
1104         sid = setsid();
1105         dbg("our session is %d", sid);
1106
1107         /* OOM_DISABLE == -17 */
1108         fd = open("/proc/self/oom_adj", O_RDWR);
1109         if (fd < 0)
1110                 err("error disabling OOM: %s", strerror(errno));
1111         else {
1112                 write(fd, "-17", 3);
1113                 close(fd);
1114         }
1115
1116         /* set signal handlers */
1117         memset(&act, 0x00, sizeof(struct sigaction));
1118         act.sa_handler = (void (*)(int)) sig_handler;
1119         sigemptyset(&act.sa_mask);
1120         act.sa_flags = SA_RESTART;
1121         sigaction(SIGINT, &act, NULL);
1122         sigaction(SIGTERM, &act, NULL);
1123         sigaction(SIGCHLD, &act, NULL);
1124         sigaction(SIGHUP, &act, NULL);
1125
1126         /* watch rules directory */
1127         inotify_fd = inotify_init();
1128         if (inotify_fd >= 0)
1129                 inotify_add_watch(inotify_fd, udev_rules_dir, IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
1130         else if (errno == ENOSYS)
1131                 err("the kernel does not support inotify, udevd can't monitor configuration file changes");
1132         else
1133                 err("inotify_init failed: %s", strerror(errno));
1134
1135         /* maximum limit of forked childs */
1136         value = getenv("UDEVD_MAX_CHILDS");
1137         if (value)
1138                 max_childs = strtoul(value, NULL, 10);
1139         else {
1140                 int memsize = mem_size_mb();
1141                 if (memsize > 0)
1142                         max_childs = 128 + (memsize / 4);
1143                 else
1144                         max_childs = UDEVD_MAX_CHILDS;
1145         }
1146         info("initialize max_childs to %u", max_childs);
1147
1148         /* start to throttle forking if maximum number of _running_ childs is reached */
1149         value = getenv("UDEVD_MAX_CHILDS_RUNNING");
1150         if (value)
1151                 max_childs_running = strtoull(value, NULL, 10);
1152         else {
1153                 int cpus = cpu_count();
1154                 if (cpus > 0)
1155                         max_childs_running = 8 + (8 * cpus);
1156                 else
1157                         max_childs_running = UDEVD_MAX_CHILDS_RUNNING;
1158         }
1159         info("initialize max_childs_running to %u", max_childs_running);
1160
1161         /* clear environment for forked event processes */
1162         clearenv();
1163
1164         /* export log_priority , as called programs may want to follow that setting */
1165         sprintf(udev_log, "UDEV_LOG=%i", udev_log_priority);
1166         putenv(udev_log);
1167         if (debug_trace)
1168                 putenv("DEBUG=1");
1169
1170         maxfd = udevd_sock;
1171         maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
1172         maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
1173         maxfd = UDEV_MAX(maxfd, inotify_fd);
1174
1175         while (!udev_exit) {
1176                 struct udevd_uevent_msg *msg;
1177                 int fdcount;
1178
1179                 FD_ZERO(&readfds);
1180                 FD_SET(signal_pipe[READ_END], &readfds);
1181                 FD_SET(udevd_sock, &readfds);
1182                 FD_SET(uevent_netlink_sock, &readfds);
1183                 if (inotify_fd >= 0)
1184                         FD_SET(inotify_fd, &readfds);
1185
1186                 fdcount = select(maxfd+1, &readfds, NULL, NULL, NULL);
1187                 if (fdcount < 0) {
1188                         if (errno != EINTR)
1189                                 err("error in select: %s", strerror(errno));
1190                         continue;
1191                 }
1192
1193                 /* get control message */
1194                 if (FD_ISSET(udevd_sock, &readfds))
1195                         get_ctrl_msg();
1196
1197                 /* get netlink message */
1198                 if (FD_ISSET(uevent_netlink_sock, &readfds)) {
1199                         msg = get_netlink_msg();
1200                         if (msg)
1201                                 msg_queue_insert(msg);
1202                 }
1203
1204                 /* received a signal, clear our notification pipe */
1205                 if (FD_ISSET(signal_pipe[READ_END], &readfds)) {
1206                         char buf[256];
1207
1208                         read(signal_pipe[READ_END], &buf, sizeof(buf));
1209                 }
1210
1211                 /* rules directory inotify watch */
1212                 if ((inotify_fd >= 0) && FD_ISSET(inotify_fd, &readfds)) {
1213                         int nbytes;
1214
1215                         /* discard all possible events, we can just reload the config */
1216                         if ((ioctl(inotify_fd, FIONREAD, &nbytes) == 0) && nbytes) {
1217                                 char *buf;
1218
1219                                 reload_config = 1;
1220                                 buf = malloc(nbytes);
1221                                 if (!buf) {
1222                                         err("error getting buffer for inotify, disable watching");
1223                                         close(inotify_fd);
1224                                         inotify_fd = -1;
1225                                 }
1226                                 read(inotify_fd, buf, nbytes);
1227                                 free(buf);
1228                         }
1229                 }
1230
1231                 /* rules changed, set by inotify or a HUP signal */
1232                 if (reload_config) {
1233                         reload_config = 0;
1234                         udev_rules_cleanup(&rules);
1235                         udev_rules_init(&rules, 1);
1236                 }
1237
1238                 /* forked child has returned */
1239                 if (sigchilds_waiting) {
1240                         sigchilds_waiting = 0;
1241                         reap_sigchilds();
1242                 }
1243
1244                 if (run_exec_q) {
1245                         run_exec_q = 0;
1246                         if (!stop_exec_q)
1247                                 msg_queue_manager();
1248                 }
1249         }
1250         rc = 0;
1251
1252 exit:
1253         udev_rules_cleanup(&rules);
1254         sysfs_cleanup();
1255         selinux_exit();
1256
1257         if (signal_pipe[READ_END] >= 0)
1258                 close(signal_pipe[READ_END]);
1259         if (signal_pipe[WRITE_END] >= 0)
1260                 close(signal_pipe[WRITE_END]);
1261
1262         if (udevd_sock >= 0)
1263                 close(udevd_sock);
1264         if (inotify_fd >= 0)
1265                 close(inotify_fd);
1266         if (uevent_netlink_sock >= 0)
1267                 close(uevent_netlink_sock);
1268
1269         logging_close();
1270
1271         return rc;
1272 }