]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Daemon.pm
daemon: favor Socket.pm in Perl 5.14+ for IPv6
[public-inbox.git] / lib / PublicInbox / Daemon.pm
index 9d27d2be522e908d4cca5b3d2e9c859aecea59b1..48051f488b6c5bbd08408c291794cc9a041a9c86 100644 (file)
@@ -234,6 +234,28 @@ sub sockname ($) {
        "$host:$port";
 }
 
+sub unpack_ipv6 ($) {
+       my ($addr) = @_;
+       my ($port, $host);
+
+       # Socket.pm in Perl 5.14+ supports IPv6:
+       eval {
+               ($port, $host) = Socket::unpack_sockaddr_in6($addr);
+               $host = Socket::inet_ntop(Socket::AF_INET6(), $host);
+       };
+
+       if ($@) {
+               # Perl 5.12 or earlier?  SpamAssassin and Net::Server use
+               # Socket6, so it may be installed on our system, already
+               # (otherwise die here):
+               require Socket6;
+
+               ($port, $host) = Socket6::unpack_sockaddr_in6($addr);
+               $host = Socket6::inet_ntop(Socket6::AF_INET6(), $host);
+       }
+       ($host, $port);
+}
+
 sub host_with_port ($) {
        my ($addr) = @_;
        my ($port, $host);
@@ -241,9 +263,7 @@ sub host_with_port ($) {
        # 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, $port) = unpack_ipv6($addr);
                        $host = "[$host]";
                } else {
                        ($port, $host) = Socket::sockaddr_in($addr);
@@ -356,6 +376,11 @@ sub unlink_pid_file_safe_ish ($$) {
 sub master_loop {
        pipe(my ($p0, $p1)) or die "failed to create parent-pipe: $!";
        pipe(my ($r, $w)) or die "failed to create self-pipe: $!";
+
+       if ($^O eq 'linux') { # 1031: F_SETPIPE_SZ = 1031
+               fcntl($_, 1031, 4096) for ($w, $p1);
+       }
+
        IO::Handle::blocking($w, 0);
        my $set_workers = $worker_processes;
        my @caught;
@@ -460,7 +485,7 @@ sub daemon_loop ($$) {
        @listeners = map {
                PublicInbox::Listener->new($_, $post_accept)
        } @listeners;
-       $PublicInbox::EvCleanup::ENABLED = 1;
+       PublicInbox::EvCleanup::enable();
        Danga::Socket->EventLoop;
        $parent_pipe = undef;
 }