From 61242b1f0f9cac399deb67c88c3b62d38218dba3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 20 Aug 2024 10:52:20 +0200 Subject: [PATCH] shared: invoke agents only when we have a controlling TTY being connected to a TTY is not really enough to determine interactivity in many cases. Let's also check if we have a controlling TTY. Inspired by #34016 --- src/shared/ask-password-agent.c | 8 ++++++++ src/shared/polkit-agent.c | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/shared/ask-password-agent.c b/src/shared/ask-password-agent.c index e425f825db8..62b73503cae 100644 --- a/src/shared/ask-password-agent.c +++ b/src/shared/ask-password-agent.c @@ -22,6 +22,14 @@ int ask_password_agent_open(void) { if (!isatty_safe(STDIN_FILENO)) return 0; + /* Also check if we have a controlling terminal. If not (ENXIO here), we aren't actually invoked + * interactively on a terminal, hence fail */ + r = get_ctty_devnr(0, NULL); + if (r == -ENXIO) + return 0; + if (r < 0) + return r; + if (!is_main_thread()) return -EPERM; diff --git a/src/shared/polkit-agent.c b/src/shared/polkit-agent.c index 92e576c9d25..842e41e8dbf 100644 --- a/src/shared/polkit-agent.c +++ b/src/shared/polkit-agent.c @@ -35,6 +35,14 @@ int polkit_agent_open(void) { if (!isatty_safe(STDIN_FILENO)) return 0; + /* Also check if we have a controlling terminal. If not (ENXIO here), we aren't actually invoked + * interactively on a terminal, hence fail */ + r = get_ctty_devnr(0, NULL); + if (r == -ENXIO) + return 0; + if (r < 0) + return r; + if (!is_main_thread()) return -EPERM; -- 2.47.3