Skip to content

Commit

Permalink
For #1594, detect and disable daemon for docker. 3.0.127
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Mar 12, 2020
1 parent c1e07d6 commit ed41ca8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ For previous versions, please read:

## V3 changes

* v3.0, 2020-03-12, For [#1594][bug #1594], detect and disable daemon for docker. 3.0.127
* v3.0, 2020-03-12, For [#1634][bug #1634], always check status in thread loop. 3.0.126
* v3.0, 2020-03-11, For [#1634][bug #1634], refactor output with datetime for ingest/encoder/exec. 3.0.125
* v3.0, 2020-03-11, For [#1634][bug #1634], fix quit by accident SIGTERM while killing FFMPEG. 3.0.124
Expand Down Expand Up @@ -1664,6 +1665,7 @@ Winlin
[bug #1579]: https://github.com/ossrs/srs/issues/1579
[bug #1598]: https://github.com/ossrs/srs/issues/1598
[bug #1634]: https://github.com/ossrs/srs/issues/1634
[bug #1594]: https://github.com/ossrs/srs/issues/1594
[bug #yyyyyyyyyyyyy]: https://github.com/ossrs/srs/issues/yyyyyyyyyyyyy

[exo #828]: https://github.com/google/ExoPlayer/pull/828
Expand Down
4 changes: 4 additions & 0 deletions trunk/conf/full.conf
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ grace_final_wait 3200;
# @see https://github.com/ossrs/srs/issues/1579#issuecomment-587475077
# default: off
force_grace_quit off;
# Whether disable daemon for docker.
# If on, it will set daemon to off in docker, even daemon is on.
# default: on
disable_daemon_for_docker on;

#############################################################################################
# heartbeat/stats sections
Expand Down
14 changes: 13 additions & 1 deletion trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3488,7 +3488,7 @@ srs_error_t SrsConfig::check_normal_config()
&& n != "http_server" && n != "stream_caster"
&& n != "utc_time" && n != "work_dir" && n != "asprocess"
&& n != "ff_log_level" && n != "grace_final_wait" && n != "force_grace_quit"
&& n != "grace_start_wait" && n != "empty_ip_ok"
&& n != "grace_start_wait" && n != "empty_ip_ok" && n != "disable_daemon_for_docker"
) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal directive %s", n.c_str());
}
Expand Down Expand Up @@ -4099,6 +4099,18 @@ bool SrsConfig::is_force_grace_quit()
return SRS_CONF_PERFER_FALSE(conf->arg0());
}

bool SrsConfig::disable_daemon_for_docker()
{
static bool DEFAULT = true;

SrsConfDirective* conf = root->get("disable_daemon_for_docker");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}

return SRS_CONF_PERFER_TRUE(conf->arg0());
}

vector<SrsConfDirective*> SrsConfig::get_stream_casters()
{
srs_assert(root);
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/app/srs_app_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ class SrsConfig
virtual srs_utime_t get_grace_final_wait();
// Whether force to gracefully quit, never fast quit.
virtual bool is_force_grace_quit();
// Whether disable daemon for docker.
virtual bool disable_daemon_for_docker();
// stream_caster section
public:
// Get all stream_caster in config file.
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_version3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
#ifndef SRS_CORE_VERSION3_HPP
#define SRS_CORE_VERSION3_HPP

#define SRS_VERSION3_REVISION 126
#define SRS_VERSION3_REVISION 127

#endif
49 changes: 47 additions & 2 deletions trunk/src/main/srs_main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ using namespace std;
#include <srs_core_performance.hpp>
#include <srs_app_utility.hpp>
#include <srs_core_autofree.hpp>
#include <srs_kernel_file.hpp>

// pre-declare
srs_error_t run(SrsServer* svr);
Expand Down Expand Up @@ -350,6 +351,36 @@ string srs_getenv(const char* name)
return "";
}

// Detect docker by https://stackoverflow.com/a/41559867
srs_error_t srs_detect_docker(bool* is_docker)
{
srs_error_t err = srs_success;

*is_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")) {
*is_docker = true;
}

return err;
}

srs_error_t run(SrsServer* svr)
{
srs_error_t err = srs_success;
Expand All @@ -358,9 +389,23 @@ srs_error_t run(SrsServer* svr)
if ((err = svr->initialize(NULL)) != srs_success) {
return srs_error_wrap(err, "server initialize");
}


// Load daemon from config, disable it for docker.
// @see https://github.com/ossrs/srs/issues/1594
bool in_daemon = _srs_config->get_daemon();
if (in_daemon && _srs_config->disable_daemon_for_docker()) {
bool is_docker = false;
err = srs_detect_docker(&is_docker);
srs_error_reset(err); // Ignore any error while detecting docker.

if (is_docker) {
srs_warn("disable daemon for docker");
in_daemon = false;
}
}

// If not daemon, directly run master.
if (!_srs_config->get_daemon()) {
if (!in_daemon) {
if ((err = run_master(svr)) != srs_success) {
return srs_error_wrap(err, "run master");
}
Expand Down

0 comments on commit ed41ca8

Please sign in to comment.