diff options
author | Lennart Poettering <lennart@poettering.net> | 2024-07-10 18:52:42 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2024-07-19 11:41:43 +0200 |
commit | ce3a1593bc1b8273ea0f94895dbae9ba12420aac (patch) | |
tree | 445ec8e872d799cbfe80a8d5262b01b506fdfab2 /src/test | |
parent | terminal-util: unify code that resets /dev/console in common helper (diff) | |
download | systemd-ce3a1593bc1b8273ea0f94895dbae9ba12420aac.tar.xz systemd-ce3a1593bc1b8273ea0f94895dbae9ba12420aac.zip |
terminal-util: add terminal_is_pty_fd() helper
The helper checks if an fd references a pty
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test-terminal-util.c | 40 |
1 files changed, 40 insertions, 0 deletions
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(); |