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 /* create node, store in db */
78 retval = udev_add_device(udev, class_dev);
80 info("device node creation supressed");
83 dbg("no dev-file found");
84 udev_rules_get_run(rules, udev, class_dev, NULL);
85 if (udev->ignore_device) {
86 info("device event will be ignored");
87 sysfs_close_class_device(class_dev);
91 sysfs_close_class_device(class_dev);
92 } else if (strcmp(udev->action, "remove") == 0) {
93 struct name_entry *name_loop;
95 /* get data from db, remove db-entry, delete node */
97 retval = udev_remove_device(udev);
99 /* restore stored persistent data */
100 list_for_each_entry(name_loop, &udev->env_list, node)
101 putenv(name_loop->name);
103 udev_rules_get_run(rules, udev, NULL, NULL);
104 if (udev->ignore_device) {
105 dbg("device event will be ignored");
110 /* export name of device node or netif */
111 if (udev->devname[0] != '\0')
112 setenv("DEVNAME", udev->devname, 1);
113 } else if (udev->type == DEV_DEVICE && strcmp(udev->action, "add") == 0) {
114 struct sysfs_device *devices_dev;
116 /* wait for sysfs of /sys/devices/ */
118 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
119 path[sizeof(path)-1] = '\0';
120 devices_dev = wait_devices_device_open(path);
122 dbg("devices device unavailable (probably remove has beaten us)");
125 dbg("devices device opened '%s'", path);
126 wait_for_devices_device(devices_dev, &error);
127 udev_rules_get_run(rules, udev, NULL, devices_dev);
128 sysfs_close_device(devices_dev);
129 if (udev->ignore_device) {
130 info("device event will be ignored");
134 dbg("default handling");
135 udev_rules_get_run(rules, udev, NULL, NULL);
136 if (udev->ignore_device) {
137 info("device event will be ignored");