From: Christian Brauner Date: Tue, 14 Nov 2017 23:23:07 +0000 (+0100) Subject: confile: add lxc.console.buffer.logfile X-Git-Tag: lxc-3.0.0.beta1~182^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3a784510e314b3d161663c5240ff32532c349c2c;p=thirdparty%2Flxc.git confile: add lxc.console.buffer.logfile Signed-off-by: Christian Brauner --- diff --git a/doc/lxc.container.conf.sgml.in b/doc/lxc.container.conf.sgml.in index 2ed83ca78..2e2879e71 100644 --- a/doc/lxc.container.conf.sgml.in +++ b/doc/lxc.container.conf.sgml.in @@ -707,6 +707,26 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + + + + + + Setting this option instructs liblxc to write the in-memory + ringbuffer to disk. For performance reasons liblxc will only write + the in-memory ringbuffer to disk when requested. Note that the this + option is only used by liblxc when + is set. + + By default liblxc will dump the contents of the in-memory ringbuffer + to disk when the container terminates. This allows users to diagnose + boot failures when the container crashed before an API request to + retrieve the in-memory ringbuffer could be sent or handled. + + + + diff --git a/src/lxc/commands.c b/src/lxc/commands.c index 653fd994f..c158672ed 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -1051,7 +1051,7 @@ static int lxc_cmd_console_log_callback(int fd, struct lxc_cmd_req *req, if (log->write_logfile && rsp.datalen > 0) { rsp.ret = -ENOENT; - if (!console->log_path) + if (!console->buffer_log_file) goto out; rsp.ret = lxc_console_write_ringbuffer(console); diff --git a/src/lxc/conf.c b/src/lxc/conf.c index a2b3673a7..4681a7274 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -2430,9 +2430,11 @@ struct lxc_conf *lxc_conf_init(void) new->loglevel = LXC_LOG_LEVEL_NOTSET; new->personality = -1; new->autodev = 1; + new->console.buffer_log_file = NULL; + new->console.buffer_log_file_fd = -1; + new->console.buffer_size = 0; new->console.log_path = NULL; new->console.log_fd = -1; - new->console.buffer_size = 0; new->console.path = NULL; new->console.peer = -1; new->console.peerpty.busy = -1; @@ -3461,6 +3463,7 @@ void lxc_conf_free(struct lxc_conf *conf) return; if (current_config == conf) current_config = NULL; + free(conf->console.buffer_log_file); free(conf->console.log_path); free(conf->console.path); if (conf->console.buffer_size > 0 && conf->console.ringbuf.addr) diff --git a/src/lxc/conf.h b/src/lxc/conf.h index 1dc892430..b07c92e74 100644 --- a/src/lxc/conf.h +++ b/src/lxc/conf.h @@ -153,7 +153,17 @@ struct lxc_console { char name[MAXPATHLEN]; struct termios *tios; struct lxc_tty_state *tty_state; + + /* size of the ringbuffer */ uint64_t buffer_size; + + /* path to the log file for the ringbuffer */ + char *buffer_log_file; + + /* fd to the log file for the ringbuffer */ + int buffer_log_file_fd; + + /* the in-memory ringbuffer */ struct lxc_ringbuf ringbuf; }; diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 852589531..977bea2ed 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -83,6 +83,7 @@ lxc_config_define(cap_keep); lxc_config_define(cgroup_controller); lxc_config_define(cgroup_dir); lxc_config_define(console_logfile); +lxc_config_define(console_buffer_logfile); lxc_config_define(console_buffer_size); lxc_config_define(console_path); lxc_config_define(environment); @@ -149,12 +150,13 @@ static struct lxc_config_t config[] = { { "lxc.cap.keep", false, set_config_cap_keep, get_config_cap_keep, clr_config_cap_keep, }, { "lxc.cgroup.dir", false, set_config_cgroup_dir, get_config_cgroup_dir, clr_config_cgroup_dir, }, { "lxc.cgroup", false, set_config_cgroup_controller, get_config_cgroup_controller, clr_config_cgroup_controller, }, + { "lxc.console.buffer.logfile", false, set_config_console_buffer_logfile, get_config_console_buffer_logfile, clr_config_console_buffer_logfile, }, { "lxc.console.buffer.size", false, set_config_console_buffer_size, get_config_console_buffer_size, clr_config_console_buffer_size, }, { "lxc.console.logfile", false, set_config_console_logfile, get_config_console_logfile, clr_config_console_logfile, }, { "lxc.console.path", false, set_config_console_path, get_config_console_path, clr_config_console_path, }, { "lxc.environment", false, set_config_environment, get_config_environment, clr_config_environment, }, { "lxc.ephemeral", false, set_config_ephemeral, get_config_ephemeral, clr_config_ephemeral, }, - { "lxc.execute.cmd", false, set_config_execute_cmd, get_config_execute_cmd, clr_config_execute_cmd, }, + { "lxc.execute.cmd", false, set_config_execute_cmd, get_config_execute_cmd, clr_config_execute_cmd, }, { "lxc.group", false, set_config_group, get_config_group, clr_config_group, }, { "lxc.hook.autodev", false, set_config_hooks, get_config_hooks, clr_config_hooks, }, { "lxc.hook.clone", false, set_config_hooks, get_config_hooks, clr_config_hooks, }, @@ -1841,6 +1843,13 @@ static int set_config_console_buffer_size(const char *key, const char *value, return 0; } +static int set_config_console_buffer_logfile(const char *key, const char *value, + struct lxc_conf *lxc_conf, + void *data) +{ + return set_config_path_item(&lxc_conf->console.buffer_log_file, value); +} + int append_unexp_config_line(const char *line, struct lxc_conf *conf) { size_t len = conf->unexpanded_len, linelen = strlen(line); @@ -3098,6 +3107,13 @@ static int get_config_console_buffer_size(const char *key, char *retv, return lxc_get_conf_uint64(c, retv, inlen, c->autodev); } +static int get_config_console_buffer_logfile(const char *key, char *retv, + int inlen, struct lxc_conf *c, + void *data) +{ + return lxc_get_conf_str(retv, inlen, c->console.buffer_log_file); +} + static int get_config_seccomp_profile(const char *key, char *retv, int inlen, struct lxc_conf *c, void *data) { @@ -3509,6 +3525,15 @@ static inline int clr_config_console_buffer_size(const char *key, return 0; } +static inline int clr_config_console_buffer_logfile(const char *key, + struct lxc_conf *c, + void *data) +{ + free(c->console.buffer_log_file); + c->console.buffer_log_file = NULL; + return 0; +} + static inline int clr_config_seccomp_profile(const char *key, struct lxc_conf *c, void *data) { diff --git a/src/lxc/console.c b/src/lxc/console.c index c16e39bb6..d83b3830e 100644 --- a/src/lxc/console.c +++ b/src/lxc/console.c @@ -558,19 +558,19 @@ int lxc_console_write_ringbuffer(struct lxc_console *console) uint64_t used; struct lxc_ringbuf *buf = &console->ringbuf; - if (!console->log_path) + if (!console->buffer_log_file) 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 | O_TRUNC, 0600)); + fd = lxc_unpriv(open(console->buffer_log_file, O_CLOEXEC | O_RDWR | O_CREAT | O_TRUNC, 0600)); if (fd < 0) { - SYSERROR("Failed to open console log file \"%s\"", console->log_path); + SYSERROR("Failed to open console log file \"%s\"", console->buffer_log_file); return -EIO; } - DEBUG("Using \"%s\" as console log file", console->log_path); + DEBUG("Using \"%s\" as console log file", console->buffer_log_file); r_addr = lxc_ringbuf_get_read_addr(buf); ret = lxc_write_nointr(fd, r_addr, used); @@ -698,7 +698,7 @@ int lxc_console_create(struct lxc_conf *conf) goto err; } - if (console->log_path && console->buffer_size <= 0) { + if (console->log_path) { 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);