From a7d47faf420cba8effe2d3cb14cb9d800befa226 Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 4 May 2019 10:14:53 +0500 Subject: [PATCH 1/2] Use IPGlobalProperties on all platforms --- .../CoreCLR/CorePsPlatform.cs | 10 ---------- .../utils/PsUtils.cs | 17 ++--------------- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index d6b4ed8f5ae..bc5770dbad5 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -522,12 +522,6 @@ internal static unsafe bool NonWindowsSetDate(DateTime dateToUse) 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); @@ -690,10 +684,6 @@ internal static class NativeMethods [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 c46871ad1c3..19144524262 100644 --- a/src/System.Management.Automation/utils/PsUtils.cs +++ b/src/System.Management.Automation/utils/PsUtils.cs @@ -7,6 +7,7 @@ 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,21 +207,7 @@ internal static string GetTemporaryDirectory() 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)) From 383119372883dcbba49f43b97a78c92f59cfcc68 Mon Sep 17 00:00:00 2001 From: Ilya Date: Sat, 4 May 2019 14:32:30 +0500 Subject: [PATCH 2/2] Fix domain name on Linux --- src/System.Management.Automation/utils/PsUtils.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/utils/PsUtils.cs b/src/System.Management.Automation/utils/PsUtils.cs index 19144524262..ced3c579135 100644 --- a/src/System.Management.Automation/utils/PsUtils.cs +++ b/src/System.Management.Automation/utils/PsUtils.cs @@ -210,9 +210,13 @@ internal static string GetHostName() 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;