Browse code

- Modules can properly register processes in SER process table - process_count function has been replaced by process_count variable - modules can increase the value of process_count in mod_init, this will be used to allocate process table that will be big enough - modules can create processes in child_init with rank PROC_MAIN - the index into process table is in last_process, this is where information about the new process should be recorded - do not forget to reset is_main in the new process otherwise it will not be terminated properly - modules should increate last_process before fork - the new child should set process_no = last_process

Jan Janak authored on 13/12/2005 12:10:15
Showing 7 changed files
... ...
@@ -159,7 +159,7 @@ static void core_ps(rpc_t* rpc, void* c)
159 159
 {
160 160
 	int p;
161 161
 
162
-	for (p=0; p<process_count();p++) {
162
+	for (p=0; p<process_count;p++) {
163 163
 		rpc->printf(c, "pid: %d", pt[p].pid);
164 164
 		rpc->printf(c, "desc: %s", pt[p].desc);
165 165
 	}
... ...
@@ -864,7 +864,7 @@ static int ps_fifo_cmd(FILE *stream, char *response_file )
864 864
 	}
865 865
 
866 866
 	fputs( "200 ok\n", reply_pipe);
867
-	for (p=0; p<process_count();p++) 
867
+	for (p=0; p<process_count;p++) 
868 868
 		fprintf( reply_pipe, "%d\t%d\t%s\n",
869 869
 			p, pt[p].pid, pt[p].desc );
870 870
 
... ...
@@ -335,6 +335,15 @@ struct host_alias* aliases=0; /* name aliases list */
335 335
 
336 336
 /* ipc related globals */
337 337
 int process_no = 0;
338
+
339
+/* Last filled entry in process table before calling
340
+ * child_init of loaded modules
341
+ */
342
+int last_process = 0;
343
+
344
+/* Total number of SER processes with given configuration */
345
+int process_count = 0;
346
+
338 347
 /* process_bm_t process_bit = 0; */
339 348
 #ifdef ROUTE_SRV
340 349
 #endif
... ...
@@ -421,7 +430,7 @@ static void kill_all_children(int signum)
421 421
 
422 422
 	if (own_pgid) kill(0, signum);
423 423
 	else if (pt)
424
-		for (r=1; r<process_count(); r++)
424
+		for (r=1; r<process_count; r++)
425 425
 			if (pt[r].pid) kill(pt[r].pid, signum);
426 426
 }
427 427
 
... ...
@@ -1010,7 +1019,7 @@ int main_loop()
1010 1010
 					 * parent gets a chance to set it*/
1011 1011
 					pt[process_no].pid=getpid();
1012 1012
 					bind_address=si; /* shortcut */
1013
-					if (init_child(i + 1) < 0) {
1013
+					if (init_child(process_no) < 0) {
1014 1014
 						LOG(L_ERR, "init_child failed\n");
1015 1015
 						goto error;
1016 1016
 					}
... ...
@@ -1174,18 +1183,11 @@ int main_loop()
1174 1174
 	}
1175 1175
 #endif
1176 1176
 	/*DEBUG- remove it*/
1177
-#ifdef EXTRA_DEBUG
1178
-	fprintf(stderr, "\n% 3d processes (%3d), % 3d children * "
1179
-			"listening addresses + tcp listeners + tls listeners"
1180
-			"+ main + fifo + timer"
1181
-# ifdef USE_SLOW_TIMER
1182
-			" + slow_timer"
1183
-# endif
1184
-			"\n", process_no+1, process_count(), children_no);
1185
-	for (r=0; r<=process_no; r++){
1186
-		fprintf(stderr, "% 3d   % 5d - %s\n", r, pt[r].pid, pt[r].desc);
1187
-	}
1188
-#endif
1177
+
1178
+	/* Modules need to know the last value of process_no to fill in
1179
+	 * process table properly
1180
+	 */
1181
+	last_process = process_no;
1189 1182
 	process_no=0; 
1190 1183
 	/* process_bit = 0; */
1191 1184
 	is_main=1;
... ...
@@ -1194,6 +1196,14 @@ int main_loop()
1194 1194
 		LOG(L_ERR, "main: error in init_child\n");
1195 1195
 		goto error;
1196 1196
 	}
