winlin

support daemon

@@ -19,13 +19,17 @@ chunk_size 60000; @@ -19,13 +19,17 @@ chunk_size 60000;
19 # default: ./objs/logs 19 # default: ./objs/logs
20 ff_log_dir ./objs/logs; 20 ff_log_dir ./objs/logs;
21 # the log file for srs. 21 # the log file for srs.
22 -# if not specified, disable log to file, only print to console. 22 +# if not specified or empty, disable log to file, only print to console.
23 # if speicfied, write log to file and print to console. 23 # if speicfied, write log to file and print to console.
  24 +# default: empty.
24 srs_log_file ./objs/srs.log; 25 srs_log_file ./objs/srs.log;
25 # the max connections. 26 # the max connections.
26 # if exceed the max connections, server will drop the new connection. 27 # if exceed the max connections, server will drop the new connection.
27 # default: 2000 28 # default: 2000
28 max_connections 1000; 29 max_connections 1000;
  30 +# whether start as deamon
  31 +# default: on
  32 +daemon on;
29 # vhost list, the __defaultVhost__ is the default vhost 33 # vhost list, the __defaultVhost__ is the default vhost
30 # for example, user use ip to access the stream: rtmp://192.168.1.2/live/livestream. 34 # for example, user use ip to access the stream: rtmp://192.168.1.2/live/livestream.
31 # for which cannot identify the required vhost. 35 # for which cannot identify the required vhost.
@@ -71,11 +71,10 @@ start() { @@ -71,11 +71,10 @@ start() {
71 srs_log_file=`cat ${ROOT}/${CONFIG} |grep '^srs_log_file'| awk '{print $2}'| awk -F ';' '{print $1}'` 71 srs_log_file=`cat ${ROOT}/${CONFIG} |grep '^srs_log_file'| awk '{print $2}'| awk -F ';' '{print $1}'`
72 72
73 # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000" 73 # TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
74 - # TODO: FIXME: support deamon, without nohup.  
75 if [[ -z $srs_log_file ]]; then 74 if [[ -z $srs_log_file ]]; then
76 - (cd ${ROOT}; nohup ${APP} -c ${CONFIG} >/dev/null 2>&1 &) 75 + (cd ${ROOT}; ${APP} -c ${CONFIG} >/dev/null 2>&1)
77 else 76 else
78 - (cd ${ROOT}; nohup ${APP} -c ${CONFIG} >> $srs_log_file 2>&1 &) 77 + (cd ${ROOT}; ${APP} -c ${CONFIG} >> $srs_log_file 2>&1)
79 fi 78 fi
80 79
81 # check again after start server 80 # check again after start server
@@ -1282,6 +1282,18 @@ string SrsConfig::get_srs_log_file() @@ -1282,6 +1282,18 @@ string SrsConfig::get_srs_log_file()
1282 return conf->arg0(); 1282 return conf->arg0();
1283 } 1283 }
1284 1284
  1285 +bool SrsConfig::get_deamon()
  1286 +{
  1287 + srs_assert(root);
  1288 +
  1289 + SrsConfDirective* conf = root->get("deamon");
  1290 + if (conf && conf->arg0() == "off") {
  1291 + return false;
  1292 + }
  1293 +
  1294 + return true;
  1295 +}
  1296 +
