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 /* false, if we are called directly */
65 if (!getenv("MANAGED_EVENT"))
68 fd = open("/proc/sys/kernel/hotplug", O_RDONLY);
72 len = read(fd, helper, 256);
79 if (strstr(helper, "udevsend"))
86 static void asmlinkage sig_handler(int signum)
97 int main(int argc, char *argv[], char *envp[])
100 struct sysfs_class_device *class_dev;
101 struct sysfs_device *devices_dev;
103 char path[SYSFS_PATH_MAX];
104 int retval = -EINVAL;
106 const char *action = getenv("ACTION");
107 const char *devpath = getenv("DEVPATH");
108 const char *subsystem = argv[1];
110 dbg("version %s", UDEV_VERSION);
111 logging_init("udev");
114 /* set signal handlers */
115 act.sa_handler = (void (*) (int))sig_handler;
116 sigemptyset (&act.sa_mask);
118 sigaction(SIGALRM, &act, NULL);
119 sigaction(SIGINT, &act, NULL);
120 sigaction(SIGTERM, &act, NULL);
122 /* trigger timeout to prevent hanging processes */
123 alarm(ALARM_TIMEOUT);
125 action = getenv("ACTION");
126 devpath = getenv("DEVPATH");
127 subsystem = getenv("SUBSYSTEM");
128 /* older kernels passed the SUBSYSTEM only as argument */
129 if (!subsystem && argc == 2)
131 udev_init_device(&udev, devpath, subsystem);
133 if (strstr(argv[0], "udevstart") || (argc == 2 && strstr(argv[1], "udevstart"))) {
136 /* disable all logging, as it's much too slow on some facilities */
140 retval = udev_start();
159 /* export logging flag, as called scripts may want to do the same as udev */
161 setenv("UDEV_LOG", "1", 1);
163 if ((strncmp(devpath, "/block/", 7) == 0) || (strncmp(devpath, "/class/", 7) == 0)) {
164 if (strcmp(action, "add") == 0) {
165 /* wait for sysfs and possibly add node */
168 /* skip subsystems without "dev", but handle net devices */
169 if (udev.type != 'n' && subsystem_expect_no_dev(udev.subsystem)) {
170 dbg("don't care about '%s' devices", udev.subsystem);
174 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
175 class_dev = wait_class_device_open(path);
176 if (class_dev == NULL) {
177 dbg ("open class device failed");
180 dbg("opened class_dev->name='%s'", class_dev->name);
182 wait_for_class_device(class_dev, &error);
184 /* init rules, permissions */
187 /* name, create node, store in db */
188 retval = udev_add_device(&udev, class_dev);
190 sysfs_close_class_device(class_dev);
191 } else if (strcmp(action, "remove") == 0) {
192 /* possibly remove a node */
195 /* skip subsystems without "dev" */
196 if (subsystem_expect_no_dev(udev.subsystem)) {
197 dbg("don't care about '%s' devices", udev.subsystem);
201 /* get node from db, remove db-entry, delete created node */
202 retval = udev_remove_device(&udev);
205 /* run dev.d/ scripts if we created/deleted a node or changed a netif name */
206 if (udev_dev_d && udev.devname[0] != '\0') {
207 setenv("DEVNAME", udev.devname, 1);
208 udev_multiplex_directory(&udev, DEVD_DIR, DEVD_SUFFIX);
210 } else if ((strncmp(devpath, "/devices/", 9) == 0)) {
211 if (strcmp(action, "add") == 0) {
215 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, devpath);
216 devices_dev = wait_devices_device_open(path);
218 dbg("devices device unavailable (probably remove has beaten us)");
221 dbg("devices device opened '%s'", path);
223 wait_for_devices_device(devices_dev, &error);
225 sysfs_close_device(devices_dev);
226 } else if (strcmp(action, "remove") == 0) {
227 dbg("devices remove");
234 if (udev_hotplug_d && manage_hotplug_event())
235 udev_multiplex_directory(&udev, HOTPLUGD_DIR, HOTPLUG_SUFFIX);