user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
blob 688e30ad99cbee6c39a186c289a42eca4d3465e3 1014 bytes (raw)
name: t/common.perl 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
# 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];
	my $str = '';
	while (defined(my $chunk = $body->getline)) {
		$str .= $chunk;
	}
	$body->close;
	$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;

debug log:

solving 688e30a ...
found 688e30a in https://80x24.org/public-inbox.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).