init-functions 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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}" ]]; 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. local thispid=$$
  194. (printf "%d %s%s\n" "$thispid" "$header" "$msg" >> $logfile) 2> /dev/null
  195. if [[ $? -ne 0 ]]; then
  196. echo "unable to write to ${logfile}" 1>&2
  197. return 1
  198. fi
  199. return 0
  200. }
  201. #log_success_msg () {
  202. # if [ -n "${1:-}" ]; then
  203. # log_begin_msg $@
  204. # fi
  205. # log_end_msg 0
  206. #}
  207. #
  208. #log_failure_msg () {
  209. # if [ -n "${1:-}" ]; then
  210. # log_begin_msg $@ "..."
  211. # fi
  212. # log_end_msg 1 || true
  213. #}
  214. log_warning_msg () {
  215. if [ -n "${1:-}" ]; then
  216. log_begin_msg $@ "..."
  217. fi
  218. log_end_msg 255 || true
  219. }
  220. #
  221. # NON-LSB HELPER FUNCTIONS
  222. #
  223. # int get_lsb_header_val (char *scriptpathname, char *key)
  224. get_lsb_header_val () {
  225. if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
  226. return 1
  227. fi
  228. LSB_S="### BEGIN INIT INFO"
  229. LSB_E="### END INIT INFO"
  230. sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
  231. }
  232. # int log_begin_message (char *message)
  233. log_begin_msg () {
  234. if [ -z "${1:-}" ]; then
  235. return 1
  236. fi
  237. echo -n "$@"
  238. }
  239. # Sample usage:
  240. # log_daemon_msg "Starting GNOME Login Manager" "gdm"
  241. #
  242. # On Debian, would output "Starting GNOME Login Manager: gdm"
  243. # On Ubuntu, would output " * Starting GNOME Login Manager..."
  244. #
  245. # If the second argument is omitted, logging suitable for use with
  246. # log_progress_msg() is used:
  247. #
  248. # log_daemon_msg "Starting remote filesystem services"
  249. #
  250. # On Debian, would output "Starting remote filesystem services:"
  251. # On Ubuntu, would output " * Starting remote filesystem services..."
  252. log_daemon_msg () {
  253. if [ -z "${1:-}" ]; then
  254. return 1
  255. fi
  256. log_daemon_msg_pre "$@"
  257. if [ -z "${2:-}" ]; then
  258. echo -n "$1:"
  259. return
  260. fi
  261. echo -n "$1: $2"
  262. log_daemon_msg_post "$@"
  263. }
  264. # #319739
  265. #
  266. # Per policy docs:
  267. #
  268. # log_daemon_msg "Starting remote file system services"
  269. # log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
  270. # log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
  271. # log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
  272. # log_end_msg 0
  273. #
  274. # You could also do something fancy with log_end_msg here based on the
  275. # return values of start-stop-daemon; this is left as an exercise for
  276. # the reader...
  277. #
  278. # On Ubuntu, one would expect log_progress_msg to be a no-op.
  279. log_progress_msg () {
  280. if [ -z "${1:-}" ]; then
  281. return 1
  282. fi
  283. echo -n " $@"
  284. }
  285. # int log_end_message (int exitstatus)
  286. log_end_msg () {
  287. # If no arguments were passed, return
  288. if [ -z "${1:-}" ]; then
  289. return 1
  290. fi
  291. retval=$1
  292. log_end_msg_pre "$@"
  293. # Only do the fancy stuff if we have an appropriate terminal
  294. # and if /usr is already mounted
  295. if log_use_fancy_output; then
  296. RED=`$TPUT setaf 1`
  297. YELLOW=`$TPUT setaf 3`
  298. NORMAL=`$TPUT op`
  299. else
  300. RED=''
  301. YELLOW=''
  302. NORMAL=''
  303. fi
  304. if [ $1 -eq 0 ]; then
  305. echo "."
  306. elif [ $1 -eq 255 ]; then
  307. /bin/echo -e " ${YELLOW}(warning).${NORMAL}"
  308. else
  309. /bin/echo -e " ${RED}failed!${NORMAL}"
  310. fi
  311. log_end_msg_post "$@"
  312. return $retval
  313. }
  314. log_action_msg () {
  315. echo "$@."
  316. }
  317. log_action_begin_msg () {
  318. echo -n "$@..."
  319. }
  320. log_action_cont_msg () {
  321. echo -n "$@..."
  322. }
  323. log_action_end_msg () {
  324. log_action_end_msg_pre "$@"
  325. if [ -z "${2:-}" ]; then
  326. end="."
  327. else
  328. end=" ($2)."
  329. fi
  330. if [ $1 -eq 0 ]; then
  331. echo "done${end}"
  332. else
  333. if log_use_fancy_output; then
  334. RED=`$TPUT setaf 1`
  335. NORMAL=`$TPUT op`
  336. /bin/echo -e "${RED}failed${end}${NORMAL}"
  337. else
  338. echo "failed${end}"
  339. fi
  340. fi
  341. log_action_end_msg_post "$@"
  342. }
  343. check_getopt () {
  344. `getopt -T 1>/dev/null 2>&1`
  345. if [ $? -ne 4 ]; then
  346. echo "Incompatible version of getopt"
  347. os=$(uname)
  348. if [ "${os}" == "Darwin" ]; then
  349. echo "Update from an external source"
  350. echo "Example: brew install gnu-getopt"
  351. elif [ "${os}" == "Linux" ]; then
  352. echo "insure the environment variable GETOPT_COMPATIBLE is unset"
  353. else
  354. echo "getopt must support long options"
  355. fi
  356. exit 3
  357. fi
  358. }
  359. distrib_check () {
  360. DISTRIB_NAME=
  361. DISTRIB_VERSION=
  362. local tmp_file=/tmp/distrib_check_$$
  363. cat /etc/*release > $tmp_file
  364. if grep -q -i ubuntu $tmp_file; then
  365. DISTRIB_NAME=ubuntu
  366. DISTRIB_VERSION=$(grep -e "DISTRIB_RELEASE" $tmp_file | head -n 1 | sed -n "s/^[[:space:]]*DISTRIB_RELEASE=\(.*\)/\1/p")
  367. elif grep -q -i centos $tmp_file; then
  368. DISTRIB_NAME=centos
  369. DISTRIB_VERSION=$(grep -e "^[[:space:]]*CentOS" $tmp_file | head -1 | awk '{ print $3 }')
  370. fi
  371. DISTRIB_MAJOR_VERSION=$(echo $DISTRIB_VERSION | cut -d'.' -f1)
  372. DISTRIB_MINOR_VERSION=$(echo $DISTRIB_VERSION | cut -d'.' -f2)
  373. rm -rf $tmp_file
  374. }
  375. # Hooks for /etc/lsb-base-logging.sh
  376. log_daemon_msg_pre () { :; }
  377. log_daemon_msg_post () { :; }
  378. log_end_msg_pre () { :; }
  379. log_end_msg_post () { :; }
  380. log_action_end_msg_pre () { :; }
  381. log_action_end_msg_post () { :; }
  382. FANCYTTY=
  383. [ -e /tmp/lsb-base-logging.sh ] && . /tmp/lsb-base-logging.sh || true