update-rc.d.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #! /usr/bin/perl
  2. #
  3. # update-rc.d Update the links in /etc/rc[0-9S].d/
  4. #
  5. # Version: @(#)update-rc.d.pl 2.02 05-Mar-1998 miquels@cistron.nl
  6. #
  7. $initd = "/etc/init.d";
  8. $etcd = "/etc/rc";
  9. $notreally = 0;
  10. # Print usage message and die.
  11. sub usage {
  12. print STDERR "update-rc.d: error: @_\n" if ($#_ >= 0);
  13. print STDERR <<EOF;
  14. usage: update-rc.d [-n] [-f] <basename> remove
  15. update-rc.d [-n] [-f] <basename> defaults [NN | sNN kNN]
  16. update-rc.d [-n] [-f] <basename> start|stop NN runlvl runlvl . ...
  17. -n: not really
  18. -f: force
  19. EOF
  20. exit (1);
  21. }
  22. # Check out options.
  23. while($#ARGV >= 0 && ($_ = $ARGV[0]) =~ /^-/) {
  24. shift @ARGV;
  25. if (/^-n$/) { $notreally++; next }
  26. if (/^-f$/) { $force++; next }
  27. if (/^-h|--help$/) { &usage; }
  28. &usage("unknown option");
  29. }
  30. # Action.
  31. &usage() if ($#ARGV < 1);
  32. $bn = shift @ARGV;
  33. if ($ARGV[0] ne 'remove') {
  34. if (! -f "$initd/$bn") {
  35. print STDERR "update-rc.d: $initd/$bn: file does not exist\n";
  36. exit (1);
  37. }
  38. } elsif (-f "$initd/$bn") {
  39. if (!$force) {
  40. printf STDERR "update-rc.d: $initd/$bn exists during rc.d purge (use -f to force)\n";
  41. exit (1);
  42. } else {
  43. printf STDERR "update-rc.d: $initd/$bn exists during rc.d purge (continuing)\n";
  44. }
  45. }
  46. $_ = $ARGV[0];
  47. if (/^remove$/) { &checklinks ("remove"); }
  48. elsif (/^defaults$/) { &defaults; &makelinks }
  49. elsif (/^(start|stop)$/) { &startstop; &makelinks; }
  50. else { &usage; }
  51. exit (0);
  52. # Check if there are links in /etc/rc[0-9S].d/
  53. # Remove if the first argument is "remove" and the links
  54. # point to $bn.
  55. sub is_link () {
  56. my ($op, $fn, $bn) = @_;
  57. if (! -l $fn) {
  58. print STDERR "update-rc.d: warning: $fn is not a symbolic link\n";
  59. return 0;
  60. } else {
  61. $linkdst = readlink ($fn);
  62. if (! defined $linkdst) {
  63. die ("update-rc.d: error reading symbolic link: $!\n");
  64. }
  65. if (($linkdst ne "../init.d/$bn") && ($linkdst ne "../init.d/$bn")) {
  66. print STDERR "update-rc.d: warning: $fn is not a link to ../init.d/$bn\n";
  67. return 0;
  68. }
  69. }
  70. return 1;
  71. }
  72. sub checklinks {
  73. my ($i, $found, $fn, $islnk);
  74. print " Removing any system startup links for $initd/$bn ...\n"
  75. if ($_[0] eq 'remove');
  76. $found = 0;
  77. foreach $i (0..9, 'S') {
  78. unless (chdir ("$etcd$i.d")) {
  79. next if ($i =~ m/^[789S]$/);
  80. die("update-rc.d: chdir $etcd$i.d: $!\n");
  81. }
  82. opendir(DIR, ".");
  83. foreach $_ (readdir(DIR)) {
  84. next unless (/^[S|K]\d\d$bn$/);
  85. $fn = "$etcd$i.d/$_";
  86. $found = 1;
  87. $islnk = &is_link ($_[0], $fn, $bn);
  88. next if ($_[0] ne 'remove');
  89. if (! $islnk) {
  90. print " $fn is not a link to ../init.d/$bn; not removing\n";
  91. next;
  92. }
  93. print " $etcd$i.d/$_\n";
  94. next if ($notreally);
  95. unlink ("$etcd$i.d/$_") ||
  96. die("update-rc.d: unlink: $!\n");
  97. }
  98. closedir(DIR);
  99. }
  100. $found;
  101. }
  102. # Process the arguments after the "defaults" keyword.
  103. sub defaults {
  104. my ($start, $stop) = (20, 20);
  105. &usage ("defaults takes only one or two codenumbers") if ($#ARGV > 2);
  106. $start = $stop = $ARGV[1] if ($#ARGV >= 1);
  107. $stop = $ARGV[2] if ($#ARGV >= 2);
  108. &usage ("codenumber must be a number between 0 and 99")
  109. if ($start !~ /^\d\d?$/ || $stop !~ /^\d\d?$/);
  110. $start = sprintf("%02d", $start);
  111. $stop = sprintf("%02d", $stop);
  112. $stoplinks[0] = $stoplinks[1] = $stoplinks[6] = "K$stop";
  113. $startlinks[2] = $startlinks[3] =
  114. $startlinks[4] = $startlinks[5] = "S$start";
  115. 1;
  116. }
  117. # Process the arguments after the start or stop keyword.
  118. sub startstop {
  119. my($letter, $NN, $level);
  120. while ($#ARGV >= 0) {
  121. if ($ARGV[0] eq 'start') { $letter = 'S'; }
  122. elsif ($ARGV[0] eq 'stop') { $letter = 'K' }
  123. else {
  124. &usage("expected start|stop");
  125. }
  126. if ($ARGV[1] !~ /^\d\d?$/) {
  127. &usage("expected NN after $ARGV[0]");
  128. }
  129. $NN = sprintf("%02d", $ARGV[1]);
  130. shift @ARGV; shift @ARGV;
  131. $level = shift @ARGV;
  132. do {
  133. if ($level !~ m/^[0-9S]$/) {
  134. &usage(
  135. "expected runlevel [0-9S] (did you forget \".\" ?)");
  136. }
  137. if (! -d "$etcd$level.d") {
  138. print STDERR
  139. "update-rc.d: $etcd$level.d: no such directory\n";
  140. exit(1);
  141. }
  142. $level = 99 if ($level eq 'S');
  143. $startlinks[$level] = "$letter$NN" if ($letter eq 'S');
  144. $stoplinks[$level] = "$letter$NN" if ($letter eq 'K');
  145. } while (($level = shift @ARGV) ne '.');
  146. &usage("action with list of runlevels not terminated by \`.'")
  147. if ($level ne '.');
  148. }
  149. 1;
  150. }
  151. # Create the links.
  152. sub makelinks {
  153. my($t, $i);
  154. my @links;
  155. if (&checklinks) {
  156. print " System startup links for $initd/$bn already exist.\n";
  157. exit (0);
  158. }
  159. print " Adding system startup for $initd/$bn ...\n";
  160. # nice unreadable perl mess :)
  161. for($t = 0; $t < 2; $t++) {
  162. @links = $t ? @startlinks : @stoplinks;
  163. for($i = 0; $i <= $#links; $i++) {
  164. $lvl = $i;
  165. $lvl = 'S' if ($i == 99);
  166. next if ($links[$i] eq '');
  167. print " $etcd$lvl.d/$links[$i]$bn -> ../init.d/$bn\n";
  168. next if ($notreally);
  169. symlink("../init.d/$bn", "$etcd$lvl.d/$links[$i]$bn")
  170. || die("update-rc.d: symlink: $!\n");
  171. }
  172. }
  173. 1;
  174. }