chiark / gitweb /
[PATCH] add udevd and udevsend to the spec file.
[elogind.git] / udevdb.c
index e4330f532029f8be4ed629e51b6e16e6026c57b9..c4e064fc115d4414ef21cbbbc44403ed4570e7bc 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>
@@ -34,6 +35,7 @@
 
 #include "udev_version.h"
 #include "udev.h"
+#include "logging.h"
 #include "namedev.h"
 #include "udevdb.h"
 #include "tdb/tdb.h"
 
 static TDB_CONTEXT *udevdb;
 
-struct sysfsdb_record {
-       char name[PATH_SIZE];
-};
 
-/**
- * namedb_record - device name is key, remaining udevice info stored here.
- */
-struct namedb_record {
-       char sysfs_dev_path[PATH_SIZE];
-       char class_dev_name[NAME_SIZE];
-       char class_name[NAME_SIZE];
-       char id[ID_SIZE];
-       char type;
-       int major;
-       int minor;
-       mode_t mode;
-};
-
-/**
- * udevdb_close: close udev database
- */
-static void udevdb_close(void)
-{
-       if (udevdb != NULL) {
-               tdb_close(udevdb);
-               udevdb = NULL;
-       }
-}
-
-/**
- * udevdb_open: opens udev's database
- * @method: database can either be in memory - UDEVDB_INTERNAL - or
- *     written to a file with UDEVDB_DEFAULT.
- */
-static int udevdb_open(int method)
-{
-       udevdb = tdb_open(UDEV_CONFIG_DIR UDEV_DB, 0, method, O_RDWR | O_CREAT, 0644);
-       if (udevdb == NULL) {
-               if (method == UDEVDB_INTERNAL)
-                       dbg("Unable to initialize in-memory database");
-               else
-                       dbg("Unable to initialize database at %s", UDEV_CONFIG_DIR UDEV_DB);
-               return -EINVAL;
-       }
-       return 0;
-}
-
-static struct sysfsdb_record *sysfsdb_fetch(const char *path)
+int udevdb_add_dev(const char *path, const struct udevice *dev)
 {
        TDB_DATA key, data;
-       char keystr[PATH_SIZE+2]; 
-       struct sysfsdb_record *rec = NULL;
+       char keystr[SYSFS_PATH_MAX];
 
-       if (strlen(path) >= PATH_SIZE)
-               return NULL;
+       if ((path == NULL) || (dev == NULL))
+               return -ENODEV;
 
-       memset(keystr, 0, sizeof(keystr));
+       memset(keystr, 0, NAME_SIZE);
        strcpy(keystr, path);
-
-       dbg("keystr = %s", keystr);
-
-       key.dptr = (void *)keystr;
+       key.dptr = keystr;
        key.dsize = strlen(keystr) + 1;
 
-       data = tdb_fetch(udevdb, key);
-       if (data.dptr == NULL || data.dsize == 0) {
-               dbg("tdb_fetch did not work :(");
-               return NULL;
-       }
+       data.dptr = (void *)dev;
+       data.dsize = sizeof(*dev);
        
-       rec = (struct sysfsdb_record *)malloc(sizeof(struct sysfsdb_record));
-       if (rec == NULL) {
-               free(data.dptr);
-               return NULL;
-       }
-       
-       memcpy(rec, data.dptr, sizeof(struct sysfsdb_record));
-       free(data.dptr);
-
-       return rec;
+       return tdb_store(udevdb, key, data, TDB_REPLACE); 
 }
 
