winlin

for #319, add signal to write config to file.

@@ -1421,6 +1421,13 @@ int SrsConfig::parse_options(int argc, char** argv) @@ -1421,6 +1421,13 @@ int SrsConfig::parse_options(int argc, char** argv)
1421 return ret; 1421 return ret;
1422 } 1422 }
1423 1423
  1424 +int SrsConfig::persistence()
  1425 +{
  1426 + int ret = ERROR_SUCCESS;
  1427 + // TODO: FIXME: implements it.
  1428 + return ret;
  1429 +}
  1430 +
1424 string SrsConfig::config() 1431 string SrsConfig::config()
1425 { 1432 {
1426 return config_file; 1433 return config_file;
@@ -284,12 +284,16 @@ private: @@ -284,12 +284,16 @@ private:
284 // parse options and file 284 // parse options and file
285 public: 285 public:
286 /** 286 /**
287 - * parse the cli, the main(argc,argv) function.  
288 - */ 287 + * parse the cli, the main(argc,argv) function.
  288 + */
289 virtual int parse_options(int argc, char** argv); 289 virtual int parse_options(int argc, char** argv);
290 /** 290 /**
291 - * get the config file path.  
292 - */ 291 + * persistence current config to file.
  292 + */
  293 + virtual int persistence();
  294 + /**
  295 + * get the config file path.
  296 + */
293 virtual std::string config(); 297 virtual std::string config();
294 private: 298 private:
295 /** 299 /**
@@ -50,7 +50,13 @@ using namespace std; @@ -50,7 +50,13 @@ using namespace std;
50 #include <srs_core_mem_watch.hpp> 50 #include <srs_core_mem_watch.hpp>
51 51
52 // signal defines. 52 // signal defines.
53 -#define SIGNAL_RELOAD SIGHUP 53 +// reload the config file and apply new config.
  54 +#define SRS_SIGNAL_RELOAD SIGHUP
  55 +// terminate the srs with dispose to detect memory leak for gmp.
  56 +#define SRS_SIGNAL_DISPOSE SIGUSR2
  57 +// persistence the config in memory to config file.
  58 +// @see https://github.com/simple-rtmp-server/srs/issues/319#issuecomment-134993922
  59 +#define SRS_SIGNAL_PERSISTENCE_CONFIG SIGUSR1
54 60
55 // system interval in ms, 61 // system interval in ms,
56 // all resolution times should be times togother, 62 // all resolution times should be times togother,
@@ -419,7 +425,7 @@ int SrsSignalManager::start() @@ -419,7 +425,7 @@ int SrsSignalManager::start()
419 sa.sa_handler = SrsSignalManager::sig_catcher; 425 sa.sa_handler = SrsSignalManager::sig_catcher;
420 sigemptyset(&sa.sa_mask); 426 sigemptyset(&sa.sa_mask);
421 sa.sa_flags = 0; 427 sa.sa_flags = 0;
422 - sigaction(SIGNAL_RELOAD, &sa, NULL); 428 + sigaction(SRS_SIGNAL_RELOAD, &sa, NULL);
423 429
424 sa.sa_handler = SrsSignalManager::sig_catcher; 430 sa.sa_handler = SrsSignalManager::sig_catcher;
425 sigemptyset(&sa.sa_mask); 431 sigemptyset(&sa.sa_mask);
@@ -434,7 +440,12 @@ int SrsSignalManager::start() @@ -434,7 +440,12 @@ int SrsSignalManager::start()
434 sa.sa_handler = SrsSignalManager::sig_catcher; 440 sa.sa_handler = SrsSignalManager::sig_catcher;
435 sigemptyset(&sa.sa_mask); 441 sigemptyset(&sa.sa_mask);
436 sa.sa_flags = 0; 442 sa.sa_flags = 0;
437 - sigaction(SIGUSR2, &sa, NULL); 443 + sigaction(SRS_SIGNAL_DISPOSE, &sa, NULL);
  444 +
  445 + sa.sa_handler = SrsSignalManager::sig_catcher;
  446 + sigemptyset(&sa.sa_mask);
  447 + sa.sa_flags = 0;
  448 + sigaction(SRS_SIGNAL_PERSISTENCE_CONFIG, &sa, NULL);
438 449
439 srs_trace("signal installed"); 450 srs_trace("signal installed");
440 451
@@ -481,6 +492,7 @@ ISrsServerCycle::~ISrsServerCycle() @@ -481,6 +492,7 @@ ISrsServerCycle::~ISrsServerCycle()
481 SrsServer::SrsServer() 492 SrsServer::SrsServer()
482 { 493 {
483 signal_reload = false; 494 signal_reload = false;
  495 + signal_persistence_config = false;
484 signal_gmc_stop = false; 496 signal_gmc_stop = false;
485 signal_gracefully_quit = false; 497 signal_gracefully_quit = false;
486 pid_fd = -1; 498 pid_fd = -1;
@@ -905,11 +917,16 @@ void SrsServer::remove(SrsConnection* conn) @@ -905,11 +917,16 @@ void SrsServer::remove(SrsConnection* conn)
905 917
906 void SrsServer::on_signal(int signo) 918 void SrsServer::on_signal(int signo)
907 { 919 {
908 - if (signo == SIGNAL_RELOAD) { 920 + if (signo == SRS_SIGNAL_RELOAD) {
909 signal_reload = true; 921 signal_reload = true;
910 return; 922 return;
911 } 923 }
912 924
  925 + if (signo == SRS_SIGNAL_PERSISTENCE_CONFIG) {
  926 + signal_persistence_config = true;
  927 + return;
  928 + }
  929 +
913 if (signo == SIGINT || signo == SIGUSR2) { 930 if (signo == SIGINT || signo == SIGUSR2) {
914 #ifdef SRS_AUTO_GPERF_MC 931 #ifdef SRS_AUTO_GPERF_MC
915 srs_trace("gmc is on, main cycle will terminate normally."); 932 srs_trace("gmc is on, main cycle will terminate normally.");
@@ -986,7 +1003,7 @@ int SrsServer::do_cycle() @@ -986,7 +1003,7 @@ int SrsServer::do_cycle()
986 // do reload the config. 1003 // do reload the config.
987 if (signal_reload) { 1004 if (signal_reload) {
988 signal_reload = false; 1005 signal_reload = false;
989 - srs_info("get signal reload, to reload the config."); 1006 + srs_info("get signal to reload the config.");
990 1007
991 if ((ret = _srs_config->reload()) != ERROR_SUCCESS) { 1008 if ((ret = _srs_config->reload()) != ERROR_SUCCESS) {
992 srs_error("reload config failed. ret=%d", ret); 1009 srs_error("reload config failed. ret=%d", ret);
@@ -995,6 +1012,18 @@ int SrsServer::do_cycle() @@ -995,6 +1012,18 @@ int SrsServer::do_cycle()
995 srs_trace("reload config success."); 1012 srs_trace("reload config success.");
996 } 1013 }
997 1014
  1015 + // do persistence config to file.
  1016 + if (signal_persistence_config) {
  1017 + signal_persistence_config = false;
  1018 + srs_info("get signal to persistence config to file.");
  1019 +
  1020 + if ((ret = _srs_config->persistence()) != ERROR_SUCCESS) {
  1021 + srs_error("persistence config to file failed. ret=%d", ret);
  1022 + return ret;
  1023 + }
  1024 + srs_trace("persistence config to file success.");
  1025 + }
  1026 +
998 // notice the stream sources to cycle. 1027 // notice the stream sources to cycle.
999 if ((ret = SrsSource::cycle_all()) != ERROR_SUCCESS) { 1028 if ((ret = SrsSource::cycle_all()) != ERROR_SUCCESS) {
1000 return ret; 1029 return ret;
@@ -275,6 +275,7 @@ private: @@ -275,6 +275,7 @@ private:
275 * user send the signal, convert to variable. 275 * user send the signal, convert to variable.
276 */ 276 */
277 bool signal_reload; 277 bool signal_reload;
  278 + bool signal_persistence_config;
278 bool signal_gmc_stop; 279 bool signal_gmc_stop;
279 bool signal_gracefully_quit; 280 bool signal_gracefully_quit;
280 public: 281 public: