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 00EAD1F4C6 for ; Sat, 9 Nov 2024 01:18:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1731115124; bh=ZDuEq0UW8q3BakbGBXv/PuYKTL7EoZbUUHietGaGv+0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=BWngymzo1wU5uK0dBswVmanT+JJZ//UseMyU3PIb1TAiVBCxb6oZ7/qFBS9ncg4Hl 3qNGRu/EqLj6gGPj1GblL1ucnNU9yNOHHkfgqOpOn/pgZ6n7QYMAGK7WHZDteXlH2L AEOqq8/9n3llFPU+F2kPEVVQHosL9dskwVfUTHuA= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 1/2] daemon: use autodie Date: Sat, 9 Nov 2024 01:13:40 +0000 Message-ID: <20241109011341.90730-2-e@80x24.org> In-Reply-To: <20241109011341.90730-1-e@80x24.org> References: <20241109011341.90730-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: autodie gives us more consistent error messages and reduces our code maintenance burden. Startup time is not a concern for daemons, either. --- lib/PublicInbox/Daemon.pm | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index 28458b19..fa4314b4 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -6,6 +6,7 @@ # and/or lossy connections. package PublicInbox::Daemon; use v5.12; +use autodie qw(chdir open pipe); use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev); use IO::Handle; # ->autoflush use IO::Socket; @@ -102,7 +103,7 @@ sub do_chown ($) { } sub open_log_path ($$) { # my ($fh, $path) = @_; # $_[0] is modified - open $_[0], '>>', $_[1] or die "open(>> $_[1]): $!"; + open $_[0], '>>', $_[1]; $_[0]->autoflush(1); do_chown($_[1]); $_[0]; @@ -316,8 +317,7 @@ sub daemonize () { check_absolute('--pid-file', $pid_file); check_absolute('--cert', $default_cert); check_absolute('--key', $default_key); - - chdir '/' or die "chdir failed: $!"; + chdir '/'; } if (defined($pid_file) || defined($group) || defined($user)) { eval { require Net::Server::Daemonize; 1 } // die <&STDIN' or die "redirect stdout failed: $!\n"; - open STDERR, '>&STDIN' or die "redirect stderr failed: $!\n"; + open STDIN, '+<', '/dev/null'; + open STDOUT, '>&STDIN'; + open STDERR, '>&STDIN'; POSIX::setsid(); $pid = PublicInbox::OnDestroy::fork_tmp; exit if $pid; @@ -471,7 +470,7 @@ sub inherit ($) { my $end = $fds + 2; # LISTEN_FDS_START - 1 my @rv = (); foreach my $fd (3..$end) { - open(my $s, '<&=', $fd) or warn "fdopen fd=$fd: $!"; + CORE::open(my $s, '<&=', $fd) or warn "fdopen fd=$fd: $!"; if (my $k = sockname($s)) { my $prev_was_blocking = $s->blocking(0); warn <<"" if $prev_was_blocking; @@ -541,7 +540,7 @@ sub upgrade_aborted { sub unlink_pid_file_safe_ish ($) { my ($fref) = @_; - open my $fh, '<', $$fref or return; + CORE::open my $fh, '<', $$fref or return; local $/ = "\n"; defined(my $read_pid = <$fh>) or return; chomp $read_pid; @@ -593,7 +592,7 @@ sub trim_workers { sub master_loop { local $parent_pipe; - pipe($parent_pipe, my $p1) or die "failed to create parent-pipe: $!"; + pipe $parent_pipe, my $p1; my $set_workers = $nworker; # for SIGWINCH reopen_logs(); spawn_xh;