-/**
- * namedb_fetch
- */
-static struct namedb_record *namedb_fetch(const char *name)
+int udevdb_get_dev(const char *path, struct udevice *dev)
 {
        TDB_DATA key, data;
-       char nm_keystr[NAME_SIZE]; 
-       struct namedb_record *nrec = NULL;
 
-       if (name == NULL)
-               return NULL; 
-       if (strlen(name) >= NAME_SIZE)
-               return NULL;
-
-       memset(nm_keystr, 0, NAME_SIZE);
-       strcpy(nm_keystr, name);
+       if (path == NULL)
+               return -ENODEV;
 
-       key.dptr = (void *)nm_keystr;
-       key.dsize = strlen(nm_keystr) + 1;
+       key.dptr = (void *)path;
+       key.dsize = strlen(path) + 1;
 
        data = tdb_fetch(udevdb, key);
        if (data.dptr == NULL || data.dsize == 0)
-               return NULL;
-
-       nrec = (struct namedb_record *)malloc(sizeof(struct namedb_record));
-       if (nrec == NULL) {
-               free(data.dptr);
-               return NULL;
-       }
-       
-       memcpy(nrec, data.dptr, sizeof(struct namedb_record));
-       free(data.dptr);
-
-       return nrec;
-}
-
-static int sysfs_store(const char *path, const struct udevice *dev)
-{
-       TDB_DATA key, data;
-       char keystr[PATH_SIZE+2];
-       struct sysfsdb_record rec;
-       int retval = 0;
-
-       if (dev == NULL)
                return -ENODEV;
 
-       memset(keystr, 0, sizeof(keystr));
-       strcpy(keystr, path);
-
-       key.dptr = (void *)keystr;
-       key.dsize = strlen(keystr) + 1;
-       
-       strcpy(rec.name, dev->name);
-
-       data.dptr = (void *) &rec;
-       data.dsize = sizeof(rec);
-       
-       retval = tdb_store(udevdb, key, data, TDB_REPLACE); 
-       return retval;
-}
-
-/**
- * namedb_store
- */
-static int namedb_store(const struct udevice *dev)
-{
-       TDB_DATA key, data;
-       char keystr[NAME_SIZE];
-       struct namedb_record rec;
-       int retval = 0;
-
-       if (dev == NULL)
-               return -1;
-
-       memset(keystr, 0, NAME_SIZE);
-       strcpy(keystr, dev->name);
-
-       key.dptr = (void *)keystr;
-       key.dsize = strlen(keystr) + 1;
-       
-       strcpy(rec.sysfs_dev_path, dev->sysfs_dev_path);
-       strcpy(rec.id, dev->bus_id);
-       strcpy(rec.class_dev_name, dev->class_dev_name);
-       strcpy(rec.class_name, dev->class_name);
-       rec.type = dev->type;
-       rec.major = dev->major;
-       rec.minor = dev->minor;
-       rec.mode = dev->mode;
-
-       data.dptr = (void *) &rec;
-       data.dsize = sizeof(rec);
-       
-       retval = tdb_store(udevdb, key, data, TDB_REPLACE); 
-       return retval;
+       memcpy(dev, data.dptr, sizeof(*dev));
+       return 0;
 }
 
-/**
- * namedb_delete
- */
-static int namedb_delete(const char *name)
+int udevdb_delete_dev(const char *path)
 {
        TDB_DATA key;
-       char keystr[NAME_SIZE]; 
-       int retval = 0;
-
-       if (name == NULL)
-               return -1; 
-       if (strlen(name) >= NAME_SIZE)
-               return -1;
+       char keystr[SYSFS_PATH_MAX];
 
-       memset(keystr, 0, NAME_SIZE);
-       strcpy(keystr, name);
-
-       key.dptr = (void *)keystr;
-       key.dsize = strlen(keystr) + 1;
-
-       retval = tdb_delete(udevdb, key);
-       return retval;
-}
-
-static int sysfs_delete(const char *path)
-{
-       TDB_DATA key;
-       char keystr[PATH_SIZE];
+       if (path == NULL)
+               return -EINVAL;
 
        memset(keystr, 0, sizeof(keystr));
        strcpy(keystr, path);
 
        key.dptr = keystr;
        key.dsize = strlen(keystr) + 1;
-       
+
        return tdb_delete(udevdb, key);
 }
 
 /**
- * udevdb_delete_udevice
+ * udevdb_exit: closes database
  */
-int udevdb_delete_udevice(const char *name)
+void udevdb_exit(void)
 {
-       struct namedb_record *nrec = NULL;
-
-       if (name == NULL)
-               return -1; 
-
-       nrec = namedb_fetch(name);
-       if (nrec == NULL)
-               return -1;
-
-       namedb_delete(name);
-       free(nrec);
-
-       return 0;
+       if (udevdb != NULL) {
+               tdb_close(udevdb);
+               udevdb = NULL;
+       }
 }
 
 /**
- * udevdb_add_device: adds class device to database
+ * udevdb_init: initializes database
+ * @init_flag: UDEVDB_INTERNAL - database stays in memory
+ *            UDEVDB_DEFAULT - database is written to a file
  */
