Daemon: cleanup logging

This commit is contained in:
Dietmar Maurer 2014-12-31 12:31:21 +01:00
parent 1a6bc2f371
commit c56111950d

View File

@ -22,15 +22,6 @@ use Time::HiRes qw (gettimeofday);
use base qw(PVE::CLIHandler); use base qw(PVE::CLIHandler);
$SIG{'__WARN__'} = sub {
my $err = $@;
my $t = $_[0];
chomp $t;
print "$t\n";
syslog('warning', "WARNING: %s", $t);
$@ = $err;
};
$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
my $daemon_initialized = 0; # we only allow one instance my $daemon_initialized = 0; # we only allow one instance
@ -44,6 +35,13 @@ my $close_daemon_lock = sub {
delete $self->{daemon_lock_fh}; delete $self->{daemon_lock_fh};
}; };
my $log_err = sub {
my ($msg) = @_;
chomp $msg;
print STDERR "$msg\n";
syslog('err', "%s", $msg);
};
# call this if you fork() from child # call this if you fork() from child
# Note: we already call this for workers, so it is only required # Note: we already call this for workers, so it is only required
# if you fork inside a simple daemon (max_workers == 0). # if you fork inside a simple daemon (max_workers == 0).
@ -87,7 +85,14 @@ my $lockpidfile = sub {
if (!flock ($self->{daemon_lock_fh}, LOCK_EX|LOCK_NB)) { if (!flock ($self->{daemon_lock_fh}, LOCK_EX|LOCK_NB)) {
&$close_daemon_lock($self); &$close_daemon_lock($self);
die "can't aquire lock '$lkfn' - $!\n"; my $err = $!;
my ($running, $pid) = $self->running();
if ($running) {
die "can't aquire lock '$lkfn' - daemon already started (pid = $pid)\n";
} else {
die "can't aquire lock '$lkfn' - $err\n";
}
} }
}; };
@ -96,11 +101,8 @@ my $writepidfile = sub {
my $pidfile = $self->{pidfile}; my $pidfile = $self->{pidfile};
if (!open (PIDFH, ">$pidfile")) { die "can't open pid file '$pidfile' - $!\n" if !open (PIDFH, ">$pidfile");
my $msg = "can't open pid file '$pidfile' - $!";
syslog ('err', $msg);
die "ERROR: $msg\n";
}
print PIDFH "$$\n"; print PIDFH "$$\n";
close (PIDFH); close (PIDFH);
}; };
@ -229,10 +231,6 @@ my $server_run = sub {
$ENV{PVE_DAEMON_LOCK_FD} = $self->{daemon_lock_fh}->fileno; $ENV{PVE_DAEMON_LOCK_FD} = $self->{daemon_lock_fh}->fileno;
# my $fd = POSIX::dup($self->{daemon_lock_fh}) ||
# die "unable to duplicate daemon_lock_fh\n";
# run in background # run in background
my $spid; my $spid;
@ -249,25 +247,22 @@ my $server_run = sub {
PVE::INotify::inotify_close(); PVE::INotify::inotify_close();
$spid = fork(); $spid = fork();
if (!defined ($spid)) { if (!defined ($spid)) {
my $msg = "can't put server into background - fork failed"; die "can't put server into background - fork failed";
syslog('err', $msg);
die "ERROR: $msg\n";
} elsif ($spid) { # parent } elsif ($spid) { # parent
exit (0); exit (0);
} }
PVE::INotify::inotify_init(); PVE::INotify::inotify_init();
} }
&$writepidfile($self);
POSIX::setsid();
if ($self->{env_restart_pve_daemon}) { if ($self->{env_restart_pve_daemon}) {
syslog('info' , "restarting server"); syslog('info' , "restarting server");
} else { } else {
&$writepidfile($self);
syslog('info' , "starting server"); syslog('info' , "starting server");
} }
POSIX::setsid();
open STDERR, '>&STDOUT' || die "can't close STDERR\n"; open STDERR, '>&STDOUT' || die "can't close STDERR\n";
my $old_sig_term = $SIG{TERM}; my $old_sig_term = $SIG{TERM};
@ -479,7 +474,8 @@ sub start {
eval { &$server_run($self, $debug); }; eval { &$server_run($self, $debug); };
if (my $err = $@) { if (my $err = $@) {
syslog('err', "start failed - $err"); &$log_err("start failed - $err");
exit(-1);
} }
} }
@ -542,7 +538,7 @@ sub stop {
&$server_cleanup($self); &$server_cleanup($self);
}; };
if (my $err = $@) { if (my $err = $@) {
syslog('err', "cleanup failed - $err"); &$log_err("cleanup failed - $err");
} }
} }
} }