winlin

support daemon

... ... @@ -19,13 +19,17 @@ chunk_size 60000;
# default: ./objs/logs
ff_log_dir ./objs/logs;
# the log file for srs.
# if not specified, disable log to file, only print to console.
# if not specified or empty, disable log to file, only print to console.
# if speicfied, write log to file and print to console.
# default: empty.
srs_log_file ./objs/srs.log;
# the max connections.
# if exceed the max connections, server will drop the new connection.
# default: 2000
max_connections 1000;
# whether start as deamon
# default: on
daemon on;
# vhost list, the __defaultVhost__ is the default vhost
# for example, user use ip to access the stream: rtmp://192.168.1.2/live/livestream.
# for which cannot identify the required vhost.
... ...
... ... @@ -71,11 +71,10 @@ start() {
srs_log_file=`cat ${ROOT}/${CONFIG} |grep '^srs_log_file'| awk '{print $2}'| awk -F ';' '{print $1}'`
# TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
# TODO: FIXME: support deamon, without nohup.
if [[ -z $srs_log_file ]]; then
(cd ${ROOT}; nohup ${APP} -c ${CONFIG} >/dev/null 2>&1 &)
(cd ${ROOT}; ${APP} -c ${CONFIG} >/dev/null 2>&1)
else
(cd ${ROOT}; nohup ${APP} -c ${CONFIG} >> $srs_log_file 2>&1 &)
(cd ${ROOT}; ${APP} -c ${CONFIG} >> $srs_log_file 2>&1)
fi
# check again after start server
... ...
... ... @@ -1282,6 +1282,18 @@ string SrsConfig::get_srs_log_file()
return conf->arg0();
}
bool SrsConfig::get_deamon()
{
srs_assert(root);
SrsConfDirective* conf = root->get("deamon");
if (conf && conf->arg0() == "off") {
return false;
}
return true;
}
int SrsConfig::get_max_connections()
{
srs_assert(root);
... ...
... ... @@ -149,6 +149,7 @@ public:
virtual std::string get_engine_output(SrsConfDirective* engine);
virtual std::string get_ffmpeg_log_dir();
virtual std::string get_srs_log_file();
virtual bool get_deamon();
virtual int get_max_connections();
virtual bool get_gop_cache(std::string vhost);
virtual double get_queue_length(std::string vhost);
... ...
... ... @@ -37,6 +37,9 @@ SrsServer* _srs_server = new SrsServer();
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifdef SRS_GPERF_MP
#include <gperftools/heap-profiler.h>
#endif
... ... @@ -50,37 +53,13 @@ void handler(int signo)
_srs_server->on_signal(signo);
}
int main(int argc, char** argv)
int run_master()
{
int ret = ERROR_SUCCESS;
// TODO: support both little and big endian.
srs_assert(srs_is_little_endian());
#ifdef SRS_GPERF_MP
HeapProfilerStart("gperf.srs.gmp");
#endif
#ifdef SRS_GPERF_CP
ProfilerStart("gperf.srs.gcp");
#endif
signal(SIGNAL_RELOAD, handler);
signal(SIGTERM, handler);
signal(SIGINT, handler);
if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) {
return ret;
}
#ifdef SRS_GPERF_MC
#ifdef SRS_GPERF_MP
srs_error("option --with-gmc confict with --with-gmp, "
"@see: http://google-perftools.googlecode.com/svn/trunk/doc/heap_checker.html\n"
"Note that since the heap-checker uses the heap-profiling framework internally, "
"it is not possible to run both the heap-checker and heap profiler at the same time");
return -1;
#endif
#endif
srs_trace("uname: "SRS_UNAME);
srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");
... ... @@ -106,3 +85,81 @@ int main(int argc, char** argv)
return 0;
}
int run()
{
// if not deamon, directly run master.
if (!_srs_config->get_deamon()) {
return run_master();
}
srs_trace("start deamon mode...");
int pid = fork();
if(pid == -1){
srs_error("create process error. ret=-1"); //ret=0
return -1;
}
// grandpa
if(pid > 0){
int status = 0;
if(waitpid(pid, &status, 0) == -1){
srs_error("wait child process error! ret=-1"); //ret=0
}
srs_trace("grandpa process exit.");
exit(0);
return 0;
}
// father
pid = fork();
if(pid == -1){
srs_error("create process error. ret=-1");
return -1;
}
if(pid > 0){
srs_trace("father process exit. ret=-1");
exit(0);
return 0;
}
// son
srs_trace("son(deamon) process running.");
return run_master();
}
int main(int argc, char** argv)
{
int ret = ERROR_SUCCESS;
// TODO: support both little and big endian.
srs_assert(srs_is_little_endian());
#ifdef SRS_GPERF_MP
HeapProfilerStart("gperf.srs.gmp");
#endif
#ifdef SRS_GPERF_CP
ProfilerStart("gperf.srs.gcp");
#endif
#ifdef SRS_GPERF_MC
#ifdef SRS_GPERF_MP
srs_error("option --with-gmc confict with --with-gmp, "
"@see: http://google-perftools.googlecode.com/svn/trunk/doc/heap_checker.html\n"
"Note that since the heap-checker uses the heap-profiling framework internally, "
"it is not possible to run both the heap-checker and heap profiler at the same time");
return -1;
#endif
#endif
if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) {
return ret;
}
return run();
}
... ...