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/<PID> file. However, .NET Core
has no API to get this information, thus it had to be plumbed through to
the native library.
This commit is contained in:
Andrew Schwartzmeyer
2016-07-13 15:46:25 -07:00
parent 13a82a59ee
commit e6d3cbcce0
2 changed files with 24 additions and 0 deletions
@@ -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;
}
@@ -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);
}
}