From eb757acfc19c090d44e3443e5dfa40452940a94b Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Fri, 6 Nov 2020 07:09:33 +0000 Subject: [PATCH 1/3] Fix illumos managed build * Fix CA1823 (unused private field) in `NetworkChange` partial for `UnknownUnix`. * Use official casing `illumos` in MSBuild property names (as done for iOS). * Fix Solaris version in test with SDK's PlatformDetection. * only major version is needed. --- eng/targetframeworksuffix.props | 2 +- src/coreclr/dir.common.props | 4 ++-- src/installer/Directory.Build.props | 2 +- .../pkg/projects/netcoreapp/Directory.Build.props | 2 +- .../TestUtilities/System/PlatformDetection.Unix.cs | 5 +++-- .../tests/TestUtilities/System/PlatformDetection.cs | 4 ++-- .../System.Data.Odbc/src/System.Data.Odbc.csproj | 2 +- .../System.Net.Http/src/System.Net.Http.csproj | 2 +- .../src/System.Net.NetworkInformation.csproj | 4 ++-- .../NetworkInformation/NetworkAddressChange.OSX.cs | 12 ++++++++++++ .../NetworkInformation/NetworkAddressChange.Unix.cs | 12 ++++++++++++ .../NetworkAddressChange.Windows.cs | 12 ++++++++++++ .../Net/NetworkInformation/NetworkAddressChange.cs | 11 ----------- .../src/System.Private.CoreLib.Shared.projitems | 1 + .../src/System/Environment.SunOS.cs | 12 ++++++++++++ src/mono/Directory.Build.props | 4 ++-- src/tests/Directory.Build.props | 2 +- 17 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 src/libraries/System.Private.CoreLib/src/System/Environment.SunOS.cs diff --git a/eng/targetframeworksuffix.props b/eng/targetframeworksuffix.props index 2525ce954a3558..e4473df46fa74d 100644 --- a/eng/targetframeworksuffix.props +++ b/eng/targetframeworksuffix.props @@ -77,7 +77,7 @@ true - true + true illumos diff --git a/src/coreclr/dir.common.props b/src/coreclr/dir.common.props index ebaf9645134abd..fbaf583697f89c 100644 --- a/src/coreclr/dir.common.props +++ b/src/coreclr/dir.common.props @@ -55,12 +55,12 @@ true true true - true + true true true true - true + true $(__DistroRid) diff --git a/src/installer/Directory.Build.props b/src/installer/Directory.Build.props index 319906aef65445..0a052f6ea00447 100644 --- a/src/installer/Directory.Build.props +++ b/src/installer/Directory.Build.props @@ -278,7 +278,7 @@ - true + true true diff --git a/src/installer/pkg/projects/netcoreapp/Directory.Build.props b/src/installer/pkg/projects/netcoreapp/Directory.Build.props index 36885e8e8b63f1..dd973d79081888 100644 --- a/src/installer/pkg/projects/netcoreapp/Directory.Build.props +++ b/src/installer/pkg/projects/netcoreapp/Directory.Build.props @@ -15,7 +15,7 @@ Browser FreeBSD NetBSD - illumos + illumos Solaris $(CoreCLRTargetOS) diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs index 80f824eb2dbde4..d3922536a5d3ed 100644 --- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs +++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs @@ -164,7 +164,7 @@ private static DistroInfo GetDistroInfo() // What we want is major release as minor releases should be compatible. result.VersionId = ToVersion(RuntimeInformation.OSDescription.Split()[1].Split('.')[0]); } - else if (IsIllumos) + else if (Isillumos) { // examples: // on OmniOS @@ -195,7 +195,8 @@ private static DistroInfo GetDistroInfo() // example: // SunOS 5.11 11.3 result.Id = "Solaris"; - result.VersionId = ToVersion(RuntimeInformation.OSDescription.Split(' ')[2]); // e.g. 11.3 + // we only need the major version; 11 + result.VersionId = ToVersion(RuntimeInformation.OSDescription.Split(' ')[2].Split('.')[0]); // e.g. 11 } else if (File.Exists("/etc/os-release")) { diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs index 313e6194e4ef03..f0f6bb77814847 100644 --- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs +++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs @@ -28,7 +28,7 @@ public static partial class PlatformDetection public static bool IsNetBSD => RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD")); public static bool IsiOS => RuntimeInformation.IsOSPlatform(OSPlatform.Create("IOS")); public static bool IstvOS => RuntimeInformation.IsOSPlatform(OSPlatform.Create("TVOS")); - public static bool IsIllumos => RuntimeInformation.IsOSPlatform(OSPlatform.Create("ILLUMOS")); + public static bool Isillumos => RuntimeInformation.IsOSPlatform(OSPlatform.Create("ILLUMOS")); public static bool IsSolaris => RuntimeInformation.IsOSPlatform(OSPlatform.Create("SOLARIS")); public static bool IsBrowser => RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")); public static bool IsNotBrowser => !IsBrowser; @@ -124,7 +124,7 @@ public static bool IsNonZeroLowerBoundArraySupported // Windows - Schannel supports alpn from win8.1/2012 R2 and higher. // Linux - OpenSsl supports alpn from openssl 1.0.2 and higher. // OSX - SecureTransport doesn't expose alpn APIs. TODO https://github.com/dotnet/runtime/issues/27727 - public static bool IsOpenSslSupported => IsLinux || IsFreeBSD || IsIllumos || IsSolaris; + public static bool IsOpenSslSupported => IsLinux || IsFreeBSD || Isillumos || IsSolaris; public static bool SupportsAlpn => (IsWindows && !IsWindows7) || (IsOpenSslSupported && diff --git a/src/libraries/System.Data.Odbc/src/System.Data.Odbc.csproj b/src/libraries/System.Data.Odbc/src/System.Data.Odbc.csproj index 87ad725689c716..20d918a90a1254 100644 --- a/src/libraries/System.Data.Odbc/src/System.Data.Odbc.csproj +++ b/src/libraries/System.Data.Odbc/src/System.Data.Odbc.csproj @@ -114,7 +114,7 @@ - + diff --git a/src/libraries/System.Net.Http/src/System.Net.Http.csproj b/src/libraries/System.Net.Http/src/System.Net.Http.csproj index febae717a7fe92..70c8da78a16dee 100644 --- a/src/libraries/System.Net.Http/src/System.Net.Http.csproj +++ b/src/libraries/System.Net.Http/src/System.Net.Http.csproj @@ -3,7 +3,7 @@ win true $(DefineConstants);HTTP_DLL - $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-Browser + $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-Solaris enable diff --git a/src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj b/src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj index ae6fb33863f0cc..be99640d82855a 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj +++ b/src/libraries/System.Net.NetworkInformation/src/System.Net.NetworkInformation.csproj @@ -1,7 +1,7 @@ true - $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-FreeBSD + $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Linux;$(NetCoreAppCurrent)-Browser;$(NetCoreAppCurrent)-OSX;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS;$(NetCoreAppCurrent)-FreeBSD;$(NetCoreAppCurrent)-illumos;$(NetCoreAppCurrent)-Solaris enable @@ -181,7 +181,7 @@ - + diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs index edf198d187f97e..2b0f852e68db42 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs @@ -32,6 +32,18 @@ public partial class NetworkChange // The listener thread's CFRunLoop. private static CFRunLoopRef s_runLoop; + // The list of current address-changed subscribers. + private static readonly Dictionary s_addressChangedSubscribers = + new Dictionary(); + + // The list of current availability-changed subscribers. + private static readonly Dictionary s_availabilityChangedSubscribers = + new Dictionary(); + + private static readonly ContextCallback s_runHandlerAvailable = new ContextCallback(RunAvailabilityHandlerAvailable!); + private static readonly ContextCallback s_runHandlerNotAvailable = new ContextCallback(RunAvailabilityHandlerNotAvailable!); + private static readonly ContextCallback s_runAddressChangedHandler = new ContextCallback(RunAddressChangedHandler!); + // Use an event to try to prevent StartRaisingEvents from returning before the // RunLoop actually begins. This will mitigate a race condition where the watcher // thread hasn't completed initialization and stop is called before the RunLoop has even started. diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs index ac4ccb1b36d31b..6dbc129cf9b72b 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs @@ -16,6 +16,18 @@ public partial class NetworkChange private static readonly object s_gate = new object(); private static readonly Interop.Sys.NetworkChangeEvent s_networkChangeCallback = ProcessEvent; + // The list of current address-changed subscribers. + private static readonly Dictionary s_addressChangedSubscribers = + new Dictionary(); + + // The list of current availability-changed subscribers. + private static readonly Dictionary s_availabilityChangedSubscribers = + new Dictionary(); + + private static readonly ContextCallback s_runHandlerAvailable = new ContextCallback(RunAvailabilityHandlerAvailable!); + private static readonly ContextCallback s_runHandlerNotAvailable = new ContextCallback(RunAvailabilityHandlerNotAvailable!); + private static readonly ContextCallback s_runAddressChangedHandler = new ContextCallback(RunAddressChangedHandler!); + // The "leniency" window for NetworkAvailabilityChanged socket events. // All socket events received within this duration will be coalesced into a // single event. Generally, many route changed events are fired in succession, diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Windows.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Windows.cs index 745b8492f87e15..12af0c5db8f9a3 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Windows.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Windows.cs @@ -13,6 +13,18 @@ public partial class NetworkChange { private static readonly object s_globalLock = new object(); + // The list of current address-changed subscribers. + private static readonly Dictionary s_addressChangedSubscribers = + new Dictionary(); + + // The list of current availability-changed subscribers. + private static readonly Dictionary s_availabilityChangedSubscribers = + new Dictionary(); + + private static readonly ContextCallback s_runHandlerAvailable = new ContextCallback(RunAvailabilityHandlerAvailable!); + private static readonly ContextCallback s_runHandlerNotAvailable = new ContextCallback(RunAvailabilityHandlerNotAvailable!); + private static readonly ContextCallback s_runAddressChangedHandler = new ContextCallback(RunAddressChangedHandler!); + public static event NetworkAvailabilityChangedEventHandler? NetworkAvailabilityChanged { add diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.cs index 0683434b9dffd1..87345329d3be0a 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.cs @@ -8,19 +8,8 @@ namespace System.Net.NetworkInformation { public partial class NetworkChange { - // The list of current address-changed subscribers. - private static readonly Dictionary s_addressChangedSubscribers = - new Dictionary(); - - // The list of current availability-changed subscribers. - private static readonly Dictionary s_availabilityChangedSubscribers = - new Dictionary(); - private static readonly NetworkAvailabilityEventArgs s_availableEventArgs = new NetworkAvailabilityEventArgs(isAvailable: true); private static readonly NetworkAvailabilityEventArgs s_notAvailableEventArgs = new NetworkAvailabilityEventArgs(isAvailable: false); - private static readonly ContextCallback s_runHandlerAvailable = new ContextCallback(RunAvailabilityHandlerAvailable!); - private static readonly ContextCallback s_runHandlerNotAvailable = new ContextCallback(RunAvailabilityHandlerNotAvailable!); - private static readonly ContextCallback s_runAddressChangedHandler = new ContextCallback(RunAddressChangedHandler!); // Introduced for supporting design-time loading of System.Windows.dll [Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.", true)] diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 159721414fde24..0800a02c955513 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -1858,6 +1858,7 @@ + diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.SunOS.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.SunOS.cs new file mode 100644 index 00000000000000..3c82c70130e30d --- /dev/null +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.SunOS.cs @@ -0,0 +1,12 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; + +namespace System +{ + public static partial class Environment + { + public static long WorkingSet => throw new PlatformNotSupportedException(); + } +} diff --git a/src/mono/Directory.Build.props b/src/mono/Directory.Build.props index 738bf3c5566cc4..a15d680c9f171d 100644 --- a/src/mono/Directory.Build.props +++ b/src/mono/Directory.Build.props @@ -34,7 +34,7 @@ true - true + true true true true @@ -46,7 +46,7 @@ true true true - true + true diff --git a/src/tests/Directory.Build.props b/src/tests/Directory.Build.props index ed2e457a2a3527..7b834896308bcd 100644 --- a/src/tests/Directory.Build.props +++ b/src/tests/Directory.Build.props @@ -152,7 +152,7 @@ true - true + true ubuntu.14.04-$(TargetArchitecture) From 02d0b7e058ca59f5d4ed2762906aa989cf078ef0 Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Wed, 4 Nov 2020 13:15:00 +0000 Subject: [PATCH 2/3] Implement Enviornment.WorkingSet for SunOS Difference between Linux and SunOS procfs is that files in latter contain binary data, so we need `read(2)` and cast into corresponding struct. Redeclaring system structs in managed code is not ideal, as they do change across the major versions of OS, which inevitably requires recompilation of binaries and replicating them in C# as is means additional/unnecessary maintenance of code. --- ...rop.ProcFsStat.TryReadProcessStatusInfo.cs | 37 +++++++++++++++++++ .../Native/Unix/System.Native/pal_io.c | 37 +++++++++++++++++++ .../Native/Unix/System.Native/pal_io.h | 17 +++++++++ .../System.Private.CoreLib.Shared.projitems | 4 +- .../src/System/Environment.SunOS.cs | 5 ++- 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFsStat.TryReadProcessStatusInfo.cs diff --git a/src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFsStat.TryReadProcessStatusInfo.cs b/src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFsStat.TryReadProcessStatusInfo.cs new file mode 100644 index 00000000000000..8bf03ad566a10d --- /dev/null +++ b/src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFsStat.TryReadProcessStatusInfo.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class procfs + { + /// + /// Attempts to get status info for the specified process ID. + /// + /// PID of the process to read status info for. + /// The pointer to processStatus instance. + /// + /// true if the process status was read; otherwise, false. + /// + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadProcessStatusInfo", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern unsafe bool TryReadProcessStatusInfo(int pid, ProcessStatusInfo* processStatus); + + internal struct ProcessStatusInfo + { + internal ulong ResidentSetSize; + // add more fields when needed. + } + + internal static unsafe bool TryReadProcessStatusInfo(int pid, out ProcessStatusInfo statusInfo) + { + fixed (ProcessStatusInfo* pStatusInfo = &statusInfo) + { + return TryReadProcessStatusInfo(pid, pStatusInfo); + } + } + } +} diff --git a/src/libraries/Native/Unix/System.Native/pal_io.c b/src/libraries/Native/Unix/System.Native/pal_io.c index ea8375c1b8df14..efd86aa38a643e 100644 --- a/src/libraries/Native/Unix/System.Native/pal_io.c +++ b/src/libraries/Native/Unix/System.Native/pal_io.c @@ -40,6 +40,15 @@ // Somehow, AIX mangles the definition for this behind a C++ def // Redeclare it here extern int getpeereid(int, uid_t *__restrict__, gid_t *__restrict__); +#elif defined(__sun) +#ifndef _KERNEL +#define _KERNEL +#define UNDEF_KERNEL +#endif +#include +#ifdef UNDEF_KERNEL +#undef _KERNEL +#endif #endif #if HAVE_STAT64 @@ -1288,3 +1297,31 @@ int32_t SystemNative_LChflagsCanSetHiddenFlag(void) return false; #endif } + +#ifdef __sun + +int32_t SystemNative_ReadProcessStatusInfo(pid_t pid, ProcessStatus* processStatus) +{ + char statusFilename[64]; + snprintf(statusFilename, sizeof(statusFilename), "/proc/%d/psinfo", pid); + + intptr_t fd; + while ((fd = open(statusFilename, O_RDONLY)) < 0 && errno == EINTR); + if (fd < 0) + { + return 0; + } + + psinfo_t status; + int result = Common_Read(fd, &status, sizeof(psinfo_t)); + while (close(fd) < 0 && errno == EINTR); + if (result >= 0) + { + processStatus->ResidentSetSize = status.pr_rssize * 100; // pr_rssize is in Kbytes + return 1; + } + + return 0; +} + +#endif // __sun diff --git a/src/libraries/Native/Unix/System.Native/pal_io.h b/src/libraries/Native/Unix/System.Native/pal_io.h index dbc2be8721a945..69b210765e20c5 100644 --- a/src/libraries/Native/Unix/System.Native/pal_io.h +++ b/src/libraries/Native/Unix/System.Native/pal_io.h @@ -35,6 +35,14 @@ typedef struct uint32_t UserFlags; // user defined flags } FileStatus; +#ifdef __sun +typedef struct +{ + size_t ResidentSetSize; + // add more fields when needed. +} ProcessStatus; +#endif + /* Provide consistent access to nanosecond fields, if they exist. */ /* Seconds are always available through st_atime, st_mtime, st_ctime. */ @@ -710,3 +718,12 @@ PALEXPORT int32_t SystemNative_LChflags(const char* path, uint32_t flags); * Returns true (non-zero) if supported, false (zero) if not. */ PALEXPORT int32_t SystemNative_LChflagsCanSetHiddenFlag(void); + +#ifdef __sun +/** + * Reads the psinfo_t struct and converts into ProcessStatus. + * + * Returns 1 if the process status was read; otherwise, 0. + */ +PALEXPORT int32_t SystemNative_ReadProcessStatusInfo(pid_t pid, ProcessStatus* processStatus); +#endif diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 0800a02c955513..9d2e1cd6180c71 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -1853,12 +1853,14 @@ Link="Common\System\IO\StringParser.cs" /> + - + diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.SunOS.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.SunOS.cs index 3c82c70130e30d..fb7ff75cb2e6c6 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.SunOS.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.SunOS.cs @@ -1,12 +1,13 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; +using System.Runtime.InteropServices; namespace System { public static partial class Environment { - public static long WorkingSet => throw new PlatformNotSupportedException(); + public static long WorkingSet => + (long)(Interop.procfs.TryReadProcessStatusInfo(ProcessId, out Interop.procfs.ProcessStatusInfo status) ? status.ResidentSetSize : 0); } } From 22a67db3d827e26e092a2e7fc0ed3cb94588ca08 Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Mon, 9 Nov 2020 17:55:59 +0000 Subject: [PATCH 3/3] Address CR feedback --- .../Interop.ProcFsStat.TryReadProcessStatusInfo.cs | 5 ++--- src/libraries/Native/Unix/System.Native/pal_io.c | 4 ++-- .../NetworkInformation/NetworkAddressChange.OSX.cs | 12 ------------ .../NetworkInformation/NetworkAddressChange.Unix.cs | 12 ------------ .../NetworkAddressChange.UnknownUnix.cs | 12 ++++++++++++ .../NetworkAddressChange.Windows.cs | 12 ------------ .../Net/NetworkInformation/NetworkAddressChange.cs | 11 +++++++++++ 7 files changed, 27 insertions(+), 41 deletions(-) diff --git a/src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFsStat.TryReadProcessStatusInfo.cs b/src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFsStat.TryReadProcessStatusInfo.cs index 8bf03ad566a10d..032186eb6a7e89 100644 --- a/src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFsStat.TryReadProcessStatusInfo.cs +++ b/src/libraries/Common/src/Interop/SunOS/procfs/Interop.ProcFsStat.TryReadProcessStatusInfo.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable enable using System.Runtime.InteropServices; internal static partial class Interop @@ -17,17 +16,17 @@ internal static partial class procfs /// true if the process status was read; otherwise, false. /// [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReadProcessStatusInfo", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] private static extern unsafe bool TryReadProcessStatusInfo(int pid, ProcessStatusInfo* processStatus); internal struct ProcessStatusInfo { - internal ulong ResidentSetSize; + internal nuint ResidentSetSize; // add more fields when needed. } internal static unsafe bool TryReadProcessStatusInfo(int pid, out ProcessStatusInfo statusInfo) { + statusInfo = default; fixed (ProcessStatusInfo* pStatusInfo = &statusInfo) { return TryReadProcessStatusInfo(pid, pStatusInfo); diff --git a/src/libraries/Native/Unix/System.Native/pal_io.c b/src/libraries/Native/Unix/System.Native/pal_io.c index efd86aa38a643e..6cf0e1d7695f7d 100644 --- a/src/libraries/Native/Unix/System.Native/pal_io.c +++ b/src/libraries/Native/Unix/System.Native/pal_io.c @@ -1314,10 +1314,10 @@ int32_t SystemNative_ReadProcessStatusInfo(pid_t pid, ProcessStatus* processStat psinfo_t status; int result = Common_Read(fd, &status, sizeof(psinfo_t)); - while (close(fd) < 0 && errno == EINTR); + close(fd); if (result >= 0) { - processStatus->ResidentSetSize = status.pr_rssize * 100; // pr_rssize is in Kbytes + processStatus->ResidentSetSize = status.pr_rssize * 1024; // pr_rssize is in Kbytes return 1; } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs index 2b0f852e68db42..edf198d187f97e 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.OSX.cs @@ -32,18 +32,6 @@ public partial class NetworkChange // The listener thread's CFRunLoop. private static CFRunLoopRef s_runLoop; - // The list of current address-changed subscribers. - private static readonly Dictionary s_addressChangedSubscribers = - new Dictionary(); - - // The list of current availability-changed subscribers. - private static readonly Dictionary s_availabilityChangedSubscribers = - new Dictionary(); - - private static readonly ContextCallback s_runHandlerAvailable = new ContextCallback(RunAvailabilityHandlerAvailable!); - private static readonly ContextCallback s_runHandlerNotAvailable = new ContextCallback(RunAvailabilityHandlerNotAvailable!); - private static readonly ContextCallback s_runAddressChangedHandler = new ContextCallback(RunAddressChangedHandler!); - // Use an event to try to prevent StartRaisingEvents from returning before the // RunLoop actually begins. This will mitigate a race condition where the watcher // thread hasn't completed initialization and stop is called before the RunLoop has even started. diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs index 6dbc129cf9b72b..ac4ccb1b36d31b 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Unix.cs @@ -16,18 +16,6 @@ public partial class NetworkChange private static readonly object s_gate = new object(); private static readonly Interop.Sys.NetworkChangeEvent s_networkChangeCallback = ProcessEvent; - // The list of current address-changed subscribers. - private static readonly Dictionary s_addressChangedSubscribers = - new Dictionary(); - - // The list of current availability-changed subscribers. - private static readonly Dictionary s_availabilityChangedSubscribers = - new Dictionary(); - - private static readonly ContextCallback s_runHandlerAvailable = new ContextCallback(RunAvailabilityHandlerAvailable!); - private static readonly ContextCallback s_runHandlerNotAvailable = new ContextCallback(RunAvailabilityHandlerNotAvailable!); - private static readonly ContextCallback s_runAddressChangedHandler = new ContextCallback(RunAddressChangedHandler!); - // The "leniency" window for NetworkAvailabilityChanged socket events. // All socket events received within this duration will be coalesced into a // single event. Generally, many route changed events are fired in succession, diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.UnknownUnix.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.UnknownUnix.cs index d22aa8b3a4af55..cac7dc0a660107 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.UnknownUnix.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.UnknownUnix.cs @@ -1,10 +1,22 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Collections.Generic; +using System.Threading; + namespace System.Net.NetworkInformation { public partial class NetworkChange { + static NetworkChange() + { + // fake usage of static readonly fields to avoid getting CA1823 warning when we are compiling this partial. + var addressChangedSubscribers = new Dictionary(s_addressChangedSubscribers); + var availabilityChangedSubscribers = new Dictionary(s_availabilityChangedSubscribers); + NetworkAvailabilityEventArgs args = addressChangedSubscribers.Count > 0 ? s_availableEventArgs : s_notAvailableEventArgs; + ContextCallback callbackContext = s_runAddressChangedHandler != null ? s_runHandlerAvailable : s_runHandlerNotAvailable; + } + public static event NetworkAddressChangedEventHandler? NetworkAddressChanged { add { throw new PlatformNotSupportedException(); } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Windows.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Windows.cs index 12af0c5db8f9a3..745b8492f87e15 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Windows.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Windows.cs @@ -13,18 +13,6 @@ public partial class NetworkChange { private static readonly object s_globalLock = new object(); - // The list of current address-changed subscribers. - private static readonly Dictionary s_addressChangedSubscribers = - new Dictionary(); - - // The list of current availability-changed subscribers. - private static readonly Dictionary s_availabilityChangedSubscribers = - new Dictionary(); - - private static readonly ContextCallback s_runHandlerAvailable = new ContextCallback(RunAvailabilityHandlerAvailable!); - private static readonly ContextCallback s_runHandlerNotAvailable = new ContextCallback(RunAvailabilityHandlerNotAvailable!); - private static readonly ContextCallback s_runAddressChangedHandler = new ContextCallback(RunAddressChangedHandler!); - public static event NetworkAvailabilityChangedEventHandler? NetworkAvailabilityChanged { add diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.cs index 87345329d3be0a..0683434b9dffd1 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.cs @@ -8,8 +8,19 @@ namespace System.Net.NetworkInformation { public partial class NetworkChange { + // The list of current address-changed subscribers. + private static readonly Dictionary s_addressChangedSubscribers = + new Dictionary(); + + // The list of current availability-changed subscribers. + private static readonly Dictionary s_availabilityChangedSubscribers = + new Dictionary(); + private static readonly NetworkAvailabilityEventArgs s_availableEventArgs = new NetworkAvailabilityEventArgs(isAvailable: true); private static readonly NetworkAvailabilityEventArgs s_notAvailableEventArgs = new NetworkAvailabilityEventArgs(isAvailable: false); + private static readonly ContextCallback s_runHandlerAvailable = new ContextCallback(RunAvailabilityHandlerAvailable!); + private static readonly ContextCallback s_runHandlerNotAvailable = new ContextCallback(RunAvailabilityHandlerNotAvailable!); + private static readonly ContextCallback s_runAddressChangedHandler = new ContextCallback(RunAddressChangedHandler!); // Introduced for supporting design-time loading of System.Windows.dll [Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.", true)]