-
Notifications
You must be signed in to change notification settings - Fork 28
/
cxnlimit
executable file
·50 lines (35 loc) · 1.07 KB
/
cxnlimit
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
#!/usr/bin/perl -w
use strict;
my $remote = $ENV{TCPREMOTEIP}
or die 'TCPREMOTEIP environment variable not set';
die "usage: $0 name prog arg ...\n" unless @ARGV;
my $name = shift;
use File::Path ();
File::Path::mkpath("/local/tmp/cxnlimit/$name");
my $lockfile = "/local/tmp/cxnlimit/$name/$remote";
if (-e $lockfile) {
open FILE, "<$lockfile"
or die "unable to open file '$lockfile' for reading: $!\n";
my $pid = <FILE>;
if (kill 0, $pid) {
# warn "refused cxn from $TCPREMOTEIP, already connected (process $pid)\n";
print "\@RSYNCD: 26\n\@ERROR: only one simultaneous connection per ip is allowed\n\@RSYNCD: EXIT\n";
exit;
}
close FILE;
}
my $pid = fork();
die "fork failed: $!" unless defined $pid;
exec @ARGV unless $pid;
$0 = "cxn from $remote";
open FILE, ">$lockfile"
or die "unable to open file '$lockfile' for writing: $!\n";
print FILE $pid;
close FILE;
$SIG{PIPE} = 'IGNORE';
# reopen the file descriptors in the parent
open STDOUT, ">/dev/null";
open STDERR, ">/dev/null";
open STDIN, "</dev/null";
1 until wait == -1;
unlink $lockfile;