chiark / gitweb /
[PATCH] skip waiting for device if we get a bad event for class creation
[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  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the
10  *      Free Software Foundation version 2 of the License.
11  * 
12  *      This program is distributed in the hope that it will be useful, but
13  *      WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *      General Public License for more details.
16  * 
17  *      You should have received a copy of the GNU General Public License along
18  *      with this program; if not, write to the Free Software Foundation, Inc.,
19  *      675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define _KLIBC_HAS_ARCH_SIG_ATOMIC_T
24 #include <stdio.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <unistd.h>
32
33 #include "libsysfs/sysfs/libsysfs.h"
34 #include "udev.h"
35 #include "udev_lib.h"
36 #include "udev_sysfs.h"
37 #include "udev_version.h"
38 #include "logging.h"
39 #include "namedev.h"
40 #include "udevdb.h"
41
42 /* timeout flag for udevdb */
43 extern sig_atomic_t gotalarm;
44
45 /* global variables */
46 char **main_argv;
47 char **main_envp;
48
49 #ifdef LOG
50 unsigned char logname[LOGNAME_SIZE];
51 void log_message(int level, const char *format, ...)
52 {
53         va_list args;
54
55         if (!udev_log)
56                 return;
57
58         va_start(args, format);
59         vsyslog(level, format, args);
60         va_end(args);
61 }
62 #endif
63
64 static void asmlinkage sig_handler(int signum)
65 {
66         switch (signum) {
67                 case SIGALRM:
68                         gotalarm = 1;
69                         info("error: timeout reached, event probably not handled correctly");
70                         break;
71                 case SIGINT:
72                 case SIGTERM:
73                         udevdb_exit();
74                         exit(20 + signum);
75                 default:
76                         dbg("unhandled signal %d", signum);
77         }
78 }
79
80 int main(int argc, char *argv[], char *envp[])
81 {
82         struct sigaction act;
83         struct sysfs_class_device *class_dev;
84         struct udevice udev;
85         char path[SYSFS_PATH_MAX];
86         int retval = -EINVAL;
87         enum {
88                 ADD,
89                 REMOVE,
90                 UDEVSTART,
91         } act_type;
92
93         dbg("version %s", UDEV_VERSION);
94
95         main_argv = argv;
96         main_envp = envp;
97
98         logging_init("udev");
99
100         udev_init_config();
101
102         if (strstr(argv[0], "udevstart")) {
103                 act_type = UDEVSTART;
104         } else {
105                 const char *action = get_action();
106                 const char *devpath = get_devpath();
107                 const char *subsystem = get_subsystem(main_argv[1]);
108
109                 if (!action) {
110                         dbg("no action?");
111                         goto exit;
112                 }
113                 if (strcmp(action, "add") == 0) {
114                         act_type = ADD;
115                 } else if (strcmp(action, "remove") == 0) {
116                         act_type = REMOVE;
117                 } else {
118                         dbg("no action '%s' for us", action);
119                         goto exit;
120                 }
121
122                 if (!devpath) {
123                         dbg("no devpath?");
124                         goto exit;
125                 }
126                 dbg("looking at '%s'", devpath);
127
128                 /* we only care about class devices and block stuff */
129                 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
130                         dbg("not a block or class device");
131                         goto exit;
132                 }
133
134                 if (!subsystem) {
135                         dbg("no subsystem");
136                         goto exit;
137                 }
138
139                 udev_set_values(&udev, devpath, subsystem);
140
141                 /* skip blacklisted subsystems */
142                 if (udev.type != 'n' && subsystem_expect_no_dev(subsystem)) {
143                         dbg("don't care about '%s' devices", subsystem);
144                         goto exit;
145                 };
146
147         }
148
149         /* set signal handlers */
150         act.sa_handler = (void (*) (int))sig_handler;
151         sigemptyset (&act.sa_mask);
152         /* alarm must not restart syscalls*/
153         sigaction(SIGALRM, &act, NULL);
154         sigaction(SIGINT, &act, NULL);
155         sigaction(SIGTERM, &act, NULL);
156
157         /* trigger timout to interrupt blocking syscalls */
158         alarm(ALARM_TIMEOUT);
159
160         /* initialize udev database */
161         if (udevdb_init(UDEVDB_DEFAULT) != 0)
162                 info("error: unable to initialize database, continuing without database");
163
164         switch(act_type) {
165         case UDEVSTART:
166                 dbg("udevstart");
167                 namedev_init();
168                 retval = udev_start();
169                 break;
170         case ADD:
171                 dbg("udev add");
172
173                 /* open the device */
174                 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
175                 class_dev = sysfs_open_class_device_path(path);
176                 if (class_dev == NULL) {
177                         dbg ("sysfs_open_class_device_path failed");
178                         break;
179                 }
180                 dbg("opened class_dev->name='%s'", class_dev->name);
181
182                 /* init rules */
183                 namedev_init();
184
185                 /* name, create node, store in db */
186                 retval = udev_add_device(&udev, class_dev);
187
188                 /* run scripts */
189                 dev_d_execute(&udev);
190
191                 sysfs_close_class_device(class_dev);
192                 break;
193         case REMOVE:
194                 dbg("udev remove");
195
196                 /* get node from db, delete it*/
197                 retval = udev_remove_device(&udev);
198
199                 /* run scripts */
200                 dev_d_execute(&udev);
201         }
202
203         udevdb_exit();
204
205 exit:
206         logging_close();
207         return retval;
208 }