update-rc.d.pl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #! /usr/bin/perl
  2. #
  3. # update-rc.d Perl script to update links in /etc/rc?.d
  4. #
  5. # Usage:
  6. # update-rc.d [-f] <basename> remove
  7. # update-rc.d <basename> [options]
  8. #
  9. # Options are:
  10. # start <codenumber> <runlevel> <runlevel> <runlevel> .
  11. # stop <codenumber> <runlevel> <runlevel> <runlevel> .
  12. #
  13. # defaults [<codenumber> | <startcode> <stopcode>]
  14. # (means start <startcode> 2 3 4 5
  15. # as well as stop <stopcode> 0 1 2 3 4 5 6
  16. # <codenumber> defaults to 20)
  17. #
  18. # Version: @(#)update-rc.d 1.02 11-Jul-1996 miquels@cistron.nl
  19. #
  20. # Changes: 1.00 Wrote perl version directly derived from shell version.
  21. # 1.01 Fixed problem when dangling symlinks are found in
  22. # /etc/rc?.d/. The shell version just exits silently!
  23. # 1.02 More misc bugs fixed caused by sh -> perl translation
  24. #
  25. $version= '1.3.2'; # This line modified by Makefile
  26. chdir('/etc') || die "chdir /etc: $!\n";
  27. $initd='init.d';
  28. sub usage {
  29. print STDERR <<EOF;
  30. Debian GNU/Linux update-rc.d $version. Copyright (C) 1996 Miquel van
  31. Smoorenburg. This is free software; see the GNU General Public Licence
  32. version 2 or later for copying conditions. There is NO warranty.
  33. update-rc.d: error: @_
  34. usage: update-rc.d [-f] <basename> remove
  35. update-rc.d <basename> defaults [<cn> | <scn> <kcn>]
  36. update-rc.d <basename> start|stop <cn> <r> <r> . ...
  37. EOF
  38. &leave(1);
  39. }
  40. sub getinode {
  41. local @tmp;
  42. unless (@tmp = stat($_[0])) {
  43. print STDERR "stat($_[0]): $!\n";
  44. $tmp[1] = 0;
  45. }
  46. $tmp[1];
  47. }
  48. sub leave {
  49. eval $atexit if ($atexit ne '');
  50. exit($_[0]);
  51. }
  52. $force = 0;
  53. if ($ARGV[0] eq '-f') {
  54. shift @ARGV;
  55. $force = 1;
  56. }
  57. &usage("too few arguments") if ($#ARGV < 1);
  58. $bn = shift @ARGV;
  59. $action = shift @ARGV;
  60. if ($action eq 'remove') {
  61. &usage("remove must be only action") if ($#ARGV > 0);
  62. if (-f "$initd/$bn") {
  63. unless ($force) {
  64. print STDERR "update-rc.d: error: /etc/$initd/$bn exists during rc.d purge (use -f to force).\n";
  65. &leave(1);
  66. }
  67. } else {
  68. $atexit = "unlink('$initd/$bn');";
  69. }
  70. print " Removing any system startup links to /etc/$initd/$bn ...\n";
  71. open(FD, ">>$initd/$bn");
  72. close FD;
  73. $own = &getinode("$initd/$bn");
  74. @files = split(/\s+/, `echo rc?.d/[SK]*`);
  75. foreach $f (@files) {
  76. $inode = &getinode($f);
  77. if ($inode == $own) {
  78. unless (unlink($f)) {
  79. print STDERR "unlink($f): $!\n";
  80. &leave(1);
  81. }
  82. print " $f\n";
  83. }
  84. }
  85. &leave(0);
  86. } elsif ($action eq 'defaults') {
  87. if ($#ARGV < 0) {
  88. $sn = $kn = 20;
  89. } elsif ($#ARGV == 0) {
  90. $sn = $kn = $ARGV[0];
  91. } elsif ($#ARGV == 1) {
  92. $sn = $ARGV[0];
  93. $kn = $ARGV[1];
  94. } else {
  95. &usage("defaults takes only one or two codenumbers");
  96. }
  97. @ARGV = ('start', "$sn", '2', '3', '4', '5', 'stop',
  98. "$kn", '0', '1', '6');
  99. } elsif ($action ne 'start' && $action ne 'stop') {
  100. &usage("unknown mode or add action $action");
  101. }
  102. unless (-f "$initd/$bn") {
  103. print STDERR "update-rc.d: warning /etc/$initd/$bn doesn't exist during rc.d setup.\n";
  104. exit(0);
  105. }
  106. $own = &getinode("$initd/$bn");
  107. @files = split(/\s+/, `echo rc?.d/[SK]*`);
  108. foreach $f (@files) {
  109. $inode = &getinode($f);
  110. if ($inode == $own) {
  111. print STDERR " System startup links pointing to /etc/$initd/$bn already exist.\n";
  112. exit(0);
  113. }
  114. }
  115. print " Adding system startup links pointing to /etc/$initd/$bn ...\n";
  116. while ($#ARGV >= 1) {
  117. if ($ARGV[0] eq 'start') {
  118. $ks = 'S';
  119. } elsif ($ARGV[0] eq 'stop') {
  120. $ks = 'K';
  121. } else {
  122. &usage("unknown action $1");
  123. }
  124. shift @ARGV;
  125. $number = shift @ARGV;
  126. while ($#ARGV >= 0) {
  127. $_ = $ARGV[0];
  128. if (/^\.$/) {
  129. shift @ARGV;
  130. last;
  131. } elsif (/^.$/) {
  132. symlink("../$initd/$bn", "rc$_.d/$ks$number$bn") ||
  133. die "symlink: $!\n";
  134. print " rc$_.d/$ks$number$bn -> ../$initd/$bn\n";
  135. shift @ARGV;
  136. next;
  137. } elsif (/^(start|stop)$/) {
  138. last;
  139. }
  140. &usage('runlevel is more than one character\n');
  141. }
  142. }
  143. if ($#ARGV >= 0) {
  144. &usage("surplus arguments, but not enough for an add action: @ARGV\n");
  145. }
  146. 0;