File: shared.perl5

package info (click to toggle)
lsof 4.91+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,984 kB
  • sloc: ansic: 68,893; sh: 6,862; makefile: 1,682; perl: 1,099; awk: 214
file content (409 lines) | stat: -rwxr-xr-x 10,553 bytes parent folder | download | duplicates (4)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/perl
#
# $Id: shared.perl5,v 1.4 2001/11/18 12:20:46 abe Exp $
#
# shared.perl5 -- sample Perl 5 script to list processes that share
#		  file descriptors or files, using `lsof +ffn -F..."
#		  output
#
# Usage:	shared [fd|file]
#
# where:	fd to list file descriptors (default)
#
#		file to list files
#
# This script has been tested under perl version 5.001e.


# IMPORTANT DEFINITIONS
# =====================
#
# 1.  Set the interpreter line of this script to the local path of the
#     Perl5 executable.


# Copyright 1998 Purdue Research Foundation, West Lafayette, Indiana
# 47907.  All rights reserved.
#
# Written by Victor A. Abell <abe@purdue.edu>
#
# This software is not subject to any license of the American Telephone
# and Telegraph Company or the Regents of the University of California.
#
# Permission is granted to anyone to use this software for any purpose on
# any computer system, and to alter it and redistribute it freely, subject
# to the following restrictions:
#
# 1. Neither the authors nor Purdue University are responsible for any
#    consequences of the use of this software.
#
# 2. The origin of this software must not be misrepresented, either by
#    explicit claim or by omission.  Credit to the authors and Purdue
#    University must appear in documentation and sources.
#
# 3. Altered versions must be plainly marked as such, and must not be
#    misrepresented as being the original software.
#
# 4. This notice may not be removed or altered.

# Initialize variables.

$Access = $Devch = $Devn = $Fd = $Fsa = $Inode = $Lock =	# file
	  $Na = $Name = "";					# | descriptor
$Cmd = $Login = $Pgrp = $Pid = $Ppid = $Uid = "";		# process var.
$Fdst = 0;							# fd state
$Hdr = 0;							# header state
$Offset = $Proto = $Size = $State = $Stream = $Type = "";	# | variables
$Pidst = 0;							# process state
$Pn = "shared";

# Set path to lsof.

if (($LSOF = &isexec("../lsof")) eq "") {	# Try .. first
    if (($LSOF = &isexec("lsof")) eq "") {	# Then try . and $PATH
	print "can't execute $LSOF\n"; exit 1
    }
}

# Define print field constants.

$CmdTtl = "CMD";
$CmdW = length($CmdTtl);
$DevTtl = "DEVICE";
$DevW = length($DevTtl);
$FdTtl = "FD";
$FdW = length($FdTtl);
$InoTtl = "NODE";
$InoW = length($InoTtl);
$KeyTtl = "FILEADDR";
$KeyW = length($KeyTtl);
$PidTtl = "PID";
$PidW = length($PidTtl);
$PpidTtl = "PPID";
$PpidW = length(PpidTtl);

# Process one (optional) argument.

