<commit_before>/*
	Flexisip, a flexible SIP proxy server with media capabilities.
    Copyright (C) 2010  Belledonne Communications SARL.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <syslog.h>
#include <sys/time.h>
#include <sys/resource.h>

#include <iostream>

#include <mediastreamer2/mscommon.h>
#include "agent.hh"
#include "stun.hh"

#include <stdlib.h>
#include <signal.h>

#include <sofia-sip/su_log.h>

#ifndef VERSION
#define VERSION "DEVEL"
#endif //VERSION


static int run=1;
static su_root_t *root=NULL;


static void usage(const char *arg0){
	printf("%s\n"
	       "\t\t [--port <port number to listen>]\n"
	       "\t\t [--tlsport <port number for sips>]\n"
	       "\t\t [--debug]\n"
	       "\t\t [--daemon]\n"
	       "\t\t [--configfile <path>]\n"
	       "\t\t [--dump-default-config]\n"
	       "\t\t [--help]\n",arg0);
	exit(-1);
}

static void flexisip_stop(int signum){
	LOGD("Received quit signal...");
	run=0;
	if (root){
		su_root_break (root);
	}
}

static void flexisip_stat(int signum){
}

static void syslogHandler(OrtpLogLevel log_level, const char *str, va_list l){
	int syslev=LOG_ALERT;
	switch(log_level){
		case ORTP_DEBUG:
			syslev=LOG_DEBUG;
			break;
		case ORTP_MESSAGE:
			syslev=LOG_INFO;
			break;
/*			
		case ORTP_NOTICE:
			syslev=LOG_NOTICE;
			break;
*/
		 case ORTP_WARNING:
			syslev=LOG_WARNING;
			break;
		case ORTP_ERROR:
			syslev=LOG_ERR;
		case ORTP_FATAL:
			syslev=LOG_ALERT;
			break;
		default:
			syslev=LOG_ERR;
	}
	vsyslog(syslev,str,l);
}

static void defaultLogHandler(OrtpLogLevel log_level, const char *str, va_list l){
	const char *levname="none";
	switch(log_level){
		case ORTP_DEBUG:
			levname="D: ";
		break;
		case ORTP_MESSAGE:
			levname="M: ";
		break;
		case ORTP_WARNING:
			levname="W: ";
		break;
		case ORTP_ERROR:
			levname="E: ";
		break;
		case ORTP_FATAL:
			levname="F: ";
		break;
		default:
			break;
	}
	fprintf(stdout,"%s",levname);
	vfprintf(stdout,str,l);
	fprintf(stdout,"\n");
}

static void sofiaLogHandler(void *, char const *fmt, va_list ap){
	ortp_logv(ORTP_MESSAGE,fmt,ap);
}

static void timerfunc(su_root_magic_t *magic, su_timer_t *t, Agent *a){
	a->idle();
}

