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];
43 static int print_all_attributes(const char *path)
45 struct dlist *attributes;
46 struct sysfs_attribute *attr;
47 struct sysfs_directory *sysfs_dir;
48 char value[SYSFS_VALUE_MAX];
52 sysfs_dir = sysfs_open_directory(path);
53 if (sysfs_dir == NULL)
56 attributes = sysfs_get_dir_attributes(sysfs_dir);
57 if (attributes == NULL) {
62 dlist_for_each_data(attributes, attr, struct sysfs_attribute) {
63 if (attr->value != NULL) {
64 strncpy(value, attr->value, SYSFS_VALUE_MAX);
69 /* remove trailing newline */
70 if (value[len-1] == '\n') {
75 /* skip nonprintable values */
77 if (isprint(value[len-1]) == 0)
82 printf(" SYSFS_%s=\"%s\"\n", attr->name, value);
88 sysfs_close_directory(sysfs_dir);
93 /* callback for database dump */
94 static int print_record(char *path, struct udevice *dev)
96 printf("P: %s\n", path);
97 printf("N: %s\n", dev->name);
98 printf("M: %#o\n", dev->mode);
99 printf("S: %s\n", dev->symlink);
100 printf("O: %s\n", dev->owner);
101 printf("G: %s\n", dev->group);
116 static int print_device_chain(const char *path)
118 struct sysfs_class_device *class_dev;
119 struct sysfs_class_device *class_dev_parent;
120 struct sysfs_attribute *attr;
121 struct sysfs_device *sysfs_dev;
122 struct sysfs_device *sysfs_dev_parent;
125 /* get the class dev */
126 class_dev = sysfs_open_class_device_path(path);
127 if (class_dev == NULL) {
128 printf("couldn't get the class device\n");
132 /* read the 'dev' file for major/minor*/
133 attr = sysfs_get_classdev_attr(class_dev, "dev");
135 printf("couldn't get the \"dev\" file\n");
139 printf("\ndevice '%s' has major:minor %s", class_dev->path, attr->value);
140 sysfs_close_attribute(attr);
142 /* open sysfs class device directory and print all attributes */
143 printf(" looking at class device '%s':\n", class_dev->path);
144 if (print_all_attributes(class_dev->path) != 0) {
145 printf("couldn't open class device directory\n");
150 /* get the device link (if parent exists look here) */
151 class_dev_parent = sysfs_get_classdev_parent(class_dev);
152 if (class_dev_parent != NULL) {
153 //sysfs_close_class_device(class_dev);
154 class_dev = class_dev_parent;
156 sysfs_dev = sysfs_get_classdev_device(class_dev);
157 if (sysfs_dev != NULL)
158 printf("follow the class device's \"device\"\n");
160 /* look the device chain upwards */
161 while (sysfs_dev != NULL) {
162 printf(" looking at the device chain at '%s':\n", sysfs_dev->path);
163 printf(" BUS=\"%s\"\n", sysfs_dev->bus);
164 printf(" ID=\"%s\"\n", sysfs_dev->bus_id);
166 /* open sysfs device directory and print all attributes */
167 print_all_attributes(sysfs_dev->path);
169 sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev);
170 if (sysfs_dev_parent == NULL)
173 //sysfs_close_device(sysfs_dev);
174 sysfs_dev = sysfs_dev_parent;
176 sysfs_close_device(sysfs_dev);
179 //sysfs_close_class_device(class_dev);
183 static int process_options(void)
185 static const char short_options[] = "adn:p:q:rVh";
191 enum query_type query = NONE;
192 char result[NAME_SIZE] = "";
193 char path[NAME_SIZE] = "";
194 char name[NAME_SIZE] = "";
195 char temp[NAME_SIZE];
198 /* get command line options */
200 option = getopt(main_argc, main_argv, short_options);
204 dbg("option '%c'", option);
207 dbg("udev name: %s\n", optarg);
208 strfieldcpy(name, optarg);
212 dbg("udev path: %s\n", optarg);
213 strfieldcpy(path, optarg);
217 dbg("udev query: %s\n", optarg);
219 if (strcmp(optarg, "name") == 0) {
224 if (strcmp(optarg, "symlink") == 0) {
229 if (strcmp(optarg, "mode") == 0) {
234 if (strcmp(optarg, "owner") == 0) {
239 if (strcmp(optarg, "group") == 0) {
244 if (strcmp(optarg, "path") == 0) {
249 printf("unknown query type\n");
261 retval = udevdb_open_ro();
263 printf("unable to open udev database\n");
266 udevdb_call_foreach(print_record);
271 printf("udevinfo, version %s\n", UDEV_VERSION);
282 /* process options */
284 retval = udevdb_open_ro();
286 printf("unable to open udev database\n");
290 if (path[0] != '\0') {
291 /* remove sysfs_path if given */
292 if (strncmp(path, sysfs_path, strlen(sysfs_path)) == 0) {
293 pos = path + strlen(sysfs_path);
295 if (path[0] != '/') {
296 /* prepend '/' if missing */
298 strncat(temp, path, sizeof(path));
304 retval = udevdb_get_dev(pos, &dev);
306 printf("device not found in database\n");
312 if (name[0] != '\0') {
313 /* remove udev_root if given */
314 if (strncmp(name, udev_root, strlen(udev_root)) == 0) {
315 pos = name + strlen(udev_root);
318 retval = udevdb_get_dev_byname(pos, path, &dev);
320 printf("device not found in database\n");
326 printf("query needs device path(-p) or node name(-n) specified\n");
333 strfieldcpy(result, udev_root);
334 strncat(result, dev.name, sizeof(result));
338 strfieldcpy(result, dev.symlink);
342 sprintf(result, "%#o", dev.mode);
346 strfieldcpy(result, dev.group);
350 strfieldcpy(result, dev.owner);
354 strfieldcpy(result, path);
360 printf("%s\n", result);
368 if (path[0] == '\0') {
369 printf("attribute walk on device chain needs path(-p) specified\n");
372 if (strncmp(path, sysfs_path, strlen(sysfs_path)) != 0) {
373 /* prepend sysfs mountpoint if not given */
374 strfieldcpy(temp, path);
375 strfieldcpy(path, sysfs_path);
376 strncat(path, temp, sizeof(path));
378 print_device_chain(path);
384 printf("%s\n", udev_root);
389 printf("Usage: [-anpqrdVh]\n"
390 " -q TYPE query database for the specified value:\n"
391 " 'name' name of device node\n"
392 " 'symlink' pointing to node\n"
393 " 'mode' permissions of node\n"
396 " 'path' sysfs device path\n"
397 " -p PATH sysfs device path used for query or chain\n"
398 " -n NAME node name used for query\n"
400 " -r print udev root\n"
401 " -a print all SYSFS_attributes along the device chain\n"
402 " -d dump whole database\n"
403 " -V print udev version\n"
404 " -h print this help text\n"
409 int main(int argc, char *argv[], char *envp[])
416 init_logging("udevinfo");
418 /* initialize our configuration */
421 retval = process_options();