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