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