chiark / gitweb /
[PATCH] add BUS= bug to TODO list so it will not get forgotten.
[elogind.git] / udevdb.c
index b200f2f6edffd02853fa0b65609261b03418d56c..678ddd960340764bb8a94f7025ebb4bf29e8d00d 100644 (file)
--- a/udevdb.c
+++ b/udevdb.c
@@ -1,3 +1,26 @@
+/*
+ * udevdb.c
+ *
+ * Userspace devfs
+ *
+ * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
+ * Copyright (C) 2003 IBM Corp.
+ *
+ *     This program is free software; you can redistribute it and/or modify it
+ *     under the terms of the GNU General Public License as published by the
+ *     Free Software Foundation version 2 of the License.
+ * 
+ *     This program is distributed in the hope that it will be useful, but
+ *     WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *     General Public License for more details.
+ * 
+ *     You should have received a copy of the GNU General Public License along
+ *     with this program; if not, write to the Free Software Foundation, Inc.,
+ *     675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
 /*
  * udev database library
  */
 #include <string.h>
 #include <sys/stat.h>
 #include <errno.h>
+#include <signal.h>
 
+#include "udev_version.h"
+#include "udev.h"
+#include "namedev.h"
 #include "udevdb.h"
-#include "tdb.h"
+#include "tdb/tdb.h"
+#include "libsysfs/libsysfs.h"
 
