Browse code

- applied patch from Dragos Vingarzan <vingarzan@fokus.fraunhofer.de> which moves all the forking part into 2 functions in pt.c (fork_process and fork_tcp_process). - added PROC_NOCHLDINIT rank value for Dragos's fork_process (if this value is used as child_id/rank_value the mod_child functions will not be called) - added register_procs(processes_no), used from mod_init when a module knows that it will fork some children (replaces the old process_count++ / the patch's estimated_process_count++) - added get_max_procs(): returns the maximum (estimated) number of processes

Andrei Pelinescu-Onciul authored on 19/09/2006 16:13:28
Showing 10 changed files
... ...
@@ -262,7 +262,7 @@ static int mod_init(void)
262 262
 	}
263 263
 	if (ctrl_sock_lst){
264 264
 		/* we will fork */
265
-		process_count++; /* we will be creating an extra process */
265
+		register_procs(1); /* we will be creating an extra process */
266 266
 	}
267 267
 #ifdef USE_FIFO
268 268
 	fifo_rpc_init();
... ...
@@ -278,41 +278,24 @@ static int mod_child(int rank)
278 278
 {
279 279
 	int pid;
280 280
 	struct ctrl_socket* cs;
281
-#ifdef USE_TCP
282
-	int sockfd[2];
283
-#endif
284 281
 	
285 282
 	/* we want to fork(), but only from one process */
286 283
 	if ((rank == PROC_MAIN ) && (ctrl_sock_lst)){ /* FIXME: no fork ?? */
287
-#ifdef USE_TCP
288
-		if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
289
-			
290
-			goto error;
291
-		}
292
-#endif
293
-		last_process++;
294
-		pid=fork();
295
-		if (pid<0){
296
-			
284
+		DBG("ctl: mod_child(%d), ctrl_sock_lst=%p\n", rank, ctrl_sock_lst);
285
+		/* fork, but  don't call init_child, we don't want all the module
286
+		 * to know about us */
287
+		pid=fork_process(PROC_NOCHLDINIT, "ctl handler",1);
288
+		DBG("ctl: mod_child(%d), fork_process=%d, csl=%p\n",
289
+				rank, pid, ctrl_sock_lst);
290
+		if (pid<0){			
297 291
 			goto error;
298 292
 		}
299 293
 		if (pid == 0){ /* child */
300
-			process_no=last_process;
301 294
 			is_main=0;
302
-			pt[process_no].pid=getpid();
303
-#ifdef USE_TCP
304
-			close(sockfd[0]);
305
-			unix_tcp_sock=sockfd[1];
306
-#endif
295
+			DBG("ctl: %d io_listen_loop(%d, %p)\n",
296
+					rank, fd_no, ctrl_sock_lst);
307 297
 			io_listen_loop(fd_no, ctrl_sock_lst);
308 298
 		}else{ /* parent */
309
-			pt[last_process].pid=pid;
310
-			snprintf(pt[last_process].desc, MAX_PT_DESC, "ctl handler");
311
-#ifdef USE_TCP
312
-			close(sockfd[1]);
313
-			pt[last_process].unix_sock=sockfd[0];
314
-			pt[last_process].idx=-1; /* mark it as non-tcp */
315
-#endif
316 299
 		}
317 300
 	}
318 301
 	/* close all the opened fds, we don't need them here */
... ...
@@ -8,4 +8,6 @@
8 8
 /* port used by default for tcp/udp if no port is explicitely specified */
9 9
 #define DEFAULT_CTL_PORT 2049
10 10
 
11
+#define PROC_CTL -32
12
+
11 13
 #endif
... ...
@@ -193,7 +193,7 @@ void io_listen_loop(int fd_no, struct ctrl_socket* cs_lst)
193 193
 	clist_init(&stream_conn_lst, next, prev);
194 194
 	type=UNKNOWN_SOCK;
195 195
 	/* estimate used fd numbers -- FIXME: broken, make it a function in pt.h */
196
-	max_fd_no=process_count*3 -1 /* timer */ +3; /* stdin/out/err*/;
196
+	max_fd_no=get_max_procs()*3 -1 /* timer */ +3; /* stdin/out/err*/;
197 197
 	max_fd_no+=fd_no+MAX_IO_READ_CONNECTIONS; /*our listen fds + max.
198 198
 												allowed tmp. fds */
199 199
 	
... ...
@@ -82,7 +82,7 @@ static int mod_init(void)
82 82
 	     /* Signal to the core that we will be creating one
83 83
 	      * additional process
84 84
 	      */
85
-	if (fifo) process_count++;
85
+	if (fifo) register_procs(1);
86 86
 	return 0;
87 87
 }
88 88
 
... ...
@@ -931,56 +931,25 @@ int init_fifo_server(void)
931 931
  */
