corosync.in 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!@BASHPATH@
  2. # Authors:
  3. # Andrew Beekhof <abeekhof@redhat.com>
  4. # Fabio M. Di Nitto <fdinitto@redhat.com>
  5. #
  6. # License: Revised BSD
  7. # chkconfig: - 20 80
  8. # description: Corosync Cluster Engine
  9. # processname: corosync
  10. #
  11. ### BEGIN INIT INFO
  12. # Provides: corosync
  13. # Required-Start: $network $syslog
  14. # Required-Stop: $network $syslog
  15. # Default-Start:
  16. # Default-Stop:
  17. # Short-Description: Starts and stops Corosync Cluster Engine.
  18. # Description: Starts and stops Corosync Cluster Engine.
  19. ### END INIT INFO
  20. desc="Corosync Cluster Engine"
  21. prog="corosync"
  22. prog_pid_file="@LOCALSTATEDIR@/run/$prog.pid"
  23. # set secure PATH
  24. PATH="/sbin:/bin:/usr/sbin:/usr/bin:@SBINDIR@"
  25. success()
  26. {
  27. echo -ne "[ OK ]\r"
  28. }
  29. failure()
  30. {
  31. echo -ne "[FAILED]\r"
  32. }
  33. warning()
  34. {
  35. echo -ne "[WARNING]\r"
  36. }
  37. # pid_var_run pid_file
  38. # Echo pid from given pid_file.
  39. # Returns LSB exit code for the 'status' action.
  40. pid_var_run()
  41. {
  42. local pid_file="$1"
  43. local pid
  44. if [ -f "$pid_file" ]; then
  45. [ ! -r "$pid_file" ] && return 4
  46. pid=$(cat "$pid_file")
  47. [ -z "$pid" ] && return 1
  48. [ -n "${pid//[0-9]/}" ] && return 1
  49. if kill -n 0 "$pid" 2>/dev/null;then
  50. echo "$pid"
  51. return 0
  52. else
  53. return 1
  54. fi
  55. fi
  56. return 3
  57. }
  58. # status [-p pid_file] {program}
  59. status()
  60. {
  61. local pid_file
  62. if [ "$1" = "-p" ]; then
  63. pid_file=$2
  64. shift 2
  65. fi
  66. pid=$(pid_var_run "$pid_file" 2>/dev/null)
  67. res=$?
  68. if [ $res -ne 0 -a -z "$pid_file" ]; then
  69. pid=$(__pids_pidof "$1")
  70. [ -n "$pid" ]
  71. res=$?
  72. fi
  73. if [ $res -ne 0 ]; then
  74. echo "$1 is stopped"
  75. else
  76. echo "$1 (pid $pid) is running..."
  77. fi
  78. return $res
  79. }
  80. [ -f @INITCONFIGDIR@/$prog ] && . @INITCONFIGDIR@/$prog
  81. case '@INITCONFIGDIR@' in
  82. */sysconfig) # rpm based distros
  83. [ -f @INITDDIR@/functions ] && . @INITDDIR@/functions
  84. [ -z "$LOCK_FILE" ] && LOCK_FILE="@LOCALSTATEDIR@/lock/subsys/$prog";;
  85. */default) # deb based distros
  86. [ -z "$LOCK_FILE" ] && LOCK_FILE="@LOCALSTATEDIR@/lock/$prog";;
  87. esac
  88. # The version of __pids_pidof in /etc/init.d/functions calls pidof with -x
  89. # This means it matches scripts, including this one.
  90. # Redefine it here so that status (from the same file) works.
  91. # Otherwise simultaneous calls to stop() will loop forever
  92. __pids_pidof() {
  93. pidof -c -o $$ -o $PPID -o %PPID "$1" || \
  94. pidof -c -o $$ -o $PPID -o %PPID "${1##*/}"
  95. }
  96. cluster_disabled_at_boot()
  97. {
  98. if grep -q nocluster /proc/cmdline && \
  99. [ "$(tty)" = "/dev/console" ]; then
  100. echo -e "not configured to run at boot"
  101. failure
  102. return 1
  103. fi
  104. return 0
  105. }
  106. wait_for_ipc()
  107. {
  108. try=0
  109. max_try=$((COROSYNC_INIT_TIMEOUT*2-1))
  110. [ "$max_try" -le "0" ] && max_try=120
  111. while [ "$try" -le "$max_try" ]; do
  112. if corosync-cpgtool > /dev/null 2>&1; then
  113. return 0
  114. fi
  115. sleep 0.5
  116. try=$((try + 1))
  117. done
  118. return 1
  119. }
  120. start()
  121. {
  122. echo -n "Starting $desc ($prog): "
  123. ! cluster_disabled_at_boot && return
  124. # most recent distributions use tmpfs for @LOCALSTATEDIR@/run
  125. # to avoid to clean it up on every boot.
  126. # they also assume that init scripts will create
  127. # required subdirectories for proper operations
  128. mkdir -p @LOCALSTATEDIR@/run
  129. if status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; then
  130. success
  131. else
  132. $prog $COROSYNC_OPTIONS > /dev/null 2>&1
  133. if [ "$?" != 0 ] || ! wait_for_ipc; then
  134. failure
  135. rtrn=1
  136. else
  137. touch $LOCK_FILE
  138. if corosync-cfgtool -s > /dev/null 2>&1; then
  139. success
  140. else
  141. warning
  142. fi
  143. fi
  144. fi
  145. echo
  146. }
  147. stop()
  148. {
  149. ! status -p "$prog_pid_file" "$prog" > /dev/null 2>&1 && return
  150. echo -n "Signaling $desc ($prog) to terminate: "
  151. # Call cfgtool to trigger shutdown callbacks
  152. corosync-cfgtool -H --force > /dev/null 2>&1
  153. # Send TERM signal (if cfgtool -H failed)
  154. kill -TERM "$(pid_var_run $prog_pid_file)" > /dev/null 2>&1
  155. success
  156. echo
  157. echo -n "Waiting for $prog services to unload:"
  158. while status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; do
  159. sleep 1
  160. echo -n "."
  161. done
  162. rm -f $LOCK_FILE
  163. success
  164. echo
  165. }
  166. restart()
  167. {
  168. stop
  169. start
  170. }
  171. rtrn=0
  172. case "$1" in
  173. start)
  174. start
  175. ;;
  176. restart|reload|force-reload)
  177. restart
  178. ;;
  179. condrestart|try-restart)
  180. if status -p "$prog_pid_file" "$prog" > /dev/null 2>&1; then
  181. restart
  182. fi
  183. ;;
  184. status)
  185. status -p "$prog_pid_file" "$prog"
  186. rtrn=$?
  187. ;;
  188. stop)
  189. stop
  190. ;;
  191. *)
  192. echo "usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
  193. rtrn=2
  194. ;;
  195. esac
  196. exit $rtrn