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"
40 #include "libsysfs/libsysfs.h"
42 /* global variables */
46 static void sig_handler(int signum)
48 dbg("caught signal %d", signum);
58 dbg("unhandled signal");
62 static inline char *get_action(void)
66 action = getenv("ACTION");
70 static inline char *get_devpath(void)
74 devpath = getenv("DEVPATH");
78 static inline char *get_seqnum(void)
82 seqnum = getenv("SEQNUM");
86 /* callback for database dump */
87 static int print_record(char *path, struct udevice *dev)
89 printf("P: %s\n", path);
90 printf("N: %s\n", dev->name);
91 printf("S: %s\n", dev->symlink);
92 printf("O: %s\n", dev->owner);
93 printf("G: %s\n", dev->group);
107 static inline int udev_user(int argc, char **argv)
109 static const char short_options[] = "dn:p:q:rVh";
111 int retval = -EINVAL;
114 enum query_type query = NONE;
115 char result[NAME_SIZE] = "";
116 char path[NAME_SIZE] = "";
117 char name[NAME_SIZE] = "";
119 /* get command line options */
121 option = getopt(argc, argv, short_options);
125 dbg("option '%c'", option);
128 dbg("udev name: %s\n", optarg);
129 strfieldcpy(name, optarg);
133 dbg("udev path: %s\n", optarg);
134 strfieldcpy(path, optarg);
138 dbg("udev query: %s\n", optarg);
140 if (strcmp(optarg, "name") == 0) {
145 if (strcmp(optarg, "symlink") == 0) {
150 if (strcmp(optarg, "owner") == 0) {
155 if (strcmp(optarg, "group") == 0) {
160 if (strcmp(optarg, "path") == 0) {
165 printf("unknown query type\n");
173 retval = udevdb_open_ro();
175 printf("unable to open udev database\n");
178 retval = udevdb_call_foreach(print_record);
183 printf("udev, version %s\n", UDEV_VERSION);
194 /* process options */
196 retval = udevdb_open_ro();
198 printf("unable to open udev database\n");
202 if (path[0] != '\0') {
203 retval = udevdb_get_dev(path, &dev);
205 printf("device not found in database\n");
211 if (name[0] != '\0') {
212 retval = udevdb_get_dev_byname(name, path, &dev);
214 printf("device not found in database\n");
220 printf("query needs device path(-p) or node name(-n) specified\n");
227 strfieldcpy(result, udev_root);
228 strncat(result, dev.name, sizeof(result));
232 strfieldcpy(result, dev.symlink);
236 strfieldcpy(result, dev.group);
240 strfieldcpy(result, dev.owner);
244 strfieldcpy(result, path);
250 printf("%s\n", result);
258 printf("%s\n", udev_root);
263 printf("Usage: [-npqrdVh]\n"
264 " -q TYPE query database for the specified value:\n"
265 " 'name' name of device node\n"
266 " 'symlink' pointing to node\n"
269 " 'path' sysfs device path\n"
270 " -p PATH sysfs device path used for query\n"
271 " -n NAME node name used for query\n"
273 " -r print udev root\n"
274 " -d dump whole database\n"
275 " -V print udev version\n"
276 " -h print this help text\n"
281 static char *subsystem_blacklist[] = {
290 static inline int udev_hotplug(int argc, char **argv)
295 int retval = -EINVAL;
300 devpath = get_devpath();
305 dbg("looking at '%s'", devpath);
307 /* we only care about class devices and block stuff */
308 if (!strstr(devpath, "class") &&
309 !strstr(devpath, "block")) {
310 dbg("not a block or class device");
314 /* skip blacklisted subsystems */
316 while (subsystem_blacklist[i][0] != '\0') {
317 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
318 dbg("don't care about '%s' devices", subsystem);
324 action = get_action();
330 /* connect to the system message bus */
333 /* initialize udev database */
334 retval = udevdb_init(UDEVDB_DEFAULT);
336 dbg("unable to initialize database");
340 /* set up a default signal handler for now */
341 signal(SIGINT, sig_handler);
342 signal(SIGTERM, sig_handler);
343 signal(SIGKILL, sig_handler);
345 /* initialize the naming deamon */
348 if (strcmp(action, "add") == 0)
349 retval = udev_add_device(devpath, subsystem);
351 else if (strcmp(action, "remove") == 0)
352 retval = udev_remove_device(devpath, subsystem);
355 dbg("unknown action '%s'", action);
361 /* disconnect from the system message bus */
371 int main(int argc, char **argv, char **envp)
377 dbg("version %s", UDEV_VERSION);
379 /* initialize our configuration */
382 if (argc == 2 && argv[1][0] != '-') {
383 dbg("called by hotplug");
384 retval = udev_hotplug(argc, argv);
386 dbg("called by user");
387 retval = udev_user(argc, argv);