2 * udev_utils.c - generic stuff used by udev
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.
31 #include "udev_rules.h"
34 struct udevice *udev_device_init(void)
38 udev = malloc(sizeof(struct udevice));
41 memset(udev, 0x00, sizeof(struct udevice));
43 INIT_LIST_HEAD(&udev->symlink_list);
44 INIT_LIST_HEAD(&udev->run_list);
45 INIT_LIST_HEAD(&udev->env_list);
47 /* set sysfs device to local storage, can be overridden if needed */
48 udev->dev = &udev->dev_local;
50 /* default node permissions */
52 strcpy(udev->owner, "root");
53 strcpy(udev->group, "root");
58 void udev_device_cleanup(struct udevice *udev)
60 name_list_cleanup(&udev->symlink_list);
61 name_list_cleanup(&udev->run_list);
62 name_list_cleanup(&udev->env_list);
66 dev_t udev_device_get_devt(struct udevice *udev)
69 unsigned int major, minor;
71 /* read it from sysfs */
72 attr = sysfs_attr_get_value(udev->dev->devpath, "dev");
74 if (sscanf(attr, "%u:%u", &major, &minor) == 2)
75 return makedev(major, minor);
80 int udev_device_event(struct udev_rules *rules, struct udevice *udev)
84 /* device node or netif */
85 if ((major(udev->devt) != 0 || strcmp(udev->dev->subsystem, "net") == 0) &&
86 strcmp(udev->action, "add") == 0) {
87 dbg("device node or netif add '%s'", udev->dev->devpath);
88 udev_rules_get_name(rules, udev);
89 if (udev->ignore_device) {
90 info("device event will be ignored");
93 /* create node, store in db */
94 if (udev->name[0] != '\0')
95 retval = udev_add_device(udev);
97 info("device node creation supressed");
101 if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
102 struct name_entry *name_loop;
104 udev_rules_get_run(rules, udev);
105 if (udev->ignore_device) {
106 info("device event will be ignored");
109 /* get data from db, remove db-entry, delete node */
110 retval = udev_remove_device(udev);
112 /* restore stored persistent data */
113 list_for_each_entry(name_loop, &udev->env_list, node)
114 putenv(name_loop->name);
118 /* default devices without a node */
119 udev_rules_get_run(rules, udev);
120 if (udev->ignore_device)
121 info("device event will be ignored");