-
Notifications
You must be signed in to change notification settings - Fork 1
/
_lsof-deleted
executable file
·305 lines (270 loc) · 7.8 KB
/
_lsof-deleted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/perl -w
# Thu Sep 24 17:42:50 EDT 2009
(my $email='XXX%YYY,ch')=~ tr/%,/@./;
use strict;
$0=~ /(.*?)([^\/]+)\z/s or die "?";
my ($mydir, $myname)=($1,$2);
sub usage {
print STDERR map{"$_\n"} @_ if @_;
print "$myname [-- lsof-arguments]
Show deleted files opened by processor, filtering the output of
lsof, relying on it's at the time used convention to display such
files.
The aim is to find programs that have object files open that have
been replaced by a package upgrade. For this reason, ignores
/(tmp|dev|var|run)/ and a couple other uninteresting paths.
(It is more precisely filtering lsof output, but otherwise doing the
same as 'lsof |grep DEL' (but there is code to alternatively do our
own stat calls, too, see source) and ignoring uninteresting paths,
too.)
Options:
--verbose
--more Show cases of deleted files which could not have been
touched by system package upgrades, too (e.g. in
home directories). By default only cases caused by system
upgrades are considered.
--style-friendly show in a grouped manner (default)
--style-lsof show in an lsof-like style ('denormalized' in lines)
(Christian Jaeger <$email>)
";
exit (@_ ? 1 : 0);
}
use Getopt::Long;
our $verbose = 0;
my $opt_more = 0;
my $style = "friendly";
GetOptions("verbose"=> \$verbose,
"help"=> sub{usage},
"more"=> sub { $opt_more++ },
"style-lsof" => sub { $style = "lsof" },
"style-friendly" => sub { $style = "friendly" },
) or exit 1;
#usage unless @ARGV;
sub uid_to_possibly_username {
my ($uid)=@_;
if(my $nam= getpwuid($uid)) {# [is this slow?shouldIcache?]
$nam
} else {
$uid # "<$uid>" ?
}
}
sub pid_to_programname_orelse {
my ($pid, $orelse)= @_;
# /proc/2710/exe -> /usr/libexec/gvfs-udisks2-volume-monitor
# /proc/3061/exe -> /usr/bin/emacs-gtk
# or
# cat /proc/2710/cmdline |tr '\0' '\n'|head -1 => same
# cat /proc/3061/cmdline |tr '\0' '\n'|head -1 => emacs
my ($_pid) = $pid=~ /^(\d+)\z/ or die "bug";
if (open my $in, "<", "/proc/$_pid/cmdline") {
local $/ = "\0";
my @cmd = <$in>;
close $in or die $!;
chomp @cmd;
my ($cmd0) = @cmd;
$cmd0=~ s{.*/}{}s;
my $progname = do {
if ($cmd0 =~ /^(python|perl|lua|ruby)/) {
# an interpreter; instead try to find the script argument
# (either literal code, or path): simply skip all options
# (how quickly will this fail?).
my @args = @cmd;
shift @args;
while (@args) {
if ($args[0]=~ /^-/) {
shift @args
} else {
last
}
}
if (@args) {
$args[0]
} else {
# fall back (maybe running as a repl?)
$cmd0
}
} else {
$cmd0
}
};
if (defined (my $l = readlink "/proc/$_pid/exe")) {
$l=~ s/ \(deleted\)$//;
"$progname ($l)"
} else {
"$progname ($cmd[0])"
}
} else {
$orelse
}
}
sub mkJustify {
my ($adjoin)=@_;
sub {
my ($width, $val)=@_;
my $len= length $val;
if ($len>= $width) {
$val
} else {
&$adjoin(" "x($width-$len), $val)
}
}
}
*JustifyLeft= mkJustify
(sub {
my ($sp,$val)=@_;
$val.$sp
});
*JustifyRight= mkJustify
(sub {
my ($sp,$val)=@_;
$sp.$val
});
use Chj::IO::Command;
use Chj::xperlfunc ':all';
our $rely_on_lsof_DEL=1; # whether to rely on DEL reporting.
sub perhaps_chase ($) {
my $c= Chj::IO::Command->new_sender ("chase",@_);
my $path= $c->xcontent;
chomp $path;
$c->xfinish == 0 ? $path : ()
}
our $ignore=
+{ map{$_=>1}
'/SYSV00000000',
'/drm mm object',
perhaps_chase ('/usr/share/icons/hicolor/icon-theme.cache'),
};
our %friendly_us; # u => c => p => path => undef
sub process_entry {
my ($h)=@_;
my $f=$$h{fileSet} or die;
return unless ($$f{t} eq "REG");
# and maybe also:
#return unless ($$f{f} eq "txt"); # ? well or whatever. maybe not.
local our $path= $$f{n};
xprint (" checking path: '$path'\n")
if $verbose;
if ($$ignore{$path}
or $path=~ m{^/SYSV[0-9a-f]{8}\z}
or $path=~ m{^/(?:tmp|dev|var|proc|run)/}
or $path=~ m|/\.galeon/mozilla/|
or $path=~ m{^/home/\w+/\.(config|cache)/dconf/user$}
or $path=~ m{^/memfd:}
or $path=~ m{^/usr/share/.*\.cache$}
or ($path=~ m{^/home/} and not $opt_more)
) {
# suppress
} else {
my $report= (
$style eq "lsof" ?
sub {
xprint( JustifyLeft(15, $$h{c}),
" ",
JustifyRight(7, $$h{p}),
" ",
JustifyLeft(15, uid_to_possibly_username($$h{u})),
" ",
$path,
"\n");
} :
$style eq "friendly" ?
sub {
# record for grouped display at the end
$friendly_us{
uid_to_possibly_username $$h{u}
}{
pid_to_programname_orelse($$h{p}, $$h{c})
}{
$$h{p}
}{
$path
} = undef;
} :
die "bug");
if ($rely_on_lsof_DEL) {
if ($$f{f} eq "DEL") {
&$report;
}
} else {
if (my $s= Xlstat $path) {
my $inode= $$f{i};
if ($s->ino == $inode) {
# no report
xprint("$inode is correct\n")
if $verbose;
} else {
&$report;
}
} else {
xprint( "process $$h{p}: can't stat '$path': $!\n"); ## assumes $! is still set.
}
}
}
}
our $in= Chj::IO::Command->new_sender ("lsof", "-n", "-F", ##"-0",
@ARGV);
our $processSet=undef;
# and inside, we put the file sets. or, each new file set. at key "
#our $fileSet= undef; # as i said, stored in the processSet. each time anew.
##local $/="\0";
#our $in_processSet; are we always well i mean just not fileset rite'
our $in_fileSet;
#statemacshin.
while (<$in>) {
chomp;
my $key= substr($_,0,1);
my $val= substr($_,1);
xprint ("key '$key' -> '$val'\n")
if $verbose;
if ($key eq 'p') {
if ($$processSet{fileSet}) {
# process it before deletion
process_entry ($processSet);
}
#^isnt' that sooo hacky. +- copy from below. but? x.
$processSet={};
$$processSet{$key}=$val;
$in_fileSet=0;
} elsif ($key eq 'f') {
$in_fileSet=1;
if ($$processSet{fileSet}) {
# process it before deletion
process_entry ($processSet);
}
$$processSet{fileSet}={};#clear it.
$$processSet{fileSet}{$key}= $val;
} else {
# other fields for the last set decided.
unless ($in_fileSet) {
$$processSet{$key}=$val;
} else {
$$processSet{fileSet}{$key}= $val;
}
}
}
$in->xxfinish; # yeah gell better than in the shell where this is verschluckt.
if ($style eq "friendly") {
for my $u (sort keys %friendly_us) {
xprint "User: $u\n";
my $cs = $friendly_us{$u};
for my $c (sort keys %$cs) {
xprint " Program: $c\n";
my $ps = $cs->{$c};
for my $p (sort { $a <=> $b } keys %$ps) {
xprint " Process: $p\n";
my $paths = $ps->{$p};
for my $path (sort keys %$paths) {
xprint " File: $path\n";
}
}
}
}
#use Data::Dumper; print Dumper(\%friendly_cs);
#use FP::Repl; repl;
} elsif ($style eq "lsof") {
# already printed
} else {
die "bug"
}
#use Chj::ruse;
#use Chj::Backtrace; use Chj::repl; repl;