bce3795
From ac43dac0a18778081ea784c98391f0333981ca13 Mon Sep 17 00:00:00 2001
bce3795
From: Adam Jackson <ajax@redhat.com>
bce3795
Date: Mon, 6 Apr 2009 15:44:05 -0400
bce3795
Subject: [PATCH] displayfd hack
bce3795
bce3795
---
bce3795
 dix/globals.c    |    1 +
bce3795
 include/opaque.h |    1 +
bce3795
 os/connection.c  |   72 ++++++++++++++++++++++++++++++++++++-----------------
bce3795
 os/utils.c       |   11 ++++++++
bce3795
 4 files changed, 62 insertions(+), 23 deletions(-)
bce3795
bce3795
diff --git a/dix/globals.c b/dix/globals.c
bce3795
index dbd76bb..749bf9b 100644
bce3795
--- a/dix/globals.c
bce3795
+++ b/dix/globals.c
bce3795
@@ -152,6 +152,7 @@ _X_EXPORT int defaultColorVisualClass = -1;
bce3795
 _X_EXPORT int monitorResolution = 0;
bce3795
 
bce3795
 _X_EXPORT char *display;
bce3795
+int displayfd;
bce3795
 char *ConnectionInfo;
bce3795
 
bce3795
 CARD32 TimeOutValue = DEFAULT_TIMEOUT * MILLI_PER_SECOND;
bce3795
diff --git a/include/opaque.h b/include/opaque.h
bce3795
index be1577b..2ae6680 100644
bce3795
--- a/include/opaque.h
bce3795
+++ b/include/opaque.h
bce3795
@@ -50,6 +50,7 @@ extern int ScreenSaverAllowExposures;
bce3795
 extern int defaultScreenSaverBlanking;
bce3795
 extern int defaultScreenSaverAllowExposures;
bce3795
 extern char *display;
bce3795
+extern int displayfd;
bce3795
 
bce3795
 extern int defaultBackingStore;
bce3795
 extern Bool disableBackingStore;
bce3795
diff --git a/os/connection.c b/os/connection.c
bce3795
index 8f1f73e..4351117 100644
bce3795
--- a/os/connection.c
bce3795
+++ b/os/connection.c
bce3795
@@ -146,6 +146,7 @@ Bool NewOutputPending;		/* not yet attempted to write some new output */
bce3795
 Bool AnyClientsWriteBlocked;	/* true if some client blocked on write */
bce3795
 
bce3795
 static Bool RunFromSmartParent;	/* send SIGUSR1 to parent process */
bce3795
+static char dynamic_display[7];
bce3795
 Bool PartialNetwork;		/* continue even if unable to bind all addrs */
bce3795
 static Pid_t ParentProcess;
bce3795
 
bce3795
@@ -367,9 +368,23 @@ NotifyParentProcess(void)
bce3795
 	    kill (ParentProcess, SIGUSR1);
bce3795
 	}
bce3795
     }
bce3795
+    if (dynamic_display[0])
bce3795
+	write(displayfd, dynamic_display, strlen(dynamic_display));
bce3795
 #endif
bce3795
 }
bce3795
 
