rc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #! /bin/sh
  2. ## Copyright (c) 2015 Minoca Corp.
  3. ##
  4. ## This file is licensed under the terms of the GNU General Public License
  5. ## version 3. Alternative licensing terms are available. Contact
  6. ## info@minocacorp.com for details. See the LICENSE file at the root of this
  7. ## project for complete licensing information..
  8. ##
  9. ## Script Name:
  10. ##
  11. ## rc <runlevel>
  12. ##
  13. ## Abstract:
  14. ##
  15. ## This script runs system initialization scripts. Inspired by the rc
  16. ## script by Miqual van Smoorenburg and Bruce Perens.
  17. ##
  18. ## Author:
  19. ##
  20. ## Evan Green 23-Mar-2015
  21. ##
  22. ## Environment:
  23. ##
  24. ## System Boot
  25. ##
  26. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  27. export PATH
  28. me="$0"
  29. umask 022
  30. ##
  31. ## Catch exits that happen too early.
  32. ##
  33. on_exit () {
  34. echo "Error: $me exited prematurely."
  35. }
  36. trap on_exit EXIT
  37. ##
  38. ## Ignore certain signals.
  39. ##
  40. trap ":" INT QUIT TSTP
  41. ##
  42. ## Get the next and previous runlevels.
  43. ##
  44. runlevel=$RUNLEVEL
  45. if [ "x$1" != "x" ] ; then
  46. runlevel="$1"
  47. fi
  48. if [ "x$runlevel" == "x" ] ; then
  49. echo "Usage: $me <runlevel>" >&2
  50. exit 1
  51. fi
  52. previous=$PREVLEVEL
  53. if [ "x$previous" == "x" ] ; then
  54. previous=N
  55. fi
  56. export runlevel previous
  57. if [ -f /etc/default/rcS ] ; then
  58. . /etc/default/rcS
  59. fi
  60. export VERBOSE
  61. if [ -f /etc/init.d/init-functions ] ; then
  62. . /etc/init.d/init-functions
  63. else
  64. log_action_msg () { echo $@; }
  65. log_failure_msg () { echo $@; }
  66. log_warning_msg () { echo $@; }
  67. fi
  68. startup () {
  69. action="$1"
  70. shift
  71. scripts="$@"
  72. for script in $scripts; do
  73. if [ -n "$debug" ] ; then
  74. echo "$script" "$action"
  75. fi
  76. "$script" $action
  77. done
  78. }
  79. if [ -d /etc/rc$runlevel.d ] ; then
  80. case "$runlevel" in
  81. 0|6) ACTION=stop ;;
  82. S|s) ACTION=start ;;
  83. *) ACTION=start ;;
  84. esac
  85. if [ "$previous" != "N" ] ; then
  86. CURLEVEL=""
  87. for script in /etc/rc$runlevel.d/K* ; do
  88. ##
  89. ## Extract the numeric part by removing the path prefix, and the
  90. ## non-numeric suffix.
  91. ##
  92. level=${script#/etc/rc$runlevel.d/K}
  93. level=${script%%[a-zA-Z]*}
  94. if [ "$level" = "$CURLEVEL" ] ; then
  95. continue
  96. fi
  97. CURLEVEL=$level
  98. SCRIPTS=""
  99. for i in /etc/rc$runlevel.d/K$level*; do
  100. ##
  101. ## Skip the script if it doesn't exist.
  102. ##
  103. [ ! -f $i ] && continue
  104. ##
  105. ## Try to find a start and stop script in the previous
  106. ## run-level for this script.
  107. ##
  108. suffix=${i#/etc/rc$runlevel.d/K[0-9][0-9]}
  109. previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
  110. previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  111. ##
  112. ## If there is a stop script in the previous runlevel but no
  113. ## start script, don't stop the service.
  114. ##
  115. [ -f $previous_stop ] && [ ! -f $previous_start ] && continue
  116. ##
  117. ## Add this to the list of services to stop.
  118. ##
  119. SCRIPTS="$SCRIPTS $i"
  120. done
  121. ##
  122. ## Actually stop the services.
  123. ##
  124. startup stop $SCRIPTS
  125. done
  126. fi
  127. ##
  128. ## Run all the start scripts for this runlevel.
  129. ##
  130. CURLEVEL=""
  131. for script in /etc/rc$runlevel.d/S* ; do
  132. ##
  133. ## Get the numerical portion by removing the path prefix and
  134. ## non-numeric suffix.
  135. ##
  136. level=${script#/etc/rc$runlevel.d/S}
  137. level=${level%%[a-zA-Z]*}
  138. if [ "$level" = "$CURLEVEL" ] ; then
  139. continue
  140. fi
  141. CURLEVEL=$level
  142. SCRIPTS=""
  143. for i in /etc/rc$runlevel.d/S$level* ; do
  144. ##
  145. ## Skip files that don't exist.
  146. ##
  147. [ ! -f $i ] && continue
  148. suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
  149. if [ "$previous" != N ] ; then
  150. ##
  151. ## Find a stop script in this runlevel and a start script in
  152. ## the previous.
  153. ##
  154. stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
  155. previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  156. ##
  157. ## If there is a start script in the previous runlevel and no
  158. ## stop script in this level, don't start the service.
  159. ##
  160. if [ "$ACTION" = start ] ; then
  161. [ -f $previous_start ] && [ ! -f $stop ] && continue
  162. else
  163. ##
  164. ## If there is a stop script in the previous runlevel and
  165. ## no start script, don't stop the service.
  166. ##
  167. previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
  168. [ -f $previous_stop ] && [ ! -f $previous_start ] && \
  169. continue
  170. fi
  171. fi
  172. ##
  173. ## Add the script to the list of things to do.
  174. ##
  175. SCRIPTS="$SCRIPTS $i"
  176. done
  177. ##
  178. ## Actually run the action.
  179. ##
  180. startup $ACTION $SCRIPTS
  181. done
  182. fi
  183. ##
  184. ## Exit gracefully.
  185. ##
  186. trap - EXIT
  187. exit 0