chiark / gitweb /
[PATCH] udev - advanced user query options
authorkay.sievers@vrfy.org <kay.sievers@vrfy.org>
Tue, 13 Jan 2004 05:18:57 +0000 (21:18 -0800)
committerGreg KH <gregkh@suse.de>
Wed, 27 Apr 2005 04:13:15 +0000 (21:13 -0700)
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

udev.c

diff --git a/udev.c b/udev.c
index 30921420a0fe9514dab8cce4a0bc3a2e76ffd774..7078f089e2db0a4d46069eda299e7d2e006b7098 100644 (file)
--- a/udev.c
+++ b/udev.c
@@ -82,13 +82,26 @@ static inline char *get_seqnum(void)
        return seqnum;
 }
 
        return seqnum;
 }
 
+enum query_type {
+       NONE,
+       NAME,
+       SYMLINK,
+       OWNER,
+       GROUP
+};
+
 static inline int udev_user(int argc, char **argv)
 {
 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 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)
        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) {
 
                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);
                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':
 
                case 'r':
-                       printf("%s\n", udev_root);
-                       return 0;
+                       root = 1;
+                       break;
 
                case 'V':
                        printf("udev, version %s\n", UDEV_VERSION);
 
                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"
 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");
               "  -r         print udev root\n"
               "  -V         print udev version\n"
               "  -h         print this help text\n"
               "\n");
-
        return retval;
 }
 
        return retval;
 }