From 0a8b555ceb07ce916b9bd48782d1eb69a12f0f2e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 3 Aug 2015 19:04:08 +0200 Subject: terminal-util: when resetting terminals, don't wait for carrier In case of non-CLOCAL lines (i.e. those with carrier detect configured) we shouldnt wait for a carrier if all we try to do is reset the TTY. Hence, whenever we open such a TTY pass O_NONBLOCK. Note that we continue to open ttys we intend to write to without O_ONBLOCK, we only add it in cases we invoke ioctl()s or other terminal operations without reading or writing to the device. Fixes #835. --- src/basic/terminal-util.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/basic') diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 042b88f222..20edfddf5b 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -44,7 +44,7 @@ static volatile unsigned cached_lines = 0; int chvt(int vt) { _cleanup_close_ int fd; - fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); + fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); if (fd < 0) return -errno; @@ -284,7 +284,11 @@ finish: int reset_terminal(const char *name) { _cleanup_close_ int fd = -1; - fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); + /* We open the terminal with O_NONBLOCK here, to ensure we + * don't block on carrier if this is a terminal with carrier + * configured. */ + + fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); if (fd < 0) return fd; @@ -499,7 +503,7 @@ int release_terminal(void) { struct sigaction sa_old; int r = 0; - fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC); + fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); if (fd < 0) return -errno; @@ -527,7 +531,7 @@ int terminal_vhangup_fd(int fd) { int terminal_vhangup(const char *name) { _cleanup_close_ int fd; - fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); + fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); if (fd < 0) return fd; @@ -574,7 +578,7 @@ int vt_disallocate(const char *name) { return -EINVAL; /* Try to deallocate */ - fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); + fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); if (fd < 0) return fd; -- cgit v1.2.3-54-g00ecf From 7d927c9a4d3e05a87659e89844a7d26bb0b86d8b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 3 Aug 2015 19:05:43 +0200 Subject: terminal-util: cast a couple of ioctl()s to void --- src/basic/terminal-util.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'src/basic') diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 20edfddf5b..f4c7489e09 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -230,14 +230,14 @@ int reset_terminal_fd(int fd, bool switch_to_text) { * interfere with that. */ /* Disable exclusive mode, just in case */ - ioctl(fd, TIOCNXCL); + (void) ioctl(fd, TIOCNXCL); /* Switch to text mode */ if (switch_to_text) - ioctl(fd, KDSETMODE, KD_TEXT); + (void) ioctl(fd, KDSETMODE, KD_TEXT); /* Enable console unicode mode */ - ioctl(fd, KDSKBMODE, K_UNICODE); + (void) ioctl(fd, KDSKBMODE, K_UNICODE); if (tcgetattr(fd, &termios) < 0) { r = -errno; @@ -276,7 +276,7 @@ int reset_terminal_fd(int fd, bool switch_to_text) { finish: /* Just in case, flush all crap out */ - tcflush(fd, TCIOFLUSH); + (void) tcflush(fd, TCIOFLUSH); return r; } @@ -417,9 +417,8 @@ int acquire_terminal( if (r < 0 && r == -EPERM && ignore_tiocstty_eperm) r = 0; - if (r < 0 && (force || fail || r != -EPERM)) { + if (r < 0 && (force || fail || r != -EPERM)) goto fail; - } if (r >= 0) break; @@ -616,16 +615,16 @@ void warn_melody(void) { /* Yeah, this is synchronous. Kinda sucks. But well... */ - ioctl(fd, KIOCSOUND, (int)(1193180/440)); + (void) ioctl(fd, KIOCSOUND, (int)(1193180/440)); usleep(125*USEC_PER_MSEC); - ioctl(fd, KIOCSOUND, (int)(1193180/220)); + (void) ioctl(fd, KIOCSOUND, (int)(1193180/220)); usleep(125*USEC_PER_MSEC); - ioctl(fd, KIOCSOUND, (int)(1193180/220)); + (void) ioctl(fd, KIOCSOUND, (int)(1193180/220)); usleep(125*USEC_PER_MSEC); - ioctl(fd, KIOCSOUND, 0); + (void) ioctl(fd, KIOCSOUND, 0); } int make_console_stdio(void) { -- cgit v1.2.3-54-g00ecf From 35bdab779ff1ebff72b9a9b1c661978d14793924 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 3 Aug 2015 19:06:01 +0200 Subject: terminal-util: no real reason to assert on O_CREAT That's just handle this as a normal error. --- src/basic/terminal-util.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/basic') diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index f4c7489e09..cf55263bbf 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -308,7 +308,8 @@ int open_terminal(const char *name, int mode) { * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245 */ - assert(!(mode & O_CREAT)); + if (mode & O_CREAT) + return -EINVAL; for (;;) { fd = open(name, mode, 0); -- cgit v1.2.3-54-g00ecf