2 * udevinfo - fetches attributes for a device
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 675 Mass Ave, Cambridge, MA 02139, USA.
30 # define VALUE_SIZE 200
35 static int print_all_attributes(char *path)
37 struct dlist *attributes;
38 struct sysfs_attribute *attr;
39 struct sysfs_directory *sysfs_dir;
40 char value[VALUE_SIZE];
44 sysfs_dir = sysfs_open_directory(path);
45 if (sysfs_dir == NULL)
48 attributes = sysfs_get_dir_attributes(sysfs_dir);
49 if (attributes == NULL) {
54 dlist_for_each_data(attributes, attr, struct sysfs_attribute) {
55 if (attr->value != NULL) {
56 strncpy(value, attr->value, VALUE_SIZE);
61 /* remove trailing newline */
62 if (value[len-1] == '\n') {
67 /* skip nonprintable values */
69 if (isprint(value[len-1]) == 0)
74 printf(" SYSFS_%s=\"%s\"\n", attr->name, value);
80 sysfs_close_directory(sysfs_dir);
85 int main(int argc, char **argv, char **envp)
89 struct sysfs_class_device *class_dev;
90 struct sysfs_class_device *class_dev_parent;
91 struct sysfs_attribute *attr;
92 struct sysfs_device *sysfs_dev;
93 struct sysfs_device *sysfs_dev_parent;
98 printf("Usage: udevinfo <sysfs_device_path>\n");
103 /* get the class dev */
104 class_dev = sysfs_open_class_device_path(path);
105 if (class_dev == NULL) {
106 printf("couldn't get the class device\n");
110 /* read the 'dev' file for major/minor*/
111 attr = sysfs_get_classdev_attr(class_dev, "dev");
113 printf("couldn't get the \"dev\" file\n");
117 printf("\ndevice '%s' has major:minor %s", class_dev->path, attr->value);
118 sysfs_close_attribute(attr);
120 /* open sysfs class device directory and print all attributes */
121 printf(" looking at class device '%s':\n", class_dev->path);
122 if (print_all_attributes(class_dev->path) != 0) {
123 printf("couldn't open class device directory\n");
128 /* get the device link (if parent exists look here) */
129 class_dev_parent = sysfs_get_classdev_parent(class_dev);
130 if (class_dev_parent != NULL) {
131 //sysfs_close_class_device(class_dev);
132 class_dev = class_dev_parent;
134 sysfs_dev = sysfs_get_classdev_device(class_dev);
135 if (sysfs_dev != NULL)
136 printf("follow the class device's \"device\"\n");
138 /* look the device chain upwards */
139 while (sysfs_dev != NULL) {
140 printf(" looking at the device chain at '%s':\n", sysfs_dev->path);
141 printf(" BUS=\"%s\"\n", sysfs_dev->bus);
142 printf(" ID=\"%s\"\n", sysfs_dev->bus_id);
144 /* open sysfs device directory and print all attributes */
145 print_all_attributes(sysfs_dev->path);
147 sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev);
148 if (sysfs_dev_parent == NULL)
151 //sysfs_close_device(sysfs_dev);
152 sysfs_dev = sysfs_dev_parent;
154 sysfs_close_device(sysfs_dev);
157 //sysfs_close_class_device(class_dev);