chiark / gitweb /
[PATCH] more advanced user query options
[elogind.git] / udevdb.c
index b75bf8048f1dd3633dea0844bfa7faae5f585018..8d077ea94fc766b652cdd0af705210867e23218f 100644 (file)
--- a/udevdb.c
+++ b/udevdb.c
@@ -24,6 +24,7 @@
 /*
  * udev database library
  */
+#define _KLIBC_HAS_ARCH_SIG_ATOMIC_T
 #include <stdlib.h>
 #include <stdio.h>
 #include <fcntl.h>
@@ -54,36 +55,29 @@ int udevdb_add_dev(const char *path, const struct udevice *dev)
        strcpy(keystr, path);
        key.dptr = keystr;
        key.dsize = strlen(keystr) + 1;
-       
+
        data.dptr = (void *)dev;
        data.dsize = sizeof(*dev);
        
        return tdb_store(udevdb, key, data, TDB_REPLACE); 
 }
 
-struct udevice *udevdb_get_dev(const char *path)
+int udevdb_get_dev(const char *path, struct udevice *dev)
 {
        TDB_DATA key, data;
-       struct udevice *dev;
 
        if (path == NULL)
-               return NULL;
+               return -ENODEV;
 
        key.dptr = (void *)path;
        key.dsize = strlen(path) + 1;
 
        data = tdb_fetch(udevdb, key);
        if (data.dptr == NULL || data.dsize == 0)
-               return NULL;
+               return -ENODEV;
 
-       dev = malloc(sizeof(*dev));
-       if (dev == NULL)
-               goto exit;
-       
        memcpy(dev, data.dptr, sizeof(*dev));
-exit:
-       free(data.dptr);
-       return dev;
+       return 0;
 }
 
 int udevdb_delete_dev(const char *path)
@@ -99,7 +93,7 @@ int udevdb_delete_dev(const char *path)
 
        key.dptr = keystr;
        key.dsize = strlen(keystr) + 1;
-       
+
        return tdb_delete(udevdb, key);
 }
 
@@ -116,21 +110,57 @@ void udevdb_exit(void)
 
 /**
  * udevdb_init: initializes database
- * @init_flag: database can either be in memory - UDEVDB_INTERNAL - or
- *     written to a file with UDEVDB_DEFAULT.
+ * @init_flag: UDEVDB_INTERNAL - database stays in memory
+ *            UDEVDB_DEFAULT - database is written to a file
  */
 int udevdb_init(int init_flag)
 {
        if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
                return -EINVAL;
 
-       udevdb = tdb_open(UDEV_CONFIG_DIR UDEV_DB, 0, init_flag, O_RDWR | O_CREAT, 0644);
+       udevdb = tdb_open(udev_db_filename, 0, init_flag, O_RDWR | O_CREAT, 0644);
        if (udevdb == NULL) {
                if (init_flag == UDEVDB_INTERNAL)
-                       dbg("Unable to initialize in-memory database");
+                       dbg("unable to initialize in-memory database");
                else
-                       dbg("Unable to initialize database at %s", UDEV_CONFIG_DIR UDEV_DB);
+                       dbg("unable to initialize database at '%s'", udev_db_filename);
+               return -EACCES;
+       }
+       return 0;
+}
+
+/**
+ * udevdb_open_ro: open database for reading
+ */
+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;
 }