rc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. 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. 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. 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. IPROMPT=${IPROMPT:-"no"}
  77. # These 3 signals will not cause our script to exit
  78. trap "" INT QUIT TSTP
  79. [ "${1}" != "" ] && runlevel=${1}
  80. if [ "${runlevel}" == "" ]; then
  81. echo "Usage: ${0} <runlevel>" >&2
  82. exit 1
  83. fi
  84. previous=${PREVLEVEL}
  85. [ "${previous}" == "" ] && previous=N
  86. if [ ! -d /etc/rc.d/rc${runlevel}.d ]; then
  87. log_info_msg "/etc/rc.d/rc${runlevel}.d does not exist.\n"
  88. exit 1
  89. fi
  90. if [ "$runlevel" == "6" -o "$runlevel" == "0" ]; then IPROMPT="no"; fi
  91. # Note: In ${LOGLEVEL:-7}, it is ':' 'dash' '7', not minus 7
  92. if [ "$runlevel" == "S" ]; then
  93. [ -r /etc/sysconfig/console ] && source /etc/sysconfig/console
  94. dmesg -n "${LOGLEVEL:-7}"
  95. fi
  96. if [ "${IPROMPT}" == "yes" -a "${runlevel}" == "S" ]; then
  97. # The total length of the distro welcome string, without escape codes
  98. wlen=${wlen:-$(echo "Welcome to ${DISTRO}" | wc -c )}
  99. welcome_message=${welcome_message:-"Welcome to ${INFO}${DISTRO}${NORMAL}"}
  100. # The total length of the interactive string, without escape codes
  101. ilen=${ilen:-$(echo "Press 'I' to enter interactive startup" | wc -c )}
  102. i_message=${i_message:-"Press '${FAILURE}I${NORMAL}' to enter interactive startup"}
  103. # dcol and icol are spaces before the message to center the message
  104. # on screen. itime is the amount of wait time for the user to press a key
  105. wcol=$(( ( ${COLUMNS} - ${wlen} ) / 2 ))
  106. icol=$(( ( ${COLUMNS} - ${ilen} ) / 2 ))
  107. itime=${itime:-"3"}
  108. echo -e "\n\n"
  109. echo -e "\\033[${wcol}G${welcome_message}"
  110. echo -e "\\033[${icol}G${i_message}${NORMAL}"
  111. echo ""
  112. read -t "${itime}" -n 1 interactive 2>&1 > /dev/null
  113. fi
  114. # Make lower case
  115. [ "${interactive}" == "I" ] && interactive="i"
  116. [ "${interactive}" != "i" ] && interactive=""
  117. # Read the state file if it exists from runlevel S
  118. [ -r /var/run/interactive ] && source /var/run/interactive
  119. # Attempt to stop all services started by the previous runlevel,
  120. # and killed in this runlevel
  121. if [ "${previous}" != "N" ]; then
  122. for i in $(ls -v /etc/rc.d/rc${runlevel}.d/K* 2> /dev/null)
  123. do
  124. check_script_status
  125. suffix=${i#/etc/rc.d/rc$runlevel.d/K[0-9][0-9]}
  126. prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
  127. sysinit_start=/etc/rc.d/rcS.d/S[0-9][0-9]$suffix
  128. if [ "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
  129. if [ ! -f ${prev_start} -a ! -f ${sysinit_start} ]; then
  130. MSG="WARNING:\n\n${i} can't be "
  131. MSG="${MSG}executed because it was not "
  132. MSG="${MSG}not started in the previous "
  133. MSG="${MSG}runlevel (${previous})."
  134. log_warning_msg "$MSG"
  135. continue
  136. fi
  137. fi
  138. run ${i} stop
  139. error_value=${?}
  140. if [ "${error_value}" != "0" ]; then print_error_msg; fi
  141. done
  142. fi
  143. if [ "${previous}" == "N" ]; then export IN_BOOT=1; fi
  144. if [ "$runlevel" == "6" -a -n "${FASTBOOT}" ]; then
  145. touch /fastboot
  146. fi
  147. # Start all functions in this runlevel
  148. for i in $( ls -v /etc/rc.d/rc${runlevel}.d/S* 2> /dev/null)
  149. do
  150. if [ "${previous}" != "N" ]; then
  151. suffix=${i#/etc/rc.d/rc$runlevel.d/S[0-9][0-9]}
  152. stop=/etc/rc.d/rc$runlevel.d/K[0-9][0-9]$suffix
  153. prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
  154. [ -f ${prev_start} -a ! -f ${stop} ] && continue
  155. fi
  156. check_script_status
  157. case ${runlevel} in
  158. 0|6)
  159. run ${i} stop
  160. ;;
  161. *)
  162. run ${i} start
  163. ;;
  164. esac
  165. error_value=${?}
  166. if [ "${error_value}" != "0" ]; then print_error_msg; fi
  167. done
  168. # Store interactive variable on switch from runlevel S and remove if not
  169. if [ "${runlevel}" == "S" -a "${interactive}" == "i" ]; then
  170. echo "interactive=\"i\"" > /var/run/interactive
  171. else
  172. rm -f /var/run/interactive 2> /dev/null
  173. fi
  174. # Copy the boot log on initial boot only
  175. if [ "${previous}" == "N" -a "${runlevel}" != "S" ]; then
  176. cat /run/var/bootlog >> /var/log/boot.log
  177. # Mark the end of boot
  178. echo "--------" >> /var/log/boot.log
  179. # Remove the temporary file
  180. rm -f /run/var/bootlog 2> /dev/null
  181. fi
  182. # End rc