]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
console: move ringbuffer into lxc_console_create()
authorChristian Brauner <christian.brauner@ubuntu.com>
Mon, 23 Oct 2017 12:23:12 +0000 (14:23 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 6 Nov 2017 23:54:53 +0000 (00:54 +0100)
This makes the whole setup more flexible.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c
src/lxc/conf.h
src/lxc/console.c
src/lxc/start.c

index 88a1cf4e207813b07627385368bb70cc2f5fed13..2e9077c1b1157400a6169667ab201acb234473fc 100644 (file)
@@ -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;
index 063b589808b624ed2012e36a9013973f307804dd..43eeb4dedf26b7815ccd4786755fd8bd78fe1e91 100644 (file)
@@ -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);
index 82af74df2f223415ca3a4e4e1c86a52e91cdc84d..8548c511ecf60a494f47d626c9cf4ecf3cc36abb 100644 (file)
@@ -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:
index ca80136d53391b7e13bc6c6299f1ea53b832ed3e..41035a11bde179cd34633a6789b723d35b4c916c 100644 (file)
@@ -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