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 117
 	group->vars = vars;
116 118
 	group->size = size;
117 119
 	group->handle = handle;
120
+	if (handle)
121
+		group->orig_handle = *handle;
118 122
 }
119 123
 
120 124
 /* clones a string to shared memory
... ...
@@ -1256,3 +1260,43 @@ int cfg_select_next(cfg_group_t *group)
1256 1260
 	return 0;
1257 1261
 }
1258 1262
 
1263
+/* Temporary set the local configuration in the main process before forking.
1264
+ * This makes the group instances usable in the main process after
1265
+ * the configuration is shmized, but before the children are forked.
1266
+ */
1267
+void cfg_main_set_local(void)
1268
+{
1269
+	/* Disable the execution of child-process callbacks,
1270
+	 * they can cause trouble because the children inherit all the
1271
+	 * values later */
1272
+	cfg_child_cb = CFG_NO_CHILD_CBS;
1273
+	cfg_update_no_cbs();
1274
+}
1275
+
1276
+/* Reset the local configuration of the main process back to its original state
1277
+ * to make sure that the forked processes are not affected.
1278
+ */
1279
+void cfg_main_reset_local(void)
1280
+{
1281
+	cfg_group_t	*group;
1282
+
1283
+	/* Unref the local config, and set it back to NULL.
1284
+	 * Each child will set its own local configuration. */
1285
+	if (cfg_local) {
1286
+		CFG_UNREF(cfg_local);
1287
+		cfg_local = NULL;
1288
+
1289
+		/* restore the original value of the module handles */
1290
+		for (	group = cfg_group;
1291
+			group;
1292
+			group = group->next
1293
+		)
1294
+			*(group->handle) = group->orig_handle;
1295
+		/* The handle might have pointed to a group instance,
1296
+		 * reset the instance counter. */
1297
+		cfg_ginst_count = 0;
1298
+	}
1299
+	cfg_child_cb = NULL;
1300
+}
1301
+
1302
+
... ...
@@ -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 538
  */
535 539
 int cfg_select_next(cfg_group_t *group);
536 540
 
541
+/* Temporary set the local configuration in the main process before forking.
542
+ * This makes the group instances usable in the main process after
543
+ * the configuration is shmized, but before the children are forked.
544
+ */
545
+void cfg_main_set_local(void);
546
+
547
+/* Reset the local configuration of the main process back to its original state
548
+ * to make sure that the forked processes are not affected.
549
+ */
550
+void cfg_main_reset_local(void);
551
+
537 552
 #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 1325
 		if (init_child(PROC_INIT) < 0) {
1321 1326
 			LOG(L_ERR, "ERROR: main_dontfork: init_child(PROC_INT) --"
1322 1327
 						" exiting\n");
1328
+			cfg_main_reset_local();
1323 1329
 			goto error;
1324 1330
 		}
1331
+		cfg_main_reset_local();
1325 1332
 		if (counters_prefork_init(get_max_procs()) == -1) goto error;
1326 1333
 
1327 1334
 #ifdef USE_SLOW_TIMER
... ...
@@ -1523,6 +1530,12 @@ int main_loop()
1523 1530
 			LOG(L_CRIT, "could not initialize shared configuration\n");
1524 1531
 			goto error;
1525 1532
 		}
1533
+
1534
+		/* Temporary set the local configuration of the main process
1535
+		 * to make the group instances available in PROC_INIT.
1536
+		 */
1537
+		cfg_main_set_local();
1538
+
1526 1539
 		/* init childs with rank==PROC_INIT before forking any process,
1527 1540
 		 * this is a place for delayed (after mod_init) initializations
1528 1541
 		 * (e.g. shared vars that depend on the total number of processes
... ...
@@ -1533,8 +1546,10 @@ int main_loop()
1533 1546
 		if (init_child(PROC_INIT) < 0) {
1534 1547
 			LOG(L_ERR, "ERROR: main: error in init_child(PROC_INT) --"
1535 1548
 					" exiting\n");
1549
+			cfg_main_reset_local();
1536 1550
 			goto error;
1537 1551
 		}
1552
+		cfg_main_reset_local();
1538 1553
 		if (counters_prefork_init(get_max_procs()) == -1) goto error;
1539 1554
 
1540 1555