1
0

rc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #!/bin/bash
  2. ########################################################################
  3. # Begin rc
  4. #
  5. # Description : Main Run Level Control Script
  6. #
  7. # Authors : Gerard Beekmans - gerard@linuxfromscratch.org
  8. # : DJ Lucas - dj@linuxfromscratch.org
  9. # Update : Bruce Dubbs - bdubbs@linuxfromscratch.org
  10. #
  11. # Version : LFS 7.0
  12. #
  13. ########################################################################
  14. . /lib/lsb/init-functions
  15. function print_error_msg()
  16. {
  17. log_failure_msg
  18. # $i is set when called
  19. MSG="FAILURE:\n\nYou should not be reading this error message.\n\n"
  20. MSG="${MSG}It means that an unforeseen error took place in\n"
  21. MSG="${MSG}${i},\n"
  22. MSG="${MSG}which exited with a return value of ${error_value}.\n"
  23. MSG="${MSG}If you're able to track this error down to a bug in one of\n"
  24. MSG="${MSG}the files provided by the files provided by\n"
  25. MSG="${MSG}the ${DISDRI_MINI} book, please be so kind to inform us at\n"
  26. MSG="${MSG}${DISTRO_CONTACT}.\n"
  27. log_failure_msg "${MSG}"
  28. log_info_msg "Press Enter to continue..."
  29. wait_for_user
  30. }
  31. function check_script_status()
  32. {
  33. # $i is set when called
  34. if [ ! -f ${i} ]; then
  35. log_warning_msg "${i} is not a valid symlink."
  36. continue
  37. fi
  38. if [ ! -x ${i} ]; then
  39. log_warning_msg "${i} is not executable, skipping."
  40. continue
  41. fi
  42. }
  43. function run()
  44. {
  45. if [ -z $interactive ]; then
  46. ${1} ${2}
  47. return $?
  48. fi
  49. while true; do
  50. read -p "Run ${1} ${2} (Yes/no/continue)? " -n 1 runit
  51. echo
  52. case ${runit} in
  53. c | C)
  54. interactive=""
  55. ${i} ${2}
  56. ret=${?}
  57. break;
  58. ;;
  59. n | N)
  60. return 0
  61. ;;
  62. y | Y)
  63. ${i} ${2}
  64. ret=${?}
  65. break
  66. ;;
  67. esac
  68. done
  69. return $ret
  70. }
  71. # Read any local settings/overrides
  72. [ -r /etc/sysconfig/rc.site ] && source /etc/sysconfig/rc.site
  73. DISTRO=${DISTRO:-"Linux From Scratch"}
  74. DISTRO_CONTACT=${DISTRO_CONTACT:-"lfs-dev@linuxfromscratch.org (Registration required)"}
  75. DISTRO_MINI=${DISTRO_MINI:-"LFS"}
  76. # These 3 signals will not cause our script to exit
  77. trap "" INT QUIT TSTP
  78. [ "${1}" != "" ] && runlevel=${1}
  79. if [ "${runlevel}" == "" ]; then
  80. echo "Usage: ${0} <runlevel>" >&2
  81. exit 1
  82. fi
  83. previous=${PREVLEVEL}
  84. [ "${previous}" == "" ] && previous=N
  85. if [ ! -d /etc/rc.d/rc${runlevel}.d ]; then
  86. log_info_msg "/etc/rc.d/rc${runlevel}.d does not exist.\n"
  87. exit 1
  88. fi
  89. if [ "$runlevel" == "6" -o "$runlevel" == "0" ]; then IPROMPT="no"; fi
  90. # Note: In ${LOGLEVEL:-7}, it is ':' 'dash' '7', not minus 7
  91. if [ "$runlevel" == "S" ]; then dmesg -n "${LOGLEVEL:-7}"; fi
  92. if [ "${IPROMPT}" == "yes" -a "${runlevel}" == "S" ]; then
  93. # dcol and icol are spaces before the message to center the
  94. # message on screen.
  95. wcol=$(( ( ${COLUMNS} - ${wlen} ) / 2 ))
  96. icol=$(( ( ${COLUMNS} - ${ilen} ) / 2 ))
  97. echo -e "\n\n"
  98. echo -e "\\033[${wcol}G${welcome_message}"
  99. echo -e "\\033[${icol}G${i_message}${NORMAL}"
  100. echo ""
  101. read -t "${itime}" -n 1 interactive 2>&1 > /dev/null
  102. fi
  103. # Make lower case
  104. [ "${interactive}" == "I" ] && interactive="i"
  105. [ "${interactive}" != "i" ] && interactive=""
  106. # Read the state file if it exists from runlevel S
  107. [ -r /var/run/interactive ] && source /var/run/interactive
  108. # Attempt to stop all services started by the previous runlevel,
  109. # and killed in this runlevel
  110. if [ "${previous}" != "N" ]; then
  111. for i in $(ls -v /etc/rc.d/rc${runlevel}.d/K* 2> /dev/null)
  112. do
  113. check_script_status
  114. suffix=${i#/etc/rc.d/rc$runlevel.d/K[0-9][0-9]}
  115. prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
  116. sysinit_start=/etc/rc.d/rcS.d/S[0-9][0-9]$suffix
  117. if [ "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
  118. if [ ! -f ${prev_start} -a ! -f ${sysinit_start} ]; then
  119. MSG="WARNING:\n\n${i} can't be "
  120. MSG="${MSG}executed because it was not "
  121. MSG="${MSG}not started in the previous "
  122. MSG="${MSG}runlevel (${previous})."
  123. log_warning_msg "$MSG"
  124. continue
  125. fi
  126. fi
  127. run ${i} stop
  128. error_value=${?}
  129. if [ "${error_value}" != "0" ]; then print_error_msg; fi
  130. done
  131. fi
  132. if [ "${previous}" == "N" ]; then export IN_BOOT=1; fi
  133. if [ "$runlevel" == "6" -a -n "${FASTBOOT}" ]; then
  134. touch /fastboot
  135. fi
  136. # Start all functions in this runlevel
  137. for i in $( ls -v /etc/rc.d/rc${runlevel}.d/S* 2> /dev/null)
  138. do
  139. if [ "${previous}" != "N" ]; then
  140. suffix=${i#/etc/rc.d/rc$runlevel.d/S[0-9][0-9]}
  141. stop=/etc/rc.d/rc$runlevel.d/K[0-9][0-9]$suffix
  142. prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
  143. [ -f ${prev_start} -a ! -f ${stop} ] && continue
  144. fi
  145. check_script_status
  146. case ${runlevel} in
  147. 0|6)
  148. run ${i} stop
  149. ;;
  150. *)
  151. run ${i} start
  152. ;;
  153. esac
  154. error_value=${?}
  155. if [ "${error_value}" != "0" ]; then print_error_msg; fi
  156. done
  157. # Store interactive variable on switch from runlevel S and remove if not
  158. if [ "${runlevel}" == "S" -a "${interactive}" == "i" ]; then
  159. echo "interactive=\"i\"" > /var/run/interactive
  160. else
  161. rm -f /var/run/interactive 2> /dev/null
  162. fi
  163. # Copy the boot log on initial boot only
  164. if [ "${previous}" == "N" -a "${runlevel}" != "S" ]; then
  165. cat /run/var/bootlog >> /var/log/boot.log
  166. # Mark the end of boot
  167. echo "--------" >> /var/log/boot.log
  168. # Remove the temporary file
  169. rm -f /run/var/bootlog 2> /dev/null
  170. fi
  171. # End rc