tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
mainroot / src / Progmasoft.Tazel.Guest / Libc.cs
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: MIT
using System;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Progmasoft.Tazel.Guest;
/// <summary>The calls into the C library the agent needs, generated by <c>[LibraryImport]</c>.</summary>
[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";
/// <summary>
/// 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.
/// </summary>
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<byte> 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);
/// <summary>The text of the last error of the C library.</summary>
public static string LastError() => Marshal.GetLastPInvokeErrorMessage();
/// <summary><c>struct sockaddr_vm</c>.</summary>
[StructLayout(LayoutKind.Sequential)]
public struct SockaddrVM : IEquatable<SockaddrVM>
{
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);
}
}