Peter Hutterer 77cc64
From 47c3d565596ad3d5c837fb6909a40f7a251c248d Mon Sep 17 00:00:00 2001
bce379
From: Adam Jackson <ajax@redhat.com>
520bba
Date: Thu, 18 Jun 2009 16:56:41 +1000
Peter Hutterer 77cc64
Subject: [PATCH] displayfd hack
bce379
bce379
---
bce379
 dix/globals.c    |    1 +
bce379
 include/opaque.h |    1 +
Peter Hutterer 77cc64
 os/connection.c  |   73 +++++++++++++++++++++++++++++++++++++-----------------
bce379
 os/utils.c       |   11 ++++++++
Peter Hutterer 77cc64
 4 files changed, 63 insertions(+), 23 deletions(-)
bce379
bce379
diff --git a/dix/globals.c b/dix/globals.c
Peter Hutterer 77cc64
index 0a6b170..0ee83c0 100644
bce379
--- a/dix/globals.c
bce379
+++ b/dix/globals.c
Peter Hutterer 77cc64
@@ -126,6 +126,7 @@ int defaultColorVisualClass = -1;
520bba
 int monitorResolution = 0;
bce379
 
520bba
 char *display;
bce379
+int displayfd;
bce379
 char *ConnectionInfo;
bce379
 
bce379
 CARD32 TimeOutValue = DEFAULT_TIMEOUT * MILLI_PER_SECOND;
bce379
diff --git a/include/opaque.h b/include/opaque.h
Peter Hutterer 77cc64
index 5c70717..f23203f 100644
bce379
--- a/include/opaque.h
bce379
+++ b/include/opaque.h
520bba
@@ -50,6 +50,7 @@ extern _X_EXPORT int ScreenSaverAllowExposures;
520bba
 extern _X_EXPORT int defaultScreenSaverBlanking;
520bba
 extern _X_EXPORT int defaultScreenSaverAllowExposures;
520bba
 extern _X_EXPORT char *display;
520bba
+extern _X_EXPORT int displayfd;
bce379
 
520bba
 extern _X_EXPORT int defaultBackingStore;
520bba
 extern _X_EXPORT Bool disableBackingStore;
bce379
diff --git a/os/connection.c b/os/connection.c
Peter Hutterer 77cc64
index 5452ae1..9c20ef1 100644
bce379
--- a/os/connection.c
bce379
+++ b/os/connection.c
Peter Hutterer 77cc64
@@ -145,6 +145,7 @@ Bool AnyClientsWriteBlocked;	/* true if some client blocked on write */
bce379
 static Bool RunFromSmartParent;	/* send SIGUSR1 to parent process */
Peter Hutterer 77cc64
 Bool RunFromSigStopParent;	/* send SIGSTOP to our own process; Upstart (or
Peter Hutterer 77cc64
 				   equivalent) will send SIGCONT back. */
bce379
+static char dynamic_display[7];
520bba
 Bool PartialNetwork;	/* continue even if unable to bind all addrs */
bce379
 static Pid_t ParentProcess;
bce379
 
Peter Hutterer 77cc64
@@ -356,11 +357,26 @@ NotifyParentProcess(void)
bce379
 	    kill (ParentProcess, SIGUSR1);
bce379
 	}
bce379
     }
bce379
+    if (dynamic_display[0])
bce379
+	write(displayfd, dynamic_display, strlen(dynamic_display));
Peter Hutterer 77cc64
+
Peter Hutterer 77cc64
     if (RunFromSigStopParent)
Peter Hutterer 77cc64
 	raise (SIGSTOP);
bce379
 #endif
bce379
 }
bce379
 
