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