From e6d3cbcce04bba15c1cd55b74a099d752fc8c793 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 12 Jul 2016 15:27:11 -0700 Subject: [PATCH] Implement Get-Process -IncludeUserName for Linux So much plumbing. The correct way to identify the owner of a process on Linux is to get the owner of the /proc/ file. However, .NET Core has no API to get this information, thus it had to be plumbed through to the native library. --- .../commands/management/Process.cs | 4 ++++ .../CoreCLR/CorePsPlatform.cs | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index 073c1e5fe5..13aabd84d7 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -866,6 +866,9 @@ namespace Microsoft.PowerShell.Commands private static string RetrieveProcessUserName(Process process, Cmdlet cmdlet) { string userName = null; +#if LINUX + userName = Platform.NonWindowsGetFileOwner("/proc/" + process.Id); +#else IntPtr tokenUserInfo = IntPtr.Zero; IntPtr processTokenHandler = IntPtr.Zero; @@ -955,6 +958,7 @@ namespace Microsoft.PowerShell.Commands } } +#endif return userName; } diff --git a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs index 063ba49dda..fc735c4700 100644 --- a/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs +++ b/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs @@ -354,6 +354,18 @@ namespace System.Management.Automation } } + internal static string NonWindowsGetFileOwner(string path) + { + if (!IsWindows) + { + return LinuxPlatform.GetFileOwner(path); + } + else + { + throw new PlatformNotSupportedException(); + } + } + #if CORECLR internal static string NonWindowsGetFolderPath(SpecialFolder folder) { @@ -740,6 +752,11 @@ namespace System.Management.Automation return Native.FollowSymLink(path); } + public static string GetFileOwner(string path) + { + return Native.GetFileOwner(path); + } + [StructLayout(LayoutKind.Sequential)] internal class SetDateInfoInternal { @@ -809,6 +826,9 @@ namespace System.Management.Automation [return: MarshalAs(UnmanagedType.LPStr)] internal static extern string FollowSymLink([MarshalAs(UnmanagedType.LPStr)]string filePath); + [DllImport(psLib, CharSet = CharSet.Ansi, SetLastError = true)] + [return: MarshalAs(UnmanagedType.LPStr)] + internal static extern string GetFileOwner([MarshalAs(UnmanagedType.LPStr)]string filePath); } }