[PATCH v4] send-email: try to get fqdn by running hostname -f on Linux and macOS

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



`hostname` is a popular command available on both Linux and macOS. As
per the man-page[1], `hostname -f` command returns the fully qualified
domain name (FQDN) of the system. The current Net::Domain perl module
being used in the script for the same has been quite unrealiable in many
cases. Thankfully, we now have a better check for valid_fqdn, which does
reject the invalid FQDNs given by this module properly, but at the same
time, it will result in a fallback to 'localhost.localdomain' being
used. `hostname -f` has been quite reliable (probably even more reliable
than the Net::Domain module) and before falling back to
'localhost.localdomain', we should try to use it.

In this patch we shall be using `hostname --fqdn` command on Linux
instead of `hostname -f`. This is because `hostname -f` could output
something else in case a Linux distro uses some other implementation of
`hostname`. On the other hand, `hostname --fqdn` is not valid on macOS,
so we shall be using `hostname -f` there.

Interestingly, the `hostname` command is actually used by perl modules
like Net::Domain[2] and Sys::Hostname[3] to get the hostname. So, lets
give `hostname -f` a chance as well!

[1]: https://man7.org/linux/man-pages/man1/hostname.1.html
[2]: https://github.com/Perl/perl5/blob/blead/cpan/libnet/lib/Net/Domain.pm#L88
[3]: https://github.com/Perl/perl5/blob/blead/ext/Sys-Hostname/Hostname.pm#L93

Signed-off-by: Aditya Garg <gargaditya08@xxxxxxxx>
---
v2: Avoid chomping $domain and assigning it to $maildomain if the command fails.
v3: Use `hostname -f` instead of `hostname --fqdn` since -f is supported everywhere.
v4: Use `hostname --fqdn` on Linux and `hostname -f` on macOS.

 git-send-email.perl | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/git-send-email.perl b/git-send-email.perl
index 55b7e00d29..bdbc7f8149 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -1393,8 +1393,24 @@ sub maildomain_mta {
 	return $maildomain;
 }
 
+sub maildomain_hostname_command {
+	my $maildomain;
+
+	if ($^O eq 'linux' || $^O eq 'darwin') {
+		my $domain = ($^O eq 'darwin') ?
+			`(hostname -f) 2>/dev/null` :
+			`(hostname --fqdn) 2>/dev/null`;
+		if (!$?) {
+			chomp($domain);
+			$maildomain = $domain if valid_fqdn($domain);
+		}
+	}
+	return $maildomain;
+}
+
 sub maildomain {
-	return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
+	return maildomain_net() || maildomain_mta() ||
+		maildomain_hostname_command || 'localhost.localdomain';
 }
 
 sub smtp_host_string {
-- 
2.49.0





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux