]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
exec-util: split out common checks before fork_agent() to can_fork_agent()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 3 Dec 2024 09:40:06 +0000 (18:40 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 11 Dec 2024 23:32:42 +0000 (08:32 +0900)
No functional change, just refactoring.

src/shared/ask-password-agent.c
src/shared/exec-util.c
src/shared/exec-util.h
src/shared/polkit-agent.c

index d02d68a4e1030ef28d33eba07a72cc8ddef3c748..b16d222359c10d92c5c26900c93f783941967698 100644 (file)
@@ -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,
index 599b925a99e8cc1f863cec6d1872af1e8e533209..e044cea33002825460c2e601ff0dbbbad2915e3e 100644 (file)
@@ -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;
index ca7d30d45e0f7b01ba0ed65aa5403e1a00d0b806..4f8eae26e74fa01be758e55fa328404e053f0d5f 100644 (file)
@@ -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)
index 4a0d83e8ba9146e2c39897afe60935928c8f510a..ce1212e15e3d88eb7aa302b1880d0daf98705a9e 100644 (file)
@@ -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;