From 21440d41b078d064a7bb3c2037b4fad08e621fe3 Mon Sep 17 00:00:00 2001
From: Gunnar Beutner <gunnar@beutner.name>
Date: Sat, 5 Jun 2021 00:40:16 +0200
Subject: [PATCH] Ensure that stdout and stderr are initialized even if no log
 file was specified

---
 client/connectdlg_common.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/client/connectdlg_common.c b/client/connectdlg_common.c
index 93398cac77..4d98833106 100644
--- a/client/connectdlg_common.c
+++ b/client/connectdlg_common.c
@@ -302,6 +302,7 @@ bool client_start_server(void)
     char dbg_lvl_buf[32]; /* Do not move this inside the block where it gets filled,
                            * it's needed via the argv[x] pointer later on, so must
                            * remain in scope. */
+    bool log_to_dev_null = FALSE;
 
     /* Set up the command-line parameters. */
     fc_snprintf(port_buf, sizeof(port_buf), "%d", internal_server_port);
@@ -376,16 +377,21 @@ bool client_start_server(void)
       fclose(stdout);
       fclose(stderr);
 
-      /* FIXME: include the port to avoid duplication? */
-      if (logfile) {
+      if (!logfile) {
+        log_to_dev_null = TRUE;
+        fd = open("/dev/null", O_WRONLY);
+      } else {
+        /* FIXME: include the port to avoid duplication? */
         fd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, 0644);
+      }
 
-        if (fd != 1) {
-          dup2(fd, 1);
-        }
-        if (fd != 2) {
-          dup2(fd, 2);
-        }
+      if (fd != 1) {
+        dup2(fd, 1);
+      }
+      if (fd != 2) {
+        dup2(fd, 2);
+      }
+      if (!log_to_dev_null) {
         fchmod(1, 0644);
       }
 
-- 
2.31.1