From: Petr Malat Date: Mon, 19 Jul 2021 19:51:25 +0000 (+0200) Subject: lxc_setup_ttys: Handle existing ttyN file without underlying device X-Git-Tag: lxc-5.0.0~133^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F3910%2Fhead;p=thirdparty%2Flxc.git 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 --- 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)",