瀏覽代碼

- core dump limits are by default set to unlimited or a high enough
value (to disable set disable_core_dump=yes in ser.cfg).
- open file limits set-able in ser.cfg, use open_files_limit=no
to make ser automatically try to increase this limit (if no<current limit
nothing will be done). You must start ser as root to be able to increase
its open file limit past the hardlimit (default 1024 on most systems).
- move process table init. before module init. (sanity)

Andrei Pelinescu-Onciul 21 年之前
父節點
當前提交
385c63ebd1
共有 7 個文件被更改,包括 154 次插入5 次删除
  1. 7 0
      cfg.lex
  2. 11 0
      cfg.y
  3. 110 0
      daemonize.c
  4. 2 0
      daemonize.h
  5. 4 0
      globals.h
  6. 19 4
      main.c
  7. 1 1
      tcp_main.c

+ 7 - 0
cfg.lex

@@ -45,6 +45,7 @@
  *  2003-10-28  added tcp_accept_aliases (andrei)
  *  2003-11-29  added {tcp_send, tcp_connect, tls_*}_timeout (andrei)
  *  2004-02-24  added LOAD_AVP_T and AVP_TO_URI_T (bogdan)
+ * 2004-03-30  added DISABLE_CORE and OPEN_FD_LIMIT (andrei)
  */
 
 
@@ -203,6 +204,8 @@ TLS_HANDSHAKE_TIMEOUT	"tls_handshake_timeout"
 TLS_SEND_TIMEOUT	"tls_send_timeout"
 ADVERTISED_ADDRESS	"advertised_address"
 ADVERTISED_PORT		"advertised_port"
+DISABLE_CORE		"disable_core_dump"
+OPEN_FD_LIMIT		"open_files_limit"
 
 LOADMODULE	loadmodule
 MODPARAM        modparam
@@ -379,6 +382,10 @@ EAT_ABLE	[\ \t\b\r]
 									return ADVERTISED_ADDRESS; }
 <INITIAL>{ADVERTISED_PORT}		{	count(); yylval.strval=yytext;
 									return ADVERTISED_PORT; }
+<INITIAL>{DISABLE_CORE}		{	count(); yylval.strval=yytext;
+									return DISABLE_CORE; }
+<INITIAL>{OPEN_FD_LIMIT}		{	count(); yylval.strval=yytext;
+									return OPEN_FD_LIMIT; }
 <INITIAL>{LOADMODULE}	{ count(); yylval.strval=yytext; return LOADMODULE; }
 <INITIAL>{MODPARAM}     { count(); yylval.strval=yytext; return MODPARAM; }
 

+ 11 - 0
cfg.y

@@ -51,6 +51,7 @@
  * 2003-10-28  added tcp_accept_aliases (andrei)
  * 2003-11-20  added {tcp_connect, tcp_send, tls_*}_timeout (andrei)
  * 2004-02-24  added LOAD_AVP_T and AVP_TO_URI_T (bogdan)
+ * 2004-03-30  added DISABLE_CORE and OPEN_FD_LIMIT (andrei)
  */
 
 
@@ -235,6 +236,8 @@ static struct id_list* mk_listen_id(char*, int, int);
 %token TLS_CA_LIST
 %token ADVERTISED_ADDRESS
 %token ADVERTISED_PORT
+%token DISABLE_CORE
+%token OPEN_FD_LIMIT
 
 
 
@@ -637,6 +640,14 @@ assign_stm:	DEBUG EQUAL NUMBER { debug=$3; }
 								}
 		|ADVERTISED_PORT EQUAL error {yyerror("ip address or hostname "
 												"expected"); }
+		| DISABLE_CORE EQUAL NUMBER {
+										disable_core_dump=$3;
+									}
+		| DISABLE_CORE EQUAL error { yyerror("boolean value expected"); }
+		| OPEN_FD_LIMIT EQUAL NUMBER {
+										open_files_limit=$3;
+									}
+		| OPEN_FD_LIMIT EQUAL error { yyerror("number expected"); }
 		| error EQUAL { yyerror("unknown config variable"); }
 	;
 

+ 110 - 0
daemonize.c

@@ -30,6 +30,7 @@
  * --------
  *  2004-02-20  removed from ser main.c into its own file (andrei)
  *  2004-03-04  moved setuid/setgid in do_suid() (andrei)
+ *  2004-03-25  added increase_open_fds & set_core_dump (andrei)
  */
 
 #include <sys/types.h>
@@ -40,6 +41,8 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/time.h>      /* setrlimit */
+#include <sys/resource.h> /* setrlimit */
 
 #include "daemonize.h"
 #include "globals.h"
@@ -178,3 +181,110 @@ error:
 }
 
 
