chiark / gitweb /
udev_device_init() remove statically allocated device support
authorKay Sievers <kay.sievers@vrfy.org>
Mon, 1 Sep 2008 18:59:09 +0000 (20:59 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Mon, 1 Sep 2008 18:59:09 +0000 (20:59 +0200)
12 files changed:
TODO
udev/lib/libudev-device.c
udev/test-udev.c
udev/udev.h
udev/udev_device.c
udev/udev_device_event.c
udev/udev_node.c
udev/udev_rules.c
udev/udevd.c
udev/udevinfo.c
udev/udevtest.c
udev/udevtrigger.c

diff --git a/TODO b/TODO
index 5afde0c..20ed648 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,6 +1,5 @@
 These things would be nice to have:
   o get all distros to agree on a default set of rules
-  o fix (non important) memleak in udevinfo at udev_device_init() loop
   o rework rules to a match-action list, instead of a rules array
 
 These things will change in future udev versions:
index c4d65e9..629cadb 100644 (file)
@@ -101,7 +101,7 @@ struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *
        if (udev_device == NULL)
                return NULL;
 
-       udevice = udev_device_init(NULL);
+       udevice = udev_device_init();
        if (udevice == NULL) {
                free(udev_device);
                return NULL;
index 591e930..4d5881d 100644 (file)
@@ -29,6 +29,7 @@
 #include <signal.h>
 #include <unistd.h>
 #include <syslog.h>
+#include <grp.h>
 
 #include "udev.h"
 #include "udev_rules.h"
@@ -138,7 +139,7 @@ int main(int argc, char *argv[], char *envp[])
                goto fail;
        }
 
-       udev = udev_device_init(NULL);
+       udev = udev_device_init();
        if (udev == NULL)
                goto fail;
 
@@ -171,6 +172,7 @@ fail:
 
 exit:
        logging_close();
+       endgrent();
        if (retval != 0)
                return 1;
        return 0;
index b633807..9445674 100644 (file)
@@ -101,7 +101,7 @@ extern int udev_run;
 extern void udev_config_init(void);
 
 /* udev_device.c */
-extern struct udevice *udev_device_init(struct udevice *udev);
+extern struct udevice *udev_device_init(void);
 extern void udev_device_cleanup(struct udevice *udev);
 extern dev_t udev_device_get_devt(struct udevice *udev);
 
index 9888676..130c714 100644 (file)
 #include "udev_rules.h"
 
 
-struct udevice *udev_device_init(struct udevice *udev)
+struct udevice *udev_device_init(void)
 {
-       if (udev == NULL)
-               udev = malloc(sizeof(struct udevice));
+       struct udevice *udev;
+
+       udev = malloc(sizeof(struct udevice));
        if (udev == NULL)
                return NULL;
        memset(udev, 0x00, sizeof(struct udevice));
@@ -55,12 +56,13 @@ struct udevice *udev_device_init(struct udevice *udev)
        strcpy(udev->group, "root");
 
        udev->event_timeout = -1;
-
        return udev;
 }
 
 void udev_device_cleanup(struct udevice *udev)
 {
+       if (udev == NULL)
+               return;
        name_list_cleanup(&udev->symlink_list);
        name_list_cleanup(&udev->run_list);
        name_list_cleanup(&udev->env_list);
index 045035d..8ad7964 100644 (file)
@@ -146,7 +146,7 @@ int udev_device_event(struct udev_rules *rules, struct udevice *udev)
                }
 
                /* read current database entry; cleanup, if it is known device */
-               udev_old = udev_device_init(NULL);
+               udev_old = udev_device_init();
                if (udev_old != NULL) {
                        udev_old->test_run = udev->test_run;
                        if (udev_db_get_device(udev_old, udev->dev->devpath) == 0) {
index 78b6747..33183c8 100644 (file)
@@ -234,7 +234,7 @@ static int update_link(struct udevice *udev, const char *name)
                }
 
                /* another device, read priority from database */
-               udev_db = udev_device_init(NULL);
+               udev_db = udev_device_init();
                if (udev_db == NULL)
                        continue;
                if (udev_db_get_device(udev_db, device->name) == 0) {
index 557513a..4719cab 100644 (file)
@@ -424,7 +424,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\n", dev_parent->devpath);
-               udev_parent = udev_device_init(NULL);
+               udev_parent = udev_device_init();
                if (udev_parent == NULL)
                        return -1;
                /* import the udev_db of the parent */
@@ -883,7 +883,7 @@ found:
                                        struct udevice *udev_parent;
 
                                        dbg("found parent '%s', get the node name\n", dev_parent->devpath);
-                                       udev_parent = udev_device_init(NULL);
+                                       udev_parent = udev_device_init();
                                        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) {
index 22d261f..654118e 100644 (file)
@@ -126,7 +126,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(NULL);
+       udev = udev_device_init();
        if (udev == NULL)
                return -1;
        strlcpy(udev->action, msg->action, sizeof(udev->action));
index a97f09c..714a69c 100644 (file)
@@ -157,7 +157,7 @@ static void export_db(void) {
        list_for_each_entry(name_loop, &name_list, node) {
                struct udevice *udev_db;
 
-               udev_db = udev_device_init(NULL);
+               udev_db = udev_device_init();
                if (udev_db == NULL)
                        continue;
                if (udev_db_get_device(udev_db, name_loop->name) == 0)
@@ -168,7 +168,7 @@ static void export_db(void) {
        name_list_cleanup(&name_list);
 }
 
-static int lookup_device_by_name(struct udevice *udev, const char *name)
+static int lookup_device_by_name(struct udevice **udev, const char *name)
 {
        LIST_HEAD(name_list);
        int count;
@@ -183,26 +183,32 @@ static int lookup_device_by_name(struct udevice *udev, const char *name)
 
        /* select the device that seems to match */
        list_for_each_entry(device, &name_list, node) {
+               struct udevice *udev_loop;
                char filename[PATH_SIZE];
                struct stat statbuf;
 
-               udev_device_init(udev);
-               if (udev_db_get_device(udev, device->name) != 0)
-                       continue;
+               udev_loop = udev_device_init();
+               if (udev_loop == NULL)
+                       break;
+               if (udev_db_get_device(udev_loop, device->name) != 0)
+                       goto next;
                info("found db entry '%s'\n", device->name);
 
-               /* make sure, we don't get a link of a differnt device */
+               /* make sure, we don't get a link of a different device */
                strlcpy(filename, udev_root, sizeof(filename));
                strlcat(filename, "/", sizeof(filename));
                strlcat(filename, name, sizeof(filename));
                if (stat(filename, &statbuf) != 0)
-                       continue;
-               if (major(udev->devt) > 0 && udev->devt != statbuf.st_rdev) {
-                       info("skip '%s', dev_t doesn't match\n", udev->name);
-                       continue;
+                       goto next;
+               if (major(udev_loop->devt) > 0 && udev_loop->devt != statbuf.st_rdev) {
+                       info("skip '%s', dev_t doesn't match\n", udev_loop->name);
+                       goto next;
                }
                rc = 0;
+               *udev = udev_loop;
                break;
+next:
+               udev_device_cleanup(udev_loop);
        }
 out:
        name_list_cleanup(&name_list);
@@ -231,7 +237,7 @@ static int stat_device(const char *name, int export, const char *prefix)
 int udevinfo(int argc, char *argv[], char *envp[])
 {
        int option;
-       struct udevice *udev;
+       struct udevice *udev = NULL;
        int root = 0;
        int export = 0;
        const char *export_prefix = NULL;
@@ -277,12 +283,6 @@ int udevinfo(int argc, char *argv[], char *envp[])
        udev_config_init();
        sysfs_init();
 
-       udev = udev_device_init(NULL);
-       if (udev == NULL) {
-               rc = 1;
-               goto exit;
-       }
-
        while (1) {
                option = getopt_long(argc, argv, "aed:n:p:q:rxPVh", options, NULL);
                if (option == -1)
@@ -408,13 +408,18 @@ int udevinfo(int argc, char *argv[], char *envp[])
        case ACTION_QUERY:
                /* needs devpath or node/symlink name for query */
                if (path[0] != '\0') {
+                       udev = udev_device_init();
+                       if (udev == NULL) {
+                               rc = 1;
+                               goto exit;
+                       }
                        if (udev_db_get_device(udev, path) != 0) {
                                fprintf(stderr, "no record for '%s' in database\n", path);
                                rc = 3;
                                goto exit;
                        }
                } else if (name[0] != '\0') {
-                       if (lookup_device_by_name(udev, name) != 0) {
+                       if (lookup_device_by_name(&udev, name) != 0) {
                                fprintf(stderr, "node name not found\n");
                                rc = 4;
                                goto exit;
@@ -465,7 +470,7 @@ int udevinfo(int argc, char *argv[], char *envp[])
                                goto exit;
                        }
                } else if (name[0] != '\0') {
-                       if (lookup_device_by_name(udev, name) != 0) {
+                       if (lookup_device_by_name(&udev, name) != 0) {
                                fprintf(stderr, "node name not found\n");
                                rc = 4;
                                goto exit;
index 2b43691..7c6e3f9 100644 (file)
@@ -157,7 +157,7 @@ int udevtest(int argc, char *argv[], char *envp[])
                goto exit;
        }
 
-       udev = udev_device_init(NULL);
+       udev = udev_device_init();
        if (udev == NULL) {
                fprintf(stderr, "error initializing device\n");
                rc = 3;
index 19a3dbb..3dd9109 100644 (file)
@@ -122,7 +122,7 @@ static void trigger_uevent(const char *devpath, const char *action)
 
 static int pass_to_socket(const char *devpath, const char *action, const char *env)
 {
-       struct udevice udev;
+       struct udevice *udev;
        struct name_entry *name_loop;
        char buf[4096];
        size_t bufpos = 0;
@@ -136,8 +136,10 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e
        if (verbose)
                printf("%s\n", devpath);
 
-       udev_device_init(&udev);
-       udev_db_get_device(&udev, devpath);
+       udev = udev_device_init();
+       if (udev == NULL)
+               return -1;
+       udev_db_get_device(udev, devpath);
 
        /* add header */
        bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
@@ -173,7 +175,7 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e
 
        /* add symlinks and node name */
        path[0] = '\0';
-       list_for_each_entry(name_loop, &udev.symlink_list, node) {
+       list_for_each_entry(name_loop, &udev->symlink_list, node) {
                strlcat(path, udev_root, sizeof(path));
                strlcat(path, "/", sizeof(path));
                strlcat(path, name_loop->name, sizeof(path));
@@ -184,10 +186,10 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e
                bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
                bufpos++;
        }
-       if (udev.name[0] != '\0') {
+       if (udev->name[0] != '\0') {
                strlcpy(path, udev_root, sizeof(path));
                strlcat(path, "/", sizeof(path));
-               strlcat(path, udev.name, sizeof(path));
+               strlcat(path, udev->name, sizeof(path));
                bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path);
                bufpos++;
        }
@@ -222,7 +224,7 @@ static int pass_to_socket(const char *devpath, const char *action, const char *e
        }
 
        /* add keys from database */
-       list_for_each_entry(name_loop, &udev.env_list, node) {
+       list_for_each_entry(name_loop, &udev->env_list, node) {
                bufpos += strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1);
                bufpos++;
        }