Replace usage of libpsl-native APIs with .NET equivalents#27327
Replace usage of libpsl-native APIs with .NET equivalents#27327adityapatwardhan wants to merge 4 commits into
Conversation
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
There was a problem hiding this comment.
Pull request overview
Refactors several non-Windows code paths in System.Management.Automation to reduce reliance on libpsl-native by replacing native interop calls with .NET APIs for link creation, process termination/waiting, and errno-to-ErrorCategory mapping.
Changes:
- Switch non-Windows symbolic/hard link creation to
File/Directory.CreateSymbolicLinkandFile.CreateHardLink, plumbing anerrorCodeback to the caller. - Replace non-Windows
waitpidusage in SSH process termination withProcess-based waiting and add exception handling around process lookup/state checks. - Replace native
GetErrorCategorywith a managedswitchmapping and remove several now-unused native method declarations.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/System.Management.Automation/namespaces/FileSystemProvider.cs | Updates UNIX link-creation call sites to use new Platform.NonWindowsCreate*Link(..., out errorCode) signatures. |
| src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs | Replaces NonWindowsWaitPid with Process.GetProcessById(...).WaitForExit() (with exception handling) in UNIX SSH teardown. |
| src/System.Management.Automation/CoreCLR/CorePsPlatform.cs | Implements .NET-based link creation + process kill; replaces native error-category mapping with managed switch; removes related native imports. |
| errorCode = Marshal.GetLastPInvokeError(); | ||
| if (errorCode == 0) | ||
| { | ||
| errorCode = ex.HResult & 0xFFFF; | ||
| } |
There was a problem hiding this comment.
@copilot apply changes based on this feedback
| return Unix.NativeMethods.WaitPid(pid, nohang); | ||
| try | ||
| { | ||
| Process.GetProcessById(pid).Kill(); |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@adityapatwardhan Are we going to get rid of the |
|
The initial plan is for Linux part only. In phase 2 I will investigate whether we can get rid of pwrshplugin as well. From what i have seen, there is no 1:1 mapping of all Linux functionality in .NET. |
PR Summary
This pull request refactors and modernizes the implementation of symbolic and hard link creation, process management, and error handling on non-Windows platforms in PowerShell. The main changes include replacing native interop calls with .NET APIs, improving error reporting, and updating related code paths to use the new patterns.
Key improvements and changes:
Symbolic and Hard Link Creation:
Unix.NativeMethods.CreateSymLinkandCreateHardLink) with .NET'sFile.CreateSymbolicLink,Directory.CreateSymbolicLink, andFile.CreateHardLinkmethods. These methods now return an error code via an out parameter and provide more robust error handling by catching relevant exceptions and extracting error information. [1] [2] [3]Process Management:
Processclass methods (Process.Kill,Process.WaitForExit) instead of native interop (Unix.NativeMethods.KillProcess,WaitPid). This includes improved exception handling to gracefully handle cases where the process may have already exited or cannot be queried. [1] [2]Error Handling and Categorization:
Code Cleanup and Removal of Native Interop:
NativeMethods, including those for link creation, process killing, and error categorization. [1] [2] [3]General Refactoring:
These changes collectively modernize the codebase, improve maintainability, and enhance cross-platform compatibility by leveraging .NET APIs instead of platform-specific native interop.
PR Context
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header