]> err.no Git - systemd/commitdiff
[PATCH] more advanced user query options
authorkay.sievers@vrfy.org <kay.sievers@vrfy.org>
Tue, 13 Jan 2004 09:01:19 +0000 (01:01 -0800)
committerGreg KH <gregkh@suse.de>
Wed, 27 Apr 2005 04:13:15 +0000 (21:13 -0700)
Here is the '-h' and a '-d' to dump the whole database:

  kay@pim:~/src/udev.kay$ ./udev -d
  P: /block/hdb/hdb1
  N: hdb1
  S:
  O:
  G:

  P: /class/video4linux/video0
  N: video/webcam0
  S: camera0 kamera0
  O: 500
  G: 500

  P: /block/hdc
  N: hdc
  S:
  O:
  G:

udev.c
udevdb.c
udevdb.h

diff --git a/udev.c b/udev.c
index 7078f089e2db0a4d46069eda299e7d2e006b7098..84950c82a344150fede11268186572ead021231f 100644 (file)
--- a/udev.c
+++ b/udev.c
@@ -82,6 +82,16 @@ static inline char *get_seqnum(void)
        return seqnum;
 }
 
+static void print_record(char *path, struct udevice *dev)
+{
+       printf("P: %s\n", path);
+       printf("N: %s\n", dev->name);
+       printf("S: %s\n", dev->symlink);
+       printf("O: %s\n", dev->owner);
+       printf("G: %s\n", dev->group);
+       printf("\n");
+}
+
 enum query_type {
        NONE,
        NAME,
@@ -92,7 +102,7 @@ enum query_type {
 
 static inline int udev_user(int argc, char **argv)
 {
-       static const char short_options[] = "p:q:rVh";
+       static const char short_options[] = "dp:q:rVh";
        int option;
        int retval = -EINVAL;
        struct udevice dev;
@@ -144,6 +154,16 @@ static inline int udev_user(int argc, char **argv)
                        root = 1;
                        break;
 
+               case 'd':
+                       retval = udevdb_open_ro();
+                       if (retval != 0) {
+                               printf("unable to open udev database\n");
+                               return -EACCES;
+                       }
+                       retval = udevdb_dump(print_record);
+                       udevdb_exit();
+                       return retval;
+
                case 'V':
                        printf("udev, version %s\n", UDEV_VERSION);
                        return 0;
@@ -206,12 +226,17 @@ static inline int udev_user(int argc, char **argv)
        }
 
 help:
-       printf("Usage: [-qrVh]\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"
+       printf("Usage: [-pqrdVh]\n"
+              "  -q TYPE  query database for the specified value:\n"
+              "             'name'    name of device node\n"
+              "             'symlink' pointing to node\n"
+              "             'owner'   of node\n"
+              "             'group'   of node\n"
+              "  -p PATH  sysfs device path used for query\n"
+              "  -r       print udev root\n"
+              "  -d       dump whole database\n"
+              "  -V       print udev version\n"
+              "  -h       print this help text\n"
               "\n");
        return retval;
 }
index ca9e63c97e61d4376f0d9593b2c54b3a4b936bcf..8d077ea94fc766b652cdd0af705210867e23218f 100644 (file)
--- a/udevdb.c
+++ b/udevdb.c
@@ -124,7 +124,7 @@ int udevdb_init(int init_flag)
                        dbg("unable to initialize in-memory database");
                else
                        dbg("unable to initialize database at '%s'", udev_db_filename);
-               return -EINVAL;
+               return -EACCES;
        }
        return 0;
 }
@@ -137,7 +137,30 @@ int udevdb_open_ro(void)
        udevdb = tdb_open(udev_db_filename, 0, 0, O_RDONLY, 0);
        if (udevdb == NULL) {
                dbg("unable to open database at '%s'", udev_db_filename);
+               return -EACCES;
+       }
+       return 0;
+}
+
+void (*user_record_callback) (char *path, struct udevice *dev);
+
+static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
+{
+       user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr);
+       return 0;
+}
+
+/**
+ * udevdb_dump: dumps whole database by passing record data to user function
+ * @user_record_handler: user function called for every record in the database
+ */
+int udevdb_dump(void (*user_record_handler) (char *path, struct udevice *dev))
+{
+       if (user_record_handler == NULL) {
+               dbg("invalid user record handling function");
                return -EINVAL;
        }
+       user_record_callback = user_record_handler;
+       tdb_traverse(udevdb, traverse_callback, NULL);
        return 0;
 }
index f36a9ca79974594f87276fe767dfe40021af0d75..a0e53382655a855e33598beb8b1a009ed2348e94 100644 (file)
--- a/udevdb.h
+++ b/udevdb.h
@@ -12,6 +12,7 @@
 extern void udevdb_exit(void);
 extern int udevdb_init(int init_flag);
 extern int udevdb_open_ro(void);
+extern int udevdb_dump(void (*user_record_handler) (char *path, struct udevice *dev));
 
 extern int udevdb_add_dev(const char *path, const struct udevice *dev);
 extern int udevdb_get_dev(const char *path, struct udevice *dev);