From 66fad903848e34daa0384687243bbe59d19a2a2a Mon Sep 17 00:00:00 2001 From: David Herrmann Date: Sun, 2 Dec 2012 23:27:00 +0100 Subject: [PATCH] uterm: vt: fix opening new VTs If no VT name is explicitly given, we need to use VT_OPENQRY to find one. This used to work until we switched to supporting fake-VTs. Fix this again to work like it used to. Signed-off-by: David Herrmann --- src/uterm_vt.c | 58 ++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/src/uterm_vt.c b/src/uterm_vt.c index 88fc924..42af3c6 100644 --- a/src/uterm_vt.c +++ b/src/uterm_vt.c @@ -271,39 +271,12 @@ static void real_vt_input(struct ev_fd *fd, int mask, void *data) static int open_tty(const char *dev, int *tty_fd, int *tty_num) { - int fd, err1, id, ret; - char filename[16]; + int fd, ret, id; struct stat st; - if (!tty_fd || !tty_num) + if (!dev || !tty_fd || !tty_num) return -EINVAL; - if (!dev) { - fd = open("/dev/tty0", O_NONBLOCK | O_NOCTTY | O_CLOEXEC); - if (fd < 0) { - err1 = errno; - fd = open("/dev/tty1", - O_NONBLOCK | O_NOCTTY | O_CLOEXEC); - if (fd < 0) { - log_error("cannot find parent tty (%d, %d): %m", - err1, errno); - return -EFAULT; - } - } - - errno = 0; - if (ioctl(fd, VT_OPENQRY, &id) || id <= 0) { - close(fd); - log_err("cannot get unused tty (%d): %m", errno); - return -EINVAL; - } - close(fd); - - snprintf(filename, sizeof(filename), "/dev/tty%d", id); - filename[sizeof(filename) - 1] = 0; - dev = filename; - } - log_notice("using tty %s", dev); fd = open(dev, O_RDWR | O_NOCTTY | O_CLOEXEC); @@ -768,7 +741,7 @@ static int seat_find_vt(const char *seat, char **out) { static const char def_vt[] = "/dev/tty0"; char *vt; - int ret; + int ret, fd, err1, id; ret = asprintf(&vt, "/dev/ttyF%s", seat); if (ret < 0) @@ -783,9 +756,30 @@ static int seat_find_vt(const char *seat, char **out) free(vt); if (!strcmp(seat, "seat0") && !access(def_vt, F_OK)) { - vt = strdup(def_vt); - if (!vt) + fd = open(def_vt, O_NONBLOCK | O_NOCTTY | O_CLOEXEC); + if (fd < 0) { + err1 = errno; + fd = open("/dev/tty1", + O_NONBLOCK | O_NOCTTY | O_CLOEXEC); + if (fd < 0) { + log_error("cannot find parent tty (%d, %d): %m", + err1, errno); + return -EFAULT; + } + } + + errno = 0; + if (ioctl(fd, VT_OPENQRY, &id) || id <= 0) { + close(fd); + log_err("cannot get unused tty (%d): %m", errno); + return -EINVAL; + } + close(fd); + + ret = asprintf(&vt, "/dev/tty%d", id); + if (ret < 0) return -ENOMEM; + *out = vt; return 0; }