OpenSolaris_b135/tools/depcheck/get_depend_info

#!/usr/bin/perl
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2000 by Sun Microsystems, Inc.
# All rights reserved.
#

# ident	"%Z%%M%	%I%	%E% SMI"

#
# check for perl5 -- we use things unavailable in perl4
#

die "Sorry, this program requires perl version 5.000 or up. You have $]. Stopping" if $] < 5.000;

$DBM_DIR_CHARACTERIZATION = "directory for the dbm databases";

$Usage =
"Usage: get_depend_info 
  -dbdir <$DBM_DIR_CHARACTERIZATION>  [ -s ] [ -cons ] [ -root directory ]
  [ -f ] [ -p ] [ -pkg SUNWxxx ] [ filename ]
  [-h for help]\n";

$Help =
"This program statically analyzes executable files and their
symbolic links using /usr/bin/ldd and /usr/bin/strings.  It
can accept filename(s) or packages as the list of files to be
analyzed.  By default, the program will report the file
dependencies and which packages those dependencies came from.
There is one required argument:

        -dbdir  <dir>			the $DBM_DIR_CHARACTERIZATION

The optional argument -h produces this message instead of any processing.
The optional argument -cons tells the tool to be conservative and not to
run /usr/bin/strings.
The optional argument -root allows you to specify a new root (useful for
doing analysis on build trees).
The optional argument -pkg allows you to specify a package name.
The optional argument -f only outputs the filename of the dependencies
The optional argument -p only outputs the packanames of the dependencies

The optional argument -s ONLY outputs symbolic links for files or packages.
No ldd or strings analysis is done.

