Add help, debug log, fix pid file creation

+ Add help in group chat
+ Add debug log ability
* A bit of cleanup
* Check against start uid rather than effective uid when creating pid-file
This commit is contained in:
eleksir
2020-09-06 20:44:54 +03:00
parent f7ecdb870f
commit 20bbfab333
4 changed files with 59 additions and 14 deletions

View File

@@ -18,17 +18,17 @@ BEGIN {
use lib ("$workdir/lib", "$workdir/vendor_perl", "$workdir/vendor_perl/lib/perl5");
use POSIX qw(setgid setuid setsid);
use Fcntl qw(O_WRONLY O_TRUNC O_CREAT);
use File::Path qw(make_path);
use File::Basename qw(basename);
use telegrambot;
use conf;
use File::Basename qw(basename dirname);
use conf qw(loadConf);
use botlib qw(logger);
use telegrambot qw(run_telegrambot);
sub daemonize ();
sub __fork ();
sub __mkpidfile ();
$| = 1;
my $runAsUid = $<;
my $piddir = "/var/run/" . basename (abs_path ($0));
my $C = loadConf ();
@@ -48,6 +48,17 @@ if ($< == 0) {
);
}
if ($C->{'debug_log'}) {
unless (-d dirname($C->{'debug_log'})) {
make_path (
dirname($C->{'debug_log'}), {
uid => $uid,
group => $gid
}
);
}
}
# script intended to run as "unit" user, so drop privs to "unit" user
setgid ($gid) or die "Unable to switch to group of $user($!)\n";
setuid ($uid) or die "Unable to switch to account $user($!)\n";
@@ -59,11 +70,12 @@ daemonize ();
# write pidfile
my $pidfile;
if ($< == 0) {
if ($runAsUid == 0) {
# TODO: handle case where we run with unpriv. user
$pidfile = sprintf ("%s/%s.pid", $piddir, basename (abs_path ($0)));
sysopen (PID, $pidfile, O_WRONLY|O_TRUNC|O_CREAT) or die "Error opening $pidfile: $!\n";
syswrite PID, $$ or die "Error writing to $pidfile: $!\n";
logger ("Creating pidfile at $pidfile");
open (PID, ">", $pidfile) or die "Error opening $pidfile: $!\n";
print PID $$ or die "Error writing to $pidfile: $!\n";
close PID;
}
@@ -72,7 +84,6 @@ $0 = abs_path ($0);
# run bot, at last
run_telegrambot ();
unlink ($pidfile) if (-f $pidfile);
exit 0;
@@ -92,6 +103,5 @@ sub __fork () {
exit if ($pid != 0);
}
__END__
# vim: set ft=perl noet ai ts=4 sw=4 sts=4: