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 static void print_record(char *path, struct udevice *dev)
88 printf("P: %s\n", path);
89 printf("N: %s\n", dev->name);
90 printf("S: %s\n", dev->symlink);
91 printf("O: %s\n", dev->owner);
92 printf("G: %s\n", dev->group);
104 static inline int udev_user(int argc, char **argv)
106 static const char short_options[] = "dp:q:rVh";
108 int retval = -EINVAL;
111 enum query_type query = NONE;
112 char result[NAME_SIZE] = "";
113 char path[NAME_SIZE] = "";
115 /* get command line options */
117 option = getopt(argc, argv, short_options);
121 dbg("option '%c'", option);
124 dbg("udev path: %s\n", optarg);
125 strfieldcpy(path, optarg);
129 dbg("udev query: %s\n", optarg);
131 if (strcmp(optarg, "name") == 0) {
136 if (strcmp(optarg, "symlink") == 0) {
141 if (strcmp(optarg, "owner") == 0) {
146 if (strcmp(optarg, "group") == 0) {
151 printf("unknown query type\n");
159 retval = udevdb_open_ro();
161 printf("unable to open udev database\n");
164 retval = udevdb_dump(print_record);
169 printf("udev, version %s\n", UDEV_VERSION);
180 /* process options */
182 if (path[0] == '\0') {
183 printf("query needs device path specified\n");
187 retval = udevdb_open_ro();
189 printf("unable to open udev database\n");
192 retval = udevdb_get_dev(path, &dev);
197 strfieldcpy(result, udev_root);
198 strncat(result, dev.name, sizeof(result));
202 strfieldcpy(result, dev.symlink);
206 strfieldcpy(result, dev.group);
210 strfieldcpy(result, dev.owner);
216 printf("%s\n", result);
218 printf("device not found in udev database\n");
225 printf("%s\n", udev_root);
230 printf("Usage: [-pqrdVh]\n"
231 " -q TYPE query database for the specified value:\n"
232 " 'name' name of device node\n"
233 " 'symlink' pointing to node\n"
236 " -p PATH sysfs device path used for query\n"
237 " -r print udev root\n"
238 " -d dump whole database\n"
239 " -V print udev version\n"
240 " -h print this help text\n"
245 static char *subsystem_blacklist[] = {
254 static inline int udev_hotplug(int argc, char **argv)
259 int retval = -EINVAL;
264 devpath = get_devpath();
269 dbg("looking at '%s'", devpath);
271 /* we only care about class devices and block stuff */
272 if (!strstr(devpath, "class") &&
273 !strstr(devpath, "block")) {
274 dbg("not a block or class device");
278 /* skip blacklisted subsystems */
280 while (subsystem_blacklist[i][0] != '\0') {
281 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
282 dbg("don't care about '%s' devices", subsystem);
288 action = get_action();
294 /* connect to the system message bus */
297 /* initialize udev database */
298 retval = udevdb_init(UDEVDB_DEFAULT);
300 dbg("unable to initialize database");
304 /* set up a default signal handler for now */
305 signal(SIGINT, sig_handler);
306 signal(SIGTERM, sig_handler);
307 signal(SIGKILL, sig_handler);
309 /* initialize the naming deamon */
312 if (strcmp(action, "add") == 0)
313 retval = udev_add_device(devpath, subsystem);
315 else if (strcmp(action, "remove") == 0)
316 retval = udev_remove_device(devpath, subsystem);
319 dbg("unknown action '%s'", action);
325 /* disconnect from the system message bus */
335 int main(int argc, char **argv, char **envp)
341 dbg("version %s", UDEV_VERSION);
343 /* initialize our configuration */
346 if (argc == 2 && argv[1][0] != '-') {
347 dbg("called by hotplug");
348 retval = udev_hotplug(argc, argv);
350 dbg("called by user");
351 retval = udev_user(argc, argv);