Skip to content
New issue

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

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

Already on GitHub? # to your account

Prevent log file from being overwritten, redirect STDOUT even when not run as daemon, log timestamp #15

Closed
wants to merge 3 commits into from
Closed
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
20 changes: 15 additions & 5 deletions bin/pgaudit_analyze
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,9 @@ sub daemonInit
chdir '/'
or confess "chdir() failed: $!";

# close stdin/stdout
# close stdin, stdout previously closed/redirected
open STDIN, '<', '/dev/null'
or confess "Couldn't close standard input: $!";
open STDOUT, '>', $strLogOutFile
or confess "Couldn't close standard output: $!";

# create new process
defined($pid = fork)
Expand All @@ -611,6 +609,14 @@ sub daemonInit
setsid() or confess("setsid() failed: $!");
}

####################################################################################################################################
# getTimestamp
####################################################################################################################################
sub getTimestamp
{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
return sprintf ( "%04d-%02d-%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min,$sec);
}
####################################################################################################################################
# Main loop
####################################################################################################################################
Expand All @@ -630,9 +636,13 @@ my $bDone = false;
my $strNextLogFile = nextLogFile($strLogPath);

# Open log file
open(my $hLog, '>', $strLogOutFile)
open(my $hLog, '>>', $strLogOutFile)
or confess "unable to open pgAudit Analyze log file $strLogOutFile: $!";

# always redirect stdout to logfile
open (STDOUT, '>&', $hLog)
or confess "Couldn't close standard output and redirect to logfile: $!";

# Daemonize the process
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you like to make this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue is when run as a systemd process without the --daemon flag all of the print statements do not make it into the logfile
I will also make this its own PR

daemonInit()
if ($bDaemon);
Expand Down Expand Up @@ -670,7 +680,7 @@ while(!$bDone)
$strLogFile = $strNextLogFile;
undef($strNextLogFile);

syswrite($hLog, "reading ${strLogFile}\n");
syswrite($hLog, "${\getTimestamp()}: reading ${strLogFile}\n");

# Read updating file
# http://stackoverflow.com/questions/1425223/how-do-i-read-a-file-which-is-constantly-updating
Expand Down