Browse code

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.

Miklos Tirpak authored on 07/06/2011 18:58:38
Showing 3 changed files
... ...
@@ -95,6 +95,8 @@ cfg_group_t *cfg_new_group(char *name, int name_len,
95 95
 	group->vars = vars;
96 96
 	group->size = size;
97 97
 	group->handle = handle;
98
+	if (handle)
99
+		group->orig_handle = *handle;
98 100
 	group->name_len = name_len;
99 101
 	memcpy(&group->name, name, name_len);
100 102
 
... ...
@@ -115,6 +117,8 @@ void cfg_set_group(cfg_group_t *group,
115 115
 	group->vars = vars;
116 116
 	group->size = size;
117 117
 	group->handle = handle;
118
+	if (handle)
119
+		group->orig_handle = *handle;
118 120
 }
119 121
 
120 122
 /* clones a string to shared memory
... ...
@@ -1256,3 +1260,43 @@ int cfg_select_next(cfg_group_t *group)
1256 1256
 	return 0;
1257 1257
 }
1258 1258
 
1259
+/* Temporary set the local configuration in the main process before forking.
1260
+ * This makes the group instances usable in the main process after
1261
+ * the configuration is shmized, but before the children are forked.
1262
+ */
1263
+void cfg_main_set_local(void)
1264
+{
1265
+	/* Disable the execution of child-process callbacks,
1266
+	 * they can cause trouble because the children inherit all the
1267
+	 * values later */
1268
+	cfg_child_cb = CFG_NO_CHILD_CBS;
1269
+	cfg_update_no_cbs();
1270
+}
1271
+
1272
+/* Reset the local configuration of the main process back to its original state
1273
+ * to make sure that the forked processes are not affected.
1274
+ */
1275
+void cfg_main_reset_local(void)
1276
+{
1277
+	cfg_group_t	*group;
1278
+
1279
+	/* Unref the local config, and set it back to NULL.
1280
+	 * Each child will set its own local configuration. */
1281
+	if (cfg_local) {
1282
+		CFG_UNREF(cfg_local);
1283
+		cfg_local = NULL;
1284
+
1285
+		/* restore the original value of the module handles */
1286
+		for (	group = cfg_group;
1287
+			group;
1288
+			group = group->next
1289
+		)
1290
+			*(group->handle) = group->orig_handle;
1291
+		/* The handle might have pointed to a group instance,
1292
+		 * reset the instance counter. */
1293
+		cfg_ginst_count = 0;
1294
+	}
1295
+	cfg_child_cb = NULL;
1296
+}
1297
+
1298
+
... ...
@@ -99,6 +99,10 @@ typedef struct _cfg_group {
99 99
 					by the modules to access the variables.
100 100
 					It is registered when the group is created,
101 101
 					and updated every time the block is replaced */
102
+	void		*orig_handle;	/*!< Original value that the handle points to
103
+					when the config group is registered. This is needed
104
+					to temporary set the handle in the main process and
105
+					restore it later to its original value. */
102 106
 
103 107
 	unsigned char	dynamic;	/*!< indicates whether the variables within the group
104 108
 					are dynamically	allocated or not */
... ...
@@ -534,4 +538,15 @@ int cfg_select_first(cfg_group_t *group);
534 534
  */
535 535
 int cfg_select_next(cfg_group_t *group);
536 536
 
537
+/* Temporary set the local configuration in the main process before forking.
538
+ * This makes the group instances usable in the main process after
539
+ * the configuration is shmized, but before the children are forked.
540
+ */
541
+void cfg_main_set_local(void);
542
+
543
+/* Reset the local configuration of the main process back to its original state
544
+ * to make sure that the forked processes are not affected.
545
+ */
546
+void cfg_main_reset_local(void);
547
+
537 548
 #endif /* _CFG_STRUCT_H */
... ...
@@ -1310,6 +1310,11 @@ int main_loop()
1310 1310
 		   as new processes are forked (while skipping 0 reserved for main
1311 1311
 		*/
1312 1312
 
1313
+		/* Temporary set the local configuration of the main process
1314
+		 * to make the group instances available in PROC_INIT.
1315
+		 */
1316
+		cfg_main_set_local();
1317
+
1313 1318
 		/* init childs with rank==PROC_INIT before forking any process,
1314 1319
 		 * this is a place for delayed (after mod_init) initializations
1315 1320
 		 * (e.g. shared vars that depend on the total number of processes
... ...
@@ -1320,8 +1325,10 @@ int main_loop()
1320 1320
 		if (init_child(PROC_INIT) < 0) {
1321 1321
 			LOG(L_ERR, "ERROR: main_dontfork: init_child(PROC_INT) --"
1322 1322
 						" exiting\n");
1323
+			cfg_main_reset_local();
1323 1324
 			goto error;
1324 1325
 		}
1326
+		cfg_main_reset_local();
1325 1327
 		if (counters_prefork_init(get_max_procs()) == -1) goto error;
1326 1328
 
1327 1329
 #ifdef USE_SLOW_TIMER
... ...
@@ -1523,6 +1530,12 @@ int main_loop()
1523 1523
 			LOG(L_CRIT, "could not initialize shared configuration\n");
1524 1524
 			goto error;
1525 1525
 		}
1526
+
1527
+		/* Temporary set the local configuration of the main process
1528
+		 * to make the group instances available in PROC_INIT.
1529
+		 */
1530
+		cfg_main_set_local();
1531
+
1526 1532
 		/* init childs with rank==PROC_INIT before forking any process,
1527 1533
 		 * this is a place for delayed (after mod_init) initializations
1528 1534
 		 * (e.g. shared vars that depend on the total number of processes
... ...
@@ -1533,8 +1546,10 @@ int main_loop()
1533 1533
 		if (init_child(PROC_INIT) < 0) {
1534 1534
 			LOG(L_ERR, "ERROR: main: error in init_child(PROC_INT) --"
1535 1535
 					" exiting\n");
1536
+			cfg_main_reset_local();
1536 1537
 			goto error;
1537 1538
 		}
1539
+		cfg_main_reset_local();
1538 1540
 		if (counters_prefork_init(get_max_procs()) == -1) goto error;
1539 1541
 
1540 1542