libct/nsenter/nsexec.c: honor _LIBCONTAINER_LOGLEVEL

Currently, if the log level is not set to e.g. "debug", runc init sends
some debug logs to the parent, which parses and discards it.

It is better to not send those in the first place.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2021-07-27 18:46:31 -07:00
parent d5ffe83f94
commit f1b703fc45
+28 -13
View File
@@ -89,14 +89,21 @@ struct nlconfig_t {
size_t gidmappath_len; size_t gidmappath_len;
}; };
#define PANIC "panic" /*
#define FATAL "fatal" * Log levels are the same as in logrus.
#define ERROR "error" */
#define WARNING "warning" #define PANIC 0
#define INFO "info" #define FATAL 1
#define DEBUG "debug" #define ERROR 2
#define WARNING 3
#define INFO 4
#define DEBUG 5
#define TRACE 6
static const char *level_str[] = { "panic", "fatal", "error", "warning", "info", "debug", "trace" };
static int logfd = -1; static int logfd = -1;
static int loglevel = DEBUG;
/* /*
* List of netlink message types sent to us as part of bootstrapping the init. * List of netlink message types sent to us as part of bootstrapping the init.
@@ -134,13 +141,13 @@ int setns(int fd, int nstype)
} }
#endif #endif
static void write_log(const char *level, const char *format, ...) static void write_log(int level, const char *format, ...)
{ {
char *message = NULL, *stage = NULL, *json = NULL; char *message = NULL, *stage = NULL, *json = NULL;
va_list args; va_list args;
int ret; int ret;
if (logfd < 0 || level == NULL) if (logfd < 0 || level > loglevel)
goto out; goto out;
va_start(args, format); va_start(args, format);
@@ -162,7 +169,8 @@ static void write_log(const char *level, const char *format, ...)
goto out; goto out;
} }
ret = asprintf(&json, "{\"level\":\"%s\", \"msg\": \"%s[%d]: %s\"}\n", level, stage, getpid(), message); ret = asprintf(&json, "{\"level\":\"%s\", \"msg\": \"%s[%d]: %s\"}\n",
level_str[level], stage, getpid(), message);
if (ret < 0) { if (ret < 0) {
json = NULL; json = NULL;
goto out; goto out;
@@ -400,16 +408,18 @@ static int getenv_int(const char *name)
if (val == endptr || *endptr != '\0') if (val == endptr || *endptr != '\0')
bail("unable to parse %s=%s", name, val); bail("unable to parse %s=%s", name, val);
/* /*
* Sanity check: this must be a non-negative number. * Sanity check: this must be a small non-negative number.
*/ * Practically, we pass two fds (3 and 4) and a log level,
if (ret < 0) * for which the maximum is 6 (TRACE).
* */
if (ret < 0 || ret > TRACE)
bail("bad value for %s=%s (%d)", name, val, ret); bail("bad value for %s=%s (%d)", name, val, ret);
return ret; return ret;
} }
/* /*
* Sets up logging by getting log fd from the environment, * Sets up logging by getting log fd and log level from the environment,
* if available. * if available.
*/ */
static void setup_logpipe(void) static void setup_logpipe(void)
@@ -422,6 +432,11 @@ static void setup_logpipe(void)
return; return;
} }
logfd = i; logfd = i;
i = getenv_int("_LIBCONTAINER_LOGLEVEL");
if (i < 0)
return;
loglevel = i;
} }
/* Returns the clone(2) flag for a namespace, given the name of a namespace. */ /* Returns the clone(2) flag for a namespace, given the name of a namespace. */