home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / upstart / migrate-inittab.pl
Encoding:
Perl Script  |  2007-03-11  |  3.1 KB  |  137 lines

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %gettys;
  7. my $have_cad = 0;
  8.  
  9.  
  10. #-----------------------------------------------------------------------------#
  11. # Parse /etc/inittab
  12. #-----------------------------------------------------------------------------#
  13.  
  14. open INITTAB, "/etc/inittab"
  15.     or die "Unable to open /etc/inittab: $!";
  16.  
  17. while (<INITTAB>) {
  18.     chomp;
  19.     s/^\s*//;
  20.  
  21.     next if /^\#/;
  22.     next unless length;
  23.  
  24.     my ($id, $rlevel, $action, $process) = split /:/, $_, 4;
  25.  
  26.     warn "missing id field" and next
  27.     unless defined $id and length $id;
  28.     warn "missing runlevel field" and next
  29.     unless defined $rlevel;
  30.     warn "missing action field" and next
  31.     unless defined $action and length $action;
  32.     warn "missing process field" and next
  33.     unless defined $process;
  34.  
  35.  
  36.     $have_cad = 1 if $action eq "ctrlaltdel";
  37.     $gettys{$1} = [ $rlevel, $process ]    if $process =~ /getty.*\b(tty\w+)/;
  38. }
  39.  
  40. close INITTAB
  41.     or warn "Error while closing /etc/inittab: $!";
  42.  
  43.  
  44. #-----------------------------------------------------------------------------#
  45. # Alter /etc/event.d
  46. #-----------------------------------------------------------------------------#
  47.  
  48. unlink "/etc/event.d/control-alt-delete"
  49.     unless $have_cad;
  50.  
  51. foreach (qw/tty1 tty2 tty3 tty4 tty5 tty6/) {
  52.     unlink "/etc/event.d/$_"
  53.     unless exists $gettys{$_};
  54. }
  55.  
  56. foreach (sort keys %gettys) {
  57.     my ($rlevel, $process) = @{$gettys{$_}};
  58.  
  59.     my @job;
  60.     if (-f "/etc/event.d/$_") {
  61.     open JOB, "/etc/event.d/$_"
  62.         or warn "Unable to open /etc/event.d/$_: $!" and next;
  63.     @job = <JOB>;
  64.     close JOB
  65.         or warn "Error while closing /etc/event,d/$_: $!" and next;
  66.  
  67.     foreach my $rl (qw/2 3 4 5/) {
  68.         my $idx;
  69.         for ($idx = 0; $idx < @job; $idx++) {
  70.         last if $job[$idx] =~ /^\s*(start|stop)\s+on\s+runlevel\s+$rl\b/;
  71.         }
  72.  
  73.         if ($idx < @job) {
  74.         if ($rlevel =~ /$rl/) {
  75.             $job[$idx] =~ s/^(\s*)stop(\s+)/$1start$2/;
  76.         } else {
  77.             $job[$idx] =~ s/^(\s*)start(\s+)/$1stop$2/;
  78.         }
  79.         } else {
  80.         if ($rlevel =~ /$rl/) {
  81.             push @job, "start on runlevel $rl\n";
  82.         } else {
  83.             push @job, "stop on runlevel $rl\n";
  84.         }
  85.         }
  86.     }
  87.  
  88.     my $idx;
  89.     for ($idx = 0; $idx < @job; $idx++) {
  90.         last if $job[$idx] =~ /^\s*respawn\s+/;
  91.     }
  92.  
  93.     if ($idx < @job) {
  94.         $job[$idx] =~ s/^(\s*respawn\s+).*/$1$process/;
  95.     } else {
  96.         push @job, "respawn\n";
  97.         push @job, "exec $process\n";
  98.     }
  99.  
  100.     } else {
  101.     push @job, "# $_ - getty\n";
  102.     push @job, "#\n";
  103.     push @job, "# Converted from /etc/inittab entry\n";
  104.     push @job, "\n";
  105.  
  106.     foreach my $rl (qw/2 3 4 5/) {
  107.         if ($rlevel =~ /$rl/) {
  108.         push @job, "start on runlevel $rl\n";
  109.         } else {
  110.         push @job, "stop on runlevel $rl\n";
  111.         }
  112.     }
  113.     push @job, "\n";
  114.  
  115.     push @job, "stop on shutdown\n";
  116.     push @job, "\n";
  117.  
  118.     push @job, "respawn\n";
  119.     push @job, "exec $process\n";
  120.     }
  121.  
  122.     open JOB, ">/etc/event.d/.$_"
  123.     or warn "Unable to write to /etc/event.d/.$_: $!" and next;
  124.     print JOB @job;
  125.     unless (close JOB) {
  126.     warn "Error while closing /etc/event.d/.$_: $!";
  127.     unlink "/etc/event.d/.$_";
  128.     next;
  129.     }
  130.  
  131.     unless (rename "/etc/event.d/.$_", "/etc/event.d/$_") {
  132.     warn "Unable to replace /etc/event.d/$_: $!";
  133.     unlink "/etc/event.d/.$_";
  134.     next;
  135.     }
  136. }
  137.