summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkay.sievers@vrfy.org <kay.sievers@vrfy.org>2004-01-12 21:18:57 -0800
committerGreg KH <gregkh@suse.de>2005-04-26 21:13:15 -0700
commit5472beeea5884dfc8bc5afb0f963de3ea3232e66 (patch)
treeb622f71003b4757515dd886e60fb848a824b1b8d
parent606143c8d214f7bb7271d40d514eb23a86547b3c (diff)
downloadsystemd-5472beeea5884dfc8bc5afb0f963de3ea3232e66.tar.gz
systemd-5472beeea5884dfc8bc5afb0f963de3ea3232e66.tar.bz2
systemd-5472beeea5884dfc8bc5afb0f963de3ea3232e66.zip
[PATCH] udev - advanced user query options
This patch improves the user options for udev. It is possible now to query for the name, the symlinks or owner/group. If asked for the name of the node we are able to prepend the udev_root with the -r option. SAMPLE: kay@pim:~/src/udev.test$ ./udev -V udev, version 012_bk kay@pim:~/src/udev.test$ ./udev -h Usage: [-qrVh] -q <name> query database for the specified value -p <path> device path used for query -r print udev root -V print udev version -h print this help text kay@pim:~/src/udev.test$ ./udev -r /udev/ kay@pim:~/src/udev.test$ ./udev -q name -p /class/video4linux/video0 video/webcam0 kay@pim:~/src/udev.test$ ./udev -q symlink -p /class/video4linux/video0 camera0 kamera0 kay@pim:~/src/udev.test$ ./udev -q owner -p /class/video4linux/video0 501 kay@pim:~/src/udev.test$ ./udev -r -q name -p /class/video4linux/video0 /udev/video/webcam0
-rw-r--r--udev.c109
1 files changed, 93 insertions, 16 deletions
diff --git a/udev.c b/udev.c
index 30921420a0..7078f089e2 100644
--- a/udev.c
+++ b/udev.c
@@ -82,13 +82,26 @@ static inline char *get_seqnum(void)
return seqnum;
}
+enum query_type {
+ NONE,
+ NAME,
+ SYMLINK,
+ OWNER,
+ GROUP
+};
+
static inline int udev_user(int argc, char **argv)
{
- static const char short_options[] = "q:rVh";
+ static const char short_options[] = "p:q:rVh";
int option;
int retval = -EINVAL;
struct udevice dev;
+ int root = 0;
+ enum query_type query = NONE;
+ char result[NAME_SIZE] = "";
+ char path[NAME_SIZE] = "";
+ /* get command line options */
while (1) {
option = getopt(argc, argv, short_options);
if (option == -1)
@@ -96,25 +109,40 @@ static inline int udev_user(int argc, char **argv)
dbg("option '%c'", option);
switch (option) {
+ case 'p':
+ dbg("udev path: %s\n", optarg);
+ strfieldcpy(path, optarg);
+ break;
+
case 'q':
dbg("udev query: %s\n", optarg);
- retval = udevdb_open_ro();
- if (retval != 0) {
- printf("unable to open udev database\n");
- return -1;
+
+ if (strcmp(optarg, "name") == 0) {
+ query = NAME;
+ break;
+ }
+
+ if (strcmp(optarg, "symlink") == 0) {
+ query = SYMLINK;
+ break;
}
- retval = udevdb_get_dev(optarg, &dev);
- if (retval == 0) {
- printf("%s\n", dev.name);
- } else {
- printf("device not found in udev database\n");
+
+ if (strcmp(optarg, "owner") == 0) {
+ query = OWNER;
+ break;
}
- udevdb_exit();
- return retval;
+
+ if (strcmp(optarg, "group") == 0) {
+ query = GROUP;
+ break;
+ }
+
+ printf("unknown query type\n");
+ return -EINVAL;
case 'r':
- printf("%s\n", udev_root);
- return 0;
+ root = 1;
+ break;
case 'V':
printf("udev, version %s\n", UDEV_VERSION);
@@ -128,14 +156,63 @@ static inline int udev_user(int argc, char **argv)
}
}
+ /* process options */
+ if (query != NONE) {
+ if (path[0] == '\0') {
+ printf("query needs device path specified\n");
+ return -EINVAL;
+ }
+
+ retval = udevdb_open_ro();
+ if (retval != 0) {
+ printf("unable to open udev database\n");
+ return -EACCES;
+ }
+ retval = udevdb_get_dev(path, &dev);
+ if (retval == 0) {
+ switch(query) {
+ case NAME:
+ if (root)
+ strfieldcpy(result, udev_root);
+ strncat(result, dev.name, sizeof(result));
+ break;
+
+ case SYMLINK:
+ strfieldcpy(result, dev.symlink);
+ break;
+
+ case GROUP:
+ strfieldcpy(result, dev.group);
+ break;
+
+ case OWNER:
+ strfieldcpy(result, dev.owner);
+ break;
+
+ default:
+ break;
+ }
+ printf("%s\n", result);
+ } else {
+ printf("device not found in udev database\n");
+ }
+ udevdb_exit();
+ return retval;
+ }
+
+ if (root) {
+ printf("%s\n", udev_root);
+ return 0;
+ }
+
help:
printf("Usage: [-qrVh]\n"
- " -q <path> query database for the name of the created node\n"
+ " -q <name> query database for the specified value\n"
+ " -p <path> device path used for query\n"
" -r print udev root\n"
" -V print udev version\n"
" -h print this help text\n"
"\n");
-
return retval;
}