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 204
 TLS_SEND_TIMEOUT	"tls_send_timeout"
204 205
 ADVERTISED_ADDRESS	"advertised_address"
205 206
 ADVERTISED_PORT		"advertised_port"
207
+DISABLE_CORE		"disable_core_dump"
208
+OPEN_FD_LIMIT		"open_files_limit"
206 209
 
207 210
 LOADMODULE	loadmodule
208 211
 MODPARAM        modparam
... ...
@@ -379,6 +382,10 @@ EAT_ABLE	[\ \t\b\r]
379 382
 									return ADVERTISED_ADDRESS; }
380 383
 <INITIAL>{ADVERTISED_PORT}		{	count(); yylval.strval=yytext;
381 384
 									return ADVERTISED_PORT; }
385
+<INITIAL>{DISABLE_CORE}		{	count(); yylval.strval=yytext;
386
+									return DISABLE_CORE; }
387
+<INITIAL>{OPEN_FD_LIMIT}		{	count(); yylval.strval=yytext;
388
+									return OPEN_FD_LIMIT; }
382 389
 <INITIAL>{LOADMODULE}	{ count(); yylval.strval=yytext; return LOADMODULE; }
383 390
 <INITIAL>{MODPARAM}     { count(); yylval.strval=yytext; return MODPARAM; }
384 391
 
... ...
@@ -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 236
 %token TLS_CA_LIST
236 237
 %token ADVERTISED_ADDRESS
237 238
 %token ADVERTISED_PORT
239
+%token DISABLE_CORE
240
+%token OPEN_FD_LIMIT
238 241
 
239 242
 
240 243
 
... ...
@@ -637,6 +640,14 @@ assign_stm:	DEBUG EQUAL NUMBER { debug=$3; }
637 640
 								}
638 641
 		|ADVERTISED_PORT EQUAL error {yyerror("ip address or hostname "
639 642
 												"expected"); }
643
+		| DISABLE_CORE EQUAL NUMBER {
644
+										disable_core_dump=$3;
645
+									}
646
+		| DISABLE_CORE EQUAL error { yyerror("boolean value expected"); }
647
+		| OPEN_FD_LIMIT EQUAL NUMBER {
648
+										open_files_limit=$3;
649
+									}
650
+		| OPEN_FD_LIMIT EQUAL error { yyerror("number expected"); }
640 651
 		| error EQUAL { yyerror("unknown config variable"); }
641 652
 	;
642 653
 
... ...
@@ -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 41
 #include <string.h>
41 42
 #include <stdio.h>
42 43
 #include <stdlib.h>
44
+#include <sys/time.h>      /* setrlimit */
45
+#include <sys/resource.h> /* setrlimit */
43 46
 
44 47
 #include "daemonize.h"
45 48
 #include "globals.h"
... ...
@@ -178,3 +181,110 @@ error:
178 181
 }
179 182
 
180 183
 
