chiark / gitweb /
volume_id: update README
[elogind.git] / extras / run_directory / udev_run_devd.c
1 /*
2  * udev_run_devd.c - directory multiplexer
3  *
4  * Copyright (C) 2005 Kay Sievers <kay@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  *
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stddef.h>
16 #include <dirent.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <sys/stat.h>
24
25 #include "../../udev.h"
26 #include "run_directory.h"
27
28
29 #ifdef USE_LOG
30 void log_message (int priority, const char *format, ...)
31 {
32         va_list args;
33         static int udev_log = -1;
34
35         if (udev_log == -1) {
36                 const char *value;
37
38                 value = getenv("UDEV_LOG");
39                 if (value)
40                         udev_log = log_priority(value);
41                 else
42                         udev_log = LOG_ERR;
43         }
44
45         if (priority > udev_log)
46                 return;
47
48         va_start(args, format);
49         vsyslog(priority, format, args);
50         va_end(args);
51 }
52 #endif
53
54 int main(int argc, char *argv[], char *envp[])
55 {
56         char dirname[NAME_SIZE];
57         const char *devname;
58         const char *my_devname;
59         const char *subsystem;
60         int fd;
61
62         devname = getenv("DEVNAME");
63         if (devname == NULL)
64                 exit(0);
65         /*
66          * Hack, we are assuming that the device nodes are in /dev,
67          * if not, this will not work, but you should be using the
68          * RUN= rule anyway...
69          */
70         my_devname = strstr(devname, "/dev/");
71         if (my_devname != NULL)
72                 my_devname = &my_devname[5];
73         else
74                 my_devname = devname;
75
76         subsystem = argv[1];
77         logging_init("udev_run_devd");
78
79         fd = open("/dev/null", O_RDWR);
80         if (fd >= 0) {
81                 dup2(fd, STDOUT_FILENO);
82                 dup2(fd, STDIN_FILENO);
83                 dup2(fd, STDERR_FILENO);
84                 close(fd);
85         }
86         dbg("running dev.d directory");
87
88         sprintf(dirname, "/etc/dev.d/%s", my_devname);
89         run_directory(dirname, ".dev", subsystem);
90         sprintf(dirname, "/etc/dev.d/%s", subsystem);
91         run_directory(dirname, ".dev", subsystem);
92         run_directory("/etc/dev.d/default", ".dev", subsystem);
93
94         exit(0);
95 }