[svn-commits] kpfleming: trunk r56092 - in /trunk: ./ apps/ build_tools/ cdr/ channels/ cod...

svn-commits at lists.digium.com svn-commits at lists.digium.com
Wed Feb 21 19:36:01 MST 2007


Author: kpfleming
Date: Wed Feb 21 20:36:00 2007
New Revision: 56092

URL: http://svn.digium.com/view/asterisk?view=rev&rev=56092
Log:
give embedded modules a helping hand by backing up and restoring their global variables when they are loaded and unloaded

Added:
    trunk/build_tools/make_linker_eo_script
      - copied unchanged from r56091, team/kpfleming/embedded-module-globals/build_tools/make_linker_eo_script
Modified:
    trunk/Makefile.moddir_rules
    trunk/Makefile.rules
    trunk/apps/   (props changed)
    trunk/cdr/   (props changed)
    trunk/channels/   (props changed)
    trunk/codecs/   (props changed)
    trunk/formats/   (props changed)
    trunk/funcs/   (props changed)
    trunk/include/asterisk/module.h
    trunk/main/loader.c
    trunk/pbx/   (props changed)
    trunk/res/   (props changed)

Modified: trunk/Makefile.moddir_rules
URL: http://svn.digium.com/view/asterisk/trunk/Makefile.moddir_rules?view=diff&rev=56092&r1=56091&r2=56092
==============================================================================
--- trunk/Makefile.moddir_rules (original)
+++ trunk/Makefile.moddir_rules Wed Feb 21 20:36:00 2007
@@ -34,11 +34,13 @@
 $(LOADABLE_MODS:%=%.so): LIBS+=$(foreach dep,$(MENUSELECT_DEPENDS_$*),$(value $(dep)_LIB))
 $(LOADABLE_MODS:%=%.so): ASTLDFLAGS+=$(foreach dep,$(MENUSELECT_DEPENDS_$*),$(value $(dep)_LDFLAGS))
 
+$(EMBEDDED_MODS:%=%.o): ASTCFLAGS+=-DEMBEDDED_MODULE=$*
+
 $(addsuffix .so,$(filter $(LOADABLE_MODS),$(C_MODS))): %.so: %.o
 $(addsuffix .so,$(filter $(LOADABLE_MODS),$(CC_MODS))): %.so: %.oo
 
-modules.link: $(addsuffix .o,$(filter $(EMBEDDED_MODS),$(C_MODS)))
-modules.link: $(addsuffix .oo,$(filter $(EMBEDDED_MODS),$(CC_MODS)))
+modules.link: $(addsuffix .eo,$(filter $(EMBEDDED_MODS),$(C_MODS)))
+modules.link: $(addsuffix .eoo,$(filter $(EMBEDDED_MODS),$(CC_MODS)))
 
 .PHONY: clean uninstall _all
 
@@ -64,11 +66,11 @@
 
 modules.link:
 	@rm -f $@
-	@for file in $(patsubst %,$(SUBDIR)/%,$(filter %.o,$^)); do echo "INPUT (../$${file})" >> $@; done
-	@for file in $(patsubst %,$(SUBDIR)/%,$(filter-out %.o,$^)); do echo "INPUT (../$${file})" >> $@; done
+	@for file in $(patsubst %,$(SUBDIR)/%,$(filter %.eo %.eoo,$^)); do echo "INPUT (../$${file})" >> $@; done
+	@for file in $(patsubst %,$(SUBDIR)/%,$(filter-out %.eo %.eoo,$^)); do echo "INPUT (../$${file})" >> $@; done
 
 clean::
-	rm -f *.so *.o *.oo
+	rm -f *.so *.o *.oo *.eo *.eoo
 	rm -f .*.o.d .*.oo.d
 	rm -f modules.link
 

Modified: trunk/Makefile.rules
URL: http://svn.digium.com/view/asterisk/trunk/Makefile.rules?view=diff&rev=56092&r1=56091&r2=56092
==============================================================================
--- trunk/Makefile.rules (original)
+++ trunk/Makefile.rules Wed Feb 21 20:36:00 2007
@@ -44,6 +44,22 @@
 	$(CMD_PREFIX) $(CC) -o $@ -c $< $(PTHREAD_CFLAGS) $(ASTCFLAGS)
 endif
 
