init-functions 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. ${START_STOP_DAEMON} --stop --signal "$sig" --quiet $name_param || status="$?"
  97. else
  98. ${START_STOP_DAEMON} --stop --quiet $name_param || status="$?"
  99. fi
  100. else
  101. ${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_timeout_msg () {
  175. status="[ TIMEOUT ]"
  176. args="$*"
  177. if [ "$args" != "" ]; then
  178. printf "\E[33m %s \n%s \033[0m \n" "${status}" "$args "
  179. else
  180. printf "\E[33m %s \033[0m \n" "${status}"
  181. fi
  182. }
  183. # general logging message for init scripts
  184. # expects $logfile to exist within the context of where it's called
  185. log() {
  186. if [[ -z ${logfile+x} ]]; then
  187. # logfile isn't set within the context of this function call
  188. return 1
  189. fi
  190. local msg=$@
  191. local header=$( date +%Y_%m_%d_%H_%M_%S )
  192. local header="${header}: "
  193. printf "%s%s\n" "$header" "$msg" >> $logfile
  194. return 0
  195. }
  196. #log_success_msg () {
  197. # if [ -n "${1:-}" ]; then
  198. # log_begin_msg $@
  199. # fi
  200. # log_end_msg 0
  201. #}
  202. #
  203. #log_failure_msg () {
  204. # if [ -n "${1:-}" ]; then
  205. # log_begin_msg $@ "..."
  206. # fi
  207. # log_end_msg 1 || true
  208. #}
  209. log_warning_msg () {
  210. if [ -n "${1:-}" ]; then
  211. log_begin_msg $@ "..."
  212. fi
  213. log_end_msg 255 || true
  214. }
  215. #
  216. # NON-LSB HELPER FUNCTIONS
  217. #
  218. # int get_lsb_header_val (char *scriptpathname, char *key)
  219. get_lsb_header_val () {
  220. if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
  221. return 1
  222. fi
  223. LSB_S="### BEGIN INIT INFO"
  224. LSB_E="### END INIT INFO"
  225. sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
  226. }
  227. # int log_begin_message (char *message)
  228. log_begin_msg () {
  229. if [ -z "${1:-}" ]; then
  230. return 1
  231. fi
  232. echo -n "$@"
  233. }
  234. # Sample usage:
  235. # log_daemon_msg "Starting GNOME Login Manager" "gdm"
  236. #
  237. # On Debian, would output "Starting GNOME Login Manager: gdm"
  238. # On Ubuntu, would output " * Starting GNOME Login Manager..."
  239. #
  240. # If the second argument is omitted, logging suitable for use with
  241. # log_progress_msg() is used:
  242. #
  243. # log_daemon_msg "Starting remote filesystem services"
  244. #
  245. # On Debian, would output "Starting remote filesystem services:"
  246. # On Ubuntu, would output " * Starting remote filesystem services..."
  247. log_daemon_msg () {
  248. if [ -z "${1:-}" ]; then
  249. return 1
  250. fi
  251. log_daemon_msg_pre "$@"
  252. if [ -z "${2:-}" ]; then
  253. echo -n "$1:"
  254. return
  255. fi
  256. echo -n "$1: $2"
  257. log_daemon_msg_post "$@"
  258. }
  259. # #319739
  260. #
  261. # Per policy docs:
  262. #
  263. # log_daemon_msg "Starting remote file system services"
  264. # log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
  265. # log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
  266. # log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
  267. # log_end_msg 0
  268. #
  269. # You could also do something fancy with log_end_msg here based on the
  270. # return values of start-stop-daemon; this is left as an exercise for
  271. # the reader...
  272. #
  273. # On Ubuntu, one would expect log_progress_msg to be a no-op.
  274. log_progress_msg () {
  275. if [ -z "${1:-}" ]; then
  276. return 1
  277. fi
  278. echo -n " $@"
  279. }
  280. # int log_end_message (int exitstatus)
  281. log_end_msg () {
  282. # If no arguments were passed, return
  283. if [ -z "${1:-}" ]; then
  284. return 1
  285. fi
  286. retval=$1
  287. log_end_msg_pre "$@"
  288. # Only do the fancy stuff if we have an appropriate terminal
  289. # and if /usr is already mounted
  290. if log_use_fancy_output; then
  291. RED=`$TPUT setaf 1`
  292. YELLOW=`$TPUT setaf 3`
  293. NORMAL=`$TPUT op`
  294. else
  295. RED=''
  296. YELLOW=''
  297. NORMAL=''
  298. fi
  299. if [ $1 -eq 0 ]; then
  300. echo "."
  301. elif [ $1 -eq 255 ]; then
  302. /bin/echo -e " ${YELLOW}(warning).${NORMAL}"
  303. else
  304. /bin/echo -e " ${RED}failed!${NORMAL}"
  305. fi
  306. log_end_msg_post "$@"
  307. return $retval
  308. }
  309. log_action_msg () {
  310. echo "$@."
  311. }
  312. log_action_begin_msg () {
  313. echo -n "$@..."
  314. }
  315. log_action_cont_msg () {
  316. echo -n "$@..."
  317. }
  318. log_action_end_msg () {
  319. log_action_end_msg_pre "$@"
  320. if [ -z "${2:-}" ]; then
  321. end="."
  322. else
  323. end=" ($2)."
  324. fi
  325. if [ $1 -eq 0 ]; then
  326. echo "done${end}"
  327. else
  328. if log_use_fancy_output; then
  329. RED=`$TPUT setaf 1`
  330. NORMAL=`$TPUT op`
  331. /bin/echo -e "${RED}failed${end}${NORMAL}"
  332. else
  333. echo "failed${end}"
  334. fi
  335. fi
  336. log_action_end_msg_post "$@"
  337. }
  338. distrib_check () {
  339. DISTRIB_NAME=
  340. DISTRIB_VERSION=
  341. local tmp_file=/tmp/distrib_check_$$
  342. cat /etc/*release > $tmp_file
  343. if grep -q -i ubuntu $tmp_file; then
  344. DISTRIB_NAME=ubuntu
  345. DISTRIB_VERSION=$(grep -e "DISTRIB_RELEASE" $tmp_file | head -n 1 | sed -n "s/^[[:space:]]*DISTRIB_RELEASE=\(.*\)/\1/p")
  346. elif grep -q -i centos $tmp_file; then
  347. DISTRIB_NAME=centos
  348. DISTRIB_VERSION=$(grep -e "^[[:space:]]*CentOS" $tmp_file | head -1 | awk '{ print $3 }')
  349. fi
  350. DISTRIB_MAJOR_VERSION=$(echo $DISTRIB_VERSION | cut -d'.' -f1)
  351. DISTRIB_MINOR_VERSION=$(echo $DISTRIB_VERSION | cut -d'.' -f2)
  352. rm -rf $tmp_file
  353. }
  354. # Hooks for /etc/lsb-base-logging.sh
  355. log_daemon_msg_pre () { :; }
  356. log_daemon_msg_post () { :; }
  357. log_end_msg_pre () { :; }
  358. log_end_msg_post () { :; }
  359. log_action_end_msg_pre () { :; }
  360. log_action_end_msg_post () { :; }
  361. FANCYTTY=
  362. [ -e /tmp/lsb-base-logging.sh ] && . /tmp/lsb-base-logging.sh || true