From ce3a1593bc1b8273ea0f94895dbae9ba12420aac Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 10 Jul 2024 18:52:42 +0200 Subject: terminal-util: add terminal_is_pty_fd() helper The helper checks if an fd references a pty --- src/test/test-terminal-util.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/test/test-terminal-util.c') diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c index 9a89f55c80..2f09ae9f71 100644 --- a/src/test/test-terminal-util.c +++ b/src/test/test-terminal-util.c @@ -209,6 +209,46 @@ TEST(terminal_fix_size) { log_notice("Fixed terminal size."); } +TEST(terminal_is_pty_fd) { + _cleanup_close_ int fd1 = -EBADF, fd2 = -EBADF; + _cleanup_free_ char *peer = NULL; + int r; + + fd1 = openpt_allocate(O_RDWR, &peer); + assert_se(fd1 >= 0); + assert_se(terminal_is_pty_fd(fd1) > 0); + + fd2 = open_terminal(peer, O_RDWR|O_CLOEXEC|O_NOCTTY); + assert_se(fd2 >= 0); + assert_se(terminal_is_pty_fd(fd2) > 0); + + fd1 = safe_close(fd1); + fd2 = safe_close(fd2); + + fd1 = open("/dev/null", O_RDONLY|O_CLOEXEC); + assert_se(fd1 >= 0); + assert_se(terminal_is_pty_fd(fd1) == 0); + + /* In container managers real tty devices might be weird, avoid them. */ + r = path_is_read_only_fs("/sys"); + if (r != 0) + return; + + FOREACH_STRING(p, "/dev/ttyS0", "/dev/tty1") { + _cleanup_close_ int tfd = -EBADF; + + tfd = open_terminal(p, O_CLOEXEC|O_NOCTTY|O_RDONLY|O_NONBLOCK); + if (tfd == -ENOENT) + continue; + if (tfd < 0) { + log_notice_errno(tfd, "Failed to open '%s', skipping: %m", p); + continue; + } + + assert_se(terminal_is_pty_fd(tfd) <= 0); + } +} + static void test_get_color_mode_with_env(const char *key, const char *val, ColorMode expected) { ASSERT_OK(setenv(key, val, true)); reset_terminal_feature_caches(); -- cgit v1.2.3