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[])
71 struct udev_rules rules;
76 const char *subsystem;
80 if (argc == 2 && strcmp(argv[1], "-V") == 0) {
81 printf("%s\n", UDEV_VERSION);
87 dbg("version %s", UDEV_VERSION);
89 /* set signal handlers */
90 memset(&act, 0x00, sizeof(act));
91 act.sa_handler = (void (*)(int)) sig_handler;
92 sigemptyset (&act.sa_mask);
94 sigaction(SIGALRM, &act, NULL);
95 sigaction(SIGINT, &act, NULL);
96 sigaction(SIGTERM, &act, NULL);
98 /* trigger timeout to prevent hanging processes */
99 alarm(UDEV_ALARM_TIMEOUT);
101 action = getenv("ACTION");
102 devpath = getenv("DEVPATH");
103 subsystem = getenv("SUBSYSTEM");
104 /* older kernels passed the SUBSYSTEM only as argument */
105 if (!subsystem && argc == 2)
108 if (!action || !subsystem || !devpath) {
109 err("action, subsystem or devpath missing");
113 /* export log_priority , as called programs may want to do the same as udev */
114 if (udev_log_priority) {
117 sprintf(priority, "%i", udev_log_priority);
118 setenv("UDEV_LOG", priority, 1);
121 udev_init_device(&udev, devpath, subsystem, action);
122 udev_rules_init(&rules, 0);
124 if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS || udev.type == DEV_NET) {
125 /* handle device node */
126 if (strcmp(action, "add") == 0) {
127 struct sysfs_class_device *class_dev;
129 /* wait for sysfs of /sys/class /sys/block */
131 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
132 path[sizeof(path)-1] = '\0';
133 class_dev = wait_class_device_open(path);
134 if (class_dev == NULL) {
135 dbg("open class device failed");
138 dbg("opened class_dev->name='%s'", class_dev->name);
139 wait_for_class_device(class_dev, &error);
141 /* get major/minor */
142 if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS)
143 udev.devt = get_devt(class_dev);
145 if (udev.type == DEV_NET || udev.devt) {
147 udev_rules_get_name(&rules, &udev, class_dev);
148 if (udev.ignore_device) {
149 info("device event will be ignored");
152 if (udev.name[0] == '\0') {
153 info("device node creation supressed");
157 /* create node, store in db */
158 retval = udev_add_device(&udev, class_dev);
160 dbg("no dev-file found");
161 udev_rules_get_run(&rules, &udev, NULL);
162 if (udev.ignore_device) {
163 info("device event will be ignored");
167 sysfs_close_class_device(class_dev);
168 } else if (strcmp(action, "remove") == 0) {
170 udev_rules_get_run(&rules, &udev, NULL);
171 if (udev.ignore_device) {
172 dbg("device event will be ignored");
176 /* get name from db, remove db-entry, delete node */
177 retval = udev_remove_device(&udev);
180 /* export name of device node or netif */
181 if (udev.devname[0] != '\0')
182 setenv("DEVNAME", udev.devname, 1);
183 } else if (udev.type == DEV_DEVICE && strcmp(action, "add") == 0) {
184 struct sysfs_device *devices_dev;
186 /* wait for sysfs of /sys/devices/ */
188 snprintf(path, sizeof(path), "%s%s", sysfs_path, devpath);
189 path[sizeof(path)-1] = '\0';
190 devices_dev = wait_devices_device_open(path);
192 dbg("devices device unavailable (probably remove has beaten us)");
195 dbg("devices device opened '%s'", path);
196 wait_for_devices_device(devices_dev, &error);
197 udev_rules_get_run(&rules, &udev, devices_dev);
198 sysfs_close_device(devices_dev);
199 if (udev.ignore_device) {
200 info("device event will be ignored");
204 dbg("default handling");
205 udev_rules_get_run(&rules, &udev, NULL);
206 if (udev.ignore_device) {
207 info("device event will be ignored");
213 if (udev_run && !list_empty(&udev.run_list)) {
214 struct name_entry *name_loop;
216 dbg("executing run list");
217 list_for_each_entry(name_loop, &udev.run_list, node)
218 execute_program(name_loop->name, udev.subsystem, NULL, 0, NULL);
222 udev_cleanup_device(&udev);