Browse code

- 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 authored on 30/03/2004 09:44:19
Showing 7 changed files
... ...
@@ -45,6 +45,7 @@
45 45
  *  2003-10-28  added tcp_accept_aliases (andrei)
46 46
  *  2003-11-29  added {tcp_send, tcp_connect, tls_*}_timeout (andrei)
47 47
  *  2004-02-24  added LOAD_AVP_T and AVP_TO_URI_T (bogdan)
48
+ * 2004-03-30  added DISABLE_CORE and OPEN_FD_LIMIT (andrei)
48 49
  */
49 50
 
50 51
 
... ...
@@ -203,6 +204,8 @@ TLS_HANDSHAKE_TIMEOUT	"tls_handshake_timeout"
203 203
 TLS_SEND_TIMEOUT	"tls_send_timeout"
204 204
 ADVERTISED_ADDRESS	"advertised_address"
205 205
 ADVERTISED_PORT		"advertised_port"
206
+DISABLE_CORE		"disable_core_dump"
207
+OPEN_FD_LIMIT		"open_files_limit"
206 208
 
207 209
 LOADMODULE	loadmodule
208 210
 MODPARAM        modparam
... ...
@@ -379,6 +382,10 @@ EAT_ABLE	[\ \t\b\r]
379 379
 									return ADVERTISED_ADDRESS; }
380 380
 <INITIAL>{ADVERTISED_PORT}		{	count(); yylval.strval=yytext;
381 381
 									return ADVERTISED_PORT; }
382
+<INITIAL>{DISABLE_CORE}		{	count(); yylval.strval=yytext;
383
+									return DISABLE_CORE; }
384
+<INITIAL>{OPEN_FD_LIMIT}		{	count(); yylval.strval=yytext;
385
+									return OPEN_FD_LIMIT; }
382 386
 <INITIAL>{LOADMODULE}	{ count(); yylval.strval=yytext; return LOADMODULE; }
383 387
 <INITIAL>{MODPARAM}     { count(); yylval.strval=yytext; return MODPARAM; }
384 388
 
... ...
@@ -51,6 +51,7 @@
51 51
  * 2003-10-28  added tcp_accept_aliases (andrei)
52 52
  * 2003-11-20  added {tcp_connect, tcp_send, tls_*}_timeout (andrei)
53 53
  * 2004-02-24  added LOAD_AVP_T and AVP_TO_URI_T (bogdan)
54
+ * 2004-03-30  added DISABLE_CORE and OPEN_FD_LIMIT (andrei)
54 55
  */
55 56
 
56 57
 
... ...
@@ -235,6 +236,8 @@ static struct id_list* mk_listen_id(char*, int, int);
235 235
 %token TLS_CA_LIST
236 236
 %token ADVERTISED_ADDRESS
237 237
 %token ADVERTISED_PORT
238
+%token DISABLE_CORE
239
+%token OPEN_FD_LIMIT
238 240
 
239 241
 
240 242
 
... ...
@@ -637,6 +640,14 @@ assign_stm:	DEBUG EQUAL NUMBER { debug=$3; }
637 637
 								}
638 638
 		|ADVERTISED_PORT EQUAL error {yyerror("ip address or hostname "
639 639
 												"expected"); }
640
+		| DISABLE_CORE EQUAL NUMBER {
641
+										disable_core_dump=$3;
642
+									}
643
+		| DISABLE_CORE EQUAL error { yyerror("boolean value expected"); }
644
+		| OPEN_FD_LIMIT EQUAL NUMBER {
645
+										open_files_limit=$3;
646
+									}
647
+		| OPEN_FD_LIMIT EQUAL error { yyerror("number expected"); }
640 648
 		| error EQUAL { yyerror("unknown config variable"); }
641 649
 	;
642 650
 
... ...
@@ -30,6 +30,7 @@
30 30
  * --------
31 31
  *  2004-02-20  removed from ser main.c into its own file (andrei)
32 32
  *  2004-03-04  moved setuid/setgid in do_suid() (andrei)
33
+ *  2004-03-25  added increase_open_fds & set_core_dump (andrei)
33 34
  */
34 35
 
35 36
 #include <sys/types.h>
... ...
@@ -40,6 +41,8 @@
40 40
 #include <string.h>
