chiark / gitweb /
read list of devices from index, make index private to database
authorKay Sievers <kay.sievers@vrfy.org>
Sat, 17 Mar 2007 09:08:25 +0000 (10:08 +0100)
committerKay Sievers <kay.sievers@vrfy.org>
Sat, 17 Mar 2007 09:08:25 +0000 (10:08 +0100)
test-udev.c
udev.h
udev_db.c
udev_device.c
udev_node.c
udev_rules.c
udevd.c
udevinfo.c
udevstart.c
udevtest.c

index e40a4484367b1b6d35401cb8f63acb0a70b09f04..ba3601591146bc8f810c6a08234e46b3510083c7 100644 (file)
@@ -136,7 +136,7 @@ int main(int argc, char *argv[], char *envp[])
                goto fail;
        }
 
-       udev = udev_device_init();
+       udev = udev_device_init(NULL);
        if (udev == NULL)
                goto fail;
 
diff --git a/udev.h b/udev.h
index 31542243e2a4188a28be75d975b7ec57abb73097..16a107a33d8b89ff56acb3414b7b12a788be4800 100644 (file)
--- a/udev.h
+++ b/udev.h
@@ -99,7 +99,7 @@ extern int udev_run;
 extern void udev_config_init(void);
 
 /* udev_device.c */
-extern struct udevice *udev_device_init(void);
+extern struct udevice *udev_device_init(struct udevice *udev);
 extern void udev_device_cleanup(struct udevice *udev);
 extern int udev_device_event(struct udev_rules *rules, struct udevice *udev);
 extern dev_t udev_device_get_devt(struct udevice *udev);
@@ -127,6 +127,7 @@ extern int udev_db_add_device(struct udevice *dev);
 extern int udev_db_delete_device(struct udevice *dev);
 extern int udev_db_get_device(struct udevice *udev, const char *devpath);
 extern int udev_db_lookup_name(const char *name, char *devpath, size_t len);
+extern int udev_db_get_devices_by_name(const char *name, struct list_head *name_list);
 extern int udev_db_get_all_entries(struct list_head *name_list);
 
 /* udev_utils.c */
index 40082abf03461a00580783c0055f7acf0f73df92..8c9e474ad402b6e380909477aa553a4cf1587782 100644 (file)
--- a/udev_db.c
+++ b/udev_db.c
@@ -77,6 +77,46 @@ static int name_index(const char *devpath, const char *name, int add)
        return 0;
 }
 
