#!/usr/bin/perl # Copyright (C) 2006 Carlos Villegas and Petter Reinholdtsen # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # # # # Author: Petter Reinholdtsen # Author: Carlos Villegas # Created: 2006-06-26 # Last updated: 2006-07-29 # # check-initd-order from Petter Reinholdtsen used as template. # # Read init script headers and check if they are LSB compliant. # It will read the init scripts from the /etc/init.d directory by default. # # To see LSB compliance and information errors type: >>checkLSB # To see the LSB header information add -d, e.g. >>checkLSB -d # Possible cases: # Case1: No LSB headers found = When ### BEGIN INIT INFO is not found. # Case2: LSB compliance. - If the headers Provides, Required-Start, Required-Stop, Default-Start and Default-Stop exist and have arguments. Besides, if Default-Start is equal to 0 or to 6, LSB compliance is satisfied only with Provides, Required-Start and Default-Start. # Case3:Missing LSB headers: in the other cases. use strict; use warnings; use Getopt::Std; use File::Basename; my $initdir = "/etc/init.d"; #my $initdir = "/home/carlos/Academia/debian/soc2006-bootsystem/code/init.d"; #my $initdir = "/home/carlos/Academia/debian/init.d"; my $debug = 0; my %opts; getopts('d', \%opts); $debug = $opts{'d'}; checkLSB(); sub checkLSB { chdir "$initdir/."; for my $script (<*>) { # print "$script\n"; print STDOUT "\n-------------------------------------------\nLoading $initdir/$script\n"; # Example of LSB complaint headers: ### BEGIN INIT INFO # Provides: xdebconfigurator # Required-Start: $syslog # Required-Stop: $syslog # Should-Start: # Should-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 1 6 # Short-Description: Genererate xfree86 configuration at boot time # Description: Preseed X configuration and use dexconf to # genereate a new configuration file. ### END INIT INFO # open script and search for initial header. open(FILE, "<$script") or die "Unable to read $script"; my $found = 0; my ($start, $provides, $requiredstart, $requiredstop, $shouldstart, $shouldstop, $defaultstart, $defaultstop, $shortdescription, $description, $ending); while () { chomp; #print "Reading a line of $found\n"; $found = 1 if (m/^\#\#\# BEGIN INIT INFO/); next unless ($found); if (m/^\#\#\# END INIT INFO/){ $ending = 1; last; } # Check for every LSB header and for them not being empty if (m/^\# provides:/i){ $provides=$1 if (m/^\# provides:\s+(\S*.*\S+)\s*$/i); $provides="" unless ($provides); } if (m/^\# required-start:/i){ $requiredstart=$1 if (m/^\# required-start:\s+(\S*.*\S+)\s*$/i); $requiredstart="" unless ($requiredstart); } if (m/^\# required-stop:/i){ $requiredstop=$1 if (m/^\# required-stop:\s+(\S*.*\S+)\s*$/i); $requiredstop="" unless ($requiredstop); } if (m/^\# should-start:/i){ $shouldstart=$1 if (m/^\# should-start:\s+(\S*.*\S+)\s*$/i); $shouldstart="" unless ($shouldstart); } if (m/^\# should-stop:/i){ $shouldstop=$1 if (m/^\# should-stop:\s+(\S*.*\S+)\s*$/i); $shouldstop="" unless ($shouldstop); } if (m/^\# default-start:/i){ $defaultstart=$1 if (m/^\# default-start:\s+(\S*.*\S+)\s*$/i); $defaultstart="" unless (defined $defaultstart); } if (m/^\# default-stop:/i){ $defaultstop=$1 if (m/^\# default-stop:\s+(\S*.*\S+)+\s*$/i); $defaultstop="" unless (defined $defaultstop); } if (m/^\# short-description:/i){ $shortdescription=$1 if (m/^\# short-description:\s+(\S*.*\S+)\s*$/i); $shortdescription="" unless ($shortdescription); } if (m/^\# description:/i){ $description=$1 if (m/^\# description:\s+(\S*.*\S+)\s*$/i); $description="" unless ($description); } } unless ($found) { print "LSB headers missing in $initdir/$script\n"; print "---------------------------------------\n"; next; } print "\nProvides=$provides\n" if (defined $provides && $debug); print "Required-Start=$requiredstart\n" if (defined $requiredstart && $debug); print "Required-Stop=$requiredstop\n" if (defined $requiredstop && $debug); print "Default-Start=$defaultstart\n" if (defined $defaultstart && $debug); print "Default-Stop=$defaultstop\n" if (defined $defaultstop && $debug); print "Should-Start=$shouldstart\n" if (defined $shouldstart && $debug); print "Should-Stop=$shouldstop\n" if (defined $shouldstop && $debug); print "Short-Description=$shortdescription\n" if (defined $shortdescription && $debug); print "Description=$description\n\n" if (defined $description && $debug); if ((($defaultstart =~ /[06]/) && ($provides && $requiredstart && $ending)) || ($provides && $requiredstart && $requiredstop && $defaultstart && ($defaultstop || $defaultstop eq 0) && $ending)) { print "LSB Compliance in $initdir/$script.\n\n"; } else { print "Missing LSB headers in $initdir/$script.\n \n"; } if (defined $provides) { print STDOUT "Provides LSB header has no arguments.\n" unless $provides;} else { print STDOUT "Provides LSB header missing.\n";} if (defined $requiredstart) { print STDOUT "Required-start LSB header has no arguments.\n" unless $requiredstart;} else { print STDOUT "Required-Start LSB header missing.\n";} if (defined $requiredstop) { if (!$requiredstop) { print STDOUT "Required-Stop LSB header has no arguments.\n";} } else { print STDOUT "Required-Stop LSB header missing.\n";} if (defined $shouldstart) { print STDOUT "Should-Start LSB header has no arguments.\n" unless $shouldstart;} else { print STDOUT "Should-Start LSB header missing.\n";} if (defined $shouldstop) { if (!$shouldstop) { print STDOUT "Should-Stop LSB header has no arguments.\n" ;} } else { print STDOUT "Should-Stop LSB header missing.\n";} if (defined $defaultstart) { print STDOUT "Default-Start LSB header has no arguments.\n" unless ($defaultstart || $defaultstart eq '0');} else { print STDOUT "Default-Start LSB header missing.\n";} if (defined $defaultstop) { print STDOUT "Default-Stop LSB header has no arguments.\n" unless ($defaultstop || $defaultstop eq '0' );} else { print STDOUT "Default-Stop LSB header missing.\n";} if (defined $shortdescription) { print STDOUT "Short-Description LSB header is empty.\n" unless $shortdescription;} else { print STDOUT "Short-Description LSB header missing.\n";} if (defined $description) { print STDOUT "Description LSB header is empty.\n" unless $description;} else { print STDOUT "Description LSB header missing.\n";} print STDOUT "LSB headers were not closed. Add after the other headers: ### END INIT INFO.\n" unless $ending; print "------------------------------------------------\n"; close(FILE); } } # my $lsbtags = # ( # 'start' => $start, # 'provides' => $provides, # 'required-start' => $requiredstart, # 'required-stop' => $requiredstop, # 'should-start' => $shouldstart, # 'should-stop' => $shouldstop, # 'default-start' => $defaultstart, # 'default-stop' => $defaultstop, # 'shortdescription'=> $shortdescription, # 'description' => $description, # 'ending' => $ending, # );