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 void log_message(int level, const char *format, ...)
51 va_start(args, format);
52 vsyslog(level, format, args);
57 /* decide if we should manage the whole hotplug event
58 * for now look if the kernel calls udevsend instead of /sbin/hotplug
60 static int manage_hotplug_event(void) {
65 /* don't handle hotplug.d if we are called directly */
66 if (!getenv("UDEVD_EVENT"))
69 fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
73 len = read(fd, helper, 256);
80 if (strstr(helper, "udevsend"))
86 static void asmlinkage sig_handler(int signum)
97 int main(int argc, char *argv[], char *envp[])
99 struct sysfs_class_device *class_dev;
100 struct sysfs_device *devices_dev;
102 char path[SYSFS_PATH_MAX];
106 const char *subsystem;
108 struct sigaction act;
109 int retval = -EINVAL;
111 if (argc == 2 && strcmp(argv[1], "-V") == 0) {
112 printf("%s\n", UDEV_VERSION);
116 logging_init("udev");
117 dbg("version %s", UDEV_VERSION);
121 /* set signal handlers */
122 memset(&act, 0x00, sizeof(act));
123 act.sa_handler = (void (*) (int))sig_handler;
124 sigemptyset (&act.sa_mask);
126 sigaction(SIGALRM, &act, NULL);
127 sigaction(SIGINT, &act, NULL);
128 sigaction(SIGTERM, &act, NULL);
130 /* trigger timeout to prevent hanging processes */
131 alarm(ALARM_TIMEOUT);
133 /* let the executed programs know if we handle the whole hotplug event */
134 managed_event = manage_hotplug_event();
136 setenv("MANAGED_EVENT", "1", 1);
138 action = getenv("ACTION");
139 devpath = getenv("DEVPATH");
140 subsystem = getenv("SUBSYSTEM");
141 /* older kernels passed the SUBSYSTEM only as argument */
142 if (!subsystem && argc == 2)
145 udev_init_device(&udev, devpath, subsystem);
147 if (!action || !subsystem || !devpath) {
148 dbg("action, subsystem or devpath missing");
152 /* export logging flag, as called scripts may want to do the same as udev */
154 setenv("UDEV_LOG", "1", 1);
156 if (udev.type == BLOCK || udev.type == CLASS || udev.type == NET) {
157 if (strcmp(action, "add") == 0) {
158 /* wait for sysfs and possibly add node */
161 /* skip subsystems without "dev", but handle net devices */
162 if (udev.type != NET && subsystem_expect_no_dev(udev.subsystem)) {
163 dbg("don't care about '%s' devices", udev.subsystem);
167 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
168 class_dev = wait_class_device_open(path);
169 if (class_dev == NULL) {
170 dbg ("open class device failed");
173 dbg("opened class_dev->name='%s'", class_dev->name);
175 wait_for_class_device(class_dev, &error);
180 /* name, create node, store in db */
181 retval = udev_add_device(&udev, class_dev);
183 sysfs_close_class_device(class_dev);
184 } else if (strcmp(action, "remove") == 0) {
185 /* possibly remove a node */
188 /* skip subsystems without "dev" */
189 if (subsystem_expect_no_dev(udev.subsystem)) {
190 dbg("don't care about '%s' devices", udev.subsystem);
194 /* get node from db, remove db-entry, delete created node */
195 retval = udev_remove_device(&udev);
198 /* run dev.d/ scripts if we created/deleted a node or changed a netif name */
199 if (udev.devname[0] != '\0') {
200 setenv("DEVNAME", udev.devname, 1);
202 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
204 } else if (udev.type == PHYSDEV) {
205 if (strcmp(action, "add") == 0) {
209 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
210 devices_dev = wait_devices_device_open(path);
212 dbg("devices device unavailable (probably remove has beaten us)");
215 dbg("devices device opened '%s'", path);
217 wait_for_devices_device(devices_dev, &error);
219 sysfs_close_device(devices_dev);
220 } else if (strcmp(action, "remove") == 0) {
221 dbg("devices remove");
228 udev_cleanup_device(&udev);
229 if (udev_hotplug_d && managed_event)
230 udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);