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_version.h"
38 # define SYSFS_VALUE_MAX 200
44 unsigned char logname[42];
45 void log_message (int level, const char *format, ...)
49 va_start(args, format);
50 vsyslog(level, format, args);
55 static int print_all_attributes(const char *path)
57 struct dlist *attributes;
58 struct sysfs_attribute *attr;
59 struct sysfs_directory *sysfs_dir;
60 char value[SYSFS_VALUE_MAX];
64 sysfs_dir = sysfs_open_directory(path);
65 if (sysfs_dir == NULL)
68 attributes = sysfs_get_dir_attributes(sysfs_dir);
69 if (attributes == NULL) {
74 dlist_for_each_data(attributes, attr, struct sysfs_attribute) {
75 if (attr->value != NULL) {
76 strfieldcpy(value, attr->value);
81 /* remove trailing newline */
82 if (value[len-1] == '\n') {
87 /* skip nonprintable values */
89 if (isprint(value[len-1]) == 0)
94 printf(" SYSFS{%s}=\"%s\"\n", attr->name, value);
100 sysfs_close_directory(sysfs_dir);
105 /* callback for database dump */
106 static int print_record(char *path, struct udevice *dev)
108 printf("P: %s\n", path);
109 printf("N: %s\n", dev->name);
110 printf("M: %#o\n", dev->mode);
111 printf("S: %s\n", dev->symlink);
112 printf("O: %s\n", dev->owner);
113 printf("G: %s\n", dev->group);
129 static int print_device_chain(const char *path)
131 struct sysfs_class_device *class_dev;
132 struct sysfs_class_device *class_dev_parent;
133 struct sysfs_attribute *attr;
134 struct sysfs_device *sysfs_dev;
135 struct sysfs_device *sysfs_dev_parent;
138 /* get the class dev */
139 class_dev = sysfs_open_class_device_path(path);
140 if (class_dev == NULL) {
141 printf("couldn't get the class device\n");
145 /* read the 'dev' file for major/minor*/
146 attr = sysfs_get_classdev_attr(class_dev, "dev");
148 printf("couldn't get the \"dev\" file\n");
152 printf("\ndevice '%s' has major:minor %s", class_dev->path, attr->value);
153 sysfs_close_attribute(attr);
155 /* open sysfs class device directory and print all attributes */
156 printf(" looking at class device '%s':\n", class_dev->path);
157 if (print_all_attributes(class_dev->path) != 0) {
158 printf("couldn't open class device directory\n");
163 /* get the device link (if parent exists look here) */
164 class_dev_parent = sysfs_get_classdev_parent(class_dev);
165 if (class_dev_parent != NULL) {
166 //sysfs_close_class_device(class_dev);
167 class_dev = class_dev_parent;
169 sysfs_dev = sysfs_get_classdev_device(class_dev);
170 if (sysfs_dev != NULL)
171 printf("follow the class device's \"device\"\n");
173 /* look the device chain upwards */
174 while (sysfs_dev != NULL) {
175 printf(" looking at the device chain at '%s':\n", sysfs_dev->path);
176 printf(" BUS=\"%s\"\n", sysfs_dev->bus);
177 printf(" ID=\"%s\"\n", sysfs_dev->bus_id);
179 /* open sysfs device directory and print all attributes */
180 print_all_attributes(sysfs_dev->path);
182 sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev);
183 if (sysfs_dev_parent == NULL)
186 //sysfs_close_device(sysfs_dev);
187 sysfs_dev = sysfs_dev_parent;
189 sysfs_close_device(sysfs_dev);
192 //sysfs_close_class_device(class_dev);
196 static int process_options(void)
198 static const char short_options[] = "adn:p:q:rVh";
204 enum query_type query = NONE;
205 char result[NAME_SIZE] = "";
206 char path[NAME_SIZE] = "";
207 char name[NAME_SIZE] = "";
208 char temp[NAME_SIZE];
211 /* get command line options */
213 option = getopt(main_argc, main_argv, short_options);
217 dbg("option '%c'", option);
220 dbg("udev name: %s\n", optarg);
221 strfieldcpy(name, optarg);
225 dbg("udev path: %s\n", optarg);
226 strfieldcpy(path, optarg);
230 dbg("udev query: %s\n", optarg);
232 if (strcmp(optarg, "name") == 0) {
237 if (strcmp(optarg, "symlink") == 0) {
242 if (strcmp(optarg, "mode") == 0) {
247 if (strcmp(optarg, "owner") == 0) {
252 if (strcmp(optarg, "group") == 0) {
257 if (strcmp(optarg, "path") == 0) {
262 if (strcmp(optarg, "all") == 0) {
267 printf("unknown query type\n");
279 retval = udevdb_open_ro();
281 printf("unable to open udev database\n");
284 udevdb_call_foreach(print_record);
289 printf("udevinfo, version %s\n", UDEV_VERSION);
300 /* process options */
302 retval = udevdb_open_ro();
304 printf("unable to open udev database\n");
308 if (path[0] != '\0') {
309 /* remove sysfs_path if given */
310 if (strncmp(path, sysfs_path, strlen(sysfs_path)) == 0) {
311 pos = path + strlen(sysfs_path);
313 if (path[0] != '/') {
314 /* prepend '/' if missing */
315 strfieldcat(temp, "/");
316 strfieldcat(temp, path);
322 retval = udevdb_get_dev(pos, &dev);
324 printf("device not found in database\n");
330 if (name[0] != '\0') {
331 /* remove udev_root if given */
332 if (strncmp(name, udev_root, strlen(udev_root)) == 0) {
333 pos = name + strlen(udev_root);
336 retval = udevdb_get_dev_byname(pos, path, &dev);
338 printf("device not found in database\n");
344 printf("query needs device path(-p) or node name(-n) specified\n");
351 strfieldcpy(result, udev_root);
352 strfieldcat(result, dev.name);
356 strfieldcpy(result, dev.symlink);
360 sprintf(result, "%#o", dev.mode);
364 strfieldcpy(result, dev.group);
368 strfieldcpy(result, dev.owner);
372 strfieldcpy(result, path);
376 print_record(path, &dev);
382 printf("%s\n", result);
390 if (path[0] == '\0') {
391 printf("attribute walk on device chain needs path(-p) specified\n");
394 if (strncmp(path, sysfs_path, strlen(sysfs_path)) != 0) {
395 /* prepend sysfs mountpoint if not given */
396 strfieldcpy(temp, path);
397 strfieldcpy(path, sysfs_path);
398 strfieldcat(path, temp);
400 print_device_chain(path);
406 printf("%s\n", udev_root);
411 printf("Usage: [-anpqrdVh]\n"
412 " -q TYPE query database for the specified value:\n"
413 " 'name' name of device node\n"
414 " 'symlink' pointing to node\n"
415 " 'mode' permissions of node\n"
418 " 'path' sysfs device path\n"
419 " 'all' all values\n"
421 " -p PATH sysfs device path used for query or chain\n"
422 " -n NAME node/symlink name used for query\n"
424 " -r print udev root\n"
425 " -a print all SYSFS_attributes along the device chain\n"
426 " -d dump whole database\n"
427 " -V print udev version\n"
428 " -h print this help text\n"
433 int main(int argc, char *argv[], char *envp[])
440 init_logging("udevinfo");
442 /* initialize our configuration */
445 retval = process_options();