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.
34 #include <sys/utsname.h>
36 #include "udev_libc_wrapper.h"
39 #include "udev_utils.h"
40 #include "udev_sysfs.h"
44 int udev_init_device(struct udevice *udev, const char* devpath, const char *subsystem, const char *action)
48 memset(udev, 0x00, sizeof(struct udevice));
49 INIT_LIST_HEAD(&udev->symlink_list);
50 INIT_LIST_HEAD(&udev->run_list);
51 INIT_LIST_HEAD(&udev->env_list);
54 strlcpy(udev->subsystem, subsystem, sizeof(udev->subsystem));
57 strlcpy(udev->action, action, sizeof(udev->action));
60 strlcpy(udev->devpath, devpath, sizeof(udev->devpath));
61 remove_trailing_char(udev->devpath, '/');
63 if (strncmp(udev->devpath, "/block/", 7) == 0)
64 udev->type = DEV_BLOCK;
65 else if (strncmp(udev->devpath, "/class/net/", 11) == 0)
67 else if (strncmp(udev->devpath, "/class/", 7) == 0)
68 udev->type = DEV_CLASS;
69 else if (strncmp(udev->devpath, "/devices/", 9) == 0)
70 udev->type = DEV_DEVICE;
73 pos = strrchr(udev->devpath, '/');
75 strlcpy(udev->kernel_name, &pos[1], sizeof(udev->kernel_name));
76 dbg("kernel_name='%s'", udev->kernel_name);
78 /* Some block devices have '!' in their name, change that to '/' */
79 pos = udev->kernel_name;
80 while (pos[0] != '\0') {
86 /* get kernel number */
87 pos = &udev->kernel_name[strlen(udev->kernel_name)];
88 while (isdigit(pos[-1]))
90 strlcpy(udev->kernel_number, pos, sizeof(udev->kernel_number));
91 dbg("kernel_number='%s'", udev->kernel_number);
95 if (udev->type == DEV_BLOCK || udev->type == DEV_CLASS) {
97 strcpy(udev->owner, "root");
98 strcpy(udev->group, "root");
104 void udev_cleanup_device(struct udevice *udev)
106 struct name_entry *name_loop;
107 struct name_entry *temp_loop;
109 list_for_each_entry_safe(name_loop, temp_loop, &udev->symlink_list, node) {
110 list_del(&name_loop->node);
113 list_for_each_entry_safe(name_loop, temp_loop, &udev->run_list, node) {
114 list_del(&name_loop->node);
117 list_for_each_entry_safe(name_loop, temp_loop, &udev->env_list, node) {
118 list_del(&name_loop->node);