Skip to content

Commit

Permalink
console: write ringbuffer to disk
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Christian Brauner committed Nov 6, 2017
1 parent 3b988b3 commit 40117d0
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/lxc/console.c
Expand Up @@ -512,10 +512,45 @@ static int lxc_console_peer_default(struct lxc_console *console)
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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 40117d0

Please sign in to comment.