6 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include "libsysfs/sysfs/libsysfs.h"
35 #include "udev_sysfs.h"
36 #include "udev_version.h"
42 unsigned char logname[LOGNAME_SIZE];
43 void log_message(int level, const char *format, ...)
50 va_start(args, format);
51 vsyslog(level, format, args);
56 static void asmlinkage sig_handler(int signum)
67 int main(int argc, char *argv[], char *envp[])
70 struct sysfs_class_device *class_dev;
72 char path[SYSFS_PATH_MAX];
80 dbg("version %s", UDEV_VERSION);
84 /* export logging flag, callouts may want to do the same as udev */
86 setenv("UDEV_LOG", "1", 1);
88 if (strstr(argv[0], "udevstart") || (argv[1] != NULL && strstr(argv[1], "udevstart"))) {
91 const char *action = getenv("ACTION");
92 const char *devpath = getenv("DEVPATH");
93 const char *subsystem = argv[1];
99 if (strcmp(action, "add") == 0) {
101 } else if (strcmp(action, "remove") == 0) {
104 dbg("no action '%s' for us", action);
112 dbg("looking at '%s'", devpath);
114 /* we only care about class devices and block stuff */
115 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
116 dbg("not a block or class device");
125 udev_set_values(&udev, devpath, subsystem, action);
127 /* skip blacklisted subsystems */
128 if (udev.type != 'n' && subsystem_expect_no_dev(subsystem)) {
129 dbg("don't care about '%s' devices", subsystem);
135 /* set signal handlers */
136 act.sa_handler = (void (*) (int))sig_handler;
137 sigemptyset (&act.sa_mask);
139 /* alarm must not restart syscalls*/
140 sigaction(SIGALRM, &act, NULL);
141 sigaction(SIGINT, &act, NULL);
142 sigaction(SIGTERM, &act, NULL);
144 /* trigger timout to interrupt blocking syscalls */
145 alarm(ALARM_TIMEOUT);
151 /* disable all logging as it's much too slow on some facilities */
153 unsetenv("UDEV_LOG");
156 retval = udev_start();
161 /* open the device */
162 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
163 class_dev = sysfs_open_class_device_path(path);
164 if (class_dev == NULL) {
165 dbg ("sysfs_open_class_device_path failed");
168 dbg("opened class_dev->name='%s'", class_dev->name);
173 /* name, create node, store in db */
174 retval = udev_add_device(&udev, class_dev);
176 /* run dev.d/ scripts if we created a node or changed a netif name */
177 if (udev.devname[0] != '\0') {
178 setenv("DEVNAME", udev.devname, 1);
179 dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);
182 sysfs_close_class_device(class_dev);
187 /* get node from db, delete it*/
188 retval = udev_remove_device(&udev);
190 /* run dev.d/ scripts if we're not instructed to ignore the event */
191 if (udev.devname[0] != '\0') {
192 setenv("DEVNAME", udev.devname, 1);
193 dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);