[svn-commits] kpfleming: branch 1.4 r62986 - /branches/1.4/main/loader.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Thu May 3 09:38:56 MST 2007


Author: kpfleming
Date: Thu May  3 11:38:56 2007
New Revision: 62986

URL: http://svn.digium.com/view/asterisk?view=rev&rev=62986
Log:
improve loader a bit, by avoiding trying to initialize embedded modules twice and avoiding trying to load modules from disk when they have been loaded already during the 'preload' pass (reported by blitzrage on IRC, patch by me)

Modified:
    branches/1.4/main/loader.c

Modified: branches/1.4/main/loader.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/loader.c?view=diff&rev=62986&r1=62985&r2=62986
==============================================================================
--- branches/1.4/main/loader.c (original)
+++ branches/1.4/main/loader.c Thu May  3 11:38:56 2007
@@ -80,17 +80,15 @@
 				      since they are here before we dlopen() any
 				   */
 
-enum flags {
-	FLAG_RUNNING = (1 << 1),		/* module successfully initialized */
-	FLAG_DECLINED = (1 << 2),		/* module declined to initialize */
-};
-
 struct ast_module {
 	const struct ast_module_info *info;
 	void *lib;					/* the shared lib, or NULL if embedded */
 	int usecount;					/* the number of 'users' currently in this module */
 	struct module_user_list users;			/* the list of users in the module */
-	unsigned int flags;				/* flags for this module */
+	struct {
+		unsigned int running:1;
+		unsigned int declined:1;
+	} flags;
 	AST_LIST_ENTRY(ast_module) entry;
 	char resource[0];
 };
@@ -442,7 +440,7 @@
 		return 0;
 	}
 
-	if (!ast_test_flag(mod, FLAG_RUNNING | FLAG_DECLINED))
+	if (!(mod->flags.running || mod->flags.declined))
 		error = 1;
 
 	if (!mod->lib) {
@@ -475,7 +473,7 @@
 	}
 
 	if (!error)
-		ast_clear_flag(mod, FLAG_RUNNING | FLAG_DECLINED);
+		mod->flags.running = mod->flags.declined = 0;
 
 	AST_LIST_UNLOCK(&module_list);
 
@@ -552,7 +550,7 @@
 		if (name && resource_name_match(name, cur->resource))
 			continue;
 
-		if (!ast_test_flag(cur, FLAG_RUNNING | FLAG_DECLINED))
+		if (!(cur->flags.running || cur->flags.declined))
 			continue;
 
 		if (!info->reload) {	/* cannot be reloaded */
@@ -600,7 +598,7 @@
 	char tmp[256];
 
 	if ((mod = find_resource(resource_name, 0))) {
-		if (ast_test_flag(mod, FLAG_RUNNING)) {
+		if (mod->flags.running) {
 			ast_log(LOG_WARNING, "Module '%s' already exists.\n", resource_name);
 			return AST_MODULE_LOAD_DECLINE;
 		}
@@ -631,7 +629,7 @@
 		return AST_MODULE_LOAD_DECLINE;
 	}
 
-	ast_clear_flag(mod, FLAG_DECLINED);
+	mod->flags.declined = 0;
 
 	if (mod->info->load)
 		res = mod->info->load();
@@ -648,12 +646,12 @@
 				ast_verbose(VERBOSE_PREFIX_1 "Loaded %s => (%s)\n", resource_name, mod->info->description);
 		}
 
-		ast_set_flag(mod, FLAG_RUNNING);
+		mod->flags.running = 1;
 
 		ast_update_use_count();
 		break;
 	case AST_MODULE_LOAD_DECLINE:
-		ast_set_flag(mod, FLAG_DECLINED);
+		mod->flags.declined = 1;
 		break;
 	case AST_MODULE_LOAD_FAILURE:
 		break;
@@ -719,17 +717,14 @@
 	if (option_verbose)
 		ast_verbose("Asterisk Dynamic Loader Starting:\n");
 
-	AST_LIST_TRAVERSE(&module_list, mod, entry) {
-		if (option_debug > 1)
-			ast_log(LOG_DEBUG, "Embedded module found: %s\n", mod->resource);
-	}
+	AST_LIST_HEAD_INIT_NOLOCK(&load_order);
+
+	AST_LIST_LOCK(&module_list);
 
 	if (!(cfg = ast_config_load(AST_MODULE_CONFIG))) {
 		ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);
-		return 0;
-	}
-
-	AST_LIST_HEAD_INIT_NOLOCK(&load_order);
+		goto done;
+	}
 
 	/* first, find all the modules we have been explicitly requested to load */
 	for (v = ast_variable_browse(cfg, "modules"); v; v = v->next) {
@@ -739,9 +734,17 @@
 
 	/* check if 'autoload' is on */
 	if (!preload_only && ast_true(ast_variable_retrieve(cfg, "modules", "autoload"))) {
-		/* if so, first add all the embedded modules to the load order */
-		AST_LIST_TRAVERSE(&module_list, mod, entry)
+		/* if so, first add all the embedded modules that are not already running to the load order */
+		AST_LIST_TRAVERSE(&module_list, mod, entry) {
+			/* if it's not embedded, skip it */
+			if (mod->lib)
+				continue;
+
+			if (mod->flags.running)
+				continue;
+
 			order = add_to_load_order(mod->resource, &load_order);
+		}
 
 #if LOADABLE_MODULES
 		/* if we are allowed to load dynamic modules, scan the directory for
@@ -756,10 +759,14 @@
 					continue;
 
 				if (strcasecmp(dirent->d_name + ld - 3, ".so"))
-				    continue;
+					continue;
+
+				/* if there is already a module by this name in the module_list,
+				   skip this file */
+				if (find_resource(dirent->d_name, 0))
+					continue;
 
 				add_to_load_order(dirent->d_name, &load_order);
-
 			}
 
 			closedir(dir);
@@ -842,6 +849,8 @@
 		free(order);
 	}
 
+	AST_LIST_UNLOCK(&module_list);
+
 	return res;
 }
 



More information about the svn-commits mailing list