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