PageRenderTime 2764ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/utilities/ovs-lib.sh.in

https://github.com/homework/openvswitch
Autoconf | 172 lines | 120 code | 19 blank | 33 comment | 22 complexity | 8db7c1a012a3ccee03a975e16cab62fe MD5 | raw file
  1. # This is a shell function library sourced by some Open vSwitch scripts.
  2. # It is not intended to be invoked on its own.
  3. # Copyright (C) 2009, 2010, 2011 Nicira Networks, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at:
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. ## ----------------- ##
  17. ## configure options ##
  18. ## ----------------- ##
  19. # All of these should be substituted by the Makefile at build time.
  20. logdir=${OVS_LOGDIR-'@LOGDIR@'} # /var/log/openvswitch
  21. rundir=${OVS_RUNDIR-'@RUNDIR@'} # /var/run/openvswitch
  22. sysconfdir=${OVS_SYSCONFDIR-'@sysconfdir@'} # /etc
  23. etcdir=$sysconfdir/openvswitch # /etc/openvswitch
  24. datadir=${OVS_PKGDATADIR-'@pkgdatadir@'} # /usr/share/openvswitch
  25. bindir=${OVS_BINDIR-'@bindir@'} # /usr/bin
  26. sbindir=${OVS_SBINDIR-'@sbindir@'} # /usr/sbin
  27. VERSION='@VERSION@'
  28. case @BUILDNR@ in
  29. [1-9]*) BUILDNR='+build@BUILDNR@' ;;
  30. *) BUILDNR= ;;
  31. esac
  32. LC_ALL=C; export LC_ALL
  33. ## ------------- ##
  34. ## LSB functions ##
  35. ## ------------- ##
  36. # Use the system's own implementations if it has any.
  37. if test -e /etc/init.d/functions; then
  38. . /etc/init.d/functions
  39. elif test -e /etc/rc.d/init.d/functions; then
  40. . /etc/rc.d/init.d/functions
  41. elif test -e /lib/lsb/init-functions; then
  42. . /lib/lsb/init-functions
  43. fi
  44. # Implement missing functions (e.g. OpenSUSE lacks 'action').
  45. if type log_success_msg >/dev/null 2>&1; then :; else
  46. log_success_msg () {
  47. printf '%s.\n' "$*"
  48. }
  49. fi
  50. if type log_failure_msg >/dev/null 2>&1; then :; else
  51. log_failure_msg () {
  52. printf '%s ... failed!\n' "$*"
  53. }
  54. fi
  55. if type log_warning_msg >/dev/null 2>&1; then :; else
  56. log_warning_msg () {
  57. printf '%s ... (warning).\n' "$*"
  58. }
  59. fi
  60. if type action >/dev/null 2>&1; then :; else
  61. action () {
  62. STRING=$1
  63. shift
  64. "$@"
  65. rc=$?
  66. if test $rc = 0; then
  67. log_success_msg "$STRING"
  68. else
  69. log_failure_msg "$STRING"
  70. fi
  71. return $rc
  72. }
  73. fi
  74. ## ------- ##
  75. ## Daemons ##
  76. ## ------- ##
  77. pid_exists () {
  78. # This is better than "kill -0" because it doesn't require permission to
  79. # send a signal (so daemon_status in particular works as non-root).
  80. test -d /proc/"$1"
  81. }
  82. start_daemon () {
  83. priority=$1
  84. shift
  85. daemon=$1
  86. # drop core files in a sensible place
  87. test -d "$DAEMON_CWD" || install -d -m 755 -o root -g root "$DAEMON_CWD"
  88. set "$@" --no-chdir
  89. cd "$DAEMON_CWD"
  90. # log file
  91. test -d "$logdir" || install -d -m 755 -o root -g root "$logdir"
  92. set "$@" --log-file="$logdir/$daemon.log"
  93. # pidfile and monitoring
  94. test -d "$rundir" || install -d -m 755 -o root -g root "$rundir"
  95. set "$@" --pidfile="$rundir/$daemon.pid"
  96. set "$@" --detach --monitor
  97. # priority
  98. if test X"$priority" != X; then
  99. set nice -n "$priority" "$@"
  100. fi
  101. action "Starting $daemon" "$@"
  102. }
  103. DAEMON_CWD=/
  104. stop_daemon () {
  105. if test -e "$rundir/$1.pid"; then
  106. if pid=`cat "$rundir/$1.pid"`; then
  107. for action in TERM .1 .25 .65 1 1 1 1 KILL 1 1 1 1 FAIL; do
  108. case $action in
  109. TERM)
  110. action "Killing $1 ($pid)" kill $pid
  111. ;;
  112. KILL)
  113. action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
  114. ;;
  115. FAIL)
  116. log_failure_msg "Killing $1 ($pid) failed"
  117. return 1
  118. ;;
  119. *)
  120. if pid_exists $pid >/dev/null 2>&1; then
  121. sleep $action
  122. else
  123. return 0
  124. fi
  125. ;;
  126. esac
  127. done
  128. fi
  129. fi
  130. log_success_msg "$1 is not running"
  131. }
  132. daemon_status () {
  133. pidfile=$rundir/$1.pid
  134. if test -e "$pidfile"; then
  135. if pid=`cat "$pidfile"`; then
  136. if pid_exists "$pid"; then
  137. echo "$1 is running with pid $pid"
  138. return 0
  139. else
  140. echo "Pidfile for $1 ($pidfile) is stale"
  141. fi
  142. else
  143. echo "Pidfile for $1 ($pidfile) exists but cannot be read"
  144. fi
  145. else
  146. echo "$1 is not running"
  147. fi
  148. return 1
  149. }
  150. daemon_is_running () {
  151. pidfile=$rundir/$1.pid
  152. test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"
  153. } >/dev/null 2>&1