From: Eric Wong Date: Mon, 16 Jun 2025 20:05:13 +0000 (+0000) Subject: daemon: reopen log files atomically X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=62b462a9845cacd3e7f45386cf12502d6456e01c;p=thirdparty%2Fpublic-inbox.git daemon: reopen log files atomically Ensuring there is no window for a Perl IO (file handle) to be invalid is useful in case some code ever gets used in a multithreaded environment. The FD stability would also be less confusing for users using `lsof(8)' or similar to track FDs. --- diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index c5af83677..c52ac0a11 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -110,7 +110,12 @@ sub do_chown ($) { } sub open_log_path ($$) { # my ($fh, $path) = @_; # $_[0] is modified - open $_[0], '>>', $_[1]; + if (defined $_[0]) { + open my $tmp_fh, '>>', $_[1]; + POSIX::dup2(fileno($tmp_fh), fileno($_[0])) or die "dup2: $!"; + } else { + open $_[0], '>>', $_[1]; + } $_[0]->autoflush(1); do_chown($_[1]); $_[0];