[syslinux:elflink] module: Replace cur_module with module_current ()

syslinux-bot for Matt Fleming matt.fleming at intel.com
Wed Jan 23 07:09:11 PST 2013


Commit-ID:  7c42c59ebd9d33efe3a5f12542580a8c1379447d
Gitweb:     http://www.syslinux.org/commit/7c42c59ebd9d33efe3a5f12542580a8c1379447d
Author:     Matt Fleming <matt.fleming at intel.com>
AuthorDate: Wed, 23 Jan 2013 14:46:08 +0000
Committer:  Matt Fleming <matt.fleming at intel.com>
CommitDate: Wed, 23 Jan 2013 15:05:29 +0000

module: Replace cur_module with module_current()

It's easy for cur_module and prev_module to get out of sync with
reality (the actual module that is running), so add module_current()
which returns the module at the head of the module_list, i.e. the
module that was loaded most recently. Better still, by using the list
we don't have to do any kind of stacking of module pointers ourselves.

This fixes a bug where cur_module contained a stale pointer (the
module had actually been unloaded) but the pointer value had since
been reallocated for a new module in spawn_load(), meaning that the
following check,

    if (!strcmp(cur_module->name, module->name))

was always going to be true, even though *no* module was actually
loaded at this point as we were reloading ldlinux.c32 from
start_ldlinux(). This could have been fixed with a NULL-assignment
after module_unload(), but using the modules_head list to detect the
current module is much cleaner.

Note that the core module loaded in load_env32() is always on the
list, therefore module_current() will always return a valid pointer.

Signed-off-by: Matt Fleming <matt.fleming at intel.com>

---
 com32/include/sys/module.h        | 11 +++++++++++
 com32/lib/sys/module/elf_module.c |  2 +-
 com32/lib/sys/module/exec.c       | 11 +++--------
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/com32/include/sys/module.h b/com32/include/sys/module.h
index a18561a..8d14420 100644
--- a/com32/include/sys/module.h
+++ b/com32/include/sys/module.h
@@ -203,6 +203,17 @@ extern struct list_head modules_head;
 	list_for_each_entry_safe(m, n, &modules_head, list)
 
 /**
+ * module_current - return the module at the head of the module list.
+ */
+static inline struct elf_module *module_current(void)
+{
+	struct elf_module *head;
+
+	head = list_entry((&modules_head)->next, typeof(*head), list);
+	return head;
+}
+
+/**
  * modules_init - initialize the module subsystem.
  *
  * This function must be called before any module operation is to be performed.
diff --git a/com32/lib/sys/module/elf_module.c b/com32/lib/sys/module/elf_module.c
index e61480f..6a54027 100644
--- a/com32/lib/sys/module/elf_module.c
+++ b/com32/lib/sys/module/elf_module.c
@@ -534,7 +534,7 @@ int module_load(struct elf_module *module) {
 	CHECKED(res, prepare_dynlinking(module), error);
 	//printf("check... 4\n");
 
-	head =  list_entry((&modules_head)->next, typeof(*head), list);
+	head = module_current();
 
 	/* Find modules we need to load as dependencies */
 	if (module->str_table) {
diff --git a/com32/lib/sys/module/exec.c b/com32/lib/sys/module/exec.c
index 2b3a4c0..18c8306 100644
--- a/com32/lib/sys/module/exec.c
+++ b/com32/lib/sys/module/exec.c
@@ -137,8 +137,6 @@ int spawnl(const char *name, const char *arg, ...)
 }
 #endif
 
-struct elf_module *cur_module;
-
 /*
  * Load a module and runs its start function.
  *
@@ -161,7 +159,7 @@ int spawn_load(const char *name, int argc, char **argv)
 	struct elf_module *previous;
 	//malloc_tag_t prev_mem_tag;
 	struct elf_module *module = module_alloc(name);
-	struct elf_module *prev_module;
+	struct elf_module *cur_module;
 	int type;
 
 	dprintf("enter: name = %s", name);
@@ -176,11 +174,11 @@ int spawn_load(const char *name, int argc, char **argv)
 		}
 	}
 
+	cur_module = module_current();
 	if (!strcmp(cur_module->name, module->name)) {
 		dprintf("We is running this module %s already!", module->name);
 
 		module_unload(cur_module);
-		cur_module = NULL;
 	}
 
 	res = module_load(module);
@@ -188,11 +186,9 @@ int spawn_load(const char *name, int argc, char **argv)
 		goto out;
 
 	type = get_module_type(module);
-	prev_module = cur_module;
-	cur_module = module;
 
 	dprintf("type = %d, prev = %s, cur = %s",
-		type, prev_module->name, cur_module->name);
+		type, cur_module->name, module->name);
 
 	if(type==EXEC_MODULE)
 	{
@@ -220,7 +216,6 @@ int spawn_load(const char *name, int argc, char **argv)
 		// Restore the process context
 		__syslinux_current = previous;
 
-		cur_module = prev_module;
 		res = module_unload(module);
 
 		if (res != 0)


More information about the Syslinux-commits mailing list