2017-11-18 17:14:42 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
2004-01-20 04:46:26 +01:00
|
|
|
/*
|
2018-06-12 17:15:23 +02:00
|
|
|
* Copyright © 2004-2009 Kay Sievers <kay@vrfy.org>
|
2004-01-20 04:46:26 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-01-23 12:01:02 +01:00
|
|
|
#include <ctype.h>
|
2004-01-27 03:20:12 +01:00
|
|
|
#include <errno.h>
|
2009-05-20 18:58:52 +02:00
|
|
|
#include <fcntl.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2006-09-05 13:54:08 +02:00
|
|
|
#include <sys/stat.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <unistd.h>
|
2004-01-20 04:46:26 +01:00
|
|
|
|
2016-12-09 10:04:30 +01:00
|
|
|
#include "dirent-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2013-12-18 03:49:07 +01:00
|
|
|
#include "udev-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "udev.h"
|
2014-11-26 01:39:19 +01:00
|
|
|
#include "udevadm-util.h"
|
2004-01-20 04:46:26 +01:00
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static bool skip_attribute(const char *name) {
|
2013-03-23 03:55:05 +01:00
|
|
|
static const char* const skip[] = {
|
2012-01-10 01:34:15 +01:00
|
|
|
"uevent",
|
|
|
|
"dev",
|
|
|
|
"modalias",
|
|
|
|
"resource",
|
|
|
|
"driver",
|
|
|
|
"subsystem",
|
|
|
|
"module",
|
|
|
|
};
|
|
|
|
unsigned int i;
|
|
|
|
|
2012-04-16 03:13:22 +02:00
|
|
|
for (i = 0; i < ELEMENTSOF(skip); i++)
|
2013-02-13 18:13:22 +01:00
|
|
|
if (streq(name, skip[i]))
|
2012-01-10 01:34:15 +01:00
|
|
|
return true;
|
|
|
|
return false;
|
2011-03-04 23:00:52 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static void print_all_attributes(struct udev_device *device, const char *key) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_list_entry *sysattr;
|
|
|
|
|
|
|
|
udev_list_entry_foreach(sysattr, udev_device_get_sysattr_list_entry(device)) {
|
|
|
|
const char *name;
|
|
|
|
const char *value;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
name = udev_list_entry_get_name(sysattr);
|
|
|
|
if (skip_attribute(name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
value = udev_device_get_sysattr_value(device, name);
|
|
|
|
if (value == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* skip any values that look like a path */
|
|
|
|
if (value[0] == '/')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* skip nonprintable attributes */
|
|
|
|
len = strlen(value);
|
|
|
|
while (len > 0 && isprint(value[len-1]))
|
|
|
|
len--;
|
2012-04-08 16:06:20 +02:00
|
|
|
if (len > 0)
|
2012-01-10 01:34:15 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
printf(" %s{%s}==\"%s\"\n", key, name, value);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2004-01-20 04:46:26 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static int print_device_chain(struct udev_device *device) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_device *device_parent;
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
printf("\n"
|
|
|
|
"Udevadm info starts with the device specified by the devpath and then\n"
|
|
|
|
"walks up the chain of parent devices. It prints for every device\n"
|
|
|
|
"found, all possible attributes in the udev rules key format.\n"
|
|
|
|
"A rule to match, can be composed by the attributes of the device\n"
|
|
|
|
"and the attributes from one single parent device.\n"
|
|
|
|
"\n");
|
|
|
|
|
|
|
|
printf(" looking at device '%s':\n", udev_device_get_devpath(device));
|
|
|
|
printf(" KERNEL==\"%s\"\n", udev_device_get_sysname(device));
|
|
|
|
str = udev_device_get_subsystem(device);
|
|
|
|
if (str == NULL)
|
|
|
|
str = "";
|
|
|
|
printf(" SUBSYSTEM==\"%s\"\n", str);
|
|
|
|
str = udev_device_get_driver(device);
|
|
|
|
if (str == NULL)
|
|
|
|
str = "";
|
|
|
|
printf(" DRIVER==\"%s\"\n", str);
|
|
|
|
print_all_attributes(device, "ATTR");
|
|
|
|
|
|
|
|
device_parent = device;
|
|
|
|
do {
|
|
|
|
device_parent = udev_device_get_parent(device_parent);
|
|
|
|
if (device_parent == NULL)
|
|
|
|
break;
|
|
|
|
printf(" looking at parent device '%s':\n", udev_device_get_devpath(device_parent));
|
|
|
|
printf(" KERNELS==\"%s\"\n", udev_device_get_sysname(device_parent));
|
|
|
|
str = udev_device_get_subsystem(device_parent);
|
|
|
|
if (str == NULL)
|
|
|
|
str = "";
|
|
|
|
printf(" SUBSYSTEMS==\"%s\"\n", str);
|
|
|
|
str = udev_device_get_driver(device_parent);
|
|
|
|
if (str == NULL)
|
|
|
|
str = "";
|
|
|
|
printf(" DRIVERS==\"%s\"\n", str);
|
|
|
|
print_all_attributes(device_parent, "ATTRS");
|
|
|
|
} while (device_parent != NULL);
|
|
|
|
|
|
|
|
return 0;
|
2006-01-09 21:18:00 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static void print_record(struct udev_device *device) {
|
2012-01-10 01:34:15 +01:00
|
|
|
const char *str;
|
|
|
|
int i;
|
|
|
|
struct udev_list_entry *list_entry;
|
|
|
|
|
|
|
|
printf("P: %s\n", udev_device_get_devpath(device));
|
|
|
|
|
|
|
|
str = udev_device_get_devnode(device);
|
|
|
|
if (str != NULL)
|
2017-12-14 19:02:29 +01:00
|
|
|
printf("N: %s\n", str + STRLEN("/dev/"));
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
i = udev_device_get_devlink_priority(device);
|
|
|
|
if (i != 0)
|
|
|
|
printf("L: %i\n", i);
|
|
|
|
|
2012-04-16 17:21:22 +02:00
|
|
|
udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device))
|
2017-12-14 19:02:29 +01:00
|
|
|
printf("S: %s\n",
|
|
|
|
udev_list_entry_get_name(list_entry) + STRLEN("/dev/"));
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(device))
|
|
|
|
printf("E: %s=%s\n",
|
|
|
|
udev_list_entry_get_name(list_entry),
|
|
|
|
udev_list_entry_get_value(list_entry));
|
|
|
|
printf("\n");
|
2007-03-17 10:08:25 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static int stat_device(const char *name, bool export, const char *prefix) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
if (stat(name, &statbuf) != 0)
|
2016-05-24 12:07:42 +02:00
|
|
|
return -errno;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
if (export) {
|
|
|
|
if (prefix == NULL)
|
|
|
|
prefix = "INFO_";
|
2015-01-21 04:22:15 +01:00
|
|
|
printf("%sMAJOR=%u\n"
|
|
|
|
"%sMINOR=%u\n",
|
2012-01-10 01:34:15 +01:00
|
|
|
prefix, major(statbuf.st_dev),
|
|
|
|
prefix, minor(statbuf.st_dev));
|
|
|
|
} else
|
2015-01-21 04:22:15 +01:00
|
|
|
printf("%u:%u\n", major(statbuf.st_dev), minor(statbuf.st_dev));
|
2012-01-10 01:34:15 +01:00
|
|
|
return 0;
|
2007-11-09 10:22:30 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static int export_devices(struct udev *udev) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_enumerate_unrefp) struct udev_enumerate *udev_enumerate;
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_list_entry *list_entry;
|
|
|
|
|
|
|
|
udev_enumerate = udev_enumerate_new(udev);
|
|
|
|
if (udev_enumerate == NULL)
|
2016-05-24 12:07:42 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
|
|
|
udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_device_unrefp) struct udev_device *device;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
|
2016-05-24 12:07:42 +02:00
|
|
|
if (device != NULL)
|
2012-01-10 01:34:15 +01:00
|
|
|
print_record(device);
|
|
|
|
}
|
2016-05-24 12:07:42 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
return 0;
|
2008-09-25 13:20:27 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static void cleanup_dir(DIR *dir, mode_t mask, int depth) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct dirent *dent;
|
|
|
|
|
|
|
|
if (depth <= 0)
|
|
|
|
return;
|
|
|
|
|
2016-12-09 10:04:30 +01:00
|
|
|
FOREACH_DIRENT_ALL(dent, dir, break) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct stat stats;
|
|
|
|
|
|
|
|
if (dent->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
if (fstatat(dirfd(dir), dent->d_name, &stats, AT_SYMLINK_NOFOLLOW) != 0)
|
|
|
|
continue;
|
|
|
|
if ((stats.st_mode & mask) != 0)
|
|
|
|
continue;
|
|
|
|
if (S_ISDIR(stats.st_mode)) {
|
2015-04-21 13:21:44 +02:00
|
|
|
_cleanup_closedir_ DIR *dir2;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
dir2 = fdopendir(openat(dirfd(dir), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
|
2015-04-21 13:21:44 +02:00
|
|
|
if (dir2 != NULL)
|
2012-01-10 01:34:15 +01:00
|
|
|
cleanup_dir(dir2, mask, depth-1);
|
2015-04-21 13:21:44 +02:00
|
|
|
|
|
|
|
(void) unlinkat(dirfd(dir), dent->d_name, AT_REMOVEDIR);
|
|
|
|
} else
|
|
|
|
(void) unlinkat(dirfd(dir), dent->d_name, 0);
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
2011-04-13 22:33:01 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static void cleanup_db(struct udev *udev) {
|
2016-05-24 12:07:42 +02:00
|
|
|
_cleanup_closedir_ DIR *dir1 = NULL, *dir2 = NULL, *dir3 = NULL, *dir4 = NULL, *dir5 = NULL;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-05-24 12:07:42 +02:00
|
|
|
(void) unlink("/run/udev/queue.bin");
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-05-24 12:07:42 +02:00
|
|
|
dir1 = opendir("/run/udev/data");
|
|
|
|
if (dir1 != NULL)
|
|
|
|
cleanup_dir(dir1, S_ISVTX, 1);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-05-24 12:07:42 +02:00
|
|
|
dir2 = opendir("/run/udev/links");
|
|
|
|
if (dir2 != NULL)
|
|
|
|
cleanup_dir(dir2, 0, 2);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-05-24 12:07:42 +02:00
|
|
|
dir3 = opendir("/run/udev/tags");
|
|
|
|
if (dir3 != NULL)
|
|
|
|
cleanup_dir(dir3, 0, 2);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2016-05-24 12:07:42 +02:00
|
|
|
dir4 = opendir("/run/udev/static_node-tags");
|
|
|
|
if (dir4 != NULL)
|
|
|
|
cleanup_dir(dir4, 0, 2);
|
2013-07-07 18:32:34 +02:00
|
|
|
|
2016-05-24 12:07:42 +02:00
|
|
|
dir5 = opendir("/run/udev/watch");
|
|
|
|
if (dir5 != NULL)
|
|
|
|
cleanup_dir(dir5, 0, 1);
|
2011-04-13 22:33:01 +02:00
|
|
|
}
|
|
|
|
|
2015-01-05 13:19:55 +01:00
|
|
|
static void help(void) {
|
|
|
|
|
|
|
|
printf("%s info [OPTIONS] [DEVPATH|FILE]\n\n"
|
|
|
|
"Query sysfs or the udev database.\n\n"
|
|
|
|
" -h --help Print this message\n"
|
2017-12-03 18:34:54 +01:00
|
|
|
" -V --version Print version of the program\n"
|
2015-01-05 13:19:55 +01:00
|
|
|
" -q --query=TYPE Query device information:\n"
|
|
|
|
" name Name of device node\n"
|
|
|
|
" symlink Pointing to node\n"
|
|
|
|
" path sysfs device path\n"
|
|
|
|
" property The device properties\n"
|
|
|
|
" all All values\n"
|
|
|
|
" -p --path=SYSPATH sysfs device path used for query or attribute walk\n"
|
|
|
|
" -n --name=NAME Node or symlink name used for query or attribute walk\n"
|
|
|
|
" -r --root Prepend dev directory to path names\n"
|
|
|
|
" -a --attribute-walk Print all key matches walking along the chain\n"
|
|
|
|
" of parent devices\n"
|
|
|
|
" -d --device-id-of-file=FILE Print major:minor of device containing this file\n"
|
|
|
|
" -x --export Export key/value pairs\n"
|
|
|
|
" -P --export-prefix Export the key name with a prefix\n"
|
|
|
|
" -e --export-db Export the content of the udev database\n"
|
|
|
|
" -c --cleanup-db Clean up the udev database\n"
|
|
|
|
, program_invocation_short_name);
|
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static int uinfo(struct udev *udev, int argc, char *argv[]) {
|
tree-wide: drop redundant _cleanup_ macros (#8810)
This drops a good number of type-specific _cleanup_ macros, and patches
all users to just use the generic ones.
In most recent code we abstained from defining type-specific macros, and
this basically removes all those added already, with the exception of
the really low-level ones.
Having explicit macros for this is not too useful, as the expression
without the extra macro is generally just 2ch wider. We should generally
emphesize generic code, unless there are really good reasons for
specific code, hence let's follow this in this case too.
Note that _cleanup_free_ and similar really low-level, libc'ish, Linux
API'ish macros continue to be defined, only the really high-level OO
ones are dropped. From now on this should really be the rule: for really
low-level stuff, such as memory allocation, fd handling and so one, go
ahead and define explicit per-type macros, but for high-level, specific
program code, just use the generic _cleanup_() macro directly, in order
to keep things simple and as readable as possible for the uninitiated.
Note that before this patch some of the APIs (notable libudev ones) were
already used with the high-level macros at some places and with the
generic _cleanup_ macro at others. With this patch we hence unify on the
latter.
2018-04-25 12:31:45 +02:00
|
|
|
_cleanup_(udev_device_unrefp) struct udev_device *device = NULL;
|
2012-01-10 01:34:15 +01:00
|
|
|
bool root = 0;
|
|
|
|
bool export = 0;
|
|
|
|
const char *export_prefix = NULL;
|
|
|
|
char name[UTIL_PATH_SIZE];
|
|
|
|
struct udev_list_entry *list_entry;
|
2013-12-18 03:49:07 +01:00
|
|
|
int c;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
static const struct option options[] = {
|
2013-12-18 03:48:14 +01:00
|
|
|
{ "name", required_argument, NULL, 'n' },
|
|
|
|
{ "path", required_argument, NULL, 'p' },
|
|
|
|
{ "query", required_argument, NULL, 'q' },
|
|
|
|
{ "attribute-walk", no_argument, NULL, 'a' },
|
|
|
|
{ "cleanup-db", no_argument, NULL, 'c' },
|
|
|
|
{ "export-db", no_argument, NULL, 'e' },
|
|
|
|
{ "root", no_argument, NULL, 'r' },
|
2012-01-10 01:34:15 +01:00
|
|
|
{ "device-id-of-file", required_argument, NULL, 'd' },
|
2013-12-18 03:48:14 +01:00
|
|
|
{ "export", no_argument, NULL, 'x' },
|
|
|
|
{ "export-prefix", required_argument, NULL, 'P' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2012-01-10 01:34:15 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
enum action_type {
|
|
|
|
ACTION_QUERY,
|
|
|
|
ACTION_ATTRIBUTE_WALK,
|
|
|
|
ACTION_DEVICE_ID_FILE,
|
2012-06-10 22:53:07 +02:00
|
|
|
} action = ACTION_QUERY;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
enum query_type {
|
|
|
|
QUERY_NAME,
|
|
|
|
QUERY_PATH,
|
|
|
|
QUERY_SYMLINK,
|
|
|
|
QUERY_PROPERTY,
|
|
|
|
QUERY_ALL,
|
2012-06-10 22:53:07 +02:00
|
|
|
} query = QUERY_ALL;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2013-12-18 03:48:14 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "aced:n:p:q:rxP:RVh", options, NULL)) >= 0)
|
|
|
|
switch (c) {
|
2012-06-10 22:53:07 +02:00
|
|
|
case 'n': {
|
2012-01-10 01:34:15 +01:00
|
|
|
if (device != NULL) {
|
|
|
|
fprintf(stderr, "device already specified\n");
|
2013-12-18 03:49:07 +01:00
|
|
|
return 2;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
2012-06-10 22:53:07 +02:00
|
|
|
|
|
|
|
device = find_device(udev, optarg, "/dev/");
|
|
|
|
if (device == NULL) {
|
2012-01-10 01:34:15 +01:00
|
|
|
fprintf(stderr, "device node not found\n");
|
2013-12-18 03:49:07 +01:00
|
|
|
return 2;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
break;
|
2012-06-10 22:53:07 +02:00
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'p':
|
|
|
|
if (device != NULL) {
|
|
|
|
fprintf(stderr, "device already specified\n");
|
2013-12-18 03:49:07 +01:00
|
|
|
return 2;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
2012-06-10 22:53:07 +02:00
|
|
|
|
|
|
|
device = find_device(udev, optarg, "/sys");
|
2012-01-10 01:34:15 +01:00
|
|
|
if (device == NULL) {
|
2012-06-10 22:53:07 +02:00
|
|
|
fprintf(stderr, "syspath not found\n");
|
2013-12-18 03:49:07 +01:00
|
|
|
return 2;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
action = ACTION_QUERY;
|
2013-12-18 03:49:07 +01:00
|
|
|
if (streq(optarg, "property") || streq(optarg, "env"))
|
2012-01-10 01:34:15 +01:00
|
|
|
query = QUERY_PROPERTY;
|
2013-12-18 03:49:07 +01:00
|
|
|
else if (streq(optarg, "name"))
|
2012-01-10 01:34:15 +01:00
|
|
|
query = QUERY_NAME;
|
2013-12-18 03:49:07 +01:00
|
|
|
else if (streq(optarg, "symlink"))
|
2012-01-10 01:34:15 +01:00
|
|
|
query = QUERY_SYMLINK;
|
2013-12-18 03:49:07 +01:00
|
|
|
else if (streq(optarg, "path"))
|
2012-01-10 01:34:15 +01:00
|
|
|
query = QUERY_PATH;
|
2013-12-18 03:49:07 +01:00
|
|
|
else if (streq(optarg, "all"))
|
2012-01-10 01:34:15 +01:00
|
|
|
query = QUERY_ALL;
|
2013-12-18 03:49:07 +01:00
|
|
|
else {
|
2012-01-10 01:34:15 +01:00
|
|
|
fprintf(stderr, "unknown query type\n");
|
2013-12-18 03:49:07 +01:00
|
|
|
return 3;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
root = true;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
action = ACTION_DEVICE_ID_FILE;
|
2013-01-09 19:06:46 +01:00
|
|
|
strscpy(name, sizeof(name), optarg);
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
action = ACTION_ATTRIBUTE_WALK;
|
|
|
|
break;
|
|
|
|
case 'e':
|
2016-05-24 12:07:42 +02:00
|
|
|
if (export_devices(udev) < 0)
|
|
|
|
return 1;
|
2013-12-18 03:49:07 +01:00
|
|
|
return 0;
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'c':
|
|
|
|
cleanup_db(udev);
|
2013-12-18 03:49:07 +01:00
|
|
|
return 0;
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'x':
|
|
|
|
export = true;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
export_prefix = optarg;
|
|
|
|
break;
|
|
|
|
case 'V':
|
2017-12-05 15:30:10 +01:00
|
|
|
print_version();
|
2013-12-18 03:49:07 +01:00
|
|
|
return 0;
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'h':
|
2015-01-05 13:19:55 +01:00
|
|
|
help();
|
2013-12-18 03:49:07 +01:00
|
|
|
return 0;
|
2012-01-10 01:34:15 +01:00
|
|
|
default:
|
2013-12-18 03:49:07 +01:00
|
|
|
return 1;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case ACTION_QUERY:
|
2012-06-10 22:53:07 +02:00
|
|
|
if (!device) {
|
|
|
|
if (!argv[optind]) {
|
2015-01-05 13:19:55 +01:00
|
|
|
help();
|
2013-12-18 03:49:07 +01:00
|
|
|
return 2;
|
2012-06-10 22:53:07 +02:00
|
|
|
}
|
|
|
|
device = find_device(udev, argv[optind], NULL);
|
|
|
|
if (!device) {
|
|
|
|
fprintf(stderr, "Unknown device, --name=, --path=, or absolute path in /dev/ or /sys expected.\n");
|
2013-12-18 03:49:07 +01:00
|
|
|
return 4;
|
2012-06-10 22:53:07 +02:00
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(query) {
|
|
|
|
case QUERY_NAME: {
|
|
|
|
const char *node = udev_device_get_devnode(device);
|
|
|
|
|
|
|
|
if (node == NULL) {
|
|
|
|
fprintf(stderr, "no device node found\n");
|
2013-12-18 03:49:07 +01:00
|
|
|
return 5;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
|
2012-04-16 17:21:22 +02:00
|
|
|
if (root)
|
2012-01-10 01:34:15 +01:00
|
|
|
printf("%s\n", udev_device_get_devnode(device));
|
2012-04-16 17:21:22 +02:00
|
|
|
else
|
2017-12-14 19:02:29 +01:00
|
|
|
printf("%s\n",
|
|
|
|
udev_device_get_devnode(device) + STRLEN("/dev/"));
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QUERY_SYMLINK:
|
|
|
|
list_entry = udev_device_get_devlinks_list_entry(device);
|
|
|
|
while (list_entry != NULL) {
|
2012-04-16 17:21:22 +02:00
|
|
|
if (root)
|
2012-01-10 01:34:15 +01:00
|
|
|
printf("%s", udev_list_entry_get_name(list_entry));
|
2012-04-16 17:21:22 +02:00
|
|
|
else
|
2017-12-14 19:02:29 +01:00
|
|
|
printf("%s",
|
|
|
|
udev_list_entry_get_name(list_entry) + STRLEN("/dev/"));
|
2012-01-10 01:34:15 +01:00
|
|
|
list_entry = udev_list_entry_get_next(list_entry);
|
|
|
|
if (list_entry != NULL)
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
case QUERY_PATH:
|
|
|
|
printf("%s\n", udev_device_get_devpath(device));
|
2013-12-18 03:49:07 +01:00
|
|
|
return 0;
|
2012-01-10 01:34:15 +01:00
|
|
|
case QUERY_PROPERTY:
|
|
|
|
list_entry = udev_device_get_properties_list_entry(device);
|
|
|
|
while (list_entry != NULL) {
|
2016-06-14 23:05:42 +02:00
|
|
|
if (export)
|
|
|
|
printf("%s%s='%s'\n", strempty(export_prefix),
|
2012-01-10 01:34:15 +01:00
|
|
|
udev_list_entry_get_name(list_entry),
|
|
|
|
udev_list_entry_get_value(list_entry));
|
2016-06-14 23:05:42 +02:00
|
|
|
else
|
2012-01-10 01:34:15 +01:00
|
|
|
printf("%s=%s\n", udev_list_entry_get_name(list_entry), udev_list_entry_get_value(list_entry));
|
2016-06-14 23:05:42 +02:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
list_entry = udev_list_entry_get_next(list_entry);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QUERY_ALL:
|
|
|
|
print_record(device);
|
|
|
|
break;
|
|
|
|
default:
|
2014-02-23 01:26:27 +01:00
|
|
|
assert_not_reached("unknown query type");
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ACTION_ATTRIBUTE_WALK:
|
2012-06-10 22:53:07 +02:00
|
|
|
if (!device && argv[optind]) {
|
|
|
|
device = find_device(udev, argv[optind], NULL);
|
|
|
|
if (!device) {
|
|
|
|
fprintf(stderr, "Unknown device, absolute path in /dev/ or /sys expected.\n");
|
2013-12-18 03:49:07 +01:00
|
|
|
return 4;
|
2012-06-10 22:53:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!device) {
|
|
|
|
fprintf(stderr, "Unknown device, --name=, --path=, or absolute path in /dev/ or /sys expected.\n");
|
2013-12-18 03:49:07 +01:00
|
|
|
return 4;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
print_device_chain(device);
|
|
|
|
break;
|
|
|
|
case ACTION_DEVICE_ID_FILE:
|
|
|
|
if (stat_device(name, export, export_prefix) != 0)
|
2013-12-18 03:49:07 +01:00
|
|
|
return 1;
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
}
|
2004-01-27 03:20:12 +01:00
|
|
|
|
2013-12-18 03:49:07 +01:00
|
|
|
return 0;
|
2004-01-27 03:20:12 +01:00
|
|
|
}
|
2011-07-14 01:53:23 +02:00
|
|
|
|
|
|
|
const struct udevadm_cmd udevadm_info = {
|
2012-01-10 01:34:15 +01:00
|
|
|
.name = "info",
|
|
|
|
.cmd = uinfo,
|
2015-01-05 13:19:55 +01:00
|
|
|
.help = "Query sysfs or the udev database",
|
2011-07-14 01:53:23 +02:00
|
|
|
};
|