-static TDB_CONTEXT *busdb;
-static TDB_CONTEXT *classdb;
-static TDB_CONTEXT *namedb;
+static TDB_CONTEXT *udevdb;
 
 /**
  * busdb_record - bus and id are keys to look up name of device
@@ -30,11 +56,15 @@ struct classdb_record {
        char name[NAME_SIZE];
 };
 
+struct sysfsdb_record {
+       char name[PATH_SIZE];
+};
+
 /**
  * namedb_record - device name is key, remaining udevice info stored here.
  */
 struct namedb_record {
-       char sysfs_path[PATH_SIZE];
+       char sysfs_dev_path[PATH_SIZE];
        char class_dev_name[NAME_SIZE];
        char class_name[NAME_SIZE];
        char bus[BUS_SIZE];
@@ -43,72 +73,35 @@ struct namedb_record {
        char type;
        int major;
        int minor;
-       int mode;
+       mode_t mode;
 };
 
 /**
- * busdb_close: close busdb database
+ * udevdb_close: close udev database
  */
-static void busdb_close(void)
+static void udevdb_close(void)
 {
-       if (busdb != NULL) {
-               tdb_close(busdb);
-               busdb = NULL;
+       if (udevdb != NULL) {
+               tdb_close(udevdb);
+               udevdb = NULL;
        }
 }
 
 /**
- * classdb_close: close classdb database
+ * udevdb_open: opens udev's database
+ * @method: database can either be in memory - UDEVDB_INTERNAL - or
+ *     written to a file with UDEVDB_DEFAULT.
  */
-static void classdb_close(void)
+static int udevdb_open(int method)
 {
-       if (classdb != NULL) {
-               tdb_close(classdb);
-               classdb = NULL;
+       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;
        }
-}
-
-/**
- * namedb_close: close name database
- */
-static void namedb_close(void)
-{
-       if (namedb != NULL) {
-               tdb_close(namedb);
-               namedb = NULL;
-       }
-}
-
-/**
- * busdb_open: open busdb's database
- */
-static int busdb_open(void)
-{
-       busdb = tdb_open(BUS_DB, 0, 0, O_RDWR | O_CREAT, 0644);
-       if (busdb == NULL)
-               return -1;
-       return 0;
-}
-
-/**
- * classdb_open: open classdb's database
- */
-static int classdb_open(void)
-{
-       classdb = tdb_open(CLASS_DB, 0, 0, O_RDWR | O_CREAT, 0644);
-       if (classdb == NULL)
-               return -1;
-       return 0;
-}
-
-/**
- * namedb_open: open name database
- */
-static int namedb_open(void)
-{
-       namedb = tdb_open(NAME_DB, 0, 0, O_RDWR | O_CREAT, 0644);
-       if (namedb == NULL)
-               return -1;
        return 0;
 }
 
@@ -126,9 +119,6 @@ static struct busdb_record *busdb_fetch(const char *bus, const char *id)
        if (strlen(bus) >= BUS_SIZE || strlen(id) >= ID_SIZE)
                return NULL;
 
-       if ((busdb_open()) != 0)
-               return NULL;
-
        memset(keystr, 0, (BUS_SIZE+ID_SIZE+2));
        strcpy(keystr, bus);
        strcat(keystr, UDEVDB_DEL);
@@ -137,17 +127,16 @@ static struct busdb_record *busdb_fetch(const char *bus, const char *id)
        key.dptr = (void *)keystr;
        key.dsize = strlen(keystr) + 1;
 
-       data = tdb_fetch(busdb, key);
-       busdb_close();
+       data = tdb_fetch(udevdb, key);
        if (data.dptr == NULL || data.dsize == 0)
                return NULL;
-       
+
        rec = (struct busdb_record *)malloc(sizeof(struct busdb_record));
        if (rec == NULL) {
                free(data.dptr);
                return NULL;
        }
-       
+
        memcpy(rec, data.dptr, sizeof(struct busdb_record));
        free(data.dptr);
 
@@ -169,9 +158,6 @@ static struct classdb_record *classdb_fetch(const char *cls,
        if (strlen(cls) >= NAME_SIZE || strlen(cls_dev) >= NAME_SIZE)
                return NULL;
 
-       if ((classdb_open()) != 0)
-               return NULL;
-
        memset(keystr, 0, (NAME_SIZE+NAME_SIZE+2));
        strcpy(keystr, cls);
        strcat(keystr, UDEVDB_DEL);
@@ -180,8 +166,7 @@ static struct classdb_record *classdb_fetch(const char *cls,
        key.dptr = (void *)keystr;
        key.dsize = strlen(keystr) + 1;
 
-       data = tdb_fetch(classdb, key);
-       classdb_close();
+       data = tdb_fetch(udevdb, key);
        if (data.dptr == NULL || data.dsize == 0)
                return NULL;
        
@@ -197,6 +182,41 @@ static struct classdb_record *classdb_fetch(const char *cls,
        return rec;
 }
 
+static struct sysfsdb_record *sysfsdb_fetch(const char *path)
+{
+       TDB_DATA key, data;
+       char keystr[PATH_SIZE+2]; 
+       struct sysfsdb_record *rec = NULL;
+
+       if (strlen(path) >= PATH_SIZE)
+               return NULL;
+
+       memset(keystr, 0, sizeof(keystr));
+       strcpy(keystr, path);
+
+       dbg("keystr = %s", keystr);
+
+       key.dptr = (void *)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;
+       }
+       
+       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;
+}
+
 /**
  * namedb_fetch
  */
@@ -211,18 +231,13 @@ static struct namedb_record *namedb_fetch(const char *name)
        if (strlen(name) >= NAME_SIZE)
                return NULL;
 
-       if ((namedb_open()) != 0)
-               return NULL;
-
        memset(nm_keystr, 0, NAME_SIZE);
        strcpy(nm_keystr, name);
 
        key.dptr = (void *)nm_keystr;
        key.dsize = strlen(nm_keystr) + 1;
 
-       data = tdb_fetch(namedb, key);
-       namedb_close();
-
+       data = tdb_fetch(udevdb, key);
        if (data.dptr == NULL || data.dsize == 0)
                return NULL;
 
@@ -251,9 +266,6 @@ static int busdb_store(const struct udevice *dev)
        if (dev == NULL)
                return -1;
 
-       if ((retval = busdb_open()) != 0)
-               return -1;
-
        memset(keystr, 0, (BUS_SIZE+ID_SIZE+2));
        strcpy(keystr, dev->bus_name);
        strcat(keystr, UDEVDB_DEL);
@@ -267,9 +279,7 @@ static int busdb_store(const struct udevice *dev)
        data.dptr = (void *) &rec;
        data.dsize = sizeof(rec);
        
-       retval = tdb_store(busdb, key, data, TDB_REPLACE); 
-
-       busdb_close();
+       retval = tdb_store(udevdb, key, data, TDB_REPLACE); 
        return retval;
 }
 
@@ -286,9 +296,6 @@ static int classdb_store(const struct udevice *dev)
        if (dev == NULL)
                return -1;
 
-       if ((retval = classdb_open()) != 0)
-               return -1;
-
        memset(keystr, 0, (NAME_SIZE+NAME_SIZE+2));
        strcpy(keystr, dev->class_name);
        strcat(keystr, UDEVDB_DEL);
@@ -302,9 +309,33 @@ static int classdb_store(const struct udevice *dev)
        data.dptr = (void *) &rec;
        data.dsize = sizeof(rec);
        
-       retval = tdb_store(classdb, key, data, TDB_REPLACE); 
+       retval = tdb_store(udevdb, key, data, TDB_REPLACE); 
+       return retval;
+}
 
-       classdb_close();
+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);
+       dbg("keystr = %s", keystr);
+
+       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;
 }
 
