udevadm-info.c revision 17fcfb5972977b6a3aedca6ad2aa8d1fbfbc761d
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering/*
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering *
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers * This program is free software: you can redistribute it and/or modify
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers * it under the terms of the GNU General Public License as published by
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering * the Free Software Foundation, either version 2 of the License, or
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering * (at your option) any later version.
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering *
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * This program is distributed in the hope that it will be useful,
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering * but WITHOUT ANY WARRANTY; without even the implied warranty of
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering * GNU General Public License for more details.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering *
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering * You should have received a copy of the GNU General Public License
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering */
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include "config.h"
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <stdlib.h>
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <string.h>
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <stdio.h>
252ff40a3827b4e8e62e03a5e2f80da3fe02bee6Lennart Poettering#include <stddef.h>
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <ctype.h>
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <stdarg.h>
4e949c11a1df4547d5f102e4131e07b026369cd7Javier Jardón#include <unistd.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <dirent.h>
78a825f216d39ee0295b00647b059d45467e1d02Kay Sievers#include <errno.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <getopt.h>
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering#include <sys/stat.h>
a80db8bd5f7f15859e8891aab9fc3694ce4cd0bdJavier Jardón#include <sys/types.h>
a80db8bd5f7f15859e8891aab9fc3694ce4cd0bdJavier Jardón
4db6d587c37c0357d20c79bf1a7c9afd4c7ced61Kay Sievers#include "udev.h"
907dd1953b7517534d646f5b2777780020c896e2Kay Sievers
eb7bbee6cd182d5c4eb1e1180631c35158f59379Kay Sieversstatic void print_all_attributes(struct udev *udev, const char *devpath, const char *key)
6aad7f2cd75c88f990c7857bb173b3e8d0faee0fUmut Tezduyar Lindskog{
4196a3ead3cfb823670d225eefcb3e60e34c7d95Kay Sievers char path[UTIL_PATH_SIZE];
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers DIR *dir;
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering struct dirent *dent;
22be093ffb403a1c474037939ca9b88b1ee39f77Lennart Poettering
d59d0a2b4b41a75eaf618b26b8f8bd1e17de7e2bcee util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
d59d0a2b4b41a75eaf618b26b8f8bd1e17de7e2bcee util_strlcat(path, devpath, sizeof(path));
d59d0a2b4b41a75eaf618b26b8f8bd1e17de7e2bcee
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering dir = opendir(path);
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers if (dir != NULL) {
8666abb452db73d9a11ead61251eec42bc531cceKay Sievers for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering struct stat statbuf;
5e63ce78b5018ba612e794a610a6f13c5eefade7Cristian Rodríguez char filename[UTIL_PATH_SIZE];
3d585edbb14b9705c80183aeb16dfd0a28df0ac9Lennart Poettering char *attr_value;
5e63ce78b5018ba612e794a610a6f13c5eefade7Cristian Rodríguez char value[UTIL_NAME_SIZE];
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera size_t len;
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger if (dent->d_name[0] == '.')
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger continue;
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger if (strcmp(dent->d_name, "uevent") == 0)
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger continue;
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger if (strcmp(dent->d_name, "dev") == 0)
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger continue;
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger util_strlcpy(filename, path, sizeof(filename));
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger util_strlcat(filename, "/", sizeof(filename));
f15515b5e6a9ebe95c938cc670df6e576fcf9176Filipe Brandenburger util_strlcat(filename, dent->d_name, sizeof(filename));
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger if (lstat(filename, &statbuf) != 0)
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger continue;
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger if (S_ISLNK(statbuf.st_mode))
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera continue;
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger attr_value = sysfs_attr_get_value(udev, devpath, dent->d_name);
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger if (attr_value == NULL)
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger continue;
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger len = util_strlcpy(value, attr_value, sizeof(value));
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger if(len >= sizeof(value))
2f96919bcdd0978164c801b21e053fb3b31e8bacFilipe Brandenburger len = sizeof(value) - 1;
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera dbg(udev, "attr '%s'='%s'(%zi)\n", dent->d_name, value, len);
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera /* skip nonprintable attributes */
1c7dde3e475978c569a982d65fd86d4b4e3caad8Bastien Nocera while (len && isprint(value[len-1]))
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering len--;
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering if (len) {
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering dbg(udev, "attribute value of '%s' non-printable, skip\n", dent->d_name);
3ce4fad8f548db9edb19869ea540e3192d2123f4Kay Sievers continue;
f975e971accc4d50c73ae53167db3df7a7099cf2Lennart Poettering }
e9da3678fcfc774b325dc1eaa054d0e00028a1fcLennart Poettering
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering printf(" %s{%s}==\"%s\"\n", key, dent->d_name, value);
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering }
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers }
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek printf("\n");
92ec4495f76a7a2a6c31f5bb2a5240f78dcfe1d2Javier Jardón}
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominenstatic int print_device_chain(struct udev *udev, const char *devpath)
b51fc639f01ee6194af3e7e944a79accce474fe1Dave Reisner{
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen struct sysfs_device *dev;
96ede2601f27cd5fe52eed96b873bef55cd0ce23Lennart Poettering
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen dev = sysfs_device_get(udev, devpath);
edeb68c53f1cdc452016b4c8512586a70b1262e3Tom Gundersen if (dev == NULL)
2d0efdf1af5ff77441228854343c61d42a89840cSamuli Suominen return -1;
7801356442578ff6e1c65844eb9e65c819af4660Zbigniew Jędrzejewski-Szmek
446883528524429283626208928b51f49f28f810Lennart Poettering printf("\n"
446883528524429283626208928b51f49f28f810Lennart Poettering "Udevinfo starts with the device specified by the devpath and then\n"
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmek "walks up the chain of parent devices. It prints for every device\n"
4acbce79798347cddf1e1d42e9be571e0a041873Zbigniew Jędrzejewski-Szmek "found, all possible attributes in the udev rules key format.\n"
80a5cbace45a6adbf2f9119edc5a4b10db493064Kay Sievers "A rule to match, can be composed by the attributes of the device\n"
b37250d661ed67d07c734630617d73e64f6d7e49Zbigniew Jędrzejewski-Szmek "and the attributes from one single parent device.\n"
b37250d661ed67d07c734630617d73e64f6d7e49Zbigniew Jędrzejewski-Szmek "\n");
6581f00f7eabdaccf587a4b6af60ed4696dd2791Zbigniew Jędrzejewski-Szmek
6581f00f7eabdaccf587a4b6af60ed4696dd2791Zbigniew Jędrzejewski-Szmek printf(" looking at device '%s':\n", dev->devpath);
92ec4495f76a7a2a6c31f5bb2a5240f78dcfe1d2Javier Jardón printf(" KERNEL==\"%s\"\n", dev->kernel);
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek printf(" SUBSYSTEM==\"%s\"\n", dev->subsystem);
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek printf(" DRIVER==\"%s\"\n", dev->driver);
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek print_all_attributes(udev, dev->devpath, "ATTR");
fe1fed02c7637a2c18cd575f78be7fda27972148Zbigniew Jędrzejewski-Szmek
6e92b23f0d6dd398848376bbaf47e54a90ed3389Kay Sievers /* walk up the chain of devices */
0eaeca1f2373a323b98c86b47561d98e59c67b25Kay Sievers while (1) {
d66ee73a3dd7b5433fc0da3125bbdff740de7745Zbigniew Jędrzejewski-Szmek dev = sysfs_device_get_parent(udev, dev);
d66ee73a3dd7b5433fc0da3125bbdff740de7745Zbigniew Jędrzejewski-Szmek if (dev == NULL)
d66ee73a3dd7b5433fc0da3125bbdff740de7745Zbigniew Jędrzejewski-Szmek break;
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers printf(" looking at parent device '%s':\n", dev->devpath);
ac714a78fdca481488d88f84b6332d28083a4511Martin Jansa printf(" KERNELS==\"%s\"\n", dev->kernel);
ac714a78fdca481488d88f84b6332d28083a4511Martin Jansa printf(" SUBSYSTEMS==\"%s\"\n", dev->subsystem);
b62cfcea00862ccbf0e5e297f8a339f70987edefMichael Biebl printf(" DRIVERS==\"%s\"\n", dev->driver);
b62cfcea00862ccbf0e5e297f8a339f70987edefMichael Biebl
b62cfcea00862ccbf0e5e297f8a339f70987edefMichael Biebl print_all_attributes(udev, dev->devpath, "ATTRS");
9a60da2834074d970ca063c210fe9d2f05c70532Thierry Reding }
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek return 0;
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek}
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmekstatic void print_record(struct udevice *udevice)
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek{
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek struct name_entry *name_loop;
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek printf("P: %s\n", udevice->dev->devpath);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek printf("N: %s\n", udevice->name);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek list_for_each_entry(name_loop, &udevice->symlink_list, node)
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek printf("S: %s\n", name_loop->name);
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek if (udevice->link_priority != 0)
732bfe09aeffc3cd78b80ee9e20c9c3babd944d6Zbigniew Jędrzejewski-Szmek printf("L: %i\n", udevice->link_priority);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek if (udevice->partitions != 0)
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek printf("A:%u\n", udevice->partitions);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek if (udevice->ignore_remove)
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek printf("R:%u\n", udevice->ignore_remove);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek list_for_each_entry(name_loop, &udevice->env_list, node)
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek printf("E: %s\n", name_loop->name);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek}
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmekstatic void export_db(struct udev *udev)
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek{
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek LIST_HEAD(name_list);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek struct name_entry *name_loop;
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek udev_db_get_all_entries(udev, &name_list);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek list_for_each_entry(name_loop, &name_list, node) {
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek struct udevice *udevice_db;
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek
eb2e280f9c59b66965c9316eadc4c113a13ca744Lucas De Marchi udevice_db = udev_device_init(udev);
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering if (udevice_db == NULL)
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering continue;
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering if (udev_db_get_device(udevice_db, name_loop->name) == 0)
27765dfc7a32d790badb29e6498b34edb0b60c33Lennart Poettering print_record(udevice_db);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering printf("\n");
780040dc2a4b08a2c1fe5bd8db3a70e966c2acb3Kay Sievers udev_device_cleanup(udevice_db);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering }
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering name_list_cleanup(udev, &name_list);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering}
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poetteringstatic int lookup_device_by_name(struct udev *udev, struct udevice **udevice, const char *name)
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering{
03930e48636e86ad84946253ca2bf4f91deeb645Lennart Poettering LIST_HEAD(name_list);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering int count;
693eb9a2d42d71445dad273a76e2470199d1dc5aLennart Poettering struct name_entry *device;
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering int rc = -1;
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering count = udev_db_get_devices_by_name(udev, name, &name_list);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering if (count <= 0)
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering goto out;
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering info(udev, "found %i devices for '%s'\n", count, name);
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering /* select the device that seems to match */
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering list_for_each_entry(device, &name_list, node) {
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering struct udevice *udevice_loop;
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering char filename[UTIL_PATH_SIZE];
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering struct stat statbuf;
8745297f9853c4a17bac69e1b7e652fe81bc1940Lennart Poettering
d200735e13c52dcfe36c0e066f9f6c2fbfb85a9cMichal Schmidt udevice_loop = udev_device_init(udev);
213298fb822258bb69c6b85b7c8d7f019fd2306aLennart Poettering if (udevice_loop == NULL)
693eb9a2d42d71445dad273a76e2470199d1dc5aLennart Poettering break;
be1a67d9d63bfdd4a5f8ba9cfc804030f10f5833Lennart Poettering if (udev_db_get_device(udevice_loop, device->name) != 0)
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering goto next;
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering info(udev, "found db entry '%s'\n", device->name);
9f64229f9ca69f5652b238a67531432e56108bd6Zbigniew Jędrzejewski-Szmek
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieri /* make sure, we don't get a link of a different device */
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieri util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieri util_strlcat(filename, "/", sizeof(filename));
5a45a93627609451784a04366cfa1150d32611d1Lennart Poettering util_strlcat(filename, name, sizeof(filename));
c1663b9daf5a43425e54bbe3daf6b10e64578f80Lennart Poettering if (stat(filename, &statbuf) != 0)
aca33b078cd32c5416a7fa3d5020a5d461c130eeCristian Rodríguez goto next;
2a4d1ec1520c926b0160efe14142634e164ddc30Cristian Rodríguez if (major(udevice_loop->devt) > 0 && udevice_loop->devt != statbuf.st_rdev) {
c1663b9daf5a43425e54bbe3daf6b10e64578f80Lennart Poettering info(udev, "skip '%s', dev_t doesn't match\n", udevice_loop->name);
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sievers goto next;
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sievers }
235c6e628199221b7885f159367614b7105547cfJohn rc = 0;
b850b06e1efcc7e27cfd785759a3a913ac9ed196Kay Sievers *udevice = udevice_loop;
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek break;
5a45a93627609451784a04366cfa1150d32611d1Lennart Poetteringnext:
03a170c03cd4a09721369ece48affb70d66c6b90Kay Sievers udev_device_cleanup(udevice_loop);
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek }
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmekout:
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek name_list_cleanup(udev, &name_list);
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek return rc;
9e7adc3ae1133fa08a468768a490812299fad030Lucas De Marchi}
9e7adc3ae1133fa08a468768a490812299fad030Lucas De Marchi
9b85fc6a89386582bfe792dba881800b0a093839Gustavo Sverzut Barbieristatic int stat_device(const char *name, int export, const char *prefix)
9d2d0fe1e3f28a639c26b62391f79cfd1450d91bEvangelos Foutras{
5a45a93627609451784a04366cfa1150d32611d1Lennart Poettering struct stat statbuf;
5a45a93627609451784a04366cfa1150d32611d1Lennart Poettering
39c4ead2323b45bbe9866e0f97fd8dcfb8a0bedeZbigniew Jędrzejewski-Szmek if (stat(name, &statbuf) != 0)
2a4d1ec1520c926b0160efe14142634e164ddc30Cristian Rodríguez return -1;
39c4ead2323b45bbe9866e0f97fd8dcfb8a0bedeZbigniew Jędrzejewski-Szmek
ae0ceefc2f432bc1068889fcff53d929eca8a3c4Zbigniew Jędrzejewski-Szmek if (export) {
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering if (prefix == NULL)
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmek prefix = "INFO_";
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmek printf("%sMAJOR=%d\n"
1c231f56482546725c4dbd3303f70300bd3c63e9Lennart Poettering "%sMINOR=%d\n",
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek prefix, major(statbuf.st_dev),
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek prefix, minor(statbuf.st_dev));
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek } else
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek printf("%d:%d\n", major(statbuf.st_dev), minor(statbuf.st_dev));
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek return 0;
ccd06097c79218f7d5ea4c21721bbcbc7c467dcaZbigniew Jędrzejewski-Szmek}
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmekint udevadm_info(struct udev *udev, int argc, char *argv[])
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek{
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek struct udevice *udevice = NULL;
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek int root = 0;
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek int export = 0;
32dcef3ab1eb91ee469c3246ef859578dccd8a45Zbigniew Jędrzejewski-Szmek const char *export_prefix = NULL;
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek static const struct option options[] = {
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger { "name", 1, NULL, 'n' },
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger { "path", 1, NULL, 'p' },
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger { "query", 1, NULL, 'q' },
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger { "attribute-walk", 0, NULL, 'a' },
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger { "export-db", 0, NULL, 'e' },
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger { "root", 0, NULL, 'r' },
be8737ae386166d2f279767ac87b226204c0de7eFilipe Brandenburger { "device-id-of-file", 1, NULL, 'd' },
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek { "export", 0, NULL, 'x' },
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek { "export-prefix", 1, NULL, 'P' },
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek { "version", 0, NULL, 1 }, /* -V outputs braindead format */
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek { "help", 0, NULL, 'h' },
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek {}
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek };
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek
747cf8cdf61cdad068c727e42eac699f2505ae77Zbigniew Jędrzejewski-Szmek enum action_type {
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek ACTION_NONE,
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek ACTION_QUERY,
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek ACTION_ATTRIBUTE_WALK,
25ee45f9953c121fc26a54a85ad7bb3a3180152bMichael Biebl ACTION_ROOT,
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek ACTION_DEVICE_ID_FILE,
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek } action = ACTION_NONE;
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
1864b0e39505cd44a98eee61c97916b86491c0b4Zbigniew Jędrzejewski-Szmek enum query_type {
1864b0e39505cd44a98eee61c97916b86491c0b4Zbigniew Jędrzejewski-Szmek QUERY_NONE,
1864b0e39505cd44a98eee61c97916b86491c0b4Zbigniew Jędrzejewski-Szmek QUERY_NAME,
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek QUERY_PATH,
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek QUERY_SYMLINK,
1864b0e39505cd44a98eee61c97916b86491c0b4Zbigniew Jędrzejewski-Szmek QUERY_ENV,
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek QUERY_ALL,
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek } query = QUERY_NONE;
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek char path[UTIL_PATH_SIZE] = "";
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek char name[UTIL_PATH_SIZE] = "";
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek struct name_entry *name_loop;
a6c0b31d509f76023d8efbcd5e912863c8fb254cZbigniew Jędrzejewski-Szmek int rc = 0;
f2ec0646aba7c6703a6c79603957e805b74c3befZbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek while (1) {
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek int option;
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek option = getopt_long(argc, argv, "aed:n:p:q:rxPVh", options, NULL);
c937e0d5c579863677e0fcb5508517f7714c332dZbigniew Jędrzejewski-Szmek if (option == -1)
afea26ad7d406d8b6c95d2642cb5a1d807b87546Lennart Poettering break;
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineau dbg(udev, "option '%c'\n", option);
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers switch (option) {
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers case 'n':
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers /* remove /dev if given */
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl if (strncmp(optarg, udev_get_dev_path(udev), strlen(udev_get_dev_path(udev))) == 0)
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl util_strlcpy(name, &optarg[strlen(udev_get_dev_path(udev))+1], sizeof(name));
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl else
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering util_strlcpy(name, optarg, sizeof(name));
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl util_remove_trailing_chars(name, '/');
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers dbg(udev, "name: %s\n", name);
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers break;
7959ff9914a6f3a59dbff95c199bcc540b70ac94Kay Sievers case 'p':
fba1ea06bb5b653e9eb0cc1b6004af8da273a4abShawn Landden /* remove /sys if given */
fba1ea06bb5b653e9eb0cc1b6004af8da273a4abShawn Landden if (strncmp(optarg, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) == 0)
fba1ea06bb5b653e9eb0cc1b6004af8da273a4abShawn Landden util_strlcpy(path, &optarg[strlen(udev_get_sys_path(udev))], sizeof(path));
85f19d825e7504676f3a80c78c1d9a7ec35a3b3fMichael Biebl else
47be870bd83fb3719dffc3ee9348a409ab762a14Lennart Poettering util_strlcpy(path, optarg, sizeof(path));
9388e99e208a6487b26dcbda86005ee9eba8d93dMichael Olbrich util_remove_trailing_chars(path, '/');
4db17f291c627c885de668200ff8cce2e57c933fZbigniew Jędrzejewski-Szmek
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek /* possibly resolve to real devpath */
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek if (util_resolve_sys_link(udev, path, sizeof(path)) != 0) {
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek char temp[UTIL_PATH_SIZE];
a8348796c0d39435b1c3d85ce6e95dad1ac85fecLennart Poettering char *pos;
9388e99e208a6487b26dcbda86005ee9eba8d93dMichael Olbrich
3b794314149e40afaf3c456285e1e529747b6560Holger Schurig /* also check if the parent is a link */
5f381b355a95b953654e46ba3ccdc81bdec165eaLennart Poettering util_strlcpy(temp, path, sizeof(temp));
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek pos = strrchr(temp, '/');
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek if (pos != 0) {
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek char tail[UTIL_PATH_SIZE];
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek util_strlcpy(tail, pos, sizeof(tail));
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek pos[0] = '\0';
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek if (util_resolve_sys_link(udev, temp, sizeof(temp)) == 0) {
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek util_strlcpy(path, temp, sizeof(path));
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek util_strlcat(path, tail, sizeof(path));
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek }
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek }
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek }
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek dbg(udev, "path: %s\n", path);
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek break;
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek case 'q':
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek action = ACTION_QUERY;
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek if (strcmp(optarg, "name") == 0) {
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek query = QUERY_NAME;
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek break;
81577dc22887debaf9b19bf1034a2887fb9069c7Zbigniew Jędrzejewski-Szmek }
a8348796c0d39435b1c3d85ce6e95dad1ac85fecLennart Poettering if (strcmp(optarg, "symlink") == 0) {
b237ef2cfac7ab0b33170809e8cb64628606207dTollef Fog Heen query = QUERY_SYMLINK;
a9b5b03212f9c854938483b8901e433c2ba6619bMichael Tremer break;
d1ab0ca07372649dad70a0348d75e394f254e1b6Lennart Poettering }
2270309471213a3c960543e523130627e9cb10e2Kay Sievers if (strcmp(optarg, "path") == 0) {
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek query = QUERY_PATH;
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek break;
8114dedc5910d9a9cec702f6b6658551a0cd9edeChengwei Yang }
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek if (strcmp(optarg, "env") == 0) {
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek query = QUERY_ENV;
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek break;
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek }
ac6b760ceedd4b21921b6a682cf1479af3d3024fZbigniew Jędrzejewski-Szmek if (strcmp(optarg, "all") == 0) {
2270309471213a3c960543e523130627e9cb10e2Kay Sievers query = QUERY_ALL;
4b357e15876b730343db08719c877fdb45b6ad42Michael Marineau break;
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek }
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek fprintf(stderr, "unknown query type\n");
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek rc = 2;
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek goto exit;
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek case 'r':
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek if (action == ACTION_NONE)
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek action = ACTION_ROOT;
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek root = 1;
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek break;
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek case 'd':
53e856e16ac37fe30b8bb59153ff69aad0fa9c27Zbigniew Jędrzejewski-Szmek action = ACTION_DEVICE_ID_FILE;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen util_strlcpy(name, optarg, sizeof(name));
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen break;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen case 'a':
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen action = ACTION_ATTRIBUTE_WALK;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen break;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen case 'e':
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen export_db(udev);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen goto exit;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen case 'x':
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen export = 1;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen break;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen case 'P':
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen export_prefix = optarg;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen break;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen case 1:
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen printf("%s\n", VERSION);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen goto exit;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen case 'V':
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen printf("udevinfo, version %s\n", VERSION);
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen goto exit;
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen case 'h':
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen printf("Usage: udevadm info OPTIONS\n"
6aea6d10f460853111ca8744201ec8dade97de3cThomas H.P. Andersen " --query=<type> query database for the specified value:\n"
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersen " name name of device node\n"
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersen " symlink pointing to node\n"
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersen " path sysfs device path\n"
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersen " env the device related imported environment\n"
e2ca86cf78f911a8be51f0224796e24883019139Dave Reisner " all all values\n"
e2ca86cf78f911a8be51f0224796e24883019139Dave Reisner " --path=<devpath> sysfs device path used for query or chain\n"
a18535d9e138c525d0443ec9f30a90b3e2184686Tom Gundersen " --name=<name> node or symlink name used for query\n"
e2ca86cf78f911a8be51f0224796e24883019139Dave Reisner " --root prepend to query result or print udev_root\n"
70d8320978dcbce022d9acbb953a10a7aca049abDavid Strauss " --attribute-walk print all key matches while walking along chain\n"
e2ca86cf78f911a8be51f0224796e24883019139Dave Reisner " of parent devices\n"
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersen " --device-id-of-file=<file> print major/minor of underlying device\n"
c4955740969d7ba8ba43b024bca1a0a5b56eb8e8Tom Gundersen " --export-db export the content of the udev database\n"
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersen " --help print this text\n"
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersen "\n");
e30431623a7d871da123cc37055ac49abf2c20eaTom Gundersen goto exit;
f553b3b1074151200187df916427a1468186435eAnders Olofsson default:
f553b3b1074151200187df916427a1468186435eAnders Olofsson goto exit;
f553b3b1074151200187df916427a1468186435eAnders Olofsson }
f553b3b1074151200187df916427a1468186435eAnders Olofsson }
f553b3b1074151200187df916427a1468186435eAnders Olofsson
f553b3b1074151200187df916427a1468186435eAnders Olofsson /* run action */
f553b3b1074151200187df916427a1468186435eAnders Olofsson switch (action) {
f553b3b1074151200187df916427a1468186435eAnders Olofsson case ACTION_QUERY:
f553b3b1074151200187df916427a1468186435eAnders Olofsson /* needs devpath or node/symlink name for query */
f553b3b1074151200187df916427a1468186435eAnders Olofsson if (path[0] != '\0') {
f553b3b1074151200187df916427a1468186435eAnders Olofsson udevice = udev_device_init(udev);
f553b3b1074151200187df916427a1468186435eAnders Olofsson if (udevice == NULL) {
728beb28a713709f521d374c9f8f3da781969d26Tom Gundersen rc = 1;
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier goto exit;
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier }
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier if (udev_db_get_device(udevice, path) != 0) {
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier fprintf(stderr, "no record for '%s' in database\n", path);
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier rc = 3;
17df7223be064b1542dbe868e3b35cca977ee639Lennart Poettering goto exit;
17df7223be064b1542dbe868e3b35cca977ee639Lennart Poettering }
17df7223be064b1542dbe868e3b35cca977ee639Lennart Poettering } else if (name[0] != '\0') {
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier if (lookup_device_by_name(udev, &udevice, name) != 0) {
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier fprintf(stderr, "node name not found\n");
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier rc = 4;
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier goto exit;
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier }
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier } else {
c0467cf387548dc98c0254f63553d862b35a84e5Ronny Chevalier fprintf(stderr, "query needs --path or node --name specified\n");
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers rc = 4;
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu goto exit;
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu }
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu switch(query) {
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu case QUERY_NAME:
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu if (root)
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu printf("%s/%s\n", udev_get_dev_path(udev), udevice->name);
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu else
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu printf("%s\n", udevice->name);
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu break;
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu case QUERY_SYMLINK:
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu list_for_each_entry(name_loop, &udevice->symlink_list, node) {
816115863962548a9a0d9fbfe429c7f8e685beacRoberto Sassu char c = name_loop->node.next != &udevice->symlink_list ? ' ' : '\n';
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering if (root)
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering printf("%s/%s%c", udev_get_dev_path(udev), name_loop->name, c);
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering else
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering printf("%s%c", name_loop->name, c);
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering }
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering break;
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering case QUERY_PATH:
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering printf("%s\n", udevice->dev->devpath);
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering goto exit;
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering case QUERY_ENV:
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering list_for_each_entry(name_loop, &udevice->env_list, node)
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering printf("%s\n", name_loop->name);
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering break;
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering case QUERY_ALL:
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering print_record(udevice);
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering break;
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering default:
77e68fa2f0bd018bab2621a31919bfaa6a6b0a35Lennart Poettering fprintf(stderr, "unknown query type\n");
3e2147858f21943d5f4a781c60f33ac22c6096edKay Sievers break;
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering }
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering break;
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering case ACTION_ATTRIBUTE_WALK:
3f8cc098d218525710e5cbad9adf37001d3b6060Jan Engelhardt if (path[0] != '\0') {
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poettering if (print_device_chain(udev, path) != 0) {
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poettering fprintf(stderr, "no valid sysfs device found\n");
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poettering rc = 4;
6a6751fe24bf456cf5c1efad785a4d11e78b42d0Lennart Poettering goto exit;
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering }
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering } else if (name[0] != '\0') {
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering if (lookup_device_by_name(udev, &udevice, name) != 0) {
56cf987fe74270bde4e16c7ec9e0414a9030723bDaniel J Walsh fprintf(stderr, "node name not found\n");
591622d7efbc828f00f190d91b6608148b967ff5Lennart Poettering rc = 4;
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez goto exit;
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer }
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer if (print_device_chain(udev, udevice->dev->devpath) != 0) {
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer fprintf(stderr, "no valid sysfs device found\n");
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer rc = 4;
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer goto exit;
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer }
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer } else {
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer fprintf(stderr, "attribute walk needs --path or node --name specified\n");
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer rc = 5;
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer goto exit;
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer }
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer break;
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer case ACTION_DEVICE_ID_FILE:
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer if (stat_device(name, export, export_prefix) != 0)
eef65bf3ee6f73afa4a5de23ae3a794a279f30c0Michael Scherer rc = 6;
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez break;
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez case ACTION_ROOT:
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez printf("%s\n", udev_get_dev_path(udev));
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez break;
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez default:
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez fprintf(stderr, "missing option\n");
5ec6b15b65304d94dc1c8cbad05c8b996b470d3aKay Sievers rc = 1;
56cf987fe74270bde4e16c7ec9e0414a9030723bDaniel J Walsh break;
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez }
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguezexit:
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez udev_device_cleanup(udevice);
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez return rc;
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez}
45df1f2c9a7fee67b37f64ddd00adad5982844faCristian Rodríguez