[sr-dev] git:master: cfg framework: set the local config in PROC_INIT

Miklos Tirpak miklos at iptel.org
Fri Jun 24 15:14:50 CEST 2011


Module: sip-router
Branch: master
Commit: 5d3449a06ac523575ba029325048efc20cd053b7
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=5d3449a06ac523575ba029325048efc20cd053b7

Author: Miklos Tirpak <miklos at iptel.org>
Committer: Miklos Tirpak <miklos at iptel.org>
Date:   Tue Jun  7 20:58:38 2011 +0200

cfg framework: set the local config in PROC_INIT

The local configuration of the main process is temporary
set during initialization in child_init with rank==PROC_INIT,
which makes the configuration group instances available for
the modules.
The local configuration is reset back to NULL afterwards to make
sure that each child process updates its own config the usual way.

---

 cfg/cfg_struct.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 cfg/cfg_struct.h |   15 +++++++++++++++
 main.c           |   15 +++++++++++++++
 3 files changed, 74 insertions(+), 0 deletions(-)

diff --git a/cfg/cfg_struct.c b/cfg/cfg_struct.c
index c10dd55..e07098a 100644
--- a/cfg/cfg_struct.c
+++ b/cfg/cfg_struct.c
@@ -95,6 +95,8 @@ cfg_group_t *cfg_new_group(char *name, int name_len,
 	group->vars = vars;
 	group->size = size;
 	group->handle = handle;
+	if (handle)
+		group->orig_handle = *handle;
 	group->name_len = name_len;
 	memcpy(&group->name, name, name_len);
 
@@ -115,6 +117,8 @@ void cfg_set_group(cfg_group_t *group,
 	group->vars = vars;
 	group->size = size;
 	group->handle = handle;
+	if (handle)
+		group->orig_handle = *handle;
 }
 
 /* clones a string to shared memory
@@ -1256,3 +1260,43 @@ int cfg_select_next(cfg_group_t *group)
 	return 0;
 }
 
+/* Temporary set the local configuration in the main process before forking.
+ * This makes the group instances usable in the main process after
+ * the configuration is shmized, but before the children are forked.
+ */
+void cfg_main_set_local(void)
+{
+	/* Disable the execution of child-process callbacks,
+	 * they can cause trouble because the children inherit all the
+	 * values later */
+	cfg_child_cb = CFG_NO_CHILD_CBS;
+	cfg_update_no_cbs();
+}
+
+/* Reset the local configuration of the main process back to its original state
+ * to make sure that the forked processes are not affected.
+ */
+void cfg_main_reset_local(void)
+{
+	cfg_group_t	*group;
+
+	/* Unref the local config, and set it back to NULL.
+	 * Each child will set its own local configuration. */
+	if (cfg_local) {
+		CFG_UNREF(cfg_local);
+		cfg_local = NULL;
+
+		/* restore the original value of the module handles */
+		for (	group = cfg_group;
+			group;
+			group = group->next
+		)
+			*(group->handle) = group->orig_handle;
+		/* The handle might have pointed to a group instance,
+		 * reset the instance counter. */
+		cfg_ginst_count = 0;
+	}
+	cfg_child_cb = NULL;
+}
+
+
diff --git a/cfg/cfg_struct.h b/cfg/cfg_struct.h
index 2e91691..dca39a0 100644
--- a/cfg/cfg_struct.h
+++ b/cfg/cfg_struct.h
@@ -99,6 +99,10 @@ typedef struct _cfg_group {
 					by the modules to access the variables.
 					It is registered when the group is created,
 					and updated every time the block is replaced */
+	void		*orig_handle;	/*!< Original value that the handle points to
+					when the config group is registered. This is needed
+					to temporary set the handle in the main process and
+					restore it later to its original value. */
 
 	unsigned char	dynamic;	/*!< indicates whether the variables within the group
 					are dynamically	allocated or not */
@@ -534,4 +538,15 @@ int cfg_select_first(cfg_group_t *group);
  */
 int cfg_select_next(cfg_group_t *group);
 
+/* Temporary set the local configuration in the main process before forking.
+ * This makes the group instances usable in the main process after
+ * the configuration is shmized, but before the children are forked.
+ */
+void cfg_main_set_local(void);
+
+/* Reset the local configuration of the main process back to its original state
+ * to make sure that the forked processes are not affected.
+ */
+void cfg_main_reset_local(void);
+
 #endif /* _CFG_STRUCT_H */
diff --git a/main.c b/main.c
index 22ed613..2fcb40b 100644
--- a/main.c
+++ b/main.c
@@ -1310,6 +1310,11 @@ int main_loop()
 		   as new processes are forked (while skipping 0 reserved for main
 		*/
 
+		/* Temporary set the local configuration of the main process
+		 * to make the group instances available in PROC_INIT.
+		 */
+		cfg_main_set_local();
+
 		/* init childs with rank==PROC_INIT before forking any process,
 		 * this is a place for delayed (after mod_init) initializations
 		 * (e.g. shared vars that depend on the total number of processes
@@ -1320,8 +1325,10 @@ int main_loop()
 		if (init_child(PROC_INIT) < 0) {
 			LOG(L_ERR, "ERROR: main_dontfork: init_child(PROC_INT) --"
 						" exiting\n");
+			cfg_main_reset_local();
 			goto error;
 		}
+		cfg_main_reset_local();
 		if (counters_prefork_init(get_max_procs()) == -1) goto error;
 
 #ifdef USE_SLOW_TIMER
@@ -1523,6 +1530,12 @@ int main_loop()
 			LOG(L_CRIT, "could not initialize shared configuration\n");
 			goto error;
 		}
+
+		/* Temporary set the local configuration of the main process
+		 * to make the group instances available in PROC_INIT.
+		 */
+		cfg_main_set_local();
+
 		/* init childs with rank==PROC_INIT before forking any process,
 		 * this is a place for delayed (after mod_init) initializations
 		 * (e.g. shared vars that depend on the total number of processes
@@ -1533,8 +1546,10 @@ int main_loop()
 		if (init_child(PROC_INIT) < 0) {
 			LOG(L_ERR, "ERROR: main: error in init_child(PROC_INT) --"
 					" exiting\n");
+			cfg_main_reset_local();
 			goto error;
 		}
+		cfg_main_reset_local();
 		if (counters_prefork_init(get_max_procs()) == -1) goto error;
 
 




More information about the sr-dev mailing list