41 41
 #include <stdio.h>
42 42
 #include <stdlib.h>
43
+#include <sys/time.h>      /* setrlimit */
44
+#include <sys/resource.h> /* setrlimit */
43 45
 
44 46
 #include "daemonize.h"
45 47
 #include "globals.h"
... ...
@@ -178,3 +181,110 @@ error:
178 178
 }
179 179
 
180 180
 
181
+
182
+/* try to increase the open file limit */
183
+int increase_open_fds(int target)
184
+{
185
+	struct rlimit lim;
186
+	struct rlimit orig;
187
+	
188
+	if (getrlimit(RLIMIT_NOFILE, &lim)<0){
189
+		LOG(L_CRIT, "cannot get the maximum number of file descriptors: %s\n",
190
+				strerror(errno));
191
+		goto error;
192
+	}
193
+	orig=lim;
194
+	DBG("current open file limits: %lu/%lu\n",
195
+			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
196
+	if ((lim.rlim_cur==RLIM_INFINITY) || (target<=lim.rlim_cur))
197
+		/* nothing to do */
198
+		goto done;
199
+	else if ((lim.rlim_max==RLIM_INFINITY) || (target<=lim.rlim_max)){
200
+		lim.rlim_cur=target; /* increase soft limit to target */
201
+	}else{
202
+		/* more than the hard limit */
203
+		LOG(L_INFO, "trying to increase the open file limit"
204
+				" past the hard limit (%ld -> %d)\n", 
205
+				(unsigned long)lim.rlim_max, target);
206
+		lim.rlim_max=target;
207
+		lim.rlim_cur=target;
208
+	}
209
+	DBG("increasing open file limits to: %lu/%lu\n",
210
+			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
211
+	if (setrlimit(RLIMIT_NOFILE, &lim)<0){
212
+		LOG(L_CRIT, "cannot increase the open file limit to"
213
+				" %lu/%lu: %s\n",
214
+				(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max,
215
+				strerror(errno));
216
+		if (orig.rlim_max>orig.rlim_cur){
217
+			/* try to increase to previous maximum, better than not increasing
218
+		 	* at all */
219
+			lim.rlim_max=orig.rlim_max;
220
+			lim.rlim_cur=orig.rlim_max;
221
+			if (setrlimit(RLIMIT_NOFILE, &lim)==0){
222
+				LOG(L_CRIT, " maximum number of file descriptors increased to"
223
+						" %u\n",(unsigned)orig.rlim_max);
224
+			}
225
+		}
226
+		goto error;
227
+	}
228
+done:
229
+	return 0;
230
+error:
231
+	return -1;
232
+}
233
+
234
+
235
+
236
+/* enable core dumps */
237
+int set_core_dump(int enable, int size)
238
+{
239
+	struct rlimit lim;
240
+	struct rlimit newlim;
241
+	
242
+	if (enable){
243
+		if (getrlimit(RLIMIT_CORE, &lim)<0){
244
+			LOG(L_CRIT, "cannot get the maximum core size: %s\n",
245
+					strerror(errno));
246
+			goto error;
247
+		}
248
+		if (lim.rlim_cur<size){
249
+			/* first try max limits */
250
+			newlim.rlim_max=RLIM_INFINITY;
251
+			newlim.rlim_cur=newlim.rlim_max;
252
+			if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
253
+			/* now try with size */
254
+			if (lim.rlim_max<size){
255
+				newlim.rlim_max=size;
256
+			}
257
+			newlim.rlim_cur=newlim.rlim_max;
258
+			if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
259
+			/* if this failed too, try rlim_max, better than nothing */
260
+			newlim.rlim_max=lim.rlim_max;
261
+			newlim.rlim_cur=newlim.rlim_max;
262
+			if (setrlimit(RLIMIT_CORE, &newlim)<0){
263
+				LOG(L_CRIT, "could increase core limits at all: %s\n",
264
+						strerror (errno));
265
+			}else{
266
+				LOG(L_CRIT, "core limits increased only to %lu\n",
267
+						(unsigned long)lim.rlim_max);
268
+			}
269
+			goto error; /* it's an error we haven't got the size we wanted*/
270
+		}
271
+		goto done; /*nothing to do */
272
+	}else{
273
+		/* disable */
274
+		newlim.rlim_cur=0;
275
+		newlim.rlim_max=0;
276
+		if (setrlimit(RLIMIT_CORE, &newlim)<0){
277
+			LOG(L_CRIT, "failed to disable core dumps: %s\n",
278
+					strerror(errno));
279
+			goto error;
280
+		}
281
+	}
282
+done:
283
+	DBG("core dump limits set to %lu\n", (unsigned long)newlim.rlim_cur);
284
+	return 0;
285
+error:
286
+	return -1;
287
+}
... ...
@@ -36,6 +36,8 @@
36 36
 
37 37
 int daemonize(char* name);
38 38
 int do_suid();
39
+int increase_open_fds(int target);
40
+int set_core_dump(int enable, int size);
39 41
 
40 42
 
41 43
 #endif
... ...
@@ -145,4 +145,8 @@ extern str default_global_address;
145 145
 /* pre-ser ports */
146 146
 extern str default_global_port;
147 147
 
148
+/* core dump and file limits */
149
+extern int disable_core_dump;
150
+extern int open_files_limit;
151
+
148 152
 #endif
... ...
@@ -46,6 +46,8 @@
46 46
  *  2003-10-10  added switch for config check (-c) (andrei)
47 47
  *  2003-10-24  converted to the new socket_info lists (andrei)
48 48
  *  2004-02-06  added support for user pref. - init_avp_child() (bogdan)
49
+ *  2004-03-30  core dump is enabled by default
50
+ *              added support for increasing the open files limit    (andrei)
49 51
  *
50 52
  */
51 53
 
... ...
@@ -327,6 +329,9 @@ char* user=0;
327 327
 char* group=0;
328 328
 int uid = 0;
329 329
 int gid = 0;
330
+/* more config stuff */
331
+int disable_core_dump=0; /* by default enabled */
332
+int open_files_limit=-1; /* don't touch it by default */
330 333
 /* a hint to reply modules whether they should send reply
331 334
    to IP advertised in Via or IP from which a request came
332 335
 */
... ...
@@ -1370,10 +1375,6 @@ try_again:
1370 1370
 		goto error;
1371 1371
 	}
1372 1372
 	
1373
-	if (init_modules() != 0) {
1374
-		fprintf(stderr, "ERROR: error while initializing modules\n");
1375
-		goto error;
1376
-	}
1377 1373
 	
1378 1374
 	/*alloc pids*/
1379 1375
 #ifdef SHM_MEM
... ...
@@ -1386,6 +1387,20 @@ try_again:
1386 1386
 		goto error;
1387 1387
 	}
1388 1388
 	memset(pt, 0, sizeof(struct process_table)*process_count());
1389
+
1390
+	if (disable_core_dump) set_core_dump(0, 0);
1391
+	else set_core_dump(1, shm_mem_size+PKG_MEM_POOL_SIZE+4*1024*1024);
1392
+	if (open_files_limit>0){
1393
+		if(increase_open_fds(open_files_limit)<0){ 
1394
+			fprintf(stderr, "ERROR: error could not increase file limits\n");
1395
+			goto error;
1396
+		}
1397
+	}
1398
+	
1399
+	if (init_modules() != 0) {
1400
+		fprintf(stderr, "ERROR: error while initializing modules\n");
1401
+		goto error;
1402
+	}
1389 1403
 	/* fix routing lists */
1390 1404
 	if ( (r=fix_rls())!=0){
1391 1405
 		fprintf(stderr, "ERROR: error %x while trying to fix configuration\n",
... ...
@@ -1228,7 +1228,7 @@ void tcp_main_loop()
1228 1228
 				bytes=recv_all(pt[r].unix_sock, response, sizeof(response));
1229 1229
 				if (bytes==0){
1230 1230
 					/* EOF -> bad, child has died */
1231
-					LOG(L_CRIT, "BUG: tcp_main_loop: dead child %d\n", r);
1231
+					LOG(L_INFO, "INFO: tcp_main_loop: dead child %d\n", r);
1232 1232
 					/* don't listen on it any more */
1233 1233
 					FD_CLR(pt[r].unix_sock, &master_set);
1234 1234
 					/*exit(-1);*/