1 # init.d -- lintian check script -*- perl -*-
3 # Copyright (C) 1998 Christian Schwarz
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, you can find it on the World Wide
17 # Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
18 # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 package Maemian::init_d;
26 # A list of valid LSB keywords. The value is 0 if optional and 1 if required.
27 my %lsb_keywords = (provides => 1,
28 'required-start' => 1,
34 'short-description' => 1,
42 my $postinst = "control/postinst";
43 my $preinst = "control/preinst";
44 my $postrm = "control/postrm";
45 my $prerm = "control/prerm";
46 my $conffiles = "control/conffiles";
52 my $opts_r = qr/-\S+\s*/;
53 my $name_r = qr/[\w.-]+/;
54 my $action_r = qr/\w+/;
55 my $exclude_r = qr/if\s+\[\s+-x\s+\S*update-rc\.d/;
57 # read postinst control file
58 if (open(IN, '<', $postinst)) {
60 next if /$exclude_r/o;
62 next unless /^(?:.+;|^\s*system[\s\(\']+)?\s*update-rc\.d\s+
63 (?:$opts_r)*($name_r)\s+($action_r)/xo;
64 my ($name,$opt) = ($1,$2);
65 next if $opt eq 'remove';
66 if ($initd_postinst{$name}++ == 1) {
67 tag "duplicate-updaterc.d-calls-in-postinst", "$name";
70 unless (m,>\s*/dev/null,o) {
71 tag "output-of-updaterc.d-not-redirected-to-dev-null", "$name postinst";
77 # read preinst control file
78 if (open(IN, '<', $preinst)) {
80 next if /$exclude_r/o;
82 next unless m/update-rc\.d\s+(?:$opts_r)*($name_r)\s+($action_r)/o;
83 my ($name,$opt) = ($1,$2);
84 next if $opt eq 'remove';
85 tag "preinst-calls-updaterc.d", "$name";
90 # read postrm control file
91 if (open(IN, '<', $postrm)) {
93 next if /$exclude_r/o;
95 next unless m/update-rc\.d\s+($opts_r)*($name_r)/o;
96 if ($initd_postrm{$2}++ == 1) {
97 tag "duplicate-updaterc.d-calls-in-postrm", "$2";
100 unless (m,>\s*/dev/null,o) {
101 tag "output-of-updaterc.d-not-redirected-to-dev-null", "$2 postrm";
107 # read prerm control file
108 if (open(IN, '<', $prerm)) {
110 next if /$exclude_r/o;
112 next unless m/update-rc\.d\s+($opts_r)*($name_r)/o;
113 tag "prerm-calls-updaterc.d", "$2";
118 # init.d scripts have to be removed in postrm
119 for (keys %initd_postinst) {
120 if ($initd_postrm{$_}) {
121 delete $initd_postrm{$_};
123 tag "postrm-does-not-call-updaterc.d-for-init.d-script", "/etc/init.d/$_";
126 for (keys %initd_postrm) {
127 tag "postrm-contains-additional-updaterc.d-calls", "/etc/init.d/$_";
131 if (open(IN, '<', $conffiles)) {
137 if (m,^/?etc/rc.\.d,o) {
138 tag "file-in-etc-rc.d-marked-as-conffile", "$_";
144 for (keys %initd_postinst) {
146 # init.d scripts have to be marked as conffiles
147 unless ($conffiles{"/etc/init.d/$_"} or $conffiles{"etc/init.d/$_"}) {
148 tag "init.d-script-not-marked-as-conffile", "/etc/init.d/$_";
151 # check if file exists in package
152 my $initd_file = "init.d/$_";
153 if (-f $initd_file) {
155 open(IN, '<', $initd_file)
156 or fail("cannot open init.d file $initd_file: $!");
158 while (defined(my $l = <IN>)) {
159 if ($l =~ m/^\#\#\# BEGIN INIT INFO/) {
161 tag "init.d-script-has-duplicate-lsb-section", "/etc/init.d/$_";
167 # We have an LSB keyword section. Parse it and save the data
168 # in %lsb for analysis.
169 while (defined(my $l = <IN>)) {
170 if ($l =~ /^\#\#\# END INIT INFO/) {
173 } elsif ($l !~ /^\#/) {
174 tag "init.d-script-has-unterminated-lsb-section", "/etc/init.d/$_:$.";
176 } elsif ($l =~ /^\# ([a-zA-Z-]+):\s*(.*?)\s*$/) {
179 tag "init.d-script-has-duplicate-lsb-keyword", "/etc/init.d/$_:$. $keyword"
180 if (defined $lsb{$keyword});
181 tag "init.d-script-has-unknown-lsb-keyword", "/etc/init.d/$_:$. $keyword"
182 unless (defined ($lsb_keywords{$keyword}) || $keyword =~ /^x-/);
183 $lsb{$keyword} = $value || '';
185 } elsif ($l =~ /^\#(\t| )/ && $last eq 'description') {
187 $value =~ s/^\#\s*//;
188 $lsb{description} .= ' ' . $value;
190 tag "init.d-script-has-bad-lsb-line", "/etc/init.d/$_:$.";
195 while ($l =~ s/(start|stop|restart|force-reload)//o) {
201 # Make sure all of the required keywords are present.
202 if (not $lsb{BEGIN}) {
203 tag "init.d-script-missing-lsb-section", "/etc/init.d/$_";
205 for my $keyword (keys %lsb_keywords) {
206 if ($lsb_keywords{$keyword} && !defined $lsb{$keyword}) {
207 if ($keyword eq 'short-description') {
208 tag "init.d-script-missing-lsb-short-description", "/etc/init.d/$_";
210 tag "init.d-script-missing-lsb-keyword", "/etc/init.d/$_ $keyword";
216 # Check the runlevels.
218 if ($lsb{'default-start'}) {
219 for my $runlevel (split (/\s+/, $lsb{'default-start'})) {
220 if ($runlevel =~ /^[sS0-6]$/) {
221 $start{lc $runlevel} = 1;
223 tag "init.d-script-has-bad-start-runlevel", "/etc/init.d/$_ $runlevel";
227 if ($lsb{'default-stop'}) {
228 for my $runlevel (split (/\s+/, $lsb{'default-stop'})) {
229 if ($runlevel =~ /^[sS0-6]$/) {
230 if ($start{$runlevel}) {
231 tag "init.d-script-has-conflicting-start-stop", "/etc/init.d/$_ $runlevel";
233 if ($runlevel =~ /[sS]/) {
234 tag "init-d-script-stops-in-s-runlevel", "/etc/init.d/$_";
237 tag "init.d-script-has-bad-stop-runlevel", "/etc/init.d/$_ $runlevel";
242 # all tags included in file?
243 $tag{'start'} or tag "init.d-script-does-not-implement-required-option", "/etc/init.d/$_ start";
244 $tag{'stop'} or tag "init.d-script-does-not-implement-required-option", "/etc/init.d/$_ stop";
245 $tag{'restart'} or tag "init.d-script-does-not-implement-required-option", "/etc/init.d/$_ restart";
246 $tag{'force-reload'} or tag "init.d-script-does-not-implement-required-option", "/etc/init.d/$_ force-reload";
248 tag "init.d-script-not-included-in-package", "/etc/init.d/$_";
252 # files actually installed in /etc/init.d should match our list :-)
253 opendir(INITD, "init.d") or fail("cannot read init.d directory: $!");
254 for (readdir(INITD)) {
255 next if $_ eq '.' || $_ eq '..';
256 tag "script-in-etc-init.d-not-registered-via-update-rc.d", "/etc/init.d/$_"
257 unless $initd_postinst{$_};
266 # indent-tabs-mode: t
267 # cperl-indent-level: 4
269 # vim: syntax=perl ts=8