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/basic/terminal-util.c | |
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/basic/terminal-util.c')
-rw-r--r-- | src/basic/terminal-util.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 54901ccada..4c4a715ff6 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -32,6 +32,7 @@ #include "io-util.h" #include "log.h" #include "macro.h" +#include "missing_magic.h" #include "namespace-util.h" #include "parse-util.h" #include "path-util.h" @@ -2117,3 +2118,31 @@ int terminal_fix_size(int input_fd, int output_fd) { log_debug("Fixed terminal dimensions to %ux%u based on ANSI sequence information.", columns, rows); return 1; } + +int terminal_is_pty_fd(int fd) { + int r; + + assert(fd >= 0); + + /* Returns true if we are looking at a pty, i.e. if it's backed by the /dev/pts/ file system */ + + if (!isatty_safe(fd)) + return false; + + r = is_fs_type_at(fd, NULL, DEVPTS_SUPER_MAGIC); + if (r != 0) + return r; + + /* The ptmx device is weird, it exists twice, once inside and once outside devpts. To detect the + * latter case, let's fire off an ioctl() that only works on ptmx devices. */ + + int v; + if (ioctl(fd, TIOCGPKT, &v) < 0) { + if (ERRNO_IS_NOT_SUPPORTED(errno)) + return false; + + return -errno; + } + + return true; +} |