]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
console: write ringbuffer to disk
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 26 Oct 2017 16:32:29 +0000 (18:32 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 6 Nov 2017 23:54:53 +0000 (00:54 +0100)
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 <christian.brauner@ubuntu.com>
src/lxc/console.c

index 8548c511ecf60a494f47d626c9cf4ecf3cc36abb..98dda69ffb75ed9c9e000e97c338790c0c1fd926 100644 (file)
@@ -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);