+%.o: %.i
+	$(ECHO_PREFIX) echo "   [CC] $< -> $@"
+ifeq ($(AST_DEVMODE),yes)
+	$(CMD_PREFIX) $(CC) -o $@ -c $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) -MMD -MT $@ -MF .$(subst /,_,$@).d -MP
+else
+	$(CMD_PREFIX) $(CC) -o $@ -c $< $(PTHREAD_CFLAGS) $(ASTCFLAGS)
+endif
+
+%.i: %.c
+	$(ECHO_PREFIX) echo "   [CC] $< -> $@"
+ifeq ($(AST_DEVMODE),yes)
+	$(CMD_PREFIX) $(CC) -o $@ -E $< $(PTHREAD_CFLAGS) $(ASTCFLAGS) -MMD -MT $@ -MF .$(subst /,_,$@).d -MP
+else
+	$(CMD_PREFIX) $(CC) -o $@ -E $< $(PTHREAD_CFLAGS) $(ASTCFLAGS)
+endif
+
 %.o: %.s
 	$(ECHO_PREFIX) echo "   [AS] $< -> $@"
 ifeq ($(AST_DEVMODE),yes)
@@ -76,6 +92,18 @@
 	$(ECHO_PREFIX) echo "   [LDXX] $^ -> $@"
 	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(PTHREAD_CFLAGS) $(ASTLDFLAGS) $(SOLINK) $^ $(PTHREAD_LIBS) $(LIBS)
 
+%.eo: %.o
+	$(ECHO_PREFIX) echo "   [EMBED] $< -> $@"
+	$(CMD_PREFIX) $(ASTTOPDIR)/build_tools/make_linker_eo_script $* > .$@.ld
+	$(CMD_PREFIX) $(LD) -r -T .$@.ld -o $@ $<
+	$(CMD_PREFIX) rm -f .$@.ld
+
+%.eoo: %.o
+	$(ECHO_PREFIX) echo "   [EMBED] $< -> $@"
+	$(CMD_PREFIX) $(ASTTOPDIR)/build_tools/make_linker_eo_script $* > .$@.ld
+	$(CMD_PREFIX) $(LD) -r -T .$@.ld -o $@ $<
+	$(CMD_PREFIX) rm -f .$@.ld
+
 %: %.o
 	$(ECHO_PREFIX) echo "   [LD] $^ -> $@"
 	$(CMD_PREFIX) $(CXX) $(STATIC_BUILD) -o $@ $(PTHREAD_CFLAGS) $(ASTLDFLAGS) $^ $(PTHREAD_LIBS) $(LIBS)

Propchange: trunk/apps/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Feb 21 20:36:00 2007
@@ -2,3 +2,5 @@
 *.a
 *.so
 modules.link
+*.eo
+*.eoo

Propchange: trunk/cdr/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Feb 21 20:36:00 2007
@@ -2,3 +2,5 @@
 *.a
 *.so
 modules.link
+*.eo
+*.eoo

Propchange: trunk/channels/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Feb 21 20:36:00 2007
@@ -6,3 +6,5 @@
 busy.h
 ringtone.h
 modules.link
+*.eo
+*.eoo

Propchange: trunk/codecs/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Feb 21 20:36:00 2007
@@ -2,3 +2,5 @@
 *.a
 *.so
 modules.link
+*.eo
+*.eoo

Propchange: trunk/formats/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Feb 21 20:36:00 2007
@@ -2,3 +2,5 @@
 *.a
 *.so
 modules.link
+*.eo
+*.eoo

Propchange: trunk/funcs/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Feb 21 20:36:00 2007
@@ -2,3 +2,5 @@
 *.a
 *.so
 modules.link
+*.eo
+*.eoo

Modified: trunk/include/asterisk/module.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/module.h?view=diff&rev=56092&r1=56091&r2=56092
==============================================================================
--- trunk/include/asterisk/module.h (original)
+++ trunk/include/asterisk/module.h Wed Feb 21 20:36:00 2007
@@ -192,6 +192,8 @@
 	enum ast_module_load_result (*load)(void);	/* register stuff etc. Optional. */
 	int (*reload)(void);			/* config etc. Optional. */
 	int (*unload)(void);			/* unload. called with the module locked */
+	int (*backup_globals)(void);		/* for embedded modules, backup global data */
+	void (*restore_globals)(void);		/* for embedded modules, restore global data */
 	const char *name;			/* name of the module for loader reference and CLI commands */
 	const char *description;		/* user friendly description of the module. */
 
