diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 19224ec04..d06634651 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -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. diff --git a/trunk/etc/init.d/srs b/trunk/etc/init.d/srs index d1b5fd139..bcbf30f9f 100755 --- a/trunk/etc/init.d/srs +++ b/trunk/etc/init.d/srs @@ -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 diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 2cbab5095..2cf21fd7c 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -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); diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 85c244fcf..b7349e7b7 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -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); diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index 21c486af8..2677228c5 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -37,6 +37,9 @@ SrsServer* _srs_server = new SrsServer(); #include #include +#include +#include + #ifdef SRS_GPERF_MP #include #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(); +}