static void initialize(bool debug, bool useSyslog){
	if (useSyslog){
		openlog("flexisip", 0, LOG_USER);
		setlogmask(~0);
		ortp_set_log_handler(syslogHandler);
	}else{
		ortp_set_log_handler(defaultLogHandler);
	}
	ortp_init();
	ortp_set_log_file(stdout);
	ortp_set_log_level_mask(ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
	LOGN("Starting version %s", VERSION);
	if (debug==false){
		ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
	}
	signal(SIGTERM,flexisip_stop);
	signal(SIGINT,flexisip_stop);
	signal(SIGUSR1,flexisip_stat);
	/*enable core dumps*/
	struct rlimit lm;
	lm.rlim_cur=RLIM_INFINITY;
	lm.rlim_max=RLIM_INFINITY;
	if (setrlimit(RLIMIT_CORE,&lm)==-1){
		LOGE("Cannot enable core dump, setrlimit() failed: %s",strerror(errno));
	}
	
	su_init();
}


static int getSystemFdLimit(){
	static int max_sys_fd=-1;
	if (max_sys_fd==-1){
#ifdef __linux
		char tmp[256];
		int fd=open("/proc/sys/fs/file-max",O_RDONLY);
		if (fd!=-1){
			if (read(fd,tmp,sizeof(tmp))>0){
				int val=0;
				if (sscanf(tmp,"%i",&val)==1){
					max_sys_fd=val;
					LOGN("System wide maximum number of file descriptors is %i",max_sys_fd);
				}
			}
			close(fd);
		}
#else
	LOGW("Guessing of system wide fd limit is not implemented.");
	max_sys_fd=2048;
#endif
	}
	return max_sys_fd;
}

static void increase_fd_limit(void){
	struct rlimit lm;
	if (getrlimit(RLIMIT_NOFILE,&lm)==-1){
		LOGE("getrlimit(RLIMIT_NOFILE) failed: %s",strerror(errno));
	}else{
		unsigned int new_limit=getSystemFdLimit();
		LOGN("Maximum number of open file descriptors is %i, limit=%i, system wide limit=%i",
		     (int)lm.rlim_cur,(int)lm.rlim_max,getSystemFdLimit());
		
		if (lm.rlim_cur<new_limit){
			lm.rlim_cur=lm.rlim_max=new_limit;
			if (setrlimit(RLIMIT_NOFILE,&lm)==-1){
				LOGE("setrlimit(RLIMIT_NOFILE) failed: %s",strerror(errno));
			}
			if (getrlimit(RLIMIT_NOFILE,&lm)==0){
				LOGN("Maximum number of file descriptor set to %i.",(int)lm.rlim_cur);
			}
		}
	}
}<commit_msg>refine method to get maximum number of file descriptors<commit_after>/*
	Flexisip, a flexible SIP proxy server with media capabilities.
    Copyright (C) 2010  Belledonne Communications SARL.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <syslog.h>
#include <sys/time.h>
#include <sys/resource.h>

#include <iostream>

#include <mediastreamer2/mscommon.h>
#include "agent.hh"
#include "stun.hh"

#include <stdlib.h>
#include <signal.h>

#include <sofia-sip/su_log.h>

#ifndef VERSION
#define VERSION "DEVEL"
#endif //VERSION


static int run=1;
static su_root_t *root=NULL;


static void usage(const char *arg0){
	printf("%s\n"
	       "\t\t [--port <port number to listen>]\n"
	       "\t\t [--tlsport <port number for sips>]\n"
	       "\t\t [--debug]\n"
	       "\t\t [--daemon]\n"
	       "\t\t [--configfile <path>]\n"
	       "\t\t [--dump-default-config]\n"
	       "\t\t [--help]\n",arg0);
	exit(-1);
}

static void flexisip_stop(int signum){
	LOGD("Received quit signal...");
	run=0;
	if (root){
		su_root_break (root);
	}
}

static void flexisip_stat(int signum){
}

static void syslogHandler(OrtpLogLevel log_level, const char *str, va_list l){
	int syslev=LOG_ALERT;
	switch(log_level){
		case ORTP_DEBUG:
			syslev=LOG_DEBUG;
			break;
		case ORTP_MESSAGE:
			syslev=LOG_INFO;
			break;
/*			
		case ORTP_NOTICE:
			syslev=LOG_NOTICE;
			break;
*/
		 case ORTP_WARNING:
			syslev=LOG_WARNING;
			break;
		case ORTP_ERROR:
			syslev=LOG_ERR;
		case ORTP_FATAL:
			syslev=LOG_ALERT;
			break;
		default:
			syslev=LOG_ERR;
	}
	vsyslog(syslev,str,l);
}

static void defaultLogHandler(OrtpLogLevel log_level, const char *str, va_list l){
	const char *levname="none";
	switch(log_level){
		case ORTP_DEBUG:
			levname="D: ";
		break;
		case ORTP_MESSAGE:
			levname="M: ";
		break;
		case ORTP_WARNING:
			levname="W: ";
		break;
		case ORTP_ERROR:
			levname="E: ";
		break;
		case ORTP_FATAL:
			levname="F: ";
		break;
		default:
			break;
	}
	fprintf(stdout,"%s",levname);
	vfprintf(stdout,str,l);
	fprintf(stdout,"\n");
}