-int udevdb_add_device(const char *device, const struct sysfs_class_device *class_dev, const char *name, char type, int major, int minor, int mode)
+int udevdb_init(int init_flag)
 {
-       struct udevice dbdev;
-
-       if (class_dev == NULL)
-               return -ENODEV;
+       if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
+               return -EINVAL;
 
-       memset(&dbdev, 0, sizeof(dbdev));
-       strncpy(dbdev.name, name, NAME_SIZE);
-       if (class_dev->sysdevice) {
-               strncpy(dbdev.sysfs_dev_path, class_dev->sysdevice->directory->path, PATH_SIZE);
-               strncpy(dbdev.bus_id, class_dev->sysdevice->bus_id, ID_SIZE);
+       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");
+               else
+                       dbg("unable to initialize database at '%s'", udev_db_filename);
+               return -EACCES;
        }
-       strncpy(dbdev.class_dev_name, class_dev->name, NAME_SIZE);
-//     if ((sysfs_get_name_from_path(subsystem, dbdev.class_name, NAME_SIZE)) != 0)
-//             strcpy(dbdev.class_name, "unknown");
-       dbdev.type = type;
-       dbdev.major = major;
-       dbdev.minor = minor;
-       dbdev.mode = mode;
-       
-       if ((sysfs_store(device, &dbdev)) != 0)
-               return -1;
-//     if ((namedb_store(&dbdev)) != 0)
-//             return -1;
-
        return 0;
 }
 
 /**
- * udevdb_get_device: grab's device by name
+ * udevdb_open_ro: open database for reading
  */
-struct udevice *udevdb_get_udevice(const char *name)
+int udevdb_open_ro(void)
 {
-       struct namedb_record *nrec = NULL;
-       struct udevice *dev = NULL;
-
-       if (name == NULL)
-               return NULL; 
-
-       nrec = namedb_fetch(name);
-       if (nrec == NULL)
-               return NULL;
-
-       dev = (struct udevice *)malloc(sizeof(struct udevice));
-       if (dev == NULL) {
-               free(nrec);
-               return NULL;
+       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;
+}
 
-       strcpy(dev->name, name);
-       strcpy(dev->sysfs_dev_path, nrec->sysfs_dev_path);
-       strcpy(dev->class_dev_name, nrec->class_dev_name);
-       strcpy(dev->class_name, nrec->class_name);
-       strcpy(dev->bus_id, nrec->id);
-       dev->type = nrec->type;
-       dev->major = nrec->major;
-       dev->minor = nrec->minor;
-       dev->mode = nrec->mode;
-
-       free(nrec);
+static int (*user_record_callback) (char *path, struct udevice *dev);
 
-       return dev;
+static int traverse_callback(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
+{
+       return user_record_callback((char*) key.dptr, (struct udevice*) dbuf.dptr);
 }
 
-int udevdb_get_dev(const char *path, char *name, size_t name_size)
+/**
+ * udevdb_call_foreach: dumps whole database by passing record data to user function
+ * @user_record_handler: user function called for every record in the database
+ */
+int udevdb_call_foreach(int (*user_record_handler) (char *path, struct udevice *dev))
 {
-       struct sysfsdb_record *rec = NULL;
+       int retval = 0;
 
-       if ((path == NULL) || (name == NULL) || (name_size < 1))
+       if (user_record_handler == NULL) {
+               dbg("invalid user record handling function");
                return -EINVAL;
-
-       rec = sysfsdb_fetch(path);
-       if (rec == NULL)
+       }
+       user_record_callback = user_record_handler;
+       retval = tdb_traverse(udevdb, traverse_callback, NULL);
+       if (retval < 0)
                return -ENODEV;
-
-       if (strlen(rec->name) >= name_size)
-               return -EINVAL;
-
-       strncpy(name, rec->name, name_size);
-       return 0;
+       else
+               return 0;
 }
 
-int udevdb_delete_dev(const char *path)
-{
-       if (path == NULL)
-               return -EINVAL;
-
-       return sysfs_delete(path);
-}
+static struct udevice *find_dev;
+static char *find_path;
+static const char *find_name;
+static int find_found;
 
-/**
- * udevdb_exit: closes database
- */
-void udevdb_exit(void)
+static int find_device_by_name(char *path, struct udevice *dev)
 {
-       udevdb_close();
+       if (strncmp(dev->name, find_name, sizeof(dev->name)) == 0) {
+               memcpy(find_dev, dev, sizeof(*find_dev));
+               strncpy(find_path, path, NAME_SIZE);
+               find_found = 1;
+               /* stop search */
+               return 1;
+       }
+       return 0;
 }
 
 /**
- * udevdb_init: initializes database
+ * udevdb_get_dev_byname: search device with given name by traversing the whole database
  */
-int udevdb_init(int init_flag)
+int udevdb_get_dev_byname(const char *name, char *path, struct udevice *dev)
 {
-       if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
+       find_found = 0;
+       find_path = path;
+       find_dev = dev;
+       find_name = name;
+       udevdb_call_foreach(find_device_by_name);
+       if (find_found == 1)
+               return 0;
+       else
                return -1;
-
-       return udevdb_open(init_flag);
 }