1285 int SrsConfig::get_max_connections() 1297 int SrsConfig::get_max_connections()
1286 { 1298 {
1287 srs_assert(root); 1299 srs_assert(root);
@@ -149,6 +149,7 @@ public: @@ -149,6 +149,7 @@ public:
149 virtual std::string get_engine_output(SrsConfDirective* engine); 149 virtual std::string get_engine_output(SrsConfDirective* engine);
150 virtual std::string get_ffmpeg_log_dir(); 150 virtual std::string get_ffmpeg_log_dir();
151 virtual std::string get_srs_log_file(); 151 virtual std::string get_srs_log_file();
  152 + virtual bool get_deamon();
152 virtual int get_max_connections(); 153 virtual int get_max_connections();
153 virtual bool get_gop_cache(std::string vhost); 154 virtual bool get_gop_cache(std::string vhost);
154 virtual double get_queue_length(std::string vhost); 155 virtual double get_queue_length(std::string vhost);
@@ -37,6 +37,9 @@ SrsServer* _srs_server = new SrsServer(); @@ -37,6 +37,9 @@ SrsServer* _srs_server = new SrsServer();
37 #include <stdlib.h> 37 #include <stdlib.h>
38 #include <signal.h> 38 #include <signal.h>
39 39
  40 +#include <sys/types.h>
  41 +#include <sys/wait.h>
  42 +
40 #ifdef SRS_GPERF_MP 43 #ifdef SRS_GPERF_MP
41 #include <gperftools/heap-profiler.h> 44 #include <gperftools/heap-profiler.h>
42 #endif 45 #endif
@@ -50,37 +53,13 @@ void handler(int signo) @@ -50,37 +53,13 @@ void handler(int signo)
50 _srs_server->on_signal(signo); 53 _srs_server->on_signal(signo);
51 } 54 }
52 55
53 -int main(int argc, char** argv) 56 +int run_master()
54 { 57 {
55 int ret = ERROR_SUCCESS; 58 int ret = ERROR_SUCCESS;
56 59
57 - // TODO: support both little and big endian.  
58 - srs_assert(srs_is_little_endian());  
59 -  
60 -#ifdef SRS_GPERF_MP  
61 - HeapProfilerStart("gperf.srs.gmp");  
62 -#endif  
63 -#ifdef SRS_GPERF_CP  
64 - ProfilerStart("gperf.srs.gcp");  
65 -#endif  
66 -  
67 signal(SIGNAL_RELOAD, handler); 60 signal(SIGNAL_RELOAD, handler);
68 signal(SIGTERM, handler); 61 signal(SIGTERM, handler);
69 signal(SIGINT, handler); 62 signal(SIGINT, handler);
70 -  
71 - if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) {  
72 - return ret;  
73 - }  
74 -  
75 -#ifdef SRS_GPERF_MC  
76 - #ifdef SRS_GPERF_MP  
77 - srs_error("option --with-gmc confict with --with-gmp, "  
78 - "@see: http://google-perftools.googlecode.com/svn/trunk/doc/heap_checker.html\n"  
79 - "Note that since the heap-checker uses the heap-profiling framework internally, "  
80 - "it is not possible to run both the heap-checker and heap profiler at the same time");  
81 - return -1;  
82 - #endif  
83 -#endif  
84 63
85 srs_trace("uname: "SRS_UNAME); 64 srs_trace("uname: "SRS_UNAME);
86 srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian"); 65 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) @@ -106,3 +85,81 @@ int main(int argc, char** argv)
106 85
107 return 0; 86 return 0;
108 } 87 }
  88 +
  89 +int run()
  90 +{
  91 + // if not deamon, directly run master.
  92 + if (!_srs_config->get_deamon()) {
  93 + return run_master();
  94 + }
  95 +
  96 + srs_trace("start deamon mode...");
  97 +
  98 + int pid = fork();
  99 +
  100 + if(pid == -1){
  101 + srs_error("create process error. ret=-1"); //ret=0
  102 + return -1;
  103 + }
  104 +
  105 + // grandpa
  106 + if(pid > 0){
  107 + int status = 0;
  108 + if(waitpid(pid, &status, 0) == -1){
  109 + srs_error("wait child process error! ret=-1"); //ret=0
  110 + }
  111 + srs_trace("grandpa process exit.");
  112 + exit(0);
  113 + return 0;
  114 + }
  115 +
  116 + // father
  117 + pid = fork();
  118 +
  119 + if(pid == -1){
  120 + srs_error("create process error. ret=-1");
  121 + return -1;
  122 + }
  123 +
  124 + if(pid > 0){
  125 + srs_trace("father process exit. ret=-1");
  126 + exit(0);
  127 + return 0;
  128 + }
  129 +
  130 + // son
  131 + srs_trace("son(deamon) process running.");
  132 +
  133 + return run_master();
  134 +}
  135 +
  136 +int main(int argc, char** argv)
  137 +{
  138 + int ret = ERROR_SUCCESS;
  139 +
  140 + // TODO: support both little and big endian.
  141 + srs_assert(srs_is_little_endian());
  142 +
  143 +#ifdef SRS_GPERF_MP
  144 + HeapProfilerStart("gperf.srs.gmp");
  145 +#endif
  146 +#ifdef SRS_GPERF_CP
  147 + ProfilerStart("gperf.srs.gcp");
  148 +#endif
  149 +
  150 +#ifdef SRS_GPERF_MC
  151 + #ifdef SRS_GPERF_MP
  152 + srs_error("option --with-gmc confict with --with-gmp, "
  153 + "@see: http://google-perftools.googlecode.com/svn/trunk/doc/heap_checker.html\n"
  154 + "Note that since the heap-checker uses the heap-profiling framework internally, "
  155 + "it is not possible to run both the heap-checker and heap profiler at the same time");
  156 + return -1;
  157 + #endif
  158 +#endif
  159 +
  160 + if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) {
  161 + return ret;
  162 + }
  163 +
  164 + return run();
  165 +}