From 39c6cdb771a4375fb6cd465fbdcd005ca217f059 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 15 Feb 2018 18:55:58 +0100 Subject: [PATCH] console: dump ringbuffer to disk on container exit The console ringbuffer will be dumped to disk if the console log file is not rotated and it's size is not unlimited. In the former two cases we will have all data from the ringbuffer available anyway. Signed-off-by: Christian Brauner --- src/lxc/console.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/lxc/console.c b/src/lxc/console.c index 21be155ef..55b925e0c 100644 --- a/src/lxc/console.c +++ b/src/lxc/console.c @@ -705,10 +705,53 @@ out: return ret; } +int lxc_console_write_ringbuffer(struct lxc_console *console) +{ + char *r_addr; + ssize_t ret; + uint64_t used; + struct lxc_ringbuf *buf = &console->ringbuf; + + /* There's not log file where we can dump the ringbuffer to. */ + if (console->log_fd < 0) + return 0; + + /* The log file is simply appended to. */ + if (console->log_size == 0) + return 0; + + /* The log file is rotated. */ + if (console->log_rotate == 0) + return 0; + + used = lxc_ringbuf_used(buf); + if (used == 0) + return 0; + + ret = lxc_console_truncate_log_file(console); + if (ret < 0) + return ret; + + /* Write as much as we can without exceeding the limit. */ + if (console->log_size < used) + used = console->log_size; + + r_addr = lxc_ringbuf_get_read_addr(buf); + ret = lxc_write_nointr(console->log_fd, r_addr, used); + if (ret < 0) + return -EIO; + + 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 ringbuffer to console log file"); + if (console->tios && console->peer >= 0) { ret = tcsetattr(console->peer, TCSAFLUSH, console->tios); if (ret < 0) -- 2.47.2