From: Christian Brauner Date: Mon, 23 Oct 2017 12:23:12 +0000 (+0200) Subject: console: move ringbuffer into lxc_console_create() X-Git-Tag: lxc-3.0.0.beta1~195^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b988b33d924557b8ab88817ccf46f585a02cec6;p=thirdparty%2Flxc.git console: move ringbuffer into lxc_console_create() This makes the whole setup more flexible. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 88a1cf4e2..2e9077c1b 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -3083,65 +3083,7 @@ static bool verify_start_hooks(struct lxc_conf *conf) return true; } -/** - * Note that this function needs to run before the mainloop starts. Since we - * register a handler for the console's masterfd when we create the mainloop - * the console handler needs to see an allocated ringbuffer. - */ -static int lxc_setup_console_ringbuf(struct lxc_console *console) -{ - int ret; - struct lxc_ringbuf *buf = &console->ringbuf; - uint64_t size = console->log_size; - - /* no ringbuffer previously allocated and no ringbuffer requested */ - if (!buf->addr && size <= 0) - return 0; - - /* ringbuffer allocated but no new ringbuffer requested */ - if (buf->addr && size <= 0) { - lxc_ringbuf_release(buf); - buf->addr = NULL; - buf->r_off = 0; - buf->w_off = 0; - buf->size = 0; - TRACE("Deallocated console ringbuffer"); - return 0; - } - - if (size <= 0) - return 0; - - /* check wether the requested size for the ringbuffer has changed */ - if (buf->addr && buf->size != size) { - TRACE("Console ringbuffer size changed from %" PRIu64 - " to %" PRIu64 " bytes. Deallocating console ringbuffer", - buf->size, size); - lxc_ringbuf_release(buf); - } - - ret = lxc_ringbuf_create(buf, size); - if (ret < 0) { - ERROR("Failed to setup %" PRIu64 " byte console ringbuffer", size); - return -1; - } - - TRACE("Allocated %" PRIu64 " byte console ringbuffer", size); - return 0; -} - -int lxc_setup_parent(struct lxc_handler *handler) -{ - int ret; - - ret = lxc_setup_console_ringbuf(&handler->conf->console); - if (ret < 0) - return -1; - - return 0; -} - -int lxc_setup_child(struct lxc_handler *handler) +int lxc_setup(struct lxc_handler *handler) { int ret; const char *name = handler->name; diff --git a/src/lxc/conf.h b/src/lxc/conf.h index 063b58980..43eeb4ded 100644 --- a/src/lxc/conf.h +++ b/src/lxc/conf.h @@ -379,7 +379,7 @@ extern int lxc_delete_autodev(struct lxc_handler *handler); extern void lxc_clear_includes(struct lxc_conf *conf); extern int do_rootfs_setup(struct lxc_conf *conf, const char *name, const char *lxcpath); -extern int lxc_setup_child(struct lxc_handler *handler); +extern int lxc_setup(struct lxc_handler *handler); extern int lxc_setup_parent(struct lxc_handler *handler); extern int setup_resource_limits(struct lxc_list *limits, pid_t pid); extern int find_unmapped_nsid(struct lxc_conf *conf, enum idtype idtype); diff --git a/src/lxc/console.c b/src/lxc/console.c index 82af74df2..8548c511e 100644 --- a/src/lxc/console.c +++ b/src/lxc/console.c @@ -535,6 +535,53 @@ void lxc_console_delete(struct lxc_console *console) console->log_fd = -1; } +/** + * Note that this function needs to run before the mainloop starts. Since we + * register a handler for the console's masterfd when we create the mainloop + * the console handler needs to see an allocated ringbuffer. + */ +static int lxc_setup_console_ringbuf(struct lxc_console *console) +{ + int ret; + struct lxc_ringbuf *buf = &console->ringbuf; + uint64_t size = console->log_size; + + /* no ringbuffer previously allocated and no ringbuffer requested */ + if (!buf->addr && size <= 0) + return 0; + + /* ringbuffer allocated but no new ringbuffer requested */ + if (buf->addr && size <= 0) { + lxc_ringbuf_release(buf); + buf->addr = NULL; + buf->r_off = 0; + buf->w_off = 0; + buf->size = 0; + TRACE("Deallocated console ringbuffer"); + return 0; + } + + if (size <= 0) + return 0; + + /* check wether the requested size for the ringbuffer has changed */ + if (buf->addr && buf->size != size) { + TRACE("Console ringbuffer size changed from %" PRIu64 + " to %" PRIu64 " bytes. Deallocating console ringbuffer", + buf->size, size); + lxc_ringbuf_release(buf); + } + + ret = lxc_ringbuf_create(buf, size); + if (ret < 0) { + ERROR("Failed to setup %" PRIu64 " byte console ringbuffer", size); + return -1; + } + + TRACE("Allocated %" PRIu64 " byte console ringbuffer", size); + return 0; +} + int lxc_console_create(struct lxc_conf *conf) { int ret, saved_errno; @@ -587,6 +634,10 @@ int lxc_console_create(struct lxc_conf *conf) DEBUG("Using \"%s\" as console log file", console->log_path); } + ret = lxc_setup_console_ringbuf(console); + if (ret < 0) + goto err; + return 0; err: diff --git a/src/lxc/start.c b/src/lxc/start.c index ca80136d5..41035a11b 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -904,7 +904,7 @@ static int do_start(void *data) } /* Setup the container, ip, names, utsname, ... */ - ret = lxc_setup_child(handler); + ret = lxc_setup(handler); close(handler->data_sock[0]); close(handler->data_sock[1]); if (ret < 0) { @@ -1266,10 +1266,6 @@ static int lxc_spawn(struct lxc_handler *handler) flags &= ~CLONE_NEWNET; } - ret = lxc_setup_parent(handler); - if (ret < 0) - goto out_delete_net; - if (fork_before_clone) handler->pid = lxc_fork_attach_clone(do_start, handler, flags | CLONE_PARENT); else