Debian Codegraph @

src/port/win32ntdll.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * win32ntdll.c
4 * Dynamically loaded Windows NT functions.
5 *
6 * Portions Copyright (c) 2021-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/port/win32ntdll.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16 #include "c.h"
17
18 #include "port/win32ntdll.h"
19
20 RtlGetLastNtStatus_t pg_RtlGetLastNtStatus;
21 RtlNtStatusToDosError_t pg_RtlNtStatusToDosError;
22 NtFlushBuffersFileEx_t pg_NtFlushBuffersFileEx;
23
24 typedef struct NtDllRoutine
25 {
26 const char *name;
27 pg_funcptr_t *address;
28 } NtDllRoutine;
29
30 static const NtDllRoutine routines[] = {
31 {"RtlGetLastNtStatus", (pg_funcptr_t *) &pg_RtlGetLastNtStatus},
32 {"RtlNtStatusToDosError", (pg_funcptr_t *) &pg_RtlNtStatusToDosError},
33 {"NtFlushBuffersFileEx", (pg_funcptr_t *) &pg_NtFlushBuffersFileEx}
34 };
35
36 static bool initialized;
37
38 int
39 initialize_ntdll(void)
40 {
41 HMODULE module;
42
43 if (initialized)
44 return 0;
45
46 if (!(module = LoadLibraryEx("ntdll.dll", NULL, 0)))
47 {
48 _dosmaperr(GetLastError());
49 return -1;
50 }
51
52 for (int i = 0; i < lengthof(routines); ++i)
53 {
54 pg_funcptr_t address;
55
56 address = (pg_funcptr_t) GetProcAddress(module, routines[i].name);
57 if (!address)
58 {
59 _dosmaperr(GetLastError());
60 FreeLibrary(module);
61
62 return -1;
63 }
64
65 *(pg_funcptr_t *) routines[i].address = address;
66 }
67
68 initialized = true;
69
70 return 0;
71 }