]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
exec-util: handle gracefully if we want to fork an agent but have no controlling tty 20583/head
authorLennart Poettering <lennart@poettering.net>
Mon, 30 Aug 2021 11:28:02 +0000 (13:28 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 30 Aug 2021 11:37:06 +0000 (13:37 +0200)
Fixes: #20576
src/shared/exec-util.c

index cffa3fe96e7644ce63f6a1cec37958f64c523c62..dc4214d162b458dcdf0234b25713ef21e3870084 100644 (file)
@@ -529,21 +529,27 @@ int fork_agent(const char *name, int except[], size_t n_except, pid_t *ret_pid,
                  * stdin around. */
                 fd = open("/dev/tty", O_WRONLY);
                 if (fd < 0) {
-                        log_error_errno(errno, "Failed to open /dev/tty: %m");
-                        _exit(EXIT_FAILURE);
-                }
+                        if (errno != -ENXIO) {
+                                log_error_errno(errno, "Failed to open /dev/tty: %m");
+                                _exit(EXIT_FAILURE);
+                        }
 
-                if (!stdout_is_tty && dup2(fd, STDOUT_FILENO) < 0) {
-                        log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
-                        _exit(EXIT_FAILURE);
-                }
+                        /* If we get ENXIO here we have no controlling TTY even though stdout/stderr are
+                         * 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 (!stdout_is_tty && dup2(fd, STDOUT_FILENO) < 0) {
+                                log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+                                _exit(EXIT_FAILURE);
+                        }
 
-                if (!stderr_is_tty && dup2(fd, STDERR_FILENO) < 0) {
-                        log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
-                        _exit(EXIT_FAILURE);
-                }
+                        if (!stderr_is_tty && dup2(fd, STDERR_FILENO) < 0) {
+                                log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+                                _exit(EXIT_FAILURE);
+                        }
 
-                safe_close_above_stdio(fd);
+                        fd = safe_close_above_stdio(fd);
+                }
         }
 
         (void) rlimit_nofile_safe();