Browse code

- close all the unneeded fds in child processes (=> significantly less fds used)

Andrei Pelinescu-Onciul authored on 05/12/2007 20:26:58
Showing 4 changed files
... ...
@@ -1160,6 +1160,7 @@ int main_loop()
1160 1160
 	/* main */
1161 1161
 	strncpy(pt[0].desc, "attendant", MAX_PT_DESC );
1162 1162
 #ifdef USE_TCP
1163
+	close_extra_socks(PROC_ATTENDANT, get_proc_no());
1163 1164
 	if(!tcp_disable){
1164 1165
 		/* main's tcp sockets are disabled by default from init_pt() */
1165 1166
 		unix_tcp_sock=-1;
... ...
@@ -42,6 +42,7 @@
42 42
 #include "pt.h"
43 43
 #include "tcp_init.h"
44 44
 #include "sr_module.h"
45
+#include "socket_info.h"
45 46
 #include "rand/fastrand.h"
46 47
 
47 48
 #include <stdio.h>
... ...
@@ -184,6 +185,47 @@ int my_pid()
184 185
 
185 186
 
186 187
 
188
+/* close unneeded sockets */
189
+int close_extra_socks(int child_id, int proc_no)
190
+{
191
+#ifdef USE_TCP
192
+	int r;
193
+	struct socket_info* si;
194
+	
195
+	if (child_id!=PROC_TCP_MAIN){
196
+		for (r=0; r<proc_no; r++){
197
+			if (pt[r].unix_sock>=0){
198
+				/* we can't change the value in pt[] because it's
199
+				 * shared so we only close it */
200
+				close(pt[r].unix_sock);
201
+			}
202
+		}
203
+		/* close all listen sockets (needed only in tcp_main */
204
+		if (!tcp_disable){
205
+			for(si=tcp_listen; si; si=si->next){
206
+				close(si->socket);
207
+				/* safe to change since this is a per process copy */
208
+				si->socket=-1;
209
+			}
210
+#ifdef USE_TLS
211
+			if (!tls_disable){
212
+				for(si=tls_listen; si; si=si->next){
213
+					close(si->socket);
214
+					/* safe to change since this is a per process copy */
215
+					si->socket=-1;
216
+				}
217
+			}
218
+#endif /* USE_TLS */
219
+		}
220
+		/* we still need the udp sockets (for sending) so we don't close them
221
+		 * too */
222
+	}
223
+#endif /* USE_TCP */
224
+	return 0;
225
+}
226
+
227
+
228
+
187 229
 /**
188 230
  * Forks a new process.
189 231
  * @param child_id - rank, if equal to PROC_NOCHLDINIT init_child will not be
... ...
@@ -246,6 +288,10 @@ int fork_process(int child_id, char *desc, int make_sock)
246 288
 		/* child */
247 289
 		is_main=0; /* a forked process cannot be the "main" one */
248 290
 		process_no=child_process_no;
291
+		/* close tcp unix sockets if this is not tcp main */
292
+#ifdef USE_TCP
293
+		close_extra_socks(child_id, process_no);
294
+#endif /* USE_TCP */
249 295
 		srand(new_seed1);
250 296
 		fastrand_seed(rand());
251 297
 		srandom(new_seed2+time(0));
... ...
@@ -325,6 +371,7 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
325 371
 	int sockfd[2];
326 372
 	int reader_fd[2]; /* for comm. with the tcp children read  */
327 373
 	int ret;
374
+	int i;
328 375
 	unsigned int new_seed1;
329 376
 	unsigned int new_seed2;
330 377
 	
... ...
@@ -381,6 +428,17 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
381 428
 	if (pid==0){
382 429
 		is_main=0; /* a forked process cannot be the "main" one */
383 430
 		process_no=child_process_no;
431
+		/* close unneeded unix sockets */
432
+		close_extra_socks(child_id, process_no);
433
+		/* same for unneeded tcp_children <-> tcp_main unix socks */
434
+		for (i=0; i<r; i++){
435
+			if (tcp_children[i].unix_sock>=0){
436
+				close(tcp_children[i].unix_sock);
437
+				/* tcp_children is per process, so it's safe to change
438
+				 * the unix_sock to -1 */
439
+				tcp_children[i].unix_sock=-1;
440
+			}
441
+		}
384 442
 		srand(new_seed1);
385 443
 		fastrand_seed(rand());
386 444
 		srandom(new_seed2+time(0));
... ...
@@ -73,6 +73,11 @@ int register_procs(int no);
73 73
 int get_max_open_fds();
74 74
 int register_fds(int no);
75 75
 
76
+
77
+int close_extra_socks(int proc_id, int proc_no);
78
+
79
+#define get_proc_no() ((process_count)?*process_count:0)
80
+
76 81
 /* return processes pid */
77 82
 int my_pid();
78 83
 
... ...
@@ -91,6 +91,7 @@ typedef int (*param_func_t)( modparam_t type, void* val);
91 91
 #define PROC_FIFO      PROC_RPC  /* FIFO attendant process */
92 92
 #define PROC_TCP_MAIN -4  /* TCP main process */
93 93
 #define PROC_UNIXSOCK -5  /* Unix socket server */
94
+#define PROC_ATTENDANT -10  /* main "attendant process */
94 95
 #define PROC_INIT     -127 /* special rank, the context is the main ser
95 96
 							  process, but this is guaranteed to be executed
96 97
 							  before any process is forked, so it can be used