Peter Hutterer 77cc640
From 47c3d565596ad3d5c837fb6909a40f7a251c248d Mon Sep 17 00:00:00 2001
bce3795
From: Adam Jackson <ajax@redhat.com>
520bba9
Date: Thu, 18 Jun 2009 16:56:41 +1000
Peter Hutterer 77cc640
Subject: [PATCH] displayfd hack
bce3795
bce3795
---
bce3795
 dix/globals.c    |    1 +
bce3795
 include/opaque.h |    1 +
Peter Hutterer 77cc640
 os/connection.c  |   73 +++++++++++++++++++++++++++++++++++++-----------------
bce3795
 os/utils.c       |   11 ++++++++
Peter Hutterer 77cc640
 4 files changed, 63 insertions(+), 23 deletions(-)
bce3795
bce3795
diff --git a/dix/globals.c b/dix/globals.c
Peter Hutterer 77cc640
index 0a6b170..0ee83c0 100644
bce3795
--- a/dix/globals.c
bce3795
+++ b/dix/globals.c
Peter Hutterer 77cc640
@@ -126,6 +126,7 @@ int defaultColorVisualClass = -1;
520bba9
 int monitorResolution = 0;
bce3795
 
520bba9
 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
Peter Hutterer 77cc640
index 5c70717..f23203f 100644
bce3795
--- a/include/opaque.h
bce3795
+++ b/include/opaque.h
520bba9
@@ -50,6 +50,7 @@ extern _X_EXPORT int ScreenSaverAllowExposures;
520bba9
 extern _X_EXPORT int defaultScreenSaverBlanking;
520bba9
 extern _X_EXPORT int defaultScreenSaverAllowExposures;
520bba9
 extern _X_EXPORT char *display;
520bba9
+extern _X_EXPORT int displayfd;
bce3795
 
520bba9
 extern _X_EXPORT int defaultBackingStore;
520bba9
 extern _X_EXPORT Bool disableBackingStore;
bce3795
diff --git a/os/connection.c b/os/connection.c
Peter Hutterer 77cc640
index 5452ae1..9c20ef1 100644
bce3795
--- a/os/connection.c
bce3795
+++ b/os/connection.c
Peter Hutterer 77cc640
@@ -145,6 +145,7 @@ Bool AnyClientsWriteBlocked;	/* true if some client blocked on write */
bce3795
 static Bool RunFromSmartParent;	/* send SIGUSR1 to parent process */
Peter Hutterer 77cc640
 Bool RunFromSigStopParent;	/* send SIGSTOP to our own process; Upstart (or
Peter Hutterer 77cc640
 				   equivalent) will send SIGCONT back. */
bce3795
+static char dynamic_display[7];
520bba9
 Bool PartialNetwork;	/* continue even if unable to bind all addrs */
bce3795
 static Pid_t ParentProcess;
bce3795
 
Peter Hutterer 77cc640
@@ -356,11 +357,26 @@ NotifyParentProcess(void)
bce3795
 	    kill (ParentProcess, SIGUSR1);
bce3795
 	}
bce3795
     }
bce3795
+    if (dynamic_display[0])
bce3795
+	write(displayfd, dynamic_display, strlen(dynamic_display));
Peter Hutterer 77cc640
+
Peter Hutterer 77cc640
     if (RunFromSigStopParent)
Peter Hutterer 77cc640
 	raise (SIGSTOP);
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.
Peter Hutterer 77cc640
@@ -371,7 +387,6 @@ CreateWellKnownSockets(void)
bce3795
 {
bce3795
     int		i;
bce3795
     int		partial;
bce3795
-    char 	port[20];
bce3795
 
bce3795
     FD_ZERO(&AllSockets);
bce3795
     FD_ZERO(&AllClients);
Peter Hutterer 77cc640
@@ -386,32 +401,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
 	{
c22de5d
-	    ListenTransFds = malloc(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]);
520bba9
+
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
Peter Hutterer 77cc640
index afdff0c..6866740 100644
bce3795
--- a/os/utils.c
bce3795
+++ b/os/utils.c
Peter Hutterer 77cc640
@@ -657,6 +657,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)
520bba9
 	    /* ignored for compatibility */ ;
bce3795
-- 
Peter Hutterer 77cc640
1.7.3.2
bce3795