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 #include "libsysfs/sysfs/libsysfs.h"
31 #include "libsysfs/dlist.h"
33 #include "udev_utils.h"
34 #include "udev_version.h"
39 #define SYSFS_VALUE_SIZE 256
42 void log_message (int level, const char *format, ...)
46 va_start(args, format);
47 vsyslog(level, format, args);
52 static void print_all_attributes(struct dlist *attr_list)
54 struct sysfs_attribute *attr;
55 char value[SYSFS_VALUE_SIZE];
58 dlist_for_each_data(attr_list, attr, struct sysfs_attribute) {
59 if (attr->value != NULL) {
60 strfieldcpy(value, attr->value);
65 /* remove trailing newline */
66 if (value[len-1] == '\n') {
71 /* skip nonprintable values */
73 if (isprint(value[len-1]) == 0)
78 printf(" SYSFS{%s}=\"%s\"\n", attr->name, value);
84 static int print_record(struct udevice *udev)
86 printf("P: %s\n", udev->devpath);
87 printf("N: %s\n", udev->name);
88 printf("S: %s\n", udev->symlink);
101 static int print_device_chain(const char *path)
103 struct sysfs_class_device *class_dev;
104 struct sysfs_class_device *class_dev_parent;
105 struct sysfs_attribute *attr;
106 struct sysfs_device *sysfs_dev;
107 struct sysfs_device *sysfs_dev_parent;
108 struct dlist *attr_list;
111 /* get the class dev */
112 class_dev = sysfs_open_class_device_path(path);
113 if (class_dev == NULL) {
114 printf("couldn't get the class device\n");
118 printf("\nudevinfo starts with the device the node belongs to and then walks up the\n"
119 "device chain, to print for every device found, all possibly useful attributes\n"
120 "in the udev key format.\n"
121 "Only attributes within one device section may be used together in one rule,\n"
122 "to match the device for which the node will be created.\n"
125 /* look for the 'dev' file */
126 attr = sysfs_get_classdev_attr(class_dev, "dev");
128 printf("device '%s' has major:minor %s", class_dev->path, attr->value);
130 /* open sysfs class device directory and print all attributes */
131 printf(" looking at class device '%s':\n", class_dev->path);
132 printf(" SUBSYSTEM=\"%s\"\n", class_dev->classname);
134 attr_list = sysfs_get_classdev_attributes(class_dev);
135 if (attr_list == NULL) {
136 printf("couldn't open class device directory\n");
140 print_all_attributes(attr_list);
142 /* get the device link (if parent exists look here) */
143 class_dev_parent = sysfs_get_classdev_parent(class_dev);
144 if (class_dev_parent != NULL)
145 sysfs_dev = sysfs_get_classdev_device(class_dev_parent);
147 sysfs_dev = sysfs_get_classdev_device(class_dev);
149 if (sysfs_dev != NULL)
150 printf("follow the class device's \"device\"\n");
152 /* look the device chain upwards */
153 while (sysfs_dev != NULL) {
154 attr_list = sysfs_get_device_attributes(sysfs_dev);
155 if (attr_list == NULL) {
156 printf("couldn't open device directory\n");
161 printf(" looking at the device chain at '%s':\n", sysfs_dev->path);
162 printf(" BUS=\"%s\"\n", sysfs_dev->bus);
163 printf(" ID=\"%s\"\n", sysfs_dev->bus_id);
164 printf(" DRIVER=\"%s\"\n", sysfs_dev->driver_name);
166 /* open sysfs device directory and print all attributes */
167 print_all_attributes(attr_list);
169 sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev);
170 if (sysfs_dev_parent == NULL)
173 sysfs_dev = sysfs_dev_parent;
177 sysfs_close_class_device(class_dev);
181 static int process_options(int argc, char *argv[])
183 static const char short_options[] = "an:p:q:rVh";
189 enum query_type query = NONE;
190 char result[1024] = "";
191 char path[NAME_SIZE] = "";
192 char name[NAME_SIZE] = "";
193 char temp[NAME_SIZE];
196 /* get command line options */
198 option = getopt(argc, argv, short_options);
202 dbg("option '%c'", option);
205 dbg("udev name: %s\n", optarg);
206 strfieldcpy(name, optarg);
210 dbg("udev path: %s\n", optarg);
211 strfieldcpy(path, optarg);
215 dbg("udev query: %s\n", optarg);
217 if (strcmp(optarg, "name") == 0) {
222 if (strcmp(optarg, "symlink") == 0) {
227 if (strcmp(optarg, "path") == 0) {
232 if (strcmp(optarg, "all") == 0) {
237 printf("unknown query type\n");
249 printf("udevinfo, version %s\n", UDEV_VERSION);
260 /* process options */
262 if (path[0] != '\0') {
263 /* remove sysfs_path if given */
264 if (strncmp(path, sysfs_path, strlen(sysfs_path)) == 0) {
265 pos = path + strlen(sysfs_path);
267 if (path[0] != '/') {
268 /* prepend '/' if missing */
269 strfieldcat(temp, "/");
270 strfieldcat(temp, path);
276 memset(&udev, 0x00, sizeof(struct udevice));
277 retval = udev_db_get_device_by_devpath(&udev, pos);
279 printf("device not found in database\n");
285 if (name[0] != '\0') {
286 /* remove udev_root if given */
287 int len = strlen(udev_root);
289 if (strncmp(name, udev_root, len) == 0) {
294 memset(&udev, 0x00, sizeof(struct udevice));
295 strfieldcpy(udev.name, pos);
296 retval = udev_db_get_device_byname(&udev, pos);
298 printf("device not found in database\n");
305 printf("query needs device path(-p) or node name(-n) specified\n");
312 snprintf(result, NAME_SIZE-1, "%s/%s", udev_root, udev.name);
313 result[NAME_SIZE-1] = '\0';
315 strfieldcpy(result, udev.name);
323 char slink[NAME_SIZE];
326 foreach_strpart(udev.symlink, " \n\r", spos, slen) {
327 strncpy(slink, spos, slen);
329 pos += sprintf(pos, "%s/%s ", udev_root, slink);
332 strfieldcpy(result, udev.symlink);
337 strfieldcpy(result, udev.devpath);
347 printf("%s\n", result);
354 if (path[0] == '\0') {
355 printf("attribute walk on device chain needs path(-p) specified\n");
358 if (strncmp(path, sysfs_path, strlen(sysfs_path)) != 0) {
359 /* prepend sysfs mountpoint if not given */
360 strfieldcpy(temp, path);
361 strfieldcpy(path, sysfs_path);
362 strfieldcat(path, temp);
364 print_device_chain(path);
370 printf("%s\n", udev_root);
375 printf("Usage: udevinfo [-anpqrVh]\n"
376 " -q TYPE query database for the specified value:\n"
377 " 'name' name of device node\n"
378 " 'symlink' pointing to node\n"
379 " 'path' sysfs device path\n"
380 " 'all' all values\n"
382 " -p PATH sysfs device path used for query or chain\n"
383 " -n NAME node/symlink name used for query\n"
385 " -r print udev root\n"
386 " -a print all SYSFS_attributes along the device chain\n"
387 " -s print all sysfs devices with major/minor, physical device and bus\n"
388 " -V print udev version\n"
389 " -h print this help text\n"
394 int main(int argc, char *argv[], char *envp[])
398 logging_init("udevinfo");
400 /* initialize our configuration */
403 rc = process_options(argc, argv);