use IO::Handle;
use IO::Socket;
use Cwd qw/abs_path/;
+use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
STDOUT->autoflush(1);
STDERR->autoflush(1);
require Danga::Socket;
}
sub daemonize () {
- foreach my $i (0..$#ARGV) {
- my $arg = $ARGV[$i];
- next unless -e $arg;
- $ARGV[$i] = abs_path($arg);
- }
- check_absolute('stdout', $stdout);
- check_absolute('stderr', $stderr);
- check_absolute('pid-file', $pid_file);
+ if ($daemonize) {
+ foreach my $i (0..$#ARGV) {
+ my $arg = $ARGV[$i];
+ next unless -e $arg;
+ $ARGV[$i] = abs_path($arg);
+ }
+ check_absolute('stdout', $stdout);
+ check_absolute('stderr', $stderr);
+ check_absolute('pid-file', $pid_file);
- chdir '/' or die "chdir failed: $!";
- open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!";
+ chdir '/' or die "chdir failed: $!";
+ }
return unless (defined $pid_file || defined $group || defined $user
|| $daemonize);
die "could not fork: $!\n" unless defined $pid;
exit if $pid;
+ open(STDIN, '+<', '/dev/null') or
+ die "redirect stdin failed: $!\n";
open STDOUT, '>&STDIN' or die "redirect stdout failed: $!\n";
open STDERR, '>&STDIN' or die "redirect stderr failed: $!\n";
POSIX::setsid();
Danga::Socket->SetPostLoopCallback(sub {
my ($dmap, undef) = @_;
my $n = 0;
+ my $now = clock_gettime(CLOCK_MONOTONIC);
foreach my $s (values %$dmap) {
- if ($s->can('busy') && $s->busy) {
+ $s->can('busy') or next;
+ if ($s->busy($now)) {
++$n;
} else {
# close as much as possible, early as possible
} elsif ($s eq 'WINCH') {
if (-t STDIN || -t STDOUT || -t STDERR) {
warn
-"ignoring SIGWINCH while connected to terminal\n";
+"ignoring SIGWINCH since we are not daemonized\n";
$SIG{WINCH} = 'IGNORE';
} else {
$worker_processes = 0;
$SIG{QUIT} = $SIG{INT} = $SIG{TERM} = *worker_quit;
$SIG{USR1} = *reopen_logs;
$SIG{HUP} = $refresh;
- $SIG{$_} = 'DEFAULT' for qw(CHLD USR2 TTIN TTOU WINCH);
+ $SIG{CHLD} = 'DEFAULT';
+ $SIG{$_} = 'IGNORE' for qw(USR2 TTIN TTOU WINCH);
# this calls epoll_create:
@listeners = map {
PublicInbox::Listener->new($_, $post_accept)