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