Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Last active August 29, 2015 14:23
Show Gist options
  • Save p120ph37/671586ec76d77724b6f5 to your computer and use it in GitHub Desktop.
Save p120ph37/671586ec76d77724b6f5 to your computer and use it in GitHub Desktop.
Cross-reference `/proc/locks` against a particular file to show what locks are present against that file.
#!/usr/bin/perl
use warnings;
use strict;
my %pl;
open(my $fh, '<', '/proc/locks') or die $!;
for(<$fh>) {
my @f = split /\s+/;
push(@{$pl{$f[5]}}, \@f);
}
close($fh);
for(@ARGV) {
my @s = stat or warn "$!: $_\n" and next;
my $i = sprintf("%2x:%2x:%d", $s[0]>>8, $s[0]&0xFF, $s[1]);
print("File: $_\tInode: $i\n");
print(" No locks.\n") unless $pl{$i};
print(" Region $_->[6]-$_->[7] locked for $_->[3] by PID $_->[4] with a $_->[1] $_->[2] lock.\n") for @{$pl{$i}};
}
@p120ph37
Copy link
Author

  • May specify multiple files.
  • Must have read-access to /proc/locks (On CentOS 6, /proc/locks is world-readable).
  • Must have access to call stat() on the files (r+x access to the containing dir).

Example usage:

$ ./show_locks.pl /var/spool/postfix/pid/*
File: /var/spool/postfix/pid/inet.smtp  Inode: ca:40:135218
  No locks.
File: /var/spool/postfix/pid/master.pid Inode: ca:40:135162
  Region 0-EOF locked for WRITE by PID 866 with a FLOCK ADVISORY lock.
File: /var/spool/postfix/pid/unix.bounce    Inode: ca:40:136968
  Region 0-EOF locked for WRITE by PID 31290 with a FLOCK ADVISORY lock.
File: /var/spool/postfix/pid/unix.cleanup   Inode: ca:40:139556
  Region 0-EOF locked for WRITE by PID 1829 with a FLOCK ADVISORY lock.
File: /var/spool/postfix/pid/unix.defer Inode: ca:40:136974
  No locks.
File: /var/spool/postfix/pid/unix.error Inode: ca:40:135163
  No locks.
File: /var/spool/postfix/pid/unix.local Inode: ca:40:139555
  Region 0-EOF locked for WRITE by PID 29432 with a FLOCK ADVISORY lock.
File: /var/spool/postfix/pid/unix.retry Inode: ca:40:138385
  No locks.
File: /var/spool/postfix/pid/unix.showq Inode: ca:40:137490
  No locks.
File: /var/spool/postfix/pid/unix.smtp  Inode: ca:40:137911
  Region 0-EOF locked for WRITE by PID 28473 with a FLOCK ADVISORY lock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment