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