if ($#ARGV >= 0) {
    $err = 0;
    if ($#ARGV > 1) { $err = 1; }
    elsif ($ARGV[0] eq "fd") {
	$KeyTtl = "FILEADDR";
	$Shfd = 1;
	$Shfile = 0;
    } elsif ($ARGV[0] eq "file") {
	$KeyTtl = "NODEID";
	$Shfd = 0;
	$Shfile = 1;
    } else { $err = 1; }
    if ($err) { die "$Pn: usage [fd|file]\n"; }
    shift;
} else { $Shfd = 1; $Shfile = 0; }
$KeyW = length($KeyTtl);

# Open a pipe from lsof.

if (!open(LSOF_PIPE, "$LSOF -R +ffn -F0pcRDfFinN |")) {
    die "$Pn: can't open pipe to: $LSOF\n";
}

# Process the lsof output a line at a time, gathering the variables for
# processes and files.

while (<LSOF_PIPE>) {
    chop;
    @F = split('\0', $_, 999);
    if ($F[0] =~ /^p/) {

# A process set begins with a PID field whose ID character is `p'.

	if ($Fdst) { &End_fd }
	if ($Pidst) { &End_proc }
	foreach $i (0 .. ($#F - 1)) {

	    PROC: {
		if ($F[$i] =~ /^c(.*)/) { $Cmd = $1; last PROC }
		if ($F[$i] =~ /^g(.*)/) { $Pgrp = $1; last PROC }
		if ($F[$i] =~ /^p(.*)/) { $Pid = $1; last PROC }
		if ($F[$i] =~ /^u(.*)/) { $Uid = $1; last PROC }
		if ($F[$i] =~ /^L(.*)/) { $Login = $1; last PROC }
		if ($F[$i] =~ /^R(.*)/) { $Ppid = $1; last PROC }
		print "ERROR: unrecognized process field: \"$F[$i]\"\n";
	    }
	}
	$Pidst = 1;
	next;
    }

# A file descriptor set begins with a file descriptor field whose ID
# character is `f'.

    if ($F[0] =~ /^f/) {
	if ($Fdst) { &End_fd }
	foreach $i (0 .. ($#F - 1)) {

	    FD: {
		if ($F[$i] =~ /^a(.*)/) { $Access = $1; last FD; }
		if ($F[$i] =~ /^f(.*)/) { $Fd = $1; last FD; }
		if ($F[$i] =~ /^F(.*)/) { $Fsa = $1; last FD; }
		if ($F[$i] =~ /^l(.*)/) { $Lock = $1; last FD; }
		if ($F[$i] =~ /^t(.*)/) { $Type = $1; last FD; }
		if ($F[$i] =~ /^d(.*)/) { $Devch = $1; last FD; }
		if ($F[$i] =~ /^D(.*)/) { $Devn = $1; last FD; }
		if ($F[$i] =~ /^s(.*)/) { $Size = $1; last FD; }
		if ($F[$i] =~ /^o(.*)/) { $Offset = $1; last FD; }
		if ($F[$i] =~ /^i(.*)/) { $Inode = $1; last FD; }
		if ($F[$i] =~ /^P(.*)/) { $Proto = $1; last FD; }
		if ($F[$i] =~ /^S(.*)/) { $Stream = $1; last FD; }
		if ($F[$i] =~ /^T(.*)/) {
		    if ($State eq "") { $State = "(" . $1; }
		    else { $State = $State . " " . $1; }
		    last FD;
		}
		if ($F[$i] =~ /^n(.*)/) { $Name = $1; last FD; }
		if ($F[$i] =~ /^N(.*)/) { $Na = $1; last FD; }
		print "ERROR: unrecognized file set field: \"$F[$i]\"\n";
	    }
	}
	$Fdst = 1;
	next;
    }
    print "ERROR: unrecognized: \"$_\"\n";
}
close(LSOF_PIPE);
if ($Fdst) { &End_fd }
if ($Pidst) { &End_proc }

# List matching files or file descriptors.

for ($pass = 0; $pass < 2; $pass++) {
    foreach $key (sort keys(%Fds)) {
	@Praw = split(' ', $Fds{$key}, 999);
	if ($#Praw < 1) { next; }
	if ($Shfd) { @P = sort Sort_by_FD_and_PID @Praw; }
	else { @P = sort Sort_by_PID_and_FD @Praw; }

    # Accumulate and print blocks of (key, PID, FD) triplets.

	for ($i = 0; $i < $#P; $i++) {
	    if ($Shfile) {
		for ($n = 0; $n <= $#P; $n++) {
		    ($pid, $fd) = split(",", $P[$n], 999);
		    $PrtPid[$n] = $pid;
		    $PrtFd[$n] = $fd;
		}
		$i = $n;
	    } else {
		($pid, $fd) = split(",", $P[$i], 999);
		$PrtFd[0] = $fd;
		$PrtPid[0] = $pid;
		for ($n = 1; $i < $#P; $i++, $n++) {
		    ($nxtpid, $nxtfd) = split(",", $P[$i + 1], 999);
		    if ($fd ne $nxtfd) { last; }
		    $PrtFd[$n] = $nxtfd;
		    $PrtPid[$n] = $nxtpid;
		}
	    }
	    if ($n > 1) { &Print_block($key, $n, $pass); }
	}
    }
}
exit(0);


## End_fd() -- process end of file descriptor

sub End_fd {

    local ($key);

    if ($Fdst && $Pidst && $Pid ne "") {
	if ($Cmd ne "") { $Cmds{$Pid} = $Cmd; }
	if ($Ppid ne "") { $Ppids{$Pid} = $Ppid; }
	$key = $Shfd ? $Fsa : $Na;
	if ($key ne "") {
	    if (!defined($Fds{$key})) { $Fds{$key} = "$Pid,$Fd"; }
	    else { $Fds{$key} .= " $Pid,$Fd"; }
	    if ($Name ne "" && !defined($Name{$key})) { $Name{$key} = $Name }
	    if ($Inode ne "" && !defined($Inodes{$key})) {
		$Inodes{$key} = $Inode;
	    }
	    if ($Devn ne "" && !defined($Devns{$key})) {
		$Devns{$key} = $Devn;
	    }
	}
    }

# Clear variables.

    $Access = $Devch = $Devn = $Fd = $Fsa = $Inode = $Lock = "";
    $Na = $Name = $Offset = $Proto = $Size = $State = $Stream = $Type = "";
    $Fdst = 0;
}


## End_proc() -- process end of process

sub End_proc {

# Clear variables.

    $Cmd = $Login = $Pgrp = $Pid = $Ppid = $Uid = "";
    $Fdst = $Pidst = 0;
}


## Print_block() -- print a block of entries
#
# entry:
#
#	@_[0] = block's key
#	@_[1] = number of entries in the block
#	@_[2] = print pass status (1 == print)

sub Print_block {

    my ($key, $n, $pass) = @_;

    local ($fd, $i, $pid, $t, $tW);

    if ($pass) {
	if (!$Hdr) {
	    printf "%${KeyW}.${KeyW}s", $KeyTtl;
	    printf " %${PidW}.${PidW}s", $PidTtl;
	    printf " %${PpidW}.${PpidW}s", $PpidTtl;
	    printf " %-${CmdW}.${CmdW}s", $CmdTtl;
	    printf " %${FdW}.${FdW}s", $FdTtl;
	    printf " %${DevW}.${DevW}s", $DevTtl;
	    printf " %${InoW}.${InoW}s", $InoTtl;
	    printf " NAME\n";
	    $Hdr = 1;
	} else { print "\n"; }
    }

# Loop through block.  During a non-print pass, caclulate maximum field widths.

    for ($i = 0; $i < $n; $i++) {
	$fd = $PrtFd[$i];
	$pid = $PrtPid[$i];

    # Process key.

	if (!$pass) {
	    $tW = length(sprintf("%s", $key));
	    if ($tW > $KeyW) { $KeyW = $tW; }
	} else { printf "%s", $key; }

    # Process PID.

	if (!$pass) {
	    $tW = length(sprintf(" %s", $pid));
	    if ($tW > $PidW) { $PidW = $tW; }
	} else { printf " %${PidW}.${PidW}s", $pid; }

    # Process parent PID.

	$t = defined($Ppids{$pid}) ? $Ppids{$pid} : "";
	if (!$pass) {
	    $tW = length(sprintf(" %s", $t));
	    if ($tW > $PpidW) { $PpidW = $tW; }
	} else { printf " %${PpidW}.${PpidW}s", $t; }

    # Process command name.

	$t = defined($Cmds{$pid}) ? $Cmds{$pid} : "";
	if (!$pass) {
	    $tW = length(sprintf(" %s", $t));
	    if ($tW > $CmdW) { $CmdW = $tW; }
	} else { printf " %-${CmdW}.${CmdW}s", $t; }

    # Process file descriptor.

	if (!$pass) {
	    $tW = length(sprintf(" %s", $fd));
	    if ($tW > $FdW) { $FdW = $tW; }
	} else { printf " %${FdW}.${FdW}s", $fd; }

    # Process device number.

	$t = defined($Devns{$key}) ? $Devns{$key} : "";
	if (!$pass) {
	    $tW = length(sprintf(" %s", $t));
	    if ($tW > $DevW) { $DevW = $tW; }
	} else { printf " %${DevW}.${DevW}s", $t; }

    # Process node number.

	$t = defined($Inodes{$key}) ? $Inodes{$key} : $t;
	if (!$pass) {
	    $tW = length(sprintf (" %s", $t));
	    if ($tW > $InoW) { $InoW = $tW; }
	} else { printf " %${InoW}.${InoW}s", $t; }

    # Print name and line terminater, if this is a print pass.

	if ($pass) {
	    if (defined($Name{$key})) { print " $Name{$key}\n"; }
	    else { print "\n"; }
	}
    }
}


## Sort_by_FD_and_PID() -- sort (PID,FD) doublets by FD first, then PID

sub Sort_by_FD_and_PID {

    local ($pida, $pidb, $fda, $fdj, $rv);

    ($pida, $fda) = split(",", $a);
    ($pidb, $fdb) = split(",", $b);
    if ($fda < $fdb) { return(-1); }
    if ($fda > $fdb) { return(1); }
    if ($pida < $pidb) { return(-1); }
    if ($pida > $pidb) { return(1); }
    return(0);
}


## Sort_by_PID_and_FD() -- sort (PID,FD) doublets by PID first, then FD

sub Sort_by_PID_and_FD {

    local ($pida, $pidb, $fda, $fdj, $rv);

    ($pida, $fda) = split(",", $a);
    ($pidb, $fdb) = split(",", $b);
    if ($pida < $pidb) { return(-1); }
    if ($pida > $pidb) { return(1); }
    if ($fda < $fdb) { return(-1); }
    return(0);
    if ($fda > $fdb) { return(1); }
}


## isexec($path) -- is $path executable
#
# $path   = absolute or relative path to file to test for executabiity.
#	    Paths that begin with neither '/' nor '.' that arent't found as
#	    simple references are also tested with the path prefixes of the
#	    PATH environment variable.  

sub
isexec {
    my ($path) = @_;
    my ($i, @P, $PATH);

    $path =~ s/^\s+|\s+$//g;
    if ($path eq "") { return(""); }
    if (($path =~ m#^[\/\.]#)) {
	if (-x $path) { return($path); }
	return("");
    }
    $PATH = $ENV{PATH};
    @P = split(":", $PATH);
    for ($i = 0; $i <= $#P; $i++) {
	if (-x "$P[$i]/$path") { return("$P[$i]/$path"); }
    }
    return("");
}