135 lines
4.7 KiB
C
135 lines
4.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 <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_SPAWN_TABLE_SIZE 32u
|
|
#define POSIX_PTHREAD_TABLE_SIZE 32u
|
|
|
|
typedef struct POSIX_SPAWN_ENTRY {
|
|
pid_t Pid;
|
|
SYS_HANDLE Handle;
|
|
uint64_t FdInheritanceToken;
|
|
bool Used;
|
|
} POSIX_SPAWN_ENTRY;
|
|
|
|
typedef struct POSIX_PTHREAD_SLOT {
|
|
uint32_t Tid;
|
|
void* RetVal;
|
|
bool Used;
|
|
} POSIX_PTHREAD_SLOT;
|
|
|
|
typedef struct POSIX_LOGICAL_PROCESS_STATE {
|
|
uint32_t PosixPid;
|
|
uint32_t Generation;
|
|
} POSIX_LOGICAL_PROCESS_STATE;
|
|
|
|
typedef struct POSIX_PROCESS_STATE {
|
|
uint32_t Magic;
|
|
SYS_PEB* Peb;
|
|
SYS_HANDLE ServerEndpoint;
|
|
POSIX_LOGICAL_PROCESS_STATE DefaultIdentity;
|
|
SYS_HANDLE FdLock;
|
|
POSIX_FD FdTable[POSIX_FD_MAX];
|
|
POSIX_SPAWN_ENTRY SpawnTable[POSIX_SPAWN_TABLE_SIZE];
|
|
POSIX_PTHREAD_SLOT PthreadTable[POSIX_PTHREAD_TABLE_SIZE];
|
|
} POSIX_PROCESS_STATE;
|
|
|
|
typedef struct POSIX_THREAD_STATE {
|
|
uint32_t Magic;
|
|
int ErrnoValue;
|
|
uint8_t DlErrorPending;
|
|
uint8_t Reserved[3];
|
|
char DlError[POSIX_DLERROR_MAX];
|
|
POSIX_PROCESS_STATE* NativeProcess;
|
|
POSIX_LOGICAL_PROCESS_STATE* LogicalProcess;
|
|
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.
|
|
uint32_t 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);
|
|
|
|
// Attaches POSIX state to the current native TEB.
|
|
uint32_t PosixRuntimeAttachThread(void);
|
|
|
|
// 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 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);
|