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
43 unsigned char logname[42];
44 void log_message (int level, const char *format, ...)
48 va_start(args, format);
49 vsyslog(level, format, args);
54 static int print_all_attributes(const char *path)
56 struct dlist *attributes;
57 struct sysfs_attribute *attr;
58 struct sysfs_directory *sysfs_dir;
59 char value[SYSFS_VALUE_MAX];
63 sysfs_dir = sysfs_open_directory(path);
64 if (sysfs_dir == NULL)
67 attributes = sysfs_get_dir_attributes(sysfs_dir);
68 if (attributes == NULL) {
73 dlist_for_each_data(attributes, attr, struct sysfs_attribute) {
74 if (attr->value != NULL) {
75 strncpy(value, attr->value, SYSFS_VALUE_MAX);
80 /* remove trailing newline */
81 if (value[len-1] == '\n') {
86 /* skip nonprintable values */
88 if (isprint(value[len-1]) == 0)
93 printf(" SYSFS_%s=\"%s\"\n", attr->name, value);
99 sysfs_close_directory(sysfs_dir);
104 /* callback for database dump */
105 static int print_record(char *path, struct udevice *dev)
107 printf("P: %s\n", path);
108 printf("N: %s\n", dev->name);
109 printf("M: %#o\n", dev->mode);
110 printf("S: %s\n", dev->symlink);
111 printf("O: %s\n", dev->owner);
112 printf("G: %s\n", dev->group);
127 static int print_device_chain(const char *path)
129 struct sysfs_class_device *class_dev;
130 struct sysfs_class_device *class_dev_parent;
131 struct sysfs_attribute *attr;
132 struct sysfs_device *sysfs_dev;
133 struct sysfs_device *sysfs_dev_parent;
136 /* get the class dev */
137 class_dev = sysfs_open_class_device_path(path);
138 if (class_dev == NULL) {
139 printf("couldn't get the class device\n");
143 /* read the 'dev' file for major/minor*/
144 attr = sysfs_get_classdev_attr(class_dev, "dev");
146 printf("couldn't get the \"dev\" file\n");
150 printf("\ndevice '%s' has major:minor %s", class_dev->path, attr->value);
151 sysfs_close_attribute(attr);
153 /* open sysfs class device directory and print all attributes */
154 printf(" looking at class device '%s':\n", class_dev->path);
155 if (print_all_attributes(class_dev->path) != 0) {
156 printf("couldn't open class device directory\n");
161 /* get the device link (if parent exists look here) */
162 class_dev_parent = sysfs_get_classdev_parent(class_dev);
163 if (class_dev_parent != NULL) {
164 //sysfs_close_class_device(class_dev);
165 class_dev = class_dev_parent;
167 sysfs_dev = sysfs_get_classdev_device(class_dev);
168 if (sysfs_dev != NULL)
169 printf("follow the class device's \"device\"\n");
171 /* look the device chain upwards */
172 while (sysfs_dev != NULL) {
173 printf(" looking at the device chain at '%s':\n", sysfs_dev->path);
174 printf(" BUS=\"%s\"\n", sysfs_dev->bus);
175 printf(" ID=\"%s\"\n", sysfs_dev->bus_id);
177 /* open sysfs device directory and print all attributes */
178 print_all_attributes(sysfs_dev->path);
180 sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev);
181 if (sysfs_dev_parent == NULL)
184 //sysfs_close_device(sysfs_dev);
185 sysfs_dev = sysfs_dev_parent;
187 sysfs_close_device(sysfs_dev);
190 //sysfs_close_class_device(class_dev);
194 static int process_options(void)
196 static const char short_options[] = "adn:p:q:rVh";
202 enum query_type query = NONE;
203 char result[NAME_SIZE] = "";
204 char path[NAME_SIZE] = "";
205 char name[NAME_SIZE] = "";
206 char temp[NAME_SIZE];
209 /* get command line options */
211 option = getopt(main_argc, main_argv, short_options);
215 dbg("option '%c'", option);
218 dbg("udev name: %s\n", optarg);
219 strfieldcpy(name, optarg);
223 dbg("udev path: %s\n", optarg);
224 strfieldcpy(path, optarg);
228 dbg("udev query: %s\n", optarg);
230 if (strcmp(optarg, "name") == 0) {
235 if (strcmp(optarg, "symlink") == 0) {
240 if (strcmp(optarg, "mode") == 0) {
245 if (strcmp(optarg, "owner") == 0) {
250 if (strcmp(optarg, "group") == 0) {
255 if (strcmp(optarg, "path") == 0) {
260 printf("unknown query type\n");
272 retval = udevdb_open_ro();
274 printf("unable to open udev database\n");
277 udevdb_call_foreach(print_record);
282 printf("udevinfo, version %s\n", UDEV_VERSION);
293 /* process options */
295 retval = udevdb_open_ro();
297 printf("unable to open udev database\n");
301 if (path[0] != '\0') {
302 /* remove sysfs_path if given */
303 if (strncmp(path, sysfs_path, strlen(sysfs_path)) == 0) {
304 pos = path + strlen(sysfs_path);
306 if (path[0] != '/') {
307 /* prepend '/' if missing */
309 strncat(temp, path, sizeof(path));
315 retval = udevdb_get_dev(pos, &dev);
317 printf("device not found in database\n");
323 if (name[0] != '\0') {
324 /* remove udev_root if given */
325 if (strncmp(name, udev_root, strlen(udev_root)) == 0) {
326 pos = name + strlen(udev_root);
329 retval = udevdb_get_dev_byname(pos, path, &dev);
331 printf("device not found in database\n");
337 printf("query needs device path(-p) or node name(-n) specified\n");
344 strfieldcpy(result, udev_root);
345 strncat(result, dev.name, sizeof(result));
349 strfieldcpy(result, dev.symlink);
353 sprintf(result, "%#o", dev.mode);
357 strfieldcpy(result, dev.group);
361 strfieldcpy(result, dev.owner);
365 strfieldcpy(result, path);
371 printf("%s\n", result);
379 if (path[0] == '\0') {
380 printf("attribute walk on device chain needs path(-p) specified\n");
383 if (strncmp(path, sysfs_path, strlen(sysfs_path)) != 0) {
384 /* prepend sysfs mountpoint if not given */
385 strfieldcpy(temp, path);
386 strfieldcpy(path, sysfs_path);
387 strncat(path, temp, sizeof(path));
389 print_device_chain(path);
395 printf("%s\n", udev_root);
400 printf("Usage: [-anpqrdVh]\n"
401 " -q TYPE query database for the specified value:\n"
402 " 'name' name of device node\n"
403 " 'symlink' pointing to node\n"
404 " 'mode' permissions of node\n"
407 " 'path' sysfs device path\n"
408 " -p PATH sysfs device path used for query or chain\n"
409 " -n NAME node name used for query\n"
411 " -r print udev root\n"
412 " -a print all SYSFS_attributes along the device chain\n"
413 " -d dump whole database\n"
414 " -V print udev version\n"
415 " -h print this help text\n"
420 int main(int argc, char *argv[], char *envp[])
427 init_logging("udevinfo");
429 /* initialize our configuration */
432 retval = process_options();