1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include <assert.h>
23 
24 #include "uv.h"
25 #include "internal.h"
26 
27 
28 /* Ntdll function pointers */
29 sRtlNtStatusToDosError pRtlNtStatusToDosError;
30 sNtDeviceIoControlFile pNtDeviceIoControlFile;
31 sNtQueryInformationFile pNtQueryInformationFile;
32 sNtSetInformationFile pNtSetInformationFile;
33 sNtQueryVolumeInformationFile pNtQueryVolumeInformationFile;
34 sNtQuerySystemInformation pNtQuerySystemInformation;
35 
36 
37 /* Kernel32 function pointers */
38 sGetQueuedCompletionStatusEx pGetQueuedCompletionStatusEx;
39 sSetFileCompletionNotificationModes pSetFileCompletionNotificationModes;
40 sCreateSymbolicLinkW pCreateSymbolicLinkW;
41 sCancelIoEx pCancelIoEx;
42 sInitializeSRWLock pInitializeSRWLock;
43 sAcquireSRWLockShared pAcquireSRWLockShared;
44 sAcquireSRWLockExclusive pAcquireSRWLockExclusive;
45 sTryAcquireSRWLockShared pTryAcquireSRWLockShared;
46 sTryAcquireSRWLockExclusive pTryAcquireSRWLockExclusive;
47 sReleaseSRWLockShared pReleaseSRWLockShared;
48 sReleaseSRWLockExclusive pReleaseSRWLockExclusive;
49 sInitializeConditionVariable pInitializeConditionVariable;
50 sSleepConditionVariableCS pSleepConditionVariableCS;
51 sSleepConditionVariableSRW pSleepConditionVariableSRW;
52 sWakeAllConditionVariable pWakeAllConditionVariable;
53 sWakeConditionVariable pWakeConditionVariable;
54 
55 
uv_winapi_init()56 void uv_winapi_init() {
57   HMODULE ntdll_module;
58   HMODULE kernel32_module;
59 
60   ntdll_module = GetModuleHandleA("ntdll.dll");
61   if (ntdll_module == NULL) {
62     uv_fatal_error(GetLastError(), "GetModuleHandleA");
63   }
64 
65   pRtlNtStatusToDosError = (sRtlNtStatusToDosError) GetProcAddress(
66       ntdll_module,
67       "RtlNtStatusToDosError");
68   if (pRtlNtStatusToDosError == NULL) {
69     uv_fatal_error(GetLastError(), "GetProcAddress");
70   }
71 
72   pNtDeviceIoControlFile = (sNtDeviceIoControlFile) GetProcAddress(
73       ntdll_module,
74       "NtDeviceIoControlFile");
75   if (pNtDeviceIoControlFile == NULL) {
76     uv_fatal_error(GetLastError(), "GetProcAddress");
77   }
78 
79   pNtQueryInformationFile = (sNtQueryInformationFile) GetProcAddress(
80       ntdll_module,
81       "NtQueryInformationFile");
82   if (pNtQueryInformationFile == NULL) {
83     uv_fatal_error(GetLastError(), "GetProcAddress");
84   }
85 
86   pNtSetInformationFile = (sNtSetInformationFile) GetProcAddress(
87       ntdll_module,
88       "NtSetInformationFile");
89   if (pNtSetInformationFile == NULL) {
90     uv_fatal_error(GetLastError(), "GetProcAddress");
91   }
92 
93   pNtQueryVolumeInformationFile = (sNtQueryVolumeInformationFile)
94       GetProcAddress(ntdll_module, "NtQueryVolumeInformationFile");
95   if (pNtQueryVolumeInformationFile == NULL) {
96     uv_fatal_error(GetLastError(), "GetProcAddress");
97   }
98 
99   pNtQuerySystemInformation = (sNtQuerySystemInformation) GetProcAddress(
100       ntdll_module,
101       "NtQuerySystemInformation");
102   if (pNtQuerySystemInformation == NULL) {
103     uv_fatal_error(GetLastError(), "GetProcAddress");
104   }
105 
106   kernel32_module = GetModuleHandleA("kernel32.dll");
107   if (kernel32_module == NULL) {
108     uv_fatal_error(GetLastError(), "GetModuleHandleA");
109   }
110 
111   pGetQueuedCompletionStatusEx = (sGetQueuedCompletionStatusEx) GetProcAddress(
112       kernel32_module,
113       "GetQueuedCompletionStatusEx");
114 
115   pSetFileCompletionNotificationModes = (sSetFileCompletionNotificationModes)
116     GetProcAddress(kernel32_module, "SetFileCompletionNotificationModes");
117 
118   pCreateSymbolicLinkW = (sCreateSymbolicLinkW)
119     GetProcAddress(kernel32_module, "CreateSymbolicLinkW");
120 
121   pCancelIoEx = (sCancelIoEx)
122     GetProcAddress(kernel32_module, "CancelIoEx");
123 
124   pInitializeSRWLock = (sInitializeSRWLock)
125     GetProcAddress(kernel32_module, "InitializeSRWLock");
126 
127   pAcquireSRWLockShared = (sAcquireSRWLockShared)
128     GetProcAddress(kernel32_module, "AcquireSRWLockShared");
129 
130   pAcquireSRWLockExclusive = (sAcquireSRWLockExclusive)
131     GetProcAddress(kernel32_module, "AcquireSRWLockExclusive");
132 
133   pTryAcquireSRWLockShared = (sTryAcquireSRWLockShared)
134     GetProcAddress(kernel32_module, "TryAcquireSRWLockShared");
135 
136   pTryAcquireSRWLockExclusive = (sTryAcquireSRWLockExclusive)
137     GetProcAddress(kernel32_module, "TryAcquireSRWLockExclusive");
138 
139   pReleaseSRWLockShared = (sReleaseSRWLockShared)
140     GetProcAddress(kernel32_module, "ReleaseSRWLockShared");
141 
142   pReleaseSRWLockExclusive = (sReleaseSRWLockExclusive)
143     GetProcAddress(kernel32_module, "ReleaseSRWLockExclusive");
144 
145   pInitializeConditionVariable = (sInitializeConditionVariable)
146     GetProcAddress(kernel32_module, "InitializeConditionVariable");
147 
148   pSleepConditionVariableCS = (sSleepConditionVariableCS)
149     GetProcAddress(kernel32_module, "SleepConditionVariableCS");
150 
151   pSleepConditionVariableSRW = (sSleepConditionVariableSRW)
152     GetProcAddress(kernel32_module, "SleepConditionVariableSRW");
153 
154   pWakeAllConditionVariable = (sWakeAllConditionVariable)
155     GetProcAddress(kernel32_module, "WakeAllConditionVariable");
156 
157   pWakeConditionVariable = (sWakeConditionVariable)
158     GetProcAddress(kernel32_module, "WakeConditionVariable");
159 }
160