]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
xap_helper: detect non-socket STDIN
authorEric Wong <e@80x24.org>
Thu, 14 Sep 2023 21:22:40 +0000 (21:22 +0000)
committerEric Wong <e@80x24.org>
Fri, 15 Sep 2023 01:24:21 +0000 (01:24 +0000)
We don't want to get into a worker respawn loop if somebody
just decides to start the executable from a terminal.

lib/PublicInbox/XapHelper.pm
lib/PublicInbox/xap_helper.h

index 0e79e5e21371c297cea629d40b468b9dbc6e8979..e8eeb2dceb2c46804739835dd0e659e54fc991fa 100644 (file)
@@ -10,6 +10,7 @@ $GLP->configure(qw(require_order bundling no_ignore_case no_auto_abbrev));
 use PublicInbox::Search qw(xap_terms);
 use PublicInbox::CodeSearch;
 use PublicInbox::IPC;
+use Socket qw(SOL_SOCKET SO_TYPE SOCK_SEQPACKET AF_UNIX);
 use PublicInbox::DS qw(awaitpid);
 use POSIX qw(:signal_h);
 use Fcntl qw(LOCK_UN LOCK_EX);
@@ -254,6 +255,9 @@ sub xh_alive { $alive || scalar(keys %WORKERS) }
 
 sub start (@) {
        my (@argv) = @_;
+       my $c = getsockopt(STDIN, SOL_SOCKET, SO_TYPE) or die "getsockopt: $!";
+       unpack('i', $c) == SOCK_SEQPACKET or die 'stdin is not SOCK_SEQPACKET';
+
        local (%SRCH, %WORKERS);
        local $alive = 1;
        PublicInbox::Search::load_xapian();
index a2fea26620c85360266946bf937bd8905a01d4d6..2322d0624522a0bcb1666cf36b9391c6fde5d2ea 100644 (file)
@@ -1108,6 +1108,12 @@ static size_t living_workers(void)
 int main(int argc, char *argv[])
 {
        int c;
+       socklen_t slen = (socklen_t)sizeof(c);
+
+       if (getsockopt(sock_fd, SOL_SOCKET, SO_TYPE, &c, &slen))
+               err(EXIT_FAILURE, "getsockopt");
+       if (c != SOCK_SEQPACKET)
+               errx(EXIT_FAILURE, "stdin is not SOCK_SEQPACKET");
 
        mail_nrp_init();
        code_nrp_init();