chiark / gitweb /
4f229cd37635dc409de2c2a311329e8fec3a513f
[elogind.git] / extras / udevinfo / udevinfo.c
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>
25
26 #include "libsysfs.h"
27
28
29 # define VALUE_SIZE 200
30
31 char **main_argv;
32 char **main_envp;
33
34 static int print_all_attributes(char *path)
35 {
36         struct dlist *attributes;
37         struct sysfs_attribute *attr;
38         struct sysfs_directory *sysfs_dir;
39         char value[VALUE_SIZE];
40         int len;
41         int retval = 0;
42
43         sysfs_dir = sysfs_open_directory(path);
44         if (sysfs_dir == NULL)
45                 return -1;
46
47         attributes = sysfs_get_dir_attributes(sysfs_dir);
48         if (attributes == NULL) {
49                 retval = -1;
50                 goto exit;
51         }
52
53         dlist_for_each_data(attributes, attr, struct sysfs_attribute) {
54                 if (attr->value != NULL) {
55                         strncpy(value, attr->value, VALUE_SIZE);
56                         len = strlen(value);
57                         if (value[len-1] == '\n')
58                                 value[len-1] = '\0';
59                         printf("  SYSFS_%s=\"%s\"\n", attr->name, value);
60                 }
61         }
62         printf("\n");
63
64 exit:
65         sysfs_close_directory(sysfs_dir);
66
67         return retval;
68 }
69
70 int main(int argc, char **argv, char **envp)
71 {
72         main_argv = argv;
73         main_envp = envp;
74         struct sysfs_class_device *class_dev;
75         struct sysfs_class_device *class_dev_parent;
76         struct sysfs_attribute *attr;
77         struct sysfs_device *sysfs_device;
78         char *path;
79         int retval = 0;
80
81         if (argc != 2) {
82                 printf("Usage: udevinfo <sysfs_device_path>\n");
83                 return -1;
84         }
85         path = argv[1];
86
87         /*  get the class dev */
88         class_dev = sysfs_open_class_device_path(path);
89         if (class_dev == NULL) {
90                 printf("couldn't get the class device\n");
91                 return -1;
92         }
93
94         /* read the 'dev' file for major/minor*/
95         attr = sysfs_get_classdev_attr(class_dev, "dev");
96         if (attr == NULL) {
97                 printf("couldn't get the \"dev\" file\n");
98                 retval = -1;
99                 goto exit;
100         }
101         printf("\ndevice '%s' has major:minor %s\n", class_dev->path, attr->value);
102         sysfs_close_attribute(attr);
103
104         /* open sysfs class device directory and print all attributes */
105         printf("looking at class device '%s':\n", class_dev->path);
106         if (print_all_attributes(class_dev->path) != 0) {
107                 printf("couldn't open class device directory\n");
108                 retval = -1;
109                 goto exit;
110         }
111
112         /* get the device (if parent exists use it instead) */
113         class_dev_parent = sysfs_get_classdev_parent(class_dev);
114         if (class_dev_parent != NULL) {
115                 //sysfs_close_class_device(class_dev);
116                 class_dev = class_dev_parent;
117         }
118         sysfs_device = sysfs_get_classdev_device(class_dev);
119         if (sysfs_device != NULL) {
120                 printf("follow class device's \"device\" link '%s':\n", class_dev->path);
121                 printf("  BUS=\"%s\"\n", sysfs_device->bus);
122                 printf("  ID=\"%s\"\n", sysfs_device->bus_id);
123
124                 /* open sysfs device directory and print all attributes */
125                 print_all_attributes(sysfs_device->path);
126                 sysfs_close_device(sysfs_device);
127         }
128
129 exit:
130         //sysfs_close_class_device(class_dev);
131         return retval;
132 }