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