static void sofiaLogHandler(void *, char const *fmt, va_list ap){
	ortp_logv(ORTP_MESSAGE,fmt,ap);
}

static void timerfunc(su_root_magic_t *magic, su_timer_t *t, Agent *a){
	a->idle();
}

static void initialize(bool debug, bool useSyslog){
	if (useSyslog){
		openlog("flexisip", 0, LOG_USER);
		setlogmask(~0);
		ortp_set_log_handler(syslogHandler);
	}else{
		ortp_set_log_handler(defaultLogHandler);
	}
	ortp_init();
	ortp_set_log_file(stdout);
	ortp_set_log_level_mask(ORTP_DEBUG|ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
	LOGN("Starting version %s", VERSION);
	if (debug==false){
		ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
	}
	signal(SIGTERM,flexisip_stop);
	signal(SIGINT,flexisip_stop);
	signal(SIGUSR1,flexisip_stat);
	/*enable core dumps*/
	struct rlimit lm;
	lm.rlim_cur=RLIM_INFINITY;
	lm.rlim_max=RLIM_INFINITY;
	if (setrlimit(RLIMIT_CORE,&lm)==-1){
		LOGE("Cannot enable core dump, setrlimit() failed: %s",strerror(errno));
	}
	
	su_init();
}


static int getSystemFdLimit(){
	static int max_sys_fd=-1;
	if (max_sys_fd==-1){
#ifdef __linux
		char tmp[256];
		int fd=open("/proc/sys/fs/file-max",O_RDONLY);
		if (fd!=-1){
			if (read(fd,tmp,sizeof(tmp))>0){
				int val=0;
				if (sscanf(tmp,"%i",&val)==1){
					max_sys_fd=val;
					LOGN("System wide maximum number of file descriptors is %i",max_sys_fd);
				}
			}
			close(fd);
			fd=open("/proc/sys/fs/nr_open",O_RDONLY);
			if (fd!=-1){
				if (read(fd,tmp,sizeof(tmp))>0){
					int val=0;
					if (sscanf(tmp,"%i",&val)==1){
						LOGN("System wide maximum number open files is %i",val);
						if (val<max_sys_fd){
							max_sys_fd=val;
						}
					}
				}
				close(fd);
			}
		}
#else
	LOGW("Guessing of system wide fd limit is not implemented.");
	max_sys_fd=2048;
#endif
	}
	return max_sys_fd;
}

static void increase_fd_limit(void){
	struct rlimit lm;
	if (getrlimit(RLIMIT_NOFILE,&lm)==-1){
		LOGE("getrlimit(RLIMIT_NOFILE) failed: %s",strerror(errno));
	}else{
		unsigned int new_limit=getSystemFdLimit();
		int old_lim=(int)lm.rlim_cur;
		LOGN("Maximum number of open file descriptors is %i, limit=%i, system wide limit=%i",
		     (int)lm.rlim_cur,(int)lm.rlim_max,getSystemFdLimit());
		
		if (lm.rlim_cur<new_limit){
			lm.rlim_cur=lm.rlim_max=new_limit;
			if (setrlimit(RLIMIT_NOFILE,&lm)==-1){
				LOGE("setrlimit(RLIMIT_NOFILE) failed: %s. Limit of number of file descriptors is low (%i).",strerror(errno),old_lim);
			}
			if (getrlimit(RLIMIT_NOFILE,&lm)==0){
				LOGN("Maximum number of file descriptor set to %i.",(int)lm.rlim_cur);
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.1388500698742264, 'fraction_numerical': 0.00658814134557796, 'mean_word_length': 4.07548125633232, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3159754', 'n_tokens_mistral': 4224, 'n_tokens_neox': 3892, 'n_words': 900}