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.
31 #include "udev_version.h"
34 #include "libsysfs/libsysfs.h"
37 # define SYSFS_VALUE_MAX 200
41 unsigned char logname[42];
48 static int print_all_attributes(const char *path)
50 struct dlist *attributes;
51 struct sysfs_attribute *attr;
52 struct sysfs_directory *sysfs_dir;
53 char value[SYSFS_VALUE_MAX];
57 sysfs_dir = sysfs_open_directory(path);
58 if (sysfs_dir == NULL)
61 attributes = sysfs_get_dir_attributes(sysfs_dir);
62 if (attributes == NULL) {
67 dlist_for_each_data(attributes, attr, struct sysfs_attribute) {
68 if (attr->value != NULL) {
69 strncpy(value, attr->value, SYSFS_VALUE_MAX);
74 /* remove trailing newline */
75 if (value[len-1] == '\n') {
80 /* skip nonprintable values */
82 if (isprint(value[len-1]) == 0)
87 printf(" SYSFS_%s=\"%s\"\n", attr->name, value);
93 sysfs_close_directory(sysfs_dir);
98 /* callback for database dump */
99 static int print_record(char *path, struct udevice *dev)
101 printf("P: %s\n", path);
102 printf("N: %s\n", dev->name);
103 printf("M: %#o\n", dev->mode);
104 printf("S: %s\n", dev->symlink);
105 printf("O: %s\n", dev->owner);
106 printf("G: %s\n", dev->group);
121 static int print_device_chain(const char *path)
123 struct sysfs_class_device *class_dev;
124 struct sysfs_class_device *class_dev_parent;
125 struct sysfs_attribute *attr;
126 struct sysfs_device *sysfs_dev;
127 struct sysfs_device *sysfs_dev_parent;
130 /* get the class dev */
131 class_dev = sysfs_open_class_device_path(path);
132 if (class_dev == NULL) {
133 printf("couldn't get the class device\n");
137 /* read the 'dev' file for major/minor*/
138 attr = sysfs_get_classdev_attr(class_dev, "dev");
140 printf("couldn't get the \"dev\" file\n");
144 printf("\ndevice '%s' has major:minor %s", class_dev->path, attr->value);
145 sysfs_close_attribute(attr);
147 /* open sysfs class device directory and print all attributes */
148 printf(" looking at class device '%s':\n", class_dev->path);
149 if (print_all_attributes(class_dev->path) != 0) {
150 printf("couldn't open class device directory\n");
155 /* get the device link (if parent exists look here) */
156 class_dev_parent = sysfs_get_classdev_parent(class_dev);
157 if (class_dev_parent != NULL) {
158 //sysfs_close_class_device(class_dev);
159 class_dev = class_dev_parent;
161 sysfs_dev = sysfs_get_classdev_device(class_dev);
162 if (sysfs_dev != NULL)
163 printf("follow the class device's \"device\"\n");
165 /* look the device chain upwards */
166 while (sysfs_dev != NULL) {
167 printf(" looking at the device chain at '%s':\n", sysfs_dev->path);
168 printf(" BUS=\"%s\"\n", sysfs_dev->bus);
169 printf(" ID=\"%s\"\n", sysfs_dev->bus_id);
171 /* open sysfs device directory and print all attributes */
172 print_all_attributes(sysfs_dev->path);
174 sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev);
175 if (sysfs_dev_parent == NULL)
178 //sysfs_close_device(sysfs_dev);
179 sysfs_dev = sysfs_dev_parent;
181 sysfs_close_device(sysfs_dev);
184 //sysfs_close_class_device(class_dev);
188 static int process_options(void)
190 static const char short_options[] = "adn:p:q:rVh";
196 enum query_type query = NONE;
197 char result[NAME_SIZE] = "";
198 char path[NAME_SIZE] = "";
199 char name[NAME_SIZE] = "";
200 char temp[NAME_SIZE];
203 /* get command line options */
205 option = getopt(main_argc, main_argv, short_options);
209 dbg("option '%c'", option);
212 dbg("udev name: %s\n", optarg);
213 strfieldcpy(name, optarg);
217 dbg("udev path: %s\n", optarg);
218 strfieldcpy(path, optarg);
222 dbg("udev query: %s\n", optarg);
224 if (strcmp(optarg, "name") == 0) {
229 if (strcmp(optarg, "symlink") == 0) {
234 if (strcmp(optarg, "mode") == 0) {
239 if (strcmp(optarg, "owner") == 0) {
244 if (strcmp(optarg, "group") == 0) {
249 if (strcmp(optarg, "path") == 0) {
254 printf("unknown query type\n");
266 retval = udevdb_open_ro();
268 printf("unable to open udev database\n");
271 udevdb_call_foreach(print_record);
276 printf("udevinfo, version %s\n", UDEV_VERSION);
287 /* process options */
289 retval = udevdb_open_ro();
291 printf("unable to open udev database\n");
295 if (path[0] != '\0') {
296 /* remove sysfs_path if given */
297 if (strncmp(path, sysfs_path, strlen(sysfs_path)) == 0) {
298 pos = path + strlen(sysfs_path);
300 if (path[0] != '/') {
301 /* prepend '/' if missing */
303 strncat(temp, path, sizeof(path));
309 retval = udevdb_get_dev(pos, &dev);
311 printf("device not found in database\n");
317 if (name[0] != '\0') {
318 /* remove udev_root if given */
319 if (strncmp(name, udev_root, strlen(udev_root)) == 0) {
320 pos = name + strlen(udev_root);
323 retval = udevdb_get_dev_byname(pos, path, &dev);
325 printf("device not found in database\n");
331 printf("query needs device path(-p) or node name(-n) specified\n");
338 strfieldcpy(result, udev_root);
339 strncat(result, dev.name, sizeof(result));
343 strfieldcpy(result, dev.symlink);
347 sprintf(result, "%#o", dev.mode);
351 strfieldcpy(result, dev.group);
355 strfieldcpy(result, dev.owner);
359 strfieldcpy(result, path);
365 printf("%s\n", result);
373 if (path[0] == '\0') {
374 printf("attribute walk on device chain needs path(-p) specified\n");
377 if (strncmp(path, sysfs_path, strlen(sysfs_path)) != 0) {
378 /* prepend sysfs mountpoint if not given */
379 strfieldcpy(temp, path);
380 strfieldcpy(path, sysfs_path);
381 strncat(path, temp, sizeof(path));
383 print_device_chain(path);
389 printf("%s\n", udev_root);
394 printf("Usage: [-anpqrdVh]\n"
395 " -q TYPE query database for the specified value:\n"
396 " 'name' name of device node\n"
397 " 'symlink' pointing to node\n"
398 " 'mode' permissions of node\n"
401 " 'path' sysfs device path\n"
402 " -p PATH sysfs device path used for query or chain\n"
403 " -n NAME node name used for query\n"
405 " -r print udev root\n"
406 " -a print all SYSFS_attributes along the device chain\n"
407 " -d dump whole database\n"
408 " -V print udev version\n"
409 " -h print this help text\n"
414 int main(int argc, char *argv[], char *envp[])
421 init_logging("udevinfo");
423 /* initialize our configuration */
426 retval = process_options();