# Copyright (C) 2015 all contributors <meta@public-inbox.org>
# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
-package PublicInbox::Daemon; # empty class :p
-
-# contains common daemon code for the nntpd server.
+# contains common daemon code for the nntpd and httpd servers.
# This may be used for read-only IMAP server if we decide to implement it.
-package main;
+package PublicInbox::Daemon;
use strict;
use warnings;
use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
use IO::Handle;
+use IO::Socket;
STDOUT->autoflush(1);
STDERR->autoflush(1);
require Danga::Socket;
foreach my $l (@cfg_listen) {
next if $listener_names{$l}; # already inherited
- require IO::Socket::INET6; # works for IPv4, too
- my %o = (
- LocalAddr => $l,
- ReuseAddr => 1,
- Proto => 'tcp',
- );
- if (my $s = IO::Socket::INET6->new(%o)) {
+ my (%o, $sock_pkg);
+ if (index($l, '/') == 0) {
+ $sock_pkg = 'IO::Socket::UNIX';
+ eval "use $sock_pkg";
+ die $@ if $@;
+ %o = (Type => SOCK_STREAM, Peer => $l);
+ if (-S $l) {
+ my $c = $sock_pkg->new(%o);
+ if (!defined($c) && $!{ECONNREFUSED}) {
+ unlink $l or die
+"failed to unlink stale socket=$l: $!\n";
+ } # else: let the bind fail
+ }
+ $o{Local} = delete $o{Peer};
+ } else {
+ $sock_pkg = 'IO::Socket::INET6'; # works for IPv4, too
+ eval "use $sock_pkg";
+ die $@ if $@;
+ %o = (LocalAddr => $l, ReuseAddr => 1, Proto => 'tcp');
+ }
+ $o{Listen} = 1024;
+ my $prev = umask 0000;
+ my $s = eval { $sock_pkg->new(%o) };
+ warn "error binding $l: $!\n" unless $s;
+ umask $prev;
+
+ if ($s) {
$listener_names{sockname($s)} = $s;
push @listeners, $s;
- } else {
- warn "error binding $l: $!\n";
}
}
- die 'No listeners bound' unless @listeners;
+ die "No listeners bound\n" unless @listeners;
}
sub daemonize () {
- chdir '/' or die "chdir failed: $!\n";
- open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!\n";
+ chdir '/' or die "chdir failed: $!";
+ open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!";
return unless (defined $pid_file || defined $group || defined $user
|| $daemonize);
# killing again terminates immediately:
exit unless @listeners;
+ $_->close foreach @listeners; # call Danga::Socket::close
@listeners = ();
# give slow clients 30s to finish reading/writing whatever
sub sockname ($) {
my ($s) = @_;
- my $n = getsockname($s) or return;
- my ($port, $addr);
- if (length($n) >= 28) {
- require Socket6;
- ($port, $addr) = Socket6::unpack_sockaddr_in6($n);
- } else {
- ($port, $addr) = Socket::sockaddr_in($n);
- }
- if (length($addr) == 4) {
- $n = Socket::inet_ntoa($addr)
- } else {
- $n = '['.Socket6::inet_ntop(Socket6::AF_INET6(), $addr).']';
- }
- $n .= ":$port";
+ my $addr = getsockname($s) or return;
+ my ($host, $port) = host_with_port($addr);
+ "$host:$port";
+}
+
+sub host_with_port ($) {
+ my ($addr) = @_;
+ my ($port, $host);
+
+ # this eval will die on Unix sockets:
+ eval {
+ if (length($addr) >= 28) {
+ require Socket6;
+ ($port, $host) = Socket6::unpack_sockaddr_in6($addr);
+ $host = Socket6::inet_ntop(Socket6::AF_INET6(), $host);
+ $host = "[$host]";
+ } else {
+ ($port, $host) = Socket::sockaddr_in($addr);
+ $host = Socket::inet_ntoa($host);
+ }
+ };
+ $@ ? ('127.0.0.1', 0) : ($host, $port);
}
sub inherit () {
my $end = $fds + 2; # LISTEN_FDS_START - 1
my @rv = ();
foreach my $fd (3..$end) {
- my $s = IO::Handle->new;
- $s->fdopen($fd, 'r');
+ my $s = IO::Handle->new_from_fd($fd, 'r');
if (my $k = sockname($s)) {
$listener_names{$k} = $s;
push @rv, $s;
my $new = POSIX::SigSet->new;
$new->fillset;
my $old = POSIX::SigSet->new;
- POSIX::sigprocmask(&POSIX::SIG_BLOCK, $new, $old) or
- die "SIG_BLOCK: $!\n";
+ POSIX::sigprocmask(&POSIX::SIG_BLOCK, $new, $old) or die "SIG_BLOCK: $!";
my $pid = fork;
my $err = $!;
- POSIX::sigprocmask(&POSIX::SIG_SETMASK, $old) or
- die "SIG_SETMASK: $!\n";
+ POSIX::sigprocmask(&POSIX::SIG_SETMASK, $old) or die "SIG_SETMASK: $!";
($pid, $err);
}
return unless $pid_file;
my $file = $pid_file;
- $file =~ s/\.oldbin\z// or die "BUG: no '.oldbin' suffix in $file\n";
+ $file =~ s/\.oldbin\z// or die "BUG: no '.oldbin' suffix in $file";
unlink_pid_file_safe_ish($$, $pid_file);
$pid_file = $file;
eval { write_pid($pid_file) };
}
sub master_loop {
- pipe(my ($p0, $p1)) or die "failed to create parent-pipe: $!\n";
- pipe(my ($r, $w)) or die "failed to create self-pipe: $!\n";
+ pipe(my ($p0, $p1)) or die "failed to create parent-pipe: $!";
+ pipe(my ($r, $w)) or die "failed to create self-pipe: $!";
IO::Handle::blocking($w, 0);
my $set_workers = $worker_processes;
my @caught;
my ($refresh, $post_accept) = @_;
my $parent_pipe;
if ($worker_processes > 0) {
+ $refresh->(); # preload by default
$parent_pipe = master_loop(); # returns if in child process
my $fd = fileno($parent_pipe);
Danga::Socket->AddOtherFds($fd => sub { kill('TERM', $$) } );
reopen_logs();
$set_user->() if $set_user;
$SIG{USR2} = sub { worker_quit() if upgrade() };
+ $refresh->();
}
$uid = $gid = undef;
reopen_logs();
- $refresh->();
$SIG{QUIT} = $SIG{INT} = $SIG{TERM} = *worker_quit;
$SIG{USR1} = *reopen_logs;
$SIG{HUP} = $refresh;
# this calls epoll_create:
- PublicInbox::Listener->new($_, $post_accept) for @listeners;
+ @listeners = map {
+ PublicInbox::Listener->new($_, $post_accept)
+ } @listeners;
Danga::Socket->EventLoop;
$parent_pipe = undef;
}
-sub daemon_run ($$$) {
+sub run ($$$) {
my ($default, $refresh, $post_accept) = @_;
daemon_prepare($default);
daemonize();