1197
+
1198
+	/*DEBUG- remove it*/
1199
+#ifdef EXTRA_DEBUG
1200
+	for (r=0; r<process_count; r++){
1201
+		fprintf(stderr, "% 3d   % 5d - %s\n", r, pt[r].pid, pt[r].desc);
1202
+	}
1203
+#endif
1204
+
1197 1205
 	for(;;){
1198 1206
 			pause();
1199 1207
 			handle_sigs();
... ...
@@ -1210,6 +1220,34 @@ int main_loop()
1210 1210
 }
1211 1211
 
1212 1212
 
1213
+/*
1214
+ * Calculate number of processes, this does not
1215
+ * include processes created by modules
1216
+ */
1217
+static int calc_proc_no(void)
1218
+{
1219
+	int udp_listeners;
1220
+	struct socket_info* si;
1221
+	
1222
+	for (si=udp_listen, udp_listeners=0; si; si=si->next, udp_listeners++);
1223
+	return
1224
+		     /* receivers and attendant */
1225
+		(dont_fork ? 1 : children_no * udp_listeners + 1)
1226
+		     /* timer process */
1227
+		+ 1 /* always, we need it in most cases, and we can't tell here
1228
+		       & now if we don't need it */
1229
+#ifdef USE_SLOW_TIMER
1230
+		+ 1 /* slow timer process */
1231
+#endif
1232
+		/* fifo server */
1233
+		+((fifo==NULL || strlen(fifo)==0) ? 0 : 1 )
1234
+		/* unixsock server*/
1235
+		+(unixsock_name?unixsock_children:0)
1236
+#ifdef USE_TCP
1237
+		+((!tcp_disable)?( 1/* tcp main */ + tcp_children_no ):0) 
1238
+#endif
1239
+		;
1240
+}
1213 1241
 
1214 1242
 
1215 1243
 int main(int argc, char** argv)
... ...
@@ -1575,19 +1613,6 @@ try_again:
1575 1575
 		fprintf(stderr, "ERROR: could not install the signal handlers\n");
1576 1576
 		goto error;
1577 1577
 	}
1578
-	
1579
-	
1580
-	/*alloc pids*/
1581
-#ifdef SHM_MEM
1582
-	pt=shm_malloc(sizeof(struct process_table)*process_count());
1583
-#else
1584
-	pt=pkg_malloc(sizeof(struct process_table)*process_count());
1585
-#endif
1586
-	if (pt==0){
1587
-		fprintf(stderr, "ERROR: out  of memory\n");
1588
-		goto error;
1589
-	}
1590
-	memset(pt, 0, sizeof(struct process_table)*process_count());
1591 1578
 
1592 1579
 	if (disable_core_dump) set_core_dump(0, 0);
1593 1580
 	else set_core_dump(1, shm_mem_size+PKG_MEM_POOL_SIZE+4*1024*1024);
... ...
@@ -1597,11 +1622,34 @@ try_again:
1597 1597
 			goto error;
1598 1598
 		}
1599 1599
 	}
1600
-	
1600
+
1601
+
1602
+	     /* Calculate initial process count, mod_init functions called 
1603
+	      * below can add to it
1604
+	      */
1605
+	process_count = calc_proc_no(); 
1601 1606
 	if (init_modules() != 0) {
1602 1607
 		fprintf(stderr, "ERROR: error while initializing modules\n");
1603 1608
 		goto error;
1604 1609
 	}
1610
+	     /* The total number of processes is know now, note that no 
1611
+	      * function being called before this point may rely on the 
1612
+	      * number of processes !
1613
+	      */
1614
+	DBG("Expect %d SER processes in your process list\n", process_count);
1615
+
1616
+	/*alloc pids*/
1617
+#ifdef SHM_MEM
1618
+	pt=shm_malloc(sizeof(struct process_table)*process_count);
1619
+#else
1620
+	pt=pkg_malloc(sizeof(struct process_table)*process_count);
1621
+#endif
1622
+	if (pt==0){
1623
+		fprintf(stderr, "ERROR: out  of memory\n");
1624
+		goto error;
1625
+	}
1626
+	memset(pt, 0, sizeof(struct process_table)*process_count);
1627
+
1605 1628
 	/* fix routing lists */
