From 128655e7654cc95f658d905927a97cf065136782 Mon Sep 17 00:00:00 2001 From: Petr Malat Date: Mon, 19 Jul 2021 21:51:25 +0200 Subject: [PATCH] lxc_setup_ttys: Handle existing ttyN file without underlying device 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 --- src/lxc/conf.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/lxc/conf.c b/src/lxc/conf.c index d691a8b04..10971e7a4 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -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. */ - 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)", -- 2.47.2