From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 8CF191F68D for ; Tue, 17 Oct 2023 23:38:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1697585897; bh=Cng2XwIfRBuJAGIv5/wL21Z+HqfJwTVhMMDNlSIh47c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=v2fNbBB+ep5dSviKxlsODhD0zzYc0xLEgcFwJt2MiJPSFIar+wweEzjEQeH6IgJoy Cba/nO+6U1EUQVmFNRn4N5LCPUAaq1A1vuXCaBVp0k77Gt0WcGJkUvt+6FktAE45ES EcFGBIDEx+A6V6obFfcEux+gSd1jeQOAHU6BLqvs= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 09/30] xap_helper: die more easily in both implementations Date: Tue, 17 Oct 2023 23:37:54 +0000 Message-ID: <20231017233815.1637932-10-e@80x24.org> In-Reply-To: <20231017233815.1637932-1-e@80x24.org> References: <20231017233815.1637932-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We don't need to tolerate bad requests since it's only handling requests from the parent process. So simplify error management and just die||exit if we get a bad request. --- lib/PublicInbox/XapHelper.pm | 11 +++-------- lib/PublicInbox/xap_helper.h | 32 ++++++++++---------------------- 2 files changed, 13 insertions(+), 30 deletions(-) diff --git a/lib/PublicInbox/XapHelper.pm b/lib/PublicInbox/XapHelper.pm index ca993ca8..fe8d20f4 100644 --- a/lib/PublicInbox/XapHelper.pm +++ b/lib/PublicInbox/XapHelper.pm @@ -13,6 +13,7 @@ use PublicInbox::IPC; use PublicInbox::Git qw(read_all); use Socket qw(SOL_SOCKET SO_TYPE SOCK_SEQPACKET AF_UNIX); use PublicInbox::DS qw(awaitpid); +use autodie qw(open); use POSIX qw(:signal_h); use Fcntl qw(LOCK_UN LOCK_EX); my $X = \%PublicInbox::Search::X; @@ -122,7 +123,7 @@ sub cmd_dump_roots { $qry_str // return warn('usage: dump_roots [OPTIONS] ROOT2ID_FILE QRY_STR'); $req->{A} or return warn('dump_roots requires -A PREFIX'); - open my $fh, '<', $root2id_file or die "open($root2id_file): $!"; + open my $fh, '<', $root2id_file; my $root2id; # record format: $OIDHEX "\0" uint32_t my @x = split(/\0/, read_all($fh)); while (@x) { @@ -184,13 +185,7 @@ sub recv_loop { PublicInbox::DS::block_signals(); my $req = bless {}, __PACKAGE__; my $i = 0; - for my $fd (@fds) { - open($req->{$i++}, '+<&=', $fd) and next; - warn("open(+<&=$fd) (FD=$i): $!"); - undef $req; - last; - } - $req or next; + open($req->{$i++}, '+<&=', $_) for @fds; local $stderr = $req->{1} // \*STDERR; if (chop($rbuf) ne "\0") { warn "not NUL-terminated"; diff --git a/lib/PublicInbox/xap_helper.h b/lib/PublicInbox/xap_helper.h index 3fa615a5..c68202c3 100644 --- a/lib/PublicInbox/xap_helper.h +++ b/lib/PublicInbox/xap_helper.h @@ -668,40 +668,28 @@ static bool recv_req(struct req *req, char *rbuf, size_t *len) size_t len = cmsg.hdr.cmsg_len; int *fdp = (int *)CMSG_DATA(&cmsg.hdr); size_t i; - bool fd_ok = true; for (i = 0; CMSG_LEN((i + 1) * sizeof(int)) <= len; i++) { int fd = *fdp++; const char *mode = NULL; - int fl = fd_ok ? fcntl(fd, F_GETFL) : 0; - if (fl == 0) { - continue; // hit previous error - } else if (fl == -1) { - warnx("invalid fd=%d", fd); - fd_ok = false; + int fl = fcntl(fd, F_GETFL); + if (fl == -1) { + errx(EXIT_FAILURE, "invalid fd=%d", fd); } else if (fl & O_WRONLY) { mode = "w"; } else if (fl & O_RDWR) { mode = "r+"; if (i == 0) req->has_input = true; } else { - warnx("invalid mode from F_GETFL: 0x%x", fl); - fd_ok = false; - } - if (!fd_ok) { - xclose(fd); - } else { - req->fp[i] = fdopen(fd, mode); - if (!req->fp[i]) { - warn("fdopen(fd=%d)", fd); - fd_ok = false; - } + errx(EXIT_FAILURE, + "invalid mode from F_GETFL: 0x%x", fl); } + req->fp[i] = fdopen(fd, mode); + if (!req->fp[i]) + err(EXIT_FAILURE, "fdopen(fd=%d)", fd); } - for (i = 0; !fd_ok && i < MY_ARRAY_SIZE(req->fp); i++) - if (req->fp[i]) fclose(req->fp[i]); - return fd_ok; + return true; } - warnx("no FD received in %zd-byte request", r); + errx(EXIT_FAILURE, "no FD received in %zd-byte request", r); return false; }