]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
console: move pty creation to separate function
authorChristian Brauner <christian.brauner@ubuntu.com>
Sat, 23 Dec 2017 10:59:36 +0000 (11:59 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 19 Jan 2018 13:45:45 +0000 (14:45 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/console.c
src/lxc/console.h

index bf9b8b7191130f807e06c5d844a526dfbc99a6a3..24d191adff661247b951e59d5f31b4f74271dcbf 100644 (file)
@@ -576,24 +576,32 @@ void lxc_console_delete(struct lxc_console *console)
        console->log_fd = -1;
 }
 
-int lxc_console_create(struct lxc_conf *conf)
+/**
+ * This is the console log file. Please note that the console log file is
+ * (implementation wise not content wise) independent of the console ringbuffer.
+ */
+int lxc_console_create_log_file(struct lxc_console *console)
 {
-       int ret, saved_errno;
-       struct lxc_console *console = &conf->console;
-
-       if (!conf->rootfs.path) {
-               INFO("Container does not have a rootfs. The console will be "
-                    "shared with the host");
+       if (!console->log_path)
                return 0;
-       }
 
-       if (console->path && !strcmp(console->path, "none")) {
-               INFO("No console was requested");
-               return 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);
+               return -1;
        }
 
+       DEBUG("Using \"%s\" as console log file", console->log_path);
+       return 0;
+}
+
+int lxc_pty_create(struct lxc_console *console)
+{
+       int ret, saved_errno;
+
        process_lock();
-       ret = openpty(&console->master, &console->slave, console->name, NULL, NULL);
+       ret = openpty(&console->master, &console->slave, console->name, NULL,
+                     NULL);
        saved_errno = errno;
        process_unlock();
        if (ret < 0) {
@@ -619,15 +627,38 @@ int lxc_console_create(struct lxc_conf *conf)
                goto err;
        }
 
-       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);
-                       goto err;
-               }
-               DEBUG("Using \"%s\" as console log file", console->log_path);
+       return 0;
+
+err:
+       lxc_console_delete(console);
+       return -ENODEV;
+}
+
+int lxc_console_create(struct lxc_conf *conf)
+{
+       int ret;
+       struct lxc_console *console = &conf->console;
+
+       if (!conf->rootfs.path) {
+               INFO("Container does not have a rootfs. The console will be "
+                    "shared with the host");
+               return 0;
        }
 
+       if (console->path && !strcmp(console->path, "none")) {
+               INFO("No console was requested");
+               return 0;
+       }
+
+       ret = lxc_pty_create(console);
+       if (ret < 0)
+               return -1;
+
+       /* create console log file */
+       ret = lxc_console_create_log_file(console);
+       if (ret < 0)
+               goto err;
+
        return 0;
 
 err:
index 290ede63928a6e3e037c13ff1c8367b3292d8717..779d1cac576a35348ed29b6920cf4fb0b873b9c9 100644 (file)
@@ -82,6 +82,12 @@ extern int  lxc_console_allocate(struct lxc_conf *conf, int sockfd, int *ttynum)
  * automatically chowned to the uid/gid of the unprivileged user. For this
  * ttys_shift_ids() can be called.)
  */
+extern int lxc_pty_create(struct lxc_console *console);
+
+/**
+ * lxc_console_create: Create a new pty.
+ * - In addition to lxc_pty_create() also sets up all pty logs.
+ */
 extern int  lxc_console_create(struct lxc_conf *);
 
 /*