932 932
 int start_fifo_server(void)
933 933
 {
934
-#ifdef USE_TCP
935
-	int sockfd[2];
936
-#endif
937 934
 	if (fifo_stream == 0) return 1; /* no error, we just don't start it */
938 935
 
939
-#ifdef USE_TCP
940
-	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
941
-		ERR("socketpair failed: %s\n", strerror(errno));
942
-		return -1;
943
-	}
944
-#endif
945
-	last_process++;
946
-	fifo_pid = fork();
936
+	fifo_pid = fork_process(PROC_FIFO,"fifo server",1);
947 937
 	if (fifo_pid < 0) {
948 938
 		ERR("Failed to fork: %s\n", strerror(errno));
949 939
 		return -1;
950 940
 	}
951 941
 	if (fifo_pid == 0) { /* child == FIFO server */
952
-		     /* record pid twice to avoid the child using it, before
953
-		      * parent gets a chance to set it*/
954
-		process_no = last_process; /* Initialize process_no to our process number */
955 942
 		is_main = 0;
956
-		pt[process_no].pid = getpid();
957 943
 		INFO("fifo process starting: %d\n", getpid());
958
-		     /* call per-child module initialization too -- some
959
-		      * FIFO commands may need it
960
-		      */
961
-#ifdef USE_TCP
962
-		close(sockfd[0]);
963
-		unix_tcp_sock = sockfd[1];
964
-#endif
965
-		if (init_child(PROC_FIFO) < 0 ) {
966
-			ERR("init_child failed\n");
967
-			return -1;
968
-		}
969
-		     /* a real server doesn't die if writing to reply fifo fails */
970
-    		signal(SIGPIPE, SIG_IGN);
944
+		/* a real server doesn't die if writing to reply fifo fails */
945
+    	signal(SIGPIPE, SIG_IGN);
971 946
 		INFO("fifo server up at %s...\n",
972 947
 		     fifo);
973 948
 		fifo_server(fifo_stream); /* never returns */
974 949
 	}
975 950
 	     /* dad process */
976
-	pt[last_process].pid=fifo_pid;
977
-	snprintf(pt[last_process].desc, MAX_PT_DESC, 
951
+	snprintf(pt[process_no].desc, MAX_PT_DESC, 
978 952
 		 "fifo server @ %s", fifo);
979
-#ifdef USE_TCP
980
-	close(sockfd[1]);
981
-	pt[last_process].unix_sock = sockfd[0];
982
-	pt[last_process].idx = -1; /* this is not "tcp" process*/
983
-#endif
984 953
 	return 1;
985 954
 }
986 955
 
... ...
@@ -33,6 +33,7 @@
33 33
 
34 34
 #include <stdio.h>
35 35
 
36
+
36 37
 #define CMD_SEPARATOR ':'
37 38
 
38 39
 extern char* fifo;
... ...
@@ -59,12 +59,16 @@ static void rpc_stats(rpc_t* rpc, void* c)
59 59
 	void* st;
60 60
 	struct sl_stats total;
61 61
 	int p;
62
+	int procs_no;
62 63
 
63 64
 	memset(&total, 0, sizeof(struct sl_stats));
64 65
 	if (dont_fork) {
65 66
 		add_sl_stats(&total, &(*sl_stats)[0]);
66
-	} else for (p=0; p < process_count; p++)
67
+	} else{
68
+		procs_no=get_max_procs();
69
+		for (p=0; p < procs_no; p++)
67 70
 			add_sl_stats(&total, &(*sl_stats)[p]);
71
+	}
68 72
 
69 73
 	if (rpc->add(c, "{", &st) < 0) return;
70 74
 	
