1 | n/a | /* |
---|
2 | n/a | * Support routines from the Windows API |
---|
3 | n/a | * |
---|
4 | n/a | * This module was originally created by merging PC/_subprocess.c with |
---|
5 | n/a | * Modules/_multiprocessing/win32_functions.c. |
---|
6 | n/a | * |
---|
7 | n/a | * Copyright (c) 2004 by Fredrik Lundh <fredrik@pythonware.com> |
---|
8 | n/a | * Copyright (c) 2004 by Secret Labs AB, http://www.pythonware.com |
---|
9 | n/a | * Copyright (c) 2004 by Peter Astrand <astrand@lysator.liu.se> |
---|
10 | n/a | * |
---|
11 | n/a | * By obtaining, using, and/or copying this software and/or its |
---|
12 | n/a | * associated documentation, you agree that you have read, understood, |
---|
13 | n/a | * and will comply with the following terms and conditions: |
---|
14 | n/a | * |
---|
15 | n/a | * Permission to use, copy, modify, and distribute this software and |
---|
16 | n/a | * its associated documentation for any purpose and without fee is |
---|
17 | n/a | * hereby granted, provided that the above copyright notice appears in |
---|
18 | n/a | * all copies, and that both that copyright notice and this permission |
---|
19 | n/a | * notice appear in supporting documentation, and that the name of the |
---|
20 | n/a | * authors not be used in advertising or publicity pertaining to |
---|
21 | n/a | * distribution of the software without specific, written prior |
---|
22 | n/a | * permission. |
---|
23 | n/a | * |
---|
24 | n/a | * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
---|
25 | n/a | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. |
---|
26 | n/a | * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
---|
27 | n/a | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
---|
28 | n/a | * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
---|
29 | n/a | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
---|
30 | n/a | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
31 | n/a | * |
---|
32 | n/a | */ |
---|
33 | n/a | |
---|
34 | n/a | /* Licensed to PSF under a Contributor Agreement. */ |
---|
35 | n/a | /* See http://www.python.org/2.4/license for licensing details. */ |
---|
36 | n/a | |
---|
37 | n/a | #include "Python.h" |
---|
38 | n/a | #include "structmember.h" |
---|
39 | n/a | |
---|
40 | n/a | #define WINDOWS_LEAN_AND_MEAN |
---|
41 | n/a | #include "windows.h" |
---|
42 | n/a | #include <crtdbg.h> |
---|
43 | n/a | #include "winreparse.h" |
---|
44 | n/a | |
---|
45 | n/a | #if defined(MS_WIN32) && !defined(MS_WIN64) |
---|
46 | n/a | #define HANDLE_TO_PYNUM(handle) \ |
---|
47 | n/a | PyLong_FromUnsignedLong((unsigned long) handle) |
---|
48 | n/a | #define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLong(obj)) |
---|
49 | n/a | #define F_POINTER "k" |
---|
50 | n/a | #define T_POINTER T_ULONG |
---|
51 | n/a | #else |
---|
52 | n/a | #define HANDLE_TO_PYNUM(handle) \ |
---|
53 | n/a | PyLong_FromUnsignedLongLong((unsigned long long) handle) |
---|
54 | n/a | #define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLongLong(obj)) |
---|
55 | n/a | #define F_POINTER "K" |
---|
56 | n/a | #define T_POINTER T_ULONGLONG |
---|
57 | n/a | #endif |
---|
58 | n/a | |
---|
59 | n/a | #define F_HANDLE F_POINTER |
---|
60 | n/a | #define F_DWORD "k" |
---|
61 | n/a | |
---|
62 | n/a | #define T_HANDLE T_POINTER |
---|
63 | n/a | |
---|
64 | n/a | #define DWORD_MAX 4294967295U |
---|
65 | n/a | |
---|
66 | n/a | /* Grab CancelIoEx dynamically from kernel32 */ |
---|
67 | n/a | static int has_CancelIoEx = -1; |
---|
68 | n/a | static BOOL (CALLBACK *Py_CancelIoEx)(HANDLE, LPOVERLAPPED); |
---|
69 | n/a | |
---|
70 | n/a | static int |
---|
71 | n/a | check_CancelIoEx() |
---|
72 | n/a | { |
---|
73 | n/a | if (has_CancelIoEx == -1) |
---|
74 | n/a | { |
---|
75 | n/a | HINSTANCE hKernel32 = GetModuleHandle("KERNEL32"); |
---|
76 | n/a | * (FARPROC *) &Py_CancelIoEx = GetProcAddress(hKernel32, |
---|
77 | n/a | "CancelIoEx"); |
---|
78 | n/a | has_CancelIoEx = (Py_CancelIoEx != NULL); |
---|
79 | n/a | } |
---|
80 | n/a | return has_CancelIoEx; |
---|
81 | n/a | } |
---|
82 | n/a | |
---|
83 | n/a | |
---|
84 | n/a | /* |
---|
85 | n/a | * A Python object wrapping an OVERLAPPED structure and other useful data |
---|
86 | n/a | * for overlapped I/O |
---|
87 | n/a | */ |
---|
88 | n/a | |
---|
89 | n/a | typedef struct { |
---|
90 | n/a | PyObject_HEAD |
---|
91 | n/a | OVERLAPPED overlapped; |
---|
92 | n/a | /* For convenience, we store the file handle too */ |
---|
93 | n/a | HANDLE handle; |
---|
94 | n/a | /* Whether there's I/O in flight */ |
---|
95 | n/a | int pending; |
---|
96 | n/a | /* Whether I/O completed successfully */ |
---|
97 | n/a | int completed; |
---|
98 | n/a | /* Buffer used for reading (optional) */ |
---|
99 | n/a | PyObject *read_buffer; |
---|
100 | n/a | /* Buffer used for writing (optional) */ |
---|
101 | n/a | Py_buffer write_buffer; |
---|
102 | n/a | } OverlappedObject; |
---|
103 | n/a | |
---|
104 | n/a | static void |
---|
105 | n/a | overlapped_dealloc(OverlappedObject *self) |
---|
106 | n/a | { |
---|
107 | n/a | DWORD bytes; |
---|
108 | n/a | int err = GetLastError(); |
---|
109 | n/a | |
---|
110 | n/a | if (self->pending) { |
---|
111 | n/a | if (check_CancelIoEx() && |
---|
112 | n/a | Py_CancelIoEx(self->handle, &self->overlapped) && |
---|
113 | n/a | GetOverlappedResult(self->handle, &self->overlapped, &bytes, TRUE)) |
---|
114 | n/a | { |
---|
115 | n/a | /* The operation is no longer pending -- nothing to do. */ |
---|
116 | n/a | } |
---|
117 | n/a | else if (_Py_Finalizing == NULL) |
---|
118 | n/a | { |
---|
119 | n/a | /* The operation is still pending -- give a warning. This |
---|
120 | n/a | will probably only happen on Windows XP. */ |
---|
121 | n/a | PyErr_SetString(PyExc_RuntimeError, |
---|
122 | n/a | "I/O operations still in flight while destroying " |
---|
123 | n/a | "Overlapped object, the process may crash"); |
---|
124 | n/a | PyErr_WriteUnraisable(NULL); |
---|
125 | n/a | } |
---|
126 | n/a | else |
---|
127 | n/a | { |
---|
128 | n/a | /* The operation is still pending, but the process is |
---|
129 | n/a | probably about to exit, so we need not worry too much |
---|
130 | n/a | about memory leaks. Leaking self prevents a potential |
---|
131 | n/a | crash. This can happen when a daemon thread is cleaned |
---|
132 | n/a | up at exit -- see #19565. We only expect to get here |
---|
133 | n/a | on Windows XP. */ |
---|
134 | n/a | CloseHandle(self->overlapped.hEvent); |
---|
135 | n/a | SetLastError(err); |
---|
136 | n/a | return; |
---|
137 | n/a | } |
---|
138 | n/a | } |
---|
139 | n/a | |
---|
140 | n/a | CloseHandle(self->overlapped.hEvent); |
---|
141 | n/a | SetLastError(err); |
---|
142 | n/a | if (self->write_buffer.obj) |
---|
143 | n/a | PyBuffer_Release(&self->write_buffer); |
---|
144 | n/a | Py_CLEAR(self->read_buffer); |
---|
145 | n/a | PyObject_Del(self); |
---|
146 | n/a | } |
---|
147 | n/a | |
---|
148 | n/a | /*[clinic input] |
---|
149 | n/a | module _winapi |
---|
150 | n/a | class _winapi.Overlapped "OverlappedObject *" "&OverlappedType" |
---|
151 | n/a | [clinic start generated code]*/ |
---|
152 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c13d3f5fd1dabb84]*/ |
---|
153 | n/a | |
---|
154 | n/a | /*[python input] |
---|
155 | n/a | def create_converter(type_, format_unit): |
---|
156 | n/a | name = type_ + '_converter' |
---|
157 | n/a | # registered upon creation by CConverter's metaclass |
---|
158 | n/a | type(name, (CConverter,), {'type': type_, 'format_unit': format_unit}) |
---|
159 | n/a | |
---|
160 | n/a | # format unit differs between platforms for these |
---|
161 | n/a | create_converter('HANDLE', '" F_HANDLE "') |
---|
162 | n/a | create_converter('HMODULE', '" F_HANDLE "') |
---|
163 | n/a | create_converter('LPSECURITY_ATTRIBUTES', '" F_POINTER "') |
---|
164 | n/a | |
---|
165 | n/a | create_converter('BOOL', 'i') # F_BOOL used previously (always 'i') |
---|
166 | n/a | create_converter('DWORD', 'k') # F_DWORD is always "k" (which is much shorter) |
---|
167 | n/a | create_converter('LPCTSTR', 's') |
---|
168 | n/a | create_converter('LPWSTR', 'u') |
---|
169 | n/a | create_converter('UINT', 'I') # F_UINT used previously (always 'I') |
---|
170 | n/a | |
---|
171 | n/a | class HANDLE_return_converter(CReturnConverter): |
---|
172 | n/a | type = 'HANDLE' |
---|
173 | n/a | |
---|
174 | n/a | def render(self, function, data): |
---|
175 | n/a | self.declare(data) |
---|
176 | n/a | self.err_occurred_if("_return_value == INVALID_HANDLE_VALUE", data) |
---|
177 | n/a | data.return_conversion.append( |
---|
178 | n/a | 'if (_return_value == NULL) {\n Py_RETURN_NONE;\n}\n') |
---|
179 | n/a | data.return_conversion.append( |
---|
180 | n/a | 'return_value = HANDLE_TO_PYNUM(_return_value);\n') |
---|
181 | n/a | |
---|
182 | n/a | class DWORD_return_converter(CReturnConverter): |
---|
183 | n/a | type = 'DWORD' |
---|
184 | n/a | |
---|
185 | n/a | def render(self, function, data): |
---|
186 | n/a | self.declare(data) |
---|
187 | n/a | self.err_occurred_if("_return_value == DWORD_MAX", data) |
---|
188 | n/a | data.return_conversion.append( |
---|
189 | n/a | 'return_value = Py_BuildValue("k", _return_value);\n') |
---|
190 | n/a | [python start generated code]*/ |
---|
191 | n/a | /*[python end generated code: output=da39a3ee5e6b4b0d input=94819e72d2c6d558]*/ |
---|
192 | n/a | |
---|
193 | n/a | #include "clinic/_winapi.c.h" |
---|
194 | n/a | |
---|
195 | n/a | /*[clinic input] |
---|
196 | n/a | _winapi.Overlapped.GetOverlappedResult |
---|
197 | n/a | |
---|
198 | n/a | wait: bool |
---|
199 | n/a | / |
---|
200 | n/a | [clinic start generated code]*/ |
---|
201 | n/a | |
---|
202 | n/a | static PyObject * |
---|
203 | n/a | _winapi_Overlapped_GetOverlappedResult_impl(OverlappedObject *self, int wait) |
---|
204 | n/a | /*[clinic end generated code: output=bdd0c1ed6518cd03 input=194505ee8e0e3565]*/ |
---|
205 | n/a | { |
---|
206 | n/a | BOOL res; |
---|
207 | n/a | DWORD transferred = 0; |
---|
208 | n/a | DWORD err; |
---|
209 | n/a | |
---|
210 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
211 | n/a | res = GetOverlappedResult(self->handle, &self->overlapped, &transferred, |
---|
212 | n/a | wait != 0); |
---|
213 | n/a | Py_END_ALLOW_THREADS |
---|
214 | n/a | |
---|
215 | n/a | err = res ? ERROR_SUCCESS : GetLastError(); |
---|
216 | n/a | switch (err) { |
---|
217 | n/a | case ERROR_SUCCESS: |
---|
218 | n/a | case ERROR_MORE_DATA: |
---|
219 | n/a | case ERROR_OPERATION_ABORTED: |
---|
220 | n/a | self->completed = 1; |
---|
221 | n/a | self->pending = 0; |
---|
222 | n/a | break; |
---|
223 | n/a | case ERROR_IO_INCOMPLETE: |
---|
224 | n/a | break; |
---|
225 | n/a | default: |
---|
226 | n/a | self->pending = 0; |
---|
227 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, err); |
---|
228 | n/a | } |
---|
229 | n/a | if (self->completed && self->read_buffer != NULL) { |
---|
230 | n/a | assert(PyBytes_CheckExact(self->read_buffer)); |
---|
231 | n/a | if (transferred != PyBytes_GET_SIZE(self->read_buffer) && |
---|
232 | n/a | _PyBytes_Resize(&self->read_buffer, transferred)) |
---|
233 | n/a | return NULL; |
---|
234 | n/a | } |
---|
235 | n/a | return Py_BuildValue("II", (unsigned) transferred, (unsigned) err); |
---|
236 | n/a | } |
---|
237 | n/a | |
---|
238 | n/a | /*[clinic input] |
---|
239 | n/a | _winapi.Overlapped.getbuffer |
---|
240 | n/a | [clinic start generated code]*/ |
---|
241 | n/a | |
---|
242 | n/a | static PyObject * |
---|
243 | n/a | _winapi_Overlapped_getbuffer_impl(OverlappedObject *self) |
---|
244 | n/a | /*[clinic end generated code: output=95a3eceefae0f748 input=347fcfd56b4ceabd]*/ |
---|
245 | n/a | { |
---|
246 | n/a | PyObject *res; |
---|
247 | n/a | if (!self->completed) { |
---|
248 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
249 | n/a | "can't get read buffer before GetOverlappedResult() " |
---|
250 | n/a | "signals the operation completed"); |
---|
251 | n/a | return NULL; |
---|
252 | n/a | } |
---|
253 | n/a | res = self->read_buffer ? self->read_buffer : Py_None; |
---|
254 | n/a | Py_INCREF(res); |
---|
255 | n/a | return res; |
---|
256 | n/a | } |
---|
257 | n/a | |
---|
258 | n/a | /*[clinic input] |
---|
259 | n/a | _winapi.Overlapped.cancel |
---|
260 | n/a | [clinic start generated code]*/ |
---|
261 | n/a | |
---|
262 | n/a | static PyObject * |
---|
263 | n/a | _winapi_Overlapped_cancel_impl(OverlappedObject *self) |
---|
264 | n/a | /*[clinic end generated code: output=fcb9ab5df4ebdae5 input=cbf3da142290039f]*/ |
---|
265 | n/a | { |
---|
266 | n/a | BOOL res = TRUE; |
---|
267 | n/a | |
---|
268 | n/a | if (self->pending) { |
---|
269 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
270 | n/a | if (check_CancelIoEx()) |
---|
271 | n/a | res = Py_CancelIoEx(self->handle, &self->overlapped); |
---|
272 | n/a | else |
---|
273 | n/a | res = CancelIo(self->handle); |
---|
274 | n/a | Py_END_ALLOW_THREADS |
---|
275 | n/a | } |
---|
276 | n/a | |
---|
277 | n/a | /* CancelIoEx returns ERROR_NOT_FOUND if the I/O completed in-between */ |
---|
278 | n/a | if (!res && GetLastError() != ERROR_NOT_FOUND) |
---|
279 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
---|
280 | n/a | self->pending = 0; |
---|
281 | n/a | Py_RETURN_NONE; |
---|
282 | n/a | } |
---|
283 | n/a | |
---|
284 | n/a | static PyMethodDef overlapped_methods[] = { |
---|
285 | n/a | _WINAPI_OVERLAPPED_GETOVERLAPPEDRESULT_METHODDEF |
---|
286 | n/a | _WINAPI_OVERLAPPED_GETBUFFER_METHODDEF |
---|
287 | n/a | _WINAPI_OVERLAPPED_CANCEL_METHODDEF |
---|
288 | n/a | {NULL} |
---|
289 | n/a | }; |
---|
290 | n/a | |
---|
291 | n/a | static PyMemberDef overlapped_members[] = { |
---|
292 | n/a | {"event", T_HANDLE, |
---|
293 | n/a | offsetof(OverlappedObject, overlapped) + offsetof(OVERLAPPED, hEvent), |
---|
294 | n/a | READONLY, "overlapped event handle"}, |
---|
295 | n/a | {NULL} |
---|
296 | n/a | }; |
---|
297 | n/a | |
---|
298 | n/a | PyTypeObject OverlappedType = { |
---|
299 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
300 | n/a | /* tp_name */ "_winapi.Overlapped", |
---|
301 | n/a | /* tp_basicsize */ sizeof(OverlappedObject), |
---|
302 | n/a | /* tp_itemsize */ 0, |
---|
303 | n/a | /* tp_dealloc */ (destructor) overlapped_dealloc, |
---|
304 | n/a | /* tp_print */ 0, |
---|
305 | n/a | /* tp_getattr */ 0, |
---|
306 | n/a | /* tp_setattr */ 0, |
---|
307 | n/a | /* tp_reserved */ 0, |
---|
308 | n/a | /* tp_repr */ 0, |
---|
309 | n/a | /* tp_as_number */ 0, |
---|
310 | n/a | /* tp_as_sequence */ 0, |
---|
311 | n/a | /* tp_as_mapping */ 0, |
---|
312 | n/a | /* tp_hash */ 0, |
---|
313 | n/a | /* tp_call */ 0, |
---|
314 | n/a | /* tp_str */ 0, |
---|
315 | n/a | /* tp_getattro */ 0, |
---|
316 | n/a | /* tp_setattro */ 0, |
---|
317 | n/a | /* tp_as_buffer */ 0, |
---|
318 | n/a | /* tp_flags */ Py_TPFLAGS_DEFAULT, |
---|
319 | n/a | /* tp_doc */ "OVERLAPPED structure wrapper", |
---|
320 | n/a | /* tp_traverse */ 0, |
---|
321 | n/a | /* tp_clear */ 0, |
---|
322 | n/a | /* tp_richcompare */ 0, |
---|
323 | n/a | /* tp_weaklistoffset */ 0, |
---|
324 | n/a | /* tp_iter */ 0, |
---|
325 | n/a | /* tp_iternext */ 0, |
---|
326 | n/a | /* tp_methods */ overlapped_methods, |
---|
327 | n/a | /* tp_members */ overlapped_members, |
---|
328 | n/a | /* tp_getset */ 0, |
---|
329 | n/a | /* tp_base */ 0, |
---|
330 | n/a | /* tp_dict */ 0, |
---|
331 | n/a | /* tp_descr_get */ 0, |
---|
332 | n/a | /* tp_descr_set */ 0, |
---|
333 | n/a | /* tp_dictoffset */ 0, |
---|
334 | n/a | /* tp_init */ 0, |
---|
335 | n/a | /* tp_alloc */ 0, |
---|
336 | n/a | /* tp_new */ 0, |
---|
337 | n/a | }; |
---|
338 | n/a | |
---|
339 | n/a | static OverlappedObject * |
---|
340 | n/a | new_overlapped(HANDLE handle) |
---|
341 | n/a | { |
---|
342 | n/a | OverlappedObject *self; |
---|
343 | n/a | |
---|
344 | n/a | self = PyObject_New(OverlappedObject, &OverlappedType); |
---|
345 | n/a | if (!self) |
---|
346 | n/a | return NULL; |
---|
347 | n/a | self->handle = handle; |
---|
348 | n/a | self->read_buffer = NULL; |
---|
349 | n/a | self->pending = 0; |
---|
350 | n/a | self->completed = 0; |
---|
351 | n/a | memset(&self->overlapped, 0, sizeof(OVERLAPPED)); |
---|
352 | n/a | memset(&self->write_buffer, 0, sizeof(Py_buffer)); |
---|
353 | n/a | /* Manual reset, initially non-signalled */ |
---|
354 | n/a | self->overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
---|
355 | n/a | return self; |
---|
356 | n/a | } |
---|
357 | n/a | |
---|
358 | n/a | /* -------------------------------------------------------------------- */ |
---|
359 | n/a | /* windows API functions */ |
---|
360 | n/a | |
---|
361 | n/a | /*[clinic input] |
---|
362 | n/a | _winapi.CloseHandle |
---|
363 | n/a | |
---|
364 | n/a | handle: HANDLE |
---|
365 | n/a | / |
---|
366 | n/a | |
---|
367 | n/a | Close handle. |
---|
368 | n/a | [clinic start generated code]*/ |
---|
369 | n/a | |
---|
370 | n/a | static PyObject * |
---|
371 | n/a | _winapi_CloseHandle_impl(PyObject *module, HANDLE handle) |
---|
372 | n/a | /*[clinic end generated code: output=7ad37345f07bd782 input=7f0e4ac36e0352b8]*/ |
---|
373 | n/a | { |
---|
374 | n/a | BOOL success; |
---|
375 | n/a | |
---|
376 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
377 | n/a | success = CloseHandle(handle); |
---|
378 | n/a | Py_END_ALLOW_THREADS |
---|
379 | n/a | |
---|
380 | n/a | if (!success) |
---|
381 | n/a | return PyErr_SetFromWindowsErr(0); |
---|
382 | n/a | |
---|
383 | n/a | Py_RETURN_NONE; |
---|
384 | n/a | } |
---|
385 | n/a | |
---|
386 | n/a | /*[clinic input] |
---|
387 | n/a | _winapi.ConnectNamedPipe |
---|
388 | n/a | |
---|
389 | n/a | handle: HANDLE |
---|
390 | n/a | overlapped as use_overlapped: int(c_default='0') = False |
---|
391 | n/a | [clinic start generated code]*/ |
---|
392 | n/a | |
---|
393 | n/a | static PyObject * |
---|
394 | n/a | _winapi_ConnectNamedPipe_impl(PyObject *module, HANDLE handle, |
---|
395 | n/a | int use_overlapped) |
---|
396 | n/a | /*[clinic end generated code: output=335a0e7086800671 input=edc83da007ebf3be]*/ |
---|
397 | n/a | { |
---|
398 | n/a | BOOL success; |
---|
399 | n/a | OverlappedObject *overlapped = NULL; |
---|
400 | n/a | |
---|
401 | n/a | if (use_overlapped) { |
---|
402 | n/a | overlapped = new_overlapped(handle); |
---|
403 | n/a | if (!overlapped) |
---|
404 | n/a | return NULL; |
---|
405 | n/a | } |
---|
406 | n/a | |
---|
407 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
408 | n/a | success = ConnectNamedPipe(handle, |
---|
409 | n/a | overlapped ? &overlapped->overlapped : NULL); |
---|
410 | n/a | Py_END_ALLOW_THREADS |
---|
411 | n/a | |
---|
412 | n/a | if (overlapped) { |
---|
413 | n/a | int err = GetLastError(); |
---|
414 | n/a | /* Overlapped ConnectNamedPipe never returns a success code */ |
---|
415 | n/a | assert(success == 0); |
---|
416 | n/a | if (err == ERROR_IO_PENDING) |
---|
417 | n/a | overlapped->pending = 1; |
---|
418 | n/a | else if (err == ERROR_PIPE_CONNECTED) |
---|
419 | n/a | SetEvent(overlapped->overlapped.hEvent); |
---|
420 | n/a | else { |
---|
421 | n/a | Py_DECREF(overlapped); |
---|
422 | n/a | return PyErr_SetFromWindowsErr(err); |
---|
423 | n/a | } |
---|
424 | n/a | return (PyObject *) overlapped; |
---|
425 | n/a | } |
---|
426 | n/a | if (!success) |
---|
427 | n/a | return PyErr_SetFromWindowsErr(0); |
---|
428 | n/a | |
---|
429 | n/a | Py_RETURN_NONE; |
---|
430 | n/a | } |
---|
431 | n/a | |
---|
432 | n/a | /*[clinic input] |
---|
433 | n/a | _winapi.CreateFile -> HANDLE |
---|
434 | n/a | |
---|
435 | n/a | file_name: LPCTSTR |
---|
436 | n/a | desired_access: DWORD |
---|
437 | n/a | share_mode: DWORD |
---|
438 | n/a | security_attributes: LPSECURITY_ATTRIBUTES |
---|
439 | n/a | creation_disposition: DWORD |
---|
440 | n/a | flags_and_attributes: DWORD |
---|
441 | n/a | template_file: HANDLE |
---|
442 | n/a | / |
---|
443 | n/a | [clinic start generated code]*/ |
---|
444 | n/a | |
---|
445 | n/a | static HANDLE |
---|
446 | n/a | _winapi_CreateFile_impl(PyObject *module, LPCTSTR file_name, |
---|
447 | n/a | DWORD desired_access, DWORD share_mode, |
---|
448 | n/a | LPSECURITY_ATTRIBUTES security_attributes, |
---|
449 | n/a | DWORD creation_disposition, |
---|
450 | n/a | DWORD flags_and_attributes, HANDLE template_file) |
---|
451 | n/a | /*[clinic end generated code: output=417ddcebfc5a3d53 input=6423c3e40372dbd5]*/ |
---|
452 | n/a | { |
---|
453 | n/a | HANDLE handle; |
---|
454 | n/a | |
---|
455 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
456 | n/a | handle = CreateFile(file_name, desired_access, |
---|
457 | n/a | share_mode, security_attributes, |
---|
458 | n/a | creation_disposition, |
---|
459 | n/a | flags_and_attributes, template_file); |
---|
460 | n/a | Py_END_ALLOW_THREADS |
---|
461 | n/a | |
---|
462 | n/a | if (handle == INVALID_HANDLE_VALUE) |
---|
463 | n/a | PyErr_SetFromWindowsErr(0); |
---|
464 | n/a | |
---|
465 | n/a | return handle; |
---|
466 | n/a | } |
---|
467 | n/a | |
---|
468 | n/a | /*[clinic input] |
---|
469 | n/a | _winapi.CreateJunction |
---|
470 | n/a | |
---|
471 | n/a | src_path: LPWSTR |
---|
472 | n/a | dst_path: LPWSTR |
---|
473 | n/a | / |
---|
474 | n/a | [clinic start generated code]*/ |
---|
475 | n/a | |
---|
476 | n/a | static PyObject * |
---|
477 | n/a | _winapi_CreateJunction_impl(PyObject *module, LPWSTR src_path, |
---|
478 | n/a | LPWSTR dst_path) |
---|
479 | n/a | /*[clinic end generated code: output=66b7eb746e1dfa25 input=8cd1f9964b6e3d36]*/ |
---|
480 | n/a | { |
---|
481 | n/a | /* Privilege adjustment */ |
---|
482 | n/a | HANDLE token = NULL; |
---|
483 | n/a | TOKEN_PRIVILEGES tp; |
---|
484 | n/a | |
---|
485 | n/a | /* Reparse data buffer */ |
---|
486 | n/a | const USHORT prefix_len = 4; |
---|
487 | n/a | USHORT print_len = 0; |
---|
488 | n/a | USHORT rdb_size = 0; |
---|
489 | n/a | _Py_PREPARSE_DATA_BUFFER rdb = NULL; |
---|
490 | n/a | |
---|
491 | n/a | /* Junction point creation */ |
---|
492 | n/a | HANDLE junction = NULL; |
---|
493 | n/a | DWORD ret = 0; |
---|
494 | n/a | |
---|
495 | n/a | if (src_path == NULL || dst_path == NULL) |
---|
496 | n/a | return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER); |
---|
497 | n/a | |
---|
498 | n/a | if (wcsncmp(src_path, L"\\??\\", prefix_len) == 0) |
---|
499 | n/a | return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER); |
---|
500 | n/a | |
---|
501 | n/a | /* Adjust privileges to allow rewriting directory entry as a |
---|
502 | n/a | junction point. */ |
---|
503 | n/a | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) |
---|
504 | n/a | goto cleanup; |
---|
505 | n/a | |
---|
506 | n/a | if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid)) |
---|
507 | n/a | goto cleanup; |
---|
508 | n/a | |
---|
509 | n/a | tp.PrivilegeCount = 1; |
---|
510 | n/a | tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
---|
511 | n/a | if (!AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), |
---|
512 | n/a | NULL, NULL)) |
---|
513 | n/a | goto cleanup; |
---|
514 | n/a | |
---|
515 | n/a | if (GetFileAttributesW(src_path) == INVALID_FILE_ATTRIBUTES) |
---|
516 | n/a | goto cleanup; |
---|
517 | n/a | |
---|
518 | n/a | /* Store the absolute link target path length in print_len. */ |
---|
519 | n/a | print_len = (USHORT)GetFullPathNameW(src_path, 0, NULL, NULL); |
---|
520 | n/a | if (print_len == 0) |
---|
521 | n/a | goto cleanup; |
---|
522 | n/a | |
---|
523 | n/a | /* NUL terminator should not be part of print_len. */ |
---|
524 | n/a | --print_len; |
---|
525 | n/a | |
---|
526 | n/a | /* REPARSE_DATA_BUFFER usage is heavily under-documented, especially for |
---|
527 | n/a | junction points. Here's what I've learned along the way: |
---|
528 | n/a | - A junction point has two components: a print name and a substitute |
---|
529 | n/a | name. They both describe the link target, but the substitute name is |
---|
530 | n/a | the physical target and the print name is shown in directory listings. |
---|
531 | n/a | - The print name must be a native name, prefixed with "\??\". |
---|
532 | n/a | - Both names are stored after each other in the same buffer (the |
---|
533 | n/a | PathBuffer) and both must be NUL-terminated. |
---|
534 | n/a | - There are four members defining their respective offset and length |
---|
535 | n/a | inside PathBuffer: SubstituteNameOffset, SubstituteNameLength, |
---|
536 | n/a | PrintNameOffset and PrintNameLength. |
---|
537 | n/a | - The total size we need to allocate for the REPARSE_DATA_BUFFER, thus, |
---|
538 | n/a | is the sum of: |
---|
539 | n/a | - the fixed header size (REPARSE_DATA_BUFFER_HEADER_SIZE) |
---|
540 | n/a | - the size of the MountPointReparseBuffer member without the PathBuffer |
---|
541 | n/a | - the size of the prefix ("\??\") in bytes |
---|
542 | n/a | - the size of the print name in bytes |
---|
543 | n/a | - the size of the substitute name in bytes |
---|
544 | n/a | - the size of two NUL terminators in bytes */ |
---|
545 | n/a | rdb_size = _Py_REPARSE_DATA_BUFFER_HEADER_SIZE + |
---|
546 | n/a | sizeof(rdb->MountPointReparseBuffer) - |
---|
547 | n/a | sizeof(rdb->MountPointReparseBuffer.PathBuffer) + |
---|
548 | n/a | /* Two +1's for NUL terminators. */ |
---|
549 | n/a | (prefix_len + print_len + 1 + print_len + 1) * sizeof(WCHAR); |
---|
550 | n/a | rdb = (_Py_PREPARSE_DATA_BUFFER)PyMem_RawMalloc(rdb_size); |
---|
551 | n/a | if (rdb == NULL) |
---|
552 | n/a | goto cleanup; |
---|
553 | n/a | |
---|
554 | n/a | memset(rdb, 0, rdb_size); |
---|
555 | n/a | rdb->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
---|
556 | n/a | rdb->ReparseDataLength = rdb_size - _Py_REPARSE_DATA_BUFFER_HEADER_SIZE; |
---|
557 | n/a | rdb->MountPointReparseBuffer.SubstituteNameOffset = 0; |
---|
558 | n/a | rdb->MountPointReparseBuffer.SubstituteNameLength = |
---|
559 | n/a | (prefix_len + print_len) * sizeof(WCHAR); |
---|
560 | n/a | rdb->MountPointReparseBuffer.PrintNameOffset = |
---|
561 | n/a | rdb->MountPointReparseBuffer.SubstituteNameLength + sizeof(WCHAR); |
---|
562 | n/a | rdb->MountPointReparseBuffer.PrintNameLength = print_len * sizeof(WCHAR); |
---|
563 | n/a | |
---|
564 | n/a | /* Store the full native path of link target at the substitute name |
---|
565 | n/a | offset (0). */ |
---|
566 | n/a | wcscpy(rdb->MountPointReparseBuffer.PathBuffer, L"\\??\\"); |
---|
567 | n/a | if (GetFullPathNameW(src_path, print_len + 1, |
---|
568 | n/a | rdb->MountPointReparseBuffer.PathBuffer + prefix_len, |
---|
569 | n/a | NULL) == 0) |
---|
570 | n/a | goto cleanup; |
---|
571 | n/a | |
---|
572 | n/a | /* Copy everything but the native prefix to the print name offset. */ |
---|
573 | n/a | wcscpy(rdb->MountPointReparseBuffer.PathBuffer + |
---|
574 | n/a | prefix_len + print_len + 1, |
---|
575 | n/a | rdb->MountPointReparseBuffer.PathBuffer + prefix_len); |
---|
576 | n/a | |
---|
577 | n/a | /* Create a directory for the junction point. */ |
---|
578 | n/a | if (!CreateDirectoryW(dst_path, NULL)) |
---|
579 | n/a | goto cleanup; |
---|
580 | n/a | |
---|
581 | n/a | junction = CreateFileW(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
---|
582 | n/a | OPEN_EXISTING, |
---|
583 | n/a | FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
---|
584 | n/a | if (junction == INVALID_HANDLE_VALUE) |
---|
585 | n/a | goto cleanup; |
---|
586 | n/a | |
---|
587 | n/a | /* Make the directory entry a junction point. */ |
---|
588 | n/a | if (!DeviceIoControl(junction, FSCTL_SET_REPARSE_POINT, rdb, rdb_size, |
---|
589 | n/a | NULL, 0, &ret, NULL)) |
---|
590 | n/a | goto cleanup; |
---|
591 | n/a | |
---|
592 | n/a | cleanup: |
---|
593 | n/a | ret = GetLastError(); |
---|
594 | n/a | |
---|
595 | n/a | CloseHandle(token); |
---|
596 | n/a | CloseHandle(junction); |
---|
597 | n/a | PyMem_RawFree(rdb); |
---|
598 | n/a | |
---|
599 | n/a | if (ret != 0) |
---|
600 | n/a | return PyErr_SetFromWindowsErr(ret); |
---|
601 | n/a | |
---|
602 | n/a | Py_RETURN_NONE; |
---|
603 | n/a | } |
---|
604 | n/a | |
---|
605 | n/a | /*[clinic input] |
---|
606 | n/a | _winapi.CreateNamedPipe -> HANDLE |
---|
607 | n/a | |
---|
608 | n/a | name: LPCTSTR |
---|
609 | n/a | open_mode: DWORD |
---|
610 | n/a | pipe_mode: DWORD |
---|
611 | n/a | max_instances: DWORD |
---|
612 | n/a | out_buffer_size: DWORD |
---|
613 | n/a | in_buffer_size: DWORD |
---|
614 | n/a | default_timeout: DWORD |
---|
615 | n/a | security_attributes: LPSECURITY_ATTRIBUTES |
---|
616 | n/a | / |
---|
617 | n/a | [clinic start generated code]*/ |
---|
618 | n/a | |
---|
619 | n/a | static HANDLE |
---|
620 | n/a | _winapi_CreateNamedPipe_impl(PyObject *module, LPCTSTR name, DWORD open_mode, |
---|
621 | n/a | DWORD pipe_mode, DWORD max_instances, |
---|
622 | n/a | DWORD out_buffer_size, DWORD in_buffer_size, |
---|
623 | n/a | DWORD default_timeout, |
---|
624 | n/a | LPSECURITY_ATTRIBUTES security_attributes) |
---|
625 | n/a | /*[clinic end generated code: output=80f8c07346a94fbc input=5a73530b84d8bc37]*/ |
---|
626 | n/a | { |
---|
627 | n/a | HANDLE handle; |
---|
628 | n/a | |
---|
629 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
630 | n/a | handle = CreateNamedPipe(name, open_mode, pipe_mode, |
---|
631 | n/a | max_instances, out_buffer_size, |
---|
632 | n/a | in_buffer_size, default_timeout, |
---|
633 | n/a | security_attributes); |
---|
634 | n/a | Py_END_ALLOW_THREADS |
---|
635 | n/a | |
---|
636 | n/a | if (handle == INVALID_HANDLE_VALUE) |
---|
637 | n/a | PyErr_SetFromWindowsErr(0); |
---|
638 | n/a | |
---|
639 | n/a | return handle; |
---|
640 | n/a | } |
---|
641 | n/a | |
---|
642 | n/a | /*[clinic input] |
---|
643 | n/a | _winapi.CreatePipe |
---|
644 | n/a | |
---|
645 | n/a | pipe_attrs: object |
---|
646 | n/a | Ignored internally, can be None. |
---|
647 | n/a | size: DWORD |
---|
648 | n/a | / |
---|
649 | n/a | |
---|
650 | n/a | Create an anonymous pipe. |
---|
651 | n/a | |
---|
652 | n/a | Returns a 2-tuple of handles, to the read and write ends of the pipe. |
---|
653 | n/a | [clinic start generated code]*/ |
---|
654 | n/a | |
---|
655 | n/a | static PyObject * |
---|
656 | n/a | _winapi_CreatePipe_impl(PyObject *module, PyObject *pipe_attrs, DWORD size) |
---|
657 | n/a | /*[clinic end generated code: output=1c4411d8699f0925 input=c4f2cfa56ef68d90]*/ |
---|
658 | n/a | { |
---|
659 | n/a | HANDLE read_pipe; |
---|
660 | n/a | HANDLE write_pipe; |
---|
661 | n/a | BOOL result; |
---|
662 | n/a | |
---|
663 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
664 | n/a | result = CreatePipe(&read_pipe, &write_pipe, NULL, size); |
---|
665 | n/a | Py_END_ALLOW_THREADS |
---|
666 | n/a | |
---|
667 | n/a | if (! result) |
---|
668 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
---|
669 | n/a | |
---|
670 | n/a | return Py_BuildValue( |
---|
671 | n/a | "NN", HANDLE_TO_PYNUM(read_pipe), HANDLE_TO_PYNUM(write_pipe)); |
---|
672 | n/a | } |
---|
673 | n/a | |
---|
674 | n/a | /* helpers for createprocess */ |
---|
675 | n/a | |
---|
676 | n/a | static unsigned long |
---|
677 | n/a | getulong(PyObject* obj, const char* name) |
---|
678 | n/a | { |
---|
679 | n/a | PyObject* value; |
---|
680 | n/a | unsigned long ret; |
---|
681 | n/a | |
---|
682 | n/a | value = PyObject_GetAttrString(obj, name); |
---|
683 | n/a | if (! value) { |
---|
684 | n/a | PyErr_Clear(); /* FIXME: propagate error? */ |
---|
685 | n/a | return 0; |
---|
686 | n/a | } |
---|
687 | n/a | ret = PyLong_AsUnsignedLong(value); |
---|
688 | n/a | Py_DECREF(value); |
---|
689 | n/a | return ret; |
---|
690 | n/a | } |
---|
691 | n/a | |
---|
692 | n/a | static HANDLE |
---|
693 | n/a | gethandle(PyObject* obj, const char* name) |
---|
694 | n/a | { |
---|
695 | n/a | PyObject* value; |
---|
696 | n/a | HANDLE ret; |
---|
697 | n/a | |
---|
698 | n/a | value = PyObject_GetAttrString(obj, name); |
---|
699 | n/a | if (! value) { |
---|
700 | n/a | PyErr_Clear(); /* FIXME: propagate error? */ |
---|
701 | n/a | return NULL; |
---|
702 | n/a | } |
---|
703 | n/a | if (value == Py_None) |
---|
704 | n/a | ret = NULL; |
---|
705 | n/a | else |
---|
706 | n/a | ret = PYNUM_TO_HANDLE(value); |
---|
707 | n/a | Py_DECREF(value); |
---|
708 | n/a | return ret; |
---|
709 | n/a | } |
---|
710 | n/a | |
---|
711 | n/a | static PyObject* |
---|
712 | n/a | getenvironment(PyObject* environment) |
---|
713 | n/a | { |
---|
714 | n/a | Py_ssize_t i, envsize, totalsize; |
---|
715 | n/a | Py_UCS4 *buffer = NULL, *p, *end; |
---|
716 | n/a | PyObject *keys, *values, *res; |
---|
717 | n/a | |
---|
718 | n/a | /* convert environment dictionary to windows environment string */ |
---|
719 | n/a | if (! PyMapping_Check(environment)) { |
---|
720 | n/a | PyErr_SetString( |
---|
721 | n/a | PyExc_TypeError, "environment must be dictionary or None"); |
---|
722 | n/a | return NULL; |
---|
723 | n/a | } |
---|
724 | n/a | |
---|
725 | n/a | envsize = PyMapping_Length(environment); |
---|
726 | n/a | |
---|
727 | n/a | keys = PyMapping_Keys(environment); |
---|
728 | n/a | values = PyMapping_Values(environment); |
---|
729 | n/a | if (!keys || !values) |
---|
730 | n/a | goto error; |
---|
731 | n/a | |
---|
732 | n/a | totalsize = 1; /* trailing null character */ |
---|
733 | n/a | for (i = 0; i < envsize; i++) { |
---|
734 | n/a | PyObject* key = PyList_GET_ITEM(keys, i); |
---|
735 | n/a | PyObject* value = PyList_GET_ITEM(values, i); |
---|
736 | n/a | |
---|
737 | n/a | if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) { |
---|
738 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
739 | n/a | "environment can only contain strings"); |
---|
740 | n/a | goto error; |
---|
741 | n/a | } |
---|
742 | n/a | if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(key) - 1) { |
---|
743 | n/a | PyErr_SetString(PyExc_OverflowError, "environment too long"); |
---|
744 | n/a | goto error; |
---|
745 | n/a | } |
---|
746 | n/a | totalsize += PyUnicode_GET_LENGTH(key) + 1; /* +1 for '=' */ |
---|
747 | n/a | if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(value) - 1) { |
---|
748 | n/a | PyErr_SetString(PyExc_OverflowError, "environment too long"); |
---|
749 | n/a | goto error; |
---|
750 | n/a | } |
---|
751 | n/a | totalsize += PyUnicode_GET_LENGTH(value) + 1; /* +1 for '\0' */ |
---|
752 | n/a | } |
---|
753 | n/a | |
---|
754 | n/a | buffer = PyMem_NEW(Py_UCS4, totalsize); |
---|
755 | n/a | if (! buffer) { |
---|
756 | n/a | PyErr_NoMemory(); |
---|
757 | n/a | goto error; |
---|
758 | n/a | } |
---|
759 | n/a | p = buffer; |
---|
760 | n/a | end = buffer + totalsize; |
---|
761 | n/a | |
---|
762 | n/a | for (i = 0; i < envsize; i++) { |
---|
763 | n/a | PyObject* key = PyList_GET_ITEM(keys, i); |
---|
764 | n/a | PyObject* value = PyList_GET_ITEM(values, i); |
---|
765 | n/a | if (!PyUnicode_AsUCS4(key, p, end - p, 0)) |
---|
766 | n/a | goto error; |
---|
767 | n/a | p += PyUnicode_GET_LENGTH(key); |
---|
768 | n/a | *p++ = '='; |
---|
769 | n/a | if (!PyUnicode_AsUCS4(value, p, end - p, 0)) |
---|
770 | n/a | goto error; |
---|
771 | n/a | p += PyUnicode_GET_LENGTH(value); |
---|
772 | n/a | *p++ = '\0'; |
---|
773 | n/a | } |
---|
774 | n/a | |
---|
775 | n/a | /* add trailing null byte */ |
---|
776 | n/a | *p++ = '\0'; |
---|
777 | n/a | assert(p == end); |
---|
778 | n/a | |
---|
779 | n/a | Py_XDECREF(keys); |
---|
780 | n/a | Py_XDECREF(values); |
---|
781 | n/a | |
---|
782 | n/a | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, p - buffer); |
---|
783 | n/a | PyMem_Free(buffer); |
---|
784 | n/a | return res; |
---|
785 | n/a | |
---|
786 | n/a | error: |
---|
787 | n/a | PyMem_Free(buffer); |
---|
788 | n/a | Py_XDECREF(keys); |
---|
789 | n/a | Py_XDECREF(values); |
---|
790 | n/a | return NULL; |
---|
791 | n/a | } |
---|
792 | n/a | |
---|
793 | n/a | /*[clinic input] |
---|
794 | n/a | _winapi.CreateProcess |
---|
795 | n/a | |
---|
796 | n/a | application_name: Py_UNICODE(accept={str, NoneType}) |
---|
797 | n/a | command_line: Py_UNICODE(accept={str, NoneType}) |
---|
798 | n/a | proc_attrs: object |
---|
799 | n/a | Ignored internally, can be None. |
---|
800 | n/a | thread_attrs: object |
---|
801 | n/a | Ignored internally, can be None. |
---|
802 | n/a | inherit_handles: BOOL |
---|
803 | n/a | creation_flags: DWORD |
---|
804 | n/a | env_mapping: object |
---|
805 | n/a | current_directory: Py_UNICODE(accept={str, NoneType}) |
---|
806 | n/a | startup_info: object |
---|
807 | n/a | / |
---|
808 | n/a | |
---|
809 | n/a | Create a new process and its primary thread. |
---|
810 | n/a | |
---|
811 | n/a | The return value is a tuple of the process handle, thread handle, |
---|
812 | n/a | process ID, and thread ID. |
---|
813 | n/a | [clinic start generated code]*/ |
---|
814 | n/a | |
---|
815 | n/a | static PyObject * |
---|
816 | n/a | _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name, |
---|
817 | n/a | Py_UNICODE *command_line, PyObject *proc_attrs, |
---|
818 | n/a | PyObject *thread_attrs, BOOL inherit_handles, |
---|
819 | n/a | DWORD creation_flags, PyObject *env_mapping, |
---|
820 | n/a | Py_UNICODE *current_directory, |
---|
821 | n/a | PyObject *startup_info) |
---|
822 | n/a | /*[clinic end generated code: output=4652a33aff4b0ae1 input=4a43b05038d639bb]*/ |
---|
823 | n/a | { |
---|
824 | n/a | BOOL result; |
---|
825 | n/a | PROCESS_INFORMATION pi; |
---|
826 | n/a | STARTUPINFOW si; |
---|
827 | n/a | PyObject* environment; |
---|
828 | n/a | wchar_t *wenvironment; |
---|
829 | n/a | |
---|
830 | n/a | ZeroMemory(&si, sizeof(si)); |
---|
831 | n/a | si.cb = sizeof(si); |
---|
832 | n/a | |
---|
833 | n/a | /* note: we only support a small subset of all SI attributes */ |
---|
834 | n/a | si.dwFlags = getulong(startup_info, "dwFlags"); |
---|
835 | n/a | si.wShowWindow = (WORD)getulong(startup_info, "wShowWindow"); |
---|
836 | n/a | si.hStdInput = gethandle(startup_info, "hStdInput"); |
---|
837 | n/a | si.hStdOutput = gethandle(startup_info, "hStdOutput"); |
---|
838 | n/a | si.hStdError = gethandle(startup_info, "hStdError"); |
---|
839 | n/a | if (PyErr_Occurred()) |
---|
840 | n/a | return NULL; |
---|
841 | n/a | |
---|
842 | n/a | if (env_mapping != Py_None) { |
---|
843 | n/a | environment = getenvironment(env_mapping); |
---|
844 | n/a | if (! environment) |
---|
845 | n/a | return NULL; |
---|
846 | n/a | wenvironment = PyUnicode_AsUnicode(environment); |
---|
847 | n/a | if (wenvironment == NULL) |
---|
848 | n/a | { |
---|
849 | n/a | Py_XDECREF(environment); |
---|
850 | n/a | return NULL; |
---|
851 | n/a | } |
---|
852 | n/a | } |
---|
853 | n/a | else { |
---|
854 | n/a | environment = NULL; |
---|
855 | n/a | wenvironment = NULL; |
---|
856 | n/a | } |
---|
857 | n/a | |
---|
858 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
859 | n/a | result = CreateProcessW(application_name, |
---|
860 | n/a | command_line, |
---|
861 | n/a | NULL, |
---|
862 | n/a | NULL, |
---|
863 | n/a | inherit_handles, |
---|
864 | n/a | creation_flags | CREATE_UNICODE_ENVIRONMENT, |
---|
865 | n/a | wenvironment, |
---|
866 | n/a | current_directory, |
---|
867 | n/a | &si, |
---|
868 | n/a | &pi); |
---|
869 | n/a | Py_END_ALLOW_THREADS |
---|
870 | n/a | |
---|
871 | n/a | Py_XDECREF(environment); |
---|
872 | n/a | |
---|
873 | n/a | if (! result) |
---|
874 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
---|
875 | n/a | |
---|
876 | n/a | return Py_BuildValue("NNkk", |
---|
877 | n/a | HANDLE_TO_PYNUM(pi.hProcess), |
---|
878 | n/a | HANDLE_TO_PYNUM(pi.hThread), |
---|
879 | n/a | pi.dwProcessId, |
---|
880 | n/a | pi.dwThreadId); |
---|
881 | n/a | } |
---|
882 | n/a | |
---|
883 | n/a | /*[clinic input] |
---|
884 | n/a | _winapi.DuplicateHandle -> HANDLE |
---|
885 | n/a | |
---|
886 | n/a | source_process_handle: HANDLE |
---|
887 | n/a | source_handle: HANDLE |
---|
888 | n/a | target_process_handle: HANDLE |
---|
889 | n/a | desired_access: DWORD |
---|
890 | n/a | inherit_handle: BOOL |
---|
891 | n/a | options: DWORD = 0 |
---|
892 | n/a | / |
---|
893 | n/a | |
---|
894 | n/a | Return a duplicate handle object. |
---|
895 | n/a | |
---|
896 | n/a | The duplicate handle refers to the same object as the original |
---|
897 | n/a | handle. Therefore, any changes to the object are reflected |
---|
898 | n/a | through both handles. |
---|
899 | n/a | [clinic start generated code]*/ |
---|
900 | n/a | |
---|
901 | n/a | static HANDLE |
---|
902 | n/a | _winapi_DuplicateHandle_impl(PyObject *module, HANDLE source_process_handle, |
---|
903 | n/a | HANDLE source_handle, |
---|
904 | n/a | HANDLE target_process_handle, |
---|
905 | n/a | DWORD desired_access, BOOL inherit_handle, |
---|
906 | n/a | DWORD options) |
---|
907 | n/a | /*[clinic end generated code: output=ad9711397b5dcd4e input=b933e3f2356a8c12]*/ |
---|
908 | n/a | { |
---|
909 | n/a | HANDLE target_handle; |
---|
910 | n/a | BOOL result; |
---|
911 | n/a | |
---|
912 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
913 | n/a | result = DuplicateHandle( |
---|
914 | n/a | source_process_handle, |
---|
915 | n/a | source_handle, |
---|
916 | n/a | target_process_handle, |
---|
917 | n/a | &target_handle, |
---|
918 | n/a | desired_access, |
---|
919 | n/a | inherit_handle, |
---|
920 | n/a | options |
---|
921 | n/a | ); |
---|
922 | n/a | Py_END_ALLOW_THREADS |
---|
923 | n/a | |
---|
924 | n/a | if (! result) { |
---|
925 | n/a | PyErr_SetFromWindowsErr(GetLastError()); |
---|
926 | n/a | return INVALID_HANDLE_VALUE; |
---|
927 | n/a | } |
---|
928 | n/a | |
---|
929 | n/a | return target_handle; |
---|
930 | n/a | } |
---|
931 | n/a | |
---|
932 | n/a | /*[clinic input] |
---|
933 | n/a | _winapi.ExitProcess |
---|
934 | n/a | |
---|
935 | n/a | ExitCode: UINT |
---|
936 | n/a | / |
---|
937 | n/a | |
---|
938 | n/a | [clinic start generated code]*/ |
---|
939 | n/a | |
---|
940 | n/a | static PyObject * |
---|
941 | n/a | _winapi_ExitProcess_impl(PyObject *module, UINT ExitCode) |
---|
942 | n/a | /*[clinic end generated code: output=a387deb651175301 input=4f05466a9406c558]*/ |
---|
943 | n/a | { |
---|
944 | n/a | #if defined(Py_DEBUG) |
---|
945 | n/a | SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT| |
---|
946 | n/a | SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX); |
---|
947 | n/a | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
---|
948 | n/a | #endif |
---|
949 | n/a | |
---|
950 | n/a | ExitProcess(ExitCode); |
---|
951 | n/a | |
---|
952 | n/a | return NULL; |
---|
953 | n/a | } |
---|
954 | n/a | |
---|
955 | n/a | /*[clinic input] |
---|
956 | n/a | _winapi.GetCurrentProcess -> HANDLE |
---|
957 | n/a | |
---|
958 | n/a | Return a handle object for the current process. |
---|
959 | n/a | [clinic start generated code]*/ |
---|
960 | n/a | |
---|
961 | n/a | static HANDLE |
---|
962 | n/a | _winapi_GetCurrentProcess_impl(PyObject *module) |
---|
963 | n/a | /*[clinic end generated code: output=ddeb4dd2ffadf344 input=b213403fd4b96b41]*/ |
---|
964 | n/a | { |
---|
965 | n/a | return GetCurrentProcess(); |
---|
966 | n/a | } |
---|
967 | n/a | |
---|
968 | n/a | /*[clinic input] |
---|
969 | n/a | _winapi.GetExitCodeProcess -> DWORD |
---|
970 | n/a | |
---|
971 | n/a | process: HANDLE |
---|
972 | n/a | / |
---|
973 | n/a | |
---|
974 | n/a | Return the termination status of the specified process. |
---|
975 | n/a | [clinic start generated code]*/ |
---|
976 | n/a | |
---|
977 | n/a | static DWORD |
---|
978 | n/a | _winapi_GetExitCodeProcess_impl(PyObject *module, HANDLE process) |
---|
979 | n/a | /*[clinic end generated code: output=b4620bdf2bccf36b input=61b6bfc7dc2ee374]*/ |
---|
980 | n/a | { |
---|
981 | n/a | DWORD exit_code; |
---|
982 | n/a | BOOL result; |
---|
983 | n/a | |
---|
984 | n/a | result = GetExitCodeProcess(process, &exit_code); |
---|
985 | n/a | |
---|
986 | n/a | if (! result) { |
---|
987 | n/a | PyErr_SetFromWindowsErr(GetLastError()); |
---|
988 | n/a | exit_code = DWORD_MAX; |
---|
989 | n/a | } |
---|
990 | n/a | |
---|
991 | n/a | return exit_code; |
---|
992 | n/a | } |
---|
993 | n/a | |
---|
994 | n/a | /*[clinic input] |
---|
995 | n/a | _winapi.GetLastError -> DWORD |
---|
996 | n/a | [clinic start generated code]*/ |
---|
997 | n/a | |
---|
998 | n/a | static DWORD |
---|
999 | n/a | _winapi_GetLastError_impl(PyObject *module) |
---|
1000 | n/a | /*[clinic end generated code: output=8585b827cb1a92c5 input=62d47fb9bce038ba]*/ |
---|
1001 | n/a | { |
---|
1002 | n/a | return GetLastError(); |
---|
1003 | n/a | } |
---|
1004 | n/a | |
---|
1005 | n/a | /*[clinic input] |
---|
1006 | n/a | _winapi.GetModuleFileName |
---|
1007 | n/a | |
---|
1008 | n/a | module_handle: HMODULE |
---|
1009 | n/a | / |
---|
1010 | n/a | |
---|
1011 | n/a | Return the fully-qualified path for the file that contains module. |
---|
1012 | n/a | |
---|
1013 | n/a | The module must have been loaded by the current process. |
---|
1014 | n/a | |
---|
1015 | n/a | The module parameter should be a handle to the loaded module |
---|
1016 | n/a | whose path is being requested. If this parameter is 0, |
---|
1017 | n/a | GetModuleFileName retrieves the path of the executable file |
---|
1018 | n/a | of the current process. |
---|
1019 | n/a | [clinic start generated code]*/ |
---|
1020 | n/a | |
---|
1021 | n/a | static PyObject * |
---|
1022 | n/a | _winapi_GetModuleFileName_impl(PyObject *module, HMODULE module_handle) |
---|
1023 | n/a | /*[clinic end generated code: output=85b4b728c5160306 input=6d66ff7deca5d11f]*/ |
---|
1024 | n/a | { |
---|
1025 | n/a | BOOL result; |
---|
1026 | n/a | WCHAR filename[MAX_PATH]; |
---|
1027 | n/a | |
---|
1028 | n/a | result = GetModuleFileNameW(module_handle, filename, MAX_PATH); |
---|
1029 | n/a | filename[MAX_PATH-1] = '\0'; |
---|
1030 | n/a | |
---|
1031 | n/a | if (! result) |
---|
1032 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
---|
1033 | n/a | |
---|
1034 | n/a | return PyUnicode_FromWideChar(filename, wcslen(filename)); |
---|
1035 | n/a | } |
---|
1036 | n/a | |
---|
1037 | n/a | /*[clinic input] |
---|
1038 | n/a | _winapi.GetStdHandle -> HANDLE |
---|
1039 | n/a | |
---|
1040 | n/a | std_handle: DWORD |
---|
1041 | n/a | One of STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE. |
---|
1042 | n/a | / |
---|
1043 | n/a | |
---|
1044 | n/a | Return a handle to the specified standard device. |
---|
1045 | n/a | |
---|
1046 | n/a | The integer associated with the handle object is returned. |
---|
1047 | n/a | [clinic start generated code]*/ |
---|
1048 | n/a | |
---|
1049 | n/a | static HANDLE |
---|
1050 | n/a | _winapi_GetStdHandle_impl(PyObject *module, DWORD std_handle) |
---|
1051 | n/a | /*[clinic end generated code: output=0e613001e73ab614 input=07016b06a2fc8826]*/ |
---|
1052 | n/a | { |
---|
1053 | n/a | HANDLE handle; |
---|
1054 | n/a | |
---|
1055 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1056 | n/a | handle = GetStdHandle(std_handle); |
---|
1057 | n/a | Py_END_ALLOW_THREADS |
---|
1058 | n/a | |
---|
1059 | n/a | if (handle == INVALID_HANDLE_VALUE) |
---|
1060 | n/a | PyErr_SetFromWindowsErr(GetLastError()); |
---|
1061 | n/a | |
---|
1062 | n/a | return handle; |
---|
1063 | n/a | } |
---|
1064 | n/a | |
---|
1065 | n/a | /*[clinic input] |
---|
1066 | n/a | _winapi.GetVersion -> long |
---|
1067 | n/a | |
---|
1068 | n/a | Return the version number of the current operating system. |
---|
1069 | n/a | [clinic start generated code]*/ |
---|
1070 | n/a | |
---|
1071 | n/a | static long |
---|
1072 | n/a | _winapi_GetVersion_impl(PyObject *module) |
---|
1073 | n/a | /*[clinic end generated code: output=e41f0db5a3b82682 input=e21dff8d0baeded2]*/ |
---|
1074 | n/a | /* Disable deprecation warnings about GetVersionEx as the result is |
---|
1075 | n/a | being passed straight through to the caller, who is responsible for |
---|
1076 | n/a | using it correctly. */ |
---|
1077 | n/a | #pragma warning(push) |
---|
1078 | n/a | #pragma warning(disable:4996) |
---|
1079 | n/a | |
---|
1080 | n/a | { |
---|
1081 | n/a | return GetVersion(); |
---|
1082 | n/a | } |
---|
1083 | n/a | |
---|
1084 | n/a | #pragma warning(pop) |
---|
1085 | n/a | |
---|
1086 | n/a | /*[clinic input] |
---|
1087 | n/a | _winapi.OpenProcess -> HANDLE |
---|
1088 | n/a | |
---|
1089 | n/a | desired_access: DWORD |
---|
1090 | n/a | inherit_handle: BOOL |
---|
1091 | n/a | process_id: DWORD |
---|
1092 | n/a | / |
---|
1093 | n/a | [clinic start generated code]*/ |
---|
1094 | n/a | |
---|
1095 | n/a | static HANDLE |
---|
1096 | n/a | _winapi_OpenProcess_impl(PyObject *module, DWORD desired_access, |
---|
1097 | n/a | BOOL inherit_handle, DWORD process_id) |
---|
1098 | n/a | /*[clinic end generated code: output=b42b6b81ea5a0fc3 input=ec98c4cf4ea2ec36]*/ |
---|
1099 | n/a | { |
---|
1100 | n/a | HANDLE handle; |
---|
1101 | n/a | |
---|
1102 | n/a | handle = OpenProcess(desired_access, inherit_handle, process_id); |
---|
1103 | n/a | if (handle == NULL) { |
---|
1104 | n/a | PyErr_SetFromWindowsErr(0); |
---|
1105 | n/a | handle = INVALID_HANDLE_VALUE; |
---|
1106 | n/a | } |
---|
1107 | n/a | |
---|
1108 | n/a | return handle; |
---|
1109 | n/a | } |
---|
1110 | n/a | |
---|
1111 | n/a | /*[clinic input] |
---|
1112 | n/a | _winapi.PeekNamedPipe |
---|
1113 | n/a | |
---|
1114 | n/a | handle: HANDLE |
---|
1115 | n/a | size: int = 0 |
---|
1116 | n/a | / |
---|
1117 | n/a | [clinic start generated code]*/ |
---|
1118 | n/a | |
---|
1119 | n/a | static PyObject * |
---|
1120 | n/a | _winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size) |
---|
1121 | n/a | /*[clinic end generated code: output=d0c3e29e49d323dd input=c7aa53bfbce69d70]*/ |
---|
1122 | n/a | { |
---|
1123 | n/a | PyObject *buf = NULL; |
---|
1124 | n/a | DWORD nread, navail, nleft; |
---|
1125 | n/a | BOOL ret; |
---|
1126 | n/a | |
---|
1127 | n/a | if (size < 0) { |
---|
1128 | n/a | PyErr_SetString(PyExc_ValueError, "negative size"); |
---|
1129 | n/a | return NULL; |
---|
1130 | n/a | } |
---|
1131 | n/a | |
---|
1132 | n/a | if (size) { |
---|
1133 | n/a | buf = PyBytes_FromStringAndSize(NULL, size); |
---|
1134 | n/a | if (!buf) |
---|
1135 | n/a | return NULL; |
---|
1136 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1137 | n/a | ret = PeekNamedPipe(handle, PyBytes_AS_STRING(buf), size, &nread, |
---|
1138 | n/a | &navail, &nleft); |
---|
1139 | n/a | Py_END_ALLOW_THREADS |
---|
1140 | n/a | if (!ret) { |
---|
1141 | n/a | Py_DECREF(buf); |
---|
1142 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
---|
1143 | n/a | } |
---|
1144 | n/a | if (_PyBytes_Resize(&buf, nread)) |
---|
1145 | n/a | return NULL; |
---|
1146 | n/a | return Py_BuildValue("Nii", buf, navail, nleft); |
---|
1147 | n/a | } |
---|
1148 | n/a | else { |
---|
1149 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1150 | n/a | ret = PeekNamedPipe(handle, NULL, 0, NULL, &navail, &nleft); |
---|
1151 | n/a | Py_END_ALLOW_THREADS |
---|
1152 | n/a | if (!ret) { |
---|
1153 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
---|
1154 | n/a | } |
---|
1155 | n/a | return Py_BuildValue("ii", navail, nleft); |
---|
1156 | n/a | } |
---|
1157 | n/a | } |
---|
1158 | n/a | |
---|
1159 | n/a | /*[clinic input] |
---|
1160 | n/a | _winapi.ReadFile |
---|
1161 | n/a | |
---|
1162 | n/a | handle: HANDLE |
---|
1163 | n/a | size: int |
---|
1164 | n/a | overlapped as use_overlapped: int(c_default='0') = False |
---|
1165 | n/a | [clinic start generated code]*/ |
---|
1166 | n/a | |
---|
1167 | n/a | static PyObject * |
---|
1168 | n/a | _winapi_ReadFile_impl(PyObject *module, HANDLE handle, int size, |
---|
1169 | n/a | int use_overlapped) |
---|
1170 | n/a | /*[clinic end generated code: output=492029ca98161d84 input=8dd810194e86ac7d]*/ |
---|
1171 | n/a | { |
---|
1172 | n/a | DWORD nread; |
---|
1173 | n/a | PyObject *buf; |
---|
1174 | n/a | BOOL ret; |
---|
1175 | n/a | DWORD err; |
---|
1176 | n/a | OverlappedObject *overlapped = NULL; |
---|
1177 | n/a | |
---|
1178 | n/a | buf = PyBytes_FromStringAndSize(NULL, size); |
---|
1179 | n/a | if (!buf) |
---|
1180 | n/a | return NULL; |
---|
1181 | n/a | if (use_overlapped) { |
---|
1182 | n/a | overlapped = new_overlapped(handle); |
---|
1183 | n/a | if (!overlapped) { |
---|
1184 | n/a | Py_DECREF(buf); |
---|
1185 | n/a | return NULL; |
---|
1186 | n/a | } |
---|
1187 | n/a | /* Steals reference to buf */ |
---|
1188 | n/a | overlapped->read_buffer = buf; |
---|
1189 | n/a | } |
---|
1190 | n/a | |
---|
1191 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1192 | n/a | ret = ReadFile(handle, PyBytes_AS_STRING(buf), size, &nread, |
---|
1193 | n/a | overlapped ? &overlapped->overlapped : NULL); |
---|
1194 | n/a | Py_END_ALLOW_THREADS |
---|
1195 | n/a | |
---|
1196 | n/a | err = ret ? 0 : GetLastError(); |
---|
1197 | n/a | |
---|
1198 | n/a | if (overlapped) { |
---|
1199 | n/a | if (!ret) { |
---|
1200 | n/a | if (err == ERROR_IO_PENDING) |
---|
1201 | n/a | overlapped->pending = 1; |
---|
1202 | n/a | else if (err != ERROR_MORE_DATA) { |
---|
1203 | n/a | Py_DECREF(overlapped); |
---|
1204 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
---|
1205 | n/a | } |
---|
1206 | n/a | } |
---|
1207 | n/a | return Py_BuildValue("NI", (PyObject *) overlapped, err); |
---|
1208 | n/a | } |
---|
1209 | n/a | |
---|
1210 | n/a | if (!ret && err != ERROR_MORE_DATA) { |
---|
1211 | n/a | Py_DECREF(buf); |
---|
1212 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
---|
1213 | n/a | } |
---|
1214 | n/a | if (_PyBytes_Resize(&buf, nread)) |
---|
1215 | n/a | return NULL; |
---|
1216 | n/a | return Py_BuildValue("NI", buf, err); |
---|
1217 | n/a | } |
---|
1218 | n/a | |
---|
1219 | n/a | /*[clinic input] |
---|
1220 | n/a | _winapi.SetNamedPipeHandleState |
---|
1221 | n/a | |
---|
1222 | n/a | named_pipe: HANDLE |
---|
1223 | n/a | mode: object |
---|
1224 | n/a | max_collection_count: object |
---|
1225 | n/a | collect_data_timeout: object |
---|
1226 | n/a | / |
---|
1227 | n/a | [clinic start generated code]*/ |
---|
1228 | n/a | |
---|
1229 | n/a | static PyObject * |
---|
1230 | n/a | _winapi_SetNamedPipeHandleState_impl(PyObject *module, HANDLE named_pipe, |
---|
1231 | n/a | PyObject *mode, |
---|
1232 | n/a | PyObject *max_collection_count, |
---|
1233 | n/a | PyObject *collect_data_timeout) |
---|
1234 | n/a | /*[clinic end generated code: output=f2129d222cbfa095 input=9142d72163d0faa6]*/ |
---|
1235 | n/a | { |
---|
1236 | n/a | PyObject *oArgs[3] = {mode, max_collection_count, collect_data_timeout}; |
---|
1237 | n/a | DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL}; |
---|
1238 | n/a | int i; |
---|
1239 | n/a | |
---|
1240 | n/a | PyErr_Clear(); |
---|
1241 | n/a | |
---|
1242 | n/a | for (i = 0 ; i < 3 ; i++) { |
---|
1243 | n/a | if (oArgs[i] != Py_None) { |
---|
1244 | n/a | dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]); |
---|
1245 | n/a | if (PyErr_Occurred()) |
---|
1246 | n/a | return NULL; |
---|
1247 | n/a | pArgs[i] = &dwArgs[i]; |
---|
1248 | n/a | } |
---|
1249 | n/a | } |
---|
1250 | n/a | |
---|
1251 | n/a | if (!SetNamedPipeHandleState(named_pipe, pArgs[0], pArgs[1], pArgs[2])) |
---|
1252 | n/a | return PyErr_SetFromWindowsErr(0); |
---|
1253 | n/a | |
---|
1254 | n/a | Py_RETURN_NONE; |
---|
1255 | n/a | } |
---|
1256 | n/a | |
---|
1257 | n/a | |
---|
1258 | n/a | /*[clinic input] |
---|
1259 | n/a | _winapi.TerminateProcess |
---|
1260 | n/a | |
---|
1261 | n/a | handle: HANDLE |
---|
1262 | n/a | exit_code: UINT |
---|
1263 | n/a | / |
---|
1264 | n/a | |
---|
1265 | n/a | Terminate the specified process and all of its threads. |
---|
1266 | n/a | [clinic start generated code]*/ |
---|
1267 | n/a | |
---|
1268 | n/a | static PyObject * |
---|
1269 | n/a | _winapi_TerminateProcess_impl(PyObject *module, HANDLE handle, |
---|
1270 | n/a | UINT exit_code) |
---|
1271 | n/a | /*[clinic end generated code: output=f4e99ac3f0b1f34a input=d6bc0aa1ee3bb4df]*/ |
---|
1272 | n/a | { |
---|
1273 | n/a | BOOL result; |
---|
1274 | n/a | |
---|
1275 | n/a | result = TerminateProcess(handle, exit_code); |
---|
1276 | n/a | |
---|
1277 | n/a | if (! result) |
---|
1278 | n/a | return PyErr_SetFromWindowsErr(GetLastError()); |
---|
1279 | n/a | |
---|
1280 | n/a | Py_RETURN_NONE; |
---|
1281 | n/a | } |
---|
1282 | n/a | |
---|
1283 | n/a | /*[clinic input] |
---|
1284 | n/a | _winapi.WaitNamedPipe |
---|
1285 | n/a | |
---|
1286 | n/a | name: LPCTSTR |
---|
1287 | n/a | timeout: DWORD |
---|
1288 | n/a | / |
---|
1289 | n/a | [clinic start generated code]*/ |
---|
1290 | n/a | |
---|
1291 | n/a | static PyObject * |
---|
1292 | n/a | _winapi_WaitNamedPipe_impl(PyObject *module, LPCTSTR name, DWORD timeout) |
---|
1293 | n/a | /*[clinic end generated code: output=c2866f4439b1fe38 input=36fc781291b1862c]*/ |
---|
1294 | n/a | { |
---|
1295 | n/a | BOOL success; |
---|
1296 | n/a | |
---|
1297 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1298 | n/a | success = WaitNamedPipe(name, timeout); |
---|
1299 | n/a | Py_END_ALLOW_THREADS |
---|
1300 | n/a | |
---|
1301 | n/a | if (!success) |
---|
1302 | n/a | return PyErr_SetFromWindowsErr(0); |
---|
1303 | n/a | |
---|
1304 | n/a | Py_RETURN_NONE; |
---|
1305 | n/a | } |
---|
1306 | n/a | |
---|
1307 | n/a | /*[clinic input] |
---|
1308 | n/a | _winapi.WaitForMultipleObjects |
---|
1309 | n/a | |
---|
1310 | n/a | handle_seq: object |
---|
1311 | n/a | wait_flag: BOOL |
---|
1312 | n/a | milliseconds: DWORD(c_default='INFINITE') = _winapi.INFINITE |
---|
1313 | n/a | / |
---|
1314 | n/a | [clinic start generated code]*/ |
---|
1315 | n/a | |
---|
1316 | n/a | static PyObject * |
---|
1317 | n/a | _winapi_WaitForMultipleObjects_impl(PyObject *module, PyObject *handle_seq, |
---|
1318 | n/a | BOOL wait_flag, DWORD milliseconds) |
---|
1319 | n/a | /*[clinic end generated code: output=295e3f00b8e45899 input=36f76ca057cd28a0]*/ |
---|
1320 | n/a | { |
---|
1321 | n/a | DWORD result; |
---|
1322 | n/a | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
---|
1323 | n/a | HANDLE sigint_event = NULL; |
---|
1324 | n/a | Py_ssize_t nhandles, i; |
---|
1325 | n/a | |
---|
1326 | n/a | if (!PySequence_Check(handle_seq)) { |
---|
1327 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1328 | n/a | "sequence type expected, got '%s'", |
---|
1329 | n/a | Py_TYPE(handle_seq)->tp_name); |
---|
1330 | n/a | return NULL; |
---|
1331 | n/a | } |
---|
1332 | n/a | nhandles = PySequence_Length(handle_seq); |
---|
1333 | n/a | if (nhandles == -1) |
---|
1334 | n/a | return NULL; |
---|
1335 | n/a | if (nhandles < 0 || nhandles >= MAXIMUM_WAIT_OBJECTS - 1) { |
---|
1336 | n/a | PyErr_Format(PyExc_ValueError, |
---|
1337 | n/a | "need at most %zd handles, got a sequence of length %zd", |
---|
1338 | n/a | MAXIMUM_WAIT_OBJECTS - 1, nhandles); |
---|
1339 | n/a | return NULL; |
---|
1340 | n/a | } |
---|
1341 | n/a | for (i = 0; i < nhandles; i++) { |
---|
1342 | n/a | HANDLE h; |
---|
1343 | n/a | PyObject *v = PySequence_GetItem(handle_seq, i); |
---|
1344 | n/a | if (v == NULL) |
---|
1345 | n/a | return NULL; |
---|
1346 | n/a | if (!PyArg_Parse(v, F_HANDLE, &h)) { |
---|
1347 | n/a | Py_DECREF(v); |
---|
1348 | n/a | return NULL; |
---|
1349 | n/a | } |
---|
1350 | n/a | handles[i] = h; |
---|
1351 | n/a | Py_DECREF(v); |
---|
1352 | n/a | } |
---|
1353 | n/a | /* If this is the main thread then make the wait interruptible |
---|
1354 | n/a | by Ctrl-C unless we are waiting for *all* handles */ |
---|
1355 | n/a | if (!wait_flag && _PyOS_IsMainThread()) { |
---|
1356 | n/a | sigint_event = _PyOS_SigintEvent(); |
---|
1357 | n/a | assert(sigint_event != NULL); |
---|
1358 | n/a | handles[nhandles++] = sigint_event; |
---|
1359 | n/a | } |
---|
1360 | n/a | |
---|
1361 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1362 | n/a | if (sigint_event != NULL) |
---|
1363 | n/a | ResetEvent(sigint_event); |
---|
1364 | n/a | result = WaitForMultipleObjects((DWORD) nhandles, handles, |
---|
1365 | n/a | wait_flag, milliseconds); |
---|
1366 | n/a | Py_END_ALLOW_THREADS |
---|
1367 | n/a | |
---|
1368 | n/a | if (result == WAIT_FAILED) |
---|
1369 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
---|
1370 | n/a | else if (sigint_event != NULL && result == WAIT_OBJECT_0 + nhandles - 1) { |
---|
1371 | n/a | errno = EINTR; |
---|
1372 | n/a | return PyErr_SetFromErrno(PyExc_IOError); |
---|
1373 | n/a | } |
---|
1374 | n/a | |
---|
1375 | n/a | return PyLong_FromLong((int) result); |
---|
1376 | n/a | } |
---|
1377 | n/a | |
---|
1378 | n/a | /*[clinic input] |
---|
1379 | n/a | _winapi.WaitForSingleObject -> long |
---|
1380 | n/a | |
---|
1381 | n/a | handle: HANDLE |
---|
1382 | n/a | milliseconds: DWORD |
---|
1383 | n/a | / |
---|
1384 | n/a | |
---|
1385 | n/a | Wait for a single object. |
---|
1386 | n/a | |
---|
1387 | n/a | Wait until the specified object is in the signaled state or |
---|
1388 | n/a | the time-out interval elapses. The timeout value is specified |
---|
1389 | n/a | in milliseconds. |
---|
1390 | n/a | [clinic start generated code]*/ |
---|
1391 | n/a | |
---|
1392 | n/a | static long |
---|
1393 | n/a | _winapi_WaitForSingleObject_impl(PyObject *module, HANDLE handle, |
---|
1394 | n/a | DWORD milliseconds) |
---|
1395 | n/a | /*[clinic end generated code: output=3c4715d8f1b39859 input=443d1ab076edc7b1]*/ |
---|
1396 | n/a | { |
---|
1397 | n/a | DWORD result; |
---|
1398 | n/a | |
---|
1399 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1400 | n/a | result = WaitForSingleObject(handle, milliseconds); |
---|
1401 | n/a | Py_END_ALLOW_THREADS |
---|
1402 | n/a | |
---|
1403 | n/a | if (result == WAIT_FAILED) { |
---|
1404 | n/a | PyErr_SetFromWindowsErr(GetLastError()); |
---|
1405 | n/a | return -1; |
---|
1406 | n/a | } |
---|
1407 | n/a | |
---|
1408 | n/a | return result; |
---|
1409 | n/a | } |
---|
1410 | n/a | |
---|
1411 | n/a | /*[clinic input] |
---|
1412 | n/a | _winapi.WriteFile |
---|
1413 | n/a | |
---|
1414 | n/a | handle: HANDLE |
---|
1415 | n/a | buffer: object |
---|
1416 | n/a | overlapped as use_overlapped: int(c_default='0') = False |
---|
1417 | n/a | [clinic start generated code]*/ |
---|
1418 | n/a | |
---|
1419 | n/a | static PyObject * |
---|
1420 | n/a | _winapi_WriteFile_impl(PyObject *module, HANDLE handle, PyObject *buffer, |
---|
1421 | n/a | int use_overlapped) |
---|
1422 | n/a | /*[clinic end generated code: output=2ca80f6bf3fa92e3 input=51846a5af52053fd]*/ |
---|
1423 | n/a | { |
---|
1424 | n/a | Py_buffer _buf, *buf; |
---|
1425 | n/a | DWORD len, written; |
---|
1426 | n/a | BOOL ret; |
---|
1427 | n/a | DWORD err; |
---|
1428 | n/a | OverlappedObject *overlapped = NULL; |
---|
1429 | n/a | |
---|
1430 | n/a | if (use_overlapped) { |
---|
1431 | n/a | overlapped = new_overlapped(handle); |
---|
1432 | n/a | if (!overlapped) |
---|
1433 | n/a | return NULL; |
---|
1434 | n/a | buf = &overlapped->write_buffer; |
---|
1435 | n/a | } |
---|
1436 | n/a | else |
---|
1437 | n/a | buf = &_buf; |
---|
1438 | n/a | |
---|
1439 | n/a | if (!PyArg_Parse(buffer, "y*", buf)) { |
---|
1440 | n/a | Py_XDECREF(overlapped); |
---|
1441 | n/a | return NULL; |
---|
1442 | n/a | } |
---|
1443 | n/a | |
---|
1444 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1445 | n/a | len = (DWORD)Py_MIN(buf->len, DWORD_MAX); |
---|
1446 | n/a | ret = WriteFile(handle, buf->buf, len, &written, |
---|
1447 | n/a | overlapped ? &overlapped->overlapped : NULL); |
---|
1448 | n/a | Py_END_ALLOW_THREADS |
---|
1449 | n/a | |
---|
1450 | n/a | err = ret ? 0 : GetLastError(); |
---|
1451 | n/a | |
---|
1452 | n/a | if (overlapped) { |
---|
1453 | n/a | if (!ret) { |
---|
1454 | n/a | if (err == ERROR_IO_PENDING) |
---|
1455 | n/a | overlapped->pending = 1; |
---|
1456 | n/a | else { |
---|
1457 | n/a | Py_DECREF(overlapped); |
---|
1458 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
---|
1459 | n/a | } |
---|
1460 | n/a | } |
---|
1461 | n/a | return Py_BuildValue("NI", (PyObject *) overlapped, err); |
---|
1462 | n/a | } |
---|
1463 | n/a | |
---|
1464 | n/a | PyBuffer_Release(buf); |
---|
1465 | n/a | if (!ret) |
---|
1466 | n/a | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
---|
1467 | n/a | return Py_BuildValue("II", written, err); |
---|
1468 | n/a | } |
---|
1469 | n/a | |
---|
1470 | n/a | |
---|
1471 | n/a | static PyMethodDef winapi_functions[] = { |
---|
1472 | n/a | _WINAPI_CLOSEHANDLE_METHODDEF |
---|
1473 | n/a | _WINAPI_CONNECTNAMEDPIPE_METHODDEF |
---|
1474 | n/a | _WINAPI_CREATEFILE_METHODDEF |
---|
1475 | n/a | _WINAPI_CREATENAMEDPIPE_METHODDEF |
---|
1476 | n/a | _WINAPI_CREATEPIPE_METHODDEF |
---|
1477 | n/a | _WINAPI_CREATEPROCESS_METHODDEF |
---|
1478 | n/a | _WINAPI_CREATEJUNCTION_METHODDEF |
---|
1479 | n/a | _WINAPI_DUPLICATEHANDLE_METHODDEF |
---|
1480 | n/a | _WINAPI_EXITPROCESS_METHODDEF |
---|
1481 | n/a | _WINAPI_GETCURRENTPROCESS_METHODDEF |
---|
1482 | n/a | _WINAPI_GETEXITCODEPROCESS_METHODDEF |
---|
1483 | n/a | _WINAPI_GETLASTERROR_METHODDEF |
---|
1484 | n/a | _WINAPI_GETMODULEFILENAME_METHODDEF |
---|
1485 | n/a | _WINAPI_GETSTDHANDLE_METHODDEF |
---|
1486 | n/a | _WINAPI_GETVERSION_METHODDEF |
---|
1487 | n/a | _WINAPI_OPENPROCESS_METHODDEF |
---|
1488 | n/a | _WINAPI_PEEKNAMEDPIPE_METHODDEF |
---|
1489 | n/a | _WINAPI_READFILE_METHODDEF |
---|
1490 | n/a | _WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF |
---|
1491 | n/a | _WINAPI_TERMINATEPROCESS_METHODDEF |
---|
1492 | n/a | _WINAPI_WAITNAMEDPIPE_METHODDEF |
---|
1493 | n/a | _WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF |
---|
1494 | n/a | _WINAPI_WAITFORSINGLEOBJECT_METHODDEF |
---|
1495 | n/a | _WINAPI_WRITEFILE_METHODDEF |
---|
1496 | n/a | {NULL, NULL} |
---|
1497 | n/a | }; |
---|
1498 | n/a | |
---|
1499 | n/a | static struct PyModuleDef winapi_module = { |
---|
1500 | n/a | PyModuleDef_HEAD_INIT, |
---|
1501 | n/a | "_winapi", |
---|
1502 | n/a | NULL, |
---|
1503 | n/a | -1, |
---|
1504 | n/a | winapi_functions, |
---|
1505 | n/a | NULL, |
---|
1506 | n/a | NULL, |
---|
1507 | n/a | NULL, |
---|
1508 | n/a | NULL |
---|
1509 | n/a | }; |
---|
1510 | n/a | |
---|
1511 | n/a | #define WINAPI_CONSTANT(fmt, con) \ |
---|
1512 | n/a | PyDict_SetItemString(d, #con, Py_BuildValue(fmt, con)) |
---|
1513 | n/a | |
---|
1514 | n/a | PyMODINIT_FUNC |
---|
1515 | n/a | PyInit__winapi(void) |
---|
1516 | n/a | { |
---|
1517 | n/a | PyObject *d; |
---|
1518 | n/a | PyObject *m; |
---|
1519 | n/a | |
---|
1520 | n/a | if (PyType_Ready(&OverlappedType) < 0) |
---|
1521 | n/a | return NULL; |
---|
1522 | n/a | |
---|
1523 | n/a | m = PyModule_Create(&winapi_module); |
---|
1524 | n/a | if (m == NULL) |
---|
1525 | n/a | return NULL; |
---|
1526 | n/a | d = PyModule_GetDict(m); |
---|
1527 | n/a | |
---|
1528 | n/a | PyDict_SetItemString(d, "Overlapped", (PyObject *) &OverlappedType); |
---|
1529 | n/a | |
---|
1530 | n/a | /* constants */ |
---|
1531 | n/a | WINAPI_CONSTANT(F_DWORD, CREATE_NEW_CONSOLE); |
---|
1532 | n/a | WINAPI_CONSTANT(F_DWORD, CREATE_NEW_PROCESS_GROUP); |
---|
1533 | n/a | WINAPI_CONSTANT(F_DWORD, DUPLICATE_SAME_ACCESS); |
---|
1534 | n/a | WINAPI_CONSTANT(F_DWORD, DUPLICATE_CLOSE_SOURCE); |
---|
1535 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS); |
---|
1536 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_BROKEN_PIPE); |
---|
1537 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_IO_PENDING); |
---|
1538 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA); |
---|
1539 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED); |
---|
1540 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES); |
---|
1541 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA); |
---|
1542 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED); |
---|
1543 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_NO_DATA); |
---|
1544 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES); |
---|
1545 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_OPERATION_ABORTED); |
---|
1546 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_BUSY); |
---|
1547 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED); |
---|
1548 | n/a | WINAPI_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT); |
---|
1549 | n/a | WINAPI_CONSTANT(F_DWORD, FILE_FLAG_FIRST_PIPE_INSTANCE); |
---|
1550 | n/a | WINAPI_CONSTANT(F_DWORD, FILE_FLAG_OVERLAPPED); |
---|
1551 | n/a | WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_READ); |
---|
1552 | n/a | WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_WRITE); |
---|
1553 | n/a | WINAPI_CONSTANT(F_DWORD, GENERIC_READ); |
---|
1554 | n/a | WINAPI_CONSTANT(F_DWORD, GENERIC_WRITE); |
---|
1555 | n/a | WINAPI_CONSTANT(F_DWORD, INFINITE); |
---|
1556 | n/a | WINAPI_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER); |
---|
1557 | n/a | WINAPI_CONSTANT(F_DWORD, OPEN_EXISTING); |
---|
1558 | n/a | WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX); |
---|
1559 | n/a | WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND); |
---|
1560 | n/a | WINAPI_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE); |
---|
1561 | n/a | WINAPI_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE); |
---|
1562 | n/a | WINAPI_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES); |
---|
1563 | n/a | WINAPI_CONSTANT(F_DWORD, PIPE_WAIT); |
---|
1564 | n/a | WINAPI_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS); |
---|
1565 | n/a | WINAPI_CONSTANT(F_DWORD, PROCESS_DUP_HANDLE); |
---|
1566 | n/a | WINAPI_CONSTANT(F_DWORD, STARTF_USESHOWWINDOW); |
---|
1567 | n/a | WINAPI_CONSTANT(F_DWORD, STARTF_USESTDHANDLES); |
---|
1568 | n/a | WINAPI_CONSTANT(F_DWORD, STD_INPUT_HANDLE); |
---|
1569 | n/a | WINAPI_CONSTANT(F_DWORD, STD_OUTPUT_HANDLE); |
---|
1570 | n/a | WINAPI_CONSTANT(F_DWORD, STD_ERROR_HANDLE); |
---|
1571 | n/a | WINAPI_CONSTANT(F_DWORD, STILL_ACTIVE); |
---|
1572 | n/a | WINAPI_CONSTANT(F_DWORD, SW_HIDE); |
---|
1573 | n/a | WINAPI_CONSTANT(F_DWORD, WAIT_OBJECT_0); |
---|
1574 | n/a | WINAPI_CONSTANT(F_DWORD, WAIT_ABANDONED_0); |
---|
1575 | n/a | WINAPI_CONSTANT(F_DWORD, WAIT_TIMEOUT); |
---|
1576 | n/a | |
---|
1577 | n/a | WINAPI_CONSTANT("i", NULL); |
---|
1578 | n/a | |
---|
1579 | n/a | return m; |
---|
1580 | n/a | } |
---|