| 1 | #! /usr/bin/perl -w | ||
| 2 | # | ||
| 3 | # children_fuser.pl -- find children of a given process | ||
| 4 | # that have opened a given file. | ||
| 5 | # | ||
| 6 | # 2010-07-31, jw - initial version (not using Proc::ProcessTable) | ||
| 7 | |||
| 8 | use Data::Dumper; | ||
| 9 | use Cwd; | ||
| 10 | |||
| 11 | my $file = shift || '/home/testy/.xsession-errors'; | ||
| 12 | my $parent = shift; | ||
| 13 | |||
| 14 | my $p = fuser($file, $parent); | ||
| 15 | fuser_offset($p); | ||
| 16 | print Dumper $p; | ||
| 17 | |||
| 18 | exit 0; | ||
| 19 | ############################################## | ||
| 20 | sub fuser | ||
| 21 | { | ||
| 22 | my ($file, $ppid) = @_; | ||
| 23 | $ppid ||= 1; | ||
| 24 | $file = Cwd::abs_path($file); | ||
| 25 | |||
| 26 | opendir DIR, "/proc" or die "opendir /proc failed: $!\n"; | ||
| 27 | my %p = map { $_ => {} } grep { /^\d+$/ } readdir DIR; | ||
| 28 | closedir DIR; | ||
| 29 | |||
| 30 | # get all procs, and their parent pids | ||
| 31 | for my $p (keys %p) | ||
| 32 | { | ||
| 33 | if (open IN, "<", "/proc/$p/stat") | ||
| 34 | { | ||
| 35 | # don't care if open fails. the process may have exited. | ||
| 36 | my $text = join '', <IN>; | ||
| 37 | close IN; | ||
| 38 | if ($text =~ m{\((.*)\)\s+(\w)\s+(\d+)}s) | ||
| 39 | { | ||
| 40 | $p{$p}{cmd} = $1; | ||
| 41 | $p{$p}{state} = $2; | ||
| 42 | $p{$p}{ppid} = $3; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | # Weed out those who are not in our family | ||
| 48 | if ($ppid > 1) | ||
| 49 | { | ||
| 50 | for my $p (keys %p) | ||
| 51 | { | ||
| 52 | my $family = 0; | ||
| 53 | my $pid = $p; | ||
| 54 | while ($pid) | ||
| 55 | { | ||
| 56 | # Those that have ppid==1 may also belong to our family. | ||
| 57 | # We never know. | ||
| 58 | if ($pid == $ppid or $pid == 1) | ||
| 59 | { | ||
| 60 | $family = 1; | ||
| 61 | last; | ||
| 62 | } | ||
| 63 | last unless $p{$pid}; | ||
| 64 | $pid = $p{$pid}{ppid}; | ||
| 65 | } | ||
| 66 | delete $p{$p} unless $family; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | my %o; # matching open files are recorded here | ||
| 71 | |||
| 72 | # see what files they have open | ||
| 73 | for my $p (keys %p) | ||
| 74 | { | ||
| 75 | if (opendir DIR, "/proc/$p/fd") | ||
| 76 | { | ||
| 77 | my @l = grep { /^\d+$/ } readdir DIR; | ||
| 78 | closedir DIR; | ||
| 79 | for my $l (@l) | ||
| 80 | { | ||
| 81 | my $r = readlink("/proc/$p/fd/$l"); | ||
| 82 | next unless defined $r; | ||
| 83 | # warn "$p, $l, $r\n"; | ||
| 84 | if ($r eq $file) | ||
| 85 | { | ||
| 86 | $o{$p}{cmd} ||= $p{$p}{cmd}; | ||
| 87 | $o{$p}{fd}{$l} = { file => $file }; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | return \%o; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | # see if we can read the file offset of a file descriptor, and the size of its file. | ||
| 97 | sub fuser_offset | ||
| 98 | { | ||
| 99 | my ($p) = @_; | ||
| 100 | for my $pid (keys %$p) | ||
| 101 | { | ||
| 102 | for my $fd (keys %{$p->{$pid}{fd}}) | ||
| 103 | { | ||
| 104 | if (open IN, "/proc/$pid/fdinfo/$fd") | ||
| 105 | { | ||
| 106 | while (defined (my $line = <IN>)) | ||
| 107 | { | ||
| 108 | chomp $line; | ||
| 109 | $p->{$pid}{fd}{$fd}{$1} = $2 if $line =~ m{^(\w+):\s+(.*)\b}; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | close IN; | ||
| 113 | $p->{$pid}{fd}{$fd}{size} = -s $p->{$pid}{fd}{$fd}{file}; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } |