浏览代码

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

Andrei Pelinescu-Onciul 18 年之前
父节点
当前提交
63b50c563f
共有 4 个文件被更改,包括 65 次插入0 次删除
  1. 1 0
      main.c
  2. 58 0
      pt.c
  3. 5 0
      pt.h
  4. 1 0
      sr_module.h

+ 1 - 0
main.c

@@ -1160,6 +1160,7 @@ int main_loop()
 	/* main */
 	/* main */
 	strncpy(pt[0].desc, "attendant", MAX_PT_DESC );
 	strncpy(pt[0].desc, "attendant", MAX_PT_DESC );
 #ifdef USE_TCP
 #ifdef USE_TCP
+	close_extra_socks(PROC_ATTENDANT, get_proc_no());
 	if(!tcp_disable){
 	if(!tcp_disable){
 		/* main's tcp sockets are disabled by default from init_pt() */
 		/* main's tcp sockets are disabled by default from init_pt() */
 		unix_tcp_sock=-1;
 		unix_tcp_sock=-1;

+ 58 - 0
pt.c

@@ -42,6 +42,7 @@
 #include "pt.h"
 #include "pt.h"
 #include "tcp_init.h"
 #include "tcp_init.h"
 #include "sr_module.h"
 #include "sr_module.h"
+#include "socket_info.h"
 #include "rand/fastrand.h"
 #include "rand/fastrand.h"
 
 
 #include <stdio.h>
 #include <stdio.h>
@@ -184,6 +185,47 @@ int my_pid()
 
 
 
 
 
 
+/* close unneeded sockets */
+int close_extra_socks(int child_id, int proc_no)
+{
+#ifdef USE_TCP
+	int r;
+	struct socket_info* si;
+	
+	if (child_id!=PROC_TCP_MAIN){
+		for (r=0; r<proc_no; r++){
+			if (pt[r].unix_sock>=0){
+				/* we can't change the value in pt[] because it's
+				 * shared so we only close it */
+				close(pt[r].unix_sock);
+			}
+		}
+		/* close all listen sockets (needed only in tcp_main */
+		if (!tcp_disable){
+			for(si=tcp_listen; si; si=si->next){
+				close(si->socket);
+				/* safe to change since this is a per process copy */
+				si->socket=-1;
+			}
+#ifdef USE_TLS
+			if (!tls_disable){
+				for(si=tls_listen; si; si=si->next){
+					close(si->socket);
+					/* safe to change since this is a per process copy */
+					si->socket=-1;
+				}
+			}
+#endif /* USE_TLS */
+		}
+		/* we still need the udp sockets (for sending) so we don't close them
+		 * too */
+	}
+#endif /* USE_TCP */
+	return 0;
+}
+
+
+
 /**
 /**
  * Forks a new process.
  * Forks a new process.
  * @param child_id - rank, if equal to PROC_NOCHLDINIT init_child will not be
  * @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)
 		/* child */
 		/* child */
 		is_main=0; /* a forked process cannot be the "main" one */
 		is_main=0; /* a forked process cannot be the "main" one */
 		process_no=child_process_no;
 		process_no=child_process_no;
+		/* close tcp unix sockets if this is not tcp main */
+#ifdef USE_TCP
+		close_extra_socks(child_id, process_no);
+#endif /* USE_TCP */
 		srand(new_seed1);
 		srand(new_seed1);
 		fastrand_seed(rand());
 		fastrand_seed(rand());
 		srandom(new_seed2+time(0));
 		srandom(new_seed2+time(0));
@@ -325,6 +371,7 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
 	int sockfd[2];
 	int sockfd[2];
 	int reader_fd[2]; /* for comm. with the tcp children read  */
 	int reader_fd[2]; /* for comm. with the tcp children read  */
 	int ret;
 	int ret;
+	int i;
 	unsigned int new_seed1;
 	unsigned int new_seed1;
 	unsigned int new_seed2;
 	unsigned int new_seed2;
 	
 	
@@ -381,6 +428,17 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
 	if (pid==0){
 	if (pid==0){
 		is_main=0; /* a forked process cannot be the "main" one */
 		is_main=0; /* a forked process cannot be the "main" one */
 		process_no=child_process_no;
 		process_no=child_process_no;
+		/* close unneeded unix sockets */
+		close_extra_socks(child_id, process_no);
+		/* same for unneeded tcp_children <-> tcp_main unix socks */
+		for (i=0; i<r; i++){
+			if (tcp_children[i].unix_sock>=0){
+				close(tcp_children[i].unix_sock);
+				/* tcp_children is per process, so it's safe to change
+				 * the unix_sock to -1 */
+				tcp_children[i].unix_sock=-1;
+			}
+		}
 		srand(new_seed1);
 		srand(new_seed1);
 		fastrand_seed(rand());
 		fastrand_seed(rand());
 		srandom(new_seed2+time(0));
 		srandom(new_seed2+time(0));

+ 5 - 0
pt.h

@@ -73,6 +73,11 @@ int register_procs(int no);
 int get_max_open_fds();
 int get_max_open_fds();
 int register_fds(int no);
 int register_fds(int no);
 
 
+
+int close_extra_socks(int proc_id, int proc_no);
+
+#define get_proc_no() ((process_count)?*process_count:0)
+
 /* return processes pid */
 /* return processes pid */
 int my_pid();
 int my_pid();
 
 

+ 1 - 0
sr_module.h

@@ -91,6 +91,7 @@ typedef int (*param_func_t)( modparam_t type, void* val);
 #define PROC_FIFO      PROC_RPC  /* FIFO attendant process */
 #define PROC_FIFO      PROC_RPC  /* FIFO attendant process */
 #define PROC_TCP_MAIN -4  /* TCP main process */
 #define PROC_TCP_MAIN -4  /* TCP main process */
 #define PROC_UNIXSOCK -5  /* Unix socket server */
 #define PROC_UNIXSOCK -5  /* Unix socket server */
+#define PROC_ATTENDANT -10  /* main "attendant process */
 #define PROC_INIT     -127 /* special rank, the context is the main ser
 #define PROC_INIT     -127 /* special rank, the context is the main ser
 							  process, but this is guaranteed to be executed
 							  process, but this is guaranteed to be executed
 							  before any process is forked, so it can be used
 							  before any process is forked, so it can be used