bce379
+static Bool
bce379
+TryCreateSocket(int num, int *partial)
bce379
+{
bce379
+    char port[20];
bce379
+
bce379
+    sprintf(port, "%d", num);
bce379
+
bce379
+    return _XSERVTransMakeAllCOTSServerListeners(port, partial,
bce379
+						 &ListenTransCount,
bce379
+						 &ListenTransConns);
bce379
+}
bce379
+
bce379
 /*****************
bce379
  * CreateWellKnownSockets
bce379
  *    At initialization, create the sockets to listen on for new clients.
Peter Hutterer 77cc64
@@ -371,7 +387,6 @@ CreateWellKnownSockets(void)
bce379
 {
bce379
     int		i;
bce379
     int		partial;
bce379
-    char 	port[20];
bce379
 
bce379
     FD_ZERO(&AllSockets);
bce379
     FD_ZERO(&AllClients);
Peter Hutterer 77cc64
@@ -386,32 +401,44 @@ CreateWellKnownSockets(void)
bce379
 
bce379
     FD_ZERO (&WellKnownConnections);
bce379
 
bce379
-    sprintf (port, "%d", atoi (display));
bce379
-
bce379
-    if ((_XSERVTransMakeAllCOTSServerListeners (port, &partial,
bce379
-	&ListenTransCount, &ListenTransConns) >= 0) &&
bce379
-	(ListenTransCount >= 1))
bce379
+    if (display)
bce379
     {
bce379
-	if (!PartialNetwork && partial)
bce379
-	{
bce379
-	    FatalError ("Failed to establish all listening sockets");
bce379
-	}
bce379
-	else
bce379
+	if (TryCreateSocket(atoi(display), &partial) &&
bce379
+	    (ListenTransCount >= 1))
bce379
+	    if (!PartialNetwork && partial)
bce379
+		FatalError ("Failed to establish all listening sockets");
bce379
+    }
bce379
+    else /* -displayfd */
bce379
+    {
bce379
+	Bool found = 0;
bce379
+	for (i = 0; i < 65535 - 1024; i++)
bce379
 	{
c22de5
-	    ListenTransFds = malloc(ListenTransCount * sizeof (int));
bce379
-
bce379
-	    for (i = 0; i < ListenTransCount; i++)
bce379
+	    if (!TryCreateSocket(i, &partial) && !partial)
bce379
 	    {
bce379
-		int fd = _XSERVTransGetConnectionNumber (ListenTransConns[i]);
bce379
-		
bce379
-		ListenTransFds[i] = fd;
bce379
-		FD_SET (fd, &WellKnownConnections);
bce379
-
bce379
-		if (!_XSERVTransIsLocal (ListenTransConns[i]))
bce379
-		{
bce379
-		    DefineSelf (fd);
bce379
-		}
bce379
+		found = 1;
bce379
+		break;
bce379
 	    }
bce379
+	    else
bce379
+		CloseWellKnownConnections();
bce379
+	}
bce379
+	if (!found)
bce379
+	    FatalError("Failed to find a socket to listen on");
bce379
+	sprintf(dynamic_display, "%d\n", i);
bce379
+	display = dynamic_display;
bce379
+    }
bce379
+
bce379
+    ListenTransFds = xalloc (ListenTransCount * sizeof (int));
bce379
+
bce379
+    for (i = 0; i < ListenTransCount; i++)
bce379
+    {
bce379
+	int fd = _XSERVTransGetConnectionNumber (ListenTransConns[i]);
520bba
+
bce379
+	ListenTransFds[i] = fd;
bce379
+	FD_SET (fd, &WellKnownConnections);
bce379
+
bce379
+	if (!_XSERVTransIsLocal (ListenTransConns[i]))
bce379
+	{
bce379
+	    DefineSelf (fd);
bce379
 	}
bce379
     }
bce379
 
bce379
diff --git a/os/utils.c b/os/utils.c
Peter Hutterer 77cc64
index afdff0c..6866740 100644
bce379
--- a/os/utils.c
bce379
+++ b/os/utils.c
Peter Hutterer 77cc64
@@ -657,6 +657,17 @@ ProcessCommandLine(int argc, char *argv[])
bce379
 	    else
bce379
 		UseMsg();
bce379
 	}
bce379
+	else if (strcmp(argv[i], "-displayfd") == 0)
bce379
+	{
bce379
+	    if (++i < argc)
bce379
+	    {
bce379
+		displayfd = atoi(argv[i]);
bce379
+		display = NULL;
bce379
+		nolock = TRUE;
bce379
+	    }
bce379
+	    else
bce379
+		UseMsg();
bce379
+	}
bce379
 #ifdef DPMSExtension
bce379
 	else if ( strcmp( argv[i], "dpms") == 0)
520bba
 	    /* ignored for compatibility */ ;
bce379
-- 
Peter Hutterer 77cc64
1.7.3.2
bce379