nt: separate out ntdll.dll dynamic import logic

This commit is contained in:
Bert Belder 2017-09-03 20:51:17 +02:00
parent 3319986f74
commit d6cbb73f4e
3 changed files with 60 additions and 29 deletions

View File

@ -6,6 +6,7 @@
#include "afd.h" #include "afd.h"
#include "epoll.h" #include "epoll.h"
#include "error.h" #include "error.h"
#include "nt.h"
#include "tree.h" #include "tree.h"
#include "win.h" #include "win.h"
@ -50,7 +51,6 @@ static int epoll__afd_poll(SOCKET socket,
OVERLAPPED* overlapped); OVERLAPPED* overlapped);
static int epoll__initialized = 0; static int epoll__initialized = 0;
static PNTDEVICEIOCONTROLFILE pNtDeviceIoControlFile;
/* State associated with a epoll handle. */ /* State associated with a epoll handle. */
struct epoll_port_data_s { struct epoll_port_data_s {
@ -570,7 +570,6 @@ int epoll_close(epoll_t port_handle) {
} }
int epoll__initialize() { int epoll__initialize() {
HMODULE ntdll;
int r; int r;
WSADATA wsa_data; WSADATA wsa_data;
@ -578,13 +577,7 @@ int epoll__initialize() {
if (r != 0) if (r != 0)
return -1; return -1;
ntdll = LoadLibraryW(L"ntdll.dll"); if (nt_initialize() < 0)
if (ntdll == NULL)
return -1;
pNtDeviceIoControlFile =
(PNTDEVICEIOCONTROLFILE) GetProcAddress(ntdll, "NtDeviceIoControlFile");
if (pNtDeviceIoControlFile == NULL)
return -1; return -1;
return 0; return 0;
@ -747,16 +740,16 @@ int epoll__afd_poll(SOCKET socket,
} }
iosb_ptr->Status = STATUS_PENDING; iosb_ptr->Status = STATUS_PENDING;
status = pNtDeviceIoControlFile((HANDLE) socket, status = NtDeviceIoControlFile((HANDLE) socket,
event, event,
NULL, NULL,
apc_context, apc_context,
iosb_ptr, iosb_ptr,
IOCTL_AFD_POLL, IOCTL_AFD_POLL,
info, info,
sizeof *info, sizeof *info,
info, info,
sizeof *info); sizeof *info);
if (overlapped == NULL) { if (overlapped == NULL) {
/* If this is a blocking operation, wait for the event to become /* If this is a blocking operation, wait for the event to become

27
src/nt.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdlib.h>
#include "nt.h"
#include "win.h"
#define X(return_type, declarators, name, ...) \
return_type(declarators* name)(__VA_ARGS__) = NULL;
NTDLL_IMPORT_LIST(X)
#undef X
int nt_initialize() {
HMODULE ntdll;
ntdll = LoadLibraryW(L"ntdll.dll");
if (ntdll == NULL)
return -1;
#define X(return_type, declarators, name, ...) \
name = \
(return_type(declarators*)(__VA_ARGS__)) GetProcAddress(ntdll, #name); \
if (name == NULL) \
return -1;
NTDLL_IMPORT_LIST(X)
#undef X
return 0;
}

View File

@ -4,6 +4,8 @@
#include "ntstatus.h" #include "ntstatus.h"
#include "win.h" #include "win.h"
int nt_initialize();
typedef struct _IO_STATUS_BLOCK { typedef struct _IO_STATUS_BLOCK {
union { union {
NTSTATUS Status; NTSTATUS Status;
@ -16,15 +18,24 @@ typedef VOID(NTAPI* PIO_APC_ROUTINE)(PVOID ApcContext,
PIO_STATUS_BLOCK IoStatusBlock, PIO_STATUS_BLOCK IoStatusBlock,
ULONG Reserved); ULONG Reserved);
typedef NTSTATUS(NTAPI* PNTDEVICEIOCONTROLFILE)(HANDLE FileHandle, #define NTDLL_IMPORT_LIST(X) \
HANDLE Event, X(NTSTATUS, \
PIO_APC_ROUTINE ApcRoutine, NTAPI, \
PVOID ApcContext, NtDeviceIoControlFile, \
PIO_STATUS_BLOCK IoStatusBlock, HANDLE FileHandle, \
ULONG IoControlCode, HANDLE Event, \
PVOID InputBuffer, PIO_APC_ROUTINE ApcRoutine, \
ULONG InputBufferLength, PVOID ApcContext, \
PVOID OutputBuffer, PIO_STATUS_BLOCK IoStatusBlock, \
ULONG OutputBufferLength); ULONG IoControlCode, \
PVOID InputBuffer, \
ULONG InputBufferLength, \
PVOID OutputBuffer, \
ULONG OutputBufferLength)
#define X(return_type, declarators, name, ...) \
extern return_type(declarators* name)(__VA_ARGS__);
NTDLL_IMPORT_LIST(X)
#undef X
#endif /* EPOLL_NT_H_ */ #endif /* EPOLL_NT_H_ */