unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/2] daemon: use autodie
Date: Sat,  9 Nov 2024 01:13:40 +0000	[thread overview]
Message-ID: <20241109011341.90730-2-e@80x24.org> (raw)
In-Reply-To: <20241109011341.90730-1-e@80x24.org>

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 <<EOF;
@@ -345,10 +345,9 @@ EOF
 	if ($daemonize) {
 		my $pid = PublicInbox::OnDestroy::fork_tmp;
 		exit if $pid;
-		open(STDIN, '+<', '/dev/null') or
-					die "redirect stdin failed: $!\n";
-		open STDOUT, '>&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;

  reply	other threads:[~2024-11-09  1:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-09  1:13 [PATCH 0/2] daemon: improve error reporting Eric Wong
2024-11-09  1:13 ` Eric Wong [this message]
2024-11-09  1:13 ` [PATCH 2/2] daemon: warn on setsockopt failures Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241109011341.90730-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).