From 459f07e344fd49b608db3240d1fb26470e12572f Mon Sep 17 00:00:00 2001
From: Jon Turney <jon.turney@dronecode.org.uk>
Date: Tue, 11 Jan 2022 17:13:27 +0000
Subject: [PATCH] Don't imply '-nolock' with '-displayfd'

Take server lockfile before trying to create socket for -displayfd.
---
 include/os.h    |  4 ++--
 os/connection.c | 17 +++++++++++++++--
 os/osinit.c     |  1 -
 os/utils.c      | 31 ++++++++++++++-----------------
 4 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/include/os.h b/include/os.h
index bb3348b185..394a3f90a7 100644
--- a/include/os.h
+++ b/include/os.h
@@ -302,8 +302,8 @@ OsRegisterSigWrapper(OsSigWrapperPtr newWrap);
 
 extern _X_EXPORT int auditTrailLevel;
 
-extern _X_EXPORT void
-LockServer(void);
+extern _X_EXPORT Bool
+LockServer(int);
 extern _X_EXPORT void
 UnlockServer(void);
 
diff --git a/os/connection.c b/os/connection.c
index 9e8d47f6b0..2b57e240bc 100644
--- a/os/connection.c
+++ b/os/connection.c
@@ -249,6 +249,13 @@ CreateWellKnownSockets(void)
         ListenTransCount = 0;
     }
     else if ((displayfd < 0) || explicit_display) {
+        if (!LockServer(atoi(display)))
+            FatalError
+                ("Server is already active for display %s\n"
+                 "\tIf this server is no longer running, remove the lockfile\n"
+                 "\tand start again.\n",
+                 display);
+
         if (TryCreateSocket(atoi(display), &partial) &&
             ListenTransCount >= 1)
             if (!PartialNetwork && partial)
@@ -257,12 +264,18 @@ CreateWellKnownSockets(void)
     else { /* -displayfd and no explicit display number */
         Bool found = 0;
         for (i = 0; i < 65536 - X_TCP_PORT; i++) {
+            DebugF("Trying to take lock for display number %d\n", i);
+            if (!LockServer(i))
+                continue;
+
+            DebugF("Trying to create socket for display number %d\n", i);
             if (TryCreateSocket(i, &partial) && !partial) {
                 found = 1;
                 break;
-            }
-            else
+            } else {
                 CloseWellKnownConnections();
+                UnlockServer();
+            }
         }
         if (!found)
             FatalError("Failed to find a socket to listen on");
diff --git a/os/osinit.c b/os/osinit.c
index 8575319fff..365e1f0237 100644
--- a/os/osinit.c
+++ b/os/osinit.c
@@ -310,7 +310,6 @@ OsInit(void)
             }
         }
 #endif
-        LockServer();
         been_here = TRUE;
     }
     TimerInit();
diff --git a/os/utils.c b/os/utils.c
index c9a8e73672..0b674d1467 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -242,9 +242,11 @@ OsSignal(int sig, OsSigHandlerPtr handler)
 #endif
 
 #ifndef LOCK_SERVER
-void
-LockServer(void)
-{}
+Bool
+LockServer(int num)
+{
+    return TRUE;
+}
 
 void
 UnlockServer(void)
@@ -257,11 +259,12 @@ static Bool nolock = FALSE;
 /*
  * LockServer --
  *      Check if the server lock file exists.  If so, check if the PID
- *      contained inside is valid.  If so, then die.  Otherwise, create
- *      the lock file containing the PID.
+ *      contained inside is valid.  Otherwise, create the lock file containing
+ *      the PID.  Return a bool indicating if lock was taken.  Die if problems
+ *      occur manipulating the lock file.
  */
-void
-LockServer(void)
+Bool
+LockServer(int num)
 {
     char tmp[PATH_MAX], pid_str[12];
     int lfd, i, haslock, l_pid, t;
@@ -270,11 +273,11 @@ LockServer(void)
     char port[20];
 
     if (nolock || NoListenAll)
-        return;
+        return TRUE;
     /*
      * Path names
      */
-    snprintf(port, sizeof(port), "%d", atoi(display));
+    snprintf(port, sizeof(port), "%d", num);
     len = strlen(LOCK_PREFIX) > strlen(LOCK_TMP_PREFIX) ? strlen(LOCK_PREFIX) :
         strlen(LOCK_TMP_PREFIX);
     len += strlen(tmppath) + strlen(port) + strlen(LOCK_SUFFIX) + 1;
@@ -370,10 +373,7 @@ LockServer(void)
                  * Process is still active.
                  */
                 unlink(tmp);
-                FatalError
-                    ("Server is already active for display %s\n%s %s\n%s\n",
-                     port, "\tIf this server is no longer running, remove",
-                     LockFile, "\tand start again.");
+                return FALSE;
             }
         }
         else {
@@ -387,6 +387,7 @@ LockServer(void)
     if (!haslock)
         FatalError("Could not create server lock file: %s\n", LockFile);
     StillLocking = FALSE;
+    return TRUE;
 }
 
 /*
@@ -400,7 +401,6 @@ UnlockServer(void)
         return;
 
     if (!StillLocking) {
-
         (void) unlink(LockFile);
     }
 }
@@ -762,9 +762,6 @@ ProcessCommandLine(int argc, char *argv[])
         else if (strcmp(argv[i], "-displayfd") == 0) {
             if (++i < argc) {
                 displayfd = atoi(argv[i]);
-#ifdef LOCK_SERVER
-                nolock = TRUE;
-#endif
             }
             else
                 UseMsg();
