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 inline int udev_hotplug(int argc, char **argv)
147 int retval = -EINVAL;
151 devpath = get_devpath();
156 dbg("looking at '%s'", devpath);
158 /* we only care about class devices and block stuff */
159 if (!strstr(devpath, "class") &&
160 !strstr(devpath, "block")) {
161 dbg("not a block or class device");
165 /* but we don't care about net class devices */
166 if (strcmp(subsystem, "net") == 0) {
167 dbg("don't care about net devices");
171 action = get_action();
177 /* connect to the system message bus */
180 /* initialize udev database */
181 retval = udevdb_init(UDEVDB_DEFAULT);
183 dbg("unable to initialize database");
187 /* set up a default signal handler for now */
188 signal(SIGINT, sig_handler);
189 signal(SIGTERM, sig_handler);
190 signal(SIGKILL, sig_handler);
192 /* initialize the naming deamon */
195 if (strcmp(action, "add") == 0)
196 retval = udev_add_device(devpath, subsystem);
198 else if (strcmp(action, "remove") == 0)
199 retval = udev_remove_device(devpath, subsystem);
202 dbg("unknown action '%s'", action);
208 /* disconnect from the system message bus */
215 int main(int argc, char **argv, char **envp)
221 dbg("version %s", UDEV_VERSION);
223 /* initialize our configuration */
226 if (argc == 2 && argv[1][0] != '-') {
227 dbg("called by hotplug");
228 retval = udev_hotplug(argc, argv);
230 dbg("called by user");
231 retval = udev_user(argc, argv);