]> Sergey Matveev's repositories - public-inbox.git/blob - t/common.perl
daemon: warn on inheriting blocking listeners
[public-inbox.git] / t / common.perl
1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
5 use POSIX qw(dup2);
6 use strict;
7 use warnings;
8
9 sub stream_to_string {
10         my ($res) = @_;
11         my $body = $res->[2];
12         my $str = '';
13         while (defined(my $chunk = $body->getline)) {
14                 $str .= $chunk;
15         }
16         $body->close;
17         $str;
18 }
19
20 sub tcp_server () {
21         IO::Socket::INET->new(
22                 LocalAddr => '127.0.0.1',
23                 ReuseAddr => 1,
24                 Proto => 'tcp',
25                 Type => Socket::SOCK_STREAM(),
26                 Listen => 1024,
27                 Blocking => 0,
28         )
29 }
30
31 sub unix_server ($) {
32         my $s = IO::Socket::UNIX->new(
33                 Listen => 1024,
34                 Type => Socket::SOCK_STREAM(),
35                 Local => $_[0],
36         );
37         $s->blocking(0);
38         $s;
39 }
40
41 sub spawn_listener {
42         my ($env, $cmd, $socks) = @_;
43         my $pid = fork;
44         defined $pid or die "fork failed: $!\n";
45         if ($pid == 0) {
46                 # pretend to be systemd (cf. sd_listen_fds(3))
47                 my $fd = 3; # 3 == SD_LISTEN_FDS_START
48                 foreach my $s (@$socks) {
49                         my $fl = fcntl($s, F_GETFD, 0);
50                         if (($fl & FD_CLOEXEC) != FD_CLOEXEC) {
51                                 warn "got FD:".fileno($s)." w/o CLOEXEC\n";
52                         }
53                         fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC);
54                         dup2(fileno($s), $fd++) or die "dup2 failed: $!\n";
55                 }
56                 $ENV{LISTEN_PID} = $$;
57                 $ENV{LISTEN_FDS} = scalar @$socks;
58                 %ENV = (%ENV, %$env) if $env;
59                 exec @$cmd;
60                 die "FAIL: ",join(' ', @$cmd), ": $!\n";
61         }
62         $pid;
63 }
64
65 sub require_git ($;$) {
66         my ($req, $maybe) = @_;
67         my ($req_maj, $req_min) = split(/\./, $req);
68         my ($cur_maj, $cur_min) = (`git --version` =~ /version (\d+)\.(\d+)/);
69
70         my $req_int = ($req_maj << 24) | ($req_min << 16);
71         my $cur_int = ($cur_maj << 24) | ($cur_min << 16);
72         if ($cur_int < $req_int) {
73                 return 0 if $maybe;
74                 plan skip_all => "git $req+ required, have $cur_maj.$cur_min";
75         }
76         1;
77 }
78
79 1;