[Asterisk-code-review] loader: Add support for built-in modules. (asterisk[master])

Corey Farrell asteriskteam at digium.com
Wed Jan 17 00:44:17 CST 2018


Corey Farrell has uploaded this change for review. ( https://gerrit.asterisk.org/7988


Change subject: loader: Add support for built-in modules.
......................................................................

loader: Add support for built-in modules.

* Add SRC_EMBEDDED variable to main/Makefile.  Built-in module sources
  must be listed in this variable to ensure they get the correct CFLAGS.

Change-Id: I920852bc17513a9c2627061a4ad40511e3a20499
---
M main/Makefile
M main/loader.c
2 files changed, 49 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/88/7988/1

diff --git a/main/Makefile b/main/Makefile
index c724e20..b148b6f 100644
--- a/main/Makefile
+++ b/main/Makefile
@@ -17,6 +17,11 @@
 
 include $(ASTTOPDIR)/Makefile.moddir_rules
 
+# Can the MODSRC list be automated without needing built-in modules to be
+# in a different directory?  Would a different directory be better?
+MOD_SRC:=
+MOD_OBJS:=$(sort $(MOD_SRC:.c=.o))
+
 # Must include the extra ast_expr2.c, ast_expr2f.c, in case they need to be regenerated (because to force regeneration, we delete them)
 SRC:=$(wildcard *.c) ast_expr2.c ast_expr2f.c
 ifeq ($(AST_ASTERISKSSL),yes)
@@ -25,7 +30,7 @@
 ifeq ($(PJPROJECT_BUNDLED),yes)
 SRC:=$(filter-out libasteriskpj.c,$(SRC))
 endif
-OBJSFILTER=fskmodem_int.o fskmodem_float.o cygload.o buildinfo.o
+OBJSFILTER:=$(MOD_OBJS) fskmodem_int.o fskmodem_float.o cygload.o buildinfo.o
 OBJS=$(filter-out $(OBJSFILTER),$(SRC:.c=.o))
 
 # we need to link in the objects statically, not as a library, because
@@ -178,6 +183,7 @@
 endif
 
 $(OBJS): _ASTCFLAGS+=-DAST_MODULE=\"core\" -DAST_IN_CORE
+$(MOD_OBJS): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,$*)
 
 libasteriskssl.o: _ASTCFLAGS+=$(OPENSSL_INCLUDE)
 
@@ -311,10 +317,10 @@
 
 tcptls.o: _ASTCFLAGS+=$(OPENSSL_INCLUDE) -Wno-deprecated-declarations
 
-$(MAIN_TGT): $(OBJS) $(ASTSSL_LIB) $(ASTPJ_LIB) $(LIBEDIT_OBJ)
+$(MAIN_TGT): $(OBJS) $(MOD_OBJS) $(ASTSSL_LIB) $(ASTPJ_LIB) $(LIBEDIT_OBJ)
 	@$(CC) -c -o buildinfo.o $(_ASTCFLAGS) buildinfo.c $(ASTCFLAGS)
-	$(ECHO_PREFIX) echo "   [LD] $(OBJS) $(LIBEDIT_OBJ) -> $@"
-	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(ASTLINK) $(_ASTLDFLAGS) $(ASTLDFLAGS) $(OBJS) $(ASTSSL_LDLIBS) $(ASTPJ_LDLIBS) $(LIBEDIT_OBJ) buildinfo.o $(AST_LIBS) $(GMIMELDFLAGS) $(LIBEDIT_LIB)
+	$(ECHO_PREFIX) echo "   [LD] $(OBJS) $(MOD_OBJS) $(LIBEDIT_OBJ) -> $@"
+	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(ASTLINK) $(_ASTLDFLAGS) $(ASTLDFLAGS) $(OBJS) $(MOD_OBJS) $(ASTSSL_LDLIBS) $(ASTPJ_LDLIBS) $(LIBEDIT_OBJ) buildinfo.o $(AST_LIBS) $(GMIMELDFLAGS) $(LIBEDIT_LIB)
 
 ifeq ($(GNU_LD),1)
 $(MAIN_TGT): asterisk.exports
diff --git a/main/loader.c b/main/loader.c
index 14de8cd..68d8355 100644
--- a/main/loader.c
+++ b/main/loader.c
@@ -112,6 +112,9 @@
 
 AST_VECTOR(module_vector, struct ast_module *);
 
+/* Built-in module registrations need special handling at startup*/
+static unsigned int loader_ready;
+
 /*!
  * \brief Internal flag to indicate all modules have been initially loaded.
  */
@@ -149,6 +152,8 @@
 		unsigned int declined:1;
 		/*! This module is being held open until it's time to shutdown. */
 		unsigned int keepuntilshutdown:1;
+		/*! The module is built-in. */
+		unsigned int builtin:1;
 	} flags;
 	AST_DLLIST_ENTRY(ast_module) entry;
 	char resource[0];
@@ -406,6 +411,13 @@
 	return mod->info->name;
 }
 
+/*
+ * module_list is cleared by its constructor possibly after
+ * we start accumulating built-in modules, so we need to
+ * use another list (without the lock) to accumulate them.
+ */
+static struct module_list builtin_module_list;
+
 struct loadupdate {
 	int (*updater)(void);
 	AST_LIST_ENTRY(loadupdate) entry;
@@ -444,6 +456,22 @@
 void ast_module_register(const struct ast_module_info *info)
 {
 	struct ast_module *mod;
+
+	if (!loader_ready) {
+		mod = ast_calloc(1, sizeof(*mod) + strlen(info->name) + 1);
+		if (!mod) {
+			/* We haven't even reached main() yet, if we can't
+			 * allocate memory at this point just give up. */
+			exit(2);
+		}
+		strcpy(mod->resource, info->name);
+		AST_DLLIST_INSERT_TAIL(&builtin_module_list, mod, entry);
+		mod->info = info;
+		mod->flags.builtin = 1;
+
+		/* ast_module_register for built-in modules is run again during module preload. */
+		return;
+	}
 
 	/*
 	 * This lock protects resource_being_loaded as well as the module
@@ -1732,6 +1760,17 @@
 
 	AST_DLLIST_LOCK(&module_list);
 
+	/*
+	 * All built-in modules have registered the first time, now it's time to complete
+	 * the registration and add them to the priority list.
+	 */
+	loader_ready = 1;
+
+	while ((resource_being_loaded = AST_DLLIST_REMOVE_HEAD(&builtin_module_list, entry))) {
+		/* ast_module_register doesn't finish when first run by built-in modules. */
+		ast_module_register(resource_being_loaded->info);
+	}
+
 	cfg = ast_config_load2(AST_MODULE_CONFIG, "" /* core, can't reload */, config_flags);
 	if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
 		ast_log(LOG_WARNING, "No '%s' found, no modules will be loaded.\n", AST_MODULE_CONFIG);

-- 
To view, visit https://gerrit.asterisk.org/7988
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I920852bc17513a9c2627061a4ad40511e3a20499
Gerrit-Change-Number: 7988
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180117/8156b0f8/attachment-0001.html>


More information about the asterisk-code-review mailing list