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 inline int udev_user(int argc, char **argv)
87 static const char short_options[] = "q:rVh";
93 option = getopt(argc, argv, short_options);
97 dbg("option '%c'", option);
100 dbg("udev query: %s\n", optarg);
101 retval = udevdb_open_ro();
103 printf("unable to open udev database\n");
106 retval = udevdb_get_dev(optarg, &dev);
108 printf("%s\n", dev.name);
110 printf("device not found in udev database\n");
116 printf("%s\n", udev_root);
120 printf("udev, version %s\n", UDEV_VERSION);
132 printf("Usage: [-qrVh]\n"
133 " -q <path> query database for the name of the created node\n"
134 " -r print udev root\n"
135 " -V print udev version\n"
136 " -h print this help text\n"
142 static char *subsystem_blacklist[] = {
149 static inline int udev_hotplug(int argc, char **argv)
154 int retval = -EINVAL;
159 devpath = get_devpath();
164 dbg("looking at '%s'", devpath);
166 /* we only care about class devices and block stuff */
167 if (!strstr(devpath, "class") &&
168 !strstr(devpath, "block")) {
169 dbg("not a block or class device");
173 /* skip blacklisted subsystems */
175 while (subsystem_blacklist[i][0] != '\0') {
176 if (strcmp(subsystem, subsystem_blacklist[i]) == 0) {
177 dbg("don't care about '%s' devices", subsystem);
183 action = get_action();
189 /* connect to the system message bus */
192 /* initialize udev database */
193 retval = udevdb_init(UDEVDB_DEFAULT);
195 dbg("unable to initialize database");
199 /* set up a default signal handler for now */
200 signal(SIGINT, sig_handler);
201 signal(SIGTERM, sig_handler);
202 signal(SIGKILL, sig_handler);
204 /* initialize the naming deamon */
207 if (strcmp(action, "add") == 0)
208 retval = udev_add_device(devpath, subsystem);
210 else if (strcmp(action, "remove") == 0)
211 retval = udev_remove_device(devpath, subsystem);
214 dbg("unknown action '%s'", action);
220 /* disconnect from the system message bus */
227 int main(int argc, char **argv, char **envp)
233 dbg("version %s", UDEV_VERSION);
235 /* initialize our configuration */
238 if (argc == 2 && argv[1][0] != '-') {
239 dbg("called by hotplug");
240 retval = udev_hotplug(argc, argv);
242 dbg("called by user");
243 retval = udev_user(argc, argv);