Don't imply '-nolock' with '-displayfd'

Take server lockfile before trying to create socket for -displayfd.
This commit is contained in:
Jon Turney 2022-01-11 17:13:27 +00:00
parent 9c35efe978
commit b24308cd28
4 changed files with 31 additions and 22 deletions

View file

@ -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);

View file

@ -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");

View file

@ -310,7 +310,6 @@ OsInit(void)
}
}
#endif
LockServer();
been_here = TRUE;
}
TimerInit();

View file

@ -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);
}
}
@ -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();