--- startx.org 2006-07-23 01:13:43.000000000 -0400 +++ startx 2006-07-25 12:40:08.000000000 -0400 @@ -14,7 +14,75 @@ eval `/bin/getltscfg -a` DEFAULT_SERVER=${SERVER:-"192.168.0.254"} + +# ######################################################## +# deterministic app server load balancing added by moquist + +# Grab everything after "-query" until the next hyphen or end-of-line +XDM_OVERRIDE=`echo $* | sed 's/.*-query\([^-]*\).*$/\1/g'` + +# Get -query out of the startx line; we'll supply it ourselves +ARGS=`echo $* | sed 's/.*-query\([^-]*\).*$//g'` + +pick_xdm_server() { + # If this is empty we'll detect that below and print a message. + XDM_SERVERS=$* + + # The character range in the sed command here intentionally has a real tab + # character in it because busybox sed doesn't understand the \t + # metacharacter. + XDM_SERVERS=`echo ${XDM_SERVERS} | sed 's/[ ,]\+/ /g'` + XDM_DONE=0 + while [ "${XDM_DONE}" -ne 1 ]; do + XDM_NUM_SERVERS=`echo "${XDM_SERVERS}" | wc -w | sed 's/ //g'` + + if [ "${XDM_NUM_SERVERS}" -eq 0 ]; then + echo "No XDM server found; rebooting in 1 minute." 1>&2 + sleep 60 + reboot + fi + + if [ "${XDM_RANDOM}" = "true" ]; then + XDM_SERVER_IDX=$((${RANDOM}%${XDM_NUM_SERVERS})) + else + XDM_SERVER_IDX=$((1`ifconfig | grep HWaddr | head -n1 | sed 's/[^0-9]//g' | sed 's/^0*//'`%${XDM_NUM_SERVERS})) + XDM_SERVER_IDX=$((${XDM_SERVER_IDX}%${XDM_NUM_SERVERS})) + fi + + # Increment by one so we don't end up with an index of 0 + XDM_SERVER_IDX=$((${XDM_SERVER_IDX}+1)) + XDM_SERVER=`echo ${XDM_SERVERS} | cut -d\ -f ${XDM_SERVER_IDX}` + + if [ "${XDM_PING}" != "false" ]; then + # Ping the app server to be sure it's up. + echo "Pinging $XDM_SERVER..." 1>&2 + ping -c 1 "${XDM_SERVER}" >/dev/null 2>&1 + if [ "$?" != "0" ]; then + # Drop the bad server out of our list of potential servers + XDM_SERVERS=`echo ${XDM_SERVERS} | sed 's/'${XDM_SERVER}' *//'` + continue + fi + fi + XDM_DONE=1 + done + echo Selected XDM_SERVER: ${XDM_SERVER} 1>&2 + echo ${XDM_SERVER} +} + +# Get rid of spaces and misc stuff at the beginning and end so when we check +# below to see if $XDM_OVERRIDE has length>0, we aren't counting whitespace or +# commas. +XDM_OVERRIDE=`echo ${XDM_OVERRIDE} | sed 's/^[ ,]*//'` +XDM_OVERRIDE=`echo ${XDM_OVERRIDE} | sed 's/[ ,]*$//'` + +if [ -n "${XDM_OVERRIDE}" ]; then + XDM_SERVER=${XDM_OVERRIDE} +fi + +XDM_SERVER=`pick_xdm_server ${XDM_SERVER}` XDM_SERVER=${XDM_SERVER:-${DEFAULT_SERVER}} +# ######################################################## + reg_info XDM_SERVER TTY=`/usr/bin/basename \`/usr/bin/tty\` | sed s/tty//` @@ -24,7 +92,7 @@ if [ $# -lt 1 ]; then XF_ARGS="" else - XF_ARGS=$* + XF_ARGS=${ARGS} fi ################################################################################