Commit b3b65739 authored by Leigh Stoller's avatar Leigh Stoller

Add TBBackGround routine. Puts the script into the background,

redirecting stdin to /dev/null and stdout/stderr to the logfile
provided. I had this duplicated in several scripts.
parent 5b9c619b
...@@ -7,7 +7,7 @@ use Exporter; ...@@ -7,7 +7,7 @@ use Exporter;
@ISA = "Exporter"; @ISA = "Exporter";
@EXPORT = @EXPORT =
qw ( SENDMAIL OPENMAIL TBTimeStamp); qw ( SENDMAIL OPENMAIL TBTimeStamp TBBackGround );
# A library of useful stuff. # A library of useful stuff.
...@@ -91,4 +91,41 @@ sub TBTimeStamp() ...@@ -91,4 +91,41 @@ sub TBTimeStamp()
return POSIX::strftime("%H:%M:%S", localtime()); return POSIX::strftime("%H:%M:%S", localtime());
} }
#
# Put ourselves into the background, directing output to the log file.
# The caller provides the logfile name, which should have been created
# with mktemp, just to be safe. Returns the usual return of fork.
#
# usage int TBBackGround(char *filename).
#
sub TBBackGround($)
{
my($logname) = @_;
my $mypid = fork();
if ($mypid) {
return $mypid;
}
#
# We have to disconnect from the caller by redirecting both STDIN and
# STDOUT away from the pipe. Otherwise the caller (the web server) will
# continue to wait even though the parent has exited.
#
open(STDIN, "< /dev/null") or
die("opening /dev/null for STDIN: $!");
# Note different taint check (allow /).
if ($logname =~ /^([-\@\w.\/]+)$/) {
$logname = $1;
} else {
die "Bad data in logfile name: $logname";
}
open(STDERR, ">> $logname") or die("opening $logname for STDERR: $!");
open(STDOUT, ">> $logname") or die("opening $logname for STDOUT: $!");
return 0;
}
1; 1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment