]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc_setup_ttys: Handle existing ttyN file without underlying device 3910/head
authorPetr Malat <oss@malat.biz>
Mon, 19 Jul 2021 19:51:25 +0000 (21:51 +0200)
committerPetr Malat <oss@malat.biz>
Tue, 20 Jul 2021 13:43:24 +0000 (15:43 +0200)
If a device file is opened and there isn't the underlying device,
the open call fails with ENXIO, but the path can be opened with
O_PATH, which is enough for mounting over the device file.

Generalize this idea and use O_PATH for all cases when the file
is there. One still must check for both ENXIO and EEXIST as it's
unspecified what error is reported if multiple error conditions
occur at the same time.

Signed-off-by: Petr Malat <oss@malat.biz>
src/lxc/conf.c

index d691a8b041668a65561e134b417241fa32de5c7f..10971e7a439b5bd0438541f00e5983bdbf858555 100644 (file)
@@ -942,6 +942,19 @@ static bool append_ttyname(char **pp, char *name)
        return true;
 }
 
+static int open_ttymnt_at(int dfd, const char *path)
+{
+       int fd;
+
+       fd = open_at(dfd, path, PROTECT_OPEN | O_CREAT | O_EXCL,
+                    PROTECT_LOOKUP_BENEATH, 0);
+       if (fd < 0 && (errno == ENXIO || errno == EEXIST))
+               fd = open_at(dfd, path, PROTECT_OPATH_FILE,
+                            PROTECT_LOOKUP_BENEATH, 0);
+
+       return fd;
+}
+
 static int lxc_setup_ttys(struct lxc_conf *conf)
 {
        int ret;
@@ -968,9 +981,7 @@ static int lxc_setup_ttys(struct lxc_conf *conf)
                        tty_name = tty_path + strlen(ttydir) + 1;
 
                        /* create bind-mount target */
-                       fd_to = open_at(rootfs->dfd_dev, tty_path,
-                                       PROTECT_OPEN_W | O_CREAT,
-                                       PROTECT_LOOKUP_BENEATH, 0);
+                       fd_to = open_ttymnt_at(rootfs->dfd_dev, tty_path);
                        if (fd_to < 0)
                                return log_error_errno(-errno, errno,
                                                       "Failed to create tty mount target %d(%s)",
@@ -1011,9 +1022,7 @@ static int lxc_setup_ttys(struct lxc_conf *conf)
                                return ret_errno(-EIO);
 
                        /* If we populated /dev, then we need to create /dev/tty<idx>. */
-                       fd_to = open_at(rootfs->dfd_dev, rootfs->buf,
-                                       PROTECT_OPEN_W | O_CREAT,
-                                       PROTECT_LOOKUP_BENEATH, 0);
+                       fd_to = open_ttymnt_at(rootfs->dfd_dev, rootfs->buf);
                        if (fd_to < 0)
                                return log_error_errno(-errno, errno,
                                                       "Failed to create tty mount target %d(%s)",