+
+/* try to increase the open file limit */
+int increase_open_fds(int target)
+{
+	struct rlimit lim;
+	struct rlimit orig;
+	
+	if (getrlimit(RLIMIT_NOFILE, &lim)<0){
+		LOG(L_CRIT, "cannot get the maximum number of file descriptors: %s\n",
+				strerror(errno));
+		goto error;
+	}
+	orig=lim;
+	DBG("current open file limits: %lu/%lu\n",
+			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
+	if ((lim.rlim_cur==RLIM_INFINITY) || (target<=lim.rlim_cur))
+		/* nothing to do */
+		goto done;
+	else if ((lim.rlim_max==RLIM_INFINITY) || (target<=lim.rlim_max)){
+		lim.rlim_cur=target; /* increase soft limit to target */
+	}else{
+		/* more than the hard limit */
+		LOG(L_INFO, "trying to increase the open file limit"
+				" past the hard limit (%ld -> %d)\n", 
+				(unsigned long)lim.rlim_max, target);
+		lim.rlim_max=target;
+		lim.rlim_cur=target;
+	}
+	DBG("increasing open file limits to: %lu/%lu\n",
+			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
+	if (setrlimit(RLIMIT_NOFILE, &lim)<0){
+		LOG(L_CRIT, "cannot increase the open file limit to"
+				" %lu/%lu: %s\n",
+				(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max,
+				strerror(errno));
+		if (orig.rlim_max>orig.rlim_cur){
+			/* try to increase to previous maximum, better than not increasing
+		 	* at all */
+			lim.rlim_max=orig.rlim_max;
+			lim.rlim_cur=orig.rlim_max;
+			if (setrlimit(RLIMIT_NOFILE, &lim)==0){
+				LOG(L_CRIT, " maximum number of file descriptors increased to"
+						" %u\n",(unsigned)orig.rlim_max);
+			}
+		}
+		goto error;
+	}
+done:
+	return 0;
+error:
+	return -1;
+}
+
+
+
+/* enable core dumps */
+int set_core_dump(int enable, int size)
+{
+	struct rlimit lim;
+	struct rlimit newlim;
+	
+	if (enable){
+		if (getrlimit(RLIMIT_CORE, &lim)<0){
+			LOG(L_CRIT, "cannot get the maximum core size: %s\n",
+					strerror(errno));
+			goto error;
+		}
+		if (lim.rlim_cur<size){
+			/* first try max limits */
+			newlim.rlim_max=RLIM_INFINITY;
+			newlim.rlim_cur=newlim.rlim_max;
+			if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
+			/* now try with size */
+			if (lim.rlim_max<size){
+				newlim.rlim_max=size;
+			}
+			newlim.rlim_cur=newlim.rlim_max;
+			if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
+			/* if this failed too, try rlim_max, better than nothing */
+			newlim.rlim_max=lim.rlim_max;
+			newlim.rlim_cur=newlim.rlim_max;
+			if (setrlimit(RLIMIT_CORE, &newlim)<0){
+				LOG(L_CRIT, "could increase core limits at all: %s\n",
+						strerror (errno));
+			}else{
+				LOG(L_CRIT, "core limits increased only to %lu\n",
+						(unsigned long)lim.rlim_max);
+			}
+			goto error; /* it's an error we haven't got the size we wanted*/
+		}
+		goto done; /*nothing to do */
+	}else{
+		/* disable */
+		newlim.rlim_cur=0;
+		newlim.rlim_max=0;
+		if (setrlimit(RLIMIT_CORE, &newlim)<0){
+			LOG(L_CRIT, "failed to disable core dumps: %s\n",
+					strerror(errno));
+			goto error;
+		}
+	}
+done:
+	DBG("core dump limits set to %lu\n", (unsigned long)newlim.rlim_cur);
+	return 0;
+error:
+	return -1;
+}

+ 2 - 0
daemonize.h

@@ -36,6 +36,8 @@
 
 int daemonize(char* name);
 int do_suid();
+int increase_open_fds(int target);
+int set_core_dump(int enable, int size);
 
 
 #endif

+ 4 - 0
globals.h

@@ -145,4 +145,8 @@ extern str default_global_address;
 /* pre-ser ports */
 extern str default_global_port;
 
+/* core dump and file limits */
+extern int disable_core_dump;
+extern int open_files_limit;
+
 #endif

+ 19 - 4
main.c

@@ -46,6 +46,8 @@
  *  2003-10-10  added switch for config check (-c) (andrei)
  *  2003-10-24  converted to the new socket_info lists (andrei)
  *  2004-02-06  added support for user pref. - init_avp_child() (bogdan)
+ *  2004-03-30  core dump is enabled by default
+ *              added support for increasing the open files limit    (andrei)
  *
  */
 
@@ -327,6 +329,9 @@ char* user=0;
 char* group=0;
 int uid = 0;
 int gid = 0;
+/* more config stuff */
+int disable_core_dump=0; /* by default enabled */
+int open_files_limit=-1; /* don't touch it by default */
 /* a hint to reply modules whether they should send reply
    to IP advertised in Via or IP from which a request came
 */
@@ -1370,10 +1375,6 @@ try_again:
 		goto error;
 	}
 	
-	if (init_modules() != 0) {
-		fprintf(stderr, "ERROR: error while initializing modules\n");
-		goto error;
-	}
 	
 	/*alloc pids*/
 #ifdef SHM_MEM
@@ -1386,6 +1387,20 @@ try_again:
 		goto error;
 	}
 	memset(pt, 0, sizeof(struct process_table)*process_count());
+
+	if (disable_core_dump) set_core_dump(0, 0);
+	else set_core_dump(1, shm_mem_size+PKG_MEM_POOL_SIZE+4*1024*1024);
+	if (open_files_limit>0){
+		if(increase_open_fds(open_files_limit)<0){ 
+			fprintf(stderr, "ERROR: error could not increase file limits\n");
+			goto error;
+		}
+	}
+	
+	if (init_modules() != 0) {
+		fprintf(stderr, "ERROR: error while initializing modules\n");
+		goto error;
+	}
 	/* fix routing lists */
 	if ( (r=fix_rls())!=0){
 		fprintf(stderr, "ERROR: error %x while trying to fix configuration\n",

+ 1 - 1
tcp_main.c

@@ -1228,7 +1228,7 @@ void tcp_main_loop()
 				bytes=recv_all(pt[r].unix_sock, response, sizeof(response));
 				if (bytes==0){
 					/* EOF -> bad, child has died */
-					LOG(L_CRIT, "BUG: tcp_main_loop: dead child %d\n", r);
+					LOG(L_INFO, "INFO: tcp_main_loop: dead child %d\n", r);
 					/* don't listen on it any more */
 					FD_CLR(pt[r].unix_sock, &master_set);
 					/*exit(-1);*/