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

200 lines
7.7 KiB
C

// POSIX personality internal declarations and process/thread runtime state.
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <pthread.h>
#include <spawn.h>
#include <signal.h>
#include <dlfcn.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <poll.h>
#include <pwd.h>
#include <grp.h>
#include <ntlite/posix_service.h>
#include <ntlite/posix_extensions.h>
#include <ntlite/syscall.h>
#include "posix_fd.h"
#define POSIX_PROCESS_STATE_MAGIC 0x50505243u
#define POSIX_THREAD_STATE_MAGIC 0x50544852u
#define POSIX_DLERROR_MAX 160u
#define POSIX_PTHREAD_TABLE_SIZE 32u
typedef struct POSIX_PTHREAD_SLOT {
uint32_t Tid;
void* RetVal;
bool Used;
} POSIX_PTHREAD_SLOT;
typedef struct POSIX_SIGNAL_ACTION_STATE {
volatile uintptr_t Handler;
volatile uint64_t Mask;
volatile uint32_t Flags;
uint32_t Reserved;
} POSIX_SIGNAL_ACTION_STATE;
typedef struct POSIX_LOGICAL_PROCESS_STATE {
uint32_t PosixPid;
uint32_t Generation;
SYS_HANDLE FdLock;
POSIX_FD FdTable[POSIX_FD_MAX];
POSIX_SIGNAL_ACTION_STATE SignalActions[NTL_POSIX_SIGNAL_MAX + 1u];
} POSIX_LOGICAL_PROCESS_STATE;
typedef struct POSIX_PROCESS_STATE {
uint32_t Magic;
SYS_PEB* Peb;
SYS_HANDLE ServerEndpoint;
POSIX_LOGICAL_PROCESS_STATE DefaultIdentity;
uint64_t BootstrapToken;
uint32_t BootstrapPosixPid;
uint32_t BootstrapGeneration;
void* ExceptionHandlerHandle;
int ArgumentCount;
char** Arguments;
char** Environment;
char* CommandLine;
POSIX_PTHREAD_SLOT PthreadTable[POSIX_PTHREAD_TABLE_SIZE];
} POSIX_PROCESS_STATE;
typedef struct POSIX_VFORK_CHILD_STATE {
POSIX_LOGICAL_PROCESS_STATE LogicalProcess;
uint64_t ServerToken;
uint64_t FdInheritanceToken;
uint64_t InitialSignalMask;
} POSIX_VFORK_CHILD_STATE;
typedef struct POSIX_THREAD_STATE {
uint32_t Magic;
int ErrnoValue;
uint32_t NativeThreadId;
uint8_t DlErrorPending;
uint8_t ServerRegistered;
uint8_t Reserved[2];
volatile uint64_t SignalBlocked;
volatile uint64_t SignalHandlerBlocked;
volatile uint64_t SignalPending;
volatile uint64_t SignalDeliveryCount;
SYS_HANDLE SignalWaitEvent;
char DlError[POSIX_DLERROR_MAX];
POSIX_PROCESS_STATE* NativeProcess;
POSIX_LOGICAL_PROCESS_STATE* LogicalProcess;
POSIX_VFORK_CHILD_STATE* BorrowedChild;
struct passwd PasswordResult;
char PasswordName[NTL_POSIX_IDENTITY_NAME_MAX + 1u];
char PasswordHome[NTL_POSIX_IDENTITY_HOME_MAX + 1u];
char PasswordShell[NTL_POSIX_IDENTITY_SHELL_MAX + 1u];
struct group GroupResult;
char GroupName[NTL_POSIX_IDENTITY_NAME_MAX + 1u];
char* GroupMembers[1];
char LoginName[NTL_POSIX_IDENTITY_NAME_MAX + 1u];
} POSIX_THREAD_STATE;
// Initializes process-local POSIX state and attaches the initial thread.
KSTATUS PosixRuntimeInitializeProcess(void);
// Returns the POSIX state for the current process after startup.
POSIX_PROCESS_STATE* PosixRuntimeProcessState(void);
// Returns the current thread state, attaching lazily when necessary.
POSIX_THREAD_STATE* PosixRuntimeThreadState(void);
// Returns the logical POSIX process selected by the current native thread.
POSIX_LOGICAL_PROCESS_STATE* PosixRuntimeLogicalProcessState(void);
// Attaches POSIX state to the current native TEB.
KSTATUS PosixRuntimeAttachThread(void);
// Attaches a thread while seeding its inherited persistent signal mask.
KSTATUS PosixRuntimeAttachThreadWithSignalMask(uint64_t signalMask);
// Allocates one borrowed logical child and pins its inherited descriptor snapshot.
KSTATUS PosixVforkBeginBorrowedChild(POSIX_VFORK_CHILD_STATE** childOut);
// Attaches the calling native thread to one prepared borrowed logical child.
KSTATUS PosixVforkAttachBorrowedChild(POSIX_VFORK_CHILD_STATE* child);
// Tears down the calling borrowed thread's local wrappers before its native thread exits.
void PosixVforkDetachBorrowedChildLocal(POSIX_VFORK_CHILD_STATE* child, bool releaseServerHolders);
// Cancels one borrowed child after setup failure or after its native thread has exited.
KSTATUS PosixVforkCancelBorrowedChild(POSIX_VFORK_CHILD_STATE* child);
// Records a genuine borrowed-child exit before its native thread terminates.
KSTATUS PosixVforkRecordExit(POSIX_VFORK_CHILD_STATE* child, int status);
// Returns whether the calling POSIX thread is one borrowed vfork child.
POSIX_VFORK_CHILD_STATE* PosixVforkCurrentBorrowedChild(void);
// Delivers one POSIX signal from the generic user-APC substrate.
void PosixSignalApc(void* signalValue, void* routeValue, void* reserved);
// Dispatches thread-pending signals made deliverable by a mask change.
void PosixSignalDispatchLocalPending(POSIX_THREAD_STATE* thread);
// Installs the POSIX exception-to-signal personality adapter.
KSTATUS PosixSignalInitializeExceptionTranslation(POSIX_PROCESS_STATE* process);
// Releases POSIX state owned by the current native TEB.
void PosixRuntimeDetachThread(void);
// Clears the current thread's dynamic-loader error state.
void PosixRuntimeClearDlError(void);
// Stores one dynamic-loader error in current-thread state.
void PosixRuntimeSetDlError(const char* message);
// Returns and consumes the current thread's dynamic-loader error.
char* PosixRuntimeTakeDlError(void);
// Calls posixsrv over the registered process endpoint and validates one fixed-size reply.
KSTATUS PosixRuntimeCallServer(const NTL_POSIX_REQUEST* request, NTL_POSIX_REPLY* replyOut);
// Calls posixsrv through a temporary registered connection for operations that may block.
KSTATUS PosixRuntimeCallServerBlocking(const NTL_POSIX_REQUEST* request, NTL_POSIX_REPLY* replyOut);
// Calls posixsrv through a temporary registered connection while transferring capabilities.
KSTATUS PosixRuntimeCallServerBlockingWithHandles(const NTL_POSIX_REQUEST* request,
const SYS_PORT_HANDLE_TRANSFER* handles,
uint32_t handleCount,
NTL_POSIX_REPLY* replyOut);
// Calls posixsrv while transferring a bounded set of native capabilities.
KSTATUS PosixRuntimeCallServerWithHandles(const NTL_POSIX_REQUEST* request,
const SYS_PORT_HANDLE_TRANSFER* handles,
uint32_t handleCount,
NTL_POSIX_REPLY* replyOut);
// Calls posixsrv with independent request/reply capability transfer.
KSTATUS PosixRuntimeCallServerExchange(const NTL_POSIX_REQUEST* request,
const SYS_PORT_HANDLE_TRANSFER* requestHandles,
uint32_t requestHandleCount,
SYS_HANDLE* replyHandlesOut,
uint32_t replyHandleCapacity,
uint32_t expectedReplyHandleCount,
NTL_POSIX_REPLY* replyOut);
// Calls posixsrv and accepts a variable number of reply capabilities up to the supplied capacity.
KSTATUS PosixRuntimeCallServerExchangeVariable(const NTL_POSIX_REQUEST* request,
SYS_HANDLE* replyHandlesOut,
uint32_t replyHandleCapacity,
uint32_t* replyHandleCountOut,
NTL_POSIX_REPLY* replyOut);
// Queries the server-owned shared process attributes for the current logical process.
KSTATUS PosixRuntimeGetProcessAttributes(NTL_POSIX_REPLY* replyOut);