From 78d64664ceda59d4a6918be1842f16da38d68c7d Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 9 May 2019 04:25:02 +0500 Subject: [PATCH] Use IPGlobalProperties on all platforms for getting host name (#9530) --- .../CoreCLR/CorePsPlatform.cs | 10 -------- .../utils/PsUtils.cs | 25 ++++++------------- 2 files changed, 8 insertions(+), 27 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index da1b80783d..c569c724da 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -517,12 +517,6 @@ namespace System.Management.Automation return Unix.NativeMethods.SetDate(&tm) == 0; } - // Hostname in this context seems to be the FQDN - internal static string NonWindowsGetHostName() - { - return Unix.NativeMethods.GetFullyQualifiedName() ?? string.Empty; - } - internal static bool NonWindowsIsSameFileSystemItem(string pathOne, string pathTwo) { return Unix.NativeMethods.IsSameFileSystemItem(pathOne, pathTwo); @@ -643,10 +637,6 @@ namespace System.Management.Automation [DllImport(psLib, CharSet = CharSet.Ansi)] internal static extern uint GetCurrentThreadId(); - [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] - [return: MarshalAs(UnmanagedType.LPStr)] - internal static extern string GetFullyQualifiedName(); - // This is a struct tm from [StructLayout(LayoutKind.Sequential)] internal unsafe struct UnixTm diff --git a/src/System.Management.Automation/utils/PsUtils.cs b/src/System.Management.Automation/utils/PsUtils.cs index c46871ad1c..ced3c57913 100644 --- a/src/System.Management.Automation/utils/PsUtils.cs +++ b/src/System.Management.Automation/utils/PsUtils.cs @@ -7,6 +7,7 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Management.Automation.Language; +using System.Net.NetworkInformation; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -206,26 +207,16 @@ namespace System.Management.Automation internal static string GetHostName() { - // Note: non-windows CoreCLR does not support System.Net yet - if (Platform.IsWindows) - { - return WinGetHostName(); - } - else - { - return Platform.NonWindowsGetHostName(); - } - } - - internal static string WinGetHostName() - { - System.Net.NetworkInformation.IPGlobalProperties ipProperties = - System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties(); + IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); string hostname = ipProperties.HostName; - if (!string.IsNullOrEmpty(ipProperties.DomainName)) + string domainName = ipProperties.DomainName; + + // CoreFX on Unix calls GLibc getdomainname() + // which returns "(none)" if a domain name is not set by setdomainname() + if (!string.IsNullOrEmpty(domainName) && !domainName.Equals("(none)", StringComparison.Ordinal)) { - hostname = hostname + "." + ipProperties.DomainName; + hostname = hostname + "." + domainName; } return hostname;