@@ -253,6 +255,75 @@
    and populated at the end of the module's source file... */
 const static __attribute__((unused)) struct ast_module_info *ast_module_info;
 
+#if defined(EMBEDDED_MODULE)
+
+#define make_var_sub(mod, type) __ ## mod ## _ ## type
+#define make_var(mod, type) make_var_sub(mod, type)
+
+extern void make_var(EMBEDDED_MODULE, bss_start);
+extern void make_var(EMBEDDED_MODULE, bss_end);
+extern void make_var(EMBEDDED_MODULE, data_start);
+extern void make_var(EMBEDDED_MODULE, data_end);
+
+static void * __attribute__((section(".embed_module"))) __global_backup;
+
+static int __backup_globals(void)
+{
+	size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
+
+	if (__global_backup)
+		return 0;
+
+	if (!data_size)
+		return 0;
+
+	if (!(__global_backup = ast_malloc(data_size)))
+		return -1;
+
+	memcpy(__global_backup, & make_var(EMBEDDED_MODULE, data_start), data_size);
+
+	return 0;
+}
+
+static void __restore_globals(void)
+{
+	size_t data_size = & make_var(EMBEDDED_MODULE, data_end) - & make_var(EMBEDDED_MODULE, data_start);
+	size_t bss_size = & make_var(EMBEDDED_MODULE, bss_end) - & make_var(EMBEDDED_MODULE, bss_start);
+
+	if (bss_size)
+		memset(& make_var(EMBEDDED_MODULE, bss_start), 0, bss_size);
+
+	if (!data_size || !__global_backup)
+		return;
+
+	memcpy(& make_var(EMBEDDED_MODULE, data_start), __global_backup, data_size);
+}
+
+#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)	\
+	static struct ast_module_info __mod_info = {		\
+		.backup_globals = __backup_globals,		\
+		.restore_globals = __restore_globals,		\
+		.name = AST_MODULE,				\
+		.flags = flags_to_set,				\
+		.description = desc,				\
+		.key = keystr,					\
+		fields						\
+	};							\
+	static void  __attribute__ ((constructor)) __reg_module(void) \
+	{ \
+		ast_module_register(&__mod_info); \
+	} \
+	static void  __attribute__ ((destructor)) __unreg_module(void) \
+	{ \
+		ast_module_unregister(&__mod_info); \
+	} \
+	const static struct ast_module_info *ast_module_info = &__mod_info
+
+#undef make_var
+#undef make_var_sub
+
+#else /* !defined(EMBEDDED_MODULE) */
+
 #define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)	\
 	static struct ast_module_info __mod_info = {		\
 		.name = AST_MODULE,				\
@@ -271,6 +342,8 @@
 	} \
 	const static struct ast_module_info *ast_module_info = &__mod_info
 
+#endif /* !defined(EMBEDDED_MODULE) */
+
 #define AST_MODULE_INFO_STANDARD(keystr, desc)		\
 	AST_MODULE_INFO(keystr, AST_MODFLAG_DEFAULT, desc,	\
 			.load = load_module,			\

Modified: trunk/main/loader.c
URL: http://svn.digium.com/view/asterisk/trunk/main/loader.c?view=diff&rev=56092&r1=56091&r2=56092
==============================================================================
--- trunk/main/loader.c (original)
+++ trunk/main/loader.c Wed Feb 21 20:36:00 2007
@@ -483,6 +483,9 @@
 	if (!error)
 		ast_update_use_count();
 
+	if (!error && !mod->lib)
+		mod->info->restore_globals();
+
 	return res;
 }
 
@@ -624,6 +627,11 @@
 #if LOADABLE_MODULES
 		unload_dynamic_module(mod);
 #endif
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	if (!mod->lib && mod->info->backup_globals()) {
+		ast_log(LOG_WARNING, "Module '%s' was unable to backup its global data.\n", resource_name);
 		return AST_MODULE_LOAD_DECLINE;
 	}
 

Propchange: trunk/pbx/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Feb 21 20:36:00 2007
@@ -2,3 +2,5 @@
 *.a
 *.so
 modules.link
+*.eo
+*.eoo

Propchange: trunk/res/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Feb 21 20:36:00 2007
@@ -2,3 +2,5 @@
 *.a
 *.so
 modules.link
+*.eo
+*.eoo



More information about the svn-commits mailing list