You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/bin/sh
  2. #
  3. # snf-server. Starts and stops the SNFServer daemon (OpenBSD).
  4. #
  5. # Author: Alban Deniz
  6. #
  7. # Copyright (C) 2008 ARM Research Labs, LLC.
  8. # See www.armresearch.com for the copyright terms.
  9. #
  10. # Directory to run in.
  11. runDir=PREFIX/share/PACKAGE_NAME
  12. # Define mode files.
  13. debugModeFile=$runDir/debug_mode
  14. productionModeFile=$runDir/production_mode
  15. # Set debug mode flag.
  16. if [ -f $debugModeFile ]
  17. then
  18. debugMode=true
  19. fi
  20. # Debug output file.
  21. debugOutputFile=/var/log/PACKAGE_NAME/debug.log
  22. # ktrace output file.
  23. ktraceOutputFile=/var/log/PACKAGE_NAME/ktrace.out
  24. # Location of installation.
  25. installedDir="PREFIX"
  26. # Location of programs.
  27. dir="$installedDir/sbin"
  28. # Name of config file.
  29. configFile="CONFFILE"
  30. # Name of daemon.
  31. debugProg="SNFDebugServer"
  32. productionProg="SNFServer"
  33. # Name of client.
  34. clientProg="SNFClient"
  35. # Name of user to run as.
  36. userName="snfuser"
  37. #
  38. # Function to create the mode file.
  39. #
  40. createModeFile()
  41. {
  42. fileName=$1
  43. # Remove any existing mode files.
  44. rm -f $productionModeFile $debugModeFile $fileName
  45. (
  46. echo "PACKAGE_NAME mode file"
  47. echo
  48. echo "This file specifies whether PACKAGE_NAME is configured to run in"
  49. echo "production mode or debug mode. If the name of this file is"
  50. echo "'production_mode', then PACKAGE_NAME is configured to run in"
  51. echo "production mode. If the name is 'debug_mode', then PACKAGE_NAME is"
  52. echo "configured to run in debug mode."
  53. echo
  54. echo "To run in debug mode:"
  55. echo
  56. echo " 1) Run 'PACKAGE_NAME debug_mode'"
  57. echo
  58. echo " 2) Run 'PACKAGE_NAME restart' (if PACKAGE_NAME is already running),"
  59. echo " or 'PACKAGE_NAME start' (to start PACKAGE_NAME)"
  60. echo
  61. echo "To run in production mode:"
  62. echo
  63. echo " 1) Run 'PACKAGE_NAME production_mode'"
  64. echo
  65. echo " 2) Run 'PACKAGE_NAME restart' (if PACKAGE_NAME is already running),"
  66. echo " or 'PACKAGE_NAME start' (to start PACKAGE_NAME)"
  67. echo
  68. echo "By default, PACKAGE_NAME is configured to run in production mode."
  69. ) > $fileName
  70. }
  71. start(){
  72. echo -n " $productionProg "
  73. for prog in $productionProg $debugProg
  74. do
  75. SNFPID=`ps -axww | grep $dir/$prog | grep -v grep | awk '{print $1}'`
  76. if [ -n "$SNFPID" ] ; then
  77. echo "already running"
  78. return 1
  79. fi
  80. done
  81. # Start.
  82. if [ $debugMode ]
  83. then
  84. # Enable core dumps and start with ktrace and output redirected.
  85. (ulimit -c unlimited; \
  86. su -m $userName -c \
  87. "echo Starting $dir/$debugProg on $(date) >> $debugOutputFile"; \
  88. cd PREFIX/share/PACKAGE_NAME; \
  89. su -m $userName -c \
  90. "ktrace -f $ktraceOutputFile $dir/$debugProg $configFile >> $debugOutputFile 2>&1 &")
  91. else
  92. (cd PREFIX/share/PACKAGE_NAME; \
  93. su -m $userName -c "$dir/$productionProg $configFile > /dev/null 2>&1 &" \
  94. > /dev/null 2>&1)
  95. fi
  96. RETVAL=$?
  97. if [ $RETVAL -eq 0 ]; then
  98. $dir/$clientProg -status.second > /dev/null 2>&1
  99. RETVAL=$?
  100. fi
  101. if [ $RETVAL -eq 0 ]; then
  102. echo "started "
  103. else
  104. echo "failed "
  105. fi
  106. return $RETVAL
  107. }
  108. getPID(){
  109. ps -axww | grep $1 | grep -v grep | awk '{print $1}'
  110. }
  111. stopFunction(){
  112. echo -n " $prog "
  113. DEBUG_SNFPID=`getPID $dir/$debugProg`
  114. PRODUCTION_SNFPID=`getPID $dir/$productionProg`
  115. if [ -n "$DEBUG_SNFPID" ] || [ -n "$PRODUCTION_SNFPID" ]; then
  116. $dir/$clientProg -shutdown > /dev/null 2>&1
  117. sleep 10
  118. # Check that the programs are no longer running.
  119. RETVAL=0
  120. for prog in $debugProg $productionProg
  121. do
  122. SNFPID=`getPID $dir/$prog`
  123. if [ -n "$SNFPID" ]; then
  124. kill $SNFPID
  125. RETVAL=$(($RETVAL+$?))
  126. fi
  127. done
  128. echo -n "stopped"
  129. else
  130. echo -n "not running"
  131. RETVAL=1
  132. fi
  133. echo ""
  134. return $RETVAL
  135. }
  136. restart(){
  137. stopFunction
  138. start
  139. }
  140. status(){
  141. # Check whether the programs are running.
  142. RETVAL=0
  143. for progName in $debugProg $productionProg
  144. do
  145. SNFPID=`getPID $dir/$progName`
  146. if [ -n "$SNFPID" ]; then
  147. echo "$progName (pid $SNFPID) is running"
  148. RETVAL=$(($RETVAL+1))
  149. else
  150. echo "$progName is not running"
  151. fi
  152. done
  153. return 0
  154. }
  155. # See how we were called.
  156. case "$1" in
  157. start)
  158. start
  159. ;;
  160. stop)
  161. stopFunction
  162. ;;
  163. status)
  164. status
  165. ;;
  166. restart)
  167. restart
  168. ;;
  169. debug_mode)
  170. #
  171. # Remove any mode flags, and create the debug_mode file.
  172. #
  173. echo "Switching to debug mode"
  174. createModeFile $debugModeFile
  175. ;;
  176. production_mode)
  177. #
  178. # Remove any mode flags, and create the debug_mode file.
  179. #
  180. echo "Switching to production mode"
  181. createModeFile $productionModeFile
  182. ;;
  183. *)
  184. echo "Usage: $0 {start|stop|status|restart|production_mode|debug_mode}"
  185. exit 1
  186. esac
  187. exit $?