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