Dumping errors from the previous run can often get lost, so just
spew to syslog since it's a standard place to put errors that
don't make it to a client. Note: we don't rely on $SIG{__WARN__}
since some of the Net:: stuff will write directly to STDERR
(as will external processes).
use Time::HiRes qw(stat); # ctime comparisons for config cache
use File::Path qw(mkpath);
use File::Spec;
use Time::HiRes qw(stat); # ctime comparisons for config cache
use File::Path qw(mkpath);
use File::Spec;
+use Sys::Syslog qw(openlog syslog closelog);
our $quit = \&CORE::exit;
our ($current_lei, $errors_log, $listener, $oldset, $dir_idle,
$recv_cmd, $send_cmd);
our $quit = \&CORE::exit;
our ($current_lei, $errors_log, $listener, $oldset, $dir_idle,
$recv_cmd, $send_cmd);
my ($self, $code) = @_;
# make sure client sees stdout before exit
$self->{1}->autoflush(1) if $self->{1};
my ($self, $code) = @_;
# make sure client sees stdout before exit
$self->{1}->autoflush(1) if $self->{1};
stop_pager($self);
if ($self->{pkt_op_p}) { # to top lei-daemon
$self->{pkt_op_p}->pkt_do('x_it', $code);
stop_pager($self);
if ($self->{pkt_op_p}) { # to top lei-daemon
$self->{pkt_op_p}->pkt_do('x_it', $code);
my ($self, $cmd, @argv) = @_;
local $current_lei = $self; # for __WARN__
$self->{2}->autoflush(1); # keep stdout buffered until x_it|DESTROY
my ($self, $cmd, @argv) = @_;
local $current_lei = $self; # for __WARN__
$self->{2}->autoflush(1); # keep stdout buffered until x_it|DESTROY
- dump_and_clear_log("from previous run\n");
return _help($self, 'no command given') unless defined($cmd);
# do not support Getopt bundling for this
while ($cmd eq '-C' || $cmd eq '-c') {
return _help($self, 'no command given') unless defined($cmd);
# do not support Getopt bundling for this
while ($cmd eq '-C' || $cmd eq '-c') {
sub dump_and_clear_log {
if (defined($errors_log) && -s STDIN && seek(STDIN, 0, SEEK_SET)) {
sub dump_and_clear_log {
if (defined($errors_log) && -s STDIN && seek(STDIN, 0, SEEK_SET)) {
- my @pfx = @_;
- unshift(@pfx, "$errors_log ") if @pfx;
- warn @pfx, do { local $/; <STDIN> };
- truncate(STDIN, 0) or warn "ftruncate ($errors_log): $!";
+ openlog('lei-daemon', 'pid,nowait,nofatal,ndelay', 'user');
+ chomp(my @lines = <STDIN>);
+ truncate(STDIN, 0) or
+ syslog('warning', "ftruncate (%s): %m", $errors_log);
+ for my $l (@lines) { syslog('warning', '%s', $l) }
+ closelog(); # don't share across fork
(-p STDOUT) or die "E: stdout must be a pipe\n";
open(STDIN, '+>>', $errors_log) or die "open($errors_log): $!";
STDIN->autoflush(1);
(-p STDOUT) or die "E: stdout must be a pipe\n";
open(STDIN, '+>>', $errors_log) or die "open($errors_log): $!";
STDIN->autoflush(1);
- dump_and_clear_log("from previous daemon process:\n");
POSIX::setsid() > 0 or die "setsid: $!";
my $pid = fork // die "fork: $!";
return if $pid;
POSIX::setsid() > 0 or die "setsid: $!";
my $pid = fork // die "fork: $!";
return if $pid;
}
$self->{1}->autoflush(1) if $self->{1};
stop_pager($self);
}
$self->{1}->autoflush(1) if $self->{1};
stop_pager($self);
# preserve $? for ->fail or ->x_it code
}
# preserve $? for ->fail or ->x_it code
}
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# forwards stderr from lei/store process to any lei clients using
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# forwards stderr from lei/store process to any lei clients using
+# the same store, falls back to syslog if no matching clients exist.
package PublicInbox::LeiStoreErr;
use strict;
use v5.10.1;
package PublicInbox::LeiStoreErr;
use strict;
use v5.10.1;
print $err $$rbuf and $printed = 1;
}
if (!$printed) {
print $err $$rbuf and $printed = 1;
}
if (!$printed) {
- openlog('lei-store', 'pid,nowait,nofatal,ndelay', 'user');
+ openlog('lei/store', 'pid,nowait,nofatal,ndelay', 'user');
for my $l (split(/\n/, $$rbuf)) { syslog('warning', '%s', $l) }
closelog(); # don't share across fork
}
for my $l (split(/\n/, $$rbuf)) { syslog('warning', '%s', $l) }
closelog(); # don't share across fork
}
ok(kill(0, $pid), 'pid is valid');
ok(-S $sock, 'sock created');
is(-s $err_log, 0, 'nothing in errors.log');
ok(kill(0, $pid), 'pid is valid');
ok(-S $sock, 'sock created');
is(-s $err_log, 0, 'nothing in errors.log');
- open my $efh, '>>', $err_log or BAIL_OUT $!;
- print $efh "phail\n" or BAIL_OUT $!;
- close $efh or BAIL_OUT $!;
-
lei_ok('daemon-pid');
chomp(my $pid_again = $lei_out);
is($pid, $pid_again, 'daemon-pid idempotent');
lei_ok('daemon-pid');
chomp(my $pid_again = $lei_out);
is($pid, $pid_again, 'daemon-pid idempotent');
- like($lei_err, qr/phail/, 'got mock "phail" error previous run');
SKIP: {
skip 'only testing open files on Linux', 1 if $^O ne 'linux';
SKIP: {
skip 'only testing open files on Linux', 1 if $^O ne 'linux';