From 44aff4cd6d74d230e4a97f8d59f780472b7cad6e Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Mon, 1 Sep 2008 20:59:09 +0200 Subject: [PATCH] udev_device_init() remove statically allocated device support --- TODO | 1 - udev/lib/libudev-device.c | 2 +- udev/test-udev.c | 4 +++- udev/udev.h | 2 +- udev/udev_device.c | 10 +++++---- udev/udev_device_event.c | 2 +- udev/udev_node.c | 2 +- udev/udev_rules.c | 4 ++-- udev/udevd.c | 2 +- udev/udevinfo.c | 43 ++++++++++++++++++++++----------------- udev/udevtest.c | 2 +- udev/udevtrigger.c | 16 ++++++++------- 12 files changed, 50 insertions(+), 40 deletions(-) diff --git a/TODO b/TODO index 5afde0c8..20ed648d 100644 --- 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: diff --git a/udev/lib/libudev-device.c b/udev/lib/libudev-device.c index c4d65e9c..629cadbc 100644 --- a/udev/lib/libudev-device.c +++ b/udev/lib/libudev-device.c @@ -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; diff --git a/udev/test-udev.c b/udev/test-udev.c index 591e9305..4d5881de 100644 --- a/udev/test-udev.c +++ b/udev/test-udev.c @@ -29,6 +29,7 @@ #include #include #include +#include #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; diff --git a/udev/udev.h b/udev/udev.h index b6338073..94456749 100644 --- a/udev/udev.h +++ b/udev/udev.h @@ -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); diff --git a/udev/udev_device.c b/udev/udev_device.c index 98886763..130c7143 100644 --- a/udev/udev_device.c +++ b/udev/udev_device.c @@ -34,10 +34,11 @@ #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); diff --git a/udev/udev_device_event.c b/udev/udev_device_event.c index 045035d4..8ad79644 100644 --- a/udev/udev_device_event.c +++ b/udev/udev_device_event.c @@ -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) { diff --git a/udev/udev_node.c b/udev/udev_node.c index 78b67470..33183c8d 100644 --- a/udev/udev_node.c +++ b/udev/udev_node.c @@ -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) { diff --git a/udev/udev_rules.c b/udev/udev_rules.c index 557513af..4719cab5 100644 --- a/udev/udev_rules.c +++ b/udev/udev_rules.c @@ -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) { diff --git a/udev/udevd.c b/udev/udevd.c index 22d261fc..654118e5 100644 --- a/udev/udevd.c +++ b/udev/udevd.c @@ -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)); diff --git a/udev/udevinfo.c b/udev/udevinfo.c index a97f09c3..714a69c3 100644 --- a/udev/udevinfo.c +++ b/udev/udevinfo.c @@ -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; diff --git a/udev/udevtest.c b/udev/udevtest.c index 2b43691f..7c6e3f91 100644 --- a/udev/udevtest.c +++ b/udev/udevtest.c @@ -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; diff --git a/udev/udevtrigger.c b/udev/udevtrigger.c index 19a3dbb1..3dd9109b 100644 --- a/udev/udevtrigger.c +++ b/udev/udevtrigger.c @@ -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++; } -- 2.39.5