Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/envvars pidfile #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ Configuration
=============


All the configuration is done in the file port-proxy.conf, which is readed from the current directory when starting port-proxy
All the configuration is done in the file port-proxy.conf, which is readed from the current directory when starting port-proxy.

It is possible to use environment variables in the configuration file. Any $WORD or ${WORD} will be replaced by the value of the environment variable WORD, provided that the variable exists.

**NOTE**: There is currently no way to use the character "$" when it is followed by a word character or a "{".

There are the following parameters which can apear multiple times:

Expand Down Expand Up @@ -117,6 +121,8 @@ perl port-proxy [-d] [-D] [-c conffile]
-d Enable debug output
-D Become a background process (detach don't work on windows)
-c conffile Specify an config file
-p PIDFILE Create a file containing th PID plus commands to
to send a kill to the process and remove the pidfile
```

**Example:**
Expand Down
54 changes: 38 additions & 16 deletions src/port-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ my $detach=0; # detach (unix only)

#-- options --
my %opts;
getopt('d:c:D',\%opts);
getopts('dc:Dp:',\%opts);
if (exists $opts{'d'}) { $debug=1; }
if (exists $opts{'c'}) { $configFile=$opts{'c'}; }
if (exists $opts{'D'}) { $detach=1; }
# -p filename.pid will put
# kill \
# PID
# rm filename.pid
# into the file. Usefull for killing the daemon and removing the pidfile

#---- Global Vars ---
my @allow_proxy_to=();
Expand Down Expand Up @@ -172,22 +177,32 @@ sub ReadConfig()
@allow_proxy_to=();
open(F,"<$configFile") || die "can't open file $configFile\n";
while(<F>) {
$line=$_;
$line=~s/#.*$//g; # remove comments
if ($debug) { print "Conf>$line"; }
if ( $line=~/^\s*(.+?)=(.*?)\s*$/) {
$param=$1; $val=$2;
if ($param eq 'forward') {
@aval=split(/ *, */,$val);
CreateListenSock($aval[0],$aval[1],$aval[2],$aval[3]);
} elsif($param eq 'allow_proxy_to') {
push(@allow_proxy_to,$val)
}
}
$line= $_;
$line=~ s/#.*$//g; # remove comments
$line=~ s/\$(\w+|\{\w+\})/envvar($1)/ge;
if ($debug) { print "Conf>$line"; }
if ( $line=~/^\s*(.+?)=(.*?)\s*$/) {
$param=$1; $val=$2;
if ($param eq 'forward') {
@aval=split(/ *, */,$val);
CreateListenSock($aval[0],$aval[1],$aval[2],$aval[3]);
} elsif($param eq 'allow_proxy_to') {
push(@allow_proxy_to,$val)
}
}
}
close(F);
}

sub envvar {
my($varname)= @_;
for ($varname) {
s/^\{//;
s/\}$//
}
return defined($ENV{$varname}) ? $ENV{$varname} : $_[0];
}

#---
# Handle some (unix) signals; HUP: restart with reading config; TERM/KILL: shut down
sub sigHandler($) # sigHandler(signame)
Expand Down Expand Up @@ -374,8 +389,15 @@ sub daemonize {
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
defined(my $pid = fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
if ($pid) {
if ($opts{'p'}) {
open my $pidfile, '>', $opts{'p'} or exit;
print $pidfile "kill \\\n$pid\nrm ".$opts{'p'}."\n";
close $pidfile;
}
exit;
}
setsid or die "Can't start a new session: $!";
open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
}