Files
nt-lite/code/user/personalities/posix/posix_fd.h
T

82 lines
3.3 KiB
C

// POSIX descriptor/open-description declarations internal to posix.dll.
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <ntlite/syscall.h>
#define POSIX_FD_MAX 64
#define POSIX_FD_INHERIT_SKIP_CLOEXEC 0x00000001u
// Open-description kinds owned by the POSIX personality.
typedef enum POSIX_FD_TYPE {
PosixFdFile = NTL_POSIX_OPEN_TYPE_FILE,
PosixFdConsoleRead = NTL_POSIX_OPEN_TYPE_CONSOLE_READ,
PosixFdConsoleWrite = NTL_POSIX_OPEN_TYPE_CONSOLE_WRITE,
PosixFdDevNull = NTL_POSIX_OPEN_TYPE_DEV_NULL,
PosixFdDevZero = NTL_POSIX_OPEN_TYPE_DEV_ZERO,
PosixFdSocket = NTL_POSIX_OPEN_TYPE_SOCKET,
PosixFdPipeRead = NTL_POSIX_OPEN_TYPE_PIPE_READ,
PosixFdPipeWrite = NTL_POSIX_OPEN_TYPE_PIPE_WRITE,
} POSIX_FD_TYPE;
// One refcounted open description shared by one or more descriptors.
typedef struct POSIX_OPEN_DESCRIPTION {
volatile int32_t RefCount;
POSIX_FD_TYPE Type;
uint32_t KHandle;
uint64_t DescriptionId;
NTL_POSIX_SHARED_OPEN_DESCRIPTION* Shared;
} POSIX_OPEN_DESCRIPTION;
// One logical-process descriptor entry with descriptor-local flags.
typedef struct POSIX_FD {
POSIX_OPEN_DESCRIPTION* Description;
int DescriptorFlags;
} POSIX_FD;
struct POSIX_LOGICAL_PROCESS_STATE;
// Initializes one logical process descriptor table from inherited state or fresh standard descriptors.
KSTATUS PosixFdInitializeLogicalProcess(struct POSIX_LOGICAL_PROCESS_STATE* process, uint64_t inheritanceToken);
// Captures the current descriptor table in one server-owned inheritance snapshot.
KSTATUS PosixFdCreateInheritanceSnapshot(uint32_t flags, uint64_t* tokenOut);
// Cancels one unclaimed inheritance snapshot owned by the current process.
void PosixFdCancelInheritanceSnapshot(uint64_t token);
// Releases descriptor-table resources during failed process startup.
void PosixFdDestroyLogicalProcess(struct POSIX_LOGICAL_PROCESS_STATE* process);
// Releases only process-local wrappers after server-side exec holder ownership has committed.
void PosixFdDestroyLogicalProcessLocal(struct POSIX_LOGICAL_PROCESS_STATE* process);
// Creates and installs one new open description at the lowest free descriptor >= 3.
int PosixFdCreate(POSIX_FD_TYPE type, uint32_t khandle, int statusFlags);
// Installs one process-local wrapper for a server-created shared open description.
int PosixFdImport(POSIX_FD_TYPE type, uint32_t khandle, uint64_t descriptionId,
NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared, int descriptorFlags);
// Releases one server holder when import/construction fails before a local wrapper exists.
void PosixFdReleaseServerDescription(uint64_t descriptionId);
// Acquires a stable reference to an open description backing fd.
POSIX_OPEN_DESCRIPTION* PosixFdAcquire(int fd);
// Returns one atomic snapshot of shared open-description status flags.
int PosixFdGetStatusFlags(const POSIX_OPEN_DESCRIPTION* description);
// Applies F_SETFL mutable status through the server-owned open-description authority.
int PosixFdSetStatusFlags(POSIX_OPEN_DESCRIPTION* description, int statusFlags);
// Releases one stable open-description reference.
void PosixFdRelease(POSIX_OPEN_DESCRIPTION* description);
// Removes one descriptor and releases its table-owned open-description reference.
int PosixFdClose(int fd);
int PosixStatusToErrno(KSTATUS status);