From: Eric Wong <e@80x24.org>
Date: Thu, 17 Dec 2020 19:40:06 +0000 (+0000)
Subject: extsearchidx: improve missing machine-id fallback
X-Git-Tag: v1.7.0~1531
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=8f4253f567852ef56e3a484c9881d4f113e5dc89;p=public-inbox.git

extsearchidx: improve missing machine-id fallback

It's likely most GNU/Linux systems have /etc/machine-id these
days, so anything missing it is likely a *BSD, most of which
support and favor "sysctl -n kern.hostid".  We'll also support
"ghostid" since GNU utils are commonly prefixed with 'g' on
non-GNU platforms.

In any case, we'll suppress stderr from missing commands and
fall back to hard coding an $OSNAME-based identifier as a last
resort and hope the hostname is unique.
---

diff --git a/lib/PublicInbox/ExtSearchIdx.pm b/lib/PublicInbox/ExtSearchIdx.pm
index c6fb398b..b82d0546 100644
--- a/lib/PublicInbox/ExtSearchIdx.pm
+++ b/lib/PublicInbox/ExtSearchIdx.pm
@@ -530,8 +530,10 @@ sub host_ident () {
 	state $retval = hostname . '-' . do {
 		my $m; # machine-id(5) is systemd
 		if (open(my $fh, '<', '/etc/machine-id')) { $m = <$fh> }
-		# hostid(1) is in GNU coreutils, kern.hostid is FreeBSD
-		chomp($m ||= `hostid` || `sysctl -n kern.hostid`);
+		# (g)hostid(1) is in GNU coreutils, kern.hostid is most BSDs
+		chomp($m ||= `{ sysctl -n kern.hostid ||
+				hostid || ghostid; } 2>/dev/null`
+			|| "no-machine-id-or-hostid-on-$^O");
 		$m;
 	};
 }