]>
Commit | Line | Data |
---|---|---|
be9b51f6 KS |
1 | /* |
2 | * udevinfo - fetches attributes for a device | |
3 | * | |
4 | * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org> | |
5 | * | |
6 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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. | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <stdio.h> | |
034f35d7 | 25 | #include <ctype.h> |
be9b51f6 KS |
26 | |
27 | #include "libsysfs.h" | |
28 | ||
29 | ||
30 | # define VALUE_SIZE 200 | |
31 | ||
32 | char **main_argv; | |
33 | char **main_envp; | |
34 | ||
35 | static int print_all_attributes(char *path) | |
36 | { | |
37 | struct dlist *attributes; | |
38 | struct sysfs_attribute *attr; | |
39 | struct sysfs_directory *sysfs_dir; | |
40 | char value[VALUE_SIZE]; | |
41 | int len; | |
42 | int retval = 0; | |
43 | ||
44 | sysfs_dir = sysfs_open_directory(path); | |
45 | if (sysfs_dir == NULL) | |
46 | return -1; | |
47 | ||
48 | attributes = sysfs_get_dir_attributes(sysfs_dir); | |
49 | if (attributes == NULL) { | |
50 | retval = -1; | |
51 | goto exit; | |
52 | } | |
53 | ||
54 | dlist_for_each_data(attributes, attr, struct sysfs_attribute) { | |
55 | if (attr->value != NULL) { | |
56 | strncpy(value, attr->value, VALUE_SIZE); | |
57 | len = strlen(value); | |
034f35d7 KS |
58 | if (len == 0) |
59 | continue; | |
60 | ||
61 | /* remove trailing newline */ | |
62 | if (value[len-1] == '\n') { | |
be9b51f6 | 63 | value[len-1] = '\0'; |
034f35d7 KS |
64 | len--; |
65 | } | |
66 | ||
67 | /* skip nonprintable values */ | |
68 | while (len) { | |
69 | if (isprint(value[len-1]) == 0) | |
70 | break; | |
71 | len--; | |
72 | } | |
73 | if (len == 0) | |
74 | printf(" SYSFS_%s=\"%s\"\n", attr->name, value); | |
be9b51f6 KS |
75 | } |
76 | } | |
77 | printf("\n"); | |
78 | ||
79 | exit: | |
80 | sysfs_close_directory(sysfs_dir); | |
81 | ||
82 | return retval; | |
83 | } | |
84 | ||
85 | int main(int argc, char **argv, char **envp) | |
86 | { | |
87 | main_argv = argv; | |
88 | main_envp = envp; | |
89 | struct sysfs_class_device *class_dev; | |
90 | struct sysfs_class_device *class_dev_parent; | |
91 | struct sysfs_attribute *attr; | |
034f35d7 KS |
92 | struct sysfs_device *sysfs_dev; |
93 | struct sysfs_device *sysfs_dev_parent; | |
be9b51f6 KS |
94 | char *path; |
95 | int retval = 0; | |
96 | ||
97 | if (argc != 2) { | |
98 | printf("Usage: udevinfo <sysfs_device_path>\n"); | |
99 | return -1; | |
100 | } | |
101 | path = argv[1]; | |
102 | ||
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"); | |
107 | return -1; | |
108 | } | |
109 | ||
110 | /* read the 'dev' file for major/minor*/ | |
111 | attr = sysfs_get_classdev_attr(class_dev, "dev"); | |
112 | if (attr == NULL) { | |
113 | printf("couldn't get the \"dev\" file\n"); | |
114 | retval = -1; | |
115 | goto exit; | |
116 | } | |
034f35d7 | 117 | printf("\ndevice '%s' has major:minor %s", class_dev->path, attr->value); |
be9b51f6 KS |
118 | sysfs_close_attribute(attr); |
119 | ||
120 | /* open sysfs class device directory and print all attributes */ | |
034f35d7 | 121 | printf(" looking at class device '%s':\n", class_dev->path); |
be9b51f6 KS |
122 | if (print_all_attributes(class_dev->path) != 0) { |
123 | printf("couldn't open class device directory\n"); | |
124 | retval = -1; | |
125 | goto exit; | |
126 | } | |
127 | ||
034f35d7 | 128 | /* get the device link (if parent exists look here) */ |
be9b51f6 KS |
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; | |
133 | } | |
034f35d7 KS |
134 | sysfs_dev = sysfs_get_classdev_device(class_dev); |
135 | if (sysfs_dev != NULL) | |
136 | printf("follow the class device's \"device\"\n"); | |
137 | ||
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); | |
be9b51f6 KS |
143 | |
144 | /* open sysfs device directory and print all attributes */ | |
034f35d7 KS |
145 | print_all_attributes(sysfs_dev->path); |
146 | ||
147 | sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev); | |
148 | if (sysfs_dev_parent == NULL) | |
149 | break; | |
150 | ||
151 | //sysfs_close_device(sysfs_dev); | |
152 | sysfs_dev = sysfs_dev_parent; | |
be9b51f6 | 153 | } |
034f35d7 | 154 | sysfs_close_device(sysfs_dev); |
be9b51f6 KS |
155 | |
156 | exit: | |
157 | //sysfs_close_class_device(class_dev); | |
158 | return retval; | |
159 | } |