-
Notifications
You must be signed in to change notification settings - Fork 2
/
LibC.cs
35 lines (28 loc) · 1.09 KB
/
LibC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System.Runtime.InteropServices;
namespace ProcessDoctor.Backend.Linux.Proc.Native;
internal static class LibC
{
private const string Name = "libc";
[DllImport(Name, EntryPoint = "readlink", SetLastError = true)]
private static extern int NativeReadLink(string path, byte[] buffer, int bufferSize); // TODO: Make testable
/// <remarks>
/// The DllImportAttribute provides a SetLastError property
/// so the runtime knows to immediately capture the last error and
/// store it in a place that the managed code can read using Marshal.GetLastWin32Error.
/// </remarks>
internal static string? GetLastError()
=> Marshal.PtrToStringAnsi(StrError(Marshal.GetLastWin32Error()));
[DllImport(Name, EntryPoint = "strerror", SetLastError = false)]
private static extern IntPtr StrError(int errorCode);
public static int ReadLink(string path, byte[] buffer, int bufferSize)
{
try
{
return NativeReadLink(path, buffer, bufferSize);
}
catch (DllNotFoundException)
{
return -1;
}
}
}