Bug 1789 - startx doesn't automatically pick an unused display number
Summary: startx doesn't automatically pick an unused display number
Status: RESOLVED FIXED
Alias: None
Product: xorg
Classification: Unclassified
Component: App/xinit (show other bugs)
Version: unspecified
Hardware: x86 (IA32) Linux (All)
: low minor
Assignee: Daniel Stone
QA Contact: Xorg Project Team
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-11-06 06:38 UTC by logrus
Modified: 2011-10-17 03:55 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Attachments

Description logrus 2004-11-06 06:38:51 UTC
For those of us dinosaurs who do not use xdm style display
controllers and who and log in multiple times on a given computer
using multiple separate x sessions, the stock startx script 
needs to be explicitly told which VT to use.  
This should be automatic.

The following version of startx will do this, as well as closing
down the automatic X11 LISTEN behaviour (which is rarely needed anyway). 
It has been in daily use on ~15 machines over the past several years.


#################################
#!/bin/sh
#
# $Xorg: startx.cpp,v 1.3 2000/08/17 19:54:29 cpqbld Exp $
# 
# This is just a sample implementation of a slightly less primitive 
# interface than xinit.  It looks for user .xinitrc and .xserverrc
# files, then system xinitrc and xserverrc files, else lets xinit choose
# its default.  The system xinitrc should probably do things like check
# for .Xresources files and merge them in, startup up a window manager,
# and pop a clock and serveral xterms.
#
# Site administrators are STRONGLY urged to write nicer versions.
# 
# $XFree86: xc/programs/xinit/startx.cpp,v 3.7 2001/04/19 15:08:32 dawes Exp $
#
# PEK May 26, 2002: starting to follow the advice above...
#       - adding automatic display choice
#       - add nolisten option
#          If there is a $HOME/.xserverrc, the user is responsible
#          for the nolisten specifications
#          If there is a /etc/X11/xinit/xserverrc, the sysadmin
#          will be responsible for the  nolisten specifications
#       - remove xauth stuff: use ssh anyway!
#
#
bindir=/usr/X11R6/bin
#
userclientrc=$HOME/.xinitrc
sysclientrc=/etc/X11/xinit/xinitrc
defaultclientargs=""
clientargs=""
#
userserverrc=$HOME/.xserverrc
sysserverrc=/etc/X11/xinit/xserverrc
defaultserverargs="-nolisten tcp"
serverargs=""

if [ -f $userclientrc ]; then
   defaultclientargs=$userclientrc
   else if [ -f $sysclientrc ]; then
       defaultclientargs=$sysclientrc
   fi
fi

if [ -f $userserverrc ]; then
   defaultserverargs=$userserverrc
   else if [ -f $sysserverrc ]; then
      defaultserverargs=$sysserverrc
   fi
fi

#
# parse the arguments and put them in the right place
# pull out the display number specially...
#
whoseargs="client"
while [ "x$1" != "x" ]; do
    case "$1" in
    --)
        whoseargs="server"
        ;;
    *)
        if [ "$whoseargs" = "client" ]; then
            clientargs="$clientargs $1"
        else
            serverargs="$serverargs $1"
            case "$1" in
            :[0-9]*)
                display="$1"
                ;;
            esac
        fi
        ;;
    esac
    shift
done

if [ x"$clientargs" = x ]; then
    clientargs="$defaultclientargs"
fi

# if no serverrc file specified, then $defaultserverargs
# will be the "-nolisten tcp" option
# if there were user or system serverrc files, then the default
# picks up the file name as per the original startx file
# so append $defaultserverargs
serverargs="$serverargs $defaultserverargs"
    
if [ X"$XAUTHORITY" = X ]; then
    export XAUTHORITY=$HOME/.Xauthority
fi

#
# Now search in $display to see if there is a display
# specified: If the user has given us one then check it.
# else check for the first free display.  see /tmp/.X$n-lock file
#
if [ "x$display" = x ]; then # no display given
   for num in {0,1,2,3,4,5,6,7,8,9}; do
      if [ ! -f /tmp/.X$num-lock ]; then
         display=:$num
         echo " Found display $display available and will use it"
         serverargs="$display $serverargs"
         break
      fi
   done
   if [ "x$display" = x ]; then
      echo "Unable to get a free display, help!"
      echo "Please have someone check the status of the"
      echo "/tmp/.Xn-lock files to find what is wrong"
      echo "I see lock files:"
      /bin/ls -AlF /tmp/.X*-lock
      exit 10
   fi
else                            # special display demanded
   echo "Checking if $display is a good display..."
   num=`echo $display | sed s/://`
   if [ -f /tmp/.X$num-lock ]; then
      echo "User provided display $display is in use!"
      echo "Try another number, or let me pick one automatically"
      echo "(i.e., don't specify one on the command line)."
      exit 11
   fi
   echo " O.K., display $display is unused. I'll use it!"
fi                              # display if

# set up default Xauth info for this machine
mcookie=`mcookie`
for displayname in $display `hostname -f`$display; do
    if ! xauth list "$displayname" | grep "$displayname " >/dev/null 2>&1; then
        xauth add $displayname . $mcookie
        removelist="$displayname $removelist"
    fi
done

xinit $clientargs -- $serverargs

if command -v deallocvt > /dev/null 2>&1; then
    deallocvt
fi
Comment 1 Samuel Thibault 2005-09-05 13:43:44 UTC
The problem here is not choosing a VT (which the X server already does correctly), but choosing a display number.
Comment 2 Daniel Stone 2007-02-27 01:24:35 UTC
Sorry about the phenomenal bug spam, guys.  Adding xorg-team@ to the QA contact so bugs don't get lost in future.
Comment 3 Daniel Stone 2007-04-07 15:15:44 UTC
and the most egregious bashism of the year award goes to ...
'for num in {0,1,2,3,4,5,6,7,8,9}; do'
;)

this looks okay to me.  will commit if there are no objections.
Comment 4 Jeremy Huddleston Sequoia 2011-10-17 03:55:54 UTC
I had something similar inside an ifdef __APPLE__ which is now available for 
all to use

commit 27be391123f5143fdccdfe975bf18bbff7517537
Author: Jeremy Huddleston <jeremyhu@apple.com>
Date:   Mon Oct 17 03:53:17 2011 -0700

    startx: Choose an unused $DISPLAY by default on all platforms
    
    Now everyone can benefit from this code that I previously added for darwin
    
    https://bugs.freedesktop.org/show_bug.cgi?id=1789
    
    Signed-off-by: Jeremy Huddleston <jeremyhu@apple.com>


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.