chiark / gitweb /
Make sure to clean up all the built sources.
[elogind.git] / udev / udevd.c
1 /*
2  * Copyright (C) 2004-2009 Kay Sievers <kay.sievers@vrfy.org>
3  * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
4  * Copyright (C) 2009 Canonical Ltd.
5  * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <stddef.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <getopt.h>
33 #include <dirent.h>
34 #include <sys/time.h>
35 #include <sys/prctl.h>
36 #include <sys/socket.h>
37 #include <sys/signalfd.h>
38 #include <sys/select.h>
39 #include <sys/poll.h>
40 #include <sys/wait.h>
41 #include <sys/stat.h>
42 #include <sys/ioctl.h>
43 #include <sys/inotify.h>
44
45 #include "udev.h"
46
47 #define UDEVD_PRIORITY                  -4
48 #define UDEV_PRIORITY                   -2
49
50 static bool debug;
51
52 static void log_fn(struct udev *udev, int priority,
53                    const char *file, int line, const char *fn,
54                    const char *format, va_list args)
55 {
56         if (debug) {
57                 char buf[1024];
58                 struct timeval tv;
59                 struct timezone tz;
60
61                 vsnprintf(buf, sizeof(buf), format, args);
62                 gettimeofday(&tv, &tz);
63                 fprintf(stderr, "%llu.%06u [%u] %s: %s",
64                         (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec,
65                         (int) getpid(), fn, buf);
66         } else {
67                 vsyslog(priority, format, args);
68         }
69 }
70
71 static bool debug_trace;
72 static struct udev_rules *rules;
73 static struct udev_queue_export *udev_queue_export;
74 static struct udev_ctrl *udev_ctrl;
75 static struct udev_monitor *monitor;
76 static int worker_watch[2];
77 static pid_t settle_pid;
78 static bool stop_exec_queue;
79 static bool reload_config;
80 static int max_childs;
81 static int childs;
82 static struct udev_list_node event_list;
83 static struct udev_list_node worker_list;
84 static bool udev_exit;
85 static volatile sig_atomic_t worker_exit;
86
87 enum poll_fd {
88         FD_CONTROL,
89         FD_NETLINK,
90         FD_INOTIFY,
91         FD_SIGNAL,
92         FD_WORKER,
93 };
94
95 static struct pollfd pfd[] = {
96         [FD_NETLINK] = { .events = POLLIN },
97         [FD_WORKER] =  { .events = POLLIN },
98         [FD_SIGNAL] =  { .events = POLLIN },
99         [FD_INOTIFY] = { .events = POLLIN },
100         [FD_CONTROL] = { .events = POLLIN },
101 };
102
103 enum event_state {
104         EVENT_UNDEF,
105         EVENT_QUEUED,
106         EVENT_RUNNING,
107 };
108
109 struct event {
110         struct udev_list_node node;
111         struct udev *udev;
112         struct udev_device *dev;
113         enum event_state state;
114         int exitcode;
115         unsigned long long int delaying_seqnum;
116         unsigned long long int seqnum;
117         const char *devpath;
118         size_t devpath_len;
119         const char *devpath_old;
120 };
121
122 static struct event *node_to_event(struct udev_list_node *node)
123 {
124         char *event;
125
126         event = (char *)node;
127         event -= offsetof(struct event, node);
128         return (struct event *)event;
129 }
130
131 enum worker_state {
132         WORKER_UNDEF,
133         WORKER_RUNNING,
134         WORKER_IDLE,
135         WORKER_KILLED,
136 };
137
138 struct worker {
139         struct udev_list_node node;
140         struct udev *udev;
141         int refcount;
142         pid_t pid;
143         struct udev_monitor *monitor;
144         enum worker_state state;
145         struct event *event;
146 };
147
148 /* passed from worker to main process */
149 struct worker_message {
150         pid_t pid;
151         int exitcode;
152 };
153
154 static struct worker *node_to_worker(struct udev_list_node *node)
155 {
156         char *worker;
157
158         worker = (char *)node;
159         worker -= offsetof(struct worker, node);
160         return (struct worker *)worker;
161 }
162
163 static void event_queue_delete(struct event *event)
164 {
165         udev_list_node_remove(&event->node);
166
167         /* mark as failed, if "add" event returns non-zero */
168         if (event->exitcode != 0 && strcmp(udev_device_get_action(event->dev), "add") == 0)
169                 udev_queue_export_device_failed(udev_queue_export, event->dev);
170         else
171                 udev_queue_export_device_finished(udev_queue_export, event->dev);
172
173         info(event->udev, "seq %llu done with %i\n", udev_device_get_seqnum(event->dev), event->exitcode);
174         udev_device_unref(event->dev);
175         free(event);
176 }
177
178 static void event_sig_handler(int signum)
179 {
180         switch (signum) {
181         case SIGALRM:
182                 _exit(1);
183                 break;
184         case SIGTERM:
185                 worker_exit = true;
186                 break;
187         }
188 }
189
190 static struct worker *worker_ref(struct worker *worker)
191 {
192         worker->refcount++;
193         return worker;
194 }
195
196 static void worker_unref(struct worker *worker)
197 {
198         worker->refcount--;
199         if (worker->refcount > 0)
200                 return;
201
202         udev_list_node_remove(&worker->node);
203         udev_monitor_unref(worker->monitor);
204         childs--;
205         info(worker->udev, "worker [%u] cleaned up\n", worker->pid);
206         free(worker);
207 }
208
209 static void worker_new(struct event *event)
210 {
211         struct worker *worker;
212         struct udev_monitor *worker_monitor;
213         pid_t pid;
214         struct sigaction act;
215
216         /* listen for new events */
217         worker_monitor = udev_monitor_new_from_netlink(event->udev, NULL);
218         if (worker_monitor == NULL)
219                 return;
220         /* allow the main daemon netlink address to send devices to the worker */
221         udev_monitor_allow_unicast_sender(worker_monitor, monitor);
222         udev_monitor_enable_receiving(worker_monitor);
223         util_set_fd_cloexec(udev_monitor_get_fd(worker_monitor));
224
225         worker = calloc(1, sizeof(struct worker));
226         if (worker == NULL)
227                 return;
228         /* worker + event reference */
229         worker->refcount = 2;
230         worker->udev = event->udev;
231
232         pid = fork();
233         switch (pid) {
234         case 0: {
235                 sigset_t sigmask;
236                 struct udev_device *dev;
237                 struct pollfd pmon = {
238                         .fd = udev_monitor_get_fd(worker_monitor),
239                         .events = POLLIN,
240                 };
241
242                 udev_queue_export_unref(udev_queue_export);
243                 udev_monitor_unref(monitor);
244                 udev_ctrl_unref(udev_ctrl);
245                 close(pfd[FD_SIGNAL].fd);
246                 close(worker_watch[READ_END]);
247                 udev_log_close();
248                 udev_log_init("udevd-work");
249                 setpriority(PRIO_PROCESS, 0, UDEV_PRIORITY);
250
251                 /* set signal handlers */
252                 memset(&act, 0x00, sizeof(act));
253                 act.sa_handler = event_sig_handler;
254                 sigemptyset (&act.sa_mask);
255                 act.sa_flags = 0;
256                 sigaction(SIGTERM, &act, NULL);
257                 sigaction(SIGALRM, &act, NULL);
258
259                 /* unblock SIGALRM */
260                 sigfillset(&sigmask);
261                 sigdelset(&sigmask, SIGALRM);
262                 sigprocmask(SIG_SETMASK, &sigmask, NULL);
263                 /* SIGTERM is unblocked in ppoll() */
264                 sigdelset(&sigmask, SIGTERM);
265
266                 /* request TERM signal if parent exits */
267                 prctl(PR_SET_PDEATHSIG, SIGTERM);
268
269                 /* initial device */
270                 dev = event->dev;
271
272                 do {
273                         struct udev_event *udev_event;
274                         struct worker_message msg = {};
275                         int err;
276                         int failed = 0;
277
278                         info(event->udev, "seq %llu running\n", udev_device_get_seqnum(dev));
279                         udev_event = udev_event_new(dev);
280                         if (udev_event == NULL)
281                                 _exit(3);
282
283                         /* set timeout to prevent hanging processes */
284                         alarm(UDEV_EVENT_TIMEOUT);
285
286                         /* apply rules, create node, symlinks */
287                         err = udev_event_execute_rules(udev_event, rules);
288
289                         /* rules may change/disable the timeout */
290                         if (udev_device_get_event_timeout(dev) >= 0)
291                                 alarm(udev_device_get_event_timeout(dev));
292
293                         /* execute RUN= */
294                         if (err == 0 && !udev_event->ignore_device && udev_get_run(udev_event->udev))
295                                 failed = udev_event_execute_run(udev_event);
296
297                         /* reset alarm */
298                         alarm(0);
299
300                         /* apply/restore inotify watch */
301                         if (err == 0 && udev_event->inotify_watch) {
302                                 udev_watch_begin(udev_event->udev, dev);
303                                 udev_device_update_db(dev);
304                         }
305
306                         /* send processed event back to libudev listeners */
307                         udev_monitor_send_device(worker_monitor, NULL, dev);
308
309                         /* send back the result of the event execution */
310                         if (err != 0)
311                                 msg.exitcode = err;
312                         else if (failed != 0)
313                                 msg.exitcode = failed;
314                         msg.pid = getpid();
315                         send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
316
317                         info(event->udev, "seq %llu processed with %i\n", udev_device_get_seqnum(dev), err);
318                         udev_event_unref(udev_event);
319                         udev_device_unref(dev);
320                         dev = NULL;
321
322                         /* wait for more device messages or signal from udevd */
323                         while (!worker_exit) {
324                                 int fdcount;
325
326                                 fdcount = ppoll(&pmon, 1, NULL, &sigmask);
327                                 if (fdcount < 0)
328                                         continue;
329
330                                 if (pmon.revents & POLLIN) {
331                                         dev = udev_monitor_receive_device(worker_monitor);
332                                         if (dev != NULL)
333                                                 break;
334                                 }
335                         }
336                 } while (dev != NULL);
337
338                 udev_monitor_unref(worker_monitor);
339                 udev_log_close();
340                 exit(0);
341         }
342         case -1:
343                 udev_monitor_unref(worker_monitor);
344                 event->state = EVENT_QUEUED;
345                 free(worker);
346                 err(event->udev, "fork of child failed: %m\n");
347                 break;
348         default:
349                 /* close monitor, but keep address around */
350                 udev_monitor_disconnect(worker_monitor);
351                 worker->monitor = worker_monitor;
352                 worker->pid = pid;
353                 worker->state = WORKER_RUNNING;
354                 worker->event = event;
355                 event->state = EVENT_RUNNING;
356                 udev_list_node_append(&worker->node, &worker_list);
357                 childs++;
358                 info(event->udev, "seq %llu forked new worker [%u]\n", udev_device_get_seqnum(event->dev), pid);
359                 break;
360         }
361 }
362
363 static void event_run(struct event *event)
364 {
365         struct udev_list_node *loop;
366
367         udev_list_node_foreach(loop, &worker_list) {
368                 struct worker *worker = node_to_worker(loop);
369                 ssize_t count;
370
371                 if (worker->state != WORKER_IDLE)
372                         continue;
373
374                 count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
375                 if (count < 0) {
376                         err(event->udev, "worker [%u] did not accept message %zi (%m), kill it\n", worker->pid, count);
377                         kill(worker->pid, SIGKILL);
378                         worker->state = WORKER_KILLED;
379                         continue;
380                 }
381                 worker_ref(worker);
382                 worker->event = event;
383                 worker->state = WORKER_RUNNING;
384                 event->state = EVENT_RUNNING;
385                 return;
386         }
387
388         if (childs >= max_childs) {
389                 info(event->udev, "maximum number (%i) of childs reached\n", childs);
390                 return;
391         }
392
393         /* start new worker and pass initial device */
394         worker_new(event);
395 }
396
397 static void event_queue_insert(struct udev_device *dev)
398 {
399         struct event *event;
400
401         event = calloc(1, sizeof(struct event));
402         if (event == NULL)
403                 return;
404
405         event->udev = udev_device_get_udev(dev);
406         event->dev = dev;
407         event->seqnum = udev_device_get_seqnum(dev);
408         event->devpath = udev_device_get_devpath(dev);
409         event->devpath_len = strlen(event->devpath);
410         event->devpath_old = udev_device_get_devpath_old(dev);
411
412         udev_queue_export_device_queued(udev_queue_export, dev);
413         info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
414              udev_device_get_action(dev), udev_device_get_subsystem(dev));
415
416         event->state = EVENT_QUEUED;
417         udev_list_node_append(&event->node, &event_list);
418
419         /* run all events with a timeout set immediately */
420         if (udev_device_get_timeout(dev) > 0) {
421                 worker_new(event);
422                 return;
423         }
424 }
425
426 static void worker_kill(struct udev *udev, int retain)
427 {
428         struct udev_list_node *loop;
429         int max;
430
431         if (childs <= retain)
432                 return;
433
434         max = childs - retain;
435
436         udev_list_node_foreach(loop, &worker_list) {
437                 struct worker *worker = node_to_worker(loop);
438
439                 if (max-- <= 0)
440                         break;
441
442                 if (worker->state == WORKER_KILLED)
443                         continue;
444
445                 worker->state = WORKER_KILLED;
446                 kill(worker->pid, SIGTERM);
447         }
448 }
449
450 static int mem_size_mb(void)
451 {
452         FILE *f;
453         char buf[4096];
454         long int memsize = -1;
455
456         f = fopen("/proc/meminfo", "r");
457         if (f == NULL)
458                 return -1;
459
460         while (fgets(buf, sizeof(buf), f) != NULL) {
461                 long int value;
462
463                 if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
464                         memsize = value / 1024;
465                         break;
466                 }
467         }
468
469         fclose(f);
470         return memsize;
471 }
472
473 /* lookup event for identical, parent, child device */
474 static int devpath_busy(struct event *event)
475 {
476         struct udev_list_node *loop;
477         size_t common;
478
479         /* check if queue contains events we depend on */
480         udev_list_node_foreach(loop, &event_list) {
481                 struct event *loop_event = node_to_event(loop);
482
483                 /* we already found a later event, earlier can not block us, no need to check again */
484                 if (loop_event->seqnum < event->delaying_seqnum)
485                         continue;
486
487                 /* event we checked earlier still exists, no need to check again */
488                 if (loop_event->seqnum == event->delaying_seqnum)
489                         return 2;
490
491                 /* found ourself, no later event can block us */
492                 if (loop_event->seqnum >= event->seqnum)
493                         break;
494
495                 /* check our old name */
496                 if (event->devpath_old != NULL)
497                         if (strcmp(loop_event->devpath, event->devpath_old) == 0) {
498                                 event->delaying_seqnum = loop_event->seqnum;
499                                 return 3;
500                         }
501
502                 /* compare devpath */
503                 common = MIN(loop_event->devpath_len, event->devpath_len);
504
505                 /* one devpath is contained in the other? */
506                 if (memcmp(loop_event->devpath, event->devpath, common) != 0)
507                         continue;
508
509                 /* identical device event found */
510                 if (loop_event->devpath_len == event->devpath_len) {
511                         event->delaying_seqnum = loop_event->seqnum;
512                         return 4;
513                 }
514
515                 /* parent device event found */
516                 if (event->devpath[common] == '/') {
517                         event->delaying_seqnum = loop_event->seqnum;
518                         return 5;
519                 }
520
521                 /* child device event found */
522                 if (loop_event->devpath[common] == '/') {
523                         event->delaying_seqnum = loop_event->seqnum;
524                         return 6;
525                 }
526
527                 /* no matching device */
528                 continue;
529         }
530
531         return 0;
532 }
533
534 static void events_start(struct udev *udev)
535 {
536         struct udev_list_node *loop;
537
538         udev_list_node_foreach(loop, &event_list) {
539                 struct event *event = node_to_event(loop);
540
541                 if (event->state != EVENT_QUEUED)
542                         continue;
543
544                 /* do not start event if parent or child event is still running */
545                 if (devpath_busy(event) != 0) {
546                         dbg(udev, "delay seq %llu (%s)\n", event->seqnum, event->devpath);
547                         continue;
548                 }
549
550                 event_run(event);
551         }
552 }
553
554 static void worker_returned(void)
555 {
556         while (1) {
557                 struct worker_message msg;
558                 ssize_t size;
559                 struct udev_list_node *loop;
560
561                 size = recv(pfd[FD_WORKER].fd, &msg, sizeof(struct worker_message), MSG_DONTWAIT);
562                 if (size != sizeof(struct worker_message))
563                         break;
564
565                 /* lookup worker who sent the signal */
566                 udev_list_node_foreach(loop, &worker_list) {
567                         struct worker *worker = node_to_worker(loop);
568
569                         if (worker->pid != msg.pid)
570                                 continue;
571
572                         /* worker returned */
573                         worker->event->exitcode = msg.exitcode;
574                         event_queue_delete(worker->event);
575                         worker->event = NULL;
576                         if (worker->state != WORKER_KILLED)
577                                 worker->state = WORKER_IDLE;
578                         worker_unref(worker);
579                         break;
580                 }
581         }
582 }
583
584 /* receive the udevd message from userspace */
585 static void handle_ctrl_msg(struct udev_ctrl *uctrl)
586 {
587         struct udev *udev = udev_ctrl_get_udev(uctrl);
588         struct udev_ctrl_msg *ctrl_msg;
589         const char *str;
590         int i;
591
592         ctrl_msg = udev_ctrl_receive_msg(uctrl);
593         if (ctrl_msg == NULL)
594                 return;
595
596         i = udev_ctrl_get_set_log_level(ctrl_msg);
597         if (i >= 0) {
598                 info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
599                 udev_set_log_priority(udev, i);
600                 worker_kill(udev, 0);
601         }
602
603         if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
604                 info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
605                 stop_exec_queue = true;
606         }
607
608         if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
609                 info(udev, "udevd message (START_EXEC_QUEUE) received\n");
610                 stop_exec_queue = false;
611         }
612
613         if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
614                 info(udev, "udevd message (RELOAD_RULES) received\n");
615                 reload_config = true;
616         }
617
618         str = udev_ctrl_get_set_env(ctrl_msg);
619         if (str != NULL) {
620                 char *key;
621
622                 key = strdup(str);
623                 if (key != NULL) {
624                         char *val;
625
626                         val = strchr(key, '=');
627                         if (val != NULL) {
628                                 val[0] = '\0';
629                                 val = &val[1];
630                                 if (val[0] == '\0') {
631                                         info(udev, "udevd message (ENV) received, unset '%s'\n", key);
632                                         udev_add_property(udev, key, NULL);
633                                 } else {
634                                         info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
635                                         udev_add_property(udev, key, val);
636                                 }
637                         } else {
638                                 err(udev, "wrong key format '%s'\n", key);
639                         }
640                         free(key);
641                 }
642                 worker_kill(udev, 0);
643         }
644
645         i = udev_ctrl_get_set_max_childs(ctrl_msg);
646         if (i >= 0) {
647                 info(udev, "udevd message (SET_MAX_CHILDS) received, max_childs=%i\n", i);
648                 max_childs = i;
649         }
650
651         settle_pid = udev_ctrl_get_settle(ctrl_msg);
652         if (settle_pid > 0) {
653                 info(udev, "udevd message (SETTLE) received\n");
654                 kill(settle_pid, SIGUSR1);
655                 settle_pid = 0;
656         }
657         udev_ctrl_msg_unref(ctrl_msg);
658 }
659
660 /* read inotify messages */
661 static int handle_inotify(struct udev *udev)
662 {
663         ssize_t nbytes, pos;
664         char *buf;
665         struct inotify_event *ev;
666
667         if ((ioctl(pfd[FD_INOTIFY].fd, FIONREAD, &nbytes) < 0) || (nbytes <= 0))
668                 return 0;
669
670         buf = malloc(nbytes);
671         if (buf == NULL) {
672                 err(udev, "error getting buffer for inotify\n");
673                 return -1;
674         }
675
676         nbytes = read(pfd[FD_INOTIFY].fd, buf, nbytes);
677
678         for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) {
679                 struct udev_device *dev;
680
681                 ev = (struct inotify_event *)(buf + pos);
682                 if (ev->len) {
683                         dbg(udev, "inotify event: %x for %s\n", ev->mask, ev->name);
684                         reload_config = true;
685                         continue;
686                 }
687
688                 dev = udev_watch_lookup(udev, ev->wd);
689                 if (dev != NULL) {
690                         dbg(udev, "inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
691                         if (ev->mask & IN_CLOSE_WRITE) {
692                                 char filename[UTIL_PATH_SIZE];
693                                 int fd;
694
695                                 info(udev, "device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
696                                 util_strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
697                                 fd = open(filename, O_WRONLY);
698                                 if (fd < 0 || write(fd, "change", 6) < 0)
699                                         info(udev, "error writing uevent: %m\n");
700                                 close(fd);
701                         }
702                         if (ev->mask & IN_IGNORED)
703                                 udev_watch_end(udev, dev);
704
705                         udev_device_unref(dev);
706                 }
707
708         }
709
710         free(buf);
711         return 0;
712 }
713
714 static void handle_signal(struct udev *udev, int signo)
715 {
716         switch (signo) {
717         case SIGINT:
718         case SIGTERM:
719                 udev_exit = true;
720                 break;
721         case SIGCHLD:
722                 while (1) {
723                         pid_t pid;
724                         int status;
725                         struct udev_list_node *loop, *tmp;
726
727                         pid = waitpid(-1, &status, WNOHANG);
728                         if (pid <= 0)
729                                 break;
730
731                         udev_list_node_foreach_safe(loop, tmp, &worker_list) {
732                                 struct worker *worker = node_to_worker(loop);
733
734                                 if (worker->pid != pid)
735                                         continue;
736
737                                 info(udev, "worker [%u] exit\n", pid);
738                                 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
739                                         err(udev, "worker [%u] unexpectedly returned with status 0x%04x\n", pid, status);
740                                         if (worker->event != NULL) {
741                                                 err(udev, "worker [%u] failed while handling '%s'\n", pid, worker->event->devpath);
742                                                 worker->event->exitcode = -32;
743                                                 event_queue_delete(worker->event);
744                                                 /* drop reference from running event */
745                                                 worker_unref(worker);
746                                         }
747                                 }
748                                 worker_unref(worker);
749                                 break;
750                         }
751                 }
752                 break;
753         case SIGHUP:
754                 reload_config = true;
755                 break;
756         }
757 }
758
759 static void startup_log(struct udev *udev)
760 {
761         FILE *f;
762         char path[UTIL_PATH_SIZE];
763         struct stat statbuf;
764
765         f = fopen("/dev/kmsg", "w");
766         if (f != NULL)
767                 fprintf(f, "<6>udev: starting version " VERSION "\n");
768
769         util_strscpyl(path, sizeof(path), udev_get_sys_path(udev), "/class/mem/null", NULL);
770         if (lstat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) {
771                 const char *depr_str =
772                         "udev: missing sysfs features; please update the kernel "
773                         "or disable the kernel's CONFIG_SYSFS_DEPRECATED option; "
774                         "udev may fail to work correctly";
775
776                 if (f != NULL)
777                         fprintf(f, "<3>%s\n", depr_str);
778                 err(udev, "%s\n", depr_str);
779                 sleep(3);
780         }
781
782         if (f != NULL)
783                 fclose(f);
784 }
785
786 int main(int argc, char *argv[])
787 {
788         struct udev *udev;
789         int fd;
790         sigset_t mask;
791         const char *value;
792         int daemonize = false;
793         int resolve_names = 1;
794         static const struct option options[] = {
795                 { "daemon", no_argument, NULL, 'd' },
796                 { "debug-trace", no_argument, NULL, 't' },
797                 { "debug", no_argument, NULL, 'D' },
798                 { "help", no_argument, NULL, 'h' },
799                 { "version", no_argument, NULL, 'V' },
800                 { "resolve-names", required_argument, NULL, 'N' },
801                 {}
802         };
803         int rc = 1;
804
805         udev = udev_new();
806         if (udev == NULL)
807                 goto exit;
808
809         udev_log_init("udevd");
810         udev_set_log_fn(udev, log_fn);
811         info(udev, "version %s\n", VERSION);
812         udev_selinux_init(udev);
813
814         while (1) {
815                 int option;
816
817                 option = getopt_long(argc, argv, "dDthV", options, NULL);
818                 if (option == -1)
819                         break;
820
821                 switch (option) {
822                 case 'd':
823                         daemonize = true;
824                         break;
825                 case 't':
826                         debug_trace = true;
827                         break;
828                 case 'D':
829                         debug = true;
830                         if (udev_get_log_priority(udev) < LOG_INFO)
831                                 udev_set_log_priority(udev, LOG_INFO);
832                         break;
833                 case 'N':
834                         if (strcmp (optarg, "early") == 0) {
835                                 resolve_names = 1;
836                         } else if (strcmp (optarg, "late") == 0) {
837                                 resolve_names = 0;
838                         } else if (strcmp (optarg, "never") == 0) {
839                                 resolve_names = -1;
840                         } else {
841                                 fprintf(stderr, "resolve-names must be early, late or never\n");
842                                 err(udev, "resolve-names must be early, late or never\n");
843                                 goto exit;
844                         }
845                         break;
846                 case 'h':
847                         printf("Usage: udevd [--help] [--daemon] [--debug-trace] [--debug] "
848                                "[--resolve-names=early|late|never] [--version]\n");
849                         goto exit;
850                 case 'V':
851                         printf("%s\n", VERSION);
852                         goto exit;
853                 default:
854                         goto exit;
855                 }
856         }
857
858         if (getuid() != 0) {
859                 fprintf(stderr, "root privileges required\n");
860                 err(udev, "root privileges required\n");
861                 goto exit;
862         }
863
864         /* make sure std{in,out,err} fd's are in a sane state */
865         fd = open("/dev/null", O_RDWR);
866         if (fd < 0) {
867                 fprintf(stderr, "cannot open /dev/null\n");
868                 err(udev, "cannot open /dev/null\n");
869         }
870         if (write(STDOUT_FILENO, 0, 0) < 0)
871                 dup2(fd, STDOUT_FILENO);
872         if (write(STDERR_FILENO, 0, 0) < 0)
873                 dup2(fd, STDERR_FILENO);
874
875         /* init control socket, bind() ensures, that only one udevd instance is running */
876         udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
877         if (udev_ctrl == NULL) {
878                 fprintf(stderr, "error initializing control socket");
879                 err(udev, "error initializing udevd socket");
880                 rc = 1;
881                 goto exit;
882         }
883         if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
884                 fprintf(stderr, "error binding control socket, seems udevd is already running\n");
885                 err(udev, "error binding control socket, seems udevd is already running\n");
886                 rc = 1;
887                 goto exit;
888         }
889         pfd[FD_CONTROL].fd = udev_ctrl_get_fd(udev_ctrl);
890
891         monitor = udev_monitor_new_from_netlink(udev, "kernel");
892         if (monitor == NULL || udev_monitor_enable_receiving(monitor) < 0) {
893                 fprintf(stderr, "error initializing netlink socket\n");
894                 err(udev, "error initializing netlink socket\n");
895                 rc = 3;
896                 goto exit;
897         }
898         udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
899         pfd[FD_NETLINK].fd = udev_monitor_get_fd(monitor);
900
901         pfd[FD_INOTIFY].fd = udev_watch_init(udev);
902         if (pfd[FD_INOTIFY].fd < 0) {
903                 fprintf(stderr, "error initializing inotify\n");
904                 err(udev, "error initializing inotify\n");
905                 rc = 4;
906                 goto exit;
907         }
908
909         if (udev_get_rules_path(udev) != NULL) {
910                 inotify_add_watch(pfd[FD_INOTIFY].fd, udev_get_rules_path(udev),
911                                   IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
912         } else {
913                 char filename[UTIL_PATH_SIZE];
914
915                 inotify_add_watch(pfd[FD_INOTIFY].fd, LIBEXECDIR "/rules.d",
916                                   IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
917                 inotify_add_watch(pfd[FD_INOTIFY].fd, SYSCONFDIR "/udev/rules.d",
918                                   IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
919
920                 /* watch dynamic rules directory */
921                 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/rules.d", NULL);
922                 inotify_add_watch(pfd[FD_INOTIFY].fd, filename,
923                                   IN_CREATE | IN_DELETE | IN_MOVE | IN_CLOSE_WRITE);
924         }
925         udev_watch_restore(udev);
926
927         /* block and listen to all signals on signalfd */
928         sigfillset(&mask);
929         sigprocmask(SIG_SETMASK, &mask, NULL);
930         pfd[FD_SIGNAL].fd = signalfd(-1, &mask, 0);
931         if (pfd[FD_SIGNAL].fd < 0) {
932                 fprintf(stderr, "error getting signalfd\n");
933                 err(udev, "error getting signalfd\n");
934                 rc = 5;
935                 goto exit;
936         }
937
938         /* unnamed socket from workers to the main daemon */
939         if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, worker_watch) < 0) {
940                 fprintf(stderr, "error getting socketpair\n");
941                 err(udev, "error getting socketpair\n");
942                 rc = 6;
943                 goto exit;
944         }
945         pfd[FD_WORKER].fd = worker_watch[READ_END];
946         util_set_fd_cloexec(worker_watch[WRITE_END]);
947
948         rules = udev_rules_new(udev, resolve_names);
949         if (rules == NULL) {
950                 err(udev, "error reading rules\n");
951                 goto exit;
952         }
953
954         udev_queue_export = udev_queue_export_new(udev);
955         if (udev_queue_export == NULL) {
956                 err(udev, "error creating queue file\n");
957                 goto exit;
958         }
959
960         if (daemonize) {
961                 pid_t pid;
962
963                 pid = fork();
964                 switch (pid) {
965                 case 0:
966                         break;
967                 case -1:
968                         err(udev, "fork of daemon failed: %m\n");
969                         rc = 4;
970                         goto exit;
971                 default:
972                         rc = 0;
973                         goto exit;
974                 }
975         }
976
977         startup_log(udev);
978
979         /* redirect std{out,err} */
980         if (!debug && !debug_trace) {
981                 dup2(fd, STDIN_FILENO);
982                 dup2(fd, STDOUT_FILENO);
983                 dup2(fd, STDERR_FILENO);
984         }
985         if (fd > STDERR_FILENO)
986                 close(fd);
987
988         /* set scheduling priority for the daemon */
989         setpriority(PRIO_PROCESS, 0, UDEVD_PRIORITY);
990
991         chdir("/");
992         umask(022);
993         setsid();
994
995         /* OOM_DISABLE == -17 */
996         fd = open("/proc/self/oom_adj", O_RDWR);
997         if (fd < 0) {
998                 err(udev, "error disabling OOM: %m\n");
999         } else {
1000                 write(fd, "-17", 3);
1001                 close(fd);
1002         }
1003
1004         /* in trace mode run one event after the other */
1005         if (debug_trace) {
1006                 max_childs = 1;
1007         } else {
1008                 int memsize = mem_size_mb();
1009
1010                 if (memsize > 0)
1011                         max_childs = 128 + (memsize / 8);
1012                 else
1013                         max_childs = 128;
1014         }
1015
1016         /* possibly overwrite maximum limit of executed events */
1017         value = getenv("UDEVD_MAX_CHILDS");
1018         if (value)
1019                 max_childs = strtoul(value, NULL, 10);
1020         info(udev, "initialize max_childs to %u\n", max_childs);
1021
1022         udev_list_init(&event_list);
1023         udev_list_init(&worker_list);
1024
1025         while (!udev_exit) {
1026                 int fdcount;
1027                 int timeout;
1028
1029                 /* set timeout to kill idle workers */
1030                 if (udev_list_is_empty(&event_list) && childs > 2)
1031                         timeout = 3 * 1000;
1032                 else
1033                         timeout = -1;
1034                 /* wait for events */
1035                 fdcount = poll(pfd, ARRAY_SIZE(pfd), timeout);
1036                 if (fdcount < 0)
1037                         continue;
1038
1039                 /* timeout - kill idle workers */
1040                 if (fdcount == 0)
1041                         worker_kill(udev, 2);
1042
1043                 /* event has finished */
1044                 if (pfd[FD_WORKER].revents & POLLIN)
1045                         worker_returned();
1046
1047                 /* get kernel uevent */
1048                 if (pfd[FD_NETLINK].revents & POLLIN) {
1049                         struct udev_device *dev;
1050
1051                         dev = udev_monitor_receive_device(monitor);
1052                         if (dev != NULL)
1053                                 event_queue_insert(dev);
1054                         else
1055                                 udev_device_unref(dev);
1056                 }
1057
1058                 /* start new events */
1059                 if (!udev_list_is_empty(&event_list) && !stop_exec_queue)
1060                         events_start(udev);
1061
1062                 /* get signal */
1063                 if (pfd[FD_SIGNAL].revents & POLLIN) {
1064                         struct signalfd_siginfo fdsi;
1065                         ssize_t size;
1066
1067                         size = read(pfd[FD_SIGNAL].fd, &fdsi, sizeof(struct signalfd_siginfo));
1068                         if (size == sizeof(struct signalfd_siginfo))
1069                                 handle_signal(udev, fdsi.ssi_signo);
1070                 }
1071
1072                 /* device node and rules directory inotify watch */
1073                 if (pfd[FD_INOTIFY].revents & POLLIN)
1074                         handle_inotify(udev);
1075
1076                 /*
1077                  * get control message
1078                  *
1079                  * This needs to be after the inotify handling, to make sure,
1080                  * that the settle signal is send back after the possibly generated
1081                  * "change" events by the inotify device node watch.
1082                  */
1083                 if (pfd[FD_CONTROL].revents & POLLIN)
1084                         handle_ctrl_msg(udev_ctrl);
1085
1086                 /* rules changed, set by inotify or a HUP signal */
1087                 if (reload_config) {
1088                         struct udev_rules *rules_new;
1089
1090                         worker_kill(udev, 0);
1091                         rules_new = udev_rules_new(udev, resolve_names);
1092                         if (rules_new != NULL) {
1093                                 udev_rules_unref(rules);
1094                                 rules = rules_new;
1095                         }
1096                         reload_config = 0;
1097                 }
1098         }
1099
1100         udev_queue_export_cleanup(udev_queue_export);
1101         rc = 0;
1102 exit:
1103         udev_queue_export_unref(udev_queue_export);
1104         udev_rules_unref(rules);
1105         udev_ctrl_unref(udev_ctrl);
1106         if (pfd[FD_SIGNAL].fd >= 0)
1107                 close(pfd[FD_SIGNAL].fd);
1108         if (worker_watch[READ_END] >= 0)
1109                 close(worker_watch[READ_END]);
1110         if (worker_watch[WRITE_END] >= 0)
1111                 close(worker_watch[WRITE_END]);
1112         udev_monitor_unref(monitor);
1113         udev_selinux_exit(udev);
1114         udev_unref(udev);
1115         udev_log_close();
1116         return rc;
1117 }