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,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 85D201F55F for ; Thu, 14 Sep 2023 21:22:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1694726571; bh=wUQSgC6TOjnBU7dEM7yN06LN/oUI70VGgqCx/dh2XjI=; h=From:To:Subject:Date:From; b=SmbP6cicng1uYdoUYQUcGO6GSN4/MTHG/x6Y9G7IUJuinJhQ6F/B8n3J4wfFpLwjW 6QZ2C4Mes9Fz1e3KQTDMESBycFoGblFG8jFBEPBW74zPpDD2lMsL91jVamM/rvJXHh gb0Lm/W01tPA+Km6fJAw7HWh5IjqHT5ip0yiQZAc= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH] xap_helper: detect non-socket STDIN Date: Thu, 14 Sep 2023 21:22:40 +0000 Message-ID: <20230914212240.19307-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 | 4 ++++ lib/PublicInbox/xap_helper.h | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/lib/PublicInbox/XapHelper.pm b/lib/PublicInbox/XapHelper.pm index 0e79e5e2..e8eeb2dc 100644 --- a/lib/PublicInbox/XapHelper.pm +++ b/lib/PublicInbox/XapHelper.pm @@ -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(); diff --git a/lib/PublicInbox/xap_helper.h b/lib/PublicInbox/xap_helper.h index a2fea266..2322d062 100644 --- a/lib/PublicInbox/xap_helper.h +++ b/lib/PublicInbox/xap_helper.h @@ -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();