]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
console: dump ringbuffer to disk on container exit
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 15 Feb 2018 17:55:58 +0000 (18:55 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 27 Feb 2018 20:38:31 +0000 (21:38 +0100)
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 <christian.brauner@ubuntu.com>
src/lxc/console.c

index 21be155ef7b97f125da7b11c63e3ac34089d3aa9..55b925e0cf10fc240456f7e5083acc94261c15a3 100644 (file)
@@ -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)