From fc3691a70adefd5519bd529fe3a204015caa476b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 3 Dec 2024 18:40:06 +0900 Subject: [PATCH] exec-util: split out common checks before fork_agent() to can_fork_agent() No functional change, just refactoring. --- src/shared/ask-password-agent.c | 12 ++---------- src/shared/exec-util.c | 17 +++++++++++++++++ src/shared/exec-util.h | 1 + src/shared/polkit-agent.c | 12 ++---------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/shared/ask-password-agent.c b/src/shared/ask-password-agent.c index d02d68a4e10..b16d222359c 100644 --- a/src/shared/ask-password-agent.c +++ b/src/shared/ask-password-agent.c @@ -8,7 +8,6 @@ #include "exec-util.h" #include "log.h" #include "process-util.h" -#include "terminal-util.h" static pid_t agent_pid = 0; @@ -18,17 +17,10 @@ int ask_password_agent_open(void) { if (agent_pid > 0) return 0; - /* 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) + r = shall_fork_agent(); + if (r <= 0) return r; - if (!is_main_thread()) - return -EPERM; - r = fork_agent("(sd-askpwagent)", NULL, 0, &agent_pid, diff --git a/src/shared/exec-util.c b/src/shared/exec-util.c index 599b925a99e..e044cea3300 100644 --- a/src/shared/exec-util.c +++ b/src/shared/exec-util.c @@ -543,6 +543,23 @@ int fexecve_or_execve(int executable_fd, const char *executable, char *const arg return -errno; } +int shall_fork_agent(void) { + int r; + + /* 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 false; + if (r < 0) + return r; + + if (!is_main_thread()) + return -EPERM; + + return true; +} + int _fork_agent(const char *name, const int except[], size_t n_except, pid_t *ret_pid, const char *path, ...) { size_t n, i; va_list ap; diff --git a/src/shared/exec-util.h b/src/shared/exec-util.h index ca7d30d45e0..4f8eae26e74 100644 --- a/src/shared/exec-util.h +++ b/src/shared/exec-util.h @@ -61,5 +61,6 @@ ExecCommandFlags exec_command_flags_from_string(const char *s); int fexecve_or_execve(int executable_fd, const char *executable, char *const argv[], char *const envp[]); +int shall_fork_agent(void); int _fork_agent(const char *name, const int except[], size_t n_except, pid_t *ret_pid, const char *path, ...) _sentinel_; #define fork_agent(name, ...) _fork_agent(name, __VA_ARGS__, NULL) diff --git a/src/shared/polkit-agent.c b/src/shared/polkit-agent.c index 4a0d83e8ba9..ce1212e15e3 100644 --- a/src/shared/polkit-agent.c +++ b/src/shared/polkit-agent.c @@ -14,7 +14,6 @@ #include "polkit-agent.h" #include "process-util.h" #include "stdio-util.h" -#include "terminal-util.h" #include "time-util.h" #if ENABLE_POLKIT @@ -32,17 +31,10 @@ int polkit_agent_open(void) { if (geteuid() == 0) return 0; - /* 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) + r = shall_fork_agent(); + if (r <= 0) return r; - if (!is_main_thread()) - return -EPERM; - if (pipe2(pipe_fd, 0) < 0) return -errno; -- 2.47.3