@@ -321,16 +352,13 @@ static int namedb_store(const struct udevice *dev)
        if (dev == NULL)
                return -1;
 
-       if ((retval = namedb_open()) != 0)
-               return -1;
-
        memset(keystr, 0, NAME_SIZE);
        strcpy(keystr, dev->name);
 
        key.dptr = (void *)keystr;
        key.dsize = strlen(keystr) + 1;
        
-       strcpy(rec.sysfs_path, dev->sysfs_path);
+       strcpy(rec.sysfs_dev_path, dev->sysfs_dev_path);
        strcpy(rec.bus, dev->bus_name);
        strcpy(rec.id, dev->bus_id);
        strcpy(rec.class_dev_name, dev->class_dev_name);
@@ -344,9 +372,7 @@ static int namedb_store(const struct udevice *dev)
        data.dptr = (void *) &rec;
        data.dsize = sizeof(rec);
        
-       retval = tdb_store(namedb, key, data, TDB_REPLACE); 
-
-       namedb_close();
+       retval = tdb_store(udevdb, key, data, TDB_REPLACE); 
        return retval;
 }
 
@@ -364,9 +390,6 @@ static int busdb_delete(const char *bus, const char *id)
        if (strlen(bus) >= BUS_SIZE || strlen(id) >= ID_SIZE)
                return -1;
 
-       if ((busdb_open()) != 0)
-               return -1;
-
        memset(keystr, 0, (BUS_SIZE+ID_SIZE+2));
        strcpy(keystr, bus);
        strcat(keystr, UDEVDB_DEL);
@@ -375,9 +398,7 @@ static int busdb_delete(const char *bus, const char *id)
        key.dptr = (void *)keystr;
        key.dsize = strlen(keystr) + 1;
 
-       retval = tdb_delete(busdb, key);
-       busdb_close();
-       
+       retval = tdb_delete(udevdb, key);
        return retval;
 }
 
@@ -395,9 +416,6 @@ static int classdb_delete(const char *cls, const char *cls_dev)
        if (strlen(cls) >= NAME_SIZE || strlen(cls_dev) >= NAME_SIZE)
                return -1;
 
-       if ((classdb_open()) != 0)
-               return -1;
-
        memset(keystr, 0, (NAME_SIZE+NAME_SIZE+2));
        strcpy(keystr, cls);
        strcat(keystr, UDEVDB_DEL);
@@ -406,9 +424,7 @@ static int classdb_delete(const char *cls, const char *cls_dev)
        key.dptr = (void *)keystr;
        key.dsize = strlen(keystr) + 1;
 
