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"
40 #include "udev_rules.h"
44 void log_message(int priority, const char *format, ...)
48 if (priority > udev_log_priority)
51 va_start(args, format);
52 vsyslog(priority, format, args);
57 static void asmlinkage sig_handler(int signum)
68 int main(int argc, char *argv[], char *envp[])
75 const char *subsystem;
79 if (argc == 2 && strcmp(argv[1], "-V") == 0) {
80 printf("%s\n", UDEV_VERSION);
86 dbg("version %s", UDEV_VERSION);
88 /* set signal handlers */
89 memset(&act, 0x00, sizeof(act));
90 act.sa_handler = (void (*)(int)) sig_handler;
91 sigemptyset (&act.sa_mask);
93 sigaction(SIGALRM, &act, NULL);
94 sigaction(SIGINT, &act, NULL);
95 sigaction(SIGTERM, &act, NULL);
97 /* trigger timeout to prevent hanging processes */
100 action = getenv("ACTION");
101 devpath = getenv("DEVPATH");
102 subsystem = getenv("SUBSYSTEM");
103 /* older kernels passed the SUBSYSTEM only as argument */
104 if (!subsystem && argc == 2)
107 if (!action || !subsystem || !devpath) {
108 err("action, subsystem or devpath missing");
112 /* export log_priority , as called programs may want to do the same as udev */
113 if (udev_log_priority) {
116 sprintf(priority, "%i", udev_log_priority);
117 setenv("UDEV_LOG", priority, 1);
120 udev_init_device(&udev, devpath, subsystem, action);
123 if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS || udev.type == DEV_NET) {
124 /* handle device node */
125 if (strcmp(action, "add") == 0) {
126 struct sysfs_class_device *class_dev;
128 /* wait for sysfs of /sys/class /sys/block */
130 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
131 path[sizeof(path)-1] = '\0';
132 class_dev = wait_class_device_open(path);
133 if (class_dev == NULL) {
134 dbg("open class device failed");
137 dbg("opened class_dev->name='%s'", class_dev->name);
138 wait_for_class_device(class_dev, &error);
140 /* get major/minor */
141 if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS)
142 udev.devt = get_devt(class_dev);
144 if (udev.type == DEV_NET || udev.devt) {
146 udev_rules_get_name(&udev, class_dev);
147 if (udev.ignore_device) {
148 info("device event will be ignored");
151 if (udev.name[0] == '\0') {
152 info("device node creation supressed");
156 /* create node, store in db */
157 retval = udev_add_device(&udev, class_dev);
159 dbg("no dev-file found");
160 udev_rules_get_run(&udev, NULL);
161 if (udev.ignore_device) {
162 info("device event will be ignored");
166 sysfs_close_class_device(class_dev);
167 } else if (strcmp(action, "remove") == 0) {
169 udev_rules_get_run(&udev, NULL);
170 if (udev.ignore_device) {
171 dbg("device event will be ignored");
175 /* get name from db, remove db-entry, delete node */
176 retval = udev_remove_device(&udev);
179 /* export name of device node or netif */
180 if (udev.devname[0] != '\0')
181 setenv("DEVNAME", udev.devname, 1);
182 } else if (udev.type == DEV_DEVICE && strcmp(action, "add") == 0) {
183 struct sysfs_device *devices_dev;
185 /* wait for sysfs of /sys/devices/ */
187 snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
188 path[sizeof(path)-1] = '\0';
189 devices_dev = wait_devices_device_open(path);
191 dbg("devices device unavailable (probably remove has beaten us)");
194 dbg("devices device opened '%s'", path);
195 wait_for_devices_device(devices_dev, &error);
196 udev_rules_get_run(&udev, devices_dev);
197 sysfs_close_device(devices_dev);
198 if (udev.ignore_device) {
199 info("device event will be ignored");
203 dbg("default handling");
204 udev_rules_get_run(&udev, NULL);
205 if (udev.ignore_device) {
206 info("device event will be ignored");
212 if (udev_run && !list_empty(&udev.run_list)) {
213 struct name_entry *name_loop;
215 dbg("executing run list");
216 list_for_each_entry(name_loop, &udev.run_list, node)
217 execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
221 udev_cleanup_device(&udev);