2 * udev_event.c - udev event process
4 * Copyright (C) 2004, 2005 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include "udev_libc_wrapper.h"
35 #include "udev_rules.h"
36 #include "udev_utils.h"
37 #include "udev_sysfs.h"
41 int udev_process_event(struct udev_rules *rules, struct udevice *udev)
47 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS || udev->type == DEV_NET) {
48 /* handle device node */
49 if (strcmp(udev->action, "add") == 0) {
50 struct sysfs_class_device *class_dev;
52 /* wait for sysfs of /sys/class /sys/block */
54 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
55 path[sizeof(path)-1] = '\0';
56 class_dev = wait_class_device_open(path);
57 if (class_dev == NULL) {
58 dbg("open class device failed");
61 dbg("opened class_dev->name='%s'", class_dev->name);
62 wait_for_class_device(class_dev, &error);
65 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS)
66 udev->devt = get_devt(class_dev);
68 if (udev->type == DEV_NET || udev->devt) {
70 udev_rules_get_name(rules, udev, class_dev);
71 if (udev->ignore_device) {
72 info("device event will be ignored");
73 sysfs_close_class_device(class_dev);
76 if (udev->name[0] == '\0') {
77 info("device node creation supressed");
78 sysfs_close_class_device(class_dev);
81 /* create node, store in db */
82 retval = udev_add_device(udev, class_dev);
84 dbg("no dev-file found");
85 udev_rules_get_run(rules, udev, class_dev, NULL);
86 if (udev->ignore_device) {
87 info("device event will be ignored");
88 sysfs_close_class_device(class_dev);
92 sysfs_close_class_device(class_dev);
93 } else if (strcmp(udev->action, "remove") == 0) {
94 struct name_entry *name_loop;
96 /* get data from db, remove db-entry, delete node */
98 retval = udev_remove_device(udev);
100 /* restore stored persistent data */
101 list_for_each_entry(name_loop, &udev->env_list, node)
102 putenv(name_loop->name);
104 udev_rules_get_run(rules, udev, NULL, NULL);
105 if (udev->ignore_device) {
106 dbg("device event will be ignored");
111 /* export name of device node or netif */
112 if (udev->devname[0] != '\0')
113 setenv("DEVNAME", udev->devname, 1);
114 } else if (udev->type == DEV_DEVICE && strcmp(udev->action, "add") == 0) {
115 struct sysfs_device *devices_dev;
117 /* wait for sysfs of /sys/devices/ */
119 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
120 path[sizeof(path)-1] = '\0';
121 devices_dev = wait_devices_device_open(path);
123 dbg("devices device unavailable (probably remove has beaten us)");
126 dbg("devices device opened '%s'", path);
127 wait_for_devices_device(devices_dev, &error);
128 udev_rules_get_run(rules, udev, NULL, devices_dev);
129 sysfs_close_device(devices_dev);
130 if (udev->ignore_device) {
131 info("device event will be ignored");
135 dbg("default handling");
136 udev_rules_get_run(rules, udev, NULL, NULL);
137 if (udev->ignore_device) {
138 info("device event will be ignored");