// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Reflection; using System.Runtime.InteropServices; namespace Progmasoft.Tazel.Guest; /// The calls into the C library the agent needs, generated by [LibraryImport]. [System.Runtime.Versioning.SupportedOSPlatform("linux")] internal static partial class Libc { public const int AfVsock = 40; public const int SockStream = 1; public const uint VmaddrCidAny = 0xFFFFFFFF; public const ulong MsRdonly = 1; public const ulong MsNosuid = 2; public const ulong MsNodev = 4; public const ulong MsNoexec = 8; public const int RebootPowerOff = 0x4321FEDC; private const string Library = "libc"; /// /// The C library is the one the program was loaded with, so its symbols are looked up through the program itself; /// that works on musl and on glibc, whatever the file of the library is called. /// public static void Resolve() => NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), (name, _, _) => name == Library ? NativeLibrary.GetMainProgramHandle() : IntPtr.Zero); [LibraryImport(Library, EntryPoint = "mount", SetLastError = true, StringMarshalling = StringMarshalling.Utf8)] public static partial int Mount(string source, string target, string type, ulong flags, string? data); [LibraryImport(Library, EntryPoint = "sethostname", SetLastError = true)] public static partial int SetHostName(ReadOnlySpan name, nuint length); [LibraryImport(Library, EntryPoint = "sync")] public static partial void Sync(); [LibraryImport(Library, EntryPoint = "reboot", SetLastError = true)] public static partial int Reboot(int how); [LibraryImport(Library, EntryPoint = "socket", SetLastError = true)] public static partial int Socket(int domain, int type, int protocol); [LibraryImport(Library, EntryPoint = "bind", SetLastError = true)] public static partial int Bind(int socket, in SockaddrVM address, uint length); [LibraryImport(Library, EntryPoint = "listen", SetLastError = true)] public static partial int Listen(int socket, int backlog); [LibraryImport(Library, EntryPoint = "accept", SetLastError = true)] public static partial int Accept(int socket, IntPtr address, IntPtr length); [LibraryImport(Library, EntryPoint = "read", SetLastError = true)] public static unsafe partial nint Read(int fd, byte* buffer, nuint count); [LibraryImport(Library, EntryPoint = "write", SetLastError = true)] public static unsafe partial nint Write(int fd, byte* buffer, nuint count); [LibraryImport(Library, EntryPoint = "close", SetLastError = true)] public static partial int Close(int fd); /// The text of the last error of the C library. public static string LastError() => Marshal.GetLastPInvokeErrorMessage(); /// struct sockaddr_vm. [StructLayout(LayoutKind.Sequential)] public struct SockaddrVM : IEquatable { public ushort Family; public ushort Reserved; public uint Port; public uint Cid; public uint Zero; public static bool operator ==(SockaddrVM left, SockaddrVM right) => left.Equals(right); public static bool operator !=(SockaddrVM left, SockaddrVM right) => !left.Equals(right); public readonly bool Equals(SockaddrVM other) => Family == other.Family && Reserved == other.Reserved && Port == other.Port && Cid == other.Cid && Zero == other.Zero; public override readonly bool Equals(object? obj) => obj is SockaddrVM other && Equals(other); public override readonly int GetHashCode() => HashCode.Combine(Family, Reserved, Port, Cid, Zero); } }