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"
36 #include "udev_utils.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 /* (for now) true if udevsend is the helper */
59 static int manage_hotplug_event(void) {
64 fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
68 len = read(fd, helper, 256);
75 if (strstr(helper, "udevsend"))
82 static void asmlinkage sig_handler(int signum)
93 int main(int argc, char *argv[], char *envp[])
96 struct sysfs_class_device *class_dev;
97 struct sysfs_device *devices_dev;
99 char path[SYSFS_PATH_MAX];
100 int retval = -EINVAL;
102 const char *action = getenv("ACTION");
103 const char *devpath = getenv("DEVPATH");
104 const char *subsystem = argv[1];
106 dbg("version %s", UDEV_VERSION);
107 logging_init("udev");
110 /* set signal handlers */
111 act.sa_handler = (void (*) (int))sig_handler;
112 sigemptyset (&act.sa_mask);
114 /* alarm must not restart syscalls*/
115 sigaction(SIGALRM, &act, NULL);
116 sigaction(SIGINT, &act, NULL);
117 sigaction(SIGTERM, &act, NULL);
119 /* trigger timeout to interrupt blocking syscalls */
120 alarm(ALARM_TIMEOUT);
122 udev_set_values(&udev, devpath, subsystem, action);
124 if (strstr(argv[0], "udevstart") || (argv[1] != NULL && strstr(argv[1], "udevstart"))) {
127 /* disable all logging, as it's much too slow on some facilities */
131 retval = udev_start();
150 /* export logging flag, as called scripts may want to do the same as udev */
152 setenv("UDEV_LOG", "1", 1);
154 if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
155 if (strcmp(action, "add") == 0) {
156 /* wait for sysfs and possibly add node */
159 /* skip subsystems without "dev", but handle net devices */
160 if (udev.type != 'n' && subsystem_expect_no_dev(udev.subsystem)) {
161 dbg("don't care about '%s' devices", udev.subsystem);
165 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
166 class_dev = wait_class_device_open(path);
167 if (class_dev == NULL) {
168 dbg ("open class device failed");
171 dbg("opened class_dev->name='%s'", class_dev->name);
173 wait_for_class_device(class_dev, &error);
175 /* init rules, permissions */
178 /* name, create node, store in db */
179 retval = udev_add_device(&udev, class_dev);
181 sysfs_close_class_device(class_dev);
182 } else if (strcmp(action, "remove") == 0) {
183 /* possibly remove a node */
186 /* skip subsystems without "dev" */
187 if (subsystem_expect_no_dev(udev.subsystem)) {
188 dbg("don't care about '%s' devices", udev.subsystem);
192 /* get node from db, remove db-entry, delete created node */
193 retval = udev_remove_device(&udev);
196 /* run dev.d/ scripts if we created/deleted a node or changed a netif name */
197 if (udev_dev_d && udev.devname[0] != '\0') {
198 setenv("DEVNAME", udev.devname, 1);
199 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
201 } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
202 if (strcmp(action, "add") == 0) {
206 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
207 devices_dev = wait_devices_device_open(path);
209 dbg("devices device unavailable (probably remove has beaten us)");
212 dbg("devices device opened '%s'", path);
214 wait_for_devices_device(devices_dev, &error);
216 sysfs_close_device(devices_dev);
217 } else if (strcmp(action, "remove") == 0) {
218 dbg("devices remove");
225 if (udev_hotplug_d && manage_hotplug_event())
226 udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);