Skip to content

Commit

Permalink
Refine docker detect.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Aug 7, 2023
1 parent 73dd8af commit eca40a5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

47 changes: 45 additions & 2 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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
Expand Down
48 changes: 3 additions & 45 deletions trunk/src/main/srs_main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit eca40a5

Please sign in to comment.