bce3795
+static Bool
bce3795
+TryCreateSocket(int num, int *partial)
bce3795
+{
bce3795
+    char port[20];
bce3795
+
bce3795
+    sprintf(port, "%d", num);
bce3795
+
bce3795
+    return _XSERVTransMakeAllCOTSServerListeners(port, partial,
bce3795
+						 &ListenTransCount,
bce3795
+						 &ListenTransConns);
bce3795
+}
bce3795
+
bce3795
 /*****************
bce3795
  * CreateWellKnownSockets
bce3795
  *    At initialization, create the sockets to listen on for new clients.
bce3795
@@ -380,7 +395,6 @@ CreateWellKnownSockets(void)
bce3795
 {
bce3795
     int		i;
bce3795
     int		partial;
bce3795
-    char 	port[20];
bce3795
 
bce3795
     FD_ZERO(&AllSockets);
bce3795
     FD_ZERO(&AllClients);
bce3795
@@ -395,32 +409,44 @@ CreateWellKnownSockets(void)
bce3795
 
bce3795
     FD_ZERO (&WellKnownConnections);
bce3795
 
bce3795
-    sprintf (port, "%d", atoi (display));
bce3795
-
bce3795
-    if ((_XSERVTransMakeAllCOTSServerListeners (port, &partial,
bce3795
-	&ListenTransCount, &ListenTransConns) >= 0) &&
bce3795
-	(ListenTransCount >= 1))
bce3795
+    if (display)
bce3795
     {
bce3795
-	if (!PartialNetwork && partial)
bce3795
-	{
bce3795
-	    FatalError ("Failed to establish all listening sockets");
bce3795
-	}
bce3795
-	else
bce3795
+	if (TryCreateSocket(atoi(display), &partial) &&
bce3795
+	    (ListenTransCount >= 1))
bce3795
+	    if (!PartialNetwork && partial)
bce3795
+		FatalError ("Failed to establish all listening sockets");
bce3795
+    }
bce3795
+    else /* -displayfd */
bce3795
+    {
bce3795
+	Bool found = 0;
bce3795
+	for (i = 0; i < 65535 - 1024; i++)
bce3795
 	{
bce3795
-	    ListenTransFds = (int *) xalloc (ListenTransCount * sizeof (int));
bce3795
-
bce3795
-	    for (i = 0; i < ListenTransCount; i++)
bce3795
+	    if (!TryCreateSocket(i, &partial) && !partial)
bce3795
 	    {
bce3795
-		int fd = _XSERVTransGetConnectionNumber (ListenTransConns[i]);
bce3795
-		
bce3795
-		ListenTransFds[i] = fd;
bce3795
-		FD_SET (fd, &WellKnownConnections);
bce3795
-
bce3795
-		if (!_XSERVTransIsLocal (ListenTransConns[i]))
bce3795
-		{
bce3795
-		    DefineSelf (fd);
bce3795
-		}
bce3795
+		found = 1;
bce3795
+		break;
bce3795
 	    }
bce3795
+	    else
bce3795
+		CloseWellKnownConnections();
bce3795
+	}
bce3795
+	if (!found)
bce3795
+	    FatalError("Failed to find a socket to listen on");
bce3795
+	sprintf(dynamic_display, "%d\n", i);
bce3795
+	display = dynamic_display;
bce3795
+    }
bce3795
+
bce3795
+    ListenTransFds = xalloc (ListenTransCount * sizeof (int));
bce3795
+
bce3795
+    for (i = 0; i < ListenTransCount; i++)
bce3795
+    {
bce3795
+	int fd = _XSERVTransGetConnectionNumber (ListenTransConns[i]);
bce3795
+	
bce3795
+	ListenTransFds[i] = fd;
bce3795
+	FD_SET (fd, &WellKnownConnections);
bce3795
+
bce3795
+	if (!_XSERVTransIsLocal (ListenTransConns[i]))
bce3795
+	{
bce3795
+	    DefineSelf (fd);
bce3795
 	}
bce3795
     }
bce3795
 
bce3795
diff --git a/os/utils.c b/os/utils.c
bce3795
index c41b45b..2372588 100644
bce3795
--- a/os/utils.c
bce3795
+++ b/os/utils.c
bce3795
@@ -674,6 +674,17 @@ ProcessCommandLine(int argc, char *argv[])
bce3795
 	    else
bce3795
 		UseMsg();
bce3795
 	}
bce3795
+	else if (strcmp(argv[i], "-displayfd") == 0)
bce3795
+	{
bce3795
+	    if (++i < argc)
bce3795
+	    {
bce3795
+		displayfd = atoi(argv[i]);
bce3795
+		display = NULL;
bce3795
+		nolock = TRUE;
bce3795
+	    }
bce3795
+	    else
bce3795
+		UseMsg();
bce3795
+	}
bce3795
 #ifdef DPMSExtension
bce3795
 	else if ( strcmp( argv[i], "dpms") == 0)
bce3795
 	    DPMSEnabledSwitch = TRUE;
bce3795
-- 
bce3795
1.6.2
bce3795