184
+
185
+/* try to increase the open file limit */
186
+int increase_open_fds(int target)
187
+{
188
+	struct rlimit lim;
189
+	struct rlimit orig;
190
+	
191
+	if (getrlimit(RLIMIT_NOFILE, &lim)<0){
192
+		LOG(L_CRIT, "cannot get the maximum number of file descriptors: %s\n",
193
+				strerror(errno));
194
+		goto error;
195
+	}
196
+	orig=lim;
197
+	DBG("current open file limits: %lu/%lu\n",
198
+			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
199
+	if ((lim.rlim_cur==RLIM_INFINITY) || (target<=lim.rlim_cur))
200
+		/* nothing to do */
201
+		goto done;
202
+	else if ((lim.rlim_max==RLIM_INFINITY) || (target<=lim.rlim_max)){
203
+		lim.rlim_cur=target; /* increase soft limit to target */
204
+	}else{
205
+		/* more than the hard limit */
206
+		LOG(L_INFO, "trying to increase the open file limit"
207
+				" past the hard limit (%ld -> %d)\n", 
208
+				(unsigned long)lim.rlim_max, target);
209
+		lim.rlim_max=target;
210
+		lim.rlim_cur=target;
211
+	}
212
+	DBG("increasing open file limits to: %lu/%lu\n",
213
+			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
214
+	if (setrlimit(RLIMIT_NOFILE, &lim)<0){
215
+		LOG(L_CRIT, "cannot increase the open file limit to"
216
+				" %lu/%lu: %s\n",
217
+				(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max,
218
+				strerror(errno));
219
+		if (orig.rlim_max>orig.rlim_cur){
220
+			/* try to increase to previous maximum, better than not increasing
221
+		 	* at all */
222
+			lim.rlim_max=orig.rlim_max;
223
+			lim.rlim_cur=orig.rlim_max;
224
+			if (setrlimit(RLIMIT_NOFILE, &lim)==0){
225
+				LOG(L_CRIT, " maximum number of file descriptors increased to"
226
+						" %u\n",(unsigned)orig.rlim_max);
227
+			}
228
+		}
229
+		goto error;
230
+	}
231
+done:
232
+	return 0;
233
+error:
234
+	return -1;
235
+}
236
+
237
+
238
+
239
+/* enable core dumps */
240
+int set_core_dump(int enable, int size)
241
+{
242
+	struct rlimit lim;
243
+	struct rlimit newlim;
244
+	
245
+	if (enable){
246
+		if (getrlimit(RLIMIT_CORE, &lim)<0){
247
+			LOG(L_CRIT, "cannot get the maximum core size: %s\n",
248
+					strerror(errno));
249
+			goto error;
250
+		}
251
+		if (lim.rlim_cur<size){
252
+			/* first try max limits */
253
+			newlim.rlim_max=RLIM_INFINITY;
254
+			newlim.rlim_cur=newlim.rlim_max;
255
+			if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
256
+			/* now try with size */
257
+			if (lim.rlim_max<size){
258
+				newlim.rlim_max=size;
259
+			}
260
+			newlim.rlim_cur=newlim.rlim_max;
261
+			if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
262
+			/* if this failed too, try rlim_max, better than nothing */
263
+			newlim.rlim_max=lim.rlim_max;
264
+			newlim.rlim_cur=newlim.rlim_max;
265
+			if (setrlimit(RLIMIT_CORE, &newlim)<0){
266
+				LOG(L_CRIT, "could increase core limits at all: %s\n",
267
+						strerror (errno));
268
+			}else{
269
+				LOG(L_CRIT, "core limits increased only to %lu\n",
270
+						(unsigned long)lim.rlim_max);
271
+			}
272
+			goto error; /* it's an error we haven't got the size we wanted*/
273
+		}
274
+		goto done; /*nothing to do */
275
+	}else{
276
+		/* disable */
277
+		newlim.rlim_cur=0;
278
+		newlim.rlim_max=0;
279
+		if (setrlimit(RLIMIT_CORE, &newlim)<0){
280
+			LOG(L_CRIT, "failed to disable core dumps: %s\n",
281
+					strerror(errno));
282
+			goto error;
283
+		}
284
+	}
285
+done:
286
+	DBG("core dump limits set to %lu\n", (unsigned long)newlim.rlim_cur);
287
+	return 0;
288
+error:
289
+	return -1;
290
+}
... ...
@@ -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 329
 char* group=0;
328 330
 int uid = 0;
329 331
 int gid = 0;
332
+/* more config stuff */
333
+int disable_core_dump=0; /* by default enabled */
334
+int open_files_limit=-1; /* don't touch it by default */
330 335
 /* a hint to reply modules whether they should send reply
331 336
    to IP advertised in Via or IP from which a request came
332 337
 */
... ...
@@ -1370,10 +1375,6 @@ try_again:
1370 1375
 		goto error;
1371 1376
 	}
1372 1377
 	
1373
-	if (init_modules() != 0) {
1374
-		fprintf(stderr, "ERROR: error while initializing modules\n");
1375
-		goto error;
1376
-	}
1377 1378
 	
1378 1379
 	/*alloc pids*/
1379 1380
 #ifdef SHM_MEM
... ...
@@ -1386,6 +1387,20 @@ try_again:
1386 1387
 		goto error;
1387 1388
 	}
1388 1389
 	memset(pt, 0, sizeof(struct process_table)*process_count());
1390
+
1391
+	if (disable_core_dump) set_core_dump(0, 0);
1392
+	else set_core_dump(1, shm_mem_size+PKG_MEM_POOL_SIZE+4*1024*1024);
1393
+	if (open_files_limit>0){
1394
+		if(increase_open_fds(open_files_limit)<0){ 
1395
+			fprintf(stderr, "ERROR: error could not increase file limits\n");
1396
+			goto error;
1397
+		}
1398
+	}
1399
+	
1400
+	if (init_modules() != 0) {
1401
+		fprintf(stderr, "ERROR: error while initializing modules\n");
1402
+		goto error;
1403
+	}
1389 1404
 	/* fix routing lists */
1390 1405
 	if ( (r=fix_rls())!=0){
1391 1406
 		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);*/