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");
85 static void print_record(char *path, struct udevice *dev)
87 printf("P: %s\n", path);
88 printf("N: %s\n", dev->name);
89 printf("S: %s\n", dev->symlink);
90 printf("O: %s\n", dev->owner);
91 printf("G: %s\n\n", dev->group);
102 static inline int udev_user(int argc, char **argv)
104 static const char short_options[] = "dp:q:rVh";
106 int retval = -EINVAL;
109 enum query_type query = NONE;
110 char result[NAME_SIZE] = "";
111 char path[NAME_SIZE] = "";
113 /* get command line options */
115 option = getopt(argc, argv, short_options);
119 dbg("option '%c'", option);
122 dbg("udev path: %s\n", optarg);
123 strfieldcpy(path, optarg);
127 dbg("udev query: %s\n", optarg);
129 if (strcmp(optarg, "name") == 0) {
134 if (strcmp(optarg, "symlink") == 0) {
139 if (strcmp(optarg, "owner") == 0) {
144 if (strcmp(optarg, "group") == 0) {
149 printf("unknown query type\n");
157 retval = udevdb_open_ro();
159 printf("unable to open udev database\n");
162 retval = udevdb_dump(print_record);
167 printf("udev, version %s\n", UDEV_VERSION);
178 /* process options */
180 if (path[0] == '\0') {
181 printf("query needs device path specified\n");
185 retval = udevdb_open_ro();
187 printf("unable to open udev database\n");
190 retval = udevdb_get_dev(path, &dev);
195 strfieldcpy(result, udev_root);
196 strncat(result, dev.name, sizeof(result));
200 strfieldcpy(result, dev.symlink);
204 strfieldcpy(result, dev.group);
208 strfieldcpy(result, dev.owner);
214 printf("%s\n", result);
216 printf("device not found in udev database\n");
223 printf("%s\n", udev_root);
228 printf("Usage: [-pqrdVh]\n"
229 " -q TYPE query database for the specified value:\n"
230 " 'name' name of device node\n"
231 " 'symlink' pointing to node\n"
234 " -p PATH sysfs device path used for query\n"
235 " -r print udev root\n"
236 " -d dump whole database\n"
237 " -V print udev version\n"
238 " -h print this help text\n"
243 static char *subsystem_blacklist[] = {
250 static inline int udev_hotplug(int argc, char **argv)
255 int retval = -EINVAL;
260 devpath = get_devpath();
265 dbg("looking at '%s'", devpath);
267 /* we only care about class devices and block stuff */
268 if (!strstr(devpath, "class") &&
269 !strstr(devpath, "block")) {
270 dbg("not a block or class device");
274 /* skip blacklisted subsystems */
276 while (subsystem_blacklist[i][0] != '\0') {
277 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
278 dbg("don't care about '%s' devices", subsystem);
284 action = get_action();
290 /* connect to the system message bus */
293 /* initialize udev database */
294 retval = udevdb_init(UDEVDB_DEFAULT);
296 dbg("unable to initialize database");
300 /* set up a default signal handler for now */
301 signal(SIGINT, sig_handler);
302 signal(SIGTERM, sig_handler);
303 signal(SIGKILL, sig_handler);
305 /* initialize the naming deamon */
308 if (strcmp(action, "add") == 0)
309 retval = udev_add_device(devpath, subsystem);
311 else if (strcmp(action, "remove") == 0)
312 retval = udev_remove_device(devpath, subsystem);
315 dbg("unknown action '%s'", action);
321 /* disconnect from the system message bus */
331 int main(int argc, char **argv, char **envp)
337 dbg("version %s", UDEV_VERSION);
339 /* initialize our configuration */
342 if (argc == 2 && argv[1][0] != '-') {
343 dbg("called by hotplug");
344 retval = udev_hotplug(argc, argv);
346 dbg("called by user");
347 retval = udev_user(argc, argv);