init-functions 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. # /lib/lsb/init-functions for Debian -*- shell-script -*-
  2. #
  3. #Copyright (c) 2002-08 Chris Lawrence
  4. #All rights reserved.
  5. #
  6. #Redistribution and use in source and binary forms, with or without
  7. #modification, are permitted provided that the following conditions
  8. #are met:
  9. #1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. #2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #3. Neither the name of the author nor the names of other contributors
  15. # may be used to endorse or promote products derived from this software
  16. # without specific prior written permission.
  17. #
  18. #THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. #IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. #WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. #ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
  22. #LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. #CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. #SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  25. #BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. #WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  27. #OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  28. #EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. pidofproc () {
  30. local pidfile line i pids= status specified pid
  31. pidfile=
  32. specified=
  33. OPTIND=1
  34. while getopts p: opt ; do
  35. case "$opt" in
  36. p) pidfile="$OPTARG"; specified=1;;
  37. esac
  38. done
  39. shift $(($OPTIND - 1))
  40. base=${1##*/}
  41. if [ ! "$specified" ]; then
  42. pidfile="/var/run/$base.pid"
  43. fi
  44. if [ -n "${pidfile:-}" -a -r "$pidfile" ]; then
  45. read pid < "$pidfile"
  46. if [ -n "${pid:-}" ]; then
  47. if $(kill -0 "${pid:-}" 2> /dev/null); then
  48. echo "$pid"
  49. return 0
  50. elif ps "${pid:-}" >/dev/null 2>&1; then
  51. echo "$pid"
  52. return 0 # program is running, but not owned by this user
  53. else
  54. return 1 # program is dead and /var/run pid file exists
  55. fi
  56. fi
  57. fi
  58. which_pidof
  59. if [ -x ${PIDOF} -a ! "$specified" ]; then
  60. status="0"
  61. ${PIDOF} -o %PPID -x $1 || status="$?"
  62. if [ "$status" = 1 ]; then
  63. return 3 # program is not running
  64. fi
  65. return 0
  66. fi
  67. return 4 # Unable to determine status
  68. }
  69. # start-stop-daemon uses the same algorithm as "pidofproc" above.
  70. killproc () {
  71. local pidfile sig status base i name_param is_term_sig
  72. pidfile=
  73. name_param=
  74. is_term_sig=no
  75. OPTIND=1
  76. while getopts p: opt ; do
  77. case "$opt" in
  78. p) pidfile="$OPTARG";;
  79. esac
  80. done
  81. shift $(($OPTIND - 1))
  82. base=${1##*/}
  83. if [ ! $pidfile ]; then
  84. name_param="--name $base --pidfile /var/run/$base.pid"
  85. else
  86. name_param="--pidfile $pidfile"
  87. fi
  88. sig=$(echo ${2:-} | sed -e 's/^-\(.*\)/\1/')
  89. sig=$(echo $sig | sed -e 's/^SIG\(.*\)/\1/')
  90. if [ -z "$sig" -o "$sig" = 15 -o "$sig" = TERM ]; then
  91. is_term_sig=yes
  92. fi
  93. status=0
  94. if [ ! "$is_term_sig" = yes ]; then
  95. if [ -n "$sig" ]; then
  96. /sbin/start-stop-daemon --stop --signal "$sig" --quiet $name_param || status="$?"
  97. else
  98. /sbin/start-stop-daemon --stop --quiet $name_param || status="$?"
  99. fi
  100. else
  101. /sbin/start-stop-daemon --stop --quiet --oknodo $name_param || status="$?"
  102. fi
  103. if [ "$status" = 1 ]; then
  104. if [ -n "$sig" ]; then
  105. return 0
  106. fi
  107. return 3 # program is not running
  108. fi
  109. if [ "$status" = 0 -a "$is_term_sig" = yes -a "$pidfile" ]; then
  110. pidofproc -p "$pidfile" "$1" >/dev/null || rm -f "$pidfile"
  111. fi
  112. return 0
  113. }
  114. # Return LSB status
  115. status_of_proc () {
  116. local pidfile daemon name status
  117. pidfile=
  118. OPTIND=1
  119. while getopts p: opt ; do
  120. case "$opt" in
  121. p) pidfile="$OPTARG";;
  122. esac
  123. done
  124. shift $(($OPTIND - 1))
  125. if [ -n "$pidfile" ]; then
  126. pidfile="-p $pidfile"
  127. fi
  128. daemon="$1"
  129. name="$2"
  130. status="0"
  131. pidofproc $pidfile $daemon >/dev/null || status="$?"
  132. if [ "$status" = 0 ]; then
  133. log_success_msg "$name is running"
  134. return 0
  135. elif [ "$status" = 4 ]; then
  136. log_failure_msg "could not access PID file for $name"
  137. return $status
  138. else
  139. log_failure_msg "$name is not running"
  140. return $status
  141. fi
  142. }
  143. log_use_fancy_output () {
  144. TPUT=/usr/bin/tput
  145. EXPR=/usr/bin/expr
  146. if [ -t 1 ] && [ "x${TERM:-}" != "x" ] && [ "x${TERM:-}" != "xdumb" ] && [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1 && $TPUT setaf 1 >/dev/null 2>&1; then
  147. [ -z $FANCYTTY ] && FANCYTTY=1 || true
  148. else
  149. FANCYTTY=0
  150. fi
  151. case "$FANCYTTY" in
  152. 1|Y|yes|true) true;;
  153. *) false;;
  154. esac
  155. }
  156. log_success_msg () {
  157. status="[ OK ]"
  158. args="$*"
  159. if [ "$args" != "" ]; then
  160. printf "\E[32m %s \n%s \033[0m \n" "${status}" "$args "
  161. else
  162. printf "\E[32m %s \033[0m \n" "${status}"
  163. fi
  164. }
  165. log_failure_msg () {
  166. status="[FAILED]"
  167. args="$*"
  168. if [ "$args" != "" ]; then
  169. printf "\E[31m %s \n%s \033[0m \n" "${status}" "$args "
  170. else
  171. printf "\E[31m %s \033[0m \n" "${status}"
  172. fi
  173. }
  174. #log_success_msg () {
  175. # if [ -n "${1:-}" ]; then
  176. # log_begin_msg $@
  177. # fi
  178. # log_end_msg 0
  179. #}
  180. #
  181. #log_failure_msg () {
  182. # if [ -n "${1:-}" ]; then
  183. # log_begin_msg $@ "..."
  184. # fi
  185. # log_end_msg 1 || true
  186. #}
  187. log_warning_msg () {
  188. if [ -n "${1:-}" ]; then
  189. log_begin_msg $@ "..."
  190. fi
  191. log_end_msg 255 || true
  192. }
  193. #
  194. # NON-LSB HELPER FUNCTIONS
  195. #
  196. # int get_lsb_header_val (char *scriptpathname, char *key)
  197. get_lsb_header_val () {
  198. if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
  199. return 1
  200. fi
  201. LSB_S="### BEGIN INIT INFO"
  202. LSB_E="### END INIT INFO"
  203. sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
  204. }
  205. # int log_begin_message (char *message)
  206. log_begin_msg () {
  207. if [ -z "${1:-}" ]; then
  208. return 1
  209. fi
  210. echo -n "$@"
  211. }
  212. # Sample usage:
  213. # log_daemon_msg "Starting GNOME Login Manager" "gdm"
  214. #
  215. # On Debian, would output "Starting GNOME Login Manager: gdm"
  216. # On Ubuntu, would output " * Starting GNOME Login Manager..."
  217. #
  218. # If the second argument is omitted, logging suitable for use with
  219. # log_progress_msg() is used:
  220. #
  221. # log_daemon_msg "Starting remote filesystem services"
  222. #
  223. # On Debian, would output "Starting remote filesystem services:"
  224. # On Ubuntu, would output " * Starting remote filesystem services..."
  225. log_daemon_msg () {
  226. if [ -z "${1:-}" ]; then
  227. return 1
  228. fi
  229. log_daemon_msg_pre "$@"
  230. if [ -z "${2:-}" ]; then
  231. echo -n "$1:"
  232. return
  233. fi
  234. echo -n "$1: $2"
  235. log_daemon_msg_post "$@"
  236. }
  237. # #319739
  238. #
  239. # Per policy docs:
  240. #
  241. # log_daemon_msg "Starting remote file system services"
  242. # log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
  243. # log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
  244. # log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
  245. # log_end_msg 0
  246. #
  247. # You could also do something fancy with log_end_msg here based on the
  248. # return values of start-stop-daemon; this is left as an exercise for
  249. # the reader...
  250. #
  251. # On Ubuntu, one would expect log_progress_msg to be a no-op.
  252. log_progress_msg () {
  253. if [ -z "${1:-}" ]; then
  254. return 1
  255. fi
  256. echo -n " $@"
  257. }
  258. # int log_end_message (int exitstatus)
  259. log_end_msg () {
  260. # If no arguments were passed, return
  261. if [ -z "${1:-}" ]; then
  262. return 1
  263. fi
  264. retval=$1
  265. log_end_msg_pre "$@"
  266. # Only do the fancy stuff if we have an appropriate terminal
  267. # and if /usr is already mounted
  268. if log_use_fancy_output; then
  269. RED=`$TPUT setaf 1`
  270. YELLOW=`$TPUT setaf 3`
  271. NORMAL=`$TPUT op`
  272. else
  273. RED=''
  274. YELLOW=''
  275. NORMAL=''
  276. fi
  277. if [ $1 -eq 0 ]; then
  278. echo "."
  279. elif [ $1 -eq 255 ]; then
  280. /bin/echo -e " ${YELLOW}(warning).${NORMAL}"
  281. else
  282. /bin/echo -e " ${RED}failed!${NORMAL}"
  283. fi
  284. log_end_msg_post "$@"
  285. return $retval
  286. }
  287. log_action_msg () {
  288. echo "$@."
  289. }
  290. log_action_begin_msg () {
  291. echo -n "$@..."
  292. }
  293. log_action_cont_msg () {
  294. echo -n "$@..."
  295. }
  296. log_action_end_msg () {
  297. log_action_end_msg_pre "$@"
  298. if [ -z "${2:-}" ]; then
  299. end="."
  300. else
  301. end=" ($2)."
  302. fi
  303. if [ $1 -eq 0 ]; then
  304. echo "done${end}"
  305. else
  306. if log_use_fancy_output; then
  307. RED=`$TPUT setaf 1`
  308. NORMAL=`$TPUT op`
  309. /bin/echo -e "${RED}failed${end}${NORMAL}"
  310. else
  311. echo "failed${end}"
  312. fi
  313. fi
  314. log_action_end_msg_post "$@"
  315. }
  316. # Hooks for /etc/lsb-base-logging.sh
  317. log_daemon_msg_pre () { :; }
  318. log_daemon_msg_post () { :; }
  319. log_end_msg_pre () { :; }
  320. log_end_msg_post () { :; }
  321. log_action_end_msg_pre () { :; }
  322. log_action_end_msg_post () { :; }
  323. FANCYTTY=
  324. [ -e /tmp/lsb-base-logging.sh ] && . /tmp/lsb-base-logging.sh || true