about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2018-12-26 09:07:49 +0000
committerEric Wong <e@80x24.org>2018-12-29 03:45:37 +0000
commit59c946a014f34cd90621b1fb3b30af99ba80bf61 (patch)
tree194643ecd5d2388dbfd62a4503842f03c414be13
parent2b5da4383d9c6defd2d473bf02eb8920bd589890 (diff)
downloadpublic-inbox-59c946a014f34cd90621b1fb3b30af99ba80bf61.tar.gz
IPC::Run provides a nice simplification in several places; and
we already use it (optionally) on a lot of tests.

For the non-test code, we still rely on our vfork-capable
Inline::C stuff since real-world server processes can get large
enough to where vfork is an advantage.  Maybe Perl5 can use
CLONE_VFORK somehow, one day:

  https://rt.perl.org/Ticket/Display.html?id=128227

Ohg V'q engure cbeg choyvp-vaobk gb Ehol :C
-rw-r--r--t/common.perl27
-rw-r--r--t/git-http-backend.t15
-rw-r--r--t/git.t33
-rw-r--r--t/httpd-corner.t51
-rw-r--r--t/httpd-unix.t1
-rw-r--r--t/httpd.t23
-rw-r--r--t/nntpd.t22
-rw-r--r--t/perf-nntpd.t21
-rw-r--r--t/v2mirror.t50
-rw-r--r--t/v2writable.t26
10 files changed, 76 insertions, 193 deletions
diff --git a/t/common.perl b/t/common.perl
index 5c5fcd84..688e30ad 100644
--- a/t/common.perl
+++ b/t/common.perl
@@ -1,6 +1,9 @@
 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
+use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
+use POSIX qw(dup2);
+
 sub stream_to_string {
         my ($res) = @_;
         my $body = $res->[2];
@@ -12,4 +15,28 @@ sub stream_to_string {
         $str;
 }
 
+sub spawn_listener {
+        my ($env, $cmd, $socks) = @_;
+        my $pid = fork;
+        defined $pid or die "fork failed: $!\n";
+        if ($pid == 0) {
+                # pretend to be systemd (cf. sd_listen_fds(3))
+                my $fd = 3; # 3 == SD_LISTEN_FDS_START
+                foreach my $s (@$socks) {
+                        my $fl = fcntl($s, F_GETFD, 0);
+                        if (($fl & FD_CLOEXEC) != FD_CLOEXEC) {
+                                warn "got FD:".fileno($s)." w/o CLOEXEC\n";
+                        }
+                        fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC);
+                        dup2(fileno($s), $fd++) or die "dup2 failed: $!\n";
+                }
+                $ENV{LISTEN_PID} = $$;
+                $ENV{LISTEN_FDS} = scalar @$socks;
+                %ENV = (%ENV, %$env) if $env;
+                exec @$cmd;
+                die "FAIL: ",join(' ', @$cmd), ": $!\n";
+        }
+        $pid;
+}
+
 1;
diff --git a/t/git-http-backend.t b/t/git-http-backend.t
index 046a7784..4e51f2bf 100644
--- a/t/git-http-backend.t
+++ b/t/git-http-backend.t
@@ -5,7 +5,6 @@ use warnings;
 use Test::More;
 use File::Temp qw/tempdir/;
 use IO::Socket;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
 use POSIX qw(dup2 setsid);
 use Cwd qw(getcwd);
