]>
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 | ||
c81b35c0 KS |
23 | #include <stdio.h> |
24 | #include <stddef.h> | |
f0083e3d GKH |
25 | #include <stdlib.h> |
26 | #include <string.h> | |
e8baccca | 27 | #include <ctype.h> |
c81b35c0 | 28 | #include <errno.h> |
d12ecb53 | 29 | #include <signal.h> |
1ceba936 | 30 | #include <unistd.h> |
85511f02 | 31 | |
c80da508 | 32 | #include "libsysfs/sysfs/libsysfs.h" |
f0083e3d | 33 | #include "udev.h" |
c81b35c0 | 34 | #include "udev_lib.h" |
f0713480 | 35 | #include "udev_sysfs.h" |
f0083e3d | 36 | #include "udev_version.h" |
54988802 | 37 | #include "logging.h" |
2232cac8 | 38 | #include "namedev.h" |
dbfc520c | 39 | #include "udevdb.h" |
f0083e3d | 40 | |
7e89a569 KS |
41 | /* timeout flag for udevdb */ |
42 | extern sig_atomic_t gotalarm; | |
43 | ||
c056c514 GKH |
44 | /* global variables */ |
45 | char **main_argv; | |
46 | char **main_envp; | |
47 | ||
d026a35d | 48 | #ifdef LOG |
d00bd172 | 49 | unsigned char logname[LOGNAME_SIZE]; |
3fe07342 | 50 | void log_message(int level, const char *format, ...) |
51a8bb2f | 51 | { |
e964c2c0 | 52 | va_list args; |
d026a35d GKH |
53 | |
54 | if (!udev_log) | |
55 | return; | |
56 | ||
57 | va_start(args, format); | |
58 | vsyslog(level, format, args); | |
59 | va_end(args); | |
51a8bb2f | 60 | } |
d026a35d | 61 | #endif |
51a8bb2f | 62 | |
e5a5b54a | 63 | static void asmlinkage sig_handler(int signum) |
d12ecb53 | 64 | { |
d12ecb53 | 65 | switch (signum) { |
7e89a569 KS |
66 | case SIGALRM: |
67 | gotalarm = 1; | |
7e89a569 | 68 | break; |
d12ecb53 MB |
69 | case SIGINT: |
70 | case SIGTERM: | |
d12ecb53 | 71 | exit(20 + signum); |
d12ecb53 MB |
72 | } |
73 | } | |
74 | ||
aee380b6 | 75 | int main(int argc, char *argv[], char *envp[]) |
f4dc8d11 | 76 | { |
f8911dbb | 77 | struct sigaction act; |
7a947ce5 KS |
78 | struct sysfs_class_device *class_dev; |
79 | struct udevice udev; | |
80 | char path[SYSFS_PATH_MAX]; | |
aee380b6 KS |
81 | int retval = -EINVAL; |
82 | enum { | |
83 | ADD, | |
84 | REMOVE, | |
85 | UDEVSTART, | |
86 | } act_type; | |
f4dc8d11 | 87 | |
aee380b6 | 88 | dbg("version %s", UDEV_VERSION); |
2232cac8 | 89 | |
92307b9e PM |
90 | main_argv = argv; |
91 | main_envp = envp; | |
92 | ||
7257cb18 | 93 | logging_init("udev"); |
7e89a569 | 94 | |
aee380b6 | 95 | udev_init_config(); |
af815f88 | 96 | |
aee380b6 KS |
97 | if (strstr(argv[0], "udevstart")) { |
98 | act_type = UDEVSTART; | |
99 | } else { | |
7a947ce5 KS |
100 | const char *action = get_action(); |
101 | const char *devpath = get_devpath(); | |
102 | const char *subsystem = get_subsystem(main_argv[1]); | |
103 | ||
aee380b6 KS |
104 | if (!action) { |
105 | dbg("no action?"); | |
106 | goto exit; | |
107 | } | |
108 | if (strcmp(action, "add") == 0) { | |
109 | act_type = ADD; | |
110 | } else if (strcmp(action, "remove") == 0) { | |
111 | act_type = REMOVE; | |
112 | } else { | |
7a947ce5 | 113 | dbg("no action '%s' for us", action); |
8eb38ef8 GKH |
114 | goto exit; |
115 | } | |
f0083e3d | 116 | |
aee380b6 KS |
117 | if (!devpath) { |
118 | dbg("no devpath?"); | |
119 | goto exit; | |
120 | } | |
f0713480 | 121 | dbg("looking at '%s'", devpath); |
aee380b6 KS |
122 | |
123 | /* we only care about class devices and block stuff */ | |
6d74f996 | 124 | if (!strstr(devpath, "class") && !strstr(devpath, "block")) { |
aee380b6 KS |
125 | dbg("not a block or class device"); |
126 | goto exit; | |
127 | } | |
128 | ||
aee380b6 | 129 | if (!subsystem) { |
7a947ce5 | 130 | dbg("no subsystem"); |
aee380b6 KS |
131 | goto exit; |
132 | } | |
133 | ||
f0713480 KS |
134 | udev_set_values(&udev, devpath, subsystem); |
135 | ||
aee380b6 | 136 | /* skip blacklisted subsystems */ |
f0713480 | 137 | if (udev.type != 'n' && subsystem_expect_no_dev(subsystem)) { |
707680b1 | 138 | dbg("don't care about '%s' devices", subsystem); |
7a947ce5 | 139 | goto exit; |
707680b1 | 140 | }; |
7a947ce5 | 141 | |
dbfc520c DS |
142 | } |
143 | ||
47bf9196 | 144 | /* set signal handlers */ |
dc117daa | 145 | act.sa_handler = (void (*) (int))sig_handler; |
f8911dbb | 146 | sigemptyset (&act.sa_mask); |
5a73b25f | 147 | act.sa_flags = 0; |
707680b1 | 148 | /* alarm must not restart syscalls*/ |
7e89a569 | 149 | sigaction(SIGALRM, &act, NULL); |
f8911dbb KS |
150 | sigaction(SIGINT, &act, NULL); |
151 | sigaction(SIGTERM, &act, NULL); | |
3fd52a76 | 152 | |
7e89a569 KS |
153 | /* trigger timout to interrupt blocking syscalls */ |
154 | alarm(ALARM_TIMEOUT); | |
155 | ||
aee380b6 | 156 | /* initialize udev database */ |
7e89a569 KS |
157 | if (udevdb_init(UDEVDB_DEFAULT) != 0) |
158 | info("error: unable to initialize database, continuing without database"); | |
f61d732a | 159 | |
aee380b6 KS |
160 | switch(act_type) { |
161 | case UDEVSTART: | |
162 | dbg("udevstart"); | |
163 | namedev_init(); | |
aee380b6 KS |
164 | retval = udev_start(); |
165 | break; | |
166 | case ADD: | |
167 | dbg("udev add"); | |
7a947ce5 | 168 | |
7a947ce5 KS |
169 | /* open the device */ |
170 | snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath); | |
171 | class_dev = sysfs_open_class_device_path(path); | |
172 | if (class_dev == NULL) { | |
173 | dbg ("sysfs_open_class_device_path failed"); | |
174 | break; | |
175 | } | |
176 | dbg("opened class_dev->name='%s'", class_dev->name); | |
177 | ||
f0713480 KS |
178 | /* init rules */ |
179 | namedev_init(); | |
180 | ||
7a947ce5 KS |
181 | /* name, create node, store in db */ |
182 | retval = udev_add_device(&udev, class_dev); | |
5d24c6ca KS |
183 | |
184 | /* run scripts */ | |
185 | dev_d_execute(&udev); | |
186 | ||
187 | sysfs_close_class_device(class_dev); | |
aee380b6 KS |
188 | break; |
189 | case REMOVE: | |
190 | dbg("udev remove"); | |
5d24c6ca KS |
191 | |
192 | /* get node from db, delete it*/ | |
7a947ce5 | 193 | retval = udev_remove_device(&udev); |
5d24c6ca KS |
194 | |
195 | /* run scripts */ | |
196 | dev_d_execute(&udev); | |
dbfc520c | 197 | } |
2a94c877 | 198 | |
dbfc520c | 199 | udevdb_exit(); |
85511f02 | 200 | |
7ac0feeb | 201 | exit: |
7257cb18 | 202 | logging_close(); |
f61d732a | 203 | return retval; |
f0083e3d | 204 | } |