6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include "udev_version.h"
36 #include "udev_dbus.h"
39 #include "libsysfs/libsysfs.h"
41 /* global variables */
45 static void sig_handler(int signum)
47 dbg("caught signal %d", signum);
57 dbg("unhandled signal");
61 static inline char *get_action(void)
65 action = getenv("ACTION");
69 static inline char *get_devpath(void)
73 devpath = getenv("DEVPATH");
77 static inline char *get_seqnum(void)
81 seqnum = getenv("SEQNUM");
93 static inline int udev_user(int argc, char **argv)
95 static const char short_options[] = "p:q:rVh";
100 enum query_type query = NONE;
101 char result[NAME_SIZE] = "";
102 char path[NAME_SIZE] = "";
104 /* get command line options */
106 option = getopt(argc, argv, short_options);
110 dbg("option '%c'", option);
113 dbg("udev path: %s\n", optarg);
114 strfieldcpy(path, optarg);
118 dbg("udev query: %s\n", optarg);
120 if (strcmp(optarg, "name") == 0) {
125 if (strcmp(optarg, "symlink") == 0) {
130 if (strcmp(optarg, "owner") == 0) {
135 if (strcmp(optarg, "group") == 0) {
140 printf("unknown query type\n");
148 printf("udev, version %s\n", UDEV_VERSION);
159 /* process options */
161 if (path[0] == '\0') {
162 printf("query needs device path specified\n");
166 retval = udevdb_open_ro();
168 printf("unable to open udev database\n");
171 retval = udevdb_get_dev(path, &dev);
176 strfieldcpy(result, udev_root);
177 strncat(result, dev.name, sizeof(result));
181 strfieldcpy(result, dev.symlink);
185 strfieldcpy(result, dev.group);
189 strfieldcpy(result, dev.owner);
195 printf("%s\n", result);
197 printf("device not found in udev database\n");
204 printf("%s\n", udev_root);
209 printf("Usage: [-qrVh]\n"
210 " -q <name> query database for the specified value\n"
211 " -p <path> device path used for query\n"
212 " -r print udev root\n"
213 " -V print udev version\n"
214 " -h print this help text\n"
219 static char *subsystem_blacklist[] = {
226 static inline int udev_hotplug(int argc, char **argv)
231 int retval = -EINVAL;
236 devpath = get_devpath();
241 dbg("looking at '%s'", devpath);
243 /* we only care about class devices and block stuff */
244 if (!strstr(devpath, "class") &&
245 !strstr(devpath, "block")) {
246 dbg("not a block or class device");
250 /* skip blacklisted subsystems */
252 while (subsystem_blacklist[i][0] != '\0') {
253 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
254 dbg("don't care about '%s' devices", subsystem);
260 action = get_action();
266 /* connect to the system message bus */
269 /* initialize udev database */
270 retval = udevdb_init(UDEVDB_DEFAULT);
272 dbg("unable to initialize database");
276 /* set up a default signal handler for now */
277 signal(SIGINT, sig_handler);
278 signal(SIGTERM, sig_handler);
279 signal(SIGKILL, sig_handler);
281 /* initialize the naming deamon */
284 if (strcmp(action, "add") == 0)
285 retval = udev_add_device(devpath, subsystem);
287 else if (strcmp(action, "remove") == 0)
288 retval = udev_remove_device(devpath, subsystem);
291 dbg("unknown action '%s'", action);
297 /* disconnect from the system message bus */
307 int main(int argc, char **argv, char **envp)
313 dbg("version %s", UDEV_VERSION);
315 /* initialize our configuration */
318 if (argc == 2 && argv[1][0] != '-') {
319 dbg("called by hotplug");
320 retval = udev_hotplug(argc, argv);
322 dbg("called by user");
323 retval = udev_user(argc, argv);