<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/>.
*/
#if defined(HAVE_CONFIG_H) && !defined(FLEXISIP_INCLUDED)
#include "flexisip-config.h"
#define FLEXISIP_INCLUDED
#endif
#include <syslog.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif

#include <iostream>

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

#include <cstdlib>
#include <cstdio>
#include <csignal>

#include <sofia-sip/su_log.h>
#ifdef ENABLE_SNMP
#include "snmp-agent.h"
#endif
#ifndef VERSION
#define VERSION "DEVEL"
#endif //VERSION


static int run=1;
static int pipe_fds[2]={-1}; //pipes used by flexisip to notify its starter process that everything went fine 
static pid_t flexisip_pid=0;
static su_root_t *root=NULL;
bool sUseSyslog=false;

using namespace ::std;

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 [node name]]\n"
	       "\t\t [--dump-snmp-mib]\n"
           "\t\t [--set <[node/]option[=value]>]\n"
	       "\t\t [--help]\n",arg0);
	exit(-1);
}

static void flexisip_stop(int signum){
	if (flexisip_pid>0){
		LOGD("Watchdog received quit signal...passing to child.");
		/*we are the watchdog, pass the signal to our child*/
		kill(flexisip_pid,signum);
	}else{
		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(stderr,"%s",levname);
	vfprintf(stderr,str,l);
	fprintf(stderr,"\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){
	sUseSyslog=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);
	
	if (debug==false){
		ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
	}
	signal(SIGPIPE,SIG_IGN);
	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){<commit_msg>Initialize array to make valgrind happy.<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/>.
*/
#if defined(HAVE_CONFIG_H) && !defined(FLEXISIP_INCLUDED)
#include "flexisip-config.h"
#define FLEXISIP_INCLUDED
#endif
#include <syslog.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif

#include <iostream>

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

#include <cstdlib>
#include <cstdio>
#include <csignal>

#include <sofia-sip/su_log.h>
#ifdef ENABLE_SNMP
#include "snmp-agent.h"
#endif
#ifndef VERSION
#define VERSION "DEVEL"
#endif //VERSION


static int run=1;
static int pipe_fds[2]={-1}; //pipes used by flexisip to notify its starter process that everything went fine 
static pid_t flexisip_pid=0;
static su_root_t *root=NULL;
bool sUseSyslog=false;

using namespace ::std;

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 [node name]]\n"
	       "\t\t [--dump-snmp-mib]\n"
           "\t\t [--set <[node/]option[=value]>]\n"
	       "\t\t [--help]\n",arg0);
	exit(-1);
}

static void flexisip_stop(int signum){
	if (flexisip_pid>0){
		LOGD("Watchdog received quit signal...passing to child.");
		/*we are the watchdog, pass the signal to our child*/
		kill(flexisip_pid,signum);
	}else{
		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(stderr,"%s",levname);
	vfprintf(stderr,str,l);
	fprintf(stderr,"\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){
	sUseSyslog=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);
	
	if (debug==false){
		ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
	}
	signal(SIGPIPE,SIG_IGN);
	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]={0}; //make valgrind happy
		int fd=open("/proc/sys/fs/file-max",O_RDONLY);
		if (fd!=-1){
			if (read(fd,tmp,sizeof(tmp))>0){
{'fraction_non_alphanumeric': 0.13412398921832885, 'fraction_numerical': 0.005714285714285714, 'mean_word_length': 4.14190687361419, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 43, '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': '3334933', 'n_tokens_mistral': 3775, 'n_tokens_neox': 3561, 'n_words': 867}