update-rc.d.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/sh
  2. #
  3. # Usage:
  4. # update-rc.d <basename> remove
  5. # update-rc.d <basename> [options]
  6. # Options are:
  7. # start <codenumber> <runlevel> <runlevel> <runlevel> .
  8. # stop <codenumber> <runlevel> <runlevel> <runlevel> .
  9. # defaults [<codenumber> | <startcode> <stopcode>]
  10. # (means start <startcode> 2 3 4 5
  11. # as well as stop <stopcode> 0 1 2 3 4 5 6
  12. # <codenumber> defaults to 20)
  13. set -e
  14. cd /etc
  15. initd='init.d'
  16. usage () { echo >&2 "\
  17. update-rc.d: error: $1.
  18. usage: update-rc.d <basename> remove
  19. update-rc.d <basename> defaults [<cn> | <scn> <kcn>]
  20. update-rc.d <basename> start|stop <cn> <r> <r> . ..."; exit 1 }
  21. getinode () {
  22. inode="`ls -Li1 \"$1\" | sed -e 's/^ *//; s/ .*//'`"
  23. }
  24. if [ $# -lt 2 ]; then usage "too few arguments"; fi
  25. bn="$1"; shift
  26. if [ xremove = "x$1" ]; then
  27. if [ $# != 1 ]; then usage "remove must be only action"; fi
  28. if [ -f "$initd/$bn" ]; then
  29. echo >&2 "update-rc.d: error: /etc/$initd/$bn exists during rc.d purge."
  30. exit 1
  31. fi
  32. echo " Removing any system startup links to /etc/$initd/$bn ..."
  33. trap 'rm -f "$initd/$bn"' 0
  34. touch "$initd/$bn"
  35. getinode "$initd/$bn"
  36. own="$inode"
  37. for f in rc?.d/[SK]*; do
  38. getinode "$f"
  39. if [ "x$inode" = "x$own" ]; then
  40. rm "$f";
  41. echo " $f"
  42. fi
  43. done
  44. exit 0
  45. elif [ xdefaults = "x$1" ]; then
  46. if [ $# = 1 ]; then sn=20; kn=20;
  47. elif [ $# = 2 ]; then sn="$2"; kn="$2";
  48. elif [ $# = 3 ]; then sn="$2"; kn="$3";
  49. else usage "defaults takes only one or two codenumbers"; fi
  50. set start "$sn" 2 3 4 5 . stop "$kn" 0 1 6 .
  51. elif ! [ xstart = "x$1" -o xstop = "x$1" ]; then
  52. usage "unknown mode or add action $1"
  53. fi
  54. if ! [ -f "$initd/$bn" ]; then
  55. echo >&2 "update-rc.d: warning /etc/$initd/$bn doesn't exist during rc.d setup."
  56. exit 0
  57. fi
  58. getinode "$initd/$bn"
  59. own="$inode"
  60. for f in rc?.d/[SK]*; do
  61. getinode "$f"
  62. if [ "x$inode" = "x$own" ]; then
  63. echo " System startup links pointing to /etc/$initd/$bn already exist."
  64. exit 0
  65. fi
  66. done
  67. echo " Adding system startup links pointing to /etc/$initd/$bn ..."
  68. while [ $# -ge 3 ]; do
  69. if [ xstart = "x$1" ]; then ks=S
  70. elif [ xstop = "x$1" ]; then ks=K
  71. else usage "unknown action $1"; fi
  72. number="$2"
  73. shift; shift
  74. while [ $# -ge 1 ]; do
  75. case "$1" in
  76. .)
  77. break
  78. ;;
  79. ?)
  80. ln -s "../$initd/$bn" "rc$1.d/$ks$number$bn"
  81. echo " rc$1.d/$ks$number$bn -> ../$initd/$bn"
  82. shift
  83. continue
  84. ;;
  85. esac
  86. usage 'runlevel is more than one character (forgotten . ?)'
  87. done
  88. shift
  89. done
  90. if [ $# != 0 ]; then
  91. usage "surplus arguments, but not enough for an add action: $*"
  92. fi
  93. exit 0