2 * Copyright (C) 2004-2009 Kay Sievers <kay.sievers@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
34 static bool skip_attribute(const char *name)
36 static const char const *skip[] = {
47 for (i = 0; i < ARRAY_SIZE(skip); i++)
48 if (strcmp(name, skip[i]) == 0)
53 static void print_all_attributes(struct udev_device *device, const char *key)
55 struct udev *udev = udev_device_get_udev(device);
56 struct udev_list_entry *sysattr;
58 udev_list_entry_foreach(sysattr, udev_device_get_sysattr_list_entry(device)) {
63 name = udev_list_entry_get_name(sysattr);
64 if (skip_attribute(name))
67 value = udev_device_get_sysattr_value(device, name);
70 dbg(udev, "attr '%s'='%s'\n", name, value);
72 /* skip any values that look like a path */
76 /* skip nonprintable attributes */
78 while (len > 0 && isprint(value[len-1]))
81 dbg(udev, "attribute value of '%s' non-printable, skip\n", name);
85 printf(" %s{%s}==\"%s\"\n", key, name, value);
90 static int print_device_chain(struct udev_device *device)
92 struct udev_device *device_parent;
96 "Udevadm info starts with the device specified by the devpath and then\n"
97 "walks up the chain of parent devices. It prints for every device\n"
98 "found, all possible attributes in the udev rules key format.\n"
99 "A rule to match, can be composed by the attributes of the device\n"
100 "and the attributes from one single parent device.\n"
103 printf(" looking at device '%s':\n", udev_device_get_devpath(device));
104 printf(" KERNEL==\"%s\"\n", udev_device_get_sysname(device));
105 str = udev_device_get_subsystem(device);
108 printf(" SUBSYSTEM==\"%s\"\n", str);
109 str = udev_device_get_driver(device);
112 printf(" DRIVER==\"%s\"\n", str);
113 print_all_attributes(device, "ATTR");
115 device_parent = device;
117 device_parent = udev_device_get_parent(device_parent);
118 if (device_parent == NULL)
120 printf(" looking at parent device '%s':\n", udev_device_get_devpath(device_parent));
121 printf(" KERNELS==\"%s\"\n", udev_device_get_sysname(device_parent));
122 str = udev_device_get_subsystem(device_parent);
125 printf(" SUBSYSTEMS==\"%s\"\n", str);
126 str = udev_device_get_driver(device_parent);
129 printf(" DRIVERS==\"%s\"\n", str);
130 print_all_attributes(device_parent, "ATTRS");
131 } while (device_parent != NULL);
136 static void print_record(struct udev_device *device)
141 struct udev_list_entry *list_entry;
143 printf("P: %s\n", udev_device_get_devpath(device));
145 len = strlen(udev_get_dev_path(udev_device_get_udev(device)));
146 str = udev_device_get_devnode(device);
148 printf("N: %s\n", &str[len+1]);
150 i = udev_device_get_devlink_priority(device);
152 printf("L: %i\n", i);
154 udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) {
155 len = strlen(udev_get_dev_path(udev_device_get_udev(device)));
156 printf("S: %s\n", &udev_list_entry_get_name(list_entry)[len+1]);
159 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
161 udev_list_entry_get_name(list_entry),
162 udev_list_entry_get_value(list_entry));
166 static int stat_device(const char *name, bool export, const char *prefix)
170 if (stat(name, &statbuf) != 0)
176 printf("%sMAJOR=%d\n"
178 prefix, major(statbuf.st_dev),
179 prefix, minor(statbuf.st_dev));
181 printf("%d:%d\n", major(statbuf.st_dev), minor(statbuf.st_dev));
185 static int export_devices(struct udev *udev)
187 struct udev_enumerate *udev_enumerate;
188 struct udev_list_entry *list_entry;
190 udev_enumerate = udev_enumerate_new(udev);
191 if (udev_enumerate == NULL)
193 udev_enumerate_scan_devices(udev_enumerate);
194 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
195 struct udev_device *device;
197 device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
198 if (device != NULL) {
199 print_record(device);
200 udev_device_unref(device);
203 udev_enumerate_unref(udev_enumerate);
207 static void cleanup_dir(DIR *dir, mode_t mask, int depth)
214 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
217 if (dent->d_name[0] == '.')
219 if (fstatat(dirfd(dir), dent->d_name, &stats, AT_SYMLINK_NOFOLLOW) != 0)
221 if ((stats.st_mode & mask) != 0)
223 if (S_ISDIR(stats.st_mode)) {
226 dir2 = fdopendir(openat(dirfd(dir), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
228 cleanup_dir(dir2, mask, depth-1);
231 unlinkat(dirfd(dir), dent->d_name, AT_REMOVEDIR);
233 unlinkat(dirfd(dir), dent->d_name, 0);
238 static void cleanup_db(struct udev *udev)
240 char filename[UTIL_PATH_SIZE];
243 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/queue.bin", NULL);
246 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/data", NULL);
247 dir = opendir(filename);
249 cleanup_dir(dir, S_ISVTX, 1);
253 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/links", NULL);
254 dir = opendir(filename);
256 cleanup_dir(dir, 0, 2);
260 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/tags", NULL);
261 dir = opendir(filename);
263 cleanup_dir(dir, 0, 2);
267 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/watch", NULL);
268 dir = opendir(filename);
270 cleanup_dir(dir, 0, 1);
274 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/firmware-missing", NULL);
275 dir = opendir(filename);
277 cleanup_dir(dir, 0, 1);
282 static int uinfo(struct udev *udev, int argc, char *argv[])
284 struct udev_device *device = NULL;
287 const char *export_prefix = NULL;
288 char path[UTIL_PATH_SIZE];
289 char name[UTIL_PATH_SIZE];
290 struct udev_list_entry *list_entry;
293 static const struct option options[] = {
294 { "name", required_argument, NULL, 'n' },
295 { "path", required_argument, NULL, 'p' },
296 { "query", required_argument, NULL, 'q' },
297 { "attribute-walk", no_argument, NULL, 'a' },
298 { "cleanup-db", no_argument, NULL, 'c' },
299 { "export-db", no_argument, NULL, 'e' },
300 { "root", no_argument, NULL, 'r' },
301 { "run", no_argument, NULL, 'R' },
302 { "device-id-of-file", required_argument, NULL, 'd' },
303 { "export", no_argument, NULL, 'x' },
304 { "export-prefix", required_argument, NULL, 'P' },
305 { "version", no_argument, NULL, 'V' },
306 { "help", no_argument, NULL, 'h' },
313 ACTION_ATTRIBUTE_WALK,
315 ACTION_DEVICE_ID_FILE,
316 } action = ACTION_NONE;
325 } query = QUERY_NONE;
331 option = getopt_long(argc, argv, "aced:n:p:q:rxP:RVh", options, NULL);
335 dbg(udev, "option '%c'\n", option);
338 if (device != NULL) {
339 fprintf(stderr, "device already specified\n");
343 /* remove /dev if given */
344 if (strncmp(optarg, udev_get_dev_path(udev), strlen(udev_get_dev_path(udev))) != 0)
345 util_strscpyl(name, sizeof(name), udev_get_dev_path(udev), "/", optarg, NULL);
347 util_strscpy(name, sizeof(name), optarg);
348 util_remove_trailing_chars(name, '/');
349 if (stat(name, &statbuf) < 0) {
350 fprintf(stderr, "device node not found\n");
356 if (S_ISBLK(statbuf.st_mode)) {
358 } else if (S_ISCHR(statbuf.st_mode)) {
361 fprintf(stderr, "device node has wrong file type\n");
365 device = udev_device_new_from_devnum(udev, type, statbuf.st_rdev);
366 if (device == NULL) {
367 fprintf(stderr, "device node not found\n");
374 if (device != NULL) {
375 fprintf(stderr, "device already specified\n");
379 /* add sys dir if needed */
380 if (strncmp(optarg, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0)
381 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev), optarg, NULL);
383 util_strscpy(path, sizeof(path), optarg);
384 util_remove_trailing_chars(path, '/');
385 device = udev_device_new_from_syspath(udev, path);
386 if (device == NULL) {
387 fprintf(stderr, "device path not found\n");
393 action = ACTION_QUERY;
394 if (strcmp(optarg, "property") == 0 || strcmp(optarg, "env") == 0) {
395 query = QUERY_PROPERTY;
396 } else if (strcmp(optarg, "name") == 0) {
398 } else if (strcmp(optarg, "symlink") == 0) {
399 query = QUERY_SYMLINK;
400 } else if (strcmp(optarg, "path") == 0) {
402 } else if (strcmp(optarg, "all") == 0) {
405 fprintf(stderr, "unknown query type\n");
411 if (action == ACTION_NONE)
412 action = ACTION_ROOT;
416 printf("%s\n", udev_get_run_path(udev));
419 action = ACTION_DEVICE_ID_FILE;
420 util_strscpy(name, sizeof(name), optarg);
423 action = ACTION_ATTRIBUTE_WALK;
426 export_devices(udev);
435 export_prefix = optarg;
438 printf("%s\n", VERSION);
441 printf("Usage: udevadm info OPTIONS\n"
442 " --query=<type> query device information:\n"
443 " name name of device node\n"
444 " symlink pointing to node\n"
445 " path sys device path\n"
446 " property the device properties\n"
448 " --path=<syspath> sys device path used for query or attribute walk\n"
449 " --name=<name> node or symlink name used for query or attribute walk\n"
450 " --root prepend dev directory to path names\n"
451 " --attribute-walk print all key matches while walking along the chain\n"
452 " of parent devices\n"
453 " --device-id-of-file=<file> print major:minor of device containing this file\n"
454 " --export export key/value pairs\n"
455 " --export-prefix export the key name with a prefix\n"
456 " --export-db export the content of the udev database\n"
457 " --cleanup-db cleanup the udev database\n"
468 if (device == NULL) {
469 fprintf(stderr, "query needs a valid device specified by --path= or --name=\n");
476 const char *node = udev_device_get_devnode(device);
479 fprintf(stderr, "no device node found\n");
485 printf("%s\n", udev_device_get_devnode(device));
487 size_t len = strlen(udev_get_dev_path(udev));
489 printf("%s\n", &udev_device_get_devnode(device)[len+1]);
494 list_entry = udev_device_get_devlinks_list_entry(device);
495 while (list_entry != NULL) {
497 printf("%s", udev_list_entry_get_name(list_entry));
501 len = strlen(udev_get_dev_path(udev_device_get_udev(device)));
502 printf("%s", &udev_list_entry_get_name(list_entry)[len+1]);
504 list_entry = udev_list_entry_get_next(list_entry);
505 if (list_entry != NULL)
511 printf("%s\n", udev_device_get_devpath(device));
514 list_entry = udev_device_get_properties_list_entry(device);
515 while (list_entry != NULL) {
517 const char *prefix = export_prefix;
521 printf("%s%s='%s'\n", prefix,
522 udev_list_entry_get_name(list_entry),
523 udev_list_entry_get_value(list_entry));
525 printf("%s=%s\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry));
527 list_entry = udev_list_entry_get_next(list_entry);
531 print_record(device);
534 fprintf(stderr, "unknown query type\n");
538 case ACTION_ATTRIBUTE_WALK:
539 if (device == NULL) {
540 fprintf(stderr, "query needs a valid device specified by --path= or --name=\n");
544 print_device_chain(device);
546 case ACTION_DEVICE_ID_FILE:
547 if (stat_device(name, export, export_prefix) != 0)
551 printf("%s\n", udev_get_dev_path(udev));
554 fprintf(stderr, "missing option\n");
560 udev_device_unref(device);
564 const struct udevadm_cmd udevadm_info = {
567 .help = "query sysfs or the udev database",