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