Some Examples:
get_depend_info -dbdir ./DBM /usr/bin/ls
get_depend_info -dbdir ./DBM /usr/bin/*
get_depend_info -dbdir ./DBM -pkg SUNWnisu
get_depend_info -f -dbdir ./DBM -pkg SUNWnisu
get_depend_info -s -dbdir ./DBM /usr/bin/*
get_depend_info -s -dbdir ./DBM -pkg SUNWnisu


NOTE: Run make_pkg_db to create the database directory for get_depend_info
";

#
# process arguments
#

@PkgList = "";
$PackageOnly = false;
$FileOnly = false;
$Verbose = true;
$Silent = false;
$SymLink = false;
$NoStrings = false;
$Root = "";

while (@ARGV) {
    $arg = shift (@ARGV);
    if ($arg eq "-h") {
        print "$Help\n$Usage";
        exit 0;
    } elsif ($arg eq "-dbdir") {
        $DBDir = shift(@ARGV) unless ($ARGV[0] =~ /^-/);
    } elsif ($arg eq "-s") {
	$SymLink = true;
	$Silent = true;
    } elsif ($arg eq "-p") {
	$PackageOnly = true;
	$Verbose = false;
    } elsif ($arg eq "-f") {
	$FileOnly = true;
	$Verbose = false;
    } elsif ($arg eq "-cons") {
	$NoStrings = true;
    } elsif ($arg eq "-pkg") {
        $PKGName = shift(@ARGV) unless ($ARGV[0] =~ /^-/);
    } elsif ($arg eq "-root") {
        $Root = shift(@ARGV) unless ($ARGV[0] =~ /^-/);
    }else {
	push(@filelist, $arg);
    }
}

if (!$DBDir) {
    print STDERR "Required argument -dbdir missing. \n$Usage";
    exit 1;
}

if ($PKGName) {
    # For a given pkg definition directory, this subroutine will
    # go through the proto files and look for executable files.
    # It will then put all the executable files into @filelist
    &HandlePackageName($PKGName);

    if ($PackageOnly eq true) {
        $Silent = true;
    }
}

&OpenDBs;

$Silent = true if (($Verbose eq false) && ($PackageOnly eq false)
			&& ($FileOnly eq false));

foreach $entry (@filelist) {

	print("\n\nAnalyzing $Root$entry:\n") unless ($Silent eq true);

        # make sure file exists
	if (!(-r $entry)) {
    		print STDERR "Could not open file $entry\n";
    		next;
	}


	$file = $FTYPE{$entry};
	$pkgs = $PKGS{$entry};
	$abslink = $ABSLINK{$entry};

	if ($file eq "d") {
		print("Input file is a directory\n") unless ($Silent eq true);
    		next;
	}

	# destfile represents the actual file we are interested in!
	if ($abslink =~ /\w/) {
		$destfile = $abslink;

		if (($FTYPE{$entry} eq "s") && ($SymLink eq true)) {
			print("$entry is linked to $destfile:$PKGS{$destfile}\n");
		}
	}
	else {
		$destfile = $entry;
	}

	# if the -s flag is set, tell 'em about sym links and go to the next file
	next if ($SymLink eq true);

	$mode = $MODE{$destfile};

        # Handle the case where the user resets $ROOT
	$destfile = "$Root$destfile" if ($Root =~ /\w/);
	$filecmd = `/usr/bin/file $destfile 2>&1`;

	# make sure we are dealing with an executable
	if (($mode !~ /(.)(.*)7/) && ($mode !~ /(.)(.*)5/) && ($mode !~ /(.)(.*)3/) && ($mode !~ /(.)(.*)1/)){
		print("Input file is not an executable\n");
		next;
	}

        # Kernel modules are handled separately
	if ($destfile =~ /\/kernel\//) {
		&HandleKernelMod($destfile, $FTYPE{$entry});
		&OutputPackageList if (($PackageOnly eq true) && !($PKGName));
		next;
	}

	# take care of scripts
	if (($filecmd =~ /script/) || ($filecmd =~ /text/)) {
		&HandleScripts($destfile);
		&OutputPackageList if (($PackageOnly eq true) && !($PKGName));
		next;
	}

	# Its not a script, not a kernel module, so its get to be a binary
	&HandleBinaries($destfile);
	&OutputPackageList if (($PackageOnly eq true) && !($PKGName));
}

if (($PKGName) && ($SymLink eq false)) {
	print ("\n\nPackage dependencies for $PKGName:\n");
	&OutputPackageList;
}

&CloseDBs;

#===========================END OF MAIN====================================

sub GetLddInfo {		# return multi-line string of ldd info for File
local ($FileID, $FileType) = @_;

    $outstring = "* Not a File\n";
    return ($outstring) if $FileType =~ /[Mlsdc]/;	# ldd results not useful here

    #
    # use map file to see if this is a file that gives a known bad ldd return
    #

#    if ($Unsup{$FileID} == 1) {
#	$outstring = "* unsupported or unknown file type, per map file";
#	return ($outstring);
#    }
#    $err = "";
#    $string = `/usr/bin/ldd $FileID 2>&1`;
#    if ($?) {	# if some error (don't just get wait status here)
#	$errnum = 0 + $!;
#	$err = "==$?==$errnum==";
#	if (($err eq "==256==29==") || ($err eq "==256==0==")) {
#	    $err = "*";			# these are normal ldd returns
#	} else {
#	    die "Unexpected ldd return $? $!";
#	}
#	$string =~ s/\/usr\/bin\/ldd:[^\0]*://g;	# trim up error line
#    } elsif ($string =~ s/warning:.*://) {	# other normal ldd returns
#	$err = "*";
#    }

    $outstring = "";
    $string = `/usr/bin/ldd $FileID 2>&1`;
    # on a non-zero ldd, return nothing
    return ($outstring) if ($?);


    $outstring = "";
    @infolines = split(/\s*\n\s*/, $string);
    foreach $line (@infolines) {
	$line =~ s/^\s+//;			# trim leading ws
	next unless $line;			# skip if blank
	@words = split(/\s/, $line);
	$filename = $words[0];
	$outstring .= "$filename\n";
    }
    return ($outstring);
}

sub CloseDBs {
	# close the dbs
	dbmclose(FTYPE);
	dbmclose(MODE);
	dbmclose(PKGS);
	dbmclose(ABSLINK);
	dbmclose(PKGNAMES);
}

