From: Christian Brauner Date: Thu, 26 Oct 2017 16:32:29 +0000 (+0200) Subject: console: write ringbuffer to disk X-Git-Tag: lxc-3.0.0.beta1~195^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=40117d053194dafd05b24b361632d2f686ff639b;p=thirdparty%2Flxc.git console: write ringbuffer to disk When users request that the container keep a console ringbuffer we will not continously write to the on-disk logfile as mirroring the contents of the in-memory ringbuffer on-disk is costly and complicated. Instead, we dump the ringbuffer contents on-disk when the container stops or fails to start. This way users can still diagnose problems or retrieve the last contents of the ringbuffer on-disk. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/console.c b/src/lxc/console.c index 8548c511e..98dda69ff 100644 --- a/src/lxc/console.c +++ b/src/lxc/console.c @@ -512,10 +512,45 @@ out: return ret; } +static int lxc_console_write_ringbuffer(struct lxc_console *console) +{ + int fd; + char *r_addr; + ssize_t ret; + uint64_t used; + struct lxc_ringbuf *buf = &console->ringbuf; + + if (!console->log_path) + return 0; + + used = lxc_ringbuf_used(buf); + if (used == 0) + return 0; + + fd = lxc_unpriv(open(console->log_path, O_CLOEXEC | O_RDWR | O_CREAT, 0600)); + if (fd < 0) { + SYSERROR("Failed to open console log file \"%s\"", console->log_path); + return -1; + } + DEBUG("Using \"%s\" as console log file", console->log_path); + + r_addr = lxc_ringbuf_get_read_addr(buf); + ret = lxc_write_nointr(fd, r_addr, used); + close(fd); + if (ret < 0) + return -1; + + return 0; +} + void lxc_console_delete(struct lxc_console *console) { int ret; + ret = lxc_console_write_ringbuffer(console); + if (ret < 0) + WARN("Failed to write console log to disk"); + if (console->tios && console->peer >= 0) { ret = tcsetattr(console->peer, TCSAFLUSH, console->tios); if (ret < 0) @@ -625,7 +660,7 @@ int lxc_console_create(struct lxc_conf *conf) goto err; } - if (console->log_path) { + if (console->log_path && console->log_size <= 0) { console->log_fd = lxc_unpriv(open(console->log_path, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600)); if (console->log_fd < 0) { SYSERROR("Failed to open console log file \"%s\"", console->log_path);