#! /usr/bin/env perl

my %pwds = ();
my %seen_stat = ();
my %seen_open = ();

use File::Spec;

sub make_abs($$)
{
    my ($pwd, $f) = @_;
    if (!File::Spec->file_name_is_absolute( $ f)) {
	$f = File::Spec->catfile($pwd, $f);
    }
    return File::Spec->canonpath($f);
}

while ( <STDIN> )
{
    if (m/^(\d*)\s*(\w*)\("([^"]*)"/)
    {
       my $pid = $1;
       my $syscall = $2;
       my $arg = $3;
       if (!defined $pwds{$pid}) {
          $pwds{$pid} = "/";
       }
       $arg = make_abs($pwds{$pid}, $arg);
       if ($syscall eq "chdir") {
            $pwds{$pid} = $arg;
            next;
       }
       next if ($arg =~ m,^/proc/, || $arg =~ m,^/sys/, || $arg =~ m,^/dev/,);
       next if (grep(/^$syscall/, ("mkdir", "chown32", "chown", "unlink", "chmod", "mknod", "statfs", "rename", "rmdir", "link", "getcwd", "creat")));
       next if (defined $seen_open{$arg});
       if (grep(/^$syscall/, ("open", "execve"))) {
	   next if ($f =~ m,^/dev/,);
           $seen_open{$arg} = 1;
           print "open $arg\n";
       } elsif (grep(/^$syscall/, ("stat", "stat64", "access", "readlink", "lstat", "lstat64"))) {
            next if (defined $seen_stat{$arg});
            $seen_stat{$arg} = 1;
            print "stat $arg\n";
       } else {
         print "unknown syscall $syscall\n";
       }
    }
}
