]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
xap_helper: die more easily in both implementations
authorEric Wong <e@80x24.org>
Tue, 17 Oct 2023 23:37:54 +0000 (23:37 +0000)
committerEric Wong <e@80x24.org>
Wed, 18 Oct 2023 20:50:26 +0000 (20:50 +0000)
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
lib/PublicInbox/xap_helper.h

index ca993ca86756f8b6d38b51a3c26a0dc7b8ae5ae2..fe8d20f4e2693cf12e37d904eb5bfb6d83e929f9 100644 (file)
@@ -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";
index 3fa615a5fa074de344c965eb9913c5f4c472e377..c68202c3029017c6ac3c25da18aa0f905bd20080 100644 (file)
@@ -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;
 }