user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/3] spawn: support send_fd+recv_fd w/o IO::FDPass
Date: Sun,  3 Jan 2021 09:48:42 +0000	[thread overview]
Message-ID: <20210103094842.22906-4-e@80x24.org> (raw)
In-Reply-To: <20210103094842.22906-1-e@80x24.org>

IO::FDPass may be an extra installation burden I don't want to
impose on users.  We only support Linux and *BSDs, however.
---
 lib/PublicInbox/LEI.pm   |  6 ++--
 lib/PublicInbox/Spawn.pm | 78 +++++++++++++++++++++++++++++++++++++---
 script/lei               |  7 ++--
 t/lei.t                  |  6 +++-
 t/spawn.t                | 18 ++++++++++
 5 files changed, 106 insertions(+), 9 deletions(-)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 2bc4a916..6f98c934 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -25,6 +25,7 @@ use Text::Wrap qw(wrap);
 use File::Path qw(mkpath);
 use File::Spec;
 our $quit = \&CORE::exit;
+my $recv_fd;
 my $GLP = Getopt::Long::Parser->new;
 $GLP->configure(qw(gnu_getopt no_ignore_case auto_abbrev));
 my $GLP_PASS = Getopt::Long::Parser->new;
@@ -615,7 +616,7 @@ sub accept_dispatch { # Listener {post_accept} callback
 	# `say $sock' triggers "die" in lei(1)
 	for my $i (0..2) {
 		if (select(my $rout = $rin, undef, undef, 1)) {
-			my $fd = IO::FDPass::recv(fileno($sock));
+			my $fd = $recv_fd->(fileno($sock));
 			if ($fd >= 0) {
 				my $rdr = ($fd == 0 ? '<&=' : '>&=');
 				if (open(my $fh, $rdr, $fd)) {
@@ -671,7 +672,8 @@ sub lazy_start {
 	my $dev_ino_expect = pack('dd', $st[0], $st[1]); # dev+ino
 	pipe(my ($eof_r, $eof_w)) or die "pipe: $!";
 	my $oldset = PublicInbox::Sigfd::block_signals();
-	require IO::FDPass;
+	$recv_fd = PublicInbox::Spawn->can('recv_fd') or die
+		"Inline::C not installed/configured or IO::FDPass missing\n";
 	require PublicInbox::Listener;
 	require PublicInbox::EOFpipe;
 	(-p STDOUT) or die "E: stdout must be a pipe\n";
diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm
index 762a0549..4ca94b9f 100644
--- a/lib/PublicInbox/Spawn.pm
+++ b/lib/PublicInbox/Spawn.pm
@@ -201,12 +201,72 @@ void nodatacow_dir(const char *dir)
 }
 SET_NODATACOW
 
+my $fdpass = <<'FDPASS';
+#include <sys/types.h>
+#include <sys/uio.h>
+#include <sys/socket.h>
+
+#if defined(CMSG_SPACE) && defined(CMSG_LEN)
+union my_cmsg {
+	struct cmsghdr hdr;
+	char pad[sizeof(struct cmsghdr)+8+sizeof(int)+8];
+};
+
+int send_fd(int sockfd, int fd)
+{
+	struct msghdr msg = { 0 };
+	struct iovec iov;
+	union my_cmsg cmsg = { 0 };
+
+	iov.iov_base = &msg.msg_namelen;
+	iov.iov_len = 1;
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = &cmsg.hdr;
+	msg.msg_controllen = CMSG_SPACE(sizeof(int));
+
+	cmsg.hdr.cmsg_level = SOL_SOCKET;
+	cmsg.hdr.cmsg_type = SCM_RIGHTS;
+	cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(int));
+	*(int *)CMSG_DATA(&cmsg.hdr) = fd;
+
+	return sendmsg(sockfd, &msg, 0) >= 0;
+}
+
+int recv_fd(int sockfd)
+{
+	union my_cmsg cmsg = { 0 };
+	struct msghdr msg = { 0 };
+	struct iovec iov;
+	int fd = -1;
+
+	iov.iov_base = &msg.msg_namelen;
+	iov.iov_len = 1;
+	msg.msg_iov = &iov;
+	msg.msg_iovlen = 1;
+	msg.msg_control = &cmsg.hdr;
+	msg.msg_controllen = CMSG_SPACE(sizeof(int));
+
+	if (recvmsg(sockfd, &msg, 0) <= 0)
+		return -1;
+
+	errno = EDOM;
+	if (cmsg.hdr.cmsg_level == SOL_SOCKET &&
+			cmsg.hdr.cmsg_type == SCM_RIGHTS &&
+			cmsg.hdr.cmsg_len == CMSG_LEN(sizeof(int)))
+		fd = *(int *)CMSG_DATA(&cmsg.hdr);
+
+	return fd;
+}
+#endif /* defined(CMSG_SPACE) && defined(CMSG_LEN) */
+FDPASS
+
 my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //= (
 		$ENV{XDG_CACHE_HOME} //
 		( ($ENV{HOME} // '/nonexistent').'/.cache' )
 	).'/public-inbox/inline-c';
 
-$set_nodatacow = $vfork_spawn = undef unless -d $inline_dir && -w _;
+$set_nodatacow = $vfork_spawn = $fdpass = undef unless -d $inline_dir && -w _;
 if (defined $vfork_spawn) {
 	# Inline 0.64 or later has locking in multi-process env,
 	# but we support 0.5 on Debian wheezy
@@ -215,13 +275,13 @@ if (defined $vfork_spawn) {
 		my $f = "$inline_dir/.public-inbox.lock";
 		open my $fh, '>', $f or die "failed to open $f: $!\n";
 		flock($fh, LOCK_EX) or die "LOCK_EX failed on $f: $!\n";
-		eval 'use Inline C => $vfork_spawn . $set_nodatacow';
+		eval 'use Inline C => $vfork_spawn . $fdpass . $set_nodatacow';
 		my $err = $@;
 		my $ndc_err;
 		if ($err && $set_nodatacow) { # missing Linux kernel headers
 			$ndc_err = $err;
 			undef $set_nodatacow;
-			eval 'use Inline C => $vfork_spawn';
+			eval 'use Inline C => $vfork_spawn . $fdpass';
 		}
 		flock($fh, LOCK_UN) or die "LOCK_UN failed on $f: $!\n";
 		die $err if $err;
@@ -229,7 +289,7 @@ if (defined $vfork_spawn) {
 	};
 	if ($@) {
 		warn "Inline::C failed for vfork: $@\n";
-		$set_nodatacow = $vfork_spawn = undef;
+		$set_nodatacow = $vfork_spawn = $fdpass = undef;
 	}
 }
 
@@ -243,8 +303,18 @@ unless ($set_nodatacow) {
 	*nodatacow_fd = \&PublicInbox::NDC_PP::nodatacow_fd;
 	*nodatacow_dir = \&PublicInbox::NDC_PP::nodatacow_dir;
 }
+unless (__PACKAGE__->can('recv_fd')) {
+	eval { # try the XS IO::FDPass package
+		require IO::FDPass;
+		no warnings 'once';
+		*recv_fd = \&IO::FDPass::recv;
+		*send_fd = \&IO::FDPass::send;
+	};
+}
+
 undef $set_nodatacow;
 undef $vfork_spawn;
+undef $fdpass;
 
 sub which ($) {
 	my ($file) = @_;
diff --git a/script/lei b/script/lei
index ff4dcd45..67e8b8b0 100755
--- a/script/lei
+++ b/script/lei
@@ -4,8 +4,11 @@
 use strict;
 use v5.10.1;
 use Socket qw(AF_UNIX SOCK_STREAM pack_sockaddr_un);
+my $send_fd;
 if (my ($sock, $pwd) = eval {
-	require IO::FDPass; # will try to use a daemon to reduce load time
+	require PublicInbox::Spawn;
+	$send_fd = PublicInbox::Spawn->can('send_fd') or die
+		"Inline::C not installed/configured or IO::FDPass missing\n";
 	my $path = do {
 		my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
 		if ($runtime_dir eq '/lei') {
@@ -57,7 +60,7 @@ Falling back to (slow) one-shot mode
 	$buf .= "\0\0";
 	select $sock;
 	$| = 1; # unbuffer selected $sock
-	IO::FDPass::send(fileno($sock), $_) for (0..2);
+	$send_fd->(fileno($sock), $_) for (0..2);
 	print $sock $buf or die "print(sock, buf): $!";
 	while ($buf = <$sock>) {
 		$buf =~ /\Aexit=([0-9]+)\n\z/ and exit($1 + 0);
diff --git a/t/lei.t b/t/lei.t
index 541d83ce..662fc545 100644
--- a/t/lei.t
+++ b/t/lei.t
@@ -192,7 +192,11 @@ if ($ENV{TEST_LEI_ONESHOT}) {
 }
 
 SKIP: { # real socket
-	require_mods(qw(IO::FDPass Cwd), 46);
+	require_mods(qw(Cwd), my $nr = 46);
+	require PublicInbox::Spawn;
+	skip "Inline::C not installed/configured or IO::FDPass missing", $nr
+		unless PublicInbox::Spawn->can('send_fd');
+
 	local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
 	my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
 
diff --git a/t/spawn.t b/t/spawn.t
index d97e13a6..e5cb09d9 100644
--- a/t/spawn.t
+++ b/t/spawn.t
@@ -5,6 +5,24 @@ use warnings;
 use Test::More;
 use PublicInbox::Spawn qw(which spawn popen_rd);
 use PublicInbox::Sigfd;
+use Socket qw(AF_UNIX SOCK_STREAM);
+
+SKIP: {
+	my $recv_fd = PublicInbox::Spawn->can('recv_fd');
+	my $send_fd = PublicInbox::Spawn->can('send_fd');
+	skip 'Inline::C not enabled', 3 unless $send_fd && $recv_fd;
+	my ($s1, $s2);
+	socketpair($s1, $s2, AF_UNIX, SOCK_STREAM, 0) or BAIL_OUT $!;
+	pipe(my ($r, $w)) or BAIL_OUT $!;
+	ok($send_fd->(fileno($s1), fileno($r)), 'pipe sent');
+	my $rfd = $recv_fd->(fileno($s2));
+	like($rfd, qr/\A\d+\z/, 'got FD');
+	open(my $rfh, '<&=', $rfd) or BAIL_OUT $!;
+	my @old = stat($r);
+	my @new = stat($rfh);
+	is("$old[0]\0$old[1]", "$new[0]\0$new[1]",
+		'device/inode matches on received FD');
+}
 
 {
 	my $true = which('true');

  parent reply	other threads:[~2021-01-03  9:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-03  9:48 [PATCH 0/3] lei-related test fixes Eric Wong
2021-01-03  9:48 ` [PATCH 1/3] t/lei: use $lei->() callback wrapper Eric Wong
2021-01-03  9:48 ` [PATCH 2/3] testcommon: prepare_redirects: fix error message Eric Wong
2021-01-03  9:48 ` Eric Wong [this message]
2021-01-03 11:24 ` [PATCH 0/2] fix race from stdout buffering in FD pass exit Eric Wong
2021-01-03 11:24   ` [PATCH 1/2] send and receive all 3 FDs at once Eric Wong
2021-01-03 11:24   ` [PATCH 2/2] lei: fix output race in client/daemon mode Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210103094842.22906-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).