chiark / gitweb /
[PATCH] allow simple-build-check.sh to go faster if MAKEOPTS is set
[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 /* decide if we should manage the whole hotplug event
58  * for now look if the kernel calls udevsend instead of /sbin/hotplug
59  */
60 static int manage_hotplug_event(void) {
61         char helper[256];
62         int fd;
63         int len;
64
65         /* don't handle hotplug.d if we are called directly */
66         if (!getenv("UDEVD_EVENT"))
67                 return 0;
68
69         fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
70         if (fd < 0)
71                 return 0;
72
73         len = read(fd, helper, 256);
74         close(fd);
75
76         if (len < 0)
77                 return 0;
78         helper[len] = '\0';
79
80         if (strstr(helper, "udevsend"))
81                 return 1;
82
83         return 0;
84 }
85
86 static void asmlinkage sig_handler(int signum)
87 {
88         switch (signum) {
89                 case SIGALRM:
90                         exit(1);
91                 case SIGINT:
92                 case SIGTERM:
93                         exit(20 + signum);
94         }
95 }
96
97 int main(int argc, char *argv[], char *envp[])
98 {
99         struct sigaction act;
100         struct sysfs_class_device *class_dev;
101         struct sysfs_device *devices_dev;
102         struct udevice udev;
103         char path[SYSFS_PATH_MAX];
104         const char *error;
105         const char *action;
106         const char *devpath;
107         const char *subsystem;
108         int managed_event;
109         int retval = -EINVAL;
110
111         if (argc == 2 && strcmp(argv[1], "-V") == 0) {
112                 printf("%s\n", UDEV_VERSION);
113                 exit(0);
114         }
115
116         logging_init("udev");
117         dbg("version %s", UDEV_VERSION);
118
119         udev_init_config();
120
121         /* set signal handlers */
122         memset(&act, 0x00, sizeof(act));
123         act.sa_handler = (void (*) (int))sig_handler;
124         sigemptyset (&act.sa_mask);
125         act.sa_flags = 0;
126         sigaction(SIGALRM, &act, NULL);
127         sigaction(SIGINT, &act, NULL);
128         sigaction(SIGTERM, &act, NULL);
129
130         /* trigger timeout to prevent hanging processes */
131         alarm(ALARM_TIMEOUT);
132
133         if (strstr(argv[0], "udevstart") || (argc == 2 && strstr(argv[1], "udevstart"))) {
134                 dbg("udevstart");
135
136                 /* disable all logging, as it's much too slow on some facilities */
137                 udev_log = 0;
138
139                 namedev_init();
140                 retval = udev_start();
141                 goto exit;
142         }
143
144         /* let the executed programs know if we handle the whole hotplug event */
145         managed_event = manage_hotplug_event();
146         if (managed_event)
147                 setenv("MANAGED_EVENT", "1", 1);
148
149         action = getenv("ACTION");
150         devpath = getenv("DEVPATH");
151         subsystem = getenv("SUBSYSTEM");
152         /* older kernels passed the SUBSYSTEM only as argument */
153         if (!subsystem && argc == 2)
154                 subsystem = argv[1];
155         udev_init_device(&udev, devpath, subsystem);
156
157         if (!action) {
158                 dbg("no action");
159                 goto hotplug;
160         }
161
162         if (!subsystem) {
163                 dbg("no subsystem");
164                 goto hotplug;
165         }
166
167         if (!devpath) {
168                 dbg("no devpath");
169                 goto hotplug;
170         }
171
172         /* export logging flag, as called scripts may want to do the same as udev */
173         if (udev_log)
174                 setenv("UDEV_LOG", "1", 1);
175
176         if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
177                 if (strcmp(action, "add") == 0) {
178                         /* wait for sysfs and possibly add node */
179                         dbg("udev add");
180
181                         /* skip subsystems without "dev", but handle net devices */
182                         if (udev.type != 'n' && subsystem_expect_no_dev(udev.subsystem)) {
183                                 dbg("don't care about '%s' devices", udev.subsystem);
184                                 goto hotplug;
185                         }
186
187                         snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
188                         class_dev = wait_class_device_open(path);
189                         if (class_dev == NULL) {
190                                 dbg ("open class device failed");
191                                 goto hotplug;
192                         }
193                         dbg("opened class_dev->name='%s'", class_dev->name);
194
195                         wait_for_class_device(class_dev, &error);
196
197                         /* init rules */
198                         namedev_init();
199
200                         /* name, create node, store in db */
201                         retval = udev_add_device(&udev, class_dev);
202
203                         sysfs_close_class_device(class_dev);
204                 } else if (strcmp(action, "remove") == 0) {
205                         /* possibly remove a node */
206                         dbg("udev remove");
207
208                         /* skip subsystems without "dev" */
209                         if (subsystem_expect_no_dev(udev.subsystem)) {
210                                 dbg("don't care about '%s' devices", udev.subsystem);
211                                 goto hotplug;
212                         }
213
214                         /* get node from db, remove db-entry, delete created node */
215                         retval = udev_remove_device(&udev);
216                 }
217
218                 /* run dev.d/ scripts if we created/deleted a node or changed a netif name */
219                 if (udev.devname[0] != '\0') {
220                         setenv("DEVNAME", udev.devname, 1);
221                         if (udev_dev_d)
222                                 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
223                 }
224         } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
225                 if (strcmp(action, "add") == 0) {
226                         /* wait for sysfs */
227                         dbg("devices add");
228
229                         snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
230                         devices_dev = wait_devices_device_open(path);
231                         if (!devices_dev) {
232                                 dbg("devices device unavailable (probably remove has beaten us)");
233                                 goto hotplug;
234                         }
235                         dbg("devices device opened '%s'", path);
236
237                         wait_for_devices_device(devices_dev, &error);
238
239                         sysfs_close_device(devices_dev);
240                 } else if (strcmp(action, "remove") == 0) {
241                         dbg("devices remove");
242                 }
243         } else {
244                 dbg("unhandled");
245         }
246
247 hotplug:
248         if (udev_hotplug_d && managed_event)
249                 udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);
250
251 exit:
252         logging_close();
253         return retval;
254 }