• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 sRtlGetVersion pRtlGetVersion;
30 sRtlNtStatusToDosError pRtlNtStatusToDosError;
31 sNtDeviceIoControlFile pNtDeviceIoControlFile;
32 sNtQueryInformationFile pNtQueryInformationFile;
33 sNtSetInformationFile pNtSetInformationFile;
34 sNtQueryVolumeInformationFile pNtQueryVolumeInformationFile;
35 sNtQueryDirectoryFile pNtQueryDirectoryFile;
36 sNtQuerySystemInformation pNtQuerySystemInformation;
37 sNtQueryInformationProcess pNtQueryInformationProcess;
38 
39 /* Advapi32 function pointers */
40 sRtlGenRandom pRtlGenRandom;
41 
42 /* Kernel32 function pointers */
43 sGetQueuedCompletionStatusEx pGetQueuedCompletionStatusEx;
44 
45 /* Powrprof.dll function pointer */
46 sPowerRegisterSuspendResumeNotification pPowerRegisterSuspendResumeNotification;
47 
48 /* User32.dll function pointer */
49 sSetWinEventHook pSetWinEventHook;
50 
51 
uv_winapi_init(void)52 void uv_winapi_init(void) {
53   HMODULE ntdll_module;
54   HMODULE powrprof_module;
55   HMODULE user32_module;
56   HMODULE kernel32_module;
57   HMODULE advapi32_module;
58 
59   ntdll_module = GetModuleHandleA("ntdll.dll");
60   if (ntdll_module == NULL) {
61     uv_fatal_error(GetLastError(), "GetModuleHandleA");
62   }
63 
64   pRtlGetVersion = (sRtlGetVersion) GetProcAddress(ntdll_module,
65                                                    "RtlGetVersion");
66 
67   pRtlNtStatusToDosError = (sRtlNtStatusToDosError) GetProcAddress(
68       ntdll_module,
69       "RtlNtStatusToDosError");
70   if (pRtlNtStatusToDosError == NULL) {
71     uv_fatal_error(GetLastError(), "GetProcAddress");
72   }
73 
74   pNtDeviceIoControlFile = (sNtDeviceIoControlFile) GetProcAddress(
75       ntdll_module,
76       "NtDeviceIoControlFile");
77   if (pNtDeviceIoControlFile == NULL) {
78     uv_fatal_error(GetLastError(), "GetProcAddress");
79   }
80 
81   pNtQueryInformationFile = (sNtQueryInformationFile) GetProcAddress(
82       ntdll_module,
83       "NtQueryInformationFile");
84   if (pNtQueryInformationFile == NULL) {
85     uv_fatal_error(GetLastError(), "GetProcAddress");
86   }
87 
88   pNtSetInformationFile = (sNtSetInformationFile) GetProcAddress(
89       ntdll_module,
90       "NtSetInformationFile");
91   if (pNtSetInformationFile == NULL) {
92     uv_fatal_error(GetLastError(), "GetProcAddress");
93   }
94 
95   pNtQueryVolumeInformationFile = (sNtQueryVolumeInformationFile)
96       GetProcAddress(ntdll_module, "NtQueryVolumeInformationFile");
97   if (pNtQueryVolumeInformationFile == NULL) {
98     uv_fatal_error(GetLastError(), "GetProcAddress");
99   }
100 
101   pNtQueryDirectoryFile = (sNtQueryDirectoryFile)
102       GetProcAddress(ntdll_module, "NtQueryDirectoryFile");
103   if (pNtQueryVolumeInformationFile == NULL) {
104     uv_fatal_error(GetLastError(), "GetProcAddress");
105   }
106 
107   pNtQuerySystemInformation = (sNtQuerySystemInformation) GetProcAddress(
108       ntdll_module,
109       "NtQuerySystemInformation");
110   if (pNtQuerySystemInformation == NULL) {
111     uv_fatal_error(GetLastError(), "GetProcAddress");
112   }
113 
114   pNtQueryInformationProcess = (sNtQueryInformationProcess) GetProcAddress(
115       ntdll_module,
116       "NtQueryInformationProcess");
117   if (pNtQueryInformationProcess == NULL) {
118     uv_fatal_error(GetLastError(), "GetProcAddress");
119   }
120 
121   kernel32_module = GetModuleHandleA("kernel32.dll");
122   if (kernel32_module == NULL) {
123     uv_fatal_error(GetLastError(), "GetModuleHandleA");
124   }
125 
126   pGetQueuedCompletionStatusEx = (sGetQueuedCompletionStatusEx) GetProcAddress(
127       kernel32_module,
128       "GetQueuedCompletionStatusEx");
129 
130   powrprof_module = LoadLibraryA("powrprof.dll");
131   if (powrprof_module != NULL) {
132     pPowerRegisterSuspendResumeNotification = (sPowerRegisterSuspendResumeNotification)
133       GetProcAddress(powrprof_module, "PowerRegisterSuspendResumeNotification");
134   }
135 
136   user32_module = LoadLibraryA("user32.dll");
137   if (user32_module != NULL) {
138     pSetWinEventHook = (sSetWinEventHook)
139       GetProcAddress(user32_module, "SetWinEventHook");
140   }
141 
142   advapi32_module = GetModuleHandleA("advapi32.dll");
143   if (advapi32_module == NULL) {
144     uv_fatal_error(GetLastError(), "GetModuleHandleA");
145   }
146 
147   pRtlGenRandom =
148       (sRtlGenRandom) GetProcAddress(advapi32_module, "SystemFunction036");
149 }
150