Guest User

Untitled

a guest
Feb 15th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.91 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2004, Valve LLC. All rights reserved.
  4. #
  5. # a wrapper script for the main Source engine dedicated server binary.
  6. # Performs auto-restarting of the server on crash. You can
  7. # extend this to log crashes and more.
  8. #
  9.  
  10. # setup the libraries, local dir first!
  11. export LD_LIBRARY_PATH=".:bin:$LD_LIBRARY_PATH"
  12.  
  13. # The srcds_run script should be at the top level of the game tree
  14. # Make sure we are in that directory since the script assumes this is the case
  15.  
  16. cd "`dirname \"$0\"`"
  17.  
  18. if test `id -u` -eq 0; then
  19. echo
  20. echo
  21. echo "************** WARNING ***************"
  22. echo "Running the dedicated server as root "
  23. echo "is highly discouraged. It is generally"
  24. echo "unnecessary to use root privileges to "
  25. echo "execute the dedicated server. "
  26. echo "**************************************"
  27. echo
  28. echo
  29. timeout=10
  30. while test $timeout -gt 0; do
  31. echo -n "The server will continue to launch in $timeout seconds\r"
  32. timeout=`expr $timeout - 1`
  33. sleep 1
  34. done
  35. fi
  36.  
  37. init() {
  38. # Initialises the various variables
  39. # Set up the defaults
  40. GAME=""
  41. DEBUG=""
  42. RESTART="yes"
  43. HL=./srcds_linux
  44. HL_DETECT=1
  45. TIMEOUT=10 # time to wait after a crash (in seconds)
  46. CRASH_DEBUG_MSG="email debug.log to linux@valvesoftware.com"
  47. GDB="gdb" # the gdb binary to run
  48. DEBUG_LOG="debug.log"
  49. PID_FILE="" # only needed it DEBUG is set so init later
  50. STEAM=""
  51. PID_FILE_SET=0
  52. STEAMERR=""
  53. SIGINT_ACTION="quit 0" # exit normally on sig int
  54. NO_TRAP=0
  55. AUTO_UPDATE=""
  56. STEAM_USER=""
  57. STEAM_PASSWORD=""
  58. STEAM_VERIFY=0
  59. PARAMS=$*
  60.  
  61. # Remove any old default pid files
  62. # Cant do this as they may be still running
  63. #rm -f hlds.*.pid
  64.  
  65. # use the $FORCE environment variable if its set
  66. if test -n "$FORCE" ; then
  67. # Note: command line -binary will override this
  68. HL=$FORCE
  69. HL_DETECT=0
  70. fi
  71.  
  72. while test $# -gt 0; do
  73. case "$1" in
  74. "+map")
  75. MAP="$2"
  76. shift;;
  77. "-game")
  78. GAME="$2"
  79. shift ;;
  80. "-debug")
  81. DEBUG=1
  82. # Ensure that PID_FILE is set
  83. PID_FILE_SET=1
  84. if test -z "$PID_FILE"; then
  85. PID_FILE="hlds.$$.pid"
  86. fi ;;
  87. "-norestart")
  88. RESTART="" ;;
  89. "-pidfile")
  90. PID_FILE="$2"
  91. PID_FILE_SET=1
  92. shift ;;
  93. "-binary")
  94. HL="$2"
  95. HL_DETECT=0
  96. shift ;;
  97. "-timeout")
  98. TIMEOUT="$2"
  99. shift ;;
  100. "-gdb")
  101. GDB="$2"
  102. shift ;;
  103. "-debuglog")
  104. DEBUG_LOG="$2"
  105. shift ;;
  106. "-consolelog")
  107. CONSOLE_LOG="$2"
  108. shift ;;
  109. "-autoupdate")
  110. AUTO_UPDATE="yes"
  111. RESTART="yes" ;;
  112. "-steamerr")
  113. STEAMERR=1 ;;
  114. "-ignoresigint")
  115. SIGINT_ACTION="" ;;
  116. "-notrap")
  117. NO_TRAP=1 ;;
  118. "-steambin")
  119. STEAM=$2
  120. shift ;;
  121. "-steamuser")
  122. STEAM_USER="$2";
  123. shift ;;
  124. "-steampass")
  125. STEAM_PASSWORD="$2";
  126. shift ;;
  127. "-steamverify")
  128. STEAM_VERIFY=1 ;;
  129. "-help")
  130. # quit with syntax
  131. quit 2
  132. ;;
  133. esac
  134. shift
  135. done
  136.  
  137. # Ensure we have a game specified
  138. if test -z "$GAME"; then
  139. GAME="tf"
  140. PARAMS="$PARAMS -game $GAME"
  141. fi
  142.  
  143. # Check game directory
  144. if test ! -d "$GAME"; then
  145. echo "ERROR: Invalid game type '$GAME' sepecified."
  146. quit 1
  147. fi
  148.  
  149. if test -z "$MAP"; then
  150. echo "WARNING: No map specified! Server may not heartbeat."
  151. fi
  152.  
  153. if test 0 -eq "$NO_TRAP"; then
  154. # Set up the int handler
  155. # N.B. Dont use SIGINT symbolic value
  156. # as its just INT under ksh
  157. trap "$SIGINT_ACTION" 2
  158. fi
  159.  
  160. # Only detect the CPU if it hasnt been set with
  161. # either environment or command line
  162. if test "$HL_DETECT" -eq 1; then
  163. detectcpu
  164. fi
  165.  
  166. if test ! -f "$HL"; then
  167. echo "ERROR: Source Engine binary '$HL' not found, exiting"
  168. quit 1
  169. elif test ! -x "$HL"; then
  170. # Could try chmod but dont know what we will be
  171. # chmoding so just fail.
  172. echo "ERROR: Source engine binary '$HL' not executable, exiting"
  173. quit 1
  174. fi
  175.  
  176. # Setup debugging
  177. if test -n "$DEBUG" ; then
  178. #turn on core dumps :) (if possible)
  179. echo "Enabling debug mode"
  180. if test "unlimited" != `ulimit -c` && test "`ulimit -c`" -eq 0 ; then
  181. ulimit -c 2000
  182. fi
  183. GDB_TEST=`$GDB -v`
  184. if test -z "$GDB_TEST"; then
  185. echo "WARNING: Please install gdb first."
  186. echo " goto http://www.gnu.org/software/gdb/ "
  187. DEBUG="" # turn off debugging cause gdb isn't installed
  188. fi
  189. fi
  190.  
  191. if test -n "$STEAM_PASSWORD" && test -z "$STEAM_USER"; then
  192. echo "ERROR: You must set both the steam username and password."
  193. quit 1
  194. fi
  195.  
  196. HL_CMD="$HL $PARAMS"
  197. if test -n "$CONSOLE_LOG" -a -x "bin/logger"; then
  198. HL_CMD="bin/logger $CONSOLE_LOG $HL_CMD"
  199. fi
  200. }
  201.  
  202. syntax () {
  203. # Prints script syntax
  204.  
  205. echo "Syntax:"
  206. echo "$0 [-game <game>] [-debug] [-norestart] [-pidfile]"
  207. echo " [-binary <binary>]"
  208. echo " [-timeout <number>] [-gdb <gdb>] [-autoupdate]"
  209. echo " [-steambin] [-steamerr] [-ignoresigint] [-steamuser <username>]"
  210. echo " [-steampass <password>] [-steamverify] [-debuglog <logname>]"
  211. echo "Params:"
  212. echo "-game <game> Specifies the <game> to run. [Default: $DEFAULT_GAME]"
  213. echo "-debug Run debugging on failed servers if possible."
  214. echo "-debuglog <logname> Log debug output to this file."
  215. echo "-consolelog <logname> Log console output to this file."
  216. echo "-norestart Don't attempt to restart failed servers."
  217. echo "-pidfile <pidfile> Use the specified <pidfile> to store the server pid."
  218. echo "-binary <binary> Use the specified binary ( no auto detection ). [Default: $HL]"
  219. echo "-timeout <number> Sleep for <number> seconds before restarting"
  220. echo " a failed server."
  221. echo "-gdb <gdb> Use <dbg> as the debugger of failed servers."
  222. echo "-steambin <path> Path to steam binary."
  223. echo "-steamerr Quit on steam update failure."
  224. echo "-steamuser <username> Use this username for steam updates."
  225. echo "-steampass <password> Use this password for steam updates"
  226. echo " (-steamuser must be specified as well)."
  227. echo "-steamverify Force steam to verify the install on updates"
  228. echo "-ignoresigint Ignore signal INT ( prevents CTRL+C quitting"
  229. echo " the script )."
  230. echo "-notrap Don't use trap. This prevents automatic"
  231. echo " removal of old lock files."
  232. echo ""
  233. echo "Note: All parameters specified as passed through to the server"
  234. echo "including any not listed."
  235. }
  236.  
  237. debugcore () {
  238. # Debugs any core file if DEBUG is set and
  239. # the exitcode is none 0
  240.  
  241. exitcode=$1
  242.  
  243. if test $exitcode -ne 0; then
  244. if test -n "$DEBUG" ; then
  245. echo "bt" > debug.cmds;
  246. echo "info locals" >> debug.cmds;
  247. echo "info registers" >> debug.cmds
  248. echo "info sharedlibrary" >> debug.cmds
  249. echo "disassemble" >> debug.cmds
  250. echo "info frame" >> debug.cmds; # works, but gives an error... must be last
  251. echo "----------------------------------------------" >> $DEBUG_LOG
  252. echo "CRASH: `date`" >> $DEBUG_LOG
  253. echo "Start Line: $HL_CMD" >> $DEBUG_LOG
  254.  
  255. # check to see if a core was dumped
  256. if test -f core ; then
  257. CORE="core"
  258. elif test -f core.`cat $PID_FILE`; then
  259. CORE=core.`cat $PID_FILE`
  260. elif test -f "$HL.core" ; then
  261. CORE="$HL.core"
  262. fi
  263.  
  264. if test -n "$CORE"; then
  265. $GDB $HL $CORE -x debug.cmds -batch >> $DEBUG_LOG
  266. fi
  267.  
  268. echo "End of Source crash report" >> $DEBUG_LOG
  269. echo "----------------------------------------------" >> $DEBUG_LOG
  270. echo $CRASH_DEBUG_MSG
  271. rm debug.cmds
  272. else
  273. echo "Add \"-debug\" to the $0 command line to generate a debug.log to help with solving this problem"
  274. fi
  275. fi
  276. }
  277.  
  278. detectcpu() {
  279. # Attempts to auto detect the CPU
  280. echo "Auto detecting CPU"
  281.  
  282. if test "Linux" = `uname`; then
  283. HL=./srcds_linux
  284. echo "Using default binary: $HL"
  285. elif test "FreeBSD" = `uname`; then
  286. HL=./srcds_linux
  287. echo "Using default binary: $HL"
  288. elif test "Darwin" = `uname`; then
  289. echo "Using OSX binary."
  290. HL=./srcds_osx
  291. else
  292. echo "Using default binary: $HL"
  293. fi
  294. }
  295.  
  296. update() {
  297. updatesingle
  298. }
  299.  
  300. locatesteam() {
  301. OLDPATH=${PATH}
  302. PATH=..:.:${PATH}
  303. STEAM=`which steam 2>/dev/null`
  304. if test "$?" -gt 0; then
  305. echo "WARNING: Failed to locate steam binary."
  306. else
  307. echo "INFO: Located steam: $STEAM"
  308. fi
  309. PATH=${OLDPATH}
  310. }
  311.  
  312. updatesingle() {
  313. # Run the steam update
  314. # exits on failure if STEAMERR is set
  315.  
  316. if test -n "$AUTO_UPDATE"; then
  317. if test -z "$STEAM"; then
  318. locatesteam
  319. fi
  320. if test -f "$STEAM" -a -x "$STEAM"; then
  321. echo "Updating server using Steam."
  322. DEPOT_ROOT=".."
  323. DEPOT_NAME=$GAME
  324.  
  325. if test "$GAME" = "cstrike"; then
  326. DEPOT_NAME="Counter-Strike Source";
  327. fi
  328.  
  329. if test "$GAME" = "cstrike_beta"; then
  330. DEPOT_NAME="cssbeta";
  331. fi
  332.  
  333. if test "$GAME" = "dod"; then
  334. DEPOT_NAME="dods";
  335. fi
  336.  
  337. CMD="$STEAM -command update -dir $DEPOT_ROOT";
  338. if test 1 -eq "$STEAM_VERIFY"; then
  339. CMD="$CMD -verify_all"
  340. fi
  341. if test -n "$STEAM_USER"; then
  342. CMD="$CMD -username $STEAM_USER";
  343. fi
  344. if test -n "$STEAM_PASSWORD"; then
  345. CMD="$CMD -password $STEAM_PASSWORD";
  346. fi
  347.  
  348. $CMD -game "$DEPOT_NAME"
  349. if test $? -ne 0; then
  350. if test -n "$STEAMERR"; then
  351. echo "`date`: Steam Update failed, exiting."
  352. quit 1
  353. else
  354. echo "`date`: Steam Update failed, ignoring."
  355. return 0
  356. fi
  357. fi
  358. else
  359. if test -n "$STEAMERR"; then
  360. echo "ERROR: Could not locate steam binary:$STEAM, exiting.";
  361. quit 1
  362. else
  363. echo "WARNING: Could not locate steam binary:$STEAM, ignoring.";
  364. return 0
  365. fi
  366. fi
  367. fi
  368.  
  369. return 1
  370. }
  371.  
  372. run() {
  373. # Runs the steam update and server
  374. # Loops if RESTART is set
  375. # Debugs if server failure is detected
  376. # Note: if RESTART is not set then
  377. # 1. DEBUG is set then the server is NOT exec'd
  378. # 2. DEBUG is not set the the server is exec'd
  379.  
  380. if test -n "$RESTART" ; then
  381. echo "Server will auto-restart if there is a crash."
  382.  
  383. #loop forever
  384. while true
  385. do
  386. # Update if needed
  387. update
  388.  
  389. # Run the server
  390. $HL_CMD
  391. retval=$?
  392. if test $retval -eq 0 && test -z "$AUTO_UPDATE"; then
  393. break; # if 0 is returned then just quit
  394. fi
  395.  
  396. debugcore $retval
  397.  
  398. echo "`date`: Server restart in $TIMEOUT seconds"
  399.  
  400. # don't thrash the hard disk if the server dies, wait a little
  401. sleep $TIMEOUT
  402. done # while true
  403. else
  404. # Update if needed
  405. update
  406.  
  407. # Run the server
  408. if test -z "$DEBUG"; then
  409. # debug not requested we can exec
  410. exec $HL_CMD
  411. else
  412. # debug requested we can't exec
  413. $HL_CMD
  414. debugcore $?
  415. fi
  416. fi
  417. }
  418.  
  419. quit() {
  420. # Exits with the give error code, 1
  421. # if none specified.
  422. # exit code 2 also prints syntax
  423. exitcode="$1"
  424.  
  425. # default to failure
  426. if test -z "$exitcode"; then
  427. exitcode=1
  428. fi
  429.  
  430. case "$exitcode" in
  431. 0)
  432. echo "`date`: Server Quit" ;;
  433. 2)
  434. syntax ;;
  435. *)
  436. echo "`date`: Server Failed" ;;
  437. esac
  438.  
  439. # Remove pid file
  440. if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
  441. # The specified pid file
  442. rm -f $PID_FILE
  443. fi
  444.  
  445. # reset SIGINT and then kill ourselves properly
  446. trap - 2
  447. kill -2 $$
  448. }
  449.  
  450. # Initialise
  451. init $*
  452.  
  453. # Run
  454. run
  455.  
  456. # Quit normally
  457. quit 0
Add Comment
Please, Sign In to add comment