From 5472beeea5884dfc8bc5afb0f963de3ea3232e66 Mon Sep 17 00:00:00 2001 From: "kay.sievers@vrfy.org" Date: Mon, 12 Jan 2004 21:18:57 -0800 Subject: [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 query database for the specified value -p 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 --- udev.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 16 deletions(-) (limited to 'udev.c') diff --git a/udev.c b/udev.c index 30921420..7078f089 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 query database for the name of the created node\n" + " -q query database for the specified value\n" + " -p 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; } -- cgit 1.2.3-korg