6 * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
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.
34 #include "libsysfs/sysfs/libsysfs.h"
37 #include "udev_sysfs.h"
38 #include "udev_version.h"
44 unsigned char logname[LOGNAME_SIZE];
45 void log_message(int level, const char *format, ...)
52 va_start(args, format);
53 vsyslog(level, format, args);
58 static void asmlinkage sig_handler(int signum)
69 int main(int argc, char *argv[], char *envp[])
72 struct sysfs_class_device *class_dev;
73 struct sysfs_device *devices_dev;
75 char path[SYSFS_PATH_MAX];
78 const char *action = getenv("ACTION");
79 const char *devpath = getenv("DEVPATH");
80 const char *subsystem = argv[1];
82 dbg("version %s", UDEV_VERSION);
86 /* set signal handlers */
87 act.sa_handler = (void (*) (int))sig_handler;
88 sigemptyset (&act.sa_mask);
90 /* alarm must not restart syscalls*/
91 sigaction(SIGALRM, &act, NULL);
92 sigaction(SIGINT, &act, NULL);
93 sigaction(SIGTERM, &act, NULL);
95 /* trigger timeout to interrupt blocking syscalls */
98 udev_set_values(&udev, devpath, subsystem, action);
100 if (strstr(argv[0], "udevstart") || (argv[1] != NULL && strstr(argv[1], "udevstart"))) {
103 /* disable all logging, as it's much too slow on some facilities */
107 retval = udev_start();
126 /* export logging flag, called scripts may want to do the same as udev */
128 setenv("UDEV_LOG", "1", 1);
130 if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
131 if (strcmp(action, "add") == 0) {
132 /* wait for sysfs and possibly add node */
135 /* skip blacklisted subsystems */
136 if (udev.type != 'n' && subsystem_expect_no_dev(udev.subsystem)) {
137 dbg("don't care about '%s' devices", udev.subsystem);
141 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
142 class_dev = wait_class_device_open(path);
143 if (class_dev == NULL) {
144 dbg ("open class device failed");
147 dbg("opened class_dev->name='%s'", class_dev->name);
149 wait_for_class_device(class_dev, &error);
151 /* init rules, permissions */
154 /* name, create node, store in db */
155 retval = udev_add_device(&udev, class_dev);
157 /* run dev.d/ scripts if we created a node or changed a netif name */
158 if (udev.devname[0] != '\0') {
159 setenv("DEVNAME", udev.devname, 1);
160 dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);
163 sysfs_close_class_device(class_dev);
164 } else if (strcmp(action, "remove") == 0) {
165 /* possibly remove a node */
168 /* get node from db, delete it */
169 retval = udev_remove_device(&udev);
171 /* run dev.d/ scripts if we're not instructed to ignore the event */
172 if (udev.devname[0] != '\0') {
173 setenv("DEVNAME", udev.devname, 1);
174 dev_d_execute(&udev, DEVD_DIR, DEVD_SUFFIX);
178 } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
179 if (strcmp(action, "add") == 0) {
183 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
184 devices_dev = wait_devices_device_open(path);
186 dbg("devices device unavailable (probably remove has beaten us)");
189 dbg("devices device opened '%s'", path);
191 wait_for_devices_device(devices_dev, &error);
193 sysfs_close_device(devices_dev);
194 } else if (strcmp(action, "remove") == 0) {
195 dbg("devices remove");