]>
Commit | Line | Data |
---|---|---|
f0083e3d GKH |
1 | /* |
2 | * udev.c | |
3 | * | |
4 | * Userspace devfs | |
5 | * | |
8202eb32 | 6 | * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com> |
f0083e3d GKH |
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 | ||
8dc0e138 | 23 | #define _KLIBC_HAS_ARCH_SIG_ATOMIC_T |
c81b35c0 KS |
24 | #include <stdio.h> |
25 | #include <stddef.h> | |
f0083e3d GKH |
26 | #include <stdlib.h> |
27 | #include <string.h> | |
e8baccca | 28 | #include <ctype.h> |
c81b35c0 | 29 | #include <errno.h> |
d12ecb53 | 30 | #include <signal.h> |
85511f02 | 31 | |
c80da508 | 32 | #include "libsysfs/sysfs/libsysfs.h" |
f0083e3d | 33 | #include "udev.h" |
c81b35c0 | 34 | #include "udev_lib.h" |
f0083e3d | 35 | #include "udev_version.h" |
54988802 | 36 | #include "logging.h" |
2232cac8 | 37 | #include "namedev.h" |
dbfc520c | 38 | #include "udevdb.h" |
f0083e3d | 39 | |
7e89a569 KS |
40 | /* timeout flag for udevdb */ |
41 | extern sig_atomic_t gotalarm; | |
42 | ||
c056c514 GKH |
43 | /* global variables */ |
44 | char **main_argv; | |
45 | char **main_envp; | |
46 | ||
d026a35d | 47 | #ifdef LOG |
d00bd172 | 48 | unsigned char logname[LOGNAME_SIZE]; |
3fe07342 | 49 | void log_message(int level, const char *format, ...) |
51a8bb2f | 50 | { |
e964c2c0 | 51 | va_list args; |
d026a35d GKH |
52 | |
53 | if (!udev_log) | |
54 | return; | |
55 | ||
56 | va_start(args, format); | |
57 | vsyslog(level, format, args); | |
58 | va_end(args); | |
51a8bb2f | 59 | } |
d026a35d | 60 | #endif |
51a8bb2f | 61 | |
e5a5b54a | 62 | static void asmlinkage sig_handler(int signum) |
d12ecb53 | 63 | { |
d12ecb53 | 64 | switch (signum) { |
7e89a569 KS |
65 | case SIGALRM: |
66 | gotalarm = 1; | |
67 | info("error: timeout reached, event probably not handled correctly"); | |
68 | break; | |
d12ecb53 MB |
69 | case SIGINT: |
70 | case SIGTERM: | |
d12ecb53 MB |
71 | udevdb_exit(); |
72 | exit(20 + signum); | |
d12ecb53 | 73 | default: |
47bf9196 | 74 | dbg("unhandled signal %d", signum); |
d12ecb53 MB |
75 | } |
76 | } | |
77 | ||
8eb38ef8 | 78 | static char *subsystem_blacklist[] = { |
8eb38ef8 GKH |
79 | "scsi_host", |
80 | "scsi_device", | |
f130b156 GKH |
81 | "usb_host", |
82 | "pci_bus", | |
e9f504e8 | 83 | "pcmcia_socket", |
d00bd172 | 84 | "" |
8eb38ef8 GKH |
85 | }; |
86 | ||
aee380b6 | 87 | int main(int argc, char *argv[], char *envp[]) |
f4dc8d11 | 88 | { |
aee380b6 KS |
89 | main_argv = argv; |
90 | main_envp = envp; | |
f8911dbb | 91 | struct sigaction act; |
aee380b6 KS |
92 | char *action; |
93 | char *devpath = ""; | |
94 | char *subsystem = ""; | |
95 | int i; | |
96 | int retval = -EINVAL; | |
97 | enum { | |
98 | ADD, | |
99 | REMOVE, | |
100 | UDEVSTART, | |
101 | } act_type; | |
f4dc8d11 | 102 | |
aee380b6 | 103 | dbg("version %s", UDEV_VERSION); |
2232cac8 | 104 | |
7e89a569 KS |
105 | init_logging("udev"); |
106 | ||
aee380b6 | 107 | udev_init_config(); |
af815f88 | 108 | |
aee380b6 KS |
109 | if (strstr(argv[0], "udevstart")) { |
110 | act_type = UDEVSTART; | |
111 | } else { | |
112 | action = get_action(); | |
113 | if (!action) { | |
114 | dbg("no action?"); | |
115 | goto exit; | |
116 | } | |
117 | if (strcmp(action, "add") == 0) { | |
118 | act_type = ADD; | |
119 | } else if (strcmp(action, "remove") == 0) { | |
120 | act_type = REMOVE; | |
121 | } else { | |
122 | dbg("unknown action '%s'", action); | |
8eb38ef8 GKH |
123 | goto exit; |
124 | } | |
f0083e3d | 125 | |
aee380b6 KS |
126 | devpath = get_devpath(); |
127 | if (!devpath) { | |
128 | dbg("no devpath?"); | |
129 | goto exit; | |
130 | } | |
131 | dbg("looking at '%s'", devpath); | |
132 | ||
133 | /* we only care about class devices and block stuff */ | |
6d74f996 | 134 | if (!strstr(devpath, "class") && !strstr(devpath, "block")) { |
aee380b6 KS |
135 | dbg("not a block or class device"); |
136 | goto exit; | |
137 | } | |
138 | ||
139 | subsystem = get_subsystem(main_argv[1]); | |
140 | if (!subsystem) { | |
141 | dbg("no subsystem?"); | |
142 | goto exit; | |
143 | } | |
144 | ||
145 | /* skip blacklisted subsystems */ | |
146 | i = 0; | |
147 | while (subsystem_blacklist[i][0] != '\0') { | |
148 | if (strcmp(subsystem, subsystem_blacklist[i]) == 0) { | |
149 | dbg("don't care about '%s' devices", subsystem); | |
150 | goto exit; | |
151 | } | |
152 | i++; | |
153 | } | |
dbfc520c DS |
154 | } |
155 | ||
47bf9196 | 156 | /* set signal handlers */ |
dc117daa | 157 | act.sa_handler = (void (*) (int))sig_handler; |
f8911dbb | 158 | sigemptyset (&act.sa_mask); |
7e89a569 KS |
159 | /* alarm must interrupt syscalls*/ |
160 | sigaction(SIGALRM, &act, NULL); | |
f8911dbb KS |
161 | sigaction(SIGINT, &act, NULL); |
162 | sigaction(SIGTERM, &act, NULL); | |
3fd52a76 | 163 | |
7e89a569 KS |
164 | /* trigger timout to interrupt blocking syscalls */ |
165 | alarm(ALARM_TIMEOUT); | |
166 | ||
aee380b6 | 167 | /* initialize udev database */ |
7e89a569 KS |
168 | if (udevdb_init(UDEVDB_DEFAULT) != 0) |
169 | info("error: unable to initialize database, continuing without database"); | |
f61d732a | 170 | |
aee380b6 KS |
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"); | |
f61d732a | 185 | retval = udev_remove_device(devpath, subsystem); |
dbfc520c | 186 | } |
2a94c877 | 187 | |
dbfc520c | 188 | udevdb_exit(); |
85511f02 | 189 | |
7ac0feeb | 190 | exit: |
f61d732a | 191 | return retval; |
f0083e3d | 192 | } |