]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: improve lxc_setup_dev_console()
authorChristian Brauner <christian.brauner@ubuntu.com>
Mon, 8 May 2017 19:11:58 +0000 (21:11 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 9 May 2017 21:18:55 +0000 (23:18 +0200)
In case the user did request a console to be set up unmount any prior
bind-mount for it.

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

index e338a55b43fb1905b9205f082203062ceaec374a..0ba854b47366fba84b328e8f1b2274653db76c5a 100644 (file)
@@ -1490,10 +1490,38 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs,
        char path[MAXPATHLEN];
        int ret, fd;
 
+       if (console->path && !strcmp(console->path, "none"))
+               return 0;
+
        ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs->mount);
        if (ret < 0 || (size_t)ret >= sizeof(path))
                return -1;
 
+       /* When we are asked to setup a console we remove any previous
+        * /dev/console bind-mounts.
+        */
+       ret = umount(path);
+       if (ret < 0) {
+               if (errno != EINVAL && errno != ENOENT) {
+                       /* EINVAL: path is not a mountpoint
+                        * ENOENT: path does not exist
+                        * anything else means something weird is happening.
+                        */
+                       ERROR("failed to unmount \"%s\": %s", path, strerror(errno));
+                       return -errno;
+               }
+       } else {
+               DEBUG("unmounted console \"%s\"", path);
+       }
+       ret = unlink(path);
+       if (ret && errno != ENOENT) {
+               SYSERROR("error unlinking %s", path);
+               return -errno;
+       }
+
+       /* For unprivileged containers autodev or automounts will already have
+        * taken care of creating /dev/console.
+        */
        fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH);
        if (fd < 0) {
                if (errno != EEXIST) {
@@ -1504,11 +1532,6 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs,
                close(fd);
        }
 
-       if (console->master < 0) {
-               INFO("no console");
-               return 0;
-       }
-
        if (chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH)) {
                SYSERROR("failed to set mode '0%o' to '%s'", S_IXUSR | S_IXGRP | S_IXOTH, console->name);
                return -errno;