source: projects/setup/trunk/serviceslint @ 8717

Revision 8717, 3.1 KB checked in by daisuke, 8 years ago (diff)

add initial version

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl
2#
3# Perform sanity checks on the services file, supplied as argument.
4# Based on an earlier shell script of the same name, but much faster,
5# and it also detects actual errors in the current services file :)
6#
7# This program includes a manual, run "perldoc serviceslint" to see it.
8#
9
10use strict;
11use warnings;
12
13die "Usage: $0 /etc/services\n" unless $#ARGV == 0;
14
15# Build a hash of known protocols
16my %protocol;
17open FH, "protocols" or die "cannot open protocols: $!\n";
18while (<FH>) {
19        chomp;
20        s/#.*$//;
21        my ($name, $port) = m/([\S]+)\s+(\d+)/ or next;
22        $protocol{$name} = $port;
23}
24close FH;
25
26# Parse the supplied services file
27my $retval = 0;
28my $line = 0;
29my %service;
30open FH, $ARGV[0] or die "cannot open $ARGV[0]: $!\n";
31while (<FH>) {
32        $line++;                                    # Keep a line count
33        chomp;                                      # Remove CR/LF chars
34        if (m/^\s+/) {
35                print "Malformed line $line\n";     # No leading whitespace
36                $retval = 1;
37                next;
38        }
39        s/\s*#.*$//;                                # Strip out comments
40        next if m/^$/;                              # Skip empty lines
41        my ($name, $port, $proto, $aliases) =       # Primary pattern match
42                m/^([\S]+)\s+(\d+)\/(\w+)\s*(.*)/
43                or die "Malformed line: $line\n";
44        if (not exists $protocol{$proto}) {
45                print "Bad protocol at line $line: $proto\n";
46                $retval = 1;
47        }
48        if (exists $service{$proto}{$port}) {
49                print "Duplicate port at line $line: $port/$proto\n";
50                $retval = 1;
51        }
52        $service{$proto}{$port} = $name;
53        foreach ($name, split /\s+/, $aliases) {
54                if (exists $service{$proto}{$_}) {
55                        print "Duplicate name at line $line: $_/$proto\n";
56                        $retval = 1;
57                }
58                $service{$proto}{$_} = $port;
59        };
60       
61}
62close FH;
63exit $retval;
64
65__END__
66
67=head1 NAME
68
69serviceslint - perform verification on the /etc/services file
70
71=head1 SYNOPSIS
72
73B<serviceslint> I<filename>
74
75=head1 DESCRIPTION
76
77The B<serviceslint> command performs syntax and content checks on the
78given filename, normally a copy of the I</etc/services> file.
79
80Syntax checking consists of a regular expression applied to
81non-empty, non-comment lines.  If the syntax check fails, then
82the program prints a message and aborts with non-zero status code.
83
84Content checking detects various kinds of duplicate entries.
85Currently, warnings are printed for duplicate entries, but execution
86continues, and the program I<exits with status code zero> (eg. success).
87
88=over
89
90=item B<Malformed line> I<NNN>
91
92The specified line has invalid syntax.  Note that leading whitespace
93is not permitted.  Non-empty lines must begin with a comment, or with
94a service name followed by a port number / protocol pair.
95
96=item B<Duplicate port at line> I<NNN>
97
98Occurs when a port number / protocol pair is found more than once
99in the services file.  The warning is flagged on the second (and any
100subsequent) occurrences.  These entries will not be found via the
101B<getservbyport()> function.
102
103=item B<Duplicate name at line> I<NNN>
104
105Occurs when a service name, or alias, occurs more than once in the
106services file.  The warning is flagged on the second (and subsequent)
107occurrence.  These entries will not be returned by the B<getservbyname()>
108function.
109
110=back
111
112=head1 SEE ALSO
113
114The services(5) man page describes the file format.
115
116=head1 AUTHOR
117
118Ralph Siemsen & Phil Knirsch
119
Note: See TracBrowser for help on using the repository browser.