@@ -18,6 +17,7 @@ foreach my $mod (qw(Danga::Socket BSD::Resource
         eval "require $mod";
         plan skip_all => "$mod missing for git-http-backend.t" if $@;
 }
+require './t/common.perl';
 my $psgi = getcwd()."/t/git-http-backend.psgi";
 my $tmpdir = tempdir('pi-git-http-backend-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $err = "$tmpdir/stderr.log";
@@ -51,16 +51,9 @@ my $get_maxrss = sub {
 
 {
         ok($sock, 'sock created');
-        $pid = fork;
-        if ($pid == 0) { # pretend to be systemd
-                fcntl($sock, F_SETFD, 0);
-                dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-                $ENV{LISTEN_PID} = $$;
-                $ENV{LISTEN_FDS} = 1;
-                exec $httpd, "--stdout=$out", "--stderr=$err", $psgi;
-                die "FAIL: $!\n";
-        }
-        ok(defined $pid, 'forked httpd process successfully');
+        my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err", $psgi ];
+        ok(defined($pid = spawn_listener(undef, $cmd, [$sock])),
+           'forked httpd process successfully');
 }
 my $mem_a = $get_maxrss->();
 
diff --git a/t/git.t b/t/git.t
index 7f96293f..5069ae2c 100644
--- a/t/git.t
+++ b/t/git.t
@@ -9,20 +9,15 @@ use Cwd qw/getcwd/;
 use PublicInbox::Spawn qw(popen_rd);
 
 use_ok 'PublicInbox::Git';
+eval { require IPC::Run } or plan skip_all => 'IPC::Run missing';
+
 {
         is(system(qw(git init -q --bare), $dir), 0, 'created git directory');
-        my @cmd = ('git', "--git-dir=$dir", 'fast-import', '--quiet');
+        my $cmd = [ 'git', "--git-dir=$dir", 'fast-import', '--quiet' ];
 
         my $fi_data = getcwd().'/t/git.fast-import-data';
         ok(-r $fi_data, "fast-import data readable (or run test at top level)");
-        my $pid = fork;
-        defined $pid or die "fork failed: $!\n";
-        if ($pid == 0) {
-                open STDIN, '<', $fi_data or die "open $fi_data: $!\n";
-                exec @cmd;
-                die "failed exec: ",join(' ', @cmd),": $!\n";
-        }
-        waitpid $pid, 0;
+        IPC::Run::run($cmd, '<', $fi_data);
         is($?, 0, 'fast-import succeeded');
 }
 
@@ -69,8 +64,7 @@ use_ok 'PublicInbox::Git';
 }
 
 if (1) {
-        use POSIX qw(dup2);
-        my @cmd = ('git', "--git-dir=$dir", qw(hash-object -w --stdin));
+        my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
 
         # need a big file, use the AGPL-3.0 :p
         my $big_data = getcwd().'/COPYING';
@@ -78,21 +72,8 @@ if (1) {
         my $size = -s $big_data;
         ok($size > 8192, 'file is big enough');
 
-        my ($r, $w);
-        ok(pipe($r, $w), 'created pipe');
-
-        my $pid = fork;
-        defined $pid or die "fork failed: $!\n";
-        if ($pid == 0) {
-                close $r;
-                open STDIN, '<', $big_data or die "open $big_data: $!\n";
-                dup2(fileno($w), 1);
-                exec @cmd;
-                die "failed exec: ",join(' ', @cmd),": $!\n";
-        }
-        close $w;
-        my $n = read $r, my $buf, 41;
-        waitpid $pid, 0;
+        my $buf = '';
+        IPC::Run::run($cmd, '<', $big_data, '>', \$buf);
         is(0, $?, 'hashed object successfully');
         chomp $buf;
 
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index a720670e..aa0698d3 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -8,7 +8,7 @@ use Test::More;
 use Time::HiRes qw(gettimeofday tv_interval);
 
 foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
-                        HTTP::Date HTTP::Status)) {
+                        HTTP::Date HTTP::Status IPC::Run)) {
         eval "require $mod";
         plan skip_all => "$mod missing for httpd-corner.t" if $@;
 }
@@ -18,9 +18,10 @@ use File::Temp qw/tempdir/;
 use Cwd qw/getcwd/;
 use IO::Socket;
 use IO::Socket::UNIX;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
+use Fcntl qw(:seek);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
-use POSIX qw(dup2 mkfifo :sys_wait_h);
+use POSIX qw(mkfifo :sys_wait_h);
+require './t/common.perl';
 my $tmpdir = tempdir('httpd-corner-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $fifo = "$tmpdir/fifo";
 ok(defined mkfifo($fifo, 0777), 'created FIFO');
@@ -47,33 +48,13 @@ my $pid;
 END { kill 'TERM', $pid if defined $pid };
 my $spawn_httpd = sub {
         my (@args) = @_;
-        $! = 0;
-        my $fl = fcntl($sock, F_GETFD, 0);
-        ok(! $!, 'no error from fcntl(F_GETFD)');
-        is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
-        $pid = fork;
-        if ($pid == 0) {
-                # pretend to be systemd
-                dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-                dup2(fileno($unix), 4) or die "dup2 failed: $!\n";
-                my $t = IO::Handle->new_from_fd(3, 'r');
-                $t->fcntl(F_SETFD, 0);
-                my $u = IO::Handle->new_from_fd(4, 'r');
-                $u->fcntl(F_SETFD, 0);
-                $ENV{LISTEN_PID} = $$;
-                $ENV{LISTEN_FDS} = 2;
-                exec $httpd, @args, "--stdout=$out", "--stderr=$err", $psgi;
-                die "FAIL: $!\n";
-        }
+        my $cmd = [ $httpd, @args, "--stdout=$out", "--stderr=$err", $psgi ];
+        $pid = spawn_listener(undef, $cmd, [ $sock, $unix ]);
         ok(defined $pid, 'forked httpd process successfully');
 };
 
 {
         ok($sock, 'sock created');
-        $! = 0;
-        my $fl = fcntl($sock, F_GETFD, 0);
-        ok(! $!, 'no error from fcntl(F_GETFD)');
-        is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
         $spawn_httpd->('-W0');
 }
 
@@ -242,7 +223,6 @@ my $check_self = sub {
 };
 
 SKIP: {
-        use POSIX qw(dup2);
         my $have_curl = 0;
         foreach my $p (split(':', $ENV{PATH})) {
                 -x "$p/curl" or next;
@@ -254,15 +234,9 @@ SKIP: {
         my $url = 'http://' . $sock->sockhost . ':' . $sock->sockport . '/sha1';
         my ($r, $w);
         pipe($r, $w) or die "pipe: $!";
-        open(my $tout, '+>', undef) or die "open temporary file: $!";
-        my $pid = fork;
-        defined $pid or die "fork: $!";
-        my @cmd = (qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url);
-        if ($pid == 0) {
-                dup2(fileno($r), 0) or die "redirect stdin failed: $!\n";
-                dup2(fileno($tout), 1) or die "redirect stdout failed: $!\n";
-                exec(@cmd) or die 'exec `' . join(' '). "' failed: $!\n";
-        }
+        my $cmd = [qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url];
+        my ($out, $err) = ('', '');
+        my $h = IPC::Run::start($cmd, $r, \$out, \$err);
         $w->autoflush(1);
         foreach my $c ('a'..'z') {
                 print $w $c or die "failed to write to curl: $!";
@@ -270,11 +244,10 @@ SKIP: {
         }
         close $w or die "close write pipe: $!";
         close $r or die "close read pipe: $!";
-        my $kid = waitpid $pid, 0;
+        IPC::Run::finish($h);
         is($?, 0, 'curl exited successfully');
-        $tout->sysseek(0, SEEK_SET);
-        $tout->sysread(my $buf, 100);
-        is($buf, sha1_hex($str), 'read expected body');
+        is($err, '', 'no errors from curl');
+        is($out, sha1_hex($str), 'read expected body');
 }
 
 {
diff --git a/t/httpd-unix.t b/t/httpd-unix.t
index b3cf8693..0a93f204 100644
--- a/t/httpd-unix.t
+++ b/t/httpd-unix.t
@@ -14,7 +14,6 @@ foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
 use File::Temp qw/tempdir/;
 use IO::Socket::UNIX;
 use Cwd qw/getcwd/;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
 my $tmpdir = tempdir('httpd-unix-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $unix = "$tmpdir/unix.sock";
 my $httpd = 'blib/script/public-inbox-httpd';
diff --git a/t/httpd.t b/t/httpd.t
index f33c0969..44df1642 100644
--- a/t/httpd.t
+++ b/t/httpd.t
@@ -12,8 +12,8 @@ foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
 use File::Temp qw/tempdir/;
 use Cwd qw/getcwd/;
 use IO::Socket;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
+require './t/common.perl';
 
 # FIXME: too much setup
 my $tmpdir = tempdir('pi-httpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
@@ -64,25 +64,8 @@ EOF
                 $im->done($mime);
         }
         ok($sock, 'sock created');
-        $! = 0;
-        my $fl = fcntl($sock, F_GETFD, 0);
-        ok(! $!, 'no error from fcntl(F_GETFD)');
-        is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
-        $pid = fork;
-        if ($pid == 0) {
-                use POSIX qw(dup2);
-                # pretend to be systemd
-                fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-                dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-                $ENV{LISTEN_PID} = $$;
-                $ENV{LISTEN_FDS} = 1;
-                exec $httpd, "--stdout=$out", "--stderr=$err";
-                die "FAIL: $!\n";
-        }
-        ok(defined $pid, 'forked httpd process successfully');
-        $! = 0;
-        fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
-        ok(! $!, 'no error from fcntl(F_SETFD)');
+        my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err" ];
+        $pid = spawn_listener(undef, $cmd, [$sock]);
         my $host = $sock->sockhost;
         my $port = $sock->sockport;
         my $conn = IO::Socket::INET->new(PeerAddr => $host,
diff --git a/t/nntpd.t b/t/nntpd.t
index ffed4376..d227b74d 100644
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -12,11 +12,11 @@ require PublicInbox::Msgmap;
 use Cwd;
 use Email::Simple;
 use IO::Socket;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
 use File::Temp qw/tempdir/;
 use Net::NNTP;
 use Sys::Hostname;
+require './t/common.perl';
 
 my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $home = "$tmpdir/pi-home";
@@ -101,25 +101,9 @@ EOF
         }
 
         ok($sock, 'sock created');
-        $! = 0;
-        my $fl = fcntl($sock, F_GETFD, 0);
-        ok(! $!, 'no error from fcntl(F_GETFD)');
-        is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
-        $pid = fork;
-        if ($pid == 0) {
-                use POSIX qw(dup2);
-                # pretend to be systemd
-                fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-                dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-                $ENV{LISTEN_PID} = $$;
-                $ENV{LISTEN_FDS} = 1;
-                exec $nntpd, "--stdout=$out", "--stderr=$err";
-                die "FAIL: $!\n";
-        }
+        my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
+        $pid = spawn_listener(undef, $cmd, [ $sock ]);
         ok(defined $pid, 'forked nntpd process successfully');
-        $! = 0;
-        fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
-        ok(! $!, 'no error from fcntl(F_SETFD)');
         my $host_port = $sock->sockhost . ':' . $sock->sockport;
         my $n = Net::NNTP->new($host_port);
         my $list = $n->list;
diff --git a/t/perf-nntpd.t b/t/perf-nntpd.t
index f7890d1c..3ca1ce33 100644
--- a/t/perf-nntpd.t
+++ b/t/perf-nntpd.t
@@ -6,13 +6,13 @@ use Test::More;
 use Benchmark qw(:all :hireswallclock);
 use PublicInbox::Inbox;
 use File::Temp qw/tempdir/;
-use POSIX qw(dup2);
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 use Net::NNTP;
 my $pi_dir = $ENV{GIANT_PI_DIR};
 plan skip_all => "GIANT_PI_DIR not defined for $0" unless $pi_dir;
 eval { require PublicInbox::Search };
 my ($host_port, $group, %opts, $s, $pid);
+require './t/common.perl';
+
 END {
         if ($s) {
                 $s->print("QUIT\r\n");
@@ -53,21 +53,8 @@ if (($ENV{NNTP_TEST_URL} || '') =~ m!\Anntp://([^/]+)/([^/]+)\z!) {
         my $sock = IO::Socket::INET->new(%opts);
 
         ok($sock, 'sock created');
-        $! = 0;
-        $pid = fork;
-        if ($pid == 0) {
-                # pretend to be systemd
-                my $fl = fcntl($sock, F_GETFD, 0);
-                dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-                dup2(1, 2) or die "dup2 failed: $!\n";
-                fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-                $ENV{LISTEN_PID} = $$;
-                $ENV{LISTEN_FDS} = 1;
-                $ENV{PI_CONFIG} = $pi_config;
-                exec $nntpd, '-W0';
-                die "FAIL: $!\n";
-        }
-        ok(defined $pid, 'forked nntpd process successfully');
+        my $cmd = [ $nntpd, '-W0' ];
+        $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [$sock]);
         $host_port = $sock->sockhost . ':' . $sock->sockport;
 }
 %opts = (
diff --git a/t/v2mirror.t b/t/v2mirror.t
index f95ad0f5..283b2b22 100644
--- a/t/v2mirror.t
+++ b/t/v2mirror.t
@@ -3,10 +3,12 @@
 use strict;
 use warnings;
 use Test::More;
+require './t/common.perl';
 
 # Integration tests for HTTP cloning + mirroring
 foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
-                        HTTP::Date HTTP::Status Search::Xapian DBD::SQLite)) {
+                        HTTP::Date HTTP::Status Search::Xapian DBD::SQLite
+                        IPC::Run)) {
         eval "require $mod";
         plan skip_all => "$mod missing for v2mirror.t" if $@;
 }
@@ -16,7 +18,6 @@ use POSIX qw(dup2);
 use_ok 'PublicInbox::V2Writable';
 use PublicInbox::MIME;
 use PublicInbox::Config;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 # FIXME: too much setup
 my $tmpdir = tempdir('pi-v2mirror-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $script = 'blib/script/public-inbox';
@@ -69,18 +70,9 @@ END { kill 'TERM', $pid if defined $pid };
 $! = 0;
 $sock = IO::Socket::INET->new(%opts);
 ok($sock, 'sock created');
-my $fl = fcntl($sock, F_GETFD, 0);
-$pid = fork;
-if ($pid == 0) {
-        # pretend to be systemd
-        fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-        dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-        $ENV{LISTEN_PID} = $$;
-        $ENV{LISTEN_FDS} = 1;
-        exec "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err";
-        die "FAIL: $!\n";
-}
-ok(defined $pid, 'forked httpd process successfully');
+my $cmd = [ "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
+ok(defined($pid = spawn_listener(undef, $cmd, [ $sock ])),
+        'spawned httpd process successfully');
 my ($host, $port) = ($sock->sockhost, $sock->sockport);
 $sock = undef;
 
@@ -150,18 +142,9 @@ is(scalar($mset->items), 0, 'purged message gone from origin');
 
 fetch_each_epoch();
 {
-        open my $err, '+>', "$tmpdir/index-err" or die "open: $!";
-        my $ipid = fork;
-        if ($ipid == 0) {
-                dup2(fileno($err), 2) or die "dup2 failed: $!";
-                exec("$script-index", '--prune', "$tmpdir/m");
-                die "exec fail: $!";
-        }
-        ok($ipid, 'running index..');
-        is(waitpid($ipid, 0), $ipid, 'index --prune done');
-        is($?, 0, 'no error from index');
-        ok(seek($err, 0, 0), 'rewound stderr');
-        $err = eval { local $/; <$err> };
+        my $cmd = [ "$script-index", '--prune', "$tmpdir/m" ];
+        my ($in, $out, $err) = ('', '', '');
+        ok(IPC::Run::run($cmd, \$in, \$out, \$err), '-index --prune');
         like($err, qr/discontiguous range/, 'warned about discontiguous range');
         unlike($err, qr/fatal/, 'no scary fatal error shown');
 }
@@ -192,18 +175,9 @@ is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
         $v2w->done;
         fetch_each_epoch();
 
-        open my $err, '+>', "$tmpdir/index-err" or die "open: $!";
-        my $ipid = fork;
-        if ($ipid == 0) {
-                dup2(fileno($err), 2) or die "dup2 failed: $!";
-                exec("$script-index", "$tmpdir/m");
-                die "exec fail: $!";
-        }
-        ok($ipid, 'running index');
-        is(waitpid($ipid, 0), $ipid, 'index done');
-        is($?, 0, 'no error from index');
-        ok(seek($err, 0, 0), 'rewound stderr');
-        $err = eval { local $/; <$err> };
+        my ($in, $out, $err) = ('', '', '');
+        my $cmd = [ "$script-index", "$tmpdir/m" ];
+        ok(IPC::Run::run($cmd, \$in, \$out, \$err), 'index ran');
         is($err, '', 'no errors reported by index');
         $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
         is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
diff --git a/t/v2writable.t b/t/v2writable.t
index b3cdbd3e..c7eeee99 100644
--- a/t/v2writable.t
+++ b/t/v2writable.t
@@ -6,6 +6,7 @@ use Test::More;
 use PublicInbox::MIME;
 use PublicInbox::ContentId qw(content_digest);
 use File::Temp qw/tempdir/;
+require './t/common.perl';
 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
         eval "require $mod";
         plan skip_all => "$mod missing for nntpd.t" if $@;
@@ -129,7 +130,6 @@ if ('ensure git configs are correct') {
 }
 
 SKIP: {
-        use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
         use Net::NNTP;
         use IO::Socket;
         use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
@@ -161,27 +161,9 @@ EOF
         my $pid;
         my $len;
         END { kill 'TERM', $pid if defined $pid };
-        $! = 0;
-        my $fl = fcntl($sock, F_GETFD, 0);
-        ok(! $!, 'no error from fcntl(F_GETFD)');
-        is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
-        $pid = fork;
-        if ($pid == 0) {
-                use POSIX qw(dup2);
-                $ENV{PI_CONFIG} = $pi_config;
-                # pretend to be systemd
-                fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-                dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-                $ENV{LISTEN_PID} = $$;
-                $ENV{LISTEN_FDS} = 1;
-                my $nntpd = 'blib/script/public-inbox-nntpd';
-                exec $nntpd, "--stdout=$out", "--stderr=$err";
-                die "FAIL: $!\n";
-        }
-        ok(defined $pid, 'forked nntpd process successfully');
-        $! = 0;
-        fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
-        ok(! $!, 'no error from fcntl(F_SETFD)');
+        my $nntpd = 'blib/script/public-inbox-nntpd';
+        my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
+        $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [ $sock ]);
         my $host_port = $sock->sockhost . ':' . $sock->sockport;
         my $n = Net::NNTP->new($host_port);
         $n->group($group);