chiark / gitweb /
[PATCH] crap, I messed up the 'sed' instances pretty badly, this fixes the config...
[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
32 #include "libsysfs/sysfs/libsysfs.h"
33 #include "udev.h"
34 #include "udev_lib.h"
35 #include "udev_version.h"
36 #include "logging.h"
37 #include "namedev.h"
38 #include "udevdb.h"
39
40 /* timeout flag for udevdb */
41 extern sig_atomic_t gotalarm;
42
43 /* global variables */
44 char **main_argv;
45 char **main_envp;
46
47 #ifdef LOG
48 unsigned char logname[LOGNAME_SIZE];
49 void log_message(int level, const char *format, ...)
50 {
51         va_list args;
52
53         if (!udev_log)
54                 return;
55
56         va_start(args, format);
57         vsyslog(level, format, args);
58         va_end(args);
59 }
60 #endif
61
62 static void asmlinkage sig_handler(int signum)
63 {
64         switch (signum) {
65                 case SIGALRM:
66                         gotalarm = 1;
67                         info("error: timeout reached, event probably not handled correctly");
68                         break;
69                 case SIGINT:
70                 case SIGTERM:
71                         udevdb_exit();
72                         exit(20 + signum);
73                 default:
74                         dbg("unhandled signal %d", signum);
75         }
76 }
77
78 /* list of subsystems we don't care about. not listing such systems here
79  * is not critical, but it makes it faster as we don't look for the "dev" file
80  */
81 static int subsystem_without_dev(const char *subsystem)
82 {
83         char *subsystem_blacklist[] = {
84                 "scsi_host",
85                 "scsi_device",
86                 "usb_host",
87                 "pci_bus",
88                 "pcmcia_socket",
89                 "bluetooth",
90                 "i2c-adapter",
91                 "pci_bus",
92                 "ieee1394",
93                 "ieee1394_host",
94                 "ieee1394_node",
95                 NULL
96         };
97         char **subsys;
98
99         for (subsys = subsystem_blacklist; *subsys != NULL; subsys++) {
100                 if (strcmp(subsystem, *subsys) == 0)
101                         return 1;
102         }
103
104         return 0;
105 }
106
107 int main(int argc, char *argv[], char *envp[])
108 {
109         main_argv = argv;
110         main_envp = envp;
111         struct sigaction act;
112         char *action;
113         char *devpath = "";
114         char *subsystem = "";
115         int retval = -EINVAL;
116         enum {
117                 ADD,
118                 REMOVE,
119                 UDEVSTART,
120         } act_type;
121
122         dbg("version %s", UDEV_VERSION);
123
124         init_logging("udev");
125
126         udev_init_config();
127
128         if (strstr(argv[0], "udevstart")) {
129                 act_type = UDEVSTART;
130         } else {
131                 action = get_action();
132                 if (!action) {
133                         dbg("no action?");
134                         goto exit;
135                 }
136                 if (strcmp(action, "add") == 0) {
137                         act_type = ADD;
138                 } else if (strcmp(action, "remove") == 0) {
139                         act_type = REMOVE;
140                 } else {
141                         dbg("unknown action '%s'", action);
142                         goto exit;
143                 }
144
145                 devpath = get_devpath();
146                 if (!devpath) {
147                         dbg("no devpath?");
148                         goto exit;
149                 }
150                 dbg("looking at '%s'", devpath);
151
152                 /* we only care about class devices and block stuff */
153                 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
154                         dbg("not a block or class device");
155                         goto exit;
156                 }
157
158                 subsystem = get_subsystem(main_argv[1]);
159                 if (!subsystem) {
160                         dbg("no subsystem?");
161                         goto exit;
162                 }
163
164                 /* skip blacklisted subsystems */
165                 if (subsystem_without_dev(subsystem)) {
166                         dbg("don't care about '%s' devices", subsystem);
167                         exit(0);
168                 };
169         }
170
171         /* set signal handlers */
172         act.sa_handler = (void (*) (int))sig_handler;
173         sigemptyset (&act.sa_mask);
174         /* alarm must not restart syscalls*/
175         sigaction(SIGALRM, &act, NULL);
176         sigaction(SIGINT, &act, NULL);
177         sigaction(SIGTERM, &act, NULL);
178
179         /* trigger timout to interrupt blocking syscalls */
180         alarm(ALARM_TIMEOUT);
181
182         /* initialize udev database */
183         if (udevdb_init(UDEVDB_DEFAULT) != 0)
184                 info("error: unable to initialize database, continuing without database");
185
186         switch(act_type) {
187         case UDEVSTART:
188                 dbg("udevstart");
189                 namedev_init();
190                 retval = udev_start();
191                 break;
192         case ADD:
193                 dbg("udev add");
194                 namedev_init();
195                 retval = udev_add_device(devpath, subsystem, NOFAKE);
196                 break;
197         case REMOVE:
198                 dbg("udev remove");
199                 retval = udev_remove_device(devpath, subsystem);
200         }
201
202         udevdb_exit();
203
204 exit:
205         return retval;
206 }