user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH 1/4] lei: fix idempotent STDERR redirect in workers
  @ 2023-11-15  9:21  5% ` Eric Wong
  0 siblings, 0 replies; 3+ results
From: Eric Wong @ 2023-11-15  9:21 UTC (permalink / raw)
  To: meta

This is needed to support forking from already-forked lei workers
and $lei->{2} is already STDERR.

Fixes: e015c3742f91 (lei: use autodie where appropriate, 2023-10-17)
---
 lib/PublicInbox/LEI.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 460aed40..8d235b37 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -581,7 +581,7 @@ sub _lei_atfork_child {
 		close($_) for (grep(defined, delete @$self{qw(0 1 2 sock)}));
 		delete $cfg->{-lei_store};
 	} else { # worker, Net::NNTP (Net::Cmd) uses STDERR directly
-		open STDERR, '+>&', $self->{2};
+		open STDERR, '+>&='.fileno($self->{2}); # idempotent w/ fileno
 		STDERR->autoflush(1);
 		$self->{2} = \*STDERR;
 		POSIX::setpgid(0, $$) // die "setpgid(0, $$): $!";

^ permalink raw reply related	[relevance 5%]

* [PATCH 26/30] lei: use autodie where appropriate
  2023-10-17 23:37  7% [PATCH 00/30] autodie-ification and code simplifications Eric Wong
@ 2023-10-17 23:38  5% ` Eric Wong
  0 siblings, 0 replies; 3+ results
From: Eric Wong @ 2023-10-17 23:38 UTC (permalink / raw)
  To: meta

This makes us a bit harsher with misbehaving clients, but we
only have one client implementation at the moment.
---
 lib/PublicInbox/LEI.pm | 48 ++++++++++++++++++------------------------
 1 file changed, 20 insertions(+), 28 deletions(-)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 1ff6d67f..3ccdd4f7 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -9,6 +9,7 @@ package PublicInbox::LEI;
 use v5.12;
 use parent qw(PublicInbox::DS PublicInbox::LeiExternal
 	PublicInbox::LeiQuery);
+use autodie qw(bind chdir fork open socket socketpair unlink);
 use Getopt::Long ();
 use Socket qw(AF_UNIX SOCK_SEQPACKET pack_sockaddr_un);
 use Errno qw(EPIPE EAGAIN ECONNREFUSED ENOENT ECONNRESET);
@@ -29,7 +30,6 @@ use File::Path ();
 use File::Spec;
 use Carp ();
 use Sys::Syslog qw(openlog syslog closelog);
-use Scalar::Util qw(looks_like_number);
 our $quit = \&CORE::exit;
 our ($current_lei, $errors_log, $listener, $oldset, $dir_idle);
 my $GLP = Getopt::Long::Parser->new;
