Index: /trunk/src/VBox/Installer/linux/VBoxHeadlessXOrg.sh
===================================================================
--- /trunk/src/VBox/Installer/linux/VBoxHeadlessXOrg.sh	(revision 43703)
+++ /trunk/src/VBox/Installer/linux/VBoxHeadlessXOrg.sh	(revision 43704)
@@ -40,9 +40,6 @@
 
 ## Default configuration file name.
-# Don't use vbox.cfg as that is currently automatically created and deleted.
-# Don't use /etc/default/virtualbox as that is Debian policy only and not very
-# nice.  Let's try to use this in future and phase out the other two.
-## @todo Should we be using /etc/virtualbox instead of /etc/vbox?
-CONFIGURATION_FILE=/etc/vbox/vbox.conf
+# @note This is not very nice - /etc/default is actually Debian-specific.
+CONFIGURATION_FILE=/etc/default/virtualbox
 ## The name of this script.
 SCRIPT_NAME="$0"
@@ -90,8 +87,21 @@
 
   HEADLESS_X_ORG_LOG_FOLDER
-    The default log folder.
+    The folder where log files will be created.
 
   HEADLESS_X_ORG_LOG_FILE
-    The default log file.
+    The main log file name.
+
+  HEADLESS_X_ORG_RUN_FOLDER
+    The folder to store run-time data in.
+
+  HEADLESS_X_ORG_CHECK_PREREQUISITES
+    Shell command to execute to check whether all dependencies for the X
+    servers are available - usually a test for a device node.  This will be
+    repeated at regular intervals until it returns successfully, so a command
+    which can be executed internally be the shell (like "[") is preferable.
+
+  HEADLESS_X_ORG_SERVER_PRE_COMMAND
+    Command to execute once to perform any set-up needed before starting the
+    X servers, such as setting up the X server authentication.
 
   HEADLESS_X_ORG_SERVER_COMMAND
@@ -114,5 +124,9 @@
 HEADLESS_X_ORG_LOG_FOLDER="/var/log/${SERVICE_NAME}"
 HEADLESS_X_ORG_LOG_FILE="${SERVICE_NAME}.log"
-HEADLESS_X_ORG_SERVER_COMMAND="Xorg :\${screen} -config \"\${conf_file}\" -logverbose 0 -logfile /dev/null -verbose 7 > \"\${log_file}\" 2>&1"
+HEADLESS_X_ORG_RUN_FOLDER="/var/run/${SERVICE_NAME}"
+HEADLESS_X_ORG_CHECK_PREREQUISITES="[ -e /dev/dri/card0 ]"
+X_AUTH_FILE="${HEADLESS_X_ORG_RUN_FOLDER}/xauth"
+HEADLESS_X_ORG_SERVER_PRE_COMMAND="echo > \"${X_AUTH_FILE}\"; xauth -f \"${X_AUTH_FILE}\" add :0 . \"\$(dd if=/dev/urandom count=1 bs=16 2>/dev/null | od -An -x)\""
+HEADLESS_X_ORG_SERVER_COMMAND="Xorg :\${screen} -auth \"${HEADLESS_X_ORG_RUN_FOLDER}/xauth\" -config \"\${conf_file}\" -logverbose 0 -logfile /dev/null -verbose 7 > \"\${log_file}\" 2>&1"
 HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE="Xorg.\${screen}.log"
 
@@ -195,7 +209,23 @@
   abort "Failed to create log folder \"${HEADLESS_X_ORG_LOG_FOLDER}\".\n"
 }
+mkdir -p "${HEADLESS_X_ORG_RUN_FOLDER}" 2>/dev/null ||
+{
+  banner
+  abort "Failed to create run folder \"${HEADLESS_X_ORG_RUN_FOLDER}\".\n"
+}
 exec > "${HEADLESS_X_ORG_LOG_FOLDER}/${HEADLESS_X_ORG_LOG_FILE}" 2>&1
 
 banner
+
+# Wait for our dependencies to become available.  The increasing delay is
+# probably not the cleverest way to do this.
+DELAY=1
+while ! eval ${HEADLESS_X_ORG_CHECK_PREREQUISITES}; do
+  sleep $((${DELAY} / 10 + 1))
+  DELAY=$((${DELAY} + 1))
+done
+
+# Do any pre-start setup.
+eval "${HEADLESS_X_ORG_SERVER_PRE_COMMAND}"
 
 X_SERVER_PIDS=""
