]>
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 | ||
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
85511f02 | 25 | #include <stdio.h> |
85511f02 | 26 | #include <errno.h> |
e8baccca | 27 | #include <ctype.h> |
d12ecb53 | 28 | #include <signal.h> |
85511f02 | 29 | |
f0083e3d GKH |
30 | #include "udev.h" |
31 | #include "udev_version.h" | |
7ac0feeb | 32 | #include "udev_dbus.h" |
54988802 | 33 | #include "logging.h" |
2232cac8 | 34 | #include "namedev.h" |
dbfc520c | 35 | #include "udevdb.h" |
19dc5d4c | 36 | #include "libsysfs/libsysfs.h" |
f0083e3d | 37 | |
c056c514 GKH |
38 | /* global variables */ |
39 | char **main_argv; | |
40 | char **main_envp; | |
41 | ||
d026a35d GKH |
42 | #ifdef LOG |
43 | unsigned char logname[42]; | |
44 | void log_message (int level, const char *format, ...) | |
51a8bb2f | 45 | { |
d026a35d GKH |
46 | va_list args; |
47 | ||
48 | if (!udev_log) | |
49 | return; | |
50 | ||
51 | va_start(args, format); | |
52 | vsyslog(level, format, args); | |
53 | va_end(args); | |
51a8bb2f | 54 | } |
d026a35d | 55 | #endif |
51a8bb2f | 56 | |
d12ecb53 MB |
57 | static void sig_handler(int signum) |
58 | { | |
d12ecb53 MB |
59 | switch (signum) { |
60 | case SIGINT: | |
61 | case SIGTERM: | |
d12ecb53 MB |
62 | sysbus_disconnect(); |
63 | udevdb_exit(); | |
64 | exit(20 + signum); | |
d12ecb53 MB |
65 | default: |
66 | dbg("unhandled signal"); | |
67 | } | |
68 | } | |
69 | ||
c056c514 | 70 | static inline char *get_action(void) |
f0083e3d GKH |
71 | { |
72 | char *action; | |
73 | ||
74 | action = getenv("ACTION"); | |
75 | return action; | |
76 | } | |
77 | ||
c056c514 GKH |
78 | static inline char *get_devpath(void) |
79 | { | |
80 | char *devpath; | |
81 | ||
82 | devpath = getenv("DEVPATH"); | |
83 | return devpath; | |
84 | } | |
f0083e3d | 85 | |
c056c514 | 86 | static inline char *get_seqnum(void) |
f0083e3d | 87 | { |
c056c514 | 88 | char *seqnum; |
f0083e3d | 89 | |
c056c514 GKH |
90 | seqnum = getenv("SEQNUM"); |
91 | return seqnum; | |
f0083e3d GKH |
92 | } |
93 | ||
8eb38ef8 GKH |
94 | static char *subsystem_blacklist[] = { |
95 | "net", | |
96 | "scsi_host", | |
97 | "scsi_device", | |
f130b156 GKH |
98 | "usb_host", |
99 | "pci_bus", | |
8eb38ef8 GKH |
100 | "", |
101 | }; | |
102 | ||
8202eb32 | 103 | static int udev_hotplug(int argc, char **argv) |
f4dc8d11 KS |
104 | { |
105 | char *action; | |
106 | char *devpath; | |
107 | char *subsystem; | |
108 | int retval = -EINVAL; | |
8eb38ef8 | 109 | int i; |
f8911dbb | 110 | struct sigaction act; |
f4dc8d11 | 111 | |
8202eb32 GKH |
112 | action = get_action(); |
113 | if (!action) { | |
114 | dbg ("no action?"); | |
115 | goto exit; | |
116 | } | |
ea733a2f | 117 | |
c056c514 GKH |
118 | devpath = get_devpath(); |
119 | if (!devpath) { | |
120 | dbg ("no devpath?"); | |
19dc5d4c GKH |
121 | goto exit; |
122 | } | |
f7b4eca4 | 123 | dbg("looking at '%s'", devpath); |
2232cac8 | 124 | |
19dc5d4c | 125 | /* we only care about class devices and block stuff */ |
c056c514 GKH |
126 | if (!strstr(devpath, "class") && |
127 | !strstr(devpath, "block")) { | |
f7b4eca4 | 128 | dbg("not a block or class device"); |
19dc5d4c GKH |
129 | goto exit; |
130 | } | |
af815f88 | 131 | |
8eb38ef8 | 132 | /* skip blacklisted subsystems */ |
8202eb32 | 133 | subsystem = argv[1]; |
8eb38ef8 GKH |
134 | i = 0; |
135 | while (subsystem_blacklist[i][0] != '\0') { | |
136 | if (strcmp(subsystem, subsystem_blacklist[i]) == 0) { | |
137 | dbg("don't care about '%s' devices", subsystem); | |
138 | goto exit; | |
139 | } | |
140 | i++; | |
ea733a2f | 141 | } |
f0083e3d | 142 | |
7ac0feeb GKH |
143 | /* connect to the system message bus */ |
144 | sysbus_connect(); | |
5aebfbcb | 145 | |
8202eb32 GKH |
146 | /* initialize our configuration */ |
147 | udev_init_config(); | |
148 | ||
dbfc520c DS |
149 | /* initialize udev database */ |
150 | retval = udevdb_init(UDEVDB_DEFAULT); | |
151 | if (retval != 0) { | |
f7b4eca4 | 152 | dbg("unable to initialize database"); |
df496acb | 153 | goto exit_sysbus; |
dbfc520c DS |
154 | } |
155 | ||
3fd52a76 | 156 | /* set up a default signal handler for now */ |
f8911dbb KS |
157 | act.sa_handler = sig_handler; |
158 | sigemptyset (&act.sa_mask); | |
159 | act.sa_flags = SA_RESTART; | |
160 | sigaction(SIGINT, &act, NULL); | |
161 | sigaction(SIGTERM, &act, NULL); | |
3fd52a76 | 162 | |
ea733a2f GKH |
163 | /* initialize the naming deamon */ |
164 | namedev_init(); | |
165 | ||
3f4967a1 | 166 | if (strcmp(action, "add") == 0) |
c056c514 | 167 | retval = udev_add_device(devpath, subsystem); |
85511f02 | 168 | |
dbfc520c | 169 | else if (strcmp(action, "remove") == 0) |
c056c514 | 170 | retval = udev_remove_device(devpath, subsystem); |
85511f02 | 171 | |
dbfc520c | 172 | else { |
f7b4eca4 | 173 | dbg("unknown action '%s'", action); |
dbfc520c DS |
174 | retval = -EINVAL; |
175 | } | |
176 | udevdb_exit(); | |
85511f02 | 177 | |
df496acb | 178 | exit_sysbus: |
7ac0feeb GKH |
179 | /* disconnect from the system message bus */ |
180 | sysbus_disconnect(); | |
5aebfbcb | 181 | |
7ac0feeb | 182 | exit: |
f1db055a | 183 | if (retval > 0) |
184 | retval = 0; | |
185 | ||
c53735ef | 186 | return -retval; |
f0083e3d | 187 | } |
f4dc8d11 KS |
188 | |
189 | int main(int argc, char **argv, char **envp) | |
190 | { | |
191 | main_argv = argv; | |
192 | main_envp = envp; | |
f4dc8d11 | 193 | |
95a6f4c8 | 194 | init_logging("udev"); |
f4dc8d11 KS |
195 | dbg("version %s", UDEV_VERSION); |
196 | ||
8202eb32 | 197 | return udev_hotplug(argc, argv); |
f4dc8d11 KS |
198 | } |
199 | ||
200 |