home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 2 / CD_Magazyn_EXEC_nr_2.iso / Linux / Archiwa / dpkg.tar.gz / dpkg-1.6.12_powerpc.nondebbin.tar / usr / sbin / update-rc.d < prev   
Text File  |  2000-04-07  |  5KB  |  208 lines

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