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 184
 
185 185
 
186 186
 
187
+/* close unneeded sockets */
188
+int close_extra_socks(int child_id, int proc_no)
189
+{
190
+#ifdef USE_TCP
191
+	int r;
192
+	struct socket_info* si;
193
+	
194
+	if (child_id!=PROC_TCP_MAIN){
195
+		for (r=0; r<proc_no; r++){
196
+			if (pt[r].unix_sock>=0){
197
+				/* we can't change the value in pt[] because it's
198
+				 * shared so we only close it */
199
+				close(pt[r].unix_sock);
200
+			}
201
+		}
202
+		/* close all listen sockets (needed only in tcp_main */
203
+		if (!tcp_disable){
204
+			for(si=tcp_listen; si; si=si->next){
205
+				close(si->socket);
206
+				/* safe to change since this is a per process copy */
207
+				si->socket=-1;
208
+			}
209
+#ifdef USE_TLS
210
+			if (!tls_disable){
211
+				for(si=tls_listen; si; si=si->next){
212
+					close(si->socket);
213
+					/* safe to change since this is a per process copy */
214
+					si->socket=-1;
215
+				}
216
+			}
217
+#endif /* USE_TLS */
218
+		}
219
+		/* we still need the udp sockets (for sending) so we don't close them
220
+		 * too */
221
+	}
222
+#endif /* USE_TCP */
223
+	return 0;
224
+}
225
+
226
+
227
+
187 228
 /**
188 229
  * Forks a new process.
189 230
  * @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 246
 		/* child */
247 247
 		is_main=0; /* a forked process cannot be the "main" one */
248 248
 		process_no=child_process_no;
249
+		/* close tcp unix sockets if this is not tcp main */
250
+#ifdef USE_TCP
251
+		close_extra_socks(child_id, process_no);
252
+#endif /* USE_TCP */
249 253
 		srand(new_seed1);
250 254
 		fastrand_seed(rand());
251 255
 		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 325
 	int sockfd[2];
326 326
 	int reader_fd[2]; /* for comm. with the tcp children read  */
327 327
 	int ret;
328
+	int i;
328 329
 	unsigned int new_seed1;
329 330
 	unsigned int new_seed2;
330 331
 	
... ...
@@ -381,6 +428,17 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
381 381
 	if (pid==0){
382 382
 		is_main=0; /* a forked process cannot be the "main" one */
383 383
 		process_no=child_process_no;
384
+		/* close unneeded unix sockets */
385
+		close_extra_socks(child_id, process_no);
386
+		/* same for unneeded tcp_children <-> tcp_main unix socks */
387
+		for (i=0; i<r; i++){
388
+			if (tcp_children[i].unix_sock>=0){
389
+				close(tcp_children[i].unix_sock);
390
+				/* tcp_children is per process, so it's safe to change
391
+				 * the unix_sock to -1 */
392
+				tcp_children[i].unix_sock=-1;
393
+			}
394
+		}
384 395
 		srand(new_seed1);
385 396
 		fastrand_seed(rand());
386 397
 		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