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.
33 #include "libsysfs/sysfs/libsysfs.h"
34 #include "udev_libc_wrapper.h"
37 #include "udev_rules.h"
38 #include "udev_utils.h"
42 dev_t get_devt(struct sysfs_class_device *class_dev)
44 struct sysfs_attribute *attr = NULL;
45 unsigned int major, minor;
48 maj = getenv("MAJOR");
49 min = getenv("MINOR");
55 attr = sysfs_get_classdev_attr(class_dev, "dev");
58 dbg("dev='%s'", attr->value);
60 if (sscanf(attr->value, "%u:%u", &major, &minor) != 2)
64 dbg("found major=%d, minor=%d", major, minor);
65 return makedev(major, minor);
68 int udev_process_event(struct udev_rules *rules, struct udevice *udev)
73 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS || udev->type == DEV_NET) {
74 /* handle device node */
75 if (strcmp(udev->action, "add") == 0) {
76 struct sysfs_class_device *class_dev;
79 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
80 path[sizeof(path)-1] = '\0';
81 class_dev = sysfs_open_class_device_path(path);
82 if (class_dev == NULL) {
83 dbg("open class device failed");
86 dbg("opened class_dev->name='%s'", class_dev->name);
89 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS)
90 udev->devt = get_devt(class_dev);
92 if (udev->type == DEV_NET || udev->devt) {
94 udev_rules_get_name(rules, udev, class_dev);
95 if (udev->ignore_device) {
96 info("device event will be ignored");
97 sysfs_close_class_device(class_dev);
100 if (udev->name[0] != '\0') {
101 /* create node, store in db */
102 retval = udev_add_device(udev, class_dev);
104 info("device node creation supressed");
107 dbg("no dev-file found");
108 udev_rules_get_run(rules, udev, class_dev, NULL);
109 if (udev->ignore_device) {
110 info("device event will be ignored");
111 sysfs_close_class_device(class_dev);
115 sysfs_close_class_device(class_dev);
116 } else if (strcmp(udev->action, "remove") == 0) {
117 struct name_entry *name_loop;
119 /* get data from db, remove db-entry, delete node */
121 retval = udev_remove_device(udev);
123 /* restore stored persistent data */
124 list_for_each_entry(name_loop, &udev->env_list, node)
125 putenv(name_loop->name);
127 udev_rules_get_run(rules, udev, NULL, NULL);
128 if (udev->ignore_device) {
129 dbg("device event will be ignored");
133 } else if (udev->type == DEV_DEVICE && strcmp(udev->action, "add") == 0) {
134 struct sysfs_device *devices_dev;
137 snprintf(path, sizeof(path), "%s%s", sysfs_path, udev->devpath);
138 path[sizeof(path)-1] = '\0';
139 devices_dev = sysfs_open_device_path(path);
141 dbg("devices device unavailable (probably remove has beaten us)");
145 dbg("devices device opened '%s'", path);
146 udev_rules_get_run(rules, udev, NULL, devices_dev);
147 sysfs_close_device(devices_dev);
148 if (udev->ignore_device) {
149 info("device event will be ignored");
153 dbg("default handling");
154 udev_rules_get_run(rules, udev, NULL, NULL);
155 if (udev->ignore_device) {
156 info("device event will be ignored");