1
0

rc 5.9 KB

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