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"
35 #include "udev_libc_wrapper.h"
37 #include "udev_utils.h"
38 #include "udev_sysfs.h"
39 #include "udev_version.h"
45 void log_message(int level, const char *format, ...)
52 va_start(args, format);
53 vsyslog(level, format, args);
58 /* decide if we should manage the whole hotplug event
59 * for now look if the kernel calls udevsend instead of /sbin/hotplug
61 static int manage_hotplug_event(void) {
66 /* don't handle hotplug.d if we are called directly */
67 if (!getenv("UDEVD_EVENT"))
70 fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
74 len = read(fd, helper, 256);
81 if (strstr(helper, "udevsend"))
87 static void asmlinkage sig_handler(int signum)
98 int main(int argc, char *argv[], char *envp[])
100 struct sysfs_class_device *class_dev;
101 struct sysfs_device *devices_dev;
103 char path[PATH_SIZE];
107 const char *subsystem;
109 struct sigaction act;
110 int retval = -EINVAL;
112 if (argc == 2 && strcmp(argv[1], "-V") == 0) {
113 printf("%s\n", UDEV_VERSION);
117 logging_init("udev");
118 dbg("version %s", UDEV_VERSION);
122 /* set signal handlers */
123 memset(&act, 0x00, sizeof(act));
124 act.sa_handler = (void (*) (int))sig_handler;
125 sigemptyset (&act.sa_mask);
127 sigaction(SIGALRM, &act, NULL);
128 sigaction(SIGINT, &act, NULL);
129 sigaction(SIGTERM, &act, NULL);
131 /* trigger timeout to prevent hanging processes */
132 alarm(ALARM_TIMEOUT);
134 /* let the executed programs know if we handle the whole hotplug event */
135 managed_event = manage_hotplug_event();
137 setenv("MANAGED_EVENT", "1", 1);
139 action = getenv("ACTION");
140 devpath = getenv("DEVPATH");
141 subsystem = getenv("SUBSYSTEM");
142 /* older kernels passed the SUBSYSTEM only as argument */
143 if (!subsystem && argc == 2)
146 udev_init_device(&udev, devpath, subsystem);
148 if (!action || !subsystem || !devpath) {
149 dbg("action, subsystem or devpath missing");
153 /* export logging flag, as called scripts may want to do the same as udev */
155 setenv("UDEV_LOG", "1", 1);
157 if (udev.type == BLOCK || udev.type == CLASS || udev.type == NET) {
158 if (strcmp(action, "add") == 0) {
159 /* wait for sysfs and possibly add node */
162 /* skip subsystems without "dev", but handle net devices */
163 if (udev.type != NET && subsystem_expect_no_dev(udev.subsystem)) {
164 dbg("don't care about '%s' devices", udev.subsystem);
168 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
169 path[sizeof(path)-1] = '\0';
170 class_dev = wait_class_device_open(path);
171 if (class_dev == NULL) {
172 dbg ("open class device failed");
175 dbg("opened class_dev->name='%s'", class_dev->name);
177 wait_for_class_device(class_dev, &error);
182 /* name, create node, store in db */
183 retval = udev_add_device(&udev, class_dev);
185 sysfs_close_class_device(class_dev);
186 } else if (strcmp(action, "remove") == 0) {
187 /* possibly remove a node */
190 /* skip subsystems without "dev" */
191 if (subsystem_expect_no_dev(udev.subsystem)) {
192 dbg("don't care about '%s' devices", udev.subsystem);
196 /* get node from db, remove db-entry, delete created node */
197 retval = udev_remove_device(&udev);
200 /* run dev.d/ scripts if we created/deleted a node or changed a netif name */
201 if (udev.devname[0] != '\0') {
202 setenv("DEVNAME", udev.devname, 1);
204 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
206 } else if (udev.type == PHYSDEV) {
207 if (strcmp(action, "add") == 0) {
211 snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
212 path[sizeof(path)-1] = '\0';
213 devices_dev = wait_devices_device_open(path);
215 dbg("devices device unavailable (probably remove has beaten us)");
218 dbg("devices device opened '%s'", path);
220 wait_for_devices_device(devices_dev, &error);
222 sysfs_close_device(devices_dev);
223 } else if (strcmp(action, "remove") == 0) {
224 dbg("devices remove");
231 udev_cleanup_device(&udev);
232 if (udev_hotplug_d && managed_event)
233 udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);