2 * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <sys/types.h>
34 static void print_all_attributes(const char *devpath, const char *key)
40 strlcpy(path, sysfs_path, sizeof(path));
41 strlcat(path, devpath, sizeof(path));
45 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
47 char filename[PATH_SIZE];
49 char value[NAME_SIZE];
52 if (dent->d_name[0] == '.')
55 if (strcmp(dent->d_name, "uevent") == 0)
58 strlcpy(filename, path, sizeof(filename));
59 strlcat(filename, "/", sizeof(filename));
60 strlcat(filename, dent->d_name, sizeof(filename));
61 if (lstat(filename, &statbuf) != 0)
63 if (S_ISLNK(statbuf.st_mode))
66 attr_value = sysfs_attr_get_value(devpath, dent->d_name);
67 if (attr_value == NULL)
69 len = strlcpy(value, attr_value, sizeof(value));
70 if(len >= sizeof(value))
71 len = sizeof(value) - 1;
72 dbg("attr '%s'='%s'(%zi)", dent->d_name, value, len);
74 /* remove trailing newlines */
75 while (len && value[len-1] == '\n')
78 /* skip nonprintable attributes */
79 while (len && isprint(value[len-1]))
82 dbg("attribute value of '%s' non-printable, skip", dent->d_name);
86 replace_chars(value, ALLOWED_CHARS_INPUT);
87 printf(" %s{%s}==\"%s\"\n", key, dent->d_name, value);
93 static int print_device_chain(const char *devpath)
95 struct sysfs_device *dev;
97 dev = sysfs_device_get(devpath);
102 "Udevinfo starts with the device specified by the devpath and then\n"
103 "walks up the chain of parent devices. It prints for every device\n"
104 "found, all possible attributes in the udev rules key format.\n"
105 "A rule to match, can be composed by the attributes of the device\n"
106 "and the attributes from one single parent device.\n"
109 printf(" looking at device '%s':\n", dev->devpath);
110 printf(" KERNEL==\"%s\"\n", dev->kernel);
111 printf(" SUBSYSTEM==\"%s\"\n", dev->subsystem);
112 printf(" DRIVER==\"%s\"\n", dev->driver);
113 print_all_attributes(dev->devpath, "ATTR");
115 /* walk up the chain of devices */
117 dev = sysfs_device_get_parent(dev);
120 printf(" looking at parent device '%s':\n", dev->devpath);
121 printf(" KERNELS==\"%s\"\n", dev->kernel);
122 printf(" SUBSYSTEMS==\"%s\"\n", dev->subsystem);
123 printf(" DRIVERS==\"%s\"\n", dev->driver);
125 print_all_attributes(dev->devpath, "ATTRS");
131 static void print_record(struct udevice *udev)
133 struct name_entry *name_loop;
135 printf("P: %s\n", udev->dev->devpath);
136 printf("N: %s\n", udev->name);
137 list_for_each_entry(name_loop, &udev->symlink_list, node)
138 printf("S: %s\n", name_loop->name);
139 if (udev->link_priority != 0)
140 printf("L: %i\n", udev->link_priority);
141 if (udev->partitions != 0)
142 printf("A:%u\n", udev->partitions);
143 if (udev->ignore_remove)
144 printf("R:%u\n", udev->ignore_remove);
145 list_for_each_entry(name_loop, &udev->env_list, node)
146 printf("E: %s\n", name_loop->name);
149 static void export_db(void) {
150 LIST_HEAD(name_list);
151 struct name_entry *name_loop;
153 udev_db_get_all_entries(&name_list);
154 list_for_each_entry(name_loop, &name_list, node) {
155 struct udevice *udev_db;
157 udev_db = udev_device_init(NULL);
160 if (udev_db_get_device(udev_db, name_loop->name) == 0)
161 print_record(udev_db);
163 udev_device_cleanup(udev_db);
165 name_list_cleanup(&name_list);
168 static int lookup_device_by_name(struct udevice *udev, const char *name)
170 LIST_HEAD(name_list);
172 struct name_entry *device;
175 count = udev_db_get_devices_by_name(name, &name_list);
179 info("found %i devices for '%s'", count, name);
181 /* select the device that seems to match */
182 list_for_each_entry(device, &name_list, node) {
183 char filename[PATH_SIZE];
186 udev_device_init(udev);
187 if (udev_db_get_device(udev, device->name) != 0)
189 info("found db entry '%s'", device->name);
191 /* make sure, we don't get a link of a differnt device */
192 strlcpy(filename, udev_root, sizeof(filename));
193 strlcat(filename, "/", sizeof(filename));
194 strlcat(filename, name, sizeof(filename));
195 if (stat(filename, &statbuf) != 0)
197 if (major(udev->devt) > 0 && udev->devt != statbuf.st_rdev) {
198 info("skip '%s', dev_t doesn't match", udev->name);
205 name_list_cleanup(&name_list);
209 static int stat_device(const char *name)
213 if (stat(name, &statbuf) != 0)
216 printf("%d %d\n", major(statbuf.st_dev), minor(statbuf.st_dev));
220 int udevinfo(int argc, char *argv[], char *envp[])
223 struct udevice *udev;
226 static const struct option options[] = {
227 { "name", 1, NULL, 'n' },
228 { "path", 1, NULL, 'p' },
229 { "query", 1, NULL, 'q' },
230 { "attribute-walk", 0, NULL, 'a' },
231 { "export-db", 0, NULL, 'e' },
232 { "root", 0, NULL, 'r' },
233 { "device-id-of-file", 1, NULL, 'd' },
234 { "version", 0, NULL, 1 }, /* -V outputs braindead format */
235 { "help", 0, NULL, 'h' },
242 ACTION_ATTRIBUTE_WALK,
244 ACTION_DEVICE_ID_FILE,
245 } action = ACTION_NONE;
254 } query = QUERY_NONE;
256 char path[PATH_SIZE] = "";
257 char name[PATH_SIZE] = "";
258 struct name_entry *name_loop;
261 logging_init("udevinfo");
265 udev = udev_device_init(NULL);
272 option = getopt_long(argc, argv, "aed:n:p:q:rVh", options, NULL);
276 dbg("option '%c'", option);
279 /* remove /dev if given */
280 if (strncmp(optarg, udev_root, strlen(udev_root)) == 0)
281 strlcpy(name, &optarg[strlen(udev_root)+1], sizeof(name));
283 strlcpy(name, optarg, sizeof(name));
284 dbg("name: %s", name);
287 /* remove /sys if given */
288 if (strncmp(optarg, sysfs_path, strlen(sysfs_path)) == 0)
289 strlcpy(path, &optarg[strlen(sysfs_path)], sizeof(path));
291 strlcpy(path, optarg, sizeof(path));
292 dbg("path: %s", path);
295 action = ACTION_QUERY;
296 if (strcmp(optarg, "name") == 0) {
300 if (strcmp(optarg, "symlink") == 0) {
301 query = QUERY_SYMLINK;
304 if (strcmp(optarg, "path") == 0) {
308 if (strcmp(optarg, "env") == 0) {
312 if (strcmp(optarg, "all") == 0) {
316 fprintf(stderr, "unknown query type\n");
320 if (action == ACTION_NONE)
321 action = ACTION_ROOT;
325 action = ACTION_DEVICE_ID_FILE;
326 strlcpy(name, optarg, sizeof(name));
329 action = ACTION_ATTRIBUTE_WALK;
335 printf("%s\n", UDEV_VERSION);
338 printf("udevinfo, version %s\n", UDEV_VERSION);
341 printf("Usage: udevadm info OPTIONS\n"
342 " --query=<type> query database for the specified value:\n"
343 " name name of device node\n"
344 " symlink pointing to node\n"
345 " path sysfs device path\n"
346 " env the device related imported environment\n"
348 " --path=<devpath> sysfs device path used for query or chain\n"
349 " --name=<name> node or symlink name used for query\n"
350 " --root prepend to query result or print udev_root\n"
351 " --attribute-walk print all key matches while walking along chain\n"
352 " of parent devices\n"
353 " --device-id-of-file=<file> print major/minor of underlying device\n"
354 " --export-db export the content of the udev database\n"
355 " --help print this text\n"
366 /* needs devpath or node/symlink name for query */
367 if (path[0] != '\0') {
368 if (udev_db_get_device(udev, path) != 0) {
369 fprintf(stderr, "no record for '%s' in database\n", path);
373 } else if (name[0] != '\0') {
374 if (lookup_device_by_name(udev, name) != 0) {
375 fprintf(stderr, "node name not found\n");
380 fprintf(stderr, "query needs --path or node --name specified\n");
388 printf("%s/%s\n", udev_root, udev->name);
390 printf("%s\n", udev->name);
393 list_for_each_entry(name_loop, &udev->symlink_list, node) {
394 char c = name_loop->node.next != &udev->symlink_list ? ' ' : '\n';
397 printf("%s/%s%c", udev_root, name_loop->name, c);
399 printf("%s%c", name_loop->name, c);
403 printf("%s\n", udev->dev->devpath);
406 list_for_each_entry(name_loop, &udev->env_list, node)
407 printf("%s\n", name_loop->name);
413 fprintf(stderr, "unknown query type\n");
417 case ACTION_ATTRIBUTE_WALK:
418 if (path[0] != '\0') {
419 if (print_device_chain(path) != 0) {
420 fprintf(stderr, "no valid sysfs device found\n");
424 } else if (name[0] != '\0') {
425 if (lookup_device_by_name(udev, name) != 0) {
426 fprintf(stderr, "node name not found\n");
430 if (print_device_chain(udev->dev->devpath) != 0) {
431 fprintf(stderr, "no valid sysfs device found\n");
436 fprintf(stderr, "attribute walk needs --path or node --name specified\n");
441 case ACTION_DEVICE_ID_FILE:
442 if (stat_device(name) != 0)
446 printf("%s\n", udev_root);
449 fprintf(stderr, "missing option\n");
455 udev_device_cleanup(udev);