console: move ringbuffer into lxc_console_create()

This makes the whole setup more flexible. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent 69629c82
......@@ -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;
......
......@@ -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);
......
......@@ -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:
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment