init-functions 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #w002 fixes for woof.
  30. start_daemon () {
  31. local force nice pidfile exec i
  32. force=0
  33. nice=0
  34. pidfile=/dev/null
  35. OPTIND=1
  36. while getopts fn:p: opt ; do
  37. case "$opt" in
  38. f) force=1;;
  39. n) nice="$OPTARG";;
  40. p) pidfile="$OPTARG";;
  41. esac
  42. done
  43. shift $(($OPTIND - 1))
  44. if [ "$1" = '--' ]; then
  45. shift
  46. fi
  47. exec="$1"; shift
  48. if [ $force = 1 ]; then
  49. /sbin/start-stop-daemon --start --nicelevel $nice --quiet --startas $exec --pidfile /dev/null --oknodo -- "$@"
  50. elif [ $pidfile ]; then
  51. /sbin/start-stop-daemon --start --nicelevel $nice --quiet --exec $exec --oknodo --pidfile "$pidfile" -- "$@"
  52. else
  53. /sbin/start-stop-daemon --start --nicelevel $nice --quiet --exec $exec --oknodo -- "$@"
  54. fi
  55. }
  56. pidofproc () {
  57. local pidfile line i pids= status specified pid
  58. pidfile=
  59. specified=
  60. OPTIND=1
  61. while getopts p: opt ; do
  62. case "$opt" in
  63. p) pidfile="$OPTARG"; specified=1;;
  64. esac
  65. done
  66. shift $(($OPTIND - 1))
  67. if [ -z "${pidfile:-}" ]; then
  68. pidfile=/var/run/${1##*/}.pid
  69. fi
  70. if [ -f "$pidfile" ]; then
  71. read pid < "$pidfile"
  72. if [ -n "${pid:-}" ]; then
  73. if $(kill -0 "${pid:-}" 2> /dev/null); then
  74. echo "$pid"
  75. return 0
  76. elif ps "${pid:-}" >/dev/null 2>&1; then
  77. echo "$pid"
  78. return 0 # program is running, but not owned by this user
  79. else
  80. return 1 # program is dead and /var/run pid file exists
  81. fi
  82. fi
  83. fi
  84. if [ -x /bin/pidof -a ! "$specified" ]; then
  85. status="0"
  86. /bin/pidof -o %PPID $1 || status="$?"
  87. if [ "$status" = 1 ]; then
  88. return 3 # program is not running
  89. fi
  90. return 0
  91. fi
  92. return 4 # program or service is unknown
  93. }
  94. # start-stop-daemon uses the same algorithm as "pidofproc" above.
  95. killproc () {
  96. local pidfile sig status base i name_param is_term_sig
  97. pidfile=
  98. name_param=
  99. is_term_sig=no
  100. OPTIND=1
  101. while getopts p: opt ; do
  102. case "$opt" in
  103. p) pidfile="$OPTARG";;
  104. esac
  105. done
  106. shift $(($OPTIND - 1))
  107. base=${1##*/}
  108. if [ ! $pidfile ]; then
  109. pidfile=/var/run/$base.pid
  110. name_param="--name $base"
  111. fi
  112. sig=$(echo ${2:-} | sed -e 's/^-\(.*\)/\1/')
  113. sig=$(echo $sig | sed -e 's/^SIG\(.*\)/\1/')
  114. if [ -z "$sig" -o "$sig" = 15 -o "$sig" = TERM ]; then
  115. is_term_sig=yes
  116. fi
  117. status=0
  118. if [ ! "$is_term_sig" = yes ]; then
  119. if [ -n "$sig" ]; then
  120. /sbin/start-stop-daemon --stop --signal "$sig" --pidfile "$pidfile" --quiet $name_param || status="$?"
  121. else
  122. /sbin/start-stop-daemon --stop --pidfile "$pidfile" --quiet $name_param || status="$?"
  123. fi
  124. else
  125. /sbin/start-stop-daemon --stop --pidfile "$pidfile" --retry 5 --quiet --oknodo $name_param || status="$?"
  126. fi
  127. if [ "$status" = 1 ]; then
  128. if [ -n "$sig" ]; then
  129. return 0
  130. fi
  131. return 3 # program is not running
  132. fi
  133. if [ "$status" = 0 -a "$is_term_sig" = yes ]; then
  134. pidofproc -p $pidfile "$1" >/dev/null || rm -f "$pidfile"
  135. fi
  136. return 0
  137. }
  138. # Return LSB status
  139. status_of_proc () {
  140. local pidfile daemon name status
  141. pidfile=
  142. OPTIND=1
  143. while getopts p: opt ; do
  144. case "$opt" in
  145. p) pidfile="$OPTARG";;
  146. esac
  147. done
  148. shift $(($OPTIND - 1))
  149. if [ -n "$pidfile" ]; then
  150. pidfile="-p $pidfile"
  151. fi
  152. daemon="$1"
  153. name="$2"
  154. status="0"
  155. pidofproc $pidfile $daemon >/dev/null || status="$?"
  156. if [ "$status" = 0 ]; then
  157. log_success_msg "$name is running."
  158. return 0
  159. else
  160. log_failure_msg "$name is not running."
  161. return $status
  162. fi
  163. }
  164. log_use_fancy_output () {
  165. TPUT=/usr/bin/tput
  166. EXPR=/usr/bin/expr
  167. if [ -t 1 ] && [ "x$TERM" != "" ] && [ "x$TERM" != "xdumb" ] && [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1 && $TPUT setaf 1 >/dev/null 2>&1; then
  168. [ -z $FANCYTTY ] && FANCYTTY=1 || true
  169. else
  170. FANCYTTY=0
  171. fi
  172. case "$FANCYTTY" in
  173. 1|Y|yes|true) true;;
  174. *) false;;
  175. esac
  176. }
  177. log_success_msg () {
  178. echo "$@" >> /tmp/bootsysinit.log #w002
  179. }
  180. log_failure_msg () {
  181. if log_use_fancy_output; then
  182. RED=`$TPUT setaf 1`
  183. NORMAL=`$TPUT op`
  184. /bin/echo -e "${RED}*${NORMAL} $@" >> /tmp/bootsysinit.log #w002
  185. else
  186. echo "$@" >> /tmp/bootsysinit.log #w002
  187. fi
  188. }
  189. log_warning_msg () {
  190. if log_use_fancy_output; then
  191. YELLOW=`$TPUT setaf 3`
  192. NORMAL=`$TPUT op`
  193. /bin/echo -e "${YELLOW}*${NORMAL} $@" >> /tmp/bootsysinit.log #w002
  194. else
  195. echo "$@" >> /tmp/bootsysinit.log #w002
  196. fi
  197. }
  198. #
  199. # NON-LSB HELPER FUNCTIONS
  200. #
  201. # int get_lsb_header_val (char *scriptpathname, char *key)
  202. get_lsb_header_val () {
  203. if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
  204. return 1
  205. fi
  206. LSB_S="### BEGIN INIT INFO"
  207. LSB_E="### END INIT INFO"
  208. sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
  209. }
  210. # int log_begin_message (char *message)
  211. log_begin_msg () {
  212. if [ -z "${1:-}" ]; then
  213. return 1
  214. fi
  215. echo -n "$@" >> /tmp/bootsysinit.log #w002
  216. }
  217. # Sample usage:
  218. # log_daemon_msg "Starting GNOME Login Manager" "gdm"
  219. #
  220. # On Debian, would output "Starting GNOME Login Manager: gdm"
  221. # On Ubuntu, would output " * Starting GNOME Login Manager..."
  222. #
  223. # If the second argument is omitted, logging suitable for use with
  224. # log_progress_msg() is used:
  225. #
  226. # log_daemon_msg "Starting remote filesystem services"
  227. #
  228. # On Debian, would output "Starting remote filesystem services:"
  229. # On Ubuntu, would output " * Starting remote filesystem services..."
  230. log_daemon_msg () {
  231. if [ -z "${1:-}" ]; then
  232. return 1
  233. fi
  234. log_daemon_msg_pre "$@"
  235. if [ -z "${2:-}" ]; then
  236. echo -n "$1:"
  237. return
  238. fi
  239. echo -n "$1: $2"
  240. log_daemon_msg_post "$@"
  241. }
  242. # #319739
  243. #
  244. # Per policy docs:
  245. #
  246. # log_daemon_msg "Starting remote file system services"
  247. # log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
  248. # log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
  249. # log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
  250. # log_end_msg 0
  251. #
  252. # You could also do something fancy with log_end_msg here based on the
  253. # return values of start-stop-daemon; this is left as an exercise for
  254. # the reader...
  255. #
  256. # On Ubuntu, one would expect log_progress_msg to be a no-op.
  257. log_progress_msg () {
  258. if [ -z "${1:-}" ]; then
  259. return 1
  260. fi
  261. echo -n " $@" >> /tmp/bootsysinit.log #w002
  262. }
  263. # int log_end_message (int exitstatus)
  264. log_end_msg () {
  265. # If no arguments were passed, return
  266. if [ -z "${1:-}" ]; then
  267. return 1
  268. fi
  269. log_end_msg_pre "$@"
  270. # Only do the fancy stuff if we have an appropriate terminal
  271. # and if /usr is already mounted
  272. if log_use_fancy_output; then
  273. RED=`$TPUT setaf 1`
  274. NORMAL=`$TPUT op`
  275. if [ $1 -eq 0 ]; then
  276. echo "." >> /tmp/bootsysinit.log #w002
  277. else
  278. /bin/echo -e " ${RED}failed!${NORMAL}" >> /tmp/bootsysinit.log #w002
  279. fi
  280. else
  281. if [ $1 -eq 0 ]; then
  282. echo "." >> /tmp/bootsysinit.log #w002
  283. else
  284. echo " failed!" >> /tmp/bootsysinit.log #w002
  285. fi
  286. fi
  287. log_end_msg_post "$@"
  288. return $1
  289. }
  290. log_action_msg () {
  291. echo "$@." >> /tmp/bootsysinit.log #w002
  292. }
  293. log_action_begin_msg () {
  294. echo -n "$@..." >> /tmp/bootsysinit.log #w002
  295. }
  296. log_action_cont_msg () {
  297. echo -n "$@..." >> /tmp/bootsysinit.log #w002
  298. }
  299. log_action_end_msg () {
  300. log_action_end_msg_pre "$@"
  301. if [ -z "${2:-}" ]; then
  302. end="."
  303. else
  304. end=" ($2)."
  305. fi
  306. if [ $1 -eq 0 ]; then
  307. echo "done${end}" >> /tmp/bootsysinit.log #w002
  308. else
  309. if log_use_fancy_output; then
  310. RED=`$TPUT setaf 1`
  311. NORMAL=`$TPUT op`
  312. /bin/echo -e "${RED}failed${end}${NORMAL}" >> /tmp/bootsysinit.log #w002
  313. else
  314. echo "failed${end}" >> /tmp/bootsysinit.log #w002
  315. fi
  316. fi
  317. log_action_end_msg_post "$@"
  318. }
  319. # Hooks for /etc/lsb-base-logging.sh
  320. log_daemon_msg_pre () { :; }
  321. log_daemon_msg_post () { :; }
  322. log_end_msg_pre () { :; }
  323. log_end_msg_post () { :; }
  324. log_action_end_msg_pre () { :; }
  325. log_action_end_msg_post () { :; }
  326. FANCYTTY=
  327. [ -e /etc/lsb-base-logging.sh ] && . /etc/lsb-base-logging.sh || true