1
0

vblade.init.daemon 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/bin/sh
  2. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  3. DESC="vblade export"
  4. NAME=vblade
  5. VBLADE="/usr/sbin/$NAME"
  6. DAEMON=/usr/bin/daemon
  7. IONICE=/usr/bin/ionice
  8. PIDDIR="/var/run/vblade/"
  9. [ -x "$VBLADE" ] || exit 0
  10. [ -x "$DAEMON" ] || exit 0
  11. mkdir -p "$PIDDIR"
  12. # Emulation of LSB functions
  13. VERBOSE=1
  14. log_daemon_msg () {
  15. printf '%s ' "$@"
  16. }
  17. log_end_msg () {
  18. local CODE="$1"
  19. if [ "$CODE" -eq 0 ] ; then
  20. echo '.'
  21. else
  22. echo 'failed!'
  23. fi
  24. }
  25. # Start a vblade instance
  26. #
  27. # Return
  28. # 0 if daemon has been started
  29. # 1 if daemon was already running
  30. # 2 if daemon could not be started
  31. do_start () {
  32. local INSTANCE="$1"
  33. local CONFIG="$2"
  34. sh -n "$CONFIG" 2>/dev/null || return 2
  35. shelf=
  36. slot=
  37. filename=
  38. netif=
  39. options=
  40. ionice=
  41. . "$CONFIG"
  42. [ "$netif" ] || return 2
  43. [ "$shelf" ] || return 2
  44. [ "$slot" ] || return 2
  45. [ "$filename" ] || return 2
  46. if [ "$ionice" ] ; then
  47. if [ -x "$IONICE" ] ; then
  48. ionice="$IONICE $ionice"
  49. else
  50. ionice=
  51. fi
  52. fi
  53. "$DAEMON" \
  54. --running \
  55. --name "$INSTANCE" \
  56. --pidfiles "$PIDDIR" \
  57. && return 1
  58. $ionice "$DAEMON" \
  59. --respawn \
  60. --name "$INSTANCE" \
  61. --pidfiles "$PIDDIR" \
  62. --output daemon.notice \
  63. --stdout daemon.notice \
  64. --stderr daemon.err -- \
  65. $VBLADE $options $shelf $slot $netif $filename || return 2
  66. }
  67. # Stop a vblade instance
  68. #
  69. # Return
  70. # 0 if daemon has been stopped
  71. # 1 if daemon was already stopped
  72. # 2 if daemon could not be stopped
  73. # other if a failure occurred
  74. do_stop () {
  75. local INSTANCE="$1"
  76. "$DAEMON" \
  77. --running \
  78. --name "$INSTANCE" \
  79. --pidfiles "$PIDDIR" || return 1
  80. "$DAEMON" \
  81. --stop \
  82. --name "$INSTANCE" \
  83. --pidfiles "$PIDDIR" \
  84. --stop || return 2
  85. # Wait until the process is gone
  86. for i in $(seq 1 10) ; do
  87. "$DAEMON" \
  88. --running \
  89. --name "$INSTANCE" \
  90. --pidfiles "$PIDDIR" || return 0
  91. done
  92. return 2
  93. }
  94. EXIT=0
  95. do_action () {
  96. local CONFIG="$1"
  97. INSTANCE="$(basename "${CONFIG%%.conf}")"
  98. case "$ACTION" in
  99. start)
  100. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$INSTANCE"
  101. do_start "$INSTANCE" "$CONFIG"
  102. case "$?" in
  103. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  104. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  105. esac
  106. ;;
  107. stop)
  108. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$INSTANCE"
  109. do_stop "$INSTANCE"
  110. case "$?" in
  111. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  112. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  113. esac
  114. ;;
  115. status)
  116. if "$DAEMON" \
  117. --running \
  118. --name "$INSTANCE" \
  119. --pidfiles "$PIDDIR"
  120. then
  121. echo "$DESC instance $INSTANCE is running"
  122. else
  123. echo "$DESC instance $INSTANCE is not running"
  124. EXIT=1
  125. fi
  126. ;;
  127. restart|force-reload)
  128. log_daemon_msg "Restarting $DESC" "$INSTANCE"
  129. do_stop "$INSTANCE"
  130. case "$?" in
  131. 0|1)
  132. do_start "$INSTANCE" "$CONFIG"
  133. case "$?" in
  134. 0) log_end_msg 0 ;;
  135. *)
  136. # Old process is still running or
  137. # failed to start
  138. log_end_msg 1 ;;
  139. esac
  140. ;;
  141. *)
  142. # Failed to stop
  143. log_end_msg 1
  144. ;;
  145. esac
  146. ;;
  147. *)
  148. echo "Usage: /etc/init.d/vblade {start|stop|status|restart|force-reload} [<export> ...]" >&2
  149. exit 3
  150. ;;
  151. esac
  152. }
  153. ACTION="$1"
  154. shift
  155. if [ "$1" ] ; then
  156. while [ "$1" ] ; do
  157. CONFIG="/etc/vblade.conf.d/$1.conf"
  158. if [ -f "$CONFIG" ] ; then
  159. do_action "$CONFIG"
  160. fi
  161. shift
  162. done
  163. else
  164. for CONFIG in /etc/vblade.conf.d/*.conf ; do
  165. if [ -f "$CONFIG" ] ; then
  166. do_action "$CONFIG"
  167. fi
  168. done
  169. fi
  170. exit $EXIT