diff --git a/include/os.h b/include/os.h index b6ebebe7c..ae3a83d2a 100644 --- a/include/os.h +++ b/include/os.h @@ -273,8 +273,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 23bdb21a7..8f9ca3d6c 100644 --- a/os/connection.c +++ b/os/connection.c @@ -241,6 +241,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) @@ -249,12 +256,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 784c9ddc7..dcafe8b2a 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 ca18f0765..61d7c0971 100644 --- a/os/utils.c +++ b/os/utils.c @@ -237,9 +237,11 @@ OsSignal(int sig, OsSigHandlerPtr handler) #endif #ifndef LOCK_SERVER -void -LockServer(void) -{} +Bool +LockServer(int num) +{ + return TRUE; +} void UnlockServer(void) @@ -252,11 +254,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; @@ -265,11 +268,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; @@ -365,10 +368,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 { @@ -382,6 +382,7 @@ LockServer(void) if (!haslock) FatalError("Could not create server lock file: %s\n", LockFile); StillLocking = FALSE; + return TRUE; } /* @@ -395,7 +396,6 @@ UnlockServer(void) return; if (!StillLocking) { - (void) unlink(LockFile); } } @@ -764,9 +764,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();