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", dev->group);
103 static inline int udev_user(int argc, char **argv)
105 static const char short_options[] = "dp:q:rVh";
107 int retval = -EINVAL;
110 enum query_type query = NONE;
111 char result[NAME_SIZE] = "";
112 char path[NAME_SIZE] = "";
114 /* get command line options */
116 option = getopt(argc, argv, short_options);
120 dbg("option '%c'", option);
123 dbg("udev path: %s\n", optarg);
124 strfieldcpy(path, optarg);
128 dbg("udev query: %s\n", optarg);
130 if (strcmp(optarg, "name") == 0) {
135 if (strcmp(optarg, "symlink") == 0) {
140 if (strcmp(optarg, "owner") == 0) {
145 if (strcmp(optarg, "group") == 0) {
150 printf("unknown query type\n");
158 retval = udevdb_open_ro();
160 printf("unable to open udev database\n");
163 retval = udevdb_dump(print_record);
168 printf("udev, version %s\n", UDEV_VERSION);
179 /* process options */
181 if (path[0] == '\0') {
182 printf("query needs device path specified\n");
186 retval = udevdb_open_ro();
188 printf("unable to open udev database\n");
191 retval = udevdb_get_dev(path, &dev);
196 strfieldcpy(result, udev_root);
197 strncat(result, dev.name, sizeof(result));
201 strfieldcpy(result, dev.symlink);
205 strfieldcpy(result, dev.group);
209 strfieldcpy(result, dev.owner);
215 printf("%s\n", result);
217 printf("device not found in udev database\n");
224 printf("%s\n", udev_root);
229 printf("Usage: [-pqrdVh]\n"
230 " -q TYPE query database for the specified value:\n"
231 " 'name' name of device node\n"
232 " 'symlink' pointing to node\n"
235 " -p PATH sysfs device path used for query\n"
236 " -r print udev root\n"
237 " -d dump whole database\n"
238 " -V print udev version\n"
239 " -h print this help text\n"
244 static char *subsystem_blacklist[] = {
251 static inline int udev_hotplug(int argc, char **argv)
256 int retval = -EINVAL;
261 devpath = get_devpath();
266 dbg("looking at '%s'", devpath);
268 /* we only care about class devices and block stuff */
269 if (!strstr(devpath, "class") &&
270 !strstr(devpath, "block")) {
271 dbg("not a block or class device");
275 /* skip blacklisted subsystems */
277 while (subsystem_blacklist[i][0] != '\0') {
278 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
279 dbg("don't care about '%s' devices", subsystem);
285 action = get_action();
291 /* connect to the system message bus */
294 /* initialize udev database */
295 retval = udevdb_init(UDEVDB_DEFAULT);
297 dbg("unable to initialize database");
301 /* set up a default signal handler for now */
302 signal(SIGINT, sig_handler);
303 signal(SIGTERM, sig_handler);
304 signal(SIGKILL, sig_handler);
306 /* initialize the naming deamon */
309 if (strcmp(action, "add") == 0)
310 retval = udev_add_device(devpath, subsystem);
312 else if (strcmp(action, "remove") == 0)
313 retval = udev_remove_device(devpath, subsystem);
316 dbg("unknown action '%s'", action);
322 /* disconnect from the system message bus */
332 int main(int argc, char **argv, char **envp)
338 dbg("version %s", UDEV_VERSION);
340 /* initialize our configuration */
343 if (argc == 2 && argv[1][0] != '-') {
344 dbg("called by hotplug");
345 retval = udev_hotplug(argc, argv);
347 dbg("called by user");
348 retval = udev_user(argc, argv);