diff --git a/libcontainer/nsenter/nsexec.c b/libcontainer/nsenter/nsexec.c index c2fa8aa34..7e79ca0eb 100644 --- a/libcontainer/nsenter/nsexec.c +++ b/libcontainer/nsenter/nsexec.c @@ -89,14 +89,21 @@ struct nlconfig_t { size_t gidmappath_len; }; -#define PANIC "panic" -#define FATAL "fatal" -#define ERROR "error" -#define WARNING "warning" -#define INFO "info" -#define DEBUG "debug" +/* + * Log levels are the same as in logrus. + */ +#define PANIC 0 +#define FATAL 1 +#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 loglevel = DEBUG; /* * 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 -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; va_list args; int ret; - if (logfd < 0 || level == NULL) + if (logfd < 0 || level > loglevel) goto out; va_start(args, format); @@ -162,7 +169,8 @@ static void write_log(const char *level, const char *format, ...) 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) { json = NULL; goto out; @@ -400,16 +408,18 @@ static int getenv_int(const char *name) if (val == endptr || *endptr != '\0') bail("unable to parse %s=%s", name, val); /* - * Sanity check: this must be a non-negative number. - */ - if (ret < 0) + * Sanity check: this must be a small non-negative number. + * Practically, we pass two fds (3 and 4) and a log level, + * for which the maximum is 6 (TRACE). + * */ + if (ret < 0 || ret > TRACE) bail("bad value for %s=%s (%d)", name, val, 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. */ static void setup_logpipe(void) @@ -422,6 +432,11 @@ static void setup_logpipe(void) return; } 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. */