]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
exec-util: allow to invoke polkit/ask-password agent even if STDIN is not a tty
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 1 Dec 2024 08:36:33 +0000 (17:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 11 Dec 2024 23:30:55 +0000 (08:30 +0900)
Closes #35018.

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

index 62b73503cae8349ea6d0a8737773a7853d64e6fc..d02d68a4e1030ef28d33eba07a72cc8ddef3c748 100644 (file)
@@ -18,12 +18,8 @@ int ask_password_agent_open(void) {
         if (agent_pid > 0)
                 return 0;
 
-        /* We check STDIN here, not STDOUT, since this is about input, not output */
-        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 */
+        /* 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;
index 8435c4f11803f1c85cd6fddeb0b65774b7848807..599b925a99e8cc1f863cec6d1872af1e8e533209 100644 (file)
@@ -544,7 +544,6 @@ int fexecve_or_execve(int executable_fd, const char *executable, char *const arg
 }
 
 int _fork_agent(const char *name, const int except[], size_t n_except, pid_t *ret_pid, const char *path, ...) {
-        bool stdout_is_tty, stderr_is_tty;
         size_t n, i;
         va_list ap;
         char **l;
@@ -567,17 +566,18 @@ int _fork_agent(const char *name, const int except[], size_t n_except, pid_t *re
 
         /* In the child: */
 
-        stdout_is_tty = isatty_safe(STDOUT_FILENO);
-        stderr_is_tty = isatty_safe(STDERR_FILENO);
+        bool stdin_is_tty = isatty_safe(STDIN_FILENO),
+                stdout_is_tty = isatty_safe(STDOUT_FILENO),
+                stderr_is_tty = isatty_safe(STDERR_FILENO);
 
-        if (!stdout_is_tty || !stderr_is_tty) {
+        if (!stdin_is_tty || !stdout_is_tty || !stderr_is_tty) {
                 int fd;
 
-                /* Detach from stdout/stderr and reopen /dev/tty for them. This is important to ensure that
-                 * when systemctl is started via popen() or a similar call that expects to read EOF we
+                /* Detach from stdin/stdout/stderr and reopen /dev/tty for them. This is important to ensure
+                 * that when systemctl is started via popen() or a similar call that expects to read EOF we
                  * actually do generate EOF and not delay this indefinitely by keeping an unused copy of
                  * stdin around. */
-                fd = open("/dev/tty", O_WRONLY);
+                fd = open("/dev/tty", stdin_is_tty ? O_WRONLY : (stdout_is_tty && stderr_is_tty) ? O_RDONLY : O_RDWR);
                 if (fd < 0) {
                         if (errno != ENXIO) {
                                 log_error_errno(errno, "Failed to open /dev/tty: %m");
@@ -588,13 +588,18 @@ int _fork_agent(const char *name, const int except[], size_t n_except, pid_t *re
                          * connected to a TTY. That's a weird setup, but let's handle it gracefully: let's
                          * skip the forking of the agents, given the TTY setup is not in order. */
                 } else {
+                        if (!stdin_is_tty && dup2(fd, STDIN_FILENO) < 0) {
+                                log_error_errno(errno, "Failed to dup2 /dev/tty to STDIN: %m");
+                                _exit(EXIT_FAILURE);
+                        }
+
                         if (!stdout_is_tty && dup2(fd, STDOUT_FILENO) < 0) {
-                                log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+                                log_error_errno(errno, "Failed to dup2 /dev/tty to STDOUT: %m");
                                 _exit(EXIT_FAILURE);
                         }
 
                         if (!stderr_is_tty && dup2(fd, STDERR_FILENO) < 0) {
-                                log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+                                log_error_errno(errno, "Failed to dup2 /dev/tty to STDERR: %m");
                                 _exit(EXIT_FAILURE);
                         }
 
index 842e41e8dbf3285283b06c0f108b13d063ee3ec7..d87eb56164dc175757f505a4424b3f2e30601281 100644 (file)
@@ -31,12 +31,8 @@ int polkit_agent_open(void) {
         if (geteuid() == 0)
                 return 0;
 
-        /* We check STDIN here, not STDOUT, since this is about input, not output */
-        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 */
+        /* 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;