update-rc.d 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/bin/sh
  2. #
  3. # update-rc.d Update the links in /etc/rc[0-9S].d/
  4. #
  5. # (c) 2003, 2004 Phil Blundell <pb@handhelds.org>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2, or (at your option)
  10. # any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. initd="/etc/init.d"
  17. etcd="/etc/rc"
  18. notreally=0
  19. force=0
  20. dostart=0
  21. verbose=0
  22. usage()
  23. {
  24. cat >&2 <<EOF
  25. usage: update-rc.d [-n] [-f] [-r <root>] <basename> remove
  26. update-rc.d [-n] [-r <root>] [-s] <basename> defaults [NN | sNN kNN]
  27. update-rc.d [-n] [-r <root>] [-s] <basename> start|stop NN runlvl [runlvl] [...] .
  28. -n: not really
  29. -f: force
  30. -v: verbose
  31. -r: alternate root path (default is /)
  32. -s: invoke start methods if appropriate to current runlevel
  33. EOF
  34. }
  35. checklinks()
  36. {
  37. local i dn fn remove=0
  38. if [ "x$1" = "xremove" ]; then
  39. echo " Removing any system startup links for $bn ..."
  40. remove=1
  41. fi
  42. for i in 0 1 2 3 4 5 6 7 8 9 S; do
  43. dn="${etcd}${i}.d"
  44. if [ ! -d $dn ]; then
  45. continue;
  46. fi
  47. for f in ${dn}/[SK]??${bn}; do
  48. if [ -L $f ]; then
  49. if [ $remove -eq 0 ]; then
  50. return 1
  51. fi
  52. echo " $f"
  53. if [ $notreally -eq 1 ]; then
  54. continue
  55. fi
  56. rm $f
  57. fi
  58. done
  59. done
  60. return 0
  61. }
  62. dolink()
  63. {
  64. startstop=$1
  65. lev=`echo $2 | cut -d/ -f1`
  66. nn=`echo $2 | cut -d/ -f2`
  67. fn="${etcd}${lev}.d/${startstop}${nn}${bn}"
  68. [ $verbose -eq 1 ] && echo " $fn -> ../init.d/$bn"
  69. if [ $notreally -eq 0 ]; then
  70. mkdir -p `dirname $fn`
  71. ln -s ../init.d/$bn $fn
  72. fi
  73. if [ $dostart -eq 1 ] && [ $startstop = "S" ] && [ $lev = $RUNLEVEL ]; then
  74. $fn start || true
  75. fi
  76. }
  77. makelinks()
  78. {
  79. if ! checklinks; then
  80. echo " System startup links for $initd/$bn already exist."
  81. if [ $dostart -eq 1 ] && [ $notreally -eq 0 ] && [ -L ${etcd}${RUNLEVEL}.d/S??${bn} ]; then
  82. ${etcd}${RUNLEVEL}.d/S??${bn} restart || true
  83. fi
  84. exit 0
  85. fi
  86. echo " Adding system startup for $initd/$bn."
  87. for i in $startlinks; do
  88. dolink S $i
  89. done
  90. for i in $stoplinks; do
  91. dolink K $i
  92. done
  93. }
  94. while [ $# -gt 0 ]; do
  95. case $1 in
  96. -n) notreally=1
  97. shift
  98. continue
  99. ;;
  100. -v) verbose=1
  101. shift
  102. continue
  103. ;;
  104. -f) force=1
  105. shift
  106. continue
  107. ;;
  108. -s) dostart=1
  109. shift
  110. continue
  111. ;;
  112. -r) shift
  113. root=$1
  114. initd="${root}${initd}"
  115. etcd="${root}${etcd}"
  116. shift
  117. ;;
  118. -h | --help)
  119. usage
  120. exit 0
  121. ;;
  122. -*)
  123. usage
  124. exit 1
  125. ;;
  126. *)
  127. break
  128. ;;
  129. esac
  130. done
  131. if [ $# -lt 2 ]; then
  132. usage
  133. exit 1
  134. fi
  135. bn=$1
  136. shift
  137. sn=$initd/$bn
  138. if [ -L "$sn" -a -n "$root" ]; then
  139. if which readlink >/dev/null; then
  140. while true; do
  141. linksn="$(readlink "$sn")"
  142. if [ -z "$linksn" ]; then
  143. break
  144. fi
  145. sn="$linksn"
  146. case "$sn" in
  147. /*) sn="$root$sn" ;;
  148. *) sn="$initd/$sn" ;;
  149. esac
  150. done
  151. else
  152. echo "update-rc.d: readlink tool not present, cannot check whether \
  153. $sn symlink points to a valid file." >&2
  154. fi
  155. fi
  156. if [ $1 != "remove" ]; then
  157. if [ ! -f "$sn" ]; then
  158. echo "update-rc.d: $initd/$bn: file does not exist" >&2
  159. exit 1
  160. fi
  161. else
  162. if [ -f "$sn" ]; then
  163. if [ $force -eq 1 ]; then
  164. echo "update-rc.d: $initd/$bn exists during rc.d purge (continuing)" >&2
  165. else
  166. echo "update-rc.d: $initd/$bn exists during rc.d purge (use -f to force)" >&2
  167. exit 1
  168. fi
  169. fi
  170. fi
  171. if [ $dostart -eq 1 ]; then
  172. #RUNLEVEL=`sed 's/.*\[\(.*\)\]/\1/' < /proc/1/cmdline`
  173. RUNLEVEL=`runlevel | cut -d" " -f2`
  174. if [ "x$RUNLEVEL" = "x" ]; then
  175. echo "Unable to determine current runlevel" >&2
  176. exit 1
  177. fi
  178. fi
  179. case $1 in
  180. remove)
  181. checklinks "remove"
  182. ;;
  183. defaults)
  184. if [ $# -gt 3 ]; then
  185. echo "defaults takes only one or two arguments" >&2
  186. usage
  187. exit 1
  188. fi
  189. start=20
  190. stop=20
  191. if [ $# -gt 1 ]; then
  192. start=$2
  193. stop=$2
  194. fi
  195. if [ $# -gt 2 ]; then
  196. stop=$3
  197. fi
  198. start=`printf %02d $start`
  199. stop=`printf %02d $stop`
  200. stoplinks="0/$stop 1/$stop 6/$stop"
  201. startlinks="2/$start 3/$start 4/$start 5/$start"
  202. makelinks
  203. ;;
  204. start | stop)
  205. while [ $# -gt 0 ]; do
  206. if [ $1 = "start" ]; then
  207. letter=S
  208. elif [ $1 = "stop" ]; then
  209. letter=K
  210. else
  211. echo "expected start or stop" >&2
  212. usage
  213. exit 1
  214. fi
  215. shift
  216. NN=`printf %02d $(expr $1 + 0)`
  217. shift
  218. while [ "x$1" != "x." ]; do
  219. if [ $# -eq 0 ]; then
  220. echo "action with list of runlevels not terminated by \`.'" >&2
  221. exit 1
  222. fi
  223. level=$1
  224. shift
  225. case $letter in
  226. S) startlinks="$startlinks $level/$NN" ;;
  227. K) stoplinks="$stoplinks $level/$NN" ;;
  228. esac
  229. done
  230. shift
  231. done
  232. makelinks
  233. ;;
  234. *)
  235. usage
  236. exit 1
  237. ;;
  238. esac