Index: /trunk/src/VBox/Installer/linux/testcase/tstHeadlessXOrg.sh
===================================================================
--- /trunk/src/VBox/Installer/linux/testcase/tstHeadlessXOrg.sh	(revision 43703)
+++ /trunk/src/VBox/Installer/linux/testcase/tstHeadlessXOrg.sh	(revision 43704)
@@ -14,6 +14,4 @@
 #
 
-[ x"$1" = x"--keep-temp" ] && KEEP_TEMP=true
-
 ## The function definition at the start of every non-trivial shell script!
 abort()
@@ -34,15 +32,13 @@
 }
 
-## Run a test in which VBoxHeadlessXOrg.sh is expected to complete within a
-# certain time and call a function if it does which should check whether the
-# test was successful and print status information.  The function takes the
-# exit status as its single parameter.
-run_expect_exit()
-{
-  CONF_FILE="$1"      ## The configuration file.
+## Expected a process to complete within a certain time and call a function if
+# it does which should check whether the test was successful and print status
+# information.  The function takes the exit status as its single parameter.
+expect_exit()
+{
+  PID="$1"            ## The PID we are waiting for.
   TIME_OUT="$2"       ## The time-out before we terminate the process.
   TEST_FUNCTION="$3"  ## The function to call on exit to check the test result.
-  ./VBoxHeadlessXOrg.sh -c "${CONF_FILE}" &
-  PID=$!
+
   # Give it time to complete.
   { sleep "${TIME_OUT}"; kill "${PID}" 2>/dev/null; } &
@@ -57,4 +53,23 @@
     ${TEST_FUNCTION} "${STATUS}"
 esac
+}
+
+## Create a simple configuration file.  Add items onto the end to override them
+# on an item-by-item basis.
+create_basic_configuration_file()
+{
+  FILE_NAME="$1"    ## The name of the configuration file to create.
+  BASE_FOLDER="$2"  ## The basic folder for creating things under.
+  cat > "${FILE_NAME}" << EOF
+HEADLESS_X_ORG_CONFIGURATION_FOLDER="${BASE_FOLDER}/xorg"
+HEADLESS_X_ORG_LOG_FOLDER="${BASE_FOLDER}/log"
+HEADLESS_X_ORG_LOG_FILE="log"
+HEADLESS_X_ORG_RUN_FOLDER="${BASE_FOLDER}/run"
+HEADLESS_X_ORG_CHECK_PREREQUISITES=
+HEADLESS_X_ORG_SERVER_PRE_COMMAND=
+HEADLESS_X_ORG_SERVER_COMMAND="echo \\\${screen} \\\${conf_file} \\\${log_file}"
+HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE="log.\\\${screen}"
+EOF
+
 }
 
@@ -76,23 +91,15 @@
 [ -d "${TEST_FOLDER}" ] || abort "Failed to create a temporary folder\n"
 # Clean up.  Small race here, but probably not important.
-[ -z "${KEEP_TEMP}" ] &&
-  trap "rm -r \"${TEST_FOLDER}\" 2>/dev/null" EXIT HUP INT QUIT ABRT TERM
+trap "rm -r \"${TEST_FOLDER}\" 2>/dev/null" EXIT HUP INT QUIT ABRT TERM
 # Server configuration folder.
 XORG_FOLDER="${TEST_FOLDER}/xorg"
-mkdir "${XORG_FOLDER}"
-
-# Set up our basic configuration file.
-cat > "${TEST_FOLDER}/conf" << EOF
-HEADLESS_X_ORG_CONFIGURATION_FOLDER="${XORG_FOLDER}"
-HEADLESS_X_ORG_LOG_FOLDER="${TEST_FOLDER}/log"
-HEADLESS_X_ORG_LOG_FILE="log"
-HEADLESS_X_ORG_SERVER_COMMAND="echo \\\${screen} \\\${conf_file} \\\${log_file}"
-HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE="log.\\\${screen}"
-EOF
+mkdir -p "${XORG_FOLDER}"
 
 # Simple start-up test.
 print_line "simple start-up test"
-touch "${XORG_FOLDER}/xorg.conf.2"
-touch "${XORG_FOLDER}/xorg.conf.4"
+create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
+touch "${XORG_FOLDER}/xorg.conf.2"
+touch "${XORG_FOLDER}/xorg.conf.4"
+
 test_simple_start_up()
 {
@@ -113,9 +120,13 @@
   esac
 }
-run_expect_exit "${TEST_FOLDER}/conf" 5 test_simple_start_up
+
+./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
+PID=$!
+expect_exit "${PID}" 5 test_simple_start_up
 rm "${XORG_FOLDER}"/xorg.conf.*
 
 # No configuration files.
 print_line "no configuration files"
+
 test_should_fail()
 {
@@ -129,5 +140,8 @@
   esac
 }
-run_expect_exit "${TEST_FOLDER}/conf" 5 test_should_fail
+
+./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
+PID=$!
+expect_exit "${PID}" 5 test_should_fail
 
 # Bad configuration files.
@@ -136,14 +150,13 @@
 touch "${XORG_FOLDER}/xorg.conf.4"
 touch "${XORG_FOLDER}/xorg.conf.other"
-run_expect_exit "${TEST_FOLDER}/conf" 5 test_should_fail
+./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
+PID=$!
+expect_exit "${PID}" 5 test_should_fail
 rm "${XORG_FOLDER}/"xorg.conf.*
 
 # Set up a configuration file for a long-running command.
-cat > "${TEST_FOLDER}/conf" << EOF
-HEADLESS_X_ORG_CONFIGURATION_FOLDER="${XORG_FOLDER}"
-HEADLESS_X_ORG_LOG_FOLDER="${TEST_FOLDER}/log"
-HEADLESS_X_ORG_LOG_FILE="log"
+create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
+cat >> "${TEST_FOLDER}/conf" << EOF
 HEADLESS_X_ORG_SERVER_COMMAND="echo $$ > ${TEST_FOLDER}/pid.\\\${screen}; cat"
-HEADLESS_X_ORG_SERVER_LOG_FILE_TEMPLATE="log.\\\${screen}"
 EOF
 
@@ -173,2 +186,60 @@
 rm "${XORG_FOLDER}/"xorg.conf.*
 rm -f "${TEST_FOLDER}/pid.1" "${TEST_FOLDER}/pid.5"
+
+# Set up a configuration file with a pre-requisite.
+create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
+cat >> "${TEST_FOLDER}/conf" << EOF
+HEADLESS_X_ORG_CHECK_PREREQUISITES="[ -e \\"${TEST_FOLDER}/run/prereq\\" ]"
+EOF
+
+# Pre-requisite test.
+print_line "configuration file with pre-requisite (sleeps)"
+touch "${XORG_FOLDER}/xorg.conf.2"
+touch "${XORG_FOLDER}/xorg.conf.4"
+FAILURE=""
+./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
+PID="$!"
+sleep 1
+ps -p "${PID}" > /dev/null 2>&1 || FAILURE="\nFAILED to wait for pre-requisite.\n"
+touch "${TEST_FOLDER}/run/prereq"
+if [ -z "${FAILURE}" ]; then
+  expect_exit "${PID}" 10 test_simple_start_up
+else
+  printf "${FAILURE}"
+fi
+rm -r "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}/run"
+
+# Set up our pre-command test configuration file.
+create_basic_configuration_file "${TEST_FOLDER}/conf" "${TEST_FOLDER}"
+cat >> "${TEST_FOLDER}/conf" << EOF
+HEADLESS_X_ORG_SERVER_PRE_COMMAND="touch \"${TEST_FOLDER}/run/pre\""
+EOF
+
+# Pre-command test.
+print_line "pre-command test"
+touch "${XORG_FOLDER}/xorg.conf.2"
+touch "${XORG_FOLDER}/xorg.conf.4"
+
+test_pre_command()
+{
+  STATUS="$1"
+  case "${STATUS}" in
+  0)
+    LOG_FOLDER="${TEST_FOLDER}/log"
+    LOG="${LOG_FOLDER}/log"
+    if [ -e "${TEST_FOLDER}/run/pre" ]; then
+      printf "SUCCESS.\n"
+    else
+      printf "\nFAILED: pre-command not executed.\n"
+    fi
+    ;;
+  *)
+    printf "\nFAILED: exit status ${STATUS}.\n"
+  esac
+}
+
+rm -f "${TEST_FOLDER}/run/pre"
+./VBoxHeadlessXOrg.sh -c "${TEST_FOLDER}/conf" &
+PID=$!
+expect_exit "${PID}" 5 test_pre_command
+rm -f "${XORG_FOLDER}"/xorg.conf.* "${TEST_FOLDER}/run/pre"
