vblade.init.in 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/bin/sh
  2. [% IF lsb -%]
  3. ### BEGIN INIT INFO
  4. # Provides: vblade
  5. # Required-Start: $remote_fs $syslog $network
  6. # Required-Stop: $remote_fs $syslog $network
  7. # Default-Start: 2 3 4 5
  8. # Default-Stop: 0 1 6
  9. # Short-Description: vblade exports
  10. # Description: Manage all vlbade exports defined in
  11. # /etc/vblade.conf.d/
  12. ### END INIT INFO
  13. [% END -%]
  14. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  15. DESC="vblade export"
  16. NAME=vblade
  17. VBLADE="/usr/sbin/$NAME"
  18. [% IF control == 'ssd' -%]
  19. [% PERL -%]die ('control=ssd cannot be used as long as vblade as no pidfile support');[% END -%]
  20. [% ELSIF control == 'daemon' -%]
  21. DAEMON=/usr/bin/daemon
  22. [% END -%]
  23. IONICE=/usr/bin/ionice
  24. PIDDIR="/var/run/vblade/"
  25. [ -x "$VBLADE" ] || exit 0
  26. [% IF control == 'daemon' -%]
  27. [ -x "$DAEMON" ] || exit 0
  28. [% END -%]
  29. mkdir -p "$PIDDIR"
  30. [% IF lsb -%]
  31. # Load the VERBOSE setting and other rcS variables
  32. . /lib/init/vars.sh
  33. # Define LSB functions
  34. . /lib/lsb/init-functions
  35. [% ELSE -%]
  36. # Emulation of LSB functions
  37. VERBOSE=1
  38. log_daemon_msg () {
  39. printf '%s ' "$@"
  40. }
  41. log_end_msg () {
  42. local CODE="$1"
  43. if [ "$CODE" -eq 0 ] ; then
  44. echo '.'
  45. else
  46. echo 'failed!'
  47. fi
  48. }
  49. [% END -%]
  50. # Start a vblade instance
  51. #
  52. # Return
  53. # 0 if daemon has been started
  54. # 1 if daemon was already running
  55. # 2 if daemon could not be started
  56. do_start () {
  57. local INSTANCE="$1"
  58. local CONFIG="$2"
  59. sh -n "$CONFIG" 2>/dev/null || return 2
  60. shelf=
  61. slot=
  62. filename=
  63. netif=
  64. options=
  65. ionice=
  66. . "$CONFIG"
  67. [ "$netif" ] || return 2
  68. [ "$shelf" ] || return 2
  69. [ "$slot" ] || return 2
  70. [ "$filename" ] || return 2
  71. if [ "$ionice" ] ; then
  72. if [ -x "$IONICE" ] ; then
  73. ionice="$IONICE $ionice"
  74. else
  75. ionice=
  76. fi
  77. fi
  78. [% IF control == 'ssd' -%]
  79. local PIDFILE="$PIDDIR/$INSTANCE.pid"
  80. start-stop-daemon --start --quiet \
  81. --pidfile "$PIDFILE" --exec "$VBLADE" --test > /dev/null \
  82. || return 1
  83. start-stop-daemon --start --quiet \
  84. --pidfile "$PIDFILE" \
  85. --exec $ionice "$VBLADE" -- \
  86. $shelf $slot $netif $filename $options \
  87. || return 2
  88. [% ELSIF control == 'daemon' -%]
  89. "$DAEMON" \
  90. --running \
  91. --name "$INSTANCE" \
  92. --pidfiles "$PIDDIR" \
  93. && return 1
  94. $ionice "$DAEMON" \
  95. --respawn \
  96. --name "$INSTANCE" \
  97. --pidfiles "$PIDDIR" \
  98. --output daemon.notice \
  99. --stdout daemon.notice \
  100. --stderr daemon.err -- \
  101. $VBLADE $options $shelf $slot $netif $filename || return 2
  102. [% END -%]
  103. }
  104. # Stop a vblade instance
  105. #
  106. # Return
  107. # 0 if daemon has been stopped
  108. # 1 if daemon was already stopped
  109. # 2 if daemon could not be stopped
  110. # other if a failure occurred
  111. do_stop () {
  112. local INSTANCE="$1"
  113. [% IF control == 'ssd' -%]
  114. local PIDFILE="$PIDDIR/$INSTANCE.pid"
  115. start-stop-daemon --stop --quiet \
  116. --retry=TERM/30/KILL/5 --pidfile "$PIDFILE" --name "$NAME"
  117. RETVAL="$?"
  118. [ "$RETVAL" = 2 ] && return 2
  119. # Many daemons don't delete their pidfiles when they exit.
  120. rm -f "$PIDFILE"
  121. return "$RETVAL"
  122. [% ELSIF control == 'daemon' -%]
  123. "$DAEMON" \
  124. --running \
  125. --name "$INSTANCE" \
  126. --pidfiles "$PIDDIR" || return 1
  127. "$DAEMON" \
  128. --stop \
  129. --name "$INSTANCE" \
  130. --pidfiles "$PIDDIR" \
  131. --stop || return 2
  132. # Wait until the process is gone
  133. for i in $(seq 1 10) ; do
  134. "$DAEMON" \
  135. --running \
  136. --name "$INSTANCE" \
  137. --pidfiles "$PIDDIR" || return 0
  138. done
  139. return 2
  140. [% END -%]
  141. }
  142. EXIT=0
  143. do_action () {
  144. local CONFIG="$1"
  145. INSTANCE="$(basename "${CONFIG%%.conf}")"
  146. case "$ACTION" in
  147. start)
  148. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$INSTANCE"
  149. do_start "$INSTANCE" "$CONFIG"
  150. case "$?" in
  151. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  152. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  153. esac
  154. ;;
  155. stop)
  156. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$INSTANCE"
  157. do_stop "$INSTANCE"
  158. case "$?" in
  159. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  160. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  161. esac
  162. ;;
  163. status)
  164. [% IF lsb -%]
  165. status_of_proc -p "$PIDDIR/$INSTANCE.pid" "$VBLADE" "vblade instance $INSTANCE" || EXIT=$?
  166. [% ELSE -%]
  167. if "$DAEMON" \
  168. --running \
  169. --name "$INSTANCE" \
  170. --pidfiles "$PIDDIR"
  171. then
  172. echo "$DESC instance $INSTANCE is running"
  173. else
  174. echo "$DESC instance $INSTANCE is not running"
  175. EXIT=1
  176. fi
  177. [% END -%]
  178. ;;
  179. restart|force-reload)
  180. log_daemon_msg "Restarting $DESC" "$INSTANCE"
  181. do_stop "$INSTANCE"
  182. case "$?" in
  183. 0|1)
  184. do_start "$INSTANCE" "$CONFIG"
  185. case "$?" in
  186. 0) log_end_msg 0 ;;
  187. *)
  188. # Old process is still running or
  189. # failed to start
  190. log_end_msg 1 ;;
  191. esac
  192. ;;
  193. *)
  194. # Failed to stop
  195. log_end_msg 1
  196. ;;
  197. esac
  198. ;;
  199. *)
  200. echo "Usage: /etc/init.d/vblade {start|stop|status|restart|force-reload} [<export> ...]" >&2
  201. exit 3
  202. ;;
  203. esac
  204. }
  205. ACTION="$1"
  206. shift
  207. if [ "$1" ] ; then
  208. while [ "$1" ] ; do
  209. CONFIG="/etc/vblade.conf.d/$1.conf"
  210. if [ -f "$CONFIG" ] ; then
  211. do_action "$CONFIG"
  212. fi
  213. shift
  214. done
  215. else
  216. for CONFIG in /etc/vblade.conf.d/*.conf ; do
  217. if [ -f "$CONFIG" ] ; then
  218. do_action "$CONFIG"
  219. fi
  220. done
  221. fi
  222. exit $EXIT