1606 1629
 	if ( (r=fix_rls())!=0){
1607 1630
 		fprintf(stderr, "ERROR: error %d while trying to fix configuration\n",
... ...
@@ -57,7 +57,7 @@ int print_stats(  FILE *f )
57 57
 	int i;
58 58
 	int pno;
59 59
 
60
-	pno=process_count();
60
+	pno=process_count;
61 61
 	for(i=0, total=0, waiting=0, total_local=0; i<pno;i++) {
62 62
 		total+=tm_stats->s_transactions[i];
63 63
 		waiting+=tm_stats->s_waiting[i];
... ...
@@ -119,7 +119,7 @@ int static unixsock_stats(str* cmd)
119 119
 
120 120
 	unixsock_reply_asciiz( "200 OK\n");
121 121
 
122
-	pno = process_count();
122
+	pno = process_count;
123 123
 	for(i = 0, total = 0, waiting = 0, total_local = 0; i < pno; i++) {
124 124
 		total += tm_stats->s_transactions[i];
125 125
 		waiting += tm_stats->s_waiting[i];
... ...
@@ -167,7 +167,7 @@ int init_tm_stats(void)
167 167
 	}
168 168
 	memset(tm_stats, 0, sizeof(struct t_stats) );
169 169
 
170
-	size=sizeof(stat_counter)*process_count();
170
+	size=sizeof(stat_counter)*process_count;
171 171
 	tm_stats->s_waiting=shm_malloc(size);
172 172
 	if (tm_stats->s_waiting==0) {
173 173
 		LOG(L_ERR, "ERROR: init_tm_stats: no mem for stats\n");
... ...
@@ -58,34 +58,8 @@ struct process_table {
58 58
 
59 59
 extern struct process_table *pt;
60 60
 extern int process_no;
61
-
62
-/* get number of process started by main with
63
-   given configuration
64
-*/
65
-inline static int process_count()
66
-{
67
-	int udp_listeners;
68
-	struct socket_info* si;
69
-	
70
-	for (si=udp_listen, udp_listeners=0; si; si=si->next, udp_listeners++);
71
-    return 
72
-		/* receivers and attendant */
73
-		(dont_fork ? 1 : children_no*udp_listeners + 1)
74
-		/* timer process */
75
-		+ 1 /* always, we need it in most cases, and we can't tell here
76
-			   & now if we don't need it */
77
-#ifdef USE_SLOW_TIMER
78
-		+ 1 /* slow timer process */
79
-#endif
80
-		/* fifo server */
81
-		+((fifo==NULL || strlen(fifo)==0) ? 0 : 1 )
82
-		/* unixsock server*/
83
-		+(unixsock_name?unixsock_children:0)
84
-#ifdef USE_TCP
85
-		+((!tcp_disable)?( 1/* tcp main */ + tcp_children_no ):0) 
86
-#endif
87
-		;
88
-}
61
+extern int process_count;
62
+extern int last_process;
89 63
 
90 64
 
91 65
 /* return processes pid */
... ...
@@ -1694,7 +1694,7 @@ int tcp_init_children()
1694 1694
 		for (si=tls_listen; si; si=si->next, r++);
1695 1695
 #endif
1696 1696
 	
1697
-	tcp_max_fd_no=process_count()*2 +r-1 /* timer */ +3; /* stdin/out/err*/
1697
+	tcp_max_fd_no=process_count*2 +r-1 /* timer */ +3; /* stdin/out/err*/
1698 1698
 	tcp_max_fd_no+=tcp_max_connections;
1699 1699
 	
1700 1700
 	/* create the tcp sock_info structures */
... ...
@@ -1741,7 +1741,7 @@ int tcp_init_children()
1741 1741
 			/* record pid twice to avoid the child using it, before
1742 1742
 			 * parent gets a chance to set it*/
1743 1743
 			pt[process_no].pid=getpid();
1744
-			if (init_child(r+children_no+1) < 0) {
1744
+			if (init_child(process_no) < 0) {
1745 1745
 				LOG(L_ERR, "init_children failed\n");
1746 1746
 				goto error;
1747 1747
 			}
... ...
@@ -193,7 +193,7 @@ static int ps_cmd(str* msg)
193 193
 
194 194
 	ret = 0;
195 195
 	unixsock_reply_asciiz("200 OK\n");
196
-	for (p = 0; p < process_count(); p++) {
196
+	for (p = 0; p < process_count; p++) {
197 197
 		if (unixsock_reply_printf("%d\t%d\t%s\n", p, pt[p].pid, pt[p].desc) < 0) {
198 198
 			unixsock_reply_reset();
199 199
 			unixsock_reply_asciiz("500 Error while printing reply\n");