@@ -574,12 +574,12 @@ sub _lei_atfork_child {
 	# we need to explicitly close things which are on stack
 	my $cfg = $self->{cfg};
 	if ($persist) {
-		open $self->{3}, '<', '/' or die "open(/) $!";
+		open $self->{3}, '<', '/';
 		fchdir($self);
 		close($_) for (grep(defined, delete @$self{qw(0 1 2 sock)}));
 		delete @$cfg{qw(-lei_store -watches -lei_note_event)};
 	} else { # worker, Net::NNTP (Net::Cmd) uses STDERR directly
-		open STDERR, '+>&='.fileno($self->{2}) or warn "open $!";
+		open STDERR, '+>&='.fileno($self->{2});
 		STDERR->autoflush(1);
 		POSIX::setpgid(0, $$) // die "setpgid(0, $$): $!";
 		delete @$cfg{qw(-watches -lei_note_event)};
@@ -813,10 +813,9 @@ sub dispatch {
 		if (my $chdir = $self->{opt}->{C}) {
 			for my $d (@$chdir) {
 				next if $d eq ''; # same as git(1)
-				chdir $d or return fail($self, "cd $d: $!");
+				chdir $d;
 			}
-			open $self->{3}, '<', '.' or
-				return fail($self, "open . $!");
+			open($self->{3}, '<', '.');
 		}
 		$cb->($self, @argv);
 	} elsif (grep(/\A-/, $cmd, @argv)) { # --help or -h only
@@ -851,7 +850,7 @@ sub _lei_cfg ($;$) {
 		}
 		my ($cfg_dir) = ($f =~ m!(.*?/)[^/]+\z!);
 		File::Path::mkpath($cfg_dir);
-		open my $fh, '>>', $f or die "open($f): $!\n";
+		open my $fh, '>>', $f;
 		@st = stat($fh) or die "fstat($f): $!\n";
 		$cur_st = pack('dd', $st[10], $st[7]);
 		qerr($self, "# $f created") if $self->{cmd} ne 'config';
@@ -1148,10 +1147,7 @@ sub accept_dispatch { # Listener {post_accept} callback
 		return send($sock, $msg, 0);
 	} else {
 		my $i = 0;
-		for my $fd (@fds) {
-			open($self->{$i++}, '+<&=', $fd) and next;
-			send($sock, "open(+<&=$fd) (FD=$i): $!", 0);
-		}
+		open($self->{$i++}, '+<&=', $_) for @fds;
 		$i == 4 or return send($sock, 'not enough FDs='.($i-1), 0)
 	}
 	# $ENV_STR = join('', map { "\0$_=$ENV{$_}" } keys %ENV);
@@ -1236,12 +1232,11 @@ sub dump_and_clear_log {
 sub cfg2lei ($) {
 	my ($cfg) = @_;
 	my $lei = bless { env => { %{$cfg->{-env}} } }, __PACKAGE__;
-	open($lei->{0}, '<&', \*STDIN) or die "dup 0: $!";
-	open($lei->{1}, '>>&', \*STDOUT) or die "dup 1: $!";
-	open($lei->{2}, '>>&', \*STDERR) or die "dup 2: $!";
-	open($lei->{3}, '<', '/') or die "open /: $!";
-	my ($x, $y);
-	socketpair($x, $y, AF_UNIX, SOCK_SEQPACKET, 0) or die "socketpair: $!";
+	open($lei->{0}, '<&', \*STDIN);
+	open($lei->{1}, '>>&', \*STDOUT);
+	open($lei->{2}, '>>&', \*STDERR);
+	open($lei->{3}, '<', '/');
+	socketpair(my $x, my $y, AF_UNIX, SOCK_SEQPACKET, 0);
 	$lei->{sock} = $x;
 	require PublicInbox::LeiSelfSocket;
 	PublicInbox::LeiSelfSocket->new($y); # adds to event loop
@@ -1317,17 +1312,15 @@ sub lazy_start {
 	my $lk = PublicInbox::Lock->new($errors_log);
 	umask(077) // die("umask(077): $!");
 	$lk->lock_acquire;
-	socket($listener, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
+	socket($listener, AF_UNIX, SOCK_SEQPACKET, 0);
 	if ($errno == ECONNREFUSED || $errno == ENOENT) {
 		return if connect($listener, $addr); # another process won
-		if ($errno == ECONNREFUSED && -S $path) {
-			unlink($path) or die "unlink($path): $!";
-		}
+		unlink($path) if $errno == ECONNREFUSED && -S $path;
 	} else {
 		$! = $errno; # allow interpolation to stringify in die
 		die "connect($path): $!";
 	}
-	bind($listener, $addr) or die "bind($path): $!";
+	bind($listener, $addr);
 	$lk->lock_release;
 	undef $lk;
 	my @st = stat($path) or die "stat($path): $!";
@@ -1340,11 +1333,11 @@ sub lazy_start {
 	require PublicInbox::Listener;
 	require PublicInbox::PktOp;
 	(-p STDOUT) or die "E: stdout must be a pipe\n";
-	open(STDIN, '+>>', $errors_log) or die "open($errors_log): $!";
+	open(STDIN, '+>>', $errors_log);
 	STDIN->autoflush(1);
 	dump_and_clear_log();
 	POSIX::setsid() > 0 or die "setsid: $!";
-	my $pid = fork // die "fork: $!";
+	my $pid = fork;
 	return if $pid;
 	$0 = "lei-daemon $path";
 	local %PATH2CFG;
@@ -1385,8 +1378,8 @@ sub lazy_start {
 	};
 	local $SIG{PIPE} = 'IGNORE';
 	local $SIG{ALRM} = 'IGNORE';
-	open STDERR, '>&STDIN' or die "redirect stderr failed: $!";
-	open STDOUT, '>&STDIN' or die "redirect stdout failed: $!";
+	open STDERR, '>&STDIN';
+	open STDOUT, '>&STDIN';
 	# $daemon pipe to `lei' closed, main loop begins:
 	eval { PublicInbox::DS::event_loop($sig, $oldset) };
 	warn "event loop error: $@\n" if $@;
@@ -1424,8 +1417,7 @@ sub wq_done_wait { # awaitpid cb (via wq_eof)
 
 sub fchdir {
 	my ($lei) = @_;
-	my $dh = $lei->{3} // die 'BUG: lei->{3} (CWD) gone';
-	chdir($dh) || die "fchdir: $!";
+	chdir($lei->{3} // die 'BUG: lei->{3} (CWD) gone');
 }
 
 sub wq_eof { # EOF callback for main daemon

^ permalink raw reply related	[relevance 5%]

* [PATCH 00/30] autodie-ification and code simplifications
@ 2023-10-17 23:37  7% Eric Wong
  2023-10-17 23:38  5% ` [PATCH 26/30] lei: use autodie where appropriate Eric Wong
  0 siblings, 1 reply; 3+ results
From: Eric Wong @ 2023-10-17 23:37 UTC (permalink / raw)
  To: meta

Noisy code is less pleasant to work on, so use autodie more and
a few more simplifications.  There's a couple of small bugfixes
discovered along the way, too.

Eric Wong (30):
  lei_mirror: start converting to autodie
  lei_mirror: autodie most `close' calls
  lei_mirror: use autodie for most `open' calls
  git: introduce read_all function
  import: use read_all to detect short reads
  lei_mirror: use read_all
  use read_all in more places to improve safety
  xap_helper*: use autodie in more places
  xap_helper: die more easily in both implementations
  xap_helper: simplify SIGTERM exit checks
  xap_helper: autodie for getsockopt
  xap_client: autodie for pipe and socketpair
  xt/git-http-backend: remove Net::HTTP usage
  ds: introduce and use do_fork helper
  ds: get rid of SetLoopTimeout
  cindex: drop some unused functions
  syscall: common $F_SETPIPE_SZ definition
  t/lei-up: additional diagnostics for match failures
  test_common: use autodie and read_all where possible
  test_common: only hide TCP port in messages
  test_common: use $cwdfh for every run_script command
  init: drop extraneous `+'
  init: use autodie to reduce distractions
  xt/mem-imapd-tls: remove unused/broken epoll imports
  xt/mem-imapd-tls: reduce FDs for lsof use
  lei: use autodie where appropriate
  lei_auth: update comments and use v5.12
  lei_config: drop redundant open check
  convert: use read_all to simplify error checks
  idx_stack: use autodie + read_all

 lib/PublicInbox/CidxLogP.pm       |   4 +-
 lib/PublicInbox/CodeSearchIdx.pm  |   5 --
 lib/PublicInbox/DS.pm             |  36 ++++----
 lib/PublicInbox/Daemon.pm         |  16 ++--
 lib/PublicInbox/EOFpipe.pm        |   6 +-
 lib/PublicInbox/Gcf2.pm           |   7 +-
 lib/PublicInbox/Git.pm            |  19 +++--
 lib/PublicInbox/IPC.pm            |  12 +--
 lib/PublicInbox/IdxStack.pm       |  20 ++---
 lib/PublicInbox/Import.pm         |   8 +-
 lib/PublicInbox/InboxWritable.pm  |   6 +-
 lib/PublicInbox/LEI.pm            |  48 +++++------
 lib/PublicInbox/LeiALE.pm         |  11 +--
 lib/PublicInbox/LeiAuth.pm        |   7 +-
 lib/PublicInbox/LeiBlob.pm        |   6 +-
 lib/PublicInbox/LeiConfig.pm      |   4 +-
 lib/PublicInbox/LeiMailSync.pm    |   5 +-
 lib/PublicInbox/LeiMirror.pm      | 131 ++++++++++++++----------------
 lib/PublicInbox/LeiSucks.pm       |   5 +-
 lib/PublicInbox/LeiXSearch.pm     |   2 +-
 lib/PublicInbox/MultiGit.pm       |   3 +-
 lib/PublicInbox/SearchIdxShard.pm |  14 ++--
 lib/PublicInbox/Syscall.pm        |  16 ++--
 lib/PublicInbox/TestCommon.pm     |  85 +++++++++----------
 lib/PublicInbox/ViewVCS.pm        |  12 ++-
 lib/PublicInbox/WWW.pm            |   4 +-
 lib/PublicInbox/Watch.pm          |  11 +--
 lib/PublicInbox/XapClient.pm      |  11 +--
 lib/PublicInbox/XapHelper.pm      |  24 ++----
 lib/PublicInbox/XapHelperCxx.pm   |  11 +--
 lib/PublicInbox/Xapcmd.pm         |   5 +-
 lib/PublicInbox/xap_helper.h      |  60 ++++++--------
 script/public-inbox-convert       |   8 +-
 script/public-inbox-edit          |   4 +-
 script/public-inbox-init          |  30 +++----
 t/dir_idle.t                      |   2 +-
 t/ds-leak.t                       |   4 +-
 t/gcf2.t                          |   5 +-
 t/init.t                          |   7 ++
 t/lei-sigpipe.t                   |   7 +-
 t/lei-up.t                        |   4 +-
 xt/git-http-backend.t             |  30 +++----
 xt/mem-imapd-tls.t                |  21 ++---
 xt/mem-nntpd-tls.t                |   8 +-
 44 files changed, 335 insertions(+), 409 deletions(-)

^ permalink raw reply	[relevance 7%]

Results 1-3 of 3 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2023-10-17 23:37  7% [PATCH 00/30] autodie-ification and code simplifications Eric Wong
2023-10-17 23:38  5% ` [PATCH 26/30] lei: use autodie where appropriate Eric Wong
2023-11-15  9:21     [PATCH 0/4] lei convert: support idempotent v2 outputs Eric Wong
2023-11-15  9:21  5% ` [PATCH 1/4] lei: fix idempotent STDERR redirect in workers Eric Wong

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).