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 5afde0c8de03b531a63e30a006a904530de80ff0..20ed648d06d0a0e3e75983b7e50ad69dd278f2ab 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 c4d65e9c49243468619c52bca6d63a698e300a30..629cadbc474598fc5f0d30b20b8fcd9001ebcba6 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 591e93058a00a2ab5348de7663735e065a5bf8b6..4d5881deae99c4f47f02e2a0dc2a48225ebd947d 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 b633807389d281d35002753b8e3631d84be861db..94456749a8f596577744ae18f52ba75d74307cb2 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 98886763c43549c135a7ef346fd927f55a612a7c..130c714301935b187a6350d32a53e12d1d44693d 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 045035d44e550b0c39f21c64eb4f3aa6c5d5f809..8ad79644af9306fe6bd22b5628171f7ed403c0d8 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 78b6747043d97b3ab7809e6ca31dd78b730c9fe6..33183c8d2a8647b7d6d4437ee79eb06f0b1c8703 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 557513afcc3ba454f8856fb3e8397cbaf51e0f25..4719cab5b8e7fe2613f54172b73e83a9fafc07f4 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 22d261fccb215424209dbdbabcb11f23e0f88f20..654118e5a935ffc86edfe688cc0671f9fcf88de4 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 a97f09c3e61db83eb879d1cb829c566e29ae919b..714a69c38490070461af37d8f49a13ffa7318358 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 2b43691f3958a0fd5f6e5b5f58dc8ec968e97688..7c6e3f9178b467a0ae85f5578ff32d9636992763 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 19a3dbb10e1979137567e356da1842fd51b7728e..3dd9109b8b5a390043660ef0ca996ab959dd2d82 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++;
        }