Add logging via Log::Any, fork() to daemonize, fix misc bugs
This commit is contained in:
@@ -25,23 +25,39 @@ BEGIN {
|
||||
|
||||
use lib ("$workdir/lib", "$workdir/vendor_perl", "$workdir/vendor_perl/lib/perl5");
|
||||
no Cwd;
|
||||
use Cwd::utf8 qw (abs_path);
|
||||
use POSIX qw (setgid setuid);
|
||||
use Cwd::utf8 qw (chdir abs_path);
|
||||
use POSIX qw (setgid setuid setsid);
|
||||
use File::Path qw (make_path);
|
||||
use File::Basename qw (dirname);
|
||||
use File::Basename qw (basename dirname);
|
||||
use Log::Any qw ($log);
|
||||
use Log::Any::Adapter;
|
||||
use Log::Any::For::Std;
|
||||
use BotLib::Conf qw (LoadConf);
|
||||
use IRCBot2 qw (RunIRCBot);
|
||||
use IRCBot qw (RunIRCBot);
|
||||
|
||||
sub daemonize ();
|
||||
sub __fork ();
|
||||
sub __signal_handler;
|
||||
|
||||
local $OUTPUT_AUTOFLUSH = 1;
|
||||
my $runAsUid = $UID;
|
||||
|
||||
my $piddir = '/var/run/' . basename (abs_path $PROGRAM_NAME);
|
||||
my $C = LoadConf ();
|
||||
my $loglevel = $C->{'loglevel'} // 'notice';
|
||||
my $logfile;
|
||||
|
||||
if (defined $C->{'log'}) {
|
||||
$logfile = $C->{'log'};
|
||||
} elsif (defined $C->{'debug_log'}) {
|
||||
$logfile = $C->{'debug_log'};
|
||||
}
|
||||
|
||||
# if we run under root, create pid dir and set correct owner for it, same for debug log if it is defined
|
||||
if ($UID == 0) {
|
||||
my $C = LoadConf ();
|
||||
|
||||
unless (defined $C->{user}) {
|
||||
die "Unable to run as root, please define unprivileged user in config.json\n";
|
||||
}
|
||||
|
||||
my ($uid, $gid) = (getpwnam $C->{user})[2,3];
|
||||
my $piddir = dirname $C->{pid};
|
||||
my $user = $C->{user};
|
||||
die "Unable to run as root, please define unprivileged user in config.json\n" unless (defined ($user));
|
||||
my ($uid, $gid) = (getpwnam $user)[2,3];
|
||||
|
||||
unless (-d $piddir) {
|
||||
make_path (
|
||||
@@ -52,14 +68,6 @@ if ($UID == 0) {
|
||||
) or die "Unable to create $piddir: $OS_ERROR\n";
|
||||
}
|
||||
|
||||
my $logfile;
|
||||
|
||||
if (defined $C->{'log'}) {
|
||||
$logfile = $C->{'log'};
|
||||
} elsif (defined $C->{'debug_log'}) {
|
||||
$logfile = $C->{'debug_log'};
|
||||
}
|
||||
|
||||
if (defined $logfile) {
|
||||
my $logdir = dirname $logfile;
|
||||
|
||||
@@ -74,16 +82,108 @@ if ($UID == 0) {
|
||||
}
|
||||
|
||||
# drop privs
|
||||
setgid ($gid) or die "Unable to switch to group of $C->{user}: $OS_ERROR\n";
|
||||
setuid ($uid) or die "Unable to switch to account $C->{user}: $OS_ERROR\n";
|
||||
setgid ($gid) or die "Unable to switch to group of $user: $OS_ERROR\n";
|
||||
setuid ($uid) or die "Unable to switch to account $user: $OS_ERROR\n";
|
||||
} else {
|
||||
$piddir = $workdir;
|
||||
}
|
||||
|
||||
if (defined $logfile) {
|
||||
# loglevel warning suppresses logging from CHI.
|
||||
Log::Any::Adapter->set ('File', $logfile, log_level => $loglevel);
|
||||
|
||||
# Some annoying CHI logging events come with category set to CHI::Driver and log_level set to debug.
|
||||
# Until we use start using Log::Any native features and set whole loglevel accordingly, lets lower
|
||||
# verbosity paticulary for CHI.
|
||||
Log::Any::Adapter->set (
|
||||
{
|
||||
category => 'CHI::Driver'
|
||||
},
|
||||
'File',
|
||||
$logfile,
|
||||
log_level => 'warn'
|
||||
);
|
||||
} else {
|
||||
# no log defined so set loglevel to the least verbose
|
||||
Log::Any::Adapter->set ('File', '/dev/null', 'log_level'=>'emergency');
|
||||
}
|
||||
|
||||
# double fork attach to /dev/null all stdio and go background
|
||||
daemonize ();
|
||||
|
||||
# write pidfile
|
||||
my $apath = abs_path $PROGRAM_NAME;
|
||||
|
||||
unless (defined $apath) {
|
||||
$log->fatal ("[FATAL] Unable to get abs_path: $OS_ERROR");
|
||||
exit 1;
|
||||
}
|
||||
|
||||
my $pidfile = sprintf '%s/%s.pid', $piddir, basename $apath;
|
||||
$log->info ("[INFO] Creating pidfile at $pidfile");
|
||||
|
||||
open (my $pid, '>', $pidfile) or do {
|
||||
$log->fatal ("[FATAL] Error opening $pidfile: $OS_ERROR");
|
||||
exit 1;
|
||||
};
|
||||
|
||||
print $pid $PID or do {
|
||||
$log->fatal ("[FATAL] Error writing to $pidfile: $OS_ERROR");
|
||||
exit 1;
|
||||
};
|
||||
|
||||
close $pid; ## no critic (InputOutput::RequireCheckedSyscalls)
|
||||
|
||||
local $SIG{INT} = \&__signal_handler;
|
||||
local $SIG{TERM} = \&__signal_handler;
|
||||
|
||||
# set correct proctitle
|
||||
$PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::RequireLocalizedPunctuationVars)
|
||||
|
||||
RunIRCBot ();
|
||||
|
||||
if (-f $pidfile) {
|
||||
unlink $pidfile;
|
||||
}
|
||||
|
||||
exit 0;
|
||||
|
||||
sub daemonize () {
|
||||
__fork ();
|
||||
POSIX::setsid ();
|
||||
open (STDIN, '<', '/dev/null'); ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
# we handle STDOUT && STDERR via Log::Any::For::Std, so do not close them explicitly
|
||||
#open (STDOUT, '>', '/dev/null'); ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
#open (STDERR, '>', '/dev/null'); ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
|
||||
__fork ();
|
||||
umask 0;
|
||||
return;
|
||||
}
|
||||
|
||||
sub __fork () {
|
||||
my $pid = fork;
|
||||
|
||||
unless (defined $pid) {
|
||||
$log->fatal ("[FATAL] Can't fork: $OS_ERROR");
|
||||
exit 1;
|
||||
}
|
||||
|
||||
if ($pid != 0) {
|
||||
exit 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub __signal_handler ($) {
|
||||
$log->info ("[INFO] Caught a signal $OS_ERROR");
|
||||
|
||||
if (-f $pidfile) {
|
||||
unlink $pidfile;
|
||||
}
|
||||
|
||||
exit 0;
|
||||
}
|
||||
|
||||
__END__
|
||||
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
||||
|
||||
Reference in New Issue
Block a user