rc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 ${DISTRO_MINI} book,\n"
  25. MSG="${MSG}please be so kind to inform us at ${DISTRO_CONTACT}.\n"
  26. log_failure_msg "${MSG}"
  27. log_info_msg "Press Enter to continue..."
  28. wait_for_user
  29. }
  30. check_script_status()
  31. {
  32. # $i is set when called
  33. if [ ! -f ${i} ]; then
  34. log_warning_msg "${i} is not a valid symlink."
  35. SCRIPT_STAT="1"
  36. fi
  37. if [ ! -x ${i} ]; then
  38. log_warning_msg "${i} is not executable, skipping."
  39. SCRIPT_STAT="1"
  40. fi
  41. }
  42. run()
  43. {
  44. if [ -z $interactive ]; then
  45. ${1} ${2}
  46. return $?
  47. fi
  48. while true; do
  49. read -p "Run ${1} ${2} (Yes/no/continue)? " -n 1 runit
  50. echo
  51. case ${runit} in
  52. c | C)
  53. interactive=""
  54. ${i} ${2}
  55. ret=${?}
  56. break;
  57. ;;
  58. n | N)
  59. return 0
  60. ;;
  61. y | Y)
  62. ${i} ${2}
  63. ret=${?}
  64. break
  65. ;;
  66. esac
  67. done
  68. return $ret
  69. }
  70. # Read any local settings/overrides
  71. [ -r /etc/sysconfig/rc.site ] && source /etc/sysconfig/rc.site
  72. DISTRO=${DISTRO:-"Linux From Scratch"}
  73. DISTRO_CONTACT=${DISTRO_CONTACT:-"lfs-dev@linuxfromscratch.org (Registration required)"}
  74. DISTRO_MINI=${DISTRO_MINI:-"LFS"}
  75. IPROMPT=${IPROMPT:-"no"}
  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
  92. [ -r /etc/sysconfig/console ] && source /etc/sysconfig/console
  93. dmesg -n "${LOGLEVEL:-7}"
  94. fi
  95. if [ "${IPROMPT}" == "yes" -a "${runlevel}" == "S" ]; then
  96. # The total length of the distro welcome string, without escape codes
  97. wlen=${wlen:-$(echo "Welcome to ${DISTRO}" | wc -c )}
  98. welcome_message=${welcome_message:-"Welcome to ${INFO}${DISTRO}${NORMAL}"}
  99. # The total length of the interactive string, without escape codes
  100. ilen=${ilen:-$(echo "Press 'I' to enter interactive startup" | wc -c )}
  101. i_message=${i_message:-"Press '${FAILURE}I${NORMAL}' to enter interactive startup"}
  102. # dcol and icol are spaces before the message to center the message
  103. # on screen. itime is the amount of wait time for the user to press a key
  104. wcol=$(( ( ${COLUMNS} - ${wlen} ) / 2 ))
  105. icol=$(( ( ${COLUMNS} - ${ilen} ) / 2 ))
  106. itime=${itime:-"3"}
  107. echo -e "\n\n"
  108. echo -e "\\033[${wcol}G${welcome_message}"
  109. echo -e "\\033[${icol}G${i_message}${NORMAL}"
  110. echo ""
  111. read -t "${itime}" -n 1 interactive 2>&1 > /dev/null
  112. fi
  113. # Make lower case
  114. [ "${interactive}" == "I" ] && interactive="i"
  115. [ "${interactive}" != "i" ] && interactive=""
  116. # Read the state file if it exists from runlevel S
  117. [ -r /var/run/interactive ] && source /var/run/interactive
  118. # Attempt to stop all services started by the previous runlevel,
  119. # and killed in this runlevel
  120. if [ "${previous}" != "N" ]; then
  121. for i in $(ls -v /etc/rc.d/rc${runlevel}.d/K* 2> /dev/null)
  122. do
  123. check_script_status
  124. if [ "${SCRIPT_STAT}" == "1" ]; then
  125. SCRIPT_STAT="0"
  126. continue
  127. fi
  128. suffix=${i#/etc/rc.d/rc$runlevel.d/K[0-9][0-9]}
  129. prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
  130. sysinit_start=/etc/rc.d/rcS.d/S[0-9][0-9]$suffix
  131. if [ "${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
  132. if [ ! -f ${prev_start} -a ! -f ${sysinit_start} ]; then
  133. MSG="WARNING:\n\n${i} can't be "
  134. MSG="${MSG}executed because it was not "
  135. MSG="${MSG}not started in the previous "
  136. MSG="${MSG}runlevel (${previous})."
  137. log_warning_msg "$MSG"
  138. continue
  139. fi
  140. fi
  141. run ${i} stop
  142. error_value=${?}
  143. if [ "${error_value}" != "0" ]; then print_error_msg; fi
  144. done
  145. fi
  146. if [ "${previous}" == "N" ]; then export IN_BOOT=1; fi
  147. if [ "$runlevel" == "6" -a -n "${FASTBOOT}" ]; then
  148. touch /fastboot
  149. fi
  150. # Start all functions in this runlevel
  151. for i in $( ls -v /etc/rc.d/rc${runlevel}.d/S* 2> /dev/null)
  152. do
  153. if [ "${previous}" != "N" ]; then
  154. suffix=${i#/etc/rc.d/rc$runlevel.d/S[0-9][0-9]}
  155. stop=/etc/rc.d/rc$runlevel.d/K[0-9][0-9]$suffix
  156. prev_start=/etc/rc.d/rc$previous.d/S[0-9][0-9]$suffix
  157. [ -f ${prev_start} -a ! -f ${stop} ] && continue
  158. fi
  159. check_script_status
  160. if [ "${SCRIPT_STAT}" == "1" ]; then
  161. SCRIPT_STAT="0"
  162. continue
  163. fi
  164. case ${runlevel} in
  165. 0|6)
  166. run ${i} stop
  167. ;;
  168. *)
  169. run ${i} start
  170. ;;
  171. esac
  172. error_value=${?}
  173. if [ "${error_value}" != "0" ]; then print_error_msg; fi
  174. done
  175. # Store interactive variable on switch from runlevel S and remove if not
  176. if [ "${runlevel}" == "S" -a "${interactive}" == "i" ]; then
  177. echo "interactive=\"i\"" > /var/run/interactive
  178. else
  179. rm -f /var/run/interactive 2> /dev/null
  180. fi
  181. # Copy the boot log on initial boot only
  182. if [ "${previous}" == "N" -a "${runlevel}" != "S" ]; then
  183. cat $BOOTLOG >> /var/log/boot.log
  184. # Mark the end of boot
  185. echo "--------" >> /var/log/boot.log
  186. # Remove the temporary file
  187. rm -f $BOOTLOG 2> /dev/null
  188. fi
  189. # End rc