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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #! /bin/sh
  2. #
  3. # snf-server Starts and stops the SNFServer daemon (Ubuntu).
  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. ### BEGIN INIT INFO
  11. # Provides: SNFServer
  12. # Required-Start: $syslog $remote_fs $network $named
  13. # Should-Start: $time ypbind smtp
  14. # Required-Stop: $syslog $remote_fs
  15. # Should-Stop: $time ypbind smtp
  16. # Default-Start: 3 4 5
  17. # Default-Stop: 0 1 2 6
  18. # Short-Description: SNFServer providing email filtering.
  19. # Description: Start SNFServer to filter email for spam,
  20. # blacklist IP addresses, etc.
  21. ### END INIT INFO
  22. # Directory to run in.
  23. runDir=PREFIX/share/PACKAGE_NAME
  24. # Define mode files.
  25. debugModeFile=$runDir/debug_mode
  26. productionModeFile=$runDir/production_mode
  27. # Set debug mode flag.
  28. if [ -f $debugModeFile ]
  29. then
  30. debugMode=true
  31. fi
  32. # Debug output file.
  33. debugOutputFile=/var/log/PACKAGE_NAME/debug.log
  34. # Location of installation.
  35. installedDir="PREFIX"
  36. # Location of programs.
  37. dir="$installedDir/sbin"
  38. # Name of config file.
  39. configFile="CONFFILE"
  40. # Name of daemon.
  41. debugProg="SNFDebugServer"
  42. productionProg="SNFServer"
  43. # Name of client.
  44. clientProg="SNFClient"
  45. # Name of user to run as.
  46. userName="snfuser"
  47. # Name of group.
  48. groupName=$userName
  49. # Name of lockfile.
  50. lockFile="/var/lock/subsys/$productionProg"
  51. # Name of client.
  52. clientProg="SNFClient"
  53. # Do NOT "set -e"
  54. # PATH should only include /usr/* if it runs after the mountnfs.sh script
  55. PATH=/usr/sbin:/usr/bin:/sbin:/bin
  56. DESC="SNFServer providing email filtering"
  57. NAME="snf-server"
  58. DEBUG_DAEMON="/usr/bin/strace"
  59. DEBUG_DAEMON_ARGS="-r -tt -v $dir/$debugProg $configFile"
  60. PRODUCTION_DAEMON=$dir/$productionProg
  61. PRODUCTION_DAEMON_ARGS="$configFile"
  62. SCRIPTNAME=/etc/init.d/$NAME
  63. # Exit if the package is not installed
  64. [ -x "$PRODUCTION_DAEMON" ] || exit 0
  65. # Read configuration variable file if it is present
  66. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  67. # Load the VERBOSE setting and other rcS variables
  68. [ -f /etc/default/rcS ] && . /etc/default/rcS
  69. # Define LSB log_* functions.
  70. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  71. . /lib/lsb/init-functions
  72. #
  73. # Function to create the mode file.
  74. #
  75. createModeFile()
  76. {
  77. fileName=$1
  78. # Remove any existing mode files.
  79. rm -f $productionModeFile $debugModeFile $fileName
  80. (
  81. echo "PACKAGE_NAME mode file"
  82. echo
  83. echo "This file specifies whether PACKAGE_NAME is configured to run in"
  84. echo "production mode or debug mode. If the name of this file is"
  85. echo "'production_mode', then PACKAGE_NAME is configured to run in"
  86. echo "production mode. If the name is 'debug_mode', then PACKAGE_NAME is"
  87. echo "configured to run in debug mode."
  88. echo
  89. echo "To run in debug mode:"
  90. echo
  91. echo " 1) Run 'PACKAGE_NAME debug_mode'"
  92. echo
  93. echo " 2) Run 'PACKAGE_NAME restart' (if PACKAGE_NAME is already running),"
  94. echo " or 'PACKAGE_NAME start' (to start PACKAGE_NAME)"
  95. echo
  96. echo "To run in production mode:"
  97. echo
  98. echo " 1) Run 'PACKAGE_NAME production_mode'"
  99. echo
  100. echo " 2) Run 'PACKAGE_NAME restart' (if PACKAGE_NAME is already running),"
  101. echo " or 'PACKAGE_NAME start' (to start PACKAGE_NAME)"
  102. echo
  103. echo "By default, PACKAGE_NAME is configured to run in production mode."
  104. ) > $fileName
  105. }
  106. #
  107. # Function that starts the daemon/service
  108. #
  109. do_start()
  110. {
  111. # Return
  112. # 0 if daemon has been started
  113. # 1 if daemon was already running
  114. # 2 if daemon could not be started
  115. start-stop-daemon --chuid $userName --start --quiet \
  116. --exec $PRODUCTION_DAEMON --test > /dev/null 2>&1 \
  117. || return 1
  118. if [ $debugMode ]
  119. then
  120. # Enable core dumps.
  121. ulimit -c unlimited
  122. # Add date to the debug file.
  123. echo "Starting $DEBUG_DAEMON $DEBUG_DAEMON_ARGS on " $(date) >> \
  124. $debugOutputFile
  125. chown $userName:$groupName $debugOutputFile
  126. # Start with output redirected.
  127. start-stop-daemon --chuid $userName --start --quiet \
  128. --chdir $runDir \
  129. --exec $DEBUG_DAEMON -- $DEBUG_DAEMON_ARGS >> \
  130. $debugOutputFile 2>&1 \
  131. || return 2 &
  132. else
  133. start-stop-daemon --chuid $userName --start --quiet \
  134. --chdir $runDir \
  135. --background --exec $PRODUCTION_DAEMON -- \
  136. $PRODUCTION_DAEMON_ARGS > /dev/null 2>&1 || return 2
  137. fi
  138. # Check that process started.
  139. $dir/$clientProg -status.second > /dev/null 2>&1
  140. RETVAL=$?
  141. if [ $RETVAL -ne 0 ]; then
  142. RETVAL=2
  143. fi
  144. return $RETVAL
  145. }
  146. #
  147. # Function that stops the daemon/service
  148. #
  149. do_stop()
  150. {
  151. # Return
  152. # 0 if daemon has been stopped
  153. # 1 if daemon was already stopped
  154. # 2 if daemon could not be stopped
  155. # other if a failure occurred
  156. # Check whether SNFServer is running.
  157. start-stop-daemon --chuid $userName --start --quiet \
  158. --exec $PRODUCTION_DAEMON --test > /dev/null
  159. RETVAL=$?
  160. if [ $RETVAL -ne 1 ]; then
  161. return 1
  162. fi
  163. # Issue shutdown command
  164. $dir/$clientProg -shutdown > /dev/null 2>&1
  165. sleep 10
  166. # Check again whether SNFServer is running.
  167. start-stop-daemon --chuid $userName --start --quiet \
  168. --exec $PRODUCTION_DAEMON --test > /dev/null
  169. RETVAL=$?
  170. if [ $RETVAL -eq 1 ] ; then
  171. # Send TERM signal to stop SNFServer.
  172. start-stop-daemon --stop --quiet --retry=TERM/5 --exec $PRODUCTION_DAEMON
  173. RETVAL="$?"
  174. [ "$RETVAL" = 2 ] && return 2
  175. return "$RETVAL"
  176. fi
  177. # SNFServer isn't running.
  178. return 0
  179. }
  180. case "$1" in
  181. start)
  182. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
  183. do_start
  184. case "$?" in
  185. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  186. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  187. esac
  188. ;;
  189. stop)
  190. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  191. do_stop
  192. case "$?" in
  193. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  194. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  195. esac
  196. ;;
  197. status)
  198. start-stop-daemon --chuid $userName --start --quiet \
  199. --exec $PRODUCTION_DAEMON --test > /dev/null
  200. RETVAL=$?
  201. if [ $RETVAL -eq 0 ]; then
  202. # Stopped
  203. echo "$productionProg is stopped"
  204. else
  205. # Running
  206. echo "$productionProg (pid $(pidof $PRODUCTION_DAEMON)) is running"
  207. fi
  208. ;;
  209. restart|force-reload)
  210. #
  211. # If the "reload" option is implemented then remove the
  212. # 'force-reload' alias
  213. #
  214. log_daemon_msg "Restarting $DESC" "$NAME"
  215. do_stop
  216. case "$?" in
  217. 0|1)
  218. do_start
  219. case "$?" in
  220. 0) log_end_msg 0 ;;
  221. 1) log_end_msg 1 ;; # Old process is still running
  222. *) log_end_msg 1 ;; # Failed to start
  223. esac
  224. ;;
  225. *)
  226. # Failed to stop
  227. log_end_msg 1
  228. ;;
  229. esac
  230. ;;
  231. debug_mode)
  232. #
  233. # Remove any mode flags, and create the debug_mode file.
  234. #
  235. log_daemon_msg "Switching to debug mode $DESC" "$NAME"
  236. createModeFile $debugModeFile
  237. ;;
  238. production_mode)
  239. #
  240. # Remove any mode flags, and create the production_mode file.
  241. #
  242. log_daemon_msg "Switching to production mode $DESC" "$NAME"
  243. createModeFile $productionModeFile
  244. ;;
  245. *)
  246. echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload|production_mode|debug_mode}" >&2
  247. exit 3
  248. ;;
  249. esac