Skip to content

Commit b1bc762

Browse files
committed
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>
1 parent 90473ea commit b1bc762

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

libcontainer/nsenter/nsexec.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,21 @@ struct nlconfig_t {
8989
size_t gidmappath_len;
9090
};
9191

92-
#define PANIC "panic"
93-
#define FATAL "fatal"
94-
#define ERROR "error"
95-
#define WARNING "warning"
96-
#define INFO "info"
97-
#define DEBUG "debug"
92+
/*
93+
* Log levels are the same as in logrus.
94+
*/
95+
#define PANIC 0
96+
#define FATAL 1
97+
#define ERROR 2
98+
#define WARNING 3
99+
#define INFO 4
100+
#define DEBUG 5
101+
#define TRACE 6
102+
103+
static const char *level_str[] = { "panic", "fatal", "error", "warning", "info", "debug", "trace" };
98104

99105
static int logfd = -1;
106+
static int loglevel = INFO;
100107

101108
/*
102109
* List of netlink message types sent to us as part of bootstrapping the init.
@@ -134,13 +141,13 @@ int setns(int fd, int nstype)
134141
}
135142
#endif
136143

137-
static void write_log(const char *level, const char *format, ...)
144+
static void write_log(int level, const char *format, ...)
138145
{
139146
char *message = NULL, *stage = NULL;
140147
va_list args;
141148
int ret;
142149

143-
if (logfd < 0 || level == NULL)
150+
if (logfd < 0 || level > loglevel)
144151
goto out;
145152

146153
va_start(args, format);
@@ -158,7 +165,7 @@ static void write_log(const char *level, const char *format, ...)
158165
if (ret < 0)
159166
goto out;
160167

161-
dprintf(logfd, "{\"level\":\"%s\", \"msg\": \"%s[%d]: %s\"}\n", level, stage, getpid(), message);
168+
dprintf(logfd, "{\"level\":\"%s\", \"msg\": \"%s[%d]: %s\"}\n", level_str[level], stage, getpid(), message);
162169

163170
out:
164171
free(message);
@@ -386,16 +393,18 @@ static int getenv_int(const char *name)
386393
if (val == endptr || *endptr != '\0')
387394
bail("unable to parse %s=%s", name, val);
388395
/*
389-
* Sanity check: this must be a non-negative number.
390-
*/
391-
if (ret < 0)
396+
* Sanity check: this must be a small non-negative number.
397+
* Practically, we pass two fds (3 and 4) and a log level,
398+
* for which the maximum is 6 (TRACE).
399+
* */
400+
if (ret < 0 || ret > TRACE)
392401
bail("bad value for %s=%s (%d)", name, val, ret);
393402

394403
return ret;
395404
}
396405

397406
/*
398-
* Sets up logging by getting log fd from the environment,
407+
* Sets up logging by getting log fd and log level from the environment,
399408
* if available.
400409
*/
401410
static void setup_logpipe(void)
@@ -408,6 +417,11 @@ static void setup_logpipe(void)
408417
return;
409418
}
410419
logfd = i;
420+
421+
i = getenv_int("_LIBCONTAINER_LOGLEVEL");
422+
if (i < 0)
423+
return;
424+
loglevel = i;
411425
}
412426

413427
/* Returns the clone(2) flag for a namespace, given the name of a namespace. */

0 commit comments

Comments
 (0)