-       retval = tdb_delete(classdb, key);
-       classdb_close();
-       
+       retval = tdb_delete(udevdb, key);
        return retval;
 }
 
@@ -426,47 +442,13 @@ static int namedb_delete(const char *name)
        if (strlen(name) >= NAME_SIZE)
                return -1;
 
-       if ((namedb_open()) != 0)
-               return -1;
-
        memset(keystr, 0, NAME_SIZE);
        strcpy(keystr, name);
 
        key.dptr = (void *)keystr;
        key.dsize = strlen(keystr) + 1;
 
-       retval = tdb_delete(namedb, key);
-       namedb_close();
-
-       return retval;
-}
-
-/**
- * namedb_exists
- */
-static int namedb_exists(const char *name)
-{
-       TDB_DATA key;
-       char keystr[NAME_SIZE]; 
-       int retval = 0;
-
-       if (name == NULL)
-               return retval; 
-       if (strlen(name) >= NAME_SIZE)
-               return retval;
-
-       if ((namedb_open()) != 0)
-               return retval;
-
-       memset(keystr, 0, NAME_SIZE);
-       strcpy(keystr, name);
-
-       key.dptr = (void *)keystr;
-       key.dsize = strlen(keystr) + 1;
-
-       retval = tdb_exists(namedb, key);
-       namedb_close();
-
+       retval = tdb_delete(udevdb, key);
        return retval;
 }
 
@@ -493,18 +475,41 @@ int udevdb_delete_udevice(const char *name)
 }
 
 /**
- * udevdb_add_udevice: adds udevice to database
+ * udevdb_add_device: adds class device to database
  */
-int udevdb_add_udevice(const struct udevice *dev)
+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)
 {
-       if (dev == NULL) 
-               return -1;
+       struct udevice dbdev;
+
+       if (class_dev == NULL)
+               return -ENODEV;
 
-       if ((busdb_store(dev)) != 0)
+       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);
+       }
+       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");
+       strcpy(dbdev.bus_name, "unknown");
+       if (class_dev->driver != NULL)
+               strncpy(dbdev.driver, class_dev->driver->name, NAME_SIZE);
+       else
+               strcpy(dbdev.driver, "unknown");
+       dbdev.type = type;
+       dbdev.major = major;
+       dbdev.minor = minor;
+       dbdev.mode = mode;
+       
+       if ((busdb_store(&dbdev)) != 0)
+               return -1;
+       if ((classdb_store(&dbdev)) != 0)
                return -1;
-       if ((classdb_store(dev)) != 0)
+       if ((sysfs_store(device, &dbdev)) != 0)
                return -1;
-       if ((namedb_store(dev)) != 0)
+       if ((namedb_store(&dbdev)) != 0)
                return -1;
 
        return 0;
@@ -532,7 +537,7 @@ struct udevice *udevdb_get_udevice(const char *name)
        }
 
        strcpy(dev->name, name);
-       strcpy(dev->sysfs_path, nrec->sysfs_path);
+       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_name, nrec->bus);
@@ -589,3 +594,43 @@ struct udevice *udevdb_get_udevice_by_class(const char *cls,
 
        return dev;
 }
+
+
+char *udevdb_get_udevice_by_sysfs(const char *path)
+{
+       struct sysfsdb_record *crec = NULL;
+//     struct udevice *dev = NULL;
+
+       if (path == NULL)
+               return NULL;
+
+       crec = sysfsdb_fetch(path);
+       if (crec == NULL)
+               return NULL;
+
+       // FIXME leak!!!
+       return crec->name;
+//     dev = udevdb_get_udevice(crec->name);
+//     free(crec);
+//
+//     return dev;
+}
+
+/**
+ * udevdb_exit: closes database
+ */
+void udevdb_exit(void)
+{
+       udevdb_close();
+}
+
+/**
+ * udevdb_init: initializes database
+ */
+int udevdb_init(int init_flag)
+{
+       if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
+               return -1;
+
+       return udevdb_open(init_flag);
+}