sub OpenDBs {
	# open the databases for read-only
	dbmopen(%FTYPE, "$DBDir/FTYPE", 0664) ||
		die"Cannot open dbm db $DBDir/FTYPE\n";

	dbmopen(%MODE, "$DBDir/MODE", 0664) ||
		die"Cannot open dbm db $DBDir/MODE\n";

	dbmopen(%PKGS, "$DBDir/PKGS", 0664) ||
		die"Cannot open dbm db $DBDir/PKGS\n";

	dbmopen(%ABSLINK, "$DBDir/ABSLINK", 0664) ||
		die"Cannot open dbm db $DBDir/ABSLINK \n";

	dbmopen(%PKGNAMES, "$DBDir/PKGNAMES", 0644) ||
		die"Cannot open dbm db $DBDir/PKGNAMES\n";
}

sub HandleKernelMod {
local ($entry, $ftype) = @_;

	# search for the magic right, starting from the right (ie. end of path)
	$index = rindex($entry, "kernel");
	# rindex() returns where the "kernel" began, add 6 to get
	# "{some path}/kernel"
	$index += 6;
	# OK, now pull out the absolute path
	$KernelPath = substr($entry, 0, $index);

	# There are two ways to figure out the dependencies.
	# First, we check to see if /usr/bin/ldd will tell us.
	# If ldd fails, then we need to look at the output of /usr/bin/strings

	$LddInfo = "";
	$LddInfo = &GetLddInfo($entry, $ftype);

	if ($LddInfo =~ /\w/) {
		@list = "";
		@list = split(/\n/, $LddInfo);
		foreach $file (@list) {
			$found = 0;

			# first, check to see if there is a module relative to
			# this file
			if ($FTYPE{"$KernelPath/$file"} =~ /\w/){
				&Output("$KernelPath/$file");
				$found++;
			}

			# Haven't found it yet, check /kernel
			if (($FTYPE{"/kernel/$file"} =~ /\w/) && ($found == 0)){
				&Output("/kernel/$file");
				$found++;
			}

			# Haven't found it yet, check /usr/kernel
			if (($FTYPE{"/usr/kernel/$file"} =~ /\w/) && ($found == 0)){
				&Output("/usr/kernel/$file");
				$found++;
			}

			if ($found == 0) {
				print("Could not resolve $file\n");
			}
		}
		return;
	}

	# the ldd failed, so now let's look at the string output
	$string = "";
	@infolines = "";
	@outlines = "";

    	$string = `/usr/bin/strings $entry 2>&1`;
    	@infolines = split(/\s*\n\s*/, $string);

	foreach $line (@infolines) {
		if ($line =~ /\//){
			push (@outlines,$line);
		}
    	}

	foreach $line (@outlines) {
		@words = split(/\s/, $line);
		foreach $word (@words) {
			$found = 0;

			# first, check to see if there is a module relative to
			# this file
			if ($FTYPE{"$KernelPath/$word"} =~ /\w/){
				&Output("$KernelPath/$word");
				$found++;
			}

			# Haven't found it yet, check /kernel
			if (($FTYPE{"/kernel/$word"} =~ /\w/) && ($found == 0)){
				&Output("/kernel/$word");
				$found++;
			}

			# Haven't found it yet, check /usr/kernel
			if (($FTYPE{"/usr/kernel/$word"} =~ /\w/) && ($found == 0)){
				&Output("/usr/kernel/$word");
				$found++;
			}
		}
	}
}