... ...
@@ -121,7 +125,7 @@ int init_sl_stats_child(void)
121 121
 {
122 122
 	int len;
123 123
 
124
-	len = sizeof(struct sl_stats) * process_count;
124
+	len = sizeof(struct sl_stats) * get_max_procs();
125 125
 	*sl_stats = shm_malloc(len);
126 126
 	if (*sl_stats == 0) {
127 127
 		ERR("No shmem\n");
... ...
@@ -79,9 +79,12 @@ tls_domain_t* tls_new_domain(int type, struct ip_addr *ip, unsigned short port)
79 79
 void tls_free_domain(tls_domain_t* d)
80 80
 {
81 81
 	int i;
82
+	int procs_no;
83
+	
82 84
 	if (!d) return;
83 85
 	if (d->ctx) {
84
-		for(i = 0; i < process_count; i++) {
86
+		procs_no=get_max_procs();
87
+		for(i = 0; i < procs_no; i++) {
85 88
 			if (d->ctx[i]) SSL_CTX_free(d->ctx[i]);
86 89
 		}
87 90
 		shm_free(d->ctx);
... ...
@@ -188,13 +191,15 @@ static int fill_missing(tls_domain_t* d, tls_domain_t* parent)
188 188
 static int load_cert(tls_domain_t* d)
189 189
 {
190 190
 	int i;
191
+	int procs_no;
191 192
 
192 193
 	if (!d->cert_file) {
193 194
 		DBG("%s: No certificate configured\n", tls_domain_str(d));
194 195
 		return 0;
195 196
 	}
196 197
 
197
-	for(i = 0; i < process_count; i++) {
198
+	procs_no=get_max_procs();
199
+	for(i = 0; i < procs_no; i++) {
198 200
 		if (!SSL_CTX_use_certificate_chain_file(d->ctx[i], d->cert_file)) {
199 201
 			ERR("%s: Unable to load certificate file '%s'\n",
200 202
 			    tls_domain_str(d), d->cert_file);
... ...
@@ -213,13 +218,15 @@ static int load_cert(tls_domain_t* d)
213 213
 static int load_ca_list(tls_domain_t* d)
214 214
 {
215 215
 	int i;
216
+	int procs_no;
216 217
 
217 218
 	if (!d->ca_file) {
218 219
 		DBG("%s: No CA list configured\n", tls_domain_str(d));
219 220
 		return 0;
220 221
 	}
221 222
 
222
-	for(i = 0; i < process_count; i++) {
223
+	procs_no=get_max_procs();
224
+	for(i = 0; i < procs_no; i++) {
223 225
 		if (SSL_CTX_load_verify_locations(d->ctx[i], d->ca_file, 0) != 1) {
224 226
 			ERR("%s: Unable to load CA list '%s'\n", tls_domain_str(d), d->ca_file);
225 227
 			TLS_ERR("load_ca_list:");
... ...
@@ -242,9 +249,11 @@ static int load_ca_list(tls_domain_t* d)
242 242
 static int set_cipher_list(tls_domain_t* d)
243 243
 {
244 244
 	int i;
245
+	int procs_no;
245 246
 
246 247
 	if (!d->cipher_list) return 0;
247
-	for(i = 0; i < process_count; i++) {
248
+	procs_no=get_max_procs();
249
+	for(i = 0; i < procs_no; i++) {
248 250
 		if (SSL_CTX_set_cipher_list(d->ctx[i], d->cipher_list) == 0 ) {
249 251
 			ERR("%s: Failure to set SSL context cipher list\n", tls_domain_str(d));
250 252
 			return -1;
... ...
@@ -260,6 +269,7 @@ static int set_cipher_list(tls_domain_t* d)
260 260
 static int set_verification(tls_domain_t* d)
261 261
 {
262 262
 	int verify_mode, i;
263
+	int procs_no;
263 264
 
264 265
 	if (d->require_cert) {
265 266
 		verify_mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
... ...
@@ -287,7 +297,8 @@ static int set_verification(tls_domain_t* d)
287 287
 		}
288 288
 	}
289 289
 	
290
-	for(i = 0; i < process_count; i++) {
290
+	procs_no=get_max_procs();
291
+	for(i = 0; i < procs_no; i++) {
291 292
 		SSL_CTX_set_verify(d->ctx[i], verify_mode, 0);
292 293
 		SSL_CTX_set_verify_depth(d->ctx[i], d->verify_depth);
293 294
 		
... ...
@@ -302,7 +313,10 @@ static int set_verification(tls_domain_t* d)
302 302
 static int set_ssl_options(tls_domain_t* d)
303 303
 {
304 304
 	int i;
305
-	for(i = 0; i < process_count; i++) {
305
+	int procs_no;
306
+	
307
+	procs_no=get_max_procs();
308
+	for(i = 0; i < procs_no; i++) {
306 309
 #if OPENSSL_VERSION_NUMBER >= 0x000907000
307 310
 		SSL_CTX_set_options(d->ctx[i], 
308 311
 				    SSL_OP_ALL | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | SSL_OP_CIPHER_SERVER_PREFERENCE);
... ...
@@ -321,7 +335,10 @@ static int set_ssl_options(tls_domain_t* d)
321 321
 static int set_session_cache(tls_domain_t* d)
322 322
 {
323 323
 	int i;
324
-	for(i = 0; i < process_count; i++) {
324
+	int procs_no;
325
+	
326
+	procs_no=get_max_procs();
327
+	for(i = 0; i < procs_no; i++) {
325 328
 		     /* janakj: I am not sure if session cache makes sense in ser, session 
326 329
 		      * cache is stored in SSL_CTX and we have one SSL_CTX per process, thus 
327 330
 		      * sessions among processes will not be reused
... ...
@@ -342,16 +359,18 @@ static int set_session_cache(tls_domain_t* d)
342 342
 static int fix_domain(tls_domain_t* d, tls_domain_t* def)
343 343
 {
344 344
 	int i;
345
+	int procs_no;
345 346
 
346 347
 	if (fill_missing(d, def) < 0) return -1;
347 348
 
348
-	d->ctx = (SSL_CTX**)shm_malloc(sizeof(SSL_CTX*) * process_count);
349
+	procs_no=get_max_procs();
350
+	d->ctx = (SSL_CTX**)shm_malloc(sizeof(SSL_CTX*) * procs_no);
349 351
 	if (!d->ctx) {
350 352
 		ERR("%s: Cannot allocate shared memory\n", tls_domain_str(d));
351 353
 		return -1;
352 354
 	}
353
-	memset(d->ctx, 0, sizeof(SSL_CTX*) * process_count);
354
-	for(i = 0; i < process_count; i++) {
355
+	memset(d->ctx, 0, sizeof(SSL_CTX*) * procs_no);
356
+	for(i = 0; i < procs_no; i++) {
355 357
 		d->ctx[i] = SSL_CTX_new(ssl_methods[d->method - 1]);
356 358
 		if (d->ctx[i] == NULL) {
357 359
 			ERR("%s: Cannot create SSL context\n", tls_domain_str(d));
... ...
@@ -410,13 +429,15 @@ static int passwd_cb(char *buf, int size, int rwflag, void *filename)
410 410
 static int load_private_key(tls_domain_t* d)
411 411
 {
412 412
 	int idx, ret_pwd, i;
413
+	int procs_no;
413 414
 	
414 415
 	if (!d->pkey_file) {
415 416
 		DBG("%s: No private key specified\n", tls_domain_str(d));
416 417
 		return 0;
417 418
 	}
418 419
 
419
-	for(i = 0; i < process_count; i++) {
420
+	procs_no=get_max_procs();
421
+	for(i = 0; i < procs_no; i++) {
420 422
 		SSL_CTX_set_default_passwd_cb(d->ctx[i], passwd_cb);
421 423
 		SSL_CTX_set_default_passwd_cb_userdata(d->ctx[i], d->pkey_file);
422 424
 		
... ...
@@ -80,7 +80,7 @@ static int mod_init(void)
80 80
 	     /* Signal to the core that we will be
81 81
 	      * creating additional processes
82 82
 	      */
83
-	if (unixsock_name) process_count += unixsock_children;
83
+	if (unixsock_name) register_procs(unixsock_children);
84 84
 	return 0;
85 85
 }
86 86
 
... ...
@@ -261,56 +261,19 @@ int init_unixsock_children(void)
261 261
 	}
262 262
 
263 263
 	for(i = 0; i < unixsock_children; i++) {
264
-		last_process++;
265
-#ifdef USE_TCP
266
-		if(!tcp_disable){
267
- 			if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
268
-				ERR("socketpair failed: %s\n", strerror(errno));
269
-				return -1;
270
-			}
271
-		}
272
-#endif
273
-		pid = fork();
264
+		pid = fork_process(PROC_UNIXSOCK,"unix domain socket",1);
274 265
 		if (pid < 0) {
275 266
 			ERR("Unable to fork: %s\n", strerror(errno));
276 267
 			close(rx_sock);
277 268
 			close(tx_sock);
278 269
 			return -1;
279 270
 		} else if (pid == 0) { /* child */
280
-			process_no = last_process;
281
-			is_main = 0;
282
-#ifdef USE_TCP
283
-			if (!tcp_disable){
284
-				close(sockfd[0]);
285
-				unix_tcp_sock=sockfd[1];
286
-			}
287
-#endif
288
-			/* record pid twice to avoid the child using it, before
289
-			 * parent gets a chance to set it*/
290
-			pt[process_no].pid=getpid();
291
-			if (init_child(PROC_UNIXSOCK) < 0) {
292
-				ERR("Error in init_child\n");
293
-				close(rx_sock);
294
-				close(tx_sock);
295
-				return -1;
296
-			}
297 271
 			unix_server_loop(); /* Never returns */
298 272
 		}
299
-
300
-		     /* Parent */
301
-		pt[last_process].pid = pid;
273
+		/* Parent */
302 274
 		snprintf(pt[last_process].desc, MAX_PT_DESC, 
303 275
 			"unix domain socket server @ %s", 
304 276
 			 unixsock_name);
305
-#ifdef USE_TCP
306
-		if (!tcp_disable){
307
-			close(sockfd[1]);
308
-			pt[last_process].unix_sock=sockfd[0];
309
-			pt[last_process].idx=-1; /* this is not a "tcp"
310
-									  process*/
311
-		}
312
-#endif
313
-
314 277
 	}
315 278
 
316 279
 	DBG("Unix domain socket server successfully initialized @ %s\n", unixsock_name);