~ldgoodridge95/ubuntu/vivid/upstart/rm-libjson0-dev

« back to all changes in this revision

Viewing changes to debian/migrate-inittab.pl

Tags: upstream-1.7
ImportĀ upstreamĀ versionĀ 1.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/init/control-alt-delete.conf"
49
 
    unless $have_cad;
50
 
 
51
 
foreach (qw/tty1 tty2 tty3 tty4 tty5 tty6/) {
52
 
    unlink "/etc/init/$_.conf"
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
 
        chomp @job;
65
 
        close JOB
66
 
            or warn "Error while closing /etc/event,d/$_: $!" and next;
67
 
 
68
 
        foreach my $rl (qw/2 3 4 5/) {
69
 
            my $idx;
70
 
            for ($idx = 0; $idx < @job; $idx++) {
71
 
                last if $job[$idx] =~ /^\s*(start|stop)\s+on\s+runlevel\s+$rl\b/;
72
 
            }
73
 
 
74
 
            if ($idx < @job) {
75
 
                if ($rlevel =~ /$rl/) {
76
 
                    $job[$idx] =~ s/^(\s*)stop(\s+)/$1start$2/;
77
 
                } else {
78
 
                    $job[$idx] =~ s/^(\s*)start(\s+)/$1stop$2/;
79
 
                }
80
 
            } else {
81
 
                if ($rlevel =~ /$rl/) {
82
 
                    push @job, "start on runlevel $rl";
83
 
                } else {
84
 
                    push @job, "stop on runlevel $rl";
85
 
                }
86
 
            }
87
 
        }
88
 
 
89
 
        my $idx;
90
 
        for ($idx = 0; $idx < @job; $idx++) {
91
 
            last if $job[$idx] =~ /^\s*respawn\s*/; # match bare 'respawn' too
92
 
        }
93
 
 
94
 
        if ($idx < @job) {
95
 
            # only match old-style 'respawn process', not bare 'respawn'
96
 
            $job[$idx] =~ s/^(\s*respawn\s+).*/$1$process/;
97
 
        } else {
98
 
            push @job, "respawn";
99
 
            push @job, "exec $process";
100
 
        }
101
 
 
102
 
        # Try to fix up effects of previous broken migrations
103
 
        if (@job and $job[$#job] =~ /.*(.+?)exec (\1)$/) {
104
 
            $job[$#job] = "exec $1";
105
 
        }
106
 
 
107
 
    } else {
108
 
        push @job, "# $_ - getty";
109
 
        push @job, "#";
110
 
        push @job, "# Converted from /etc/inittab entry";
111
 
        push @job, "";
112
 
 
113
 
        foreach my $rl (qw/2 3 4 5/) {
114
 
            if ($rlevel =~ /$rl/) {
115
 
                push @job, "start on runlevel $rl";
116
 
            } else {
117
 
                push @job, "stop on runlevel $rl";
118
 
            }
119
 
        }
120
 
        push @job, "";
121
 
 
122
 
        push @job, "stop on shutdown";
123
 
        push @job, "";
124
 
 
125
 
        push @job, "respawn";
126
 
        push @job, "exec $process";
127
 
    }
128
 
 
129
 
    open JOB, ">/etc/event.d/.$_"
130
 
        or warn "Unable to write to /etc/event.d/.$_: $!" and next;
131
 
    print JOB map { "$_\n" } @job;
132
 
    unless (close JOB) {
133
 
        warn "Error while closing /etc/event.d/.$_: $!";
134
 
        unlink "/etc/event.d/.$_";
135
 
        next;
136
 
    }
137
 
 
138
 
    unless (rename "/etc/event.d/.$_", "/etc/event.d/$_") {
139
 
        warn "Unable to replace /etc/event.d/$_: $!";
140
 
        unlink "/etc/event.d/.$_";
141
 
        next;
142
 
    }
143
 
}