Back to home page

LXR

 
 

    


0001 #!/usr/bin/perl -w
0002 #
0003 # headers_check.pl execute a number of trivial consistency checks
0004 #
0005 # Usage: headers_check.pl dir arch [files...]
0006 # dir:   dir to look for included files
0007 # arch:  architecture
0008 # files: list of files to check
0009 #
0010 # The script reads the supplied files line by line and:
0011 #
0012 # 1) for each include statement it checks if the
0013 #    included file actually exists.
0014 #    Only include files located in asm* and linux* are checked.
0015 #    The rest are assumed to be system include files.
0016 #
0017 # 2) It is checked that prototypes does not use "extern"
0018 #
0019 # 3) Check for leaked CONFIG_ symbols
0020 
0021 use strict;
0022 use File::Basename;
0023 
0024 my ($dir, $arch, @files) = @ARGV;
0025 
0026 my $ret = 0;
0027 my $line;
0028 my $lineno = 0;
0029 my $filename;
0030 
0031 foreach my $file (@files) {
0032     $filename = $file;
0033 
0034     open(my $fh, '<', $filename)
0035         or die "$filename: $!\n";
0036     $lineno = 0;
0037     while ($line = <$fh>) {
0038         $lineno++;
0039         &check_include();
0040         &check_asm_types();
0041         &check_sizetypes();
0042         &check_declarations();
0043         # Dropped for now. Too much noise &check_config();
0044     }
0045     close $fh;
0046 }
0047 exit $ret;
0048 
0049 sub check_include
0050 {
0051     if ($line =~ m/^\s*#\s*include\s+<((asm|linux).*)>/) {
0052         my $inc = $1;
0053         my $found;
0054         $found = stat($dir . "/" . $inc);
0055         if (!$found) {
0056             $inc =~ s#asm/#asm-$arch/#;
0057             $found = stat($dir . "/" . $inc);
0058         }
0059         if (!$found) {
0060             printf STDERR "$filename:$lineno: included file '$inc' is not exported\n";
0061             $ret = 1;
0062         }
0063     }
0064 }
0065 
0066 sub check_declarations
0067 {
0068     # soundcard.h is what it is
0069     if ($line =~ m/^void seqbuf_dump\(void\);/) {
0070         return;
0071     }
0072     # drm headers are being C++ friendly
0073     if ($line =~ m/^extern "C"/) {
0074         return;
0075     }
0076     if ($line =~ m/^(\s*extern|unsigned|char|short|int|long|void)\b/) {
0077         printf STDERR "$filename:$lineno: " .
0078                   "userspace cannot reference function or " .
0079                   "variable defined in the kernel\n";
0080     }
0081 }
0082 
0083 sub check_config
0084 {
0085     if ($line =~ m/[^a-zA-Z0-9_]+CONFIG_([a-zA-Z0-9_]+)[^a-zA-Z0-9_]/) {
0086         printf STDERR "$filename:$lineno: leaks CONFIG_$1 to userspace where it is not valid\n";
0087     }
0088 }
0089 
0090 my $linux_asm_types;
0091 sub check_asm_types
0092 {
0093     if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
0094         return;
0095     }
0096     if ($lineno == 1) {
0097         $linux_asm_types = 0;
0098     } elsif ($linux_asm_types >= 1) {
0099         return;
0100     }
0101     if ($line =~ m/^\s*#\s*include\s+<asm\/types.h>/) {
0102         $linux_asm_types = 1;
0103         printf STDERR "$filename:$lineno: " .
0104         "include of <linux/types.h> is preferred over <asm/types.h>\n"
0105         # Warn until headers are all fixed
0106         #$ret = 1;
0107     }
0108 }
0109 
0110 my $linux_types;
0111 my %import_stack = ();
0112 sub check_include_typesh
0113 {
0114     my $path = $_[0];
0115     my $import_path;
0116 
0117     my $fh;
0118     my @file_paths = ($path, $dir . "/" .  $path, dirname($filename) . "/" . $path);
0119     for my $possible ( @file_paths ) {
0120         if (not $import_stack{$possible} and open($fh, '<', $possible)) {
0121         $import_path = $possible;
0122         $import_stack{$import_path} = 1;
0123         last;
0124         }
0125     }
0126     if (eof $fh) {
0127         return;
0128     }
0129 
0130     my $line;
0131     while ($line = <$fh>) {
0132         if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
0133             $linux_types = 1;
0134             last;
0135         }
0136         if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
0137             check_include_typesh($included);
0138         }
0139     }
0140     close $fh;
0141     delete $import_stack{$import_path};
0142 }
0143 
0144 sub check_sizetypes
0145 {
0146     if ($filename =~ /types.h|int-l64.h|int-ll64.h/o) {
0147         return;
0148     }
0149     if ($lineno == 1) {
0150         $linux_types = 0;
0151     } elsif ($linux_types >= 1) {
0152         return;
0153     }
0154     if ($line =~ m/^\s*#\s*include\s+<linux\/types.h>/) {
0155         $linux_types = 1;
0156         return;
0157     }
0158     if (my $included = ($line =~ /^\s*#\s*include\s+[<"](\S+)[>"]/)[0]) {
0159         check_include_typesh($included);
0160     }
0161     if ($line =~ m/__[us](8|16|32|64)\b/) {
0162         printf STDERR "$filename:$lineno: " .
0163                       "found __[us]{8,16,32,64} type " .
0164                       "without #include <linux/types.h>\n";
0165         $linux_types = 2;
0166         # Warn until headers are all fixed
0167         #$ret = 1;
0168     }
0169 }