+int udev_db_get_devices_by_name(const char *name, struct list_head *name_list)
+{
+       char dirname[PATH_MAX];
+       size_t start;
+       DIR *dir;
+       int rc = 0;
+
+       strlcpy(dirname, udev_root, sizeof(dirname));
+       start = strlcat(dirname, "/"DB_NAME_INDEX_DIR"/", sizeof(dirname));
+       strlcat(dirname, name, sizeof(dirname));
+       path_encode(&dirname[start], sizeof(dirname) - start);
+
+       dir = opendir(dirname);
+       if (dir == NULL) {
+               info("no index directory '%s': %s", dirname, strerror(errno));
+               rc = -1;
+               goto out;
+       }
+
+       info("found index directory '%s'", dirname);
+       while (1) {
+               struct dirent *ent;
+               char device[PATH_SIZE];
+
+               ent = readdir(dir);
+               if (ent == NULL || ent->d_name[0] == '\0')
+                       break;
+               if (ent->d_name[0] == '.')
+                       continue;
+
+               strlcpy(device, ent->d_name, sizeof(device));
+               path_decode(device);
+               name_list_add(name_list, device, 0);
+               rc++;
+       }
+       closedir(dir);
+out:
+       return rc;
+}
+
 int udev_db_add_device(struct udevice *udev)
 {
        char filename[PATH_SIZE];
@@ -288,7 +328,7 @@ int udev_db_lookup_name(const char *name, char *devpath, size_t len)
 
                strlcpy(device, ent->d_name, sizeof(device));
                path_decode(device);
-               udev = udev_device_init();
+               udev = udev_device_init(NULL);
                if (udev == NULL)
                        break;
                if (udev_db_get_device(udev, device) == 0) {
index daf94a577264341c1be6c5cd51e648b2eac42351..4cd5526dea834c1a93dd820dac0338cb71a2b73d 100644 (file)
 #include "udev_rules.h"
 
 
-struct udevice *udev_device_init(void)
+struct udevice *udev_device_init(struct udevice *udev)
 {
-       struct udevice *udev;
-
-       udev = malloc(sizeof(struct udevice));
+       if (udev == NULL)
+               udev = malloc(sizeof(struct udevice));
        if (udev == NULL)
                return NULL;
        memset(udev, 0x00, sizeof(struct udevice));
@@ -162,8 +161,8 @@ int udev_device_event(struct udev_rules *rules, struct udevice *udev)
                        goto exit;
                }
 
-               /* read current database entry, we may need to cleanup */
-               udev_old = udev_device_init();
+               /* read current database entry; cleanup, if it is known device */
+               udev_old = udev_device_init(NULL);
                if (udev_old != NULL) {
                        if (udev_db_get_device(udev_old, udev->dev->devpath) == 0) {
                                info("device '%s' already in database, cleanup", udev->dev->devpath);
@@ -241,7 +240,7 @@ int udev_device_event(struct udev_rules *rules, struct udevice *udev)
        if (major(udev->devt) != 0 && strcmp(udev->action, "remove") == 0) {
                struct name_entry *name_loop;
 
-               /* import and delete database entry */
+               /* import database entry, and delete it */
                if (udev_db_get_device(udev, udev->dev->devpath) == 0) {
                        udev_db_delete_device(udev);
                        if (udev->ignore_remove) {
index ed323e94495fd71b1c31494a60751f3e988557a9..1558df849c097a407f537af27392a36d673e9e22 100644 (file)
@@ -275,7 +275,7 @@ void udev_node_remove_symlinks(struct udevice *udev)
                                struct udevice *old;
 
                                info("found overwritten symlink '%s' of '%s'", name_loop->name, devpath);
-                               old = udev_device_init();
+                               old = udev_device_init(NULL);
                                if (old != NULL) {
                                        if (udev_db_get_device(old, devpath) == 0) {
                                                char slink[PATH_SIZE];
index c0f4cd2077b1f643898eeb7e9e9a97cda5982413..ea02b8e9455763ae9346dfc96a2661ecc2f44b97 100644 (file)
@@ -236,7 +236,7 @@ static int import_parent_into_env(struct udevice *udev, const char *filter)
                struct name_entry *name_loop;
 
                dbg("found parent '%s', get the node name", dev_parent->devpath);
-               udev_parent = udev_device_init();
+               udev_parent = udev_device_init(NULL);
                if (udev_parent == NULL)
                        return -1;
                /* import the udev_db of the parent */
@@ -518,7 +518,7 @@ found:
                                        struct udevice *udev_parent;
 
                                        dbg("found parent '%s', get the node name", dev_parent->devpath);
-                                       udev_parent = udev_device_init();
+                                       udev_parent = udev_device_init(NULL);
                                        if (udev_parent != NULL) {
                                                /* lookup the name in the udev_db with the DEVPATH of the parent */
                                                if (udev_db_get_device(udev_parent, dev_parent->devpath) == 0) {
diff --git a/udevd.c b/udevd.c
index 4aad165d8a85c50f91a92344713e6dbda9b9ba76..8f3329ea635ab0f5d542f252a1d4b58e95708ad6 100644 (file)
--- a/udevd.c
+++ b/udevd.c
@@ -124,7 +124,7 @@ static int udev_event_process(struct udevd_uevent_msg *msg)
        for (i = 0; msg->envp[i]; i++)
                putenv(msg->envp[i]);
 
-       udev = udev_device_init();
+       udev = udev_device_init(NULL);
        if (udev == NULL)
                return -1;
        strlcpy(udev->action, msg->action, sizeof(udev->action));
index 3f47add87cd6f7197ec2d81dc5603bbf56564d3a..e892b1092e311687058b5e3c2ec4dd20c5f37bac 100644 (file)
@@ -167,7 +167,7 @@ static void export_db(void fnct(struct udevice *udev)) {
        list_for_each_entry(name_loop, &name_list, node) {
                struct udevice *udev_db;
 
-               udev_db = udev_device_init();
+               udev_db = udev_device_init(NULL);
                if (udev_db == NULL)
                        continue;
                if (udev_db_get_device(udev_db, name_loop->name) == 0)
@@ -177,6 +177,41 @@ static void export_db(void fnct(struct udevice *udev)) {
        name_list_cleanup(&name_list);
 }
 
+static int lookup_device_by_name(struct udevice *udev, const char *name)
+{
+       LIST_HEAD(name_list);
+       struct name_entry *device;
+       int rc  = -1;
+
+       if (udev_db_get_devices_by_name(name, &name_list) <= 0)
+               goto out;
+
+       /* select the device that matches the dev_t of name */
+       list_for_each_entry(device, &name_list, node) {
+               char filename[PATH_SIZE];
+               struct stat statbuf;
+
+               udev_device_init(udev);
+               if (udev_db_get_device(udev, device->name) != 0)
+                       continue;
+               info("found db entry '%s'", device->name);
+
+               strlcpy(filename, udev_root, sizeof(filename));
+               strlcat(filename, "/", sizeof(filename));
+               strlcat(filename, name, sizeof(filename));
+               if (stat(filename, &statbuf) != 0)
+                       continue;
+               if (statbuf.st_rdev == udev->devt) {
+                       info("found '%s', dev_t matches", udev->name);
+                       rc = 0;
+                       break;
+               }
+       }
+out:
+       name_list_cleanup(&name_list);
+       return rc;
+}
+
 int main(int argc, char *argv[], char *envp[])
 {
        int option;
@@ -220,7 +255,7 @@ int main(int argc, char *argv[], char *envp[])
        udev_config_init();
        sysfs_init();
 
-       udev = udev_device_init();
+       udev = udev_device_init(NULL);
        if (udev == NULL) {
                rc = 1;
                goto exit;
@@ -330,14 +365,11 @@ int main(int argc, char *argv[], char *envp[])
                                goto exit;
                        }
                } else if (name[0] != '\0') {
-                       char devpath[PATH_SIZE];
-
-                       if (udev_db_lookup_name(name, devpath, sizeof(devpath)) != 0) {
+                       if (lookup_device_by_name(udev, name) != 0) {
                                fprintf(stderr, "node name not found\n");
                                rc = 4;
                                goto exit;
                        }
-                       udev_db_get_device(udev, devpath);
                } else {
                        fprintf(stderr, "query needs --path or node --name specified\n");
                        rc = 4;
@@ -385,14 +417,12 @@ int main(int argc, char *argv[], char *envp[])
                                goto exit;
                        }
                } else if (name[0] != '\0') {
-                       char devpath[PATH_SIZE];
-
-                       if (udev_db_lookup_name(name, devpath, sizeof(devpath)) != 0) {
+                       if (lookup_device_by_name(udev, name) != 0) {
                                fprintf(stderr, "node name not found\n");
                                rc = 4;
                                goto exit;
                        }
-                       if (print_device_chain(devpath) != 0) {
+                       if (print_device_chain(udev->dev->devpath) != 0) {
                                fprintf(stderr, "device not found\n");
                                rc = 4;
                                goto exit;
index 6ea18272ea013a0788571dfa546e51c89ce7bc71..a381c411a80f437157ed38a622723f52ba531041 100644 (file)
@@ -120,7 +120,7 @@ static int add_device(const char *devpath)
        if (dev == NULL)
                return -1;
 
-       udev = udev_device_init();
+       udev = udev_device_init(NULL);
        if (udev == NULL)
                return -1;
 
index 73bf7dd1f8dde459575c11cc55a347cf7141ff49..bb889a70fde8eb6b0071c14903aabd2ce48e3eca 100644 (file)
@@ -97,7 +97,7 @@ int main(int argc, char *argv[], char *envp[])
                goto exit;
        }
 
-       udev = udev_device_init();
+       udev = udev_device_init(NULL);
        if (udev == NULL) {
                fprintf(stderr, "error initializing device\n");
                rc = 3;