From 980e3921e40fc0b0ced66dd45fd2a410e77e3949 Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 26 Aug 2015 21:11:28 +0800 Subject: [PATCH] for #319, add signal to write config to file. --- trunk/src/app/srs_app_config.cpp | 7 ++++++ trunk/src/app/srs_app_config.hpp | 12 ++++++---- trunk/src/app/srs_app_server.cpp | 39 ++++++++++++++++++++++++++++---- trunk/src/app/srs_app_server.hpp | 1 + 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 28b74f88b..27c398ea1 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -1421,6 +1421,13 @@ int SrsConfig::parse_options(int argc, char** argv) return ret; } +int SrsConfig::persistence() +{ + int ret = ERROR_SUCCESS; + // TODO: FIXME: implements it. + return ret; +} + string SrsConfig::config() { return config_file; diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index aad2ee498..7479c3f24 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -284,12 +284,16 @@ private: // parse options and file public: /** - * parse the cli, the main(argc,argv) function. - */ + * parse the cli, the main(argc,argv) function. + */ virtual int parse_options(int argc, char** argv); /** - * get the config file path. - */ + * persistence current config to file. + */ + virtual int persistence(); + /** + * get the config file path. + */ virtual std::string config(); private: /** diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 3727a695e..2127f96c1 100755 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -50,7 +50,13 @@ using namespace std; #include // signal defines. -#define SIGNAL_RELOAD SIGHUP +// reload the config file and apply new config. +#define SRS_SIGNAL_RELOAD SIGHUP +// terminate the srs with dispose to detect memory leak for gmp. +#define SRS_SIGNAL_DISPOSE SIGUSR2 +// persistence the config in memory to config file. +// @see https://github.com/simple-rtmp-server/srs/issues/319#issuecomment-134993922 +#define SRS_SIGNAL_PERSISTENCE_CONFIG SIGUSR1 // system interval in ms, // all resolution times should be times togother, @@ -419,7 +425,7 @@ int SrsSignalManager::start() sa.sa_handler = SrsSignalManager::sig_catcher; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; - sigaction(SIGNAL_RELOAD, &sa, NULL); + sigaction(SRS_SIGNAL_RELOAD, &sa, NULL); sa.sa_handler = SrsSignalManager::sig_catcher; sigemptyset(&sa.sa_mask); @@ -434,7 +440,12 @@ int SrsSignalManager::start() sa.sa_handler = SrsSignalManager::sig_catcher; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; - sigaction(SIGUSR2, &sa, NULL); + sigaction(SRS_SIGNAL_DISPOSE, &sa, NULL); + + sa.sa_handler = SrsSignalManager::sig_catcher; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sigaction(SRS_SIGNAL_PERSISTENCE_CONFIG, &sa, NULL); srs_trace("signal installed"); @@ -481,6 +492,7 @@ ISrsServerCycle::~ISrsServerCycle() SrsServer::SrsServer() { signal_reload = false; + signal_persistence_config = false; signal_gmc_stop = false; signal_gracefully_quit = false; pid_fd = -1; @@ -905,11 +917,16 @@ void SrsServer::remove(SrsConnection* conn) void SrsServer::on_signal(int signo) { - if (signo == SIGNAL_RELOAD) { + if (signo == SRS_SIGNAL_RELOAD) { signal_reload = true; return; } + if (signo == SRS_SIGNAL_PERSISTENCE_CONFIG) { + signal_persistence_config = true; + return; + } + if (signo == SIGINT || signo == SIGUSR2) { #ifdef SRS_AUTO_GPERF_MC srs_trace("gmc is on, main cycle will terminate normally."); @@ -986,7 +1003,7 @@ int SrsServer::do_cycle() // do reload the config. if (signal_reload) { signal_reload = false; - srs_info("get signal reload, to reload the config."); + srs_info("get signal to reload the config."); if ((ret = _srs_config->reload()) != ERROR_SUCCESS) { srs_error("reload config failed. ret=%d", ret); @@ -995,6 +1012,18 @@ int SrsServer::do_cycle() srs_trace("reload config success."); } + // do persistence config to file. + if (signal_persistence_config) { + signal_persistence_config = false; + srs_info("get signal to persistence config to file."); + + if ((ret = _srs_config->persistence()) != ERROR_SUCCESS) { + srs_error("persistence config to file failed. ret=%d", ret); + return ret; + } + srs_trace("persistence config to file success."); + } + // notice the stream sources to cycle. if ((ret = SrsSource::cycle_all()) != ERROR_SUCCESS) { return ret; diff --git a/trunk/src/app/srs_app_server.hpp b/trunk/src/app/srs_app_server.hpp index c5ba5031f..f471795ac 100644 --- a/trunk/src/app/srs_app_server.hpp +++ b/trunk/src/app/srs_app_server.hpp @@ -275,6 +275,7 @@ private: * user send the signal, convert to variable. */ bool signal_reload; + bool signal_persistence_config; bool signal_gmc_stop; bool signal_gracefully_quit; public: