chiark / gitweb /
[PATCH] complete removal of explicit udev permissions config file
[elogind.git] / udev.c
1 /*
2  * udev.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
8  *
9  *      This program is free software; you can redistribute it and/or modify it
10  *      under the terms of the GNU General Public License as published by the
11  *      Free Software Foundation version 2 of the License.
12  * 
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  * 
18  *      You should have received a copy of the GNU General Public License along
19  *      with this program; if not, write to the Free Software Foundation, Inc.,
20  *      675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdio.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <unistd.h>
33
34 #include "libsysfs/sysfs/libsysfs.h"
35 #include "udev.h"
36 #include "udev_utils.h"
37 #include "udev_sysfs.h"
38 #include "udev_version.h"
39 #include "namedev.h"
40 #include "logging.h"
41
42
43 #ifdef LOG
44 void log_message(int level, const char *format, ...)
45 {
46         va_list args;
47
48         if (!udev_log)
49                 return;
50
51         va_start(args, format);
52         vsyslog(level, format, args);
53         va_end(args);
54 }
55 #endif
56
57 /* (for now) true if udevsend is the helper */
58 static int manage_hotplug_event(void) {
59         char helper[256];
60         int fd;
61         int len;
62
63         /* false, if we are called directly */
64         if (!getenv("MANAGED_EVENT"))
65                 goto exit;
66
67         fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
68         if (fd < 0)
69                 goto exit;
70
71         len = read(fd, helper, 256);
72         close(fd);
73
74         if (len < 0)
75                 goto exit;
76         helper[len] = '\0';
77
78         if (strstr(helper, "udevsend"))
79                 return 1;
80
81 exit:
82         return 0;
83 }
84
85 static void asmlinkage sig_handler(int signum)
86 {
87         switch (signum) {
88                 case SIGALRM:
89                         exit(1);
90                 case SIGINT:
91                 case SIGTERM:
92                         exit(20 + signum);
93         }
94 }
95
96 int main(int argc, char *argv[], char *envp[])
97 {
98         struct sigaction act;
99         struct sysfs_class_device *class_dev;
100         struct sysfs_device *devices_dev;
101         struct udevice udev;
102         char path[SYSFS_PATH_MAX];
103         const char *error;
104         const char *action;
105         const char *devpath;
106         const char *subsystem;
107         int retval = -EINVAL;
108
109         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
110                 printf("%s\n", UDEV_VERSION);
111                 exit(0);
112         }
113
114         logging_init("udev");
115         dbg("version %s", UDEV_VERSION);
116
117         udev_init_config();
118
119         /* set signal handlers */
120         memset(&act, 0x00, sizeof(act));
121         act.sa_handler = (void (*) (int))sig_handler;
122         sigemptyset (&act.sa_mask);
123         act.sa_flags = 0;
124         sigaction(SIGALRM, &act, NULL);
125         sigaction(SIGINT, &act, NULL);
126         sigaction(SIGTERM, &act, NULL);
127
128         /* trigger timeout to prevent hanging processes */
129         alarm(ALARM_TIMEOUT);
130
131         if (strstr(argv[0], "udevstart") || (argc == 2 && strstr(argv[1], "udevstart"))) {
132                 dbg("udevstart");
133
134                 /* disable all logging, as it's much too slow on some facilities */
135                 udev_log = 0;
136
137                 namedev_init();
138                 retval = udev_start();
139                 goto exit;
140         }
141
142         action = getenv("ACTION");
143         devpath = getenv("DEVPATH");
144         subsystem = getenv("SUBSYSTEM");
145         /* older kernels passed the SUBSYSTEM only as argument */
146         if (!subsystem && argc == 2)
147                 subsystem = argv[1];
148         udev_init_device(&udev, devpath, subsystem);
149
150         if (!action) {
151                 dbg("no action");
152                 goto hotplug;
153         }
154
155         if (!subsystem) {
156                 dbg("no subsystem");
157                 goto hotplug;
158         }
159
160         if (!devpath) {
161                 dbg("no devpath");
162                 goto hotplug;
163         }
164
165         /* export logging flag, as called scripts may want to do the same as udev */
166         if (udev_log)
167                 setenv("UDEV_LOG", "1", 1);
168
169         if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
170                 if (strcmp(action, "add") == 0) {
171                         /* wait for sysfs and possibly add node */
172                         dbg("udev add");
173
174                         /* skip subsystems without "dev", but handle net devices */
175                         if (udev.type != 'n' && subsystem_expect_no_dev(udev.subsystem)) {
176                                 dbg("don't care about '%s' devices", udev.subsystem);
177                                 goto hotplug;
178                         }
179
180                         snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
181                         class_dev = wait_class_device_open(path);
182                         if (class_dev == NULL) {
183                                 dbg ("open class device failed");
184                                 goto hotplug;
185                         }
186                         dbg("opened class_dev->name='%s'", class_dev->name);
187
188                         wait_for_class_device(class_dev, &error);
189
190                         /* init rules */
191                         namedev_init();
192
193                         /* name, create node, store in db */
194                         retval = udev_add_device(&udev, class_dev);
195
196                         sysfs_close_class_device(class_dev);
197                 } else if (strcmp(action, "remove") == 0) {
198                         /* possibly remove a node */
199                         dbg("udev remove");
200
201                         /* skip subsystems without "dev" */
202                         if (subsystem_expect_no_dev(udev.subsystem)) {
203                                 dbg("don't care about '%s' devices", udev.subsystem);
204                                 goto hotplug;
205                         }
206
207                         /* get node from db, remove db-entry, delete created node */
208                         retval = udev_remove_device(&udev);
209                 }
210
211                 /* run dev.d/ scripts if we created/deleted a node or changed a netif name */
212                 if (udev.devname[0] != '\0') {
213                         setenv("DEVNAME", udev.devname, 1);
214                         if (udev_dev_d)
215                                 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
216                 }
217         } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
218                 if (strcmp(action, "add") == 0) {
219                         /* wait for sysfs */
220                         dbg("devices add");
221
222                         snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
223                         devices_dev = wait_devices_device_open(path);
224                         if (!devices_dev) {
225                                 dbg("devices device unavailable (probably remove has beaten us)");
226                                 goto hotplug;
227                         }
228                         dbg("devices device opened '%s'", path);
229
230                         wait_for_devices_device(devices_dev, &error);
231
232                         sysfs_close_device(devices_dev);
233                 } else if (strcmp(action, "remove") == 0) {
234                         dbg("devices remove");
235                 }
236         } else {
237                 dbg("unhandled");
238         }
239
240 hotplug:
241         if (udev_hotplug_d && manage_hotplug_event())
242                 udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);
243
244 exit:
245         logging_close();
246         return retval;
247 }