# 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];
$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;
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);
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";
{
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->();
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');
}
}
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';
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;
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 $@;
}
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');
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');
}
};
SKIP: {
- use POSIX qw(dup2);
my $have_curl = 0;
foreach my $p (split(':', $ENV{PATH})) {
-x "$p/curl" or next;
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: $!";
}
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');
}
{
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';
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);
$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,
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";
}
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;
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");
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 = (
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 $@;
}
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';
$! = 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;
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');
}
$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');
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 $@;
}
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);
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);