1# Copyright 2017 Cloudbase Solutions Srl
2# All Rights Reserved.
3#
4#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5#    not use this file except in compliance with the License. You may obtain
6#    a copy of the License at
7#
8#         http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13#    License for the specific language governing permissions and limitations
14#    under the License.
15
16import ctypes
17
18from os_win.utils.winapi import wintypes
19
20lib_handle = None
21
22
23class IO_COUNTERS(ctypes.Structure):
24    _fields_ = [
25        ('ReadOperationCount', wintypes.ULONGLONG),
26        ('WriteOperationCount', wintypes.ULONGLONG),
27        ('OtherOperationCount', wintypes.ULONGLONG),
28        ('ReadTransferCount', wintypes.ULONGLONG),
29        ('WriteTransferCount', wintypes.ULONGLONG),
30        ('OtherTransferCount', wintypes.ULONGLONG)
31    ]
32
33
34class JOBOBJECT_BASIC_LIMIT_INFORMATION(ctypes.Structure):
35    _fields_ = [
36        ('PerProcessUserTimeLimit', wintypes.LARGE_INTEGER),
37        ('PerJobUserTimeLimit', wintypes.LARGE_INTEGER),
38        ('LimitFlags', wintypes.DWORD),
39        ('MinimumWorkingSetSize', ctypes.c_size_t),
40        ('MaximumWorkingSetSize', ctypes.c_size_t),
41        ('ActiveProcessLimit', wintypes.DWORD),
42        ('Affinity', wintypes.PULONG),
43        ('PriorityClass', wintypes.DWORD),
44        ('SchedulingClass', wintypes.DWORD)
45    ]
46
47
48class JOBOBJECT_EXTENDED_LIMIT_INFORMATION(ctypes.Structure):
49    _fields_ = [
50        ('BasicLimitInformation', JOBOBJECT_BASIC_LIMIT_INFORMATION),
51        ('IoInfo', IO_COUNTERS),
52        ('ProcessMemoryLimit', ctypes.c_size_t),
53        ('JobMemoryLimit', ctypes.c_size_t),
54        ('PeakProcessMemoryUsed', ctypes.c_size_t),
55        ('PeakJobMemoryUsed', ctypes.c_size_t)
56    ]
57
58
59class FILE_ID_128(ctypes.Structure):
60    _fields_ = [
61        ('Identifier', wintypes.BYTE * 16)
62    ]
63
64
65class FILE_ID_INFO(ctypes.Structure):
66    _fields_ = [
67        ('VolumeSerialNumber', wintypes.ULONGLONG),
68        ('FileId', FILE_ID_128)
69    ]
70
71
72def register():
73    global lib_handle
74    lib_handle = ctypes.windll.kernel32
75
76    lib_handle.CancelIoEx.argtypes = [
77        wintypes.HANDLE,
78        wintypes.LPOVERLAPPED
79    ]
80    lib_handle.CancelIoEx.restype = wintypes.BOOL
81
82    lib_handle.CloseHandle.argtypes = [wintypes.HANDLE]
83    lib_handle.CloseHandle.restype = wintypes.BOOL
84
85    lib_handle.CopyFileW.argtypes = [
86        wintypes.LPCWSTR,
87        wintypes.LPCWSTR,
88        wintypes.BOOL
89    ]
90    lib_handle.CopyFileW.restype = wintypes.BOOL
91
92    lib_handle.CreateEventW.argtypes = [
93        wintypes.PVOID,  # unused
94        wintypes.BOOL,
95        wintypes.BOOL,
96        wintypes.LPCWSTR
97    ]
98    lib_handle.CreateEventW.restype = wintypes.HANDLE
99
100    lib_handle.CreateFileW.argtypes = [
101        wintypes.LPCWSTR,
102        wintypes.DWORD,
103        wintypes.DWORD,
104        wintypes.PVOID,  # unused
105        wintypes.DWORD,
106        wintypes.DWORD,
107        wintypes.HANDLE
108    ]
109    lib_handle.CreateFileW.restype = wintypes.HANDLE
110
111    lib_handle.CreateMutexW.argtypes = [
112        wintypes.LPCVOID,
113        wintypes.BOOL,
114        wintypes.LPCWSTR]
115    lib_handle.CreateMutexW.restype = wintypes.HANDLE
116
117    lib_handle.CreatePipe.argtypes = [
118        wintypes.PHANDLE,
119        wintypes.PHANDLE,
120        wintypes.PVOID,
121        wintypes.DWORD
122    ]
123    lib_handle.CreatePipe.restype = wintypes.BOOL
124
125    lib_handle.CreateSymbolicLinkW.argtypes = [
126        wintypes.LPCWSTR,
127        wintypes.LPCWSTR,
128        wintypes.DWORD
129    ]
130    lib_handle.CreateSymbolicLinkW.restype = wintypes.BOOL
131
132    lib_handle.FormatMessageA.argtypes = [
133        wintypes.DWORD,
134        wintypes.LPCVOID,
135        wintypes.DWORD,
136        wintypes.DWORD,
137        wintypes.PVOID,
138        wintypes.DWORD,
139        wintypes.PVOID
140    ]
141    lib_handle.FormatMessageA.restype = wintypes.DWORD
142
143    lib_handle.GetFileInformationByHandleEx.argtypes = [
144        wintypes.HANDLE,
145        wintypes.DWORD,
146        wintypes.LPVOID,
147        wintypes.DWORD
148
149    ]
150    lib_handle.GetFileInformationByHandleEx.restype = wintypes.BOOL
151
152    lib_handle.GetDiskFreeSpaceExW.argtypes = [
153        wintypes.LPCWSTR,
154        wintypes.PULARGE_INTEGER,
155        wintypes.PULARGE_INTEGER,
156        wintypes.PULARGE_INTEGER
157    ]
158    lib_handle.GetDiskFreeSpaceExW.restype = wintypes.BOOL
159
160    lib_handle.GetFileAttributesW.argtypes = [wintypes.LPCWSTR]
161    lib_handle.GetFileAttributesW.restype = wintypes.DWORD
162
163    lib_handle.GetLastError.argtypes = []
164    lib_handle.GetLastError.restype = wintypes.DWORD
165
166    lib_handle.GetTickCount64.argtypes = []
167    lib_handle.GetTickCount64.restype = wintypes.ULONGLONG
168
169    lib_handle.IsProcessorFeaturePresent.argtypes = [wintypes.DWORD]
170    lib_handle.IsProcessorFeaturePresent.restype = wintypes.BOOL
171
172    lib_handle.LocalFree.argtypes = [wintypes.HANDLE]
173    lib_handle.LocalFree.restype = wintypes.HANDLE
174
175    lib_handle.ReadFile.argtypes = [
176        wintypes.HANDLE,
177        wintypes.LPVOID,
178        wintypes.DWORD,
179        wintypes.LPDWORD,
180        wintypes.LPOVERLAPPED
181    ]
182    lib_handle.ReadFile.restype = wintypes.BOOL
183
184    lib_handle.ReadFileEx.argtypes = [
185        wintypes.HANDLE,
186        wintypes.LPVOID,
187        wintypes.DWORD,
188        wintypes.LPOVERLAPPED,
189        wintypes.LPOVERLAPPED_COMPLETION_ROUTINE
190    ]
191    lib_handle.ReadFileEx.restype = wintypes.BOOL
192
193    lib_handle.ReleaseMutex.argtypes = [wintypes.HANDLE]
194    lib_handle.ReleaseMutex.restype = wintypes.BOOL
195
196    lib_handle.ResetEvent.argtypes = [wintypes.HANDLE]
197    lib_handle.ResetEvent.restype = wintypes.BOOL
198
199    lib_handle.SetEvent.argtypes = [wintypes.HANDLE]
200    lib_handle.SetEvent.restype = wintypes.BOOL
201
202    lib_handle.SetLastError.argtypes = [wintypes.DWORD]
203    lib_handle.SetLastError.restype = None
204
205    lib_handle.WaitForSingleObject.argtypes = [
206        wintypes.HANDLE,
207        wintypes.DWORD
208    ]
209    lib_handle.WaitForSingleObject.restype = wintypes.DWORD
210
211    lib_handle.WaitForSingleObjectEx.argtypes = [
212        wintypes.HANDLE,
213        wintypes.DWORD,
214        wintypes.BOOL
215    ]
216    lib_handle.WaitForSingleObjectEx.restype = wintypes.DWORD
217
218    lib_handle.WaitNamedPipeW.argtypes = [
219        wintypes.LPCWSTR,
220        wintypes.DWORD
221    ]
222    lib_handle.WaitNamedPipeW.restype = wintypes.BOOL
223
224    lib_handle.WriteFile.argtypes = [
225        wintypes.HANDLE,
226        wintypes.LPCVOID,
227        wintypes.DWORD,
228        wintypes.LPDWORD,
229        wintypes.LPOVERLAPPED,
230    ]
231    lib_handle.WriteFile.restype = wintypes.BOOL
232
233    lib_handle.WriteFileEx.argtypes = [
234        wintypes.HANDLE,
235        wintypes.LPCVOID,
236        wintypes.DWORD,
237        wintypes.LPOVERLAPPED,
238        wintypes.LPOVERLAPPED_COMPLETION_ROUTINE
239    ]
240    lib_handle.WriteFileEx.restype = wintypes.BOOL
241
242    lib_handle.CreateJobObjectW.argtypes = [
243        wintypes.LPCVOID,
244        wintypes.LPCWSTR
245    ]
246    lib_handle.CreateJobObjectW.restype = wintypes.HANDLE
247
248    lib_handle.SetInformationJobObject.argtypes = [
249        wintypes.HANDLE,
250        wintypes.INT,
251        wintypes.LPVOID,
252        wintypes.DWORD
253    ]
254    lib_handle.SetInformationJobObject.restype = wintypes.BOOL
255
256    lib_handle.AssignProcessToJobObject.argtypes = [
257        wintypes.HANDLE,
258        wintypes.HANDLE
259    ]
260    lib_handle.AssignProcessToJobObject.restype = wintypes.BOOL
261
262    lib_handle.OpenProcess.argtypes = [
263        wintypes.DWORD,
264        wintypes.BOOL,
265        wintypes.DWORD
266    ]
267    lib_handle.OpenProcess.restype = wintypes.HANDLE
268
269    lib_handle.WaitForMultipleObjects.argtypes = [
270        wintypes.DWORD,
271        wintypes.LPHANDLE,
272        wintypes.BOOL,
273        wintypes.DWORD
274    ]
275    lib_handle.WaitForMultipleObjects.restype = wintypes.DWORD
276