blob: e7b221d888ef8d81d302d27a4c18d233dbee5163 [file] [log] [blame]
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001/*
2 * Support routines from the Windows API
3 *
4 * This module was originally created by merging PC/_subprocess.c with
5 * Modules/_multiprocessing/win32_functions.c.
6 *
7 * Copyright (c) 2004 by Fredrik Lundh <fredrik@pythonware.com>
8 * Copyright (c) 2004 by Secret Labs AB, http://www.pythonware.com
9 * Copyright (c) 2004 by Peter Astrand <astrand@lysator.liu.se>
10 *
11 * By obtaining, using, and/or copying this software and/or its
12 * associated documentation, you agree that you have read, understood,
13 * and will comply with the following terms and conditions:
14 *
15 * Permission to use, copy, modify, and distribute this software and
16 * its associated documentation for any purpose and without fee is
17 * hereby granted, provided that the above copyright notice appears in
18 * all copies, and that both that copyright notice and this permission
19 * notice appear in supporting documentation, and that the name of the
20 * authors not be used in advertising or publicity pertaining to
21 * distribution of the software without specific, written prior
22 * permission.
23 *
24 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
25 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
26 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
27 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
28 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
29 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
30 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 *
32 */
33
34/* Licensed to PSF under a Contributor Agreement. */
35/* See http://www.python.org/2.4/license for licensing details. */
36
37#include "Python.h"
38#include "structmember.h"
39
40#define WINDOWS_LEAN_AND_MEAN
41#include "windows.h"
42#include <crtdbg.h>
Tim Golden0321cf22014-05-05 19:46:17 +010043#include "winreparse.h"
Antoine Pitrou23bba4c2012-04-18 20:51:15 +020044
45#if defined(MS_WIN32) && !defined(MS_WIN64)
46#define HANDLE_TO_PYNUM(handle) \
47 PyLong_FromUnsignedLong((unsigned long) handle)
48#define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLong(obj))
49#define F_POINTER "k"
50#define T_POINTER T_ULONG
51#else
52#define HANDLE_TO_PYNUM(handle) \
53 PyLong_FromUnsignedLongLong((unsigned long long) handle)
54#define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLongLong(obj))
55#define F_POINTER "K"
56#define T_POINTER T_ULONGLONG
57#endif
58
59#define F_HANDLE F_POINTER
60#define F_DWORD "k"
Antoine Pitrou23bba4c2012-04-18 20:51:15 +020061
62#define T_HANDLE T_POINTER
63
64/* Grab CancelIoEx dynamically from kernel32 */
65static int has_CancelIoEx = -1;
66static BOOL (CALLBACK *Py_CancelIoEx)(HANDLE, LPOVERLAPPED);
67
68static int
69check_CancelIoEx()
70{
71 if (has_CancelIoEx == -1)
72 {
73 HINSTANCE hKernel32 = GetModuleHandle("KERNEL32");
74 * (FARPROC *) &Py_CancelIoEx = GetProcAddress(hKernel32,
75 "CancelIoEx");
76 has_CancelIoEx = (Py_CancelIoEx != NULL);
77 }
78 return has_CancelIoEx;
79}
80
81
82/*
83 * A Python object wrapping an OVERLAPPED structure and other useful data
84 * for overlapped I/O
85 */
86
87typedef struct {
88 PyObject_HEAD
89 OVERLAPPED overlapped;
90 /* For convenience, we store the file handle too */
91 HANDLE handle;
92 /* Whether there's I/O in flight */
93 int pending;
94 /* Whether I/O completed successfully */
95 int completed;
96 /* Buffer used for reading (optional) */
97 PyObject *read_buffer;
98 /* Buffer used for writing (optional) */
99 Py_buffer write_buffer;
100} OverlappedObject;
101
102static void
103overlapped_dealloc(OverlappedObject *self)
104{
105 DWORD bytes;
106 int err = GetLastError();
Richard Oudkerk633db6f2013-11-17 13:15:51 +0000107
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200108 if (self->pending) {
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200109 if (check_CancelIoEx() &&
Richard Oudkerk633db6f2013-11-17 13:15:51 +0000110 Py_CancelIoEx(self->handle, &self->overlapped) &&
111 GetOverlappedResult(self->handle, &self->overlapped, &bytes, TRUE))
112 {
113 /* The operation is no longer pending -- nothing to do. */
114 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600115 else if (_Py_IsFinalizing())
Richard Oudkerk633db6f2013-11-17 13:15:51 +0000116 {
117 /* The operation is still pending -- give a warning. This
118 will probably only happen on Windows XP. */
119 PyErr_SetString(PyExc_RuntimeError,
120 "I/O operations still in flight while destroying "
121 "Overlapped object, the process may crash");
122 PyErr_WriteUnraisable(NULL);
123 }
124 else
125 {
126 /* The operation is still pending, but the process is
127 probably about to exit, so we need not worry too much
128 about memory leaks. Leaking self prevents a potential
129 crash. This can happen when a daemon thread is cleaned
130 up at exit -- see #19565. We only expect to get here
131 on Windows XP. */
132 CloseHandle(self->overlapped.hEvent);
133 SetLastError(err);
134 return;
135 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200136 }
Richard Oudkerk633db6f2013-11-17 13:15:51 +0000137
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200138 CloseHandle(self->overlapped.hEvent);
139 SetLastError(err);
140 if (self->write_buffer.obj)
141 PyBuffer_Release(&self->write_buffer);
142 Py_CLEAR(self->read_buffer);
143 PyObject_Del(self);
144}
145
Zachary Waref2244ea2015-05-13 01:22:54 -0500146/*[clinic input]
147module _winapi
148class _winapi.Overlapped "OverlappedObject *" "&OverlappedType"
149[clinic start generated code]*/
150/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c13d3f5fd1dabb84]*/
151
152/*[python input]
153def create_converter(type_, format_unit):
154 name = type_ + '_converter'
155 # registered upon creation by CConverter's metaclass
156 type(name, (CConverter,), {'type': type_, 'format_unit': format_unit})
157
158# format unit differs between platforms for these
159create_converter('HANDLE', '" F_HANDLE "')
160create_converter('HMODULE', '" F_HANDLE "')
161create_converter('LPSECURITY_ATTRIBUTES', '" F_POINTER "')
Davin Pottse895de32019-02-23 22:08:16 -0600162create_converter('LPCVOID', '" F_POINTER "')
Zachary Waref2244ea2015-05-13 01:22:54 -0500163
164create_converter('BOOL', 'i') # F_BOOL used previously (always 'i')
165create_converter('DWORD', 'k') # F_DWORD is always "k" (which is much shorter)
166create_converter('LPCTSTR', 's')
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200167create_converter('LPCWSTR', 'u')
Zachary Waref2244ea2015-05-13 01:22:54 -0500168create_converter('LPWSTR', 'u')
169create_converter('UINT', 'I') # F_UINT used previously (always 'I')
170
171class HANDLE_return_converter(CReturnConverter):
172 type = 'HANDLE'
173
174 def render(self, function, data):
175 self.declare(data)
176 self.err_occurred_if("_return_value == INVALID_HANDLE_VALUE", data)
177 data.return_conversion.append(
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300178 'if (_return_value == NULL) {\n Py_RETURN_NONE;\n}\n')
Zachary Waref2244ea2015-05-13 01:22:54 -0500179 data.return_conversion.append(
180 'return_value = HANDLE_TO_PYNUM(_return_value);\n')
181
182class DWORD_return_converter(CReturnConverter):
183 type = 'DWORD'
184
185 def render(self, function, data):
186 self.declare(data)
Victor Stinner850a18e2017-10-24 16:53:32 -0700187 self.err_occurred_if("_return_value == PY_DWORD_MAX", data)
Zachary Waref2244ea2015-05-13 01:22:54 -0500188 data.return_conversion.append(
189 'return_value = Py_BuildValue("k", _return_value);\n')
Davin Pottse895de32019-02-23 22:08:16 -0600190
191class LPVOID_return_converter(CReturnConverter):
192 type = 'LPVOID'
193
194 def render(self, function, data):
195 self.declare(data)
196 self.err_occurred_if("_return_value == NULL", data)
197 data.return_conversion.append(
198 'return_value = HANDLE_TO_PYNUM(_return_value);\n')
Zachary Waref2244ea2015-05-13 01:22:54 -0500199[python start generated code]*/
Davin Pottse895de32019-02-23 22:08:16 -0600200/*[python end generated code: output=da39a3ee5e6b4b0d input=79464c61a31ae932]*/
Zachary Waref2244ea2015-05-13 01:22:54 -0500201
202#include "clinic/_winapi.c.h"
203
204/*[clinic input]
205_winapi.Overlapped.GetOverlappedResult
206
207 wait: bool
208 /
209[clinic start generated code]*/
210
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200211static PyObject *
Zachary Waref2244ea2015-05-13 01:22:54 -0500212_winapi_Overlapped_GetOverlappedResult_impl(OverlappedObject *self, int wait)
213/*[clinic end generated code: output=bdd0c1ed6518cd03 input=194505ee8e0e3565]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200214{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200215 BOOL res;
216 DWORD transferred = 0;
217 DWORD err;
218
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200219 Py_BEGIN_ALLOW_THREADS
220 res = GetOverlappedResult(self->handle, &self->overlapped, &transferred,
221 wait != 0);
222 Py_END_ALLOW_THREADS
223
224 err = res ? ERROR_SUCCESS : GetLastError();
225 switch (err) {
226 case ERROR_SUCCESS:
227 case ERROR_MORE_DATA:
228 case ERROR_OPERATION_ABORTED:
229 self->completed = 1;
230 self->pending = 0;
231 break;
232 case ERROR_IO_INCOMPLETE:
233 break;
234 default:
235 self->pending = 0;
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300236 return PyErr_SetExcFromWindowsErr(PyExc_OSError, err);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200237 }
238 if (self->completed && self->read_buffer != NULL) {
239 assert(PyBytes_CheckExact(self->read_buffer));
240 if (transferred != PyBytes_GET_SIZE(self->read_buffer) &&
241 _PyBytes_Resize(&self->read_buffer, transferred))
242 return NULL;
243 }
244 return Py_BuildValue("II", (unsigned) transferred, (unsigned) err);
245}
246
Zachary Waref2244ea2015-05-13 01:22:54 -0500247/*[clinic input]
248_winapi.Overlapped.getbuffer
249[clinic start generated code]*/
250
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200251static PyObject *
Zachary Waref2244ea2015-05-13 01:22:54 -0500252_winapi_Overlapped_getbuffer_impl(OverlappedObject *self)
253/*[clinic end generated code: output=95a3eceefae0f748 input=347fcfd56b4ceabd]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200254{
255 PyObject *res;
256 if (!self->completed) {
257 PyErr_SetString(PyExc_ValueError,
258 "can't get read buffer before GetOverlappedResult() "
259 "signals the operation completed");
260 return NULL;
261 }
262 res = self->read_buffer ? self->read_buffer : Py_None;
263 Py_INCREF(res);
264 return res;
265}
266
Zachary Waref2244ea2015-05-13 01:22:54 -0500267/*[clinic input]
268_winapi.Overlapped.cancel
269[clinic start generated code]*/
270
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200271static PyObject *
Zachary Waref2244ea2015-05-13 01:22:54 -0500272_winapi_Overlapped_cancel_impl(OverlappedObject *self)
273/*[clinic end generated code: output=fcb9ab5df4ebdae5 input=cbf3da142290039f]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200274{
275 BOOL res = TRUE;
276
277 if (self->pending) {
278 Py_BEGIN_ALLOW_THREADS
279 if (check_CancelIoEx())
280 res = Py_CancelIoEx(self->handle, &self->overlapped);
281 else
282 res = CancelIo(self->handle);
283 Py_END_ALLOW_THREADS
284 }
285
286 /* CancelIoEx returns ERROR_NOT_FOUND if the I/O completed in-between */
287 if (!res && GetLastError() != ERROR_NOT_FOUND)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300288 return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200289 self->pending = 0;
290 Py_RETURN_NONE;
291}
292
293static PyMethodDef overlapped_methods[] = {
Zachary Waref2244ea2015-05-13 01:22:54 -0500294 _WINAPI_OVERLAPPED_GETOVERLAPPEDRESULT_METHODDEF
295 _WINAPI_OVERLAPPED_GETBUFFER_METHODDEF
296 _WINAPI_OVERLAPPED_CANCEL_METHODDEF
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200297 {NULL}
298};
299
300static PyMemberDef overlapped_members[] = {
301 {"event", T_HANDLE,
302 offsetof(OverlappedObject, overlapped) + offsetof(OVERLAPPED, hEvent),
303 READONLY, "overlapped event handle"},
304 {NULL}
305};
306
307PyTypeObject OverlappedType = {
308 PyVarObject_HEAD_INIT(NULL, 0)
309 /* tp_name */ "_winapi.Overlapped",
310 /* tp_basicsize */ sizeof(OverlappedObject),
311 /* tp_itemsize */ 0,
312 /* tp_dealloc */ (destructor) overlapped_dealloc,
313 /* tp_print */ 0,
314 /* tp_getattr */ 0,
315 /* tp_setattr */ 0,
316 /* tp_reserved */ 0,
317 /* tp_repr */ 0,
318 /* tp_as_number */ 0,
319 /* tp_as_sequence */ 0,
320 /* tp_as_mapping */ 0,
321 /* tp_hash */ 0,
322 /* tp_call */ 0,
323 /* tp_str */ 0,
324 /* tp_getattro */ 0,
325 /* tp_setattro */ 0,
326 /* tp_as_buffer */ 0,
327 /* tp_flags */ Py_TPFLAGS_DEFAULT,
328 /* tp_doc */ "OVERLAPPED structure wrapper",
329 /* tp_traverse */ 0,
330 /* tp_clear */ 0,
331 /* tp_richcompare */ 0,
332 /* tp_weaklistoffset */ 0,
333 /* tp_iter */ 0,
334 /* tp_iternext */ 0,
335 /* tp_methods */ overlapped_methods,
336 /* tp_members */ overlapped_members,
337 /* tp_getset */ 0,
338 /* tp_base */ 0,
339 /* tp_dict */ 0,
340 /* tp_descr_get */ 0,
341 /* tp_descr_set */ 0,
342 /* tp_dictoffset */ 0,
343 /* tp_init */ 0,
344 /* tp_alloc */ 0,
345 /* tp_new */ 0,
346};
347
348static OverlappedObject *
349new_overlapped(HANDLE handle)
350{
351 OverlappedObject *self;
352
353 self = PyObject_New(OverlappedObject, &OverlappedType);
354 if (!self)
355 return NULL;
356 self->handle = handle;
357 self->read_buffer = NULL;
358 self->pending = 0;
359 self->completed = 0;
360 memset(&self->overlapped, 0, sizeof(OVERLAPPED));
361 memset(&self->write_buffer, 0, sizeof(Py_buffer));
362 /* Manual reset, initially non-signalled */
363 self->overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
364 return self;
365}
366
367/* -------------------------------------------------------------------- */
368/* windows API functions */
369
Zachary Waref2244ea2015-05-13 01:22:54 -0500370/*[clinic input]
371_winapi.CloseHandle
372
373 handle: HANDLE
374 /
375
376Close handle.
377[clinic start generated code]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200378
379static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300380_winapi_CloseHandle_impl(PyObject *module, HANDLE handle)
381/*[clinic end generated code: output=7ad37345f07bd782 input=7f0e4ac36e0352b8]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200382{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200383 BOOL success;
384
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200385 Py_BEGIN_ALLOW_THREADS
Zachary Waref2244ea2015-05-13 01:22:54 -0500386 success = CloseHandle(handle);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200387 Py_END_ALLOW_THREADS
388
389 if (!success)
390 return PyErr_SetFromWindowsErr(0);
391
392 Py_RETURN_NONE;
393}
394
Zachary Waref2244ea2015-05-13 01:22:54 -0500395/*[clinic input]
396_winapi.ConnectNamedPipe
397
398 handle: HANDLE
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200399 overlapped as use_overlapped: bool(accept={int}) = False
Zachary Waref2244ea2015-05-13 01:22:54 -0500400[clinic start generated code]*/
401
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200402static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300403_winapi_ConnectNamedPipe_impl(PyObject *module, HANDLE handle,
Zachary Ware77772c02015-05-13 10:58:35 -0500404 int use_overlapped)
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200405/*[clinic end generated code: output=335a0e7086800671 input=34f937c1c86e5e68]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200406{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200407 BOOL success;
408 OverlappedObject *overlapped = NULL;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200409
410 if (use_overlapped) {
Zachary Waref2244ea2015-05-13 01:22:54 -0500411 overlapped = new_overlapped(handle);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200412 if (!overlapped)
413 return NULL;
414 }
415
416 Py_BEGIN_ALLOW_THREADS
Zachary Waref2244ea2015-05-13 01:22:54 -0500417 success = ConnectNamedPipe(handle,
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200418 overlapped ? &overlapped->overlapped : NULL);
419 Py_END_ALLOW_THREADS
420
421 if (overlapped) {
422 int err = GetLastError();
423 /* Overlapped ConnectNamedPipe never returns a success code */
424 assert(success == 0);
425 if (err == ERROR_IO_PENDING)
426 overlapped->pending = 1;
427 else if (err == ERROR_PIPE_CONNECTED)
428 SetEvent(overlapped->overlapped.hEvent);
429 else {
430 Py_DECREF(overlapped);
431 return PyErr_SetFromWindowsErr(err);
432 }
433 return (PyObject *) overlapped;
434 }
435 if (!success)
436 return PyErr_SetFromWindowsErr(0);
437
438 Py_RETURN_NONE;
439}
440
Zachary Waref2244ea2015-05-13 01:22:54 -0500441/*[clinic input]
442_winapi.CreateFile -> HANDLE
443
444 file_name: LPCTSTR
445 desired_access: DWORD
446 share_mode: DWORD
447 security_attributes: LPSECURITY_ATTRIBUTES
448 creation_disposition: DWORD
449 flags_and_attributes: DWORD
450 template_file: HANDLE
451 /
452[clinic start generated code]*/
453
454static HANDLE
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300455_winapi_CreateFile_impl(PyObject *module, LPCTSTR file_name,
Zachary Ware77772c02015-05-13 10:58:35 -0500456 DWORD desired_access, DWORD share_mode,
457 LPSECURITY_ATTRIBUTES security_attributes,
458 DWORD creation_disposition,
459 DWORD flags_and_attributes, HANDLE template_file)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300460/*[clinic end generated code: output=417ddcebfc5a3d53 input=6423c3e40372dbd5]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200461{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200462 HANDLE handle;
463
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200464 Py_BEGIN_ALLOW_THREADS
Zachary Waref2244ea2015-05-13 01:22:54 -0500465 handle = CreateFile(file_name, desired_access,
466 share_mode, security_attributes,
467 creation_disposition,
468 flags_and_attributes, template_file);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200469 Py_END_ALLOW_THREADS
470
471 if (handle == INVALID_HANDLE_VALUE)
Zachary Waref2244ea2015-05-13 01:22:54 -0500472 PyErr_SetFromWindowsErr(0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200473
Zachary Waref2244ea2015-05-13 01:22:54 -0500474 return handle;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200475}
476
Zachary Waref2244ea2015-05-13 01:22:54 -0500477/*[clinic input]
Davin Pottse895de32019-02-23 22:08:16 -0600478_winapi.CreateFileMapping -> HANDLE
479
480 file_handle: HANDLE
481 security_attributes: LPSECURITY_ATTRIBUTES
482 protect: DWORD
483 max_size_high: DWORD
484 max_size_low: DWORD
485 name: LPCWSTR
486 /
487[clinic start generated code]*/
488
489static HANDLE
490_winapi_CreateFileMapping_impl(PyObject *module, HANDLE file_handle,
491 LPSECURITY_ATTRIBUTES security_attributes,
492 DWORD protect, DWORD max_size_high,
493 DWORD max_size_low, LPCWSTR name)
494/*[clinic end generated code: output=6c0a4d5cf7f6fcc6 input=3dc5cf762a74dee8]*/
495{
496 HANDLE handle;
497
498 Py_BEGIN_ALLOW_THREADS
499 handle = CreateFileMappingW(file_handle, security_attributes,
500 protect, max_size_high, max_size_low,
501 name);
502 Py_END_ALLOW_THREADS
503
504 if (handle == NULL) {
505 PyErr_SetFromWindowsErrWithUnicodeFilename(0, name);
506 handle = INVALID_HANDLE_VALUE;
507 }
508
509 return handle;
510}
511
512/*[clinic input]
Zachary Waref2244ea2015-05-13 01:22:54 -0500513_winapi.CreateJunction
Tim Golden0321cf22014-05-05 19:46:17 +0100514
Zachary Waref2244ea2015-05-13 01:22:54 -0500515 src_path: LPWSTR
516 dst_path: LPWSTR
517 /
518[clinic start generated code]*/
519
520static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300521_winapi_CreateJunction_impl(PyObject *module, LPWSTR src_path,
Zachary Ware77772c02015-05-13 10:58:35 -0500522 LPWSTR dst_path)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300523/*[clinic end generated code: output=66b7eb746e1dfa25 input=8cd1f9964b6e3d36]*/
Zachary Waref2244ea2015-05-13 01:22:54 -0500524{
Tim Golden0321cf22014-05-05 19:46:17 +0100525 /* Privilege adjustment */
526 HANDLE token = NULL;
527 TOKEN_PRIVILEGES tp;
528
529 /* Reparse data buffer */
530 const USHORT prefix_len = 4;
531 USHORT print_len = 0;
532 USHORT rdb_size = 0;
Martin Panter70214ad2016-08-04 02:38:59 +0000533 _Py_PREPARSE_DATA_BUFFER rdb = NULL;
Tim Golden0321cf22014-05-05 19:46:17 +0100534
535 /* Junction point creation */
536 HANDLE junction = NULL;
537 DWORD ret = 0;
538
Tim Golden0321cf22014-05-05 19:46:17 +0100539 if (src_path == NULL || dst_path == NULL)
540 return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER);
541
542 if (wcsncmp(src_path, L"\\??\\", prefix_len) == 0)
543 return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER);
544
545 /* Adjust privileges to allow rewriting directory entry as a
546 junction point. */
547 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token))
548 goto cleanup;
549
550 if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid))
551 goto cleanup;
552
553 tp.PrivilegeCount = 1;
554 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
555 if (!AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
556 NULL, NULL))
557 goto cleanup;
558
559 if (GetFileAttributesW(src_path) == INVALID_FILE_ATTRIBUTES)
560 goto cleanup;
561
562 /* Store the absolute link target path length in print_len. */
563 print_len = (USHORT)GetFullPathNameW(src_path, 0, NULL, NULL);
564 if (print_len == 0)
565 goto cleanup;
566
567 /* NUL terminator should not be part of print_len. */
568 --print_len;
569
570 /* REPARSE_DATA_BUFFER usage is heavily under-documented, especially for
571 junction points. Here's what I've learned along the way:
572 - A junction point has two components: a print name and a substitute
573 name. They both describe the link target, but the substitute name is
574 the physical target and the print name is shown in directory listings.
575 - The print name must be a native name, prefixed with "\??\".
576 - Both names are stored after each other in the same buffer (the
577 PathBuffer) and both must be NUL-terminated.
578 - There are four members defining their respective offset and length
579 inside PathBuffer: SubstituteNameOffset, SubstituteNameLength,
580 PrintNameOffset and PrintNameLength.
581 - The total size we need to allocate for the REPARSE_DATA_BUFFER, thus,
582 is the sum of:
583 - the fixed header size (REPARSE_DATA_BUFFER_HEADER_SIZE)
584 - the size of the MountPointReparseBuffer member without the PathBuffer
585 - the size of the prefix ("\??\") in bytes
586 - the size of the print name in bytes
587 - the size of the substitute name in bytes
588 - the size of two NUL terminators in bytes */
Martin Panter70214ad2016-08-04 02:38:59 +0000589 rdb_size = _Py_REPARSE_DATA_BUFFER_HEADER_SIZE +
Tim Golden0321cf22014-05-05 19:46:17 +0100590 sizeof(rdb->MountPointReparseBuffer) -
591 sizeof(rdb->MountPointReparseBuffer.PathBuffer) +
592 /* Two +1's for NUL terminators. */
593 (prefix_len + print_len + 1 + print_len + 1) * sizeof(WCHAR);
Martin Panter70214ad2016-08-04 02:38:59 +0000594 rdb = (_Py_PREPARSE_DATA_BUFFER)PyMem_RawMalloc(rdb_size);
Tim Golden0321cf22014-05-05 19:46:17 +0100595 if (rdb == NULL)
596 goto cleanup;
597
598 memset(rdb, 0, rdb_size);
599 rdb->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
Martin Panter70214ad2016-08-04 02:38:59 +0000600 rdb->ReparseDataLength = rdb_size - _Py_REPARSE_DATA_BUFFER_HEADER_SIZE;
Tim Golden0321cf22014-05-05 19:46:17 +0100601 rdb->MountPointReparseBuffer.SubstituteNameOffset = 0;
602 rdb->MountPointReparseBuffer.SubstituteNameLength =
603 (prefix_len + print_len) * sizeof(WCHAR);
604 rdb->MountPointReparseBuffer.PrintNameOffset =
605 rdb->MountPointReparseBuffer.SubstituteNameLength + sizeof(WCHAR);
606 rdb->MountPointReparseBuffer.PrintNameLength = print_len * sizeof(WCHAR);
607
608 /* Store the full native path of link target at the substitute name
609 offset (0). */
610 wcscpy(rdb->MountPointReparseBuffer.PathBuffer, L"\\??\\");
611 if (GetFullPathNameW(src_path, print_len + 1,
612 rdb->MountPointReparseBuffer.PathBuffer + prefix_len,
613 NULL) == 0)
614 goto cleanup;
615
616 /* Copy everything but the native prefix to the print name offset. */
617 wcscpy(rdb->MountPointReparseBuffer.PathBuffer +
618 prefix_len + print_len + 1,
619 rdb->MountPointReparseBuffer.PathBuffer + prefix_len);
620
621 /* Create a directory for the junction point. */
622 if (!CreateDirectoryW(dst_path, NULL))
623 goto cleanup;
624
625 junction = CreateFileW(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL,
626 OPEN_EXISTING,
627 FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
628 if (junction == INVALID_HANDLE_VALUE)
629 goto cleanup;
630
631 /* Make the directory entry a junction point. */
632 if (!DeviceIoControl(junction, FSCTL_SET_REPARSE_POINT, rdb, rdb_size,
633 NULL, 0, &ret, NULL))
634 goto cleanup;
635
636cleanup:
637 ret = GetLastError();
638
639 CloseHandle(token);
640 CloseHandle(junction);
641 PyMem_RawFree(rdb);
642
643 if (ret != 0)
644 return PyErr_SetFromWindowsErr(ret);
645
646 Py_RETURN_NONE;
647}
648
Zachary Waref2244ea2015-05-13 01:22:54 -0500649/*[clinic input]
650_winapi.CreateNamedPipe -> HANDLE
651
652 name: LPCTSTR
653 open_mode: DWORD
654 pipe_mode: DWORD
655 max_instances: DWORD
656 out_buffer_size: DWORD
657 in_buffer_size: DWORD
658 default_timeout: DWORD
659 security_attributes: LPSECURITY_ATTRIBUTES
660 /
661[clinic start generated code]*/
662
663static HANDLE
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300664_winapi_CreateNamedPipe_impl(PyObject *module, LPCTSTR name, DWORD open_mode,
665 DWORD pipe_mode, DWORD max_instances,
666 DWORD out_buffer_size, DWORD in_buffer_size,
667 DWORD default_timeout,
Zachary Ware77772c02015-05-13 10:58:35 -0500668 LPSECURITY_ATTRIBUTES security_attributes)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300669/*[clinic end generated code: output=80f8c07346a94fbc input=5a73530b84d8bc37]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200670{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200671 HANDLE handle;
672
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200673 Py_BEGIN_ALLOW_THREADS
Zachary Waref2244ea2015-05-13 01:22:54 -0500674 handle = CreateNamedPipe(name, open_mode, pipe_mode,
675 max_instances, out_buffer_size,
676 in_buffer_size, default_timeout,
677 security_attributes);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200678 Py_END_ALLOW_THREADS
679
680 if (handle == INVALID_HANDLE_VALUE)
Zachary Waref2244ea2015-05-13 01:22:54 -0500681 PyErr_SetFromWindowsErr(0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200682
Zachary Waref2244ea2015-05-13 01:22:54 -0500683 return handle;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200684}
685
Zachary Waref2244ea2015-05-13 01:22:54 -0500686/*[clinic input]
687_winapi.CreatePipe
688
689 pipe_attrs: object
690 Ignored internally, can be None.
691 size: DWORD
692 /
693
694Create an anonymous pipe.
695
696Returns a 2-tuple of handles, to the read and write ends of the pipe.
697[clinic start generated code]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200698
699static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300700_winapi_CreatePipe_impl(PyObject *module, PyObject *pipe_attrs, DWORD size)
701/*[clinic end generated code: output=1c4411d8699f0925 input=c4f2cfa56ef68d90]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200702{
703 HANDLE read_pipe;
704 HANDLE write_pipe;
705 BOOL result;
706
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200707 Py_BEGIN_ALLOW_THREADS
708 result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
709 Py_END_ALLOW_THREADS
710
711 if (! result)
712 return PyErr_SetFromWindowsErr(GetLastError());
713
714 return Py_BuildValue(
715 "NN", HANDLE_TO_PYNUM(read_pipe), HANDLE_TO_PYNUM(write_pipe));
716}
717
718/* helpers for createprocess */
719
720static unsigned long
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200721getulong(PyObject* obj, const char* name)
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200722{
723 PyObject* value;
724 unsigned long ret;
725
726 value = PyObject_GetAttrString(obj, name);
727 if (! value) {
728 PyErr_Clear(); /* FIXME: propagate error? */
729 return 0;
730 }
731 ret = PyLong_AsUnsignedLong(value);
732 Py_DECREF(value);
733 return ret;
734}
735
736static HANDLE
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200737gethandle(PyObject* obj, const char* name)
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200738{
739 PyObject* value;
740 HANDLE ret;
741
742 value = PyObject_GetAttrString(obj, name);
743 if (! value) {
744 PyErr_Clear(); /* FIXME: propagate error? */
745 return NULL;
746 }
747 if (value == Py_None)
748 ret = NULL;
749 else
750 ret = PYNUM_TO_HANDLE(value);
751 Py_DECREF(value);
752 return ret;
753}
754
755static PyObject*
756getenvironment(PyObject* environment)
757{
758 Py_ssize_t i, envsize, totalsize;
759 Py_UCS4 *buffer = NULL, *p, *end;
760 PyObject *keys, *values, *res;
761
Ezio Melotti85a86292013-08-17 16:57:41 +0300762 /* convert environment dictionary to windows environment string */
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200763 if (! PyMapping_Check(environment)) {
764 PyErr_SetString(
765 PyExc_TypeError, "environment must be dictionary or None");
766 return NULL;
767 }
768
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200769 keys = PyMapping_Keys(environment);
Oren Milman0b3a87e2017-09-14 22:30:28 +0300770 if (!keys) {
771 return NULL;
772 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200773 values = PyMapping_Values(environment);
Oren Milman0b3a87e2017-09-14 22:30:28 +0300774 if (!values) {
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200775 goto error;
Oren Milman0b3a87e2017-09-14 22:30:28 +0300776 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200777
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300778 envsize = PySequence_Fast_GET_SIZE(keys);
779 if (PySequence_Fast_GET_SIZE(values) != envsize) {
780 PyErr_SetString(PyExc_RuntimeError,
781 "environment changed size during iteration");
782 goto error;
783 }
784
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200785 totalsize = 1; /* trailing null character */
786 for (i = 0; i < envsize; i++) {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300787 PyObject* key = PySequence_Fast_GET_ITEM(keys, i);
788 PyObject* value = PySequence_Fast_GET_ITEM(values, i);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200789
790 if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) {
791 PyErr_SetString(PyExc_TypeError,
792 "environment can only contain strings");
793 goto error;
794 }
Serhiy Storchakad174d242017-06-23 19:39:27 +0300795 if (PyUnicode_FindChar(key, '\0', 0, PyUnicode_GET_LENGTH(key), 1) != -1 ||
796 PyUnicode_FindChar(value, '\0', 0, PyUnicode_GET_LENGTH(value), 1) != -1)
797 {
798 PyErr_SetString(PyExc_ValueError, "embedded null character");
799 goto error;
800 }
801 /* Search from index 1 because on Windows starting '=' is allowed for
802 defining hidden environment variables. */
803 if (PyUnicode_GET_LENGTH(key) == 0 ||
804 PyUnicode_FindChar(key, '=', 1, PyUnicode_GET_LENGTH(key), 1) != -1)
805 {
806 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
807 goto error;
808 }
Benjamin Peterson8ce68062015-02-09 20:58:12 -0500809 if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(key) - 1) {
810 PyErr_SetString(PyExc_OverflowError, "environment too long");
811 goto error;
812 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200813 totalsize += PyUnicode_GET_LENGTH(key) + 1; /* +1 for '=' */
Benjamin Peterson8ce68062015-02-09 20:58:12 -0500814 if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(value) - 1) {
815 PyErr_SetString(PyExc_OverflowError, "environment too long");
816 goto error;
817 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200818 totalsize += PyUnicode_GET_LENGTH(value) + 1; /* +1 for '\0' */
819 }
820
Benjamin Peterson8ce68062015-02-09 20:58:12 -0500821 buffer = PyMem_NEW(Py_UCS4, totalsize);
822 if (! buffer) {
823 PyErr_NoMemory();
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200824 goto error;
Benjamin Peterson8ce68062015-02-09 20:58:12 -0500825 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200826 p = buffer;
827 end = buffer + totalsize;
828
829 for (i = 0; i < envsize; i++) {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +0300830 PyObject* key = PySequence_Fast_GET_ITEM(keys, i);
831 PyObject* value = PySequence_Fast_GET_ITEM(values, i);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200832 if (!PyUnicode_AsUCS4(key, p, end - p, 0))
833 goto error;
834 p += PyUnicode_GET_LENGTH(key);
835 *p++ = '=';
836 if (!PyUnicode_AsUCS4(value, p, end - p, 0))
837 goto error;
838 p += PyUnicode_GET_LENGTH(value);
839 *p++ = '\0';
840 }
841
842 /* add trailing null byte */
843 *p++ = '\0';
844 assert(p == end);
845
846 Py_XDECREF(keys);
847 Py_XDECREF(values);
848
849 res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, p - buffer);
850 PyMem_Free(buffer);
851 return res;
852
853 error:
854 PyMem_Free(buffer);
855 Py_XDECREF(keys);
856 Py_XDECREF(values);
857 return NULL;
858}
859
Segev Finerb2a60832017-12-18 11:28:19 +0200860static LPHANDLE
861gethandlelist(PyObject *mapping, const char *name, Py_ssize_t *size)
862{
863 LPHANDLE ret = NULL;
864 PyObject *value_fast = NULL;
865 PyObject *value;
866 Py_ssize_t i;
867
868 value = PyMapping_GetItemString(mapping, name);
869 if (!value) {
870 PyErr_Clear();
871 return NULL;
872 }
873
874 if (value == Py_None) {
875 goto cleanup;
876 }
877
878 value_fast = PySequence_Fast(value, "handle_list must be a sequence or None");
879 if (value_fast == NULL)
880 goto cleanup;
881
882 *size = PySequence_Fast_GET_SIZE(value_fast) * sizeof(HANDLE);
883
884 /* Passing an empty array causes CreateProcess to fail so just don't set it */
885 if (*size == 0) {
886 goto cleanup;
887 }
888
889 ret = PyMem_Malloc(*size);
890 if (ret == NULL)
891 goto cleanup;
892
893 for (i = 0; i < PySequence_Fast_GET_SIZE(value_fast); i++) {
894 ret[i] = PYNUM_TO_HANDLE(PySequence_Fast_GET_ITEM(value_fast, i));
895 if (ret[i] == (HANDLE)-1 && PyErr_Occurred()) {
896 PyMem_Free(ret);
897 ret = NULL;
898 goto cleanup;
899 }
900 }
901
902cleanup:
903 Py_DECREF(value);
904 Py_XDECREF(value_fast);
905 return ret;
906}
907
908typedef struct {
909 LPPROC_THREAD_ATTRIBUTE_LIST attribute_list;
910 LPHANDLE handle_list;
911} AttributeList;
912
913static void
914freeattributelist(AttributeList *attribute_list)
915{
916 if (attribute_list->attribute_list != NULL) {
917 DeleteProcThreadAttributeList(attribute_list->attribute_list);
918 PyMem_Free(attribute_list->attribute_list);
919 }
920
921 PyMem_Free(attribute_list->handle_list);
922
923 memset(attribute_list, 0, sizeof(*attribute_list));
924}
925
926static int
927getattributelist(PyObject *obj, const char *name, AttributeList *attribute_list)
928{
929 int ret = 0;
930 DWORD err;
931 BOOL result;
932 PyObject *value;
933 Py_ssize_t handle_list_size;
934 DWORD attribute_count = 0;
935 SIZE_T attribute_list_size = 0;
936
937 value = PyObject_GetAttrString(obj, name);
938 if (!value) {
939 PyErr_Clear(); /* FIXME: propagate error? */
940 return 0;
941 }
942
943 if (value == Py_None) {
944 ret = 0;
945 goto cleanup;
946 }
947
948 if (!PyMapping_Check(value)) {
949 ret = -1;
950 PyErr_Format(PyExc_TypeError, "%s must be a mapping or None", name);
951 goto cleanup;
952 }
953
954 attribute_list->handle_list = gethandlelist(value, "handle_list", &handle_list_size);
955 if (attribute_list->handle_list == NULL && PyErr_Occurred()) {
956 ret = -1;
957 goto cleanup;
958 }
959
960 if (attribute_list->handle_list != NULL)
961 ++attribute_count;
962
963 /* Get how many bytes we need for the attribute list */
964 result = InitializeProcThreadAttributeList(NULL, attribute_count, 0, &attribute_list_size);
965 if (result || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
966 ret = -1;
967 PyErr_SetFromWindowsErr(GetLastError());
968 goto cleanup;
969 }
970
971 attribute_list->attribute_list = PyMem_Malloc(attribute_list_size);
972 if (attribute_list->attribute_list == NULL) {
973 ret = -1;
974 goto cleanup;
975 }
976
977 result = InitializeProcThreadAttributeList(
978 attribute_list->attribute_list,
979 attribute_count,
980 0,
981 &attribute_list_size);
982 if (!result) {
983 err = GetLastError();
984
985 /* So that we won't call DeleteProcThreadAttributeList */
986 PyMem_Free(attribute_list->attribute_list);
987 attribute_list->attribute_list = NULL;
988
989 ret = -1;
990 PyErr_SetFromWindowsErr(err);
991 goto cleanup;
992 }
993
994 if (attribute_list->handle_list != NULL) {
995 result = UpdateProcThreadAttribute(
996 attribute_list->attribute_list,
997 0,
998 PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
999 attribute_list->handle_list,
1000 handle_list_size,
1001 NULL,
1002 NULL);
1003 if (!result) {
1004 ret = -1;
1005 PyErr_SetFromWindowsErr(GetLastError());
1006 goto cleanup;
1007 }
1008 }
1009
1010cleanup:
1011 Py_DECREF(value);
1012
1013 if (ret < 0)
1014 freeattributelist(attribute_list);
1015
1016 return ret;
1017}
1018
Zachary Waref2244ea2015-05-13 01:22:54 -05001019/*[clinic input]
1020_winapi.CreateProcess
1021
Zachary Ware77772c02015-05-13 10:58:35 -05001022 application_name: Py_UNICODE(accept={str, NoneType})
Vladimir Matveev7b360162018-12-14 00:30:51 -08001023 command_line: object
1024 Can be str or None
Zachary Waref2244ea2015-05-13 01:22:54 -05001025 proc_attrs: object
1026 Ignored internally, can be None.
1027 thread_attrs: object
1028 Ignored internally, can be None.
1029 inherit_handles: BOOL
1030 creation_flags: DWORD
1031 env_mapping: object
Zachary Ware77772c02015-05-13 10:58:35 -05001032 current_directory: Py_UNICODE(accept={str, NoneType})
Zachary Waref2244ea2015-05-13 01:22:54 -05001033 startup_info: object
1034 /
1035
1036Create a new process and its primary thread.
1037
1038The return value is a tuple of the process handle, thread handle,
1039process ID, and thread ID.
1040[clinic start generated code]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001041
1042static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001043_winapi_CreateProcess_impl(PyObject *module,
1044 const Py_UNICODE *application_name,
Vladimir Matveev7b360162018-12-14 00:30:51 -08001045 PyObject *command_line, PyObject *proc_attrs,
Zachary Ware77772c02015-05-13 10:58:35 -05001046 PyObject *thread_attrs, BOOL inherit_handles,
1047 DWORD creation_flags, PyObject *env_mapping,
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001048 const Py_UNICODE *current_directory,
Zachary Ware77772c02015-05-13 10:58:35 -05001049 PyObject *startup_info)
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02001050/*[clinic end generated code: output=9b2423a609230132 input=42ac293eaea03fc4]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001051{
Segev Finerb2a60832017-12-18 11:28:19 +02001052 PyObject *ret = NULL;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001053 BOOL result;
1054 PROCESS_INFORMATION pi;
Segev Finerb2a60832017-12-18 11:28:19 +02001055 STARTUPINFOEXW si;
1056 PyObject *environment = NULL;
Serhiy Storchaka0ee32c12017-06-24 16:14:08 +03001057 wchar_t *wenvironment;
Vladimir Matveev7b360162018-12-14 00:30:51 -08001058 wchar_t *command_line_copy = NULL;
Segev Finerb2a60832017-12-18 11:28:19 +02001059 AttributeList attribute_list = {0};
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001060
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001061 ZeroMemory(&si, sizeof(si));
Segev Finerb2a60832017-12-18 11:28:19 +02001062 si.StartupInfo.cb = sizeof(si);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001063
1064 /* note: we only support a small subset of all SI attributes */
Segev Finerb2a60832017-12-18 11:28:19 +02001065 si.StartupInfo.dwFlags = getulong(startup_info, "dwFlags");
1066 si.StartupInfo.wShowWindow = (WORD)getulong(startup_info, "wShowWindow");
1067 si.StartupInfo.hStdInput = gethandle(startup_info, "hStdInput");
1068 si.StartupInfo.hStdOutput = gethandle(startup_info, "hStdOutput");
1069 si.StartupInfo.hStdError = gethandle(startup_info, "hStdError");
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001070 if (PyErr_Occurred())
Segev Finerb2a60832017-12-18 11:28:19 +02001071 goto cleanup;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001072
1073 if (env_mapping != Py_None) {
1074 environment = getenvironment(env_mapping);
Serhiy Storchakad174d242017-06-23 19:39:27 +03001075 if (environment == NULL) {
Segev Finerb2a60832017-12-18 11:28:19 +02001076 goto cleanup;
Serhiy Storchakad174d242017-06-23 19:39:27 +03001077 }
1078 /* contains embedded null characters */
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001079 wenvironment = PyUnicode_AsUnicode(environment);
Serhiy Storchakad174d242017-06-23 19:39:27 +03001080 if (wenvironment == NULL) {
Segev Finerb2a60832017-12-18 11:28:19 +02001081 goto cleanup;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001082 }
1083 }
1084 else {
1085 environment = NULL;
1086 wenvironment = NULL;
1087 }
1088
Segev Finerb2a60832017-12-18 11:28:19 +02001089 if (getattributelist(startup_info, "lpAttributeList", &attribute_list) < 0)
1090 goto cleanup;
1091
1092 si.lpAttributeList = attribute_list.attribute_list;
Vladimir Matveev7b360162018-12-14 00:30:51 -08001093 if (PyUnicode_Check(command_line)) {
1094 command_line_copy = PyUnicode_AsWideCharString(command_line, NULL);
1095 if (command_line_copy == NULL) {
1096 goto cleanup;
1097 }
1098 }
1099 else if (command_line != Py_None) {
1100 PyErr_Format(PyExc_TypeError,
1101 "CreateProcess() argument 2 must be str or None, not %s",
1102 Py_TYPE(command_line)->tp_name);
1103 goto cleanup;
1104 }
1105
Segev Finerb2a60832017-12-18 11:28:19 +02001106
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001107 Py_BEGIN_ALLOW_THREADS
1108 result = CreateProcessW(application_name,
Vladimir Matveev7b360162018-12-14 00:30:51 -08001109 command_line_copy,
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001110 NULL,
1111 NULL,
1112 inherit_handles,
Segev Finerb2a60832017-12-18 11:28:19 +02001113 creation_flags | EXTENDED_STARTUPINFO_PRESENT |
1114 CREATE_UNICODE_ENVIRONMENT,
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001115 wenvironment,
1116 current_directory,
Segev Finerb2a60832017-12-18 11:28:19 +02001117 (LPSTARTUPINFOW)&si,
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001118 &pi);
1119 Py_END_ALLOW_THREADS
1120
Segev Finerb2a60832017-12-18 11:28:19 +02001121 if (!result) {
1122 PyErr_SetFromWindowsErr(GetLastError());
1123 goto cleanup;
1124 }
1125
1126 ret = Py_BuildValue("NNkk",
1127 HANDLE_TO_PYNUM(pi.hProcess),
1128 HANDLE_TO_PYNUM(pi.hThread),
1129 pi.dwProcessId,
1130 pi.dwThreadId);
1131
1132cleanup:
Vladimir Matveev7b360162018-12-14 00:30:51 -08001133 PyMem_Free(command_line_copy);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001134 Py_XDECREF(environment);
Segev Finerb2a60832017-12-18 11:28:19 +02001135 freeattributelist(&attribute_list);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001136
Segev Finerb2a60832017-12-18 11:28:19 +02001137 return ret;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001138}
1139
Zachary Waref2244ea2015-05-13 01:22:54 -05001140/*[clinic input]
1141_winapi.DuplicateHandle -> HANDLE
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001142
Zachary Waref2244ea2015-05-13 01:22:54 -05001143 source_process_handle: HANDLE
1144 source_handle: HANDLE
1145 target_process_handle: HANDLE
1146 desired_access: DWORD
1147 inherit_handle: BOOL
1148 options: DWORD = 0
1149 /
1150
1151Return a duplicate handle object.
1152
1153The duplicate handle refers to the same object as the original
1154handle. Therefore, any changes to the object are reflected
1155through both handles.
1156[clinic start generated code]*/
1157
1158static HANDLE
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001159_winapi_DuplicateHandle_impl(PyObject *module, HANDLE source_process_handle,
Zachary Ware77772c02015-05-13 10:58:35 -05001160 HANDLE source_handle,
1161 HANDLE target_process_handle,
1162 DWORD desired_access, BOOL inherit_handle,
1163 DWORD options)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001164/*[clinic end generated code: output=ad9711397b5dcd4e input=b933e3f2356a8c12]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001165{
1166 HANDLE target_handle;
1167 BOOL result;
1168
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001169 Py_BEGIN_ALLOW_THREADS
1170 result = DuplicateHandle(
1171 source_process_handle,
1172 source_handle,
1173 target_process_handle,
1174 &target_handle,
1175 desired_access,
1176 inherit_handle,
1177 options
1178 );
1179 Py_END_ALLOW_THREADS
1180
Zachary Waref2244ea2015-05-13 01:22:54 -05001181 if (! result) {
1182 PyErr_SetFromWindowsErr(GetLastError());
1183 return INVALID_HANDLE_VALUE;
1184 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001185
Zachary Waref2244ea2015-05-13 01:22:54 -05001186 return target_handle;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001187}
1188
Zachary Waref2244ea2015-05-13 01:22:54 -05001189/*[clinic input]
1190_winapi.ExitProcess
1191
1192 ExitCode: UINT
1193 /
1194
1195[clinic start generated code]*/
1196
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001197static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001198_winapi_ExitProcess_impl(PyObject *module, UINT ExitCode)
1199/*[clinic end generated code: output=a387deb651175301 input=4f05466a9406c558]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001200{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001201 #if defined(Py_DEBUG)
1202 SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT|
1203 SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX);
1204 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
1205 #endif
1206
Zachary Waref2244ea2015-05-13 01:22:54 -05001207 ExitProcess(ExitCode);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001208
1209 return NULL;
1210}
1211
Zachary Waref2244ea2015-05-13 01:22:54 -05001212/*[clinic input]
1213_winapi.GetCurrentProcess -> HANDLE
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001214
Zachary Waref2244ea2015-05-13 01:22:54 -05001215Return a handle object for the current process.
1216[clinic start generated code]*/
1217
1218static HANDLE
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001219_winapi_GetCurrentProcess_impl(PyObject *module)
1220/*[clinic end generated code: output=ddeb4dd2ffadf344 input=b213403fd4b96b41]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001221{
Zachary Waref2244ea2015-05-13 01:22:54 -05001222 return GetCurrentProcess();
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001223}
1224
Zachary Waref2244ea2015-05-13 01:22:54 -05001225/*[clinic input]
1226_winapi.GetExitCodeProcess -> DWORD
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001227
Zachary Waref2244ea2015-05-13 01:22:54 -05001228 process: HANDLE
1229 /
1230
1231Return the termination status of the specified process.
1232[clinic start generated code]*/
1233
1234static DWORD
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001235_winapi_GetExitCodeProcess_impl(PyObject *module, HANDLE process)
1236/*[clinic end generated code: output=b4620bdf2bccf36b input=61b6bfc7dc2ee374]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001237{
1238 DWORD exit_code;
1239 BOOL result;
1240
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001241 result = GetExitCodeProcess(process, &exit_code);
1242
Zachary Waref2244ea2015-05-13 01:22:54 -05001243 if (! result) {
1244 PyErr_SetFromWindowsErr(GetLastError());
Victor Stinner850a18e2017-10-24 16:53:32 -07001245 exit_code = PY_DWORD_MAX;
Zachary Waref2244ea2015-05-13 01:22:54 -05001246 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001247
Zachary Waref2244ea2015-05-13 01:22:54 -05001248 return exit_code;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001249}
1250
Zachary Waref2244ea2015-05-13 01:22:54 -05001251/*[clinic input]
1252_winapi.GetLastError -> DWORD
1253[clinic start generated code]*/
1254
1255static DWORD
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001256_winapi_GetLastError_impl(PyObject *module)
1257/*[clinic end generated code: output=8585b827cb1a92c5 input=62d47fb9bce038ba]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001258{
Zachary Waref2244ea2015-05-13 01:22:54 -05001259 return GetLastError();
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001260}
1261
Zachary Waref2244ea2015-05-13 01:22:54 -05001262/*[clinic input]
1263_winapi.GetModuleFileName
1264
1265 module_handle: HMODULE
1266 /
1267
1268Return the fully-qualified path for the file that contains module.
1269
1270The module must have been loaded by the current process.
1271
1272The module parameter should be a handle to the loaded module
1273whose path is being requested. If this parameter is 0,
1274GetModuleFileName retrieves the path of the executable file
1275of the current process.
1276[clinic start generated code]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001277
1278static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001279_winapi_GetModuleFileName_impl(PyObject *module, HMODULE module_handle)
1280/*[clinic end generated code: output=85b4b728c5160306 input=6d66ff7deca5d11f]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001281{
1282 BOOL result;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001283 WCHAR filename[MAX_PATH];
1284
Zachary Waref2244ea2015-05-13 01:22:54 -05001285 result = GetModuleFileNameW(module_handle, filename, MAX_PATH);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001286 filename[MAX_PATH-1] = '\0';
1287
1288 if (! result)
1289 return PyErr_SetFromWindowsErr(GetLastError());
1290
1291 return PyUnicode_FromWideChar(filename, wcslen(filename));
1292}
1293
Zachary Waref2244ea2015-05-13 01:22:54 -05001294/*[clinic input]
1295_winapi.GetStdHandle -> HANDLE
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001296
Zachary Waref2244ea2015-05-13 01:22:54 -05001297 std_handle: DWORD
1298 One of STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE.
1299 /
1300
1301Return a handle to the specified standard device.
1302
1303The integer associated with the handle object is returned.
1304[clinic start generated code]*/
1305
1306static HANDLE
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001307_winapi_GetStdHandle_impl(PyObject *module, DWORD std_handle)
1308/*[clinic end generated code: output=0e613001e73ab614 input=07016b06a2fc8826]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001309{
1310 HANDLE handle;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001311
1312 Py_BEGIN_ALLOW_THREADS
1313 handle = GetStdHandle(std_handle);
1314 Py_END_ALLOW_THREADS
1315
1316 if (handle == INVALID_HANDLE_VALUE)
Zachary Waref2244ea2015-05-13 01:22:54 -05001317 PyErr_SetFromWindowsErr(GetLastError());
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001318
Zachary Waref2244ea2015-05-13 01:22:54 -05001319 return handle;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001320}
1321
Zachary Waref2244ea2015-05-13 01:22:54 -05001322/*[clinic input]
1323_winapi.GetVersion -> long
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001324
Zachary Waref2244ea2015-05-13 01:22:54 -05001325Return the version number of the current operating system.
1326[clinic start generated code]*/
1327
1328static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001329_winapi_GetVersion_impl(PyObject *module)
1330/*[clinic end generated code: output=e41f0db5a3b82682 input=e21dff8d0baeded2]*/
Steve Dower3e96f322015-03-02 08:01:10 -08001331/* Disable deprecation warnings about GetVersionEx as the result is
1332 being passed straight through to the caller, who is responsible for
1333 using it correctly. */
1334#pragma warning(push)
1335#pragma warning(disable:4996)
1336
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001337{
Zachary Waref2244ea2015-05-13 01:22:54 -05001338 return GetVersion();
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001339}
1340
Steve Dower3e96f322015-03-02 08:01:10 -08001341#pragma warning(pop)
1342
Zachary Waref2244ea2015-05-13 01:22:54 -05001343/*[clinic input]
Davin Pottse895de32019-02-23 22:08:16 -06001344_winapi.MapViewOfFile -> LPVOID
1345
1346 file_map: HANDLE
1347 desired_access: DWORD
1348 file_offset_high: DWORD
1349 file_offset_low: DWORD
1350 number_bytes: size_t
1351 /
1352[clinic start generated code]*/
1353
1354static LPVOID
1355_winapi_MapViewOfFile_impl(PyObject *module, HANDLE file_map,
1356 DWORD desired_access, DWORD file_offset_high,
1357 DWORD file_offset_low, size_t number_bytes)
1358/*[clinic end generated code: output=f23b1ee4823663e3 input=177471073be1a103]*/
1359{
1360 LPVOID address;
1361
1362 Py_BEGIN_ALLOW_THREADS
1363 address = MapViewOfFile(file_map, desired_access, file_offset_high,
1364 file_offset_low, number_bytes);
1365 Py_END_ALLOW_THREADS
1366
1367 if (address == NULL)
1368 PyErr_SetFromWindowsErr(0);
1369
1370 return address;
1371}
1372
1373/*[clinic input]
1374_winapi.OpenFileMapping -> HANDLE
1375
1376 desired_access: DWORD
1377 inherit_handle: BOOL
1378 name: LPCWSTR
1379 /
1380[clinic start generated code]*/
1381
1382static HANDLE
1383_winapi_OpenFileMapping_impl(PyObject *module, DWORD desired_access,
1384 BOOL inherit_handle, LPCWSTR name)
1385/*[clinic end generated code: output=08cc44def1cb11f1 input=131f2a405359de7f]*/
1386{
1387 HANDLE handle;
1388
1389 Py_BEGIN_ALLOW_THREADS
1390 handle = OpenFileMappingW(desired_access, inherit_handle, name);
1391 Py_END_ALLOW_THREADS
1392
1393 if (handle == NULL) {
1394 PyErr_SetFromWindowsErrWithUnicodeFilename(0, name);
1395 handle = INVALID_HANDLE_VALUE;
1396 }
1397
1398 return handle;
1399}
1400
1401/*[clinic input]
Zachary Waref2244ea2015-05-13 01:22:54 -05001402_winapi.OpenProcess -> HANDLE
1403
1404 desired_access: DWORD
1405 inherit_handle: BOOL
1406 process_id: DWORD
1407 /
1408[clinic start generated code]*/
1409
1410static HANDLE
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001411_winapi_OpenProcess_impl(PyObject *module, DWORD desired_access,
Zachary Ware77772c02015-05-13 10:58:35 -05001412 BOOL inherit_handle, DWORD process_id)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001413/*[clinic end generated code: output=b42b6b81ea5a0fc3 input=ec98c4cf4ea2ec36]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001414{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001415 HANDLE handle;
1416
Zachary Waref2244ea2015-05-13 01:22:54 -05001417 handle = OpenProcess(desired_access, inherit_handle, process_id);
1418 if (handle == NULL) {
1419 PyErr_SetFromWindowsErr(0);
1420 handle = INVALID_HANDLE_VALUE;
1421 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001422
Zachary Waref2244ea2015-05-13 01:22:54 -05001423 return handle;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001424}
1425
Zachary Waref2244ea2015-05-13 01:22:54 -05001426/*[clinic input]
1427_winapi.PeekNamedPipe
1428
1429 handle: HANDLE
1430 size: int = 0
1431 /
1432[clinic start generated code]*/
1433
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001434static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001435_winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size)
1436/*[clinic end generated code: output=d0c3e29e49d323dd input=c7aa53bfbce69d70]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001437{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001438 PyObject *buf = NULL;
1439 DWORD nread, navail, nleft;
1440 BOOL ret;
1441
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001442 if (size < 0) {
1443 PyErr_SetString(PyExc_ValueError, "negative size");
1444 return NULL;
1445 }
1446
1447 if (size) {
1448 buf = PyBytes_FromStringAndSize(NULL, size);
1449 if (!buf)
1450 return NULL;
1451 Py_BEGIN_ALLOW_THREADS
1452 ret = PeekNamedPipe(handle, PyBytes_AS_STRING(buf), size, &nread,
1453 &navail, &nleft);
1454 Py_END_ALLOW_THREADS
1455 if (!ret) {
1456 Py_DECREF(buf);
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001457 return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001458 }
1459 if (_PyBytes_Resize(&buf, nread))
1460 return NULL;
Alexander Buchkovsky266f4902018-09-04 19:10:28 +03001461 return Py_BuildValue("NII", buf, navail, nleft);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001462 }
1463 else {
1464 Py_BEGIN_ALLOW_THREADS
1465 ret = PeekNamedPipe(handle, NULL, 0, NULL, &navail, &nleft);
1466 Py_END_ALLOW_THREADS
1467 if (!ret) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001468 return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001469 }
Alexander Buchkovsky266f4902018-09-04 19:10:28 +03001470 return Py_BuildValue("II", navail, nleft);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001471 }
1472}
1473
Zachary Waref2244ea2015-05-13 01:22:54 -05001474/*[clinic input]
1475_winapi.ReadFile
1476
1477 handle: HANDLE
Alexander Buchkovsky266f4902018-09-04 19:10:28 +03001478 size: DWORD
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001479 overlapped as use_overlapped: bool(accept={int}) = False
Zachary Waref2244ea2015-05-13 01:22:54 -05001480[clinic start generated code]*/
1481
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001482static PyObject *
Alexander Buchkovsky266f4902018-09-04 19:10:28 +03001483_winapi_ReadFile_impl(PyObject *module, HANDLE handle, DWORD size,
Zachary Ware77772c02015-05-13 10:58:35 -05001484 int use_overlapped)
Alexander Buchkovsky266f4902018-09-04 19:10:28 +03001485/*[clinic end generated code: output=d3d5b44a8201b944 input=08c439d03a11aac5]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001486{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001487 DWORD nread;
1488 PyObject *buf;
1489 BOOL ret;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001490 DWORD err;
1491 OverlappedObject *overlapped = NULL;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001492
1493 buf = PyBytes_FromStringAndSize(NULL, size);
1494 if (!buf)
1495 return NULL;
1496 if (use_overlapped) {
1497 overlapped = new_overlapped(handle);
1498 if (!overlapped) {
1499 Py_DECREF(buf);
1500 return NULL;
1501 }
1502 /* Steals reference to buf */
1503 overlapped->read_buffer = buf;
1504 }
1505
1506 Py_BEGIN_ALLOW_THREADS
1507 ret = ReadFile(handle, PyBytes_AS_STRING(buf), size, &nread,
1508 overlapped ? &overlapped->overlapped : NULL);
1509 Py_END_ALLOW_THREADS
1510
1511 err = ret ? 0 : GetLastError();
1512
1513 if (overlapped) {
1514 if (!ret) {
1515 if (err == ERROR_IO_PENDING)
1516 overlapped->pending = 1;
1517 else if (err != ERROR_MORE_DATA) {
1518 Py_DECREF(overlapped);
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001519 return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001520 }
1521 }
1522 return Py_BuildValue("NI", (PyObject *) overlapped, err);
1523 }
1524
1525 if (!ret && err != ERROR_MORE_DATA) {
1526 Py_DECREF(buf);
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001527 return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001528 }
1529 if (_PyBytes_Resize(&buf, nread))
1530 return NULL;
1531 return Py_BuildValue("NI", buf, err);
1532}
1533
Zachary Waref2244ea2015-05-13 01:22:54 -05001534/*[clinic input]
1535_winapi.SetNamedPipeHandleState
1536
1537 named_pipe: HANDLE
1538 mode: object
1539 max_collection_count: object
1540 collect_data_timeout: object
1541 /
1542[clinic start generated code]*/
1543
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001544static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001545_winapi_SetNamedPipeHandleState_impl(PyObject *module, HANDLE named_pipe,
Zachary Ware77772c02015-05-13 10:58:35 -05001546 PyObject *mode,
1547 PyObject *max_collection_count,
1548 PyObject *collect_data_timeout)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001549/*[clinic end generated code: output=f2129d222cbfa095 input=9142d72163d0faa6]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001550{
Zachary Waref2244ea2015-05-13 01:22:54 -05001551 PyObject *oArgs[3] = {mode, max_collection_count, collect_data_timeout};
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001552 DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};
1553 int i;
1554
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001555 for (i = 0 ; i < 3 ; i++) {
1556 if (oArgs[i] != Py_None) {
1557 dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]);
1558 if (PyErr_Occurred())
1559 return NULL;
1560 pArgs[i] = &dwArgs[i];
1561 }
1562 }
1563
Zachary Waref2244ea2015-05-13 01:22:54 -05001564 if (!SetNamedPipeHandleState(named_pipe, pArgs[0], pArgs[1], pArgs[2]))
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001565 return PyErr_SetFromWindowsErr(0);
1566
1567 Py_RETURN_NONE;
1568}
1569
Zachary Waref2244ea2015-05-13 01:22:54 -05001570
1571/*[clinic input]
1572_winapi.TerminateProcess
1573
1574 handle: HANDLE
1575 exit_code: UINT
1576 /
1577
1578Terminate the specified process and all of its threads.
1579[clinic start generated code]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001580
1581static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001582_winapi_TerminateProcess_impl(PyObject *module, HANDLE handle,
Zachary Ware77772c02015-05-13 10:58:35 -05001583 UINT exit_code)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001584/*[clinic end generated code: output=f4e99ac3f0b1f34a input=d6bc0aa1ee3bb4df]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001585{
1586 BOOL result;
1587
Zachary Waref2244ea2015-05-13 01:22:54 -05001588 result = TerminateProcess(handle, exit_code);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001589
1590 if (! result)
1591 return PyErr_SetFromWindowsErr(GetLastError());
1592
Zachary Waref2244ea2015-05-13 01:22:54 -05001593 Py_RETURN_NONE;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001594}
1595
Zachary Waref2244ea2015-05-13 01:22:54 -05001596/*[clinic input]
Davin Pottse895de32019-02-23 22:08:16 -06001597_winapi.VirtualQuerySize -> size_t
1598
1599 address: LPCVOID
1600 /
1601[clinic start generated code]*/
1602
1603static size_t
1604_winapi_VirtualQuerySize_impl(PyObject *module, LPCVOID address)
1605/*[clinic end generated code: output=40c8e0ff5ec964df input=6b784a69755d0bb6]*/
1606{
1607 SIZE_T size_of_buf;
1608 MEMORY_BASIC_INFORMATION mem_basic_info;
1609 SIZE_T region_size;
1610
1611 Py_BEGIN_ALLOW_THREADS
1612 size_of_buf = VirtualQuery(address, &mem_basic_info, sizeof(mem_basic_info));
1613 Py_END_ALLOW_THREADS
1614
1615 if (size_of_buf == 0)
1616 PyErr_SetFromWindowsErr(0);
1617
1618 region_size = mem_basic_info.RegionSize;
1619 return region_size;
1620}
1621
1622/*[clinic input]
Zachary Waref2244ea2015-05-13 01:22:54 -05001623_winapi.WaitNamedPipe
1624
1625 name: LPCTSTR
1626 timeout: DWORD
1627 /
1628[clinic start generated code]*/
1629
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001630static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001631_winapi_WaitNamedPipe_impl(PyObject *module, LPCTSTR name, DWORD timeout)
1632/*[clinic end generated code: output=c2866f4439b1fe38 input=36fc781291b1862c]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001633{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001634 BOOL success;
1635
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001636 Py_BEGIN_ALLOW_THREADS
Zachary Waref2244ea2015-05-13 01:22:54 -05001637 success = WaitNamedPipe(name, timeout);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001638 Py_END_ALLOW_THREADS
1639
1640 if (!success)
1641 return PyErr_SetFromWindowsErr(0);
1642
1643 Py_RETURN_NONE;
1644}
1645
Zachary Waref2244ea2015-05-13 01:22:54 -05001646/*[clinic input]
1647_winapi.WaitForMultipleObjects
1648
1649 handle_seq: object
1650 wait_flag: BOOL
1651 milliseconds: DWORD(c_default='INFINITE') = _winapi.INFINITE
1652 /
1653[clinic start generated code]*/
1654
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001655static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001656_winapi_WaitForMultipleObjects_impl(PyObject *module, PyObject *handle_seq,
1657 BOOL wait_flag, DWORD milliseconds)
1658/*[clinic end generated code: output=295e3f00b8e45899 input=36f76ca057cd28a0]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001659{
1660 DWORD result;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001661 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
1662 HANDLE sigint_event = NULL;
1663 Py_ssize_t nhandles, i;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001664
1665 if (!PySequence_Check(handle_seq)) {
1666 PyErr_Format(PyExc_TypeError,
1667 "sequence type expected, got '%s'",
Richard Oudkerk67339272012-08-21 14:54:22 +01001668 Py_TYPE(handle_seq)->tp_name);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001669 return NULL;
1670 }
1671 nhandles = PySequence_Length(handle_seq);
1672 if (nhandles == -1)
1673 return NULL;
1674 if (nhandles < 0 || nhandles >= MAXIMUM_WAIT_OBJECTS - 1) {
1675 PyErr_Format(PyExc_ValueError,
1676 "need at most %zd handles, got a sequence of length %zd",
1677 MAXIMUM_WAIT_OBJECTS - 1, nhandles);
1678 return NULL;
1679 }
1680 for (i = 0; i < nhandles; i++) {
1681 HANDLE h;
1682 PyObject *v = PySequence_GetItem(handle_seq, i);
1683 if (v == NULL)
1684 return NULL;
1685 if (!PyArg_Parse(v, F_HANDLE, &h)) {
1686 Py_DECREF(v);
1687 return NULL;
1688 }
1689 handles[i] = h;
1690 Py_DECREF(v);
1691 }
1692 /* If this is the main thread then make the wait interruptible
1693 by Ctrl-C unless we are waiting for *all* handles */
1694 if (!wait_flag && _PyOS_IsMainThread()) {
1695 sigint_event = _PyOS_SigintEvent();
1696 assert(sigint_event != NULL);
1697 handles[nhandles++] = sigint_event;
1698 }
1699
1700 Py_BEGIN_ALLOW_THREADS
1701 if (sigint_event != NULL)
1702 ResetEvent(sigint_event);
1703 result = WaitForMultipleObjects((DWORD) nhandles, handles,
1704 wait_flag, milliseconds);
1705 Py_END_ALLOW_THREADS
1706
1707 if (result == WAIT_FAILED)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001708 return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001709 else if (sigint_event != NULL && result == WAIT_OBJECT_0 + nhandles - 1) {
1710 errno = EINTR;
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001711 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001712 }
1713
1714 return PyLong_FromLong((int) result);
1715}
1716
Zachary Waref2244ea2015-05-13 01:22:54 -05001717/*[clinic input]
1718_winapi.WaitForSingleObject -> long
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001719
Zachary Waref2244ea2015-05-13 01:22:54 -05001720 handle: HANDLE
1721 milliseconds: DWORD
1722 /
1723
1724Wait for a single object.
1725
1726Wait until the specified object is in the signaled state or
1727the time-out interval elapses. The timeout value is specified
1728in milliseconds.
1729[clinic start generated code]*/
1730
1731static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001732_winapi_WaitForSingleObject_impl(PyObject *module, HANDLE handle,
Zachary Ware77772c02015-05-13 10:58:35 -05001733 DWORD milliseconds)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001734/*[clinic end generated code: output=3c4715d8f1b39859 input=443d1ab076edc7b1]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001735{
1736 DWORD result;
1737
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001738 Py_BEGIN_ALLOW_THREADS
1739 result = WaitForSingleObject(handle, milliseconds);
1740 Py_END_ALLOW_THREADS
1741
Zachary Waref2244ea2015-05-13 01:22:54 -05001742 if (result == WAIT_FAILED) {
1743 PyErr_SetFromWindowsErr(GetLastError());
1744 return -1;
1745 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001746
Zachary Waref2244ea2015-05-13 01:22:54 -05001747 return result;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001748}
1749
Zachary Waref2244ea2015-05-13 01:22:54 -05001750/*[clinic input]
1751_winapi.WriteFile
1752
1753 handle: HANDLE
1754 buffer: object
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001755 overlapped as use_overlapped: bool(accept={int}) = False
Zachary Waref2244ea2015-05-13 01:22:54 -05001756[clinic start generated code]*/
1757
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001758static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001759_winapi_WriteFile_impl(PyObject *module, HANDLE handle, PyObject *buffer,
Zachary Ware77772c02015-05-13 10:58:35 -05001760 int use_overlapped)
Serhiy Storchaka202fda52017-03-12 10:10:47 +02001761/*[clinic end generated code: output=2ca80f6bf3fa92e3 input=11eae2a03aa32731]*/
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001762{
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001763 Py_buffer _buf, *buf;
Victor Stinner71765772013-06-24 23:13:24 +02001764 DWORD len, written;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001765 BOOL ret;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001766 DWORD err;
1767 OverlappedObject *overlapped = NULL;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001768
1769 if (use_overlapped) {
1770 overlapped = new_overlapped(handle);
1771 if (!overlapped)
1772 return NULL;
1773 buf = &overlapped->write_buffer;
1774 }
1775 else
1776 buf = &_buf;
1777
Zachary Waref2244ea2015-05-13 01:22:54 -05001778 if (!PyArg_Parse(buffer, "y*", buf)) {
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001779 Py_XDECREF(overlapped);
1780 return NULL;
1781 }
1782
1783 Py_BEGIN_ALLOW_THREADS
Victor Stinner850a18e2017-10-24 16:53:32 -07001784 len = (DWORD)Py_MIN(buf->len, PY_DWORD_MAX);
Victor Stinner71765772013-06-24 23:13:24 +02001785 ret = WriteFile(handle, buf->buf, len, &written,
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001786 overlapped ? &overlapped->overlapped : NULL);
1787 Py_END_ALLOW_THREADS
1788
1789 err = ret ? 0 : GetLastError();
1790
1791 if (overlapped) {
1792 if (!ret) {
1793 if (err == ERROR_IO_PENDING)
1794 overlapped->pending = 1;
1795 else {
1796 Py_DECREF(overlapped);
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001797 return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001798 }
1799 }
1800 return Py_BuildValue("NI", (PyObject *) overlapped, err);
1801 }
1802
1803 PyBuffer_Release(buf);
1804 if (!ret)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +03001805 return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001806 return Py_BuildValue("II", written, err);
1807}
1808
Victor Stinner91106cd2017-12-13 12:29:09 +01001809/*[clinic input]
1810_winapi.GetACP
1811
1812Get the current Windows ANSI code page identifier.
1813[clinic start generated code]*/
1814
1815static PyObject *
1816_winapi_GetACP_impl(PyObject *module)
1817/*[clinic end generated code: output=f7ee24bf705dbb88 input=1433c96d03a05229]*/
1818{
1819 return PyLong_FromUnsignedLong(GetACP());
1820}
1821
Segev Finerb2a60832017-12-18 11:28:19 +02001822/*[clinic input]
1823_winapi.GetFileType -> DWORD
1824
1825 handle: HANDLE
1826[clinic start generated code]*/
1827
1828static DWORD
1829_winapi_GetFileType_impl(PyObject *module, HANDLE handle)
1830/*[clinic end generated code: output=92b8466ac76ecc17 input=0058366bc40bbfbf]*/
1831{
1832 DWORD result;
1833
1834 Py_BEGIN_ALLOW_THREADS
1835 result = GetFileType(handle);
1836 Py_END_ALLOW_THREADS
1837
1838 if (result == FILE_TYPE_UNKNOWN && GetLastError() != NO_ERROR) {
1839 PyErr_SetFromWindowsErr(0);
1840 return -1;
1841 }
1842
1843 return result;
1844}
1845
Victor Stinner91106cd2017-12-13 12:29:09 +01001846
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001847static PyMethodDef winapi_functions[] = {
Zachary Waref2244ea2015-05-13 01:22:54 -05001848 _WINAPI_CLOSEHANDLE_METHODDEF
1849 _WINAPI_CONNECTNAMEDPIPE_METHODDEF
1850 _WINAPI_CREATEFILE_METHODDEF
Davin Pottse895de32019-02-23 22:08:16 -06001851 _WINAPI_CREATEFILEMAPPING_METHODDEF
Zachary Waref2244ea2015-05-13 01:22:54 -05001852 _WINAPI_CREATENAMEDPIPE_METHODDEF
1853 _WINAPI_CREATEPIPE_METHODDEF
1854 _WINAPI_CREATEPROCESS_METHODDEF
1855 _WINAPI_CREATEJUNCTION_METHODDEF
1856 _WINAPI_DUPLICATEHANDLE_METHODDEF
1857 _WINAPI_EXITPROCESS_METHODDEF
1858 _WINAPI_GETCURRENTPROCESS_METHODDEF
1859 _WINAPI_GETEXITCODEPROCESS_METHODDEF
1860 _WINAPI_GETLASTERROR_METHODDEF
1861 _WINAPI_GETMODULEFILENAME_METHODDEF
1862 _WINAPI_GETSTDHANDLE_METHODDEF
1863 _WINAPI_GETVERSION_METHODDEF
Davin Pottse895de32019-02-23 22:08:16 -06001864 _WINAPI_MAPVIEWOFFILE_METHODDEF
1865 _WINAPI_OPENFILEMAPPING_METHODDEF
Zachary Waref2244ea2015-05-13 01:22:54 -05001866 _WINAPI_OPENPROCESS_METHODDEF
1867 _WINAPI_PEEKNAMEDPIPE_METHODDEF
1868 _WINAPI_READFILE_METHODDEF
1869 _WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF
1870 _WINAPI_TERMINATEPROCESS_METHODDEF
Davin Pottse895de32019-02-23 22:08:16 -06001871 _WINAPI_VIRTUALQUERYSIZE_METHODDEF
Zachary Waref2244ea2015-05-13 01:22:54 -05001872 _WINAPI_WAITNAMEDPIPE_METHODDEF
1873 _WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF
1874 _WINAPI_WAITFORSINGLEOBJECT_METHODDEF
1875 _WINAPI_WRITEFILE_METHODDEF
Victor Stinner91106cd2017-12-13 12:29:09 +01001876 _WINAPI_GETACP_METHODDEF
Segev Finerb2a60832017-12-18 11:28:19 +02001877 _WINAPI_GETFILETYPE_METHODDEF
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001878 {NULL, NULL}
1879};
1880
1881static struct PyModuleDef winapi_module = {
1882 PyModuleDef_HEAD_INIT,
1883 "_winapi",
1884 NULL,
1885 -1,
1886 winapi_functions,
1887 NULL,
1888 NULL,
1889 NULL,
1890 NULL
1891};
1892
1893#define WINAPI_CONSTANT(fmt, con) \
1894 PyDict_SetItemString(d, #con, Py_BuildValue(fmt, con))
1895
1896PyMODINIT_FUNC
1897PyInit__winapi(void)
1898{
1899 PyObject *d;
1900 PyObject *m;
1901
1902 if (PyType_Ready(&OverlappedType) < 0)
1903 return NULL;
1904
1905 m = PyModule_Create(&winapi_module);
1906 if (m == NULL)
1907 return NULL;
1908 d = PyModule_GetDict(m);
1909
1910 PyDict_SetItemString(d, "Overlapped", (PyObject *) &OverlappedType);
1911
1912 /* constants */
1913 WINAPI_CONSTANT(F_DWORD, CREATE_NEW_CONSOLE);
1914 WINAPI_CONSTANT(F_DWORD, CREATE_NEW_PROCESS_GROUP);
1915 WINAPI_CONSTANT(F_DWORD, DUPLICATE_SAME_ACCESS);
Antoine Pitrou5438ed12012-04-24 22:56:57 +02001916 WINAPI_CONSTANT(F_DWORD, DUPLICATE_CLOSE_SOURCE);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001917 WINAPI_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS);
1918 WINAPI_CONSTANT(F_DWORD, ERROR_BROKEN_PIPE);
1919 WINAPI_CONSTANT(F_DWORD, ERROR_IO_PENDING);
1920 WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA);
1921 WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED);
1922 WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001923 WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA);
1924 WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED);
Richard Oudkerkfdb8dcf2012-05-05 19:45:37 +01001925 WINAPI_CONSTANT(F_DWORD, ERROR_NO_DATA);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001926 WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES);
1927 WINAPI_CONSTANT(F_DWORD, ERROR_OPERATION_ABORTED);
1928 WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);
1929 WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
1930 WINAPI_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
1931 WINAPI_CONSTANT(F_DWORD, FILE_FLAG_FIRST_PIPE_INSTANCE);
1932 WINAPI_CONSTANT(F_DWORD, FILE_FLAG_OVERLAPPED);
Antoine Pitrou5438ed12012-04-24 22:56:57 +02001933 WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_READ);
1934 WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_WRITE);
Davin Pottse895de32019-02-23 22:08:16 -06001935 WINAPI_CONSTANT(F_DWORD, FILE_MAP_ALL_ACCESS);
1936 WINAPI_CONSTANT(F_DWORD, FILE_MAP_COPY);
1937 WINAPI_CONSTANT(F_DWORD, FILE_MAP_EXECUTE);
1938 WINAPI_CONSTANT(F_DWORD, FILE_MAP_READ);
1939 WINAPI_CONSTANT(F_DWORD, FILE_MAP_WRITE);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001940 WINAPI_CONSTANT(F_DWORD, GENERIC_READ);
1941 WINAPI_CONSTANT(F_DWORD, GENERIC_WRITE);
1942 WINAPI_CONSTANT(F_DWORD, INFINITE);
Davin Pottse895de32019-02-23 22:08:16 -06001943 WINAPI_CONSTANT(F_HANDLE, INVALID_HANDLE_VALUE);
1944 WINAPI_CONSTANT(F_DWORD, MEM_COMMIT);
1945 WINAPI_CONSTANT(F_DWORD, MEM_FREE);
1946 WINAPI_CONSTANT(F_DWORD, MEM_IMAGE);
1947 WINAPI_CONSTANT(F_DWORD, MEM_MAPPED);
1948 WINAPI_CONSTANT(F_DWORD, MEM_PRIVATE);
1949 WINAPI_CONSTANT(F_DWORD, MEM_RESERVE);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001950 WINAPI_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER);
1951 WINAPI_CONSTANT(F_DWORD, OPEN_EXISTING);
Davin Pottse895de32019-02-23 22:08:16 -06001952 WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE);
1953 WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE_READ);
1954 WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE_READWRITE);
1955 WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE_WRITECOPY);
1956 WINAPI_CONSTANT(F_DWORD, PAGE_GUARD);
1957 WINAPI_CONSTANT(F_DWORD, PAGE_NOACCESS);
1958 WINAPI_CONSTANT(F_DWORD, PAGE_NOCACHE);
1959 WINAPI_CONSTANT(F_DWORD, PAGE_READONLY);
1960 WINAPI_CONSTANT(F_DWORD, PAGE_READWRITE);
1961 WINAPI_CONSTANT(F_DWORD, PAGE_WRITECOMBINE);
1962 WINAPI_CONSTANT(F_DWORD, PAGE_WRITECOPY);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001963 WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX);
1964 WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND);
1965 WINAPI_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE);
1966 WINAPI_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE);
1967 WINAPI_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES);
1968 WINAPI_CONSTANT(F_DWORD, PIPE_WAIT);
1969 WINAPI_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS);
Antoine Pitrou5438ed12012-04-24 22:56:57 +02001970 WINAPI_CONSTANT(F_DWORD, PROCESS_DUP_HANDLE);
Davin Pottse895de32019-02-23 22:08:16 -06001971 WINAPI_CONSTANT(F_DWORD, SEC_COMMIT);
1972 WINAPI_CONSTANT(F_DWORD, SEC_IMAGE);
1973 WINAPI_CONSTANT(F_DWORD, SEC_LARGE_PAGES);
1974 WINAPI_CONSTANT(F_DWORD, SEC_NOCACHE);
1975 WINAPI_CONSTANT(F_DWORD, SEC_RESERVE);
1976 WINAPI_CONSTANT(F_DWORD, SEC_WRITECOMBINE);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001977 WINAPI_CONSTANT(F_DWORD, STARTF_USESHOWWINDOW);
1978 WINAPI_CONSTANT(F_DWORD, STARTF_USESTDHANDLES);
1979 WINAPI_CONSTANT(F_DWORD, STD_INPUT_HANDLE);
1980 WINAPI_CONSTANT(F_DWORD, STD_OUTPUT_HANDLE);
1981 WINAPI_CONSTANT(F_DWORD, STD_ERROR_HANDLE);
1982 WINAPI_CONSTANT(F_DWORD, STILL_ACTIVE);
1983 WINAPI_CONSTANT(F_DWORD, SW_HIDE);
1984 WINAPI_CONSTANT(F_DWORD, WAIT_OBJECT_0);
Victor Stinner373f0a92014-03-20 09:26:55 +01001985 WINAPI_CONSTANT(F_DWORD, WAIT_ABANDONED_0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001986 WINAPI_CONSTANT(F_DWORD, WAIT_TIMEOUT);
Victor Stinner91106cd2017-12-13 12:29:09 +01001987
Jamesb5d9e082017-11-08 14:18:59 +00001988 WINAPI_CONSTANT(F_DWORD, ABOVE_NORMAL_PRIORITY_CLASS);
1989 WINAPI_CONSTANT(F_DWORD, BELOW_NORMAL_PRIORITY_CLASS);
1990 WINAPI_CONSTANT(F_DWORD, HIGH_PRIORITY_CLASS);
1991 WINAPI_CONSTANT(F_DWORD, IDLE_PRIORITY_CLASS);
1992 WINAPI_CONSTANT(F_DWORD, NORMAL_PRIORITY_CLASS);
1993 WINAPI_CONSTANT(F_DWORD, REALTIME_PRIORITY_CLASS);
Victor Stinner91106cd2017-12-13 12:29:09 +01001994
Jamesb5d9e082017-11-08 14:18:59 +00001995 WINAPI_CONSTANT(F_DWORD, CREATE_NO_WINDOW);
1996 WINAPI_CONSTANT(F_DWORD, DETACHED_PROCESS);
1997 WINAPI_CONSTANT(F_DWORD, CREATE_DEFAULT_ERROR_MODE);
1998 WINAPI_CONSTANT(F_DWORD, CREATE_BREAKAWAY_FROM_JOB);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001999
Segev Finerb2a60832017-12-18 11:28:19 +02002000 WINAPI_CONSTANT(F_DWORD, FILE_TYPE_UNKNOWN);
2001 WINAPI_CONSTANT(F_DWORD, FILE_TYPE_DISK);
2002 WINAPI_CONSTANT(F_DWORD, FILE_TYPE_CHAR);
2003 WINAPI_CONSTANT(F_DWORD, FILE_TYPE_PIPE);
2004 WINAPI_CONSTANT(F_DWORD, FILE_TYPE_REMOTE);
2005
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02002006 WINAPI_CONSTANT("i", NULL);
2007
2008 return m;
2009}