[PATCH 1/3] os: Add -displayfd option.

Julien Cristau jcristau at debian.org
Thu Jan 6 15:02:17 PST 2011


On Thu, Jan  6, 2011 at 17:28:59 -0500, Adam Jackson wrote:

> diff --git a/os/connection.c b/os/connection.c
> index 5580fab..8fb8c36 100644
> --- a/os/connection.c
> +++ b/os/connection.c
> @@ -145,6 +145,7 @@ Bool AnyClientsWriteBlocked;	/* true if some client blocked on write */
>  static Bool RunFromSmartParent;	/* send SIGUSR1 to parent process */
>  Bool RunFromSigStopParent;	/* send SIGSTOP to our own process; Upstart (or
>  				   equivalent) will send SIGCONT back. */
> +static char dynamic_display[7];
>  Bool PartialNetwork;	/* continue even if unable to bind all addrs */
>  static Pid_t ParentProcess;
>  
> @@ -356,11 +357,25 @@ NotifyParentProcess(void)
>  	    kill (ParentProcess, SIGUSR1);
>  	}
>      }
> +    if (dynamic_display[0])
> +	write(displayfd, dynamic_display, strlen(dynamic_display));
>      if (RunFromSigStopParent)
>  	raise (SIGSTOP);
>  #endif
>  }
>  
> +static Bool
> +TryCreateSocket(int num, int *partial)
> +{
> +    char port[20];
> +
> +    sprintf(port, "%d", num);
> +
> +    return _XSERVTransMakeAllCOTSServerListeners(port, partial,
> +						 &ListenTransCount,
> +						 &ListenTransConns);

Cast from int to Bool, _XSERVTransMakeAllCOTSServerListeners seems to
return 0 on success, I guess this should return
_XSERVTransMakeAllCOTSServerListeners() >= 0?

> +}
> +
>  /*****************
>   * CreateWellKnownSockets
>   *    At initialization, create the sockets to listen on for new clients.
> @@ -371,7 +386,6 @@ CreateWellKnownSockets(void)
>  {
>      int		i;
>      int		partial;
> -    char 	port[20];
>  
>      FD_ZERO(&AllSockets);
>      FD_ZERO(&AllClients);
> @@ -386,32 +400,44 @@ CreateWellKnownSockets(void)
>  
>      FD_ZERO (&WellKnownConnections);
>  
> -    sprintf (port, "%d", atoi (display));
> -
> -    if ((_XSERVTransMakeAllCOTSServerListeners (port, &partial,
> -	&ListenTransCount, &ListenTransConns) >= 0) &&
> -	(ListenTransCount >= 1))
> +    if (display)
>      {
> -	if (!PartialNetwork && partial)
> -	{
> -	    FatalError ("Failed to establish all listening sockets");
> -	}
> -	else
> +	if (TryCreateSocket(atoi(display), &partial) &&
> +	    (ListenTransCount >= 1))
> +	    if (!PartialNetwork && partial)
> +		FatalError ("Failed to establish all listening sockets");
> +    }
> +    else /* -displayfd */
> +    {
> +	Bool found = 0;
> +	for (i = 0; i < 65535 - 1024; i++)

Should this be 65535 - 6000?

>  	{
> -	    ListenTransFds = malloc(ListenTransCount * sizeof (int));
> -
> -	    for (i = 0; i < ListenTransCount; i++)
> +	    if (!TryCreateSocket(i, &partial) && !partial)
>  	    {
> -		int fd = _XSERVTransGetConnectionNumber (ListenTransConns[i]);
> -		
> -		ListenTransFds[i] = fd;
> -		FD_SET (fd, &WellKnownConnections);
> -
> -		if (!_XSERVTransIsLocal (ListenTransConns[i]))
> -		{
> -		    DefineSelf (fd);
> -		}
> +		found = 1;
> +		break;
>  	    }
> +	    else
> +		CloseWellKnownConnections();
> +	}
> +	if (!found)
> +	    FatalError("Failed to find a socket to listen on");
> +	sprintf(dynamic_display, "%d\n", i);
> +	display = dynamic_display;
> +    }
> +
> +    ListenTransFds = malloc (ListenTransCount * sizeof (int));
> +
> +    for (i = 0; i < ListenTransCount; i++)
> +    {
> +	int fd = _XSERVTransGetConnectionNumber (ListenTransConns[i]);
> +
> +	ListenTransFds[i] = fd;
> +	FD_SET (fd, &WellKnownConnections);
> +
> +	if (!_XSERVTransIsLocal (ListenTransConns[i]))
> +	{
> +	    DefineSelf (fd);
>  	}
>      }
>  
Cheers,
Julien


More information about the xorg-devel mailing list