sub GetStringsInfo {		# return multi-line string of ldd info for File
local ($FileID, $FileType) = @_;

    $outstring = "* Not a File\n";
    return ($outstring) if $FileType =~ /[Mlsdc]/;	# ldd results not useful here
    return ($outstring) if ($NoStrings eq true);	# we are running in conservative mode

    # use map file to see if this is a file that gives a known bad ldd return
    if ($Unsup{$FileID} == 1) {
	$outstring = "* unsupported or unknown file type, per map file";
	return ($outstring);
    }
    $err = "";
    $string = "";
    $string = `/usr/bin/strings $FileID 2>&1`;

    $outstring = "";
    @infolines = "";
    @outlines = "";
    @infolines = split(/\s*\n\s*/, $string);

    foreach $line (@infolines) {
	if (($line =~ /\//) && !($line =~ /%/) && !($line =~ m%/$%)){
		push (@outlines,$line);
        }
    }
    @outlines = sort(@outlines);

    foreach $word (@outlines) {
	if ($lastword ne $word) {
		$outstring .= $word; $outstring .= "\n";
	}
	$lastword = $word;
    }
    return ($outstring);
}

sub HandleScripts {
local ($filename) = @_;
	open(SCRIPT, $filename);

	undef @output;
	while (<SCRIPT>) {
		s/^\s+//;		# trim leading ws
		s/=/ /g;		# get rid of all =
		s/\`/ /g;		# get rid of all `
		next if ($_ =~ /^#/);	# strip out obvious comments
		next unless $_;		# skip if blank

		$line = $_;
		@words = split(/\s/, $line);
		foreach $word (@words) {
			if (($PKGS{$word} =~ /\w/) && ($FTYPE{$word} ne "d")) {
				push(@output, $word);
			}
		}
	}

	@output = sort(@output);
	$count = 0;

	# make sure we don't output dupes
	foreach $file (@output) {
		if ($count == 0) {
			&Output($file);
		}

		if (($count > 0) && ($output[$count] ne $output[$count-1])) {
			&Output($file);
		}
		$count++;
	}

	# remember to play nice
	close(SCRIPT);
}

sub HandleBinaries {
local ($filename) = @_;
	$LddInfo = &GetLddInfo($destfile, $FTYPE{$filename});
	$StringInfo = &GetStringsInfo($destfile, $FTYPE{$filename});

	# Parse the ldd output.
	# Libs can be found in /kernel or /usr/lib
	@list = split(/\n/, $LddInfo);
	foreach $file (@list) {
		$found = 0;
		if ($FTYPE{"/kernel/$file"} =~ /\w/){
			&Output("/kernel/$file");
			$found++;
		}

		if ($FTYPE{"/usr/lib/$file"} =~ /\w/){
			&Output("/usr/lib/$file");
			$found++;
		}

		if ($found == 0) {
			print("Could not resolve $file\n");
		}
	}

	# For the strings output, we parse it to see if we can match it to
	# any files distributed in a package.
	@list = split(/\n/, $StringInfo);
	foreach $file (@list) {
		if (($FTYPE{$file} =~ /\w/) && ($FTYPE{$file} ne "d")) {
			&Output($file);
		}
	}
}

sub Output {
local($filename) = @_;
	
        # If they want a package listing, a unique sorted list
        # will be outputted later.  Here we simply push elements onto
	# this list.
	if ($PKGName) {
		push(@PkgList, "$PKGS{$filename}\n");
	}

	if ($Verbose eq true) {
		print("$filename:$PKGS{$filename}\n");
		return;
	}

        # If they want a package listing, a unique sorted list
        # will be outputted later.  Here we simply push elements onto
	# this list.
	if ($PackageOnly eq true) {
		push(@PkgList, "$PKGS{$filename}\n");
		return;
	}

	if ($FileOnly eq true) {
		print("$filename\n");
		return;
	}
}

sub HandlePackageName {
local($pkg) = @_;
	$pkgchk = `/usr/sbin/pkgchk -l $pkg | grep Pathname | sed 's/Pathname: //'`;

	@files = split(/\n/, $pkgchk);
	foreach $file (@files) {
		push(@filelist, $file);
	}
}

sub OutputPackageList {
local($filename) = @_;
	# If the user specified a package list, here we sort
	# the list and make sure we don't output dupes.
	$lastpkg = "";
	@outPkgs = sort(@PkgList);

	foreach $pkg (@outPkgs) {
		$pkg =~ s/\s*$//;   # trim extra space off the end

		# make sure this entry isn't a dupe before
		# printing it
		if ($lastpkg ne $pkg) {
			print("P $pkg\t$PKGNAMES{$pkg}\n");
		}

		$lastpkg = $pkg;
	}

	# reset the list for the next entry
	@PkgList = "";
}