From eca40a5726d6fc02545fc1e3a607288a808992b5 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 7 Aug 2023 15:42:05 +0800 Subject: [PATCH 1/3] Refine docker detect. --- Dockerfile | 2 +- trunk/src/app/srs_app_config.cpp | 47 +++++++++++++++++++++++++++-- trunk/src/main/srs_main_server.cpp | 48 ++---------------------------- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/Dockerfile b/Dockerfile index bf9eb1628c..51c80bc5c9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -56,6 +56,6 @@ RUN ldd /usr/local/srs/objs/ffmpeg/bin/ffmpeg && \ # Default workdir and command. WORKDIR /usr/local/srs -ENV SRS_DAEMON=off +ENV SRS_DAEMON=off SRS_IN_DOCKER=on CMD ["./objs/srs", "-c", "conf/srs.conf"] diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 74093a7481..3e9679d33a 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -94,6 +94,38 @@ bool is_common_space(char ch) return (ch == ' ' || ch == '\t' || ch == SRS_CR || ch == SRS_LF); } +extern bool _srs_in_docker; + +// Detect docker by https://stackoverflow.com/a/41559867 +srs_error_t srs_detect_docker() +{ + srs_error_t err = srs_success; + + _srs_in_docker = false; + + SrsFileReader fr; + if ((err = fr.open("/proc/1/cgroup")) != srs_success) { + return err; + } + + ssize_t nn; + char buf[1024]; + if ((err = fr.read(buf, sizeof(buf), &nn)) != srs_success) { + return err; + } + + if (nn <= 0) { + return err; + } + + string s(buf, nn); + if (srs_string_contains(s, "/docker")) { + _srs_in_docker = true; + } + + return err; +} + namespace srs_internal { SrsConfigBuffer::SrsConfigBuffer() @@ -1923,6 +1955,19 @@ srs_error_t SrsConfig::parse_options(int argc, char** argv) if (root->directives.empty()) root->get_or_create("vhost", "__defaultVhost__"); } + // Ignore any error while detecting docker. + if ((err = srs_detect_docker()) != srs_success) { + srs_error_reset(err); + } + + // Try to load the config if docker detect failed. + if (!_srs_in_docker) { + _srs_in_docker = _srs_config->get_in_docker(); + if (_srs_in_docker) { + srs_trace("enable in_docker by config"); + } + } + //////////////////////////////////////////////////////////////////////// // check log name and level //////////////////////////////////////////////////////////////////////// @@ -6509,8 +6554,6 @@ string SrsConfig::get_ingest_input_url(SrsConfDirective* conf) return conf->arg0(); } -extern bool _srs_in_docker; - bool SrsConfig::get_log_tank_file() { if (!srs_getenv("srs.srs_log_tank").empty()) { // SRS_SRS_LOG_TANK diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index 519b7ef384..6bf06d1d30 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -57,7 +57,6 @@ using namespace std; // pre-declare srs_error_t run_directly_or_daemon(); srs_error_t run_in_thread_pool(); -srs_error_t srs_detect_docker(); void show_macro_features(); // @global log and context. @@ -79,6 +78,9 @@ bool _srs_config_by_env = false; // The binary name of SRS. const char* _srs_binary = NULL; +// @global Other variables. +bool _srs_in_docker = false; + // Free global data, for address sanitizer. extern void srs_free_global_system_ips(); @@ -131,11 +133,6 @@ srs_error_t do_main(int argc, char** argv, char** envp) #ifdef SRS_GPERF_MP #warning "gmp is not used for memory leak, please use gmc instead." #endif - - // Ignore any error while detecting docker. - if ((err = srs_detect_docker()) != srs_success) { - srs_error_reset(err); - } // never use srs log(srs_trace, srs_error, etc) before config parse the option, // which will load the log config and apply it. @@ -400,49 +397,10 @@ void show_macro_features() #endif } -// Detect docker by https://stackoverflow.com/a/41559867 -bool _srs_in_docker = false; -srs_error_t srs_detect_docker() -{ - srs_error_t err = srs_success; - - _srs_in_docker = false; - - SrsFileReader fr; - if ((err = fr.open("/proc/1/cgroup")) != srs_success) { - return err; - } - - ssize_t nn; - char buf[1024]; - if ((err = fr.read(buf, sizeof(buf), &nn)) != srs_success) { - return err; - } - - if (nn <= 0) { - return err; - } - - string s(buf, nn); - if (srs_string_contains(s, "/docker")) { - _srs_in_docker = true; - } - - return err; -} - srs_error_t run_directly_or_daemon() { srs_error_t err = srs_success; - // Try to load the config if docker detect failed. - if (!_srs_in_docker) { - _srs_in_docker = _srs_config->get_in_docker(); - if (_srs_in_docker) { - srs_trace("enable in_docker by config"); - } - } - // Load daemon from config, disable it for docker. // @see https://github.com/ossrs/srs/issues/1594 bool run_as_daemon = _srs_config->get_daemon(); From 6cd37174a3d09561c3f02dd76ea6fe4d943ad690 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 7 Aug 2023 15:46:34 +0800 Subject: [PATCH 2/3] Refine code. --- trunk/src/app/srs_app_config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 3e9679d33a..02c602698f 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -2890,7 +2890,7 @@ string SrsConfig::argv() bool SrsConfig::get_daemon() { - SRS_OVERWRITE_BY_ENV_BOOL2("srs.daemon"); + SRS_OVERWRITE_BY_ENV_BOOL2("srs.daemon"); // SRS_DAEMON SrsConfDirective* conf = root->get("daemon"); if (!conf || conf->arg0().empty()) { From f51467117993bfd1957e43e9cc34ec412b9ac771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E5=BF=97=E5=AE=8F?= Date: Tue, 17 Oct 2023 20:48:27 +0800 Subject: [PATCH 3/3] Update release to v5.0.190 v6.0.90 --- trunk/doc/CHANGELOG.md | 2 ++ trunk/src/core/srs_core_version5.hpp | 2 +- trunk/src/core/srs_core_version6.hpp | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index e6db4a8e15..94d188e022 100644 --- a/trunk/doc/CHANGELOG.md +++ b/trunk/doc/CHANGELOG.md @@ -7,6 +7,7 @@ The changelog for SRS. ## SRS 6.0 Changelog +* v6.0, 2023-10-17, Merge [#3758](https://github.com/ossrs/srs/pull/3758): Refine docker detect mechenism. v6.0.90 (#3758) * v6.0, 2023-10-11, Merge [#3827](https://github.com/ossrs/srs/pull/3827): Fix bug for upgrading to OpenSSL 3.0. v6.0.89 (#3827) * v6.0, 2023-10-10, Merge [#3825](https://github.com/ossrs/srs/pull/3825): SRT: Fix the missing config mss. v6.0.88 (#3825) * v6.0, 2023-10-08, Merge [#3824](https://github.com/ossrs/srs/pull/3824): Solve the problem of inaccurate HLS TS duration. v6.0.87 (#3824) @@ -101,6 +102,7 @@ The changelog for SRS. ## SRS 5.0 Changelog +* v5.0, 2023-10-17, Merge [#3758](https://github.com/ossrs/srs/pull/3758): Refine docker detect mechenism. v5.0.190 (#3758) * v5.0, 2023-10-11, Merge [#3827](https://github.com/ossrs/srs/pull/3827): Fix bug for upgrading to OpenSSL 3.0. v5.0.189 (#3827) * v5.0, 2023-10-10, Merge [#3825](https://github.com/ossrs/srs/pull/3825): SRT: Fix the missing config mss. v5.0.188 (#3825) * v5.0, 2023-10-08, Merge [#3824](https://github.com/ossrs/srs/pull/3824): Solve the problem of inaccurate HLS TS duration. v5.0.187 (#3824) diff --git a/trunk/src/core/srs_core_version5.hpp b/trunk/src/core/srs_core_version5.hpp index c4b5686a05..d6d5b78033 100644 --- a/trunk/src/core/srs_core_version5.hpp +++ b/trunk/src/core/srs_core_version5.hpp @@ -9,6 +9,6 @@ #define VERSION_MAJOR 5 #define VERSION_MINOR 0 -#define VERSION_REVISION 189 +#define VERSION_REVISION 190 #endif diff --git a/trunk/src/core/srs_core_version6.hpp b/trunk/src/core/srs_core_version6.hpp index a614245bc0..033e1007b6 100644 --- a/trunk/src/core/srs_core_version6.hpp +++ b/trunk/src/core/srs_core_version6.hpp @@ -9,6 +9,6 @@ #define VERSION_MAJOR 6 #define VERSION_MINOR 0 -#define VERSION_REVISION 89 +#define VERSION_REVISION 90 #endif