Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1 | /* |
| 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 Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 43 | #include "winreparse.h" |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 44 | |
| 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 Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 61 | |
| 62 | #define T_HANDLE T_POINTER |
| 63 | |
| 64 | /* Grab CancelIoEx dynamically from kernel32 */ |
| 65 | static int has_CancelIoEx = -1; |
| 66 | static BOOL (CALLBACK *Py_CancelIoEx)(HANDLE, LPOVERLAPPED); |
| 67 | |
| 68 | static int |
| 69 | check_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 | |
| 87 | typedef 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 | |
| 102 | static void |
| 103 | overlapped_dealloc(OverlappedObject *self) |
| 104 | { |
| 105 | DWORD bytes; |
| 106 | int err = GetLastError(); |
Richard Oudkerk | 633db6f | 2013-11-17 13:15:51 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 108 | if (self->pending) { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 109 | if (check_CancelIoEx() && |
Richard Oudkerk | 633db6f | 2013-11-17 13:15:51 +0000 | [diff] [blame] | 110 | 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 Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 115 | else if (_Py_IsFinalizing()) |
Richard Oudkerk | 633db6f | 2013-11-17 13:15:51 +0000 | [diff] [blame] | 116 | { |
| 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 Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 136 | } |
Richard Oudkerk | 633db6f | 2013-11-17 13:15:51 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 138 | 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 Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 146 | /*[clinic input] |
| 147 | module _winapi |
| 148 | class _winapi.Overlapped "OverlappedObject *" "&OverlappedType" |
| 149 | [clinic start generated code]*/ |
| 150 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c13d3f5fd1dabb84]*/ |
| 151 | |
| 152 | /*[python input] |
| 153 | def 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 |
| 159 | create_converter('HANDLE', '" F_HANDLE "') |
| 160 | create_converter('HMODULE', '" F_HANDLE "') |
| 161 | create_converter('LPSECURITY_ATTRIBUTES', '" F_POINTER "') |
| 162 | |
| 163 | create_converter('BOOL', 'i') # F_BOOL used previously (always 'i') |
| 164 | create_converter('DWORD', 'k') # F_DWORD is always "k" (which is much shorter) |
| 165 | create_converter('LPCTSTR', 's') |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 166 | create_converter('LPCWSTR', 'u') |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 167 | create_converter('LPWSTR', 'u') |
| 168 | create_converter('UINT', 'I') # F_UINT used previously (always 'I') |
| 169 | |
| 170 | class HANDLE_return_converter(CReturnConverter): |
| 171 | type = 'HANDLE' |
| 172 | |
| 173 | def render(self, function, data): |
| 174 | self.declare(data) |
| 175 | self.err_occurred_if("_return_value == INVALID_HANDLE_VALUE", data) |
| 176 | data.return_conversion.append( |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 177 | 'if (_return_value == NULL) {\n Py_RETURN_NONE;\n}\n') |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 178 | data.return_conversion.append( |
| 179 | 'return_value = HANDLE_TO_PYNUM(_return_value);\n') |
| 180 | |
| 181 | class DWORD_return_converter(CReturnConverter): |
| 182 | type = 'DWORD' |
| 183 | |
| 184 | def render(self, function, data): |
| 185 | self.declare(data) |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 186 | self.err_occurred_if("_return_value == PY_DWORD_MAX", data) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 187 | data.return_conversion.append( |
| 188 | 'return_value = Py_BuildValue("k", _return_value);\n') |
| 189 | [python start generated code]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 190 | /*[python end generated code: output=da39a3ee5e6b4b0d input=27456f8555228b62]*/ |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 191 | |
| 192 | #include "clinic/_winapi.c.h" |
| 193 | |
| 194 | /*[clinic input] |
| 195 | _winapi.Overlapped.GetOverlappedResult |
| 196 | |
| 197 | wait: bool |
| 198 | / |
| 199 | [clinic start generated code]*/ |
| 200 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 201 | static PyObject * |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 202 | _winapi_Overlapped_GetOverlappedResult_impl(OverlappedObject *self, int wait) |
| 203 | /*[clinic end generated code: output=bdd0c1ed6518cd03 input=194505ee8e0e3565]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 204 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 205 | BOOL res; |
| 206 | DWORD transferred = 0; |
| 207 | DWORD err; |
| 208 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 209 | Py_BEGIN_ALLOW_THREADS |
| 210 | res = GetOverlappedResult(self->handle, &self->overlapped, &transferred, |
| 211 | wait != 0); |
| 212 | Py_END_ALLOW_THREADS |
| 213 | |
| 214 | err = res ? ERROR_SUCCESS : GetLastError(); |
| 215 | switch (err) { |
| 216 | case ERROR_SUCCESS: |
| 217 | case ERROR_MORE_DATA: |
| 218 | case ERROR_OPERATION_ABORTED: |
| 219 | self->completed = 1; |
| 220 | self->pending = 0; |
| 221 | break; |
| 222 | case ERROR_IO_INCOMPLETE: |
| 223 | break; |
| 224 | default: |
| 225 | self->pending = 0; |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 226 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, err); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 227 | } |
| 228 | if (self->completed && self->read_buffer != NULL) { |
| 229 | assert(PyBytes_CheckExact(self->read_buffer)); |
| 230 | if (transferred != PyBytes_GET_SIZE(self->read_buffer) && |
| 231 | _PyBytes_Resize(&self->read_buffer, transferred)) |
| 232 | return NULL; |
| 233 | } |
| 234 | return Py_BuildValue("II", (unsigned) transferred, (unsigned) err); |
| 235 | } |
| 236 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 237 | /*[clinic input] |
| 238 | _winapi.Overlapped.getbuffer |
| 239 | [clinic start generated code]*/ |
| 240 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 241 | static PyObject * |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 242 | _winapi_Overlapped_getbuffer_impl(OverlappedObject *self) |
| 243 | /*[clinic end generated code: output=95a3eceefae0f748 input=347fcfd56b4ceabd]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 244 | { |
| 245 | PyObject *res; |
| 246 | if (!self->completed) { |
| 247 | PyErr_SetString(PyExc_ValueError, |
| 248 | "can't get read buffer before GetOverlappedResult() " |
| 249 | "signals the operation completed"); |
| 250 | return NULL; |
| 251 | } |
| 252 | res = self->read_buffer ? self->read_buffer : Py_None; |
| 253 | Py_INCREF(res); |
| 254 | return res; |
| 255 | } |
| 256 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 257 | /*[clinic input] |
| 258 | _winapi.Overlapped.cancel |
| 259 | [clinic start generated code]*/ |
| 260 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 261 | static PyObject * |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 262 | _winapi_Overlapped_cancel_impl(OverlappedObject *self) |
| 263 | /*[clinic end generated code: output=fcb9ab5df4ebdae5 input=cbf3da142290039f]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 264 | { |
| 265 | BOOL res = TRUE; |
| 266 | |
| 267 | if (self->pending) { |
| 268 | Py_BEGIN_ALLOW_THREADS |
| 269 | if (check_CancelIoEx()) |
| 270 | res = Py_CancelIoEx(self->handle, &self->overlapped); |
| 271 | else |
| 272 | res = CancelIo(self->handle); |
| 273 | Py_END_ALLOW_THREADS |
| 274 | } |
| 275 | |
| 276 | /* CancelIoEx returns ERROR_NOT_FOUND if the I/O completed in-between */ |
| 277 | if (!res && GetLastError() != ERROR_NOT_FOUND) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 278 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 279 | self->pending = 0; |
| 280 | Py_RETURN_NONE; |
| 281 | } |
| 282 | |
| 283 | static PyMethodDef overlapped_methods[] = { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 284 | _WINAPI_OVERLAPPED_GETOVERLAPPEDRESULT_METHODDEF |
| 285 | _WINAPI_OVERLAPPED_GETBUFFER_METHODDEF |
| 286 | _WINAPI_OVERLAPPED_CANCEL_METHODDEF |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 287 | {NULL} |
| 288 | }; |
| 289 | |
| 290 | static PyMemberDef overlapped_members[] = { |
| 291 | {"event", T_HANDLE, |
| 292 | offsetof(OverlappedObject, overlapped) + offsetof(OVERLAPPED, hEvent), |
| 293 | READONLY, "overlapped event handle"}, |
| 294 | {NULL} |
| 295 | }; |
| 296 | |
| 297 | PyTypeObject OverlappedType = { |
| 298 | PyVarObject_HEAD_INIT(NULL, 0) |
| 299 | /* tp_name */ "_winapi.Overlapped", |
| 300 | /* tp_basicsize */ sizeof(OverlappedObject), |
| 301 | /* tp_itemsize */ 0, |
| 302 | /* tp_dealloc */ (destructor) overlapped_dealloc, |
| 303 | /* tp_print */ 0, |
| 304 | /* tp_getattr */ 0, |
| 305 | /* tp_setattr */ 0, |
| 306 | /* tp_reserved */ 0, |
| 307 | /* tp_repr */ 0, |
| 308 | /* tp_as_number */ 0, |
| 309 | /* tp_as_sequence */ 0, |
| 310 | /* tp_as_mapping */ 0, |
| 311 | /* tp_hash */ 0, |
| 312 | /* tp_call */ 0, |
| 313 | /* tp_str */ 0, |
| 314 | /* tp_getattro */ 0, |
| 315 | /* tp_setattro */ 0, |
| 316 | /* tp_as_buffer */ 0, |
| 317 | /* tp_flags */ Py_TPFLAGS_DEFAULT, |
| 318 | /* tp_doc */ "OVERLAPPED structure wrapper", |
| 319 | /* tp_traverse */ 0, |
| 320 | /* tp_clear */ 0, |
| 321 | /* tp_richcompare */ 0, |
| 322 | /* tp_weaklistoffset */ 0, |
| 323 | /* tp_iter */ 0, |
| 324 | /* tp_iternext */ 0, |
| 325 | /* tp_methods */ overlapped_methods, |
| 326 | /* tp_members */ overlapped_members, |
| 327 | /* tp_getset */ 0, |
| 328 | /* tp_base */ 0, |
| 329 | /* tp_dict */ 0, |
| 330 | /* tp_descr_get */ 0, |
| 331 | /* tp_descr_set */ 0, |
| 332 | /* tp_dictoffset */ 0, |
| 333 | /* tp_init */ 0, |
| 334 | /* tp_alloc */ 0, |
| 335 | /* tp_new */ 0, |
| 336 | }; |
| 337 | |
| 338 | static OverlappedObject * |
| 339 | new_overlapped(HANDLE handle) |
| 340 | { |
| 341 | OverlappedObject *self; |
| 342 | |
| 343 | self = PyObject_New(OverlappedObject, &OverlappedType); |
| 344 | if (!self) |
| 345 | return NULL; |
| 346 | self->handle = handle; |
| 347 | self->read_buffer = NULL; |
| 348 | self->pending = 0; |
| 349 | self->completed = 0; |
| 350 | memset(&self->overlapped, 0, sizeof(OVERLAPPED)); |
| 351 | memset(&self->write_buffer, 0, sizeof(Py_buffer)); |
| 352 | /* Manual reset, initially non-signalled */ |
| 353 | self->overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 354 | return self; |
| 355 | } |
| 356 | |
| 357 | /* -------------------------------------------------------------------- */ |
| 358 | /* windows API functions */ |
| 359 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 360 | /*[clinic input] |
| 361 | _winapi.CloseHandle |
| 362 | |
| 363 | handle: HANDLE |
| 364 | / |
| 365 | |
| 366 | Close handle. |
| 367 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 368 | |
| 369 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 370 | _winapi_CloseHandle_impl(PyObject *module, HANDLE handle) |
| 371 | /*[clinic end generated code: output=7ad37345f07bd782 input=7f0e4ac36e0352b8]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 372 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 373 | BOOL success; |
| 374 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 375 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 376 | success = CloseHandle(handle); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 377 | Py_END_ALLOW_THREADS |
| 378 | |
| 379 | if (!success) |
| 380 | return PyErr_SetFromWindowsErr(0); |
| 381 | |
| 382 | Py_RETURN_NONE; |
| 383 | } |
| 384 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 385 | /*[clinic input] |
| 386 | _winapi.ConnectNamedPipe |
| 387 | |
| 388 | handle: HANDLE |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 389 | overlapped as use_overlapped: bool(accept={int}) = False |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 390 | [clinic start generated code]*/ |
| 391 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 392 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 393 | _winapi_ConnectNamedPipe_impl(PyObject *module, HANDLE handle, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 394 | int use_overlapped) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 395 | /*[clinic end generated code: output=335a0e7086800671 input=34f937c1c86e5e68]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 396 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 397 | BOOL success; |
| 398 | OverlappedObject *overlapped = NULL; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 399 | |
| 400 | if (use_overlapped) { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 401 | overlapped = new_overlapped(handle); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 402 | if (!overlapped) |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 407 | success = ConnectNamedPipe(handle, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 408 | overlapped ? &overlapped->overlapped : NULL); |
| 409 | Py_END_ALLOW_THREADS |
| 410 | |
| 411 | if (overlapped) { |
| 412 | int err = GetLastError(); |
| 413 | /* Overlapped ConnectNamedPipe never returns a success code */ |
| 414 | assert(success == 0); |
| 415 | if (err == ERROR_IO_PENDING) |
| 416 | overlapped->pending = 1; |
| 417 | else if (err == ERROR_PIPE_CONNECTED) |
| 418 | SetEvent(overlapped->overlapped.hEvent); |
| 419 | else { |
| 420 | Py_DECREF(overlapped); |
| 421 | return PyErr_SetFromWindowsErr(err); |
| 422 | } |
| 423 | return (PyObject *) overlapped; |
| 424 | } |
| 425 | if (!success) |
| 426 | return PyErr_SetFromWindowsErr(0); |
| 427 | |
| 428 | Py_RETURN_NONE; |
| 429 | } |
| 430 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 431 | /*[clinic input] |
| 432 | _winapi.CreateFile -> HANDLE |
| 433 | |
| 434 | file_name: LPCTSTR |
| 435 | desired_access: DWORD |
| 436 | share_mode: DWORD |
| 437 | security_attributes: LPSECURITY_ATTRIBUTES |
| 438 | creation_disposition: DWORD |
| 439 | flags_and_attributes: DWORD |
| 440 | template_file: HANDLE |
| 441 | / |
| 442 | [clinic start generated code]*/ |
| 443 | |
| 444 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 445 | _winapi_CreateFile_impl(PyObject *module, LPCTSTR file_name, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 446 | DWORD desired_access, DWORD share_mode, |
| 447 | LPSECURITY_ATTRIBUTES security_attributes, |
| 448 | DWORD creation_disposition, |
| 449 | DWORD flags_and_attributes, HANDLE template_file) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 450 | /*[clinic end generated code: output=417ddcebfc5a3d53 input=6423c3e40372dbd5]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 451 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 452 | HANDLE handle; |
| 453 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 454 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 455 | handle = CreateFile(file_name, desired_access, |
| 456 | share_mode, security_attributes, |
| 457 | creation_disposition, |
| 458 | flags_and_attributes, template_file); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 459 | Py_END_ALLOW_THREADS |
| 460 | |
| 461 | if (handle == INVALID_HANDLE_VALUE) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 462 | PyErr_SetFromWindowsErr(0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 463 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 464 | return handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 467 | /*[clinic input] |
| 468 | _winapi.CreateJunction |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 469 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 470 | src_path: LPWSTR |
| 471 | dst_path: LPWSTR |
| 472 | / |
| 473 | [clinic start generated code]*/ |
| 474 | |
| 475 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 476 | _winapi_CreateJunction_impl(PyObject *module, LPWSTR src_path, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 477 | LPWSTR dst_path) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 478 | /*[clinic end generated code: output=66b7eb746e1dfa25 input=8cd1f9964b6e3d36]*/ |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 479 | { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 480 | /* Privilege adjustment */ |
| 481 | HANDLE token = NULL; |
| 482 | TOKEN_PRIVILEGES tp; |
| 483 | |
| 484 | /* Reparse data buffer */ |
| 485 | const USHORT prefix_len = 4; |
| 486 | USHORT print_len = 0; |
| 487 | USHORT rdb_size = 0; |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 488 | _Py_PREPARSE_DATA_BUFFER rdb = NULL; |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 489 | |
| 490 | /* Junction point creation */ |
| 491 | HANDLE junction = NULL; |
| 492 | DWORD ret = 0; |
| 493 | |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 494 | if (src_path == NULL || dst_path == NULL) |
| 495 | return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER); |
| 496 | |
| 497 | if (wcsncmp(src_path, L"\\??\\", prefix_len) == 0) |
| 498 | return PyErr_SetFromWindowsErr(ERROR_INVALID_PARAMETER); |
| 499 | |
| 500 | /* Adjust privileges to allow rewriting directory entry as a |
| 501 | junction point. */ |
| 502 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) |
| 503 | goto cleanup; |
| 504 | |
| 505 | if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid)) |
| 506 | goto cleanup; |
| 507 | |
| 508 | tp.PrivilegeCount = 1; |
| 509 | tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 510 | if (!AdjustTokenPrivileges(token, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), |
| 511 | NULL, NULL)) |
| 512 | goto cleanup; |
| 513 | |
| 514 | if (GetFileAttributesW(src_path) == INVALID_FILE_ATTRIBUTES) |
| 515 | goto cleanup; |
| 516 | |
| 517 | /* Store the absolute link target path length in print_len. */ |
| 518 | print_len = (USHORT)GetFullPathNameW(src_path, 0, NULL, NULL); |
| 519 | if (print_len == 0) |
| 520 | goto cleanup; |
| 521 | |
| 522 | /* NUL terminator should not be part of print_len. */ |
| 523 | --print_len; |
| 524 | |
| 525 | /* REPARSE_DATA_BUFFER usage is heavily under-documented, especially for |
| 526 | junction points. Here's what I've learned along the way: |
| 527 | - A junction point has two components: a print name and a substitute |
| 528 | name. They both describe the link target, but the substitute name is |
| 529 | the physical target and the print name is shown in directory listings. |
| 530 | - The print name must be a native name, prefixed with "\??\". |
| 531 | - Both names are stored after each other in the same buffer (the |
| 532 | PathBuffer) and both must be NUL-terminated. |
| 533 | - There are four members defining their respective offset and length |
| 534 | inside PathBuffer: SubstituteNameOffset, SubstituteNameLength, |
| 535 | PrintNameOffset and PrintNameLength. |
| 536 | - The total size we need to allocate for the REPARSE_DATA_BUFFER, thus, |
| 537 | is the sum of: |
| 538 | - the fixed header size (REPARSE_DATA_BUFFER_HEADER_SIZE) |
| 539 | - the size of the MountPointReparseBuffer member without the PathBuffer |
| 540 | - the size of the prefix ("\??\") in bytes |
| 541 | - the size of the print name in bytes |
| 542 | - the size of the substitute name in bytes |
| 543 | - the size of two NUL terminators in bytes */ |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 544 | rdb_size = _Py_REPARSE_DATA_BUFFER_HEADER_SIZE + |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 545 | sizeof(rdb->MountPointReparseBuffer) - |
| 546 | sizeof(rdb->MountPointReparseBuffer.PathBuffer) + |
| 547 | /* Two +1's for NUL terminators. */ |
| 548 | (prefix_len + print_len + 1 + print_len + 1) * sizeof(WCHAR); |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 549 | rdb = (_Py_PREPARSE_DATA_BUFFER)PyMem_RawMalloc(rdb_size); |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 550 | if (rdb == NULL) |
| 551 | goto cleanup; |
| 552 | |
| 553 | memset(rdb, 0, rdb_size); |
| 554 | rdb->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 555 | rdb->ReparseDataLength = rdb_size - _Py_REPARSE_DATA_BUFFER_HEADER_SIZE; |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 556 | rdb->MountPointReparseBuffer.SubstituteNameOffset = 0; |
| 557 | rdb->MountPointReparseBuffer.SubstituteNameLength = |
| 558 | (prefix_len + print_len) * sizeof(WCHAR); |
| 559 | rdb->MountPointReparseBuffer.PrintNameOffset = |
| 560 | rdb->MountPointReparseBuffer.SubstituteNameLength + sizeof(WCHAR); |
| 561 | rdb->MountPointReparseBuffer.PrintNameLength = print_len * sizeof(WCHAR); |
| 562 | |
| 563 | /* Store the full native path of link target at the substitute name |
| 564 | offset (0). */ |
| 565 | wcscpy(rdb->MountPointReparseBuffer.PathBuffer, L"\\??\\"); |
| 566 | if (GetFullPathNameW(src_path, print_len + 1, |
| 567 | rdb->MountPointReparseBuffer.PathBuffer + prefix_len, |
| 568 | NULL) == 0) |
| 569 | goto cleanup; |
| 570 | |
| 571 | /* Copy everything but the native prefix to the print name offset. */ |
| 572 | wcscpy(rdb->MountPointReparseBuffer.PathBuffer + |
| 573 | prefix_len + print_len + 1, |
| 574 | rdb->MountPointReparseBuffer.PathBuffer + prefix_len); |
| 575 | |
| 576 | /* Create a directory for the junction point. */ |
| 577 | if (!CreateDirectoryW(dst_path, NULL)) |
| 578 | goto cleanup; |
| 579 | |
| 580 | junction = CreateFileW(dst_path, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
| 581 | OPEN_EXISTING, |
| 582 | FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 583 | if (junction == INVALID_HANDLE_VALUE) |
| 584 | goto cleanup; |
| 585 | |
| 586 | /* Make the directory entry a junction point. */ |
| 587 | if (!DeviceIoControl(junction, FSCTL_SET_REPARSE_POINT, rdb, rdb_size, |
| 588 | NULL, 0, &ret, NULL)) |
| 589 | goto cleanup; |
| 590 | |
| 591 | cleanup: |
| 592 | ret = GetLastError(); |
| 593 | |
| 594 | CloseHandle(token); |
| 595 | CloseHandle(junction); |
| 596 | PyMem_RawFree(rdb); |
| 597 | |
| 598 | if (ret != 0) |
| 599 | return PyErr_SetFromWindowsErr(ret); |
| 600 | |
| 601 | Py_RETURN_NONE; |
| 602 | } |
| 603 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 604 | /*[clinic input] |
| 605 | _winapi.CreateNamedPipe -> HANDLE |
| 606 | |
| 607 | name: LPCTSTR |
| 608 | open_mode: DWORD |
| 609 | pipe_mode: DWORD |
| 610 | max_instances: DWORD |
| 611 | out_buffer_size: DWORD |
| 612 | in_buffer_size: DWORD |
| 613 | default_timeout: DWORD |
| 614 | security_attributes: LPSECURITY_ATTRIBUTES |
| 615 | / |
| 616 | [clinic start generated code]*/ |
| 617 | |
| 618 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 619 | _winapi_CreateNamedPipe_impl(PyObject *module, LPCTSTR name, DWORD open_mode, |
| 620 | DWORD pipe_mode, DWORD max_instances, |
| 621 | DWORD out_buffer_size, DWORD in_buffer_size, |
| 622 | DWORD default_timeout, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 623 | LPSECURITY_ATTRIBUTES security_attributes) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 624 | /*[clinic end generated code: output=80f8c07346a94fbc input=5a73530b84d8bc37]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 625 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 626 | HANDLE handle; |
| 627 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 628 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 629 | handle = CreateNamedPipe(name, open_mode, pipe_mode, |
| 630 | max_instances, out_buffer_size, |
| 631 | in_buffer_size, default_timeout, |
| 632 | security_attributes); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 633 | Py_END_ALLOW_THREADS |
| 634 | |
| 635 | if (handle == INVALID_HANDLE_VALUE) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 636 | PyErr_SetFromWindowsErr(0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 637 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 638 | return handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 639 | } |
| 640 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 641 | /*[clinic input] |
| 642 | _winapi.CreatePipe |
| 643 | |
| 644 | pipe_attrs: object |
| 645 | Ignored internally, can be None. |
| 646 | size: DWORD |
| 647 | / |
| 648 | |
| 649 | Create an anonymous pipe. |
| 650 | |
| 651 | Returns a 2-tuple of handles, to the read and write ends of the pipe. |
| 652 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 653 | |
| 654 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 655 | _winapi_CreatePipe_impl(PyObject *module, PyObject *pipe_attrs, DWORD size) |
| 656 | /*[clinic end generated code: output=1c4411d8699f0925 input=c4f2cfa56ef68d90]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 657 | { |
| 658 | HANDLE read_pipe; |
| 659 | HANDLE write_pipe; |
| 660 | BOOL result; |
| 661 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 662 | Py_BEGIN_ALLOW_THREADS |
| 663 | result = CreatePipe(&read_pipe, &write_pipe, NULL, size); |
| 664 | Py_END_ALLOW_THREADS |
| 665 | |
| 666 | if (! result) |
| 667 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 668 | |
| 669 | return Py_BuildValue( |
| 670 | "NN", HANDLE_TO_PYNUM(read_pipe), HANDLE_TO_PYNUM(write_pipe)); |
| 671 | } |
| 672 | |
| 673 | /* helpers for createprocess */ |
| 674 | |
| 675 | static unsigned long |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 676 | getulong(PyObject* obj, const char* name) |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 677 | { |
| 678 | PyObject* value; |
| 679 | unsigned long ret; |
| 680 | |
| 681 | value = PyObject_GetAttrString(obj, name); |
| 682 | if (! value) { |
| 683 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 684 | return 0; |
| 685 | } |
| 686 | ret = PyLong_AsUnsignedLong(value); |
| 687 | Py_DECREF(value); |
| 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | static HANDLE |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 692 | gethandle(PyObject* obj, const char* name) |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 693 | { |
| 694 | PyObject* value; |
| 695 | HANDLE ret; |
| 696 | |
| 697 | value = PyObject_GetAttrString(obj, name); |
| 698 | if (! value) { |
| 699 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 700 | return NULL; |
| 701 | } |
| 702 | if (value == Py_None) |
| 703 | ret = NULL; |
| 704 | else |
| 705 | ret = PYNUM_TO_HANDLE(value); |
| 706 | Py_DECREF(value); |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | static PyObject* |
| 711 | getenvironment(PyObject* environment) |
| 712 | { |
| 713 | Py_ssize_t i, envsize, totalsize; |
| 714 | Py_UCS4 *buffer = NULL, *p, *end; |
| 715 | PyObject *keys, *values, *res; |
| 716 | |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 717 | /* convert environment dictionary to windows environment string */ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 718 | if (! PyMapping_Check(environment)) { |
| 719 | PyErr_SetString( |
| 720 | PyExc_TypeError, "environment must be dictionary or None"); |
| 721 | return NULL; |
| 722 | } |
| 723 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 724 | keys = PyMapping_Keys(environment); |
Oren Milman | 0b3a87e | 2017-09-14 22:30:28 +0300 | [diff] [blame] | 725 | if (!keys) { |
| 726 | return NULL; |
| 727 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 728 | values = PyMapping_Values(environment); |
Oren Milman | 0b3a87e | 2017-09-14 22:30:28 +0300 | [diff] [blame] | 729 | if (!values) { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 730 | goto error; |
Oren Milman | 0b3a87e | 2017-09-14 22:30:28 +0300 | [diff] [blame] | 731 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 732 | |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 733 | envsize = PySequence_Fast_GET_SIZE(keys); |
| 734 | if (PySequence_Fast_GET_SIZE(values) != envsize) { |
| 735 | PyErr_SetString(PyExc_RuntimeError, |
| 736 | "environment changed size during iteration"); |
| 737 | goto error; |
| 738 | } |
| 739 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 740 | totalsize = 1; /* trailing null character */ |
| 741 | for (i = 0; i < envsize; i++) { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 742 | PyObject* key = PySequence_Fast_GET_ITEM(keys, i); |
| 743 | PyObject* value = PySequence_Fast_GET_ITEM(values, i); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 744 | |
| 745 | if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) { |
| 746 | PyErr_SetString(PyExc_TypeError, |
| 747 | "environment can only contain strings"); |
| 748 | goto error; |
| 749 | } |
Serhiy Storchaka | d174d24 | 2017-06-23 19:39:27 +0300 | [diff] [blame] | 750 | if (PyUnicode_FindChar(key, '\0', 0, PyUnicode_GET_LENGTH(key), 1) != -1 || |
| 751 | PyUnicode_FindChar(value, '\0', 0, PyUnicode_GET_LENGTH(value), 1) != -1) |
| 752 | { |
| 753 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 754 | goto error; |
| 755 | } |
| 756 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 757 | defining hidden environment variables. */ |
| 758 | if (PyUnicode_GET_LENGTH(key) == 0 || |
| 759 | PyUnicode_FindChar(key, '=', 1, PyUnicode_GET_LENGTH(key), 1) != -1) |
| 760 | { |
| 761 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 762 | goto error; |
| 763 | } |
Benjamin Peterson | 8ce6806 | 2015-02-09 20:58:12 -0500 | [diff] [blame] | 764 | if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(key) - 1) { |
| 765 | PyErr_SetString(PyExc_OverflowError, "environment too long"); |
| 766 | goto error; |
| 767 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 768 | totalsize += PyUnicode_GET_LENGTH(key) + 1; /* +1 for '=' */ |
Benjamin Peterson | 8ce6806 | 2015-02-09 20:58:12 -0500 | [diff] [blame] | 769 | if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(value) - 1) { |
| 770 | PyErr_SetString(PyExc_OverflowError, "environment too long"); |
| 771 | goto error; |
| 772 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 773 | totalsize += PyUnicode_GET_LENGTH(value) + 1; /* +1 for '\0' */ |
| 774 | } |
| 775 | |
Benjamin Peterson | 8ce6806 | 2015-02-09 20:58:12 -0500 | [diff] [blame] | 776 | buffer = PyMem_NEW(Py_UCS4, totalsize); |
| 777 | if (! buffer) { |
| 778 | PyErr_NoMemory(); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 779 | goto error; |
Benjamin Peterson | 8ce6806 | 2015-02-09 20:58:12 -0500 | [diff] [blame] | 780 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 781 | p = buffer; |
| 782 | end = buffer + totalsize; |
| 783 | |
| 784 | for (i = 0; i < envsize; i++) { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 785 | PyObject* key = PySequence_Fast_GET_ITEM(keys, i); |
| 786 | PyObject* value = PySequence_Fast_GET_ITEM(values, i); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 787 | if (!PyUnicode_AsUCS4(key, p, end - p, 0)) |
| 788 | goto error; |
| 789 | p += PyUnicode_GET_LENGTH(key); |
| 790 | *p++ = '='; |
| 791 | if (!PyUnicode_AsUCS4(value, p, end - p, 0)) |
| 792 | goto error; |
| 793 | p += PyUnicode_GET_LENGTH(value); |
| 794 | *p++ = '\0'; |
| 795 | } |
| 796 | |
| 797 | /* add trailing null byte */ |
| 798 | *p++ = '\0'; |
| 799 | assert(p == end); |
| 800 | |
| 801 | Py_XDECREF(keys); |
| 802 | Py_XDECREF(values); |
| 803 | |
| 804 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, p - buffer); |
| 805 | PyMem_Free(buffer); |
| 806 | return res; |
| 807 | |
| 808 | error: |
| 809 | PyMem_Free(buffer); |
| 810 | Py_XDECREF(keys); |
| 811 | Py_XDECREF(values); |
| 812 | return NULL; |
| 813 | } |
| 814 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 815 | static LPHANDLE |
| 816 | gethandlelist(PyObject *mapping, const char *name, Py_ssize_t *size) |
| 817 | { |
| 818 | LPHANDLE ret = NULL; |
| 819 | PyObject *value_fast = NULL; |
| 820 | PyObject *value; |
| 821 | Py_ssize_t i; |
| 822 | |
| 823 | value = PyMapping_GetItemString(mapping, name); |
| 824 | if (!value) { |
| 825 | PyErr_Clear(); |
| 826 | return NULL; |
| 827 | } |
| 828 | |
| 829 | if (value == Py_None) { |
| 830 | goto cleanup; |
| 831 | } |
| 832 | |
| 833 | value_fast = PySequence_Fast(value, "handle_list must be a sequence or None"); |
| 834 | if (value_fast == NULL) |
| 835 | goto cleanup; |
| 836 | |
| 837 | *size = PySequence_Fast_GET_SIZE(value_fast) * sizeof(HANDLE); |
| 838 | |
| 839 | /* Passing an empty array causes CreateProcess to fail so just don't set it */ |
| 840 | if (*size == 0) { |
| 841 | goto cleanup; |
| 842 | } |
| 843 | |
| 844 | ret = PyMem_Malloc(*size); |
| 845 | if (ret == NULL) |
| 846 | goto cleanup; |
| 847 | |
| 848 | for (i = 0; i < PySequence_Fast_GET_SIZE(value_fast); i++) { |
| 849 | ret[i] = PYNUM_TO_HANDLE(PySequence_Fast_GET_ITEM(value_fast, i)); |
| 850 | if (ret[i] == (HANDLE)-1 && PyErr_Occurred()) { |
| 851 | PyMem_Free(ret); |
| 852 | ret = NULL; |
| 853 | goto cleanup; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | cleanup: |
| 858 | Py_DECREF(value); |
| 859 | Py_XDECREF(value_fast); |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | typedef struct { |
| 864 | LPPROC_THREAD_ATTRIBUTE_LIST attribute_list; |
| 865 | LPHANDLE handle_list; |
| 866 | } AttributeList; |
| 867 | |
| 868 | static void |
| 869 | freeattributelist(AttributeList *attribute_list) |
| 870 | { |
| 871 | if (attribute_list->attribute_list != NULL) { |
| 872 | DeleteProcThreadAttributeList(attribute_list->attribute_list); |
| 873 | PyMem_Free(attribute_list->attribute_list); |
| 874 | } |
| 875 | |
| 876 | PyMem_Free(attribute_list->handle_list); |
| 877 | |
| 878 | memset(attribute_list, 0, sizeof(*attribute_list)); |
| 879 | } |
| 880 | |
| 881 | static int |
| 882 | getattributelist(PyObject *obj, const char *name, AttributeList *attribute_list) |
| 883 | { |
| 884 | int ret = 0; |
| 885 | DWORD err; |
| 886 | BOOL result; |
| 887 | PyObject *value; |
| 888 | Py_ssize_t handle_list_size; |
| 889 | DWORD attribute_count = 0; |
| 890 | SIZE_T attribute_list_size = 0; |
| 891 | |
| 892 | value = PyObject_GetAttrString(obj, name); |
| 893 | if (!value) { |
| 894 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | if (value == Py_None) { |
| 899 | ret = 0; |
| 900 | goto cleanup; |
| 901 | } |
| 902 | |
| 903 | if (!PyMapping_Check(value)) { |
| 904 | ret = -1; |
| 905 | PyErr_Format(PyExc_TypeError, "%s must be a mapping or None", name); |
| 906 | goto cleanup; |
| 907 | } |
| 908 | |
| 909 | attribute_list->handle_list = gethandlelist(value, "handle_list", &handle_list_size); |
| 910 | if (attribute_list->handle_list == NULL && PyErr_Occurred()) { |
| 911 | ret = -1; |
| 912 | goto cleanup; |
| 913 | } |
| 914 | |
| 915 | if (attribute_list->handle_list != NULL) |
| 916 | ++attribute_count; |
| 917 | |
| 918 | /* Get how many bytes we need for the attribute list */ |
| 919 | result = InitializeProcThreadAttributeList(NULL, attribute_count, 0, &attribute_list_size); |
| 920 | if (result || GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 921 | ret = -1; |
| 922 | PyErr_SetFromWindowsErr(GetLastError()); |
| 923 | goto cleanup; |
| 924 | } |
| 925 | |
| 926 | attribute_list->attribute_list = PyMem_Malloc(attribute_list_size); |
| 927 | if (attribute_list->attribute_list == NULL) { |
| 928 | ret = -1; |
| 929 | goto cleanup; |
| 930 | } |
| 931 | |
| 932 | result = InitializeProcThreadAttributeList( |
| 933 | attribute_list->attribute_list, |
| 934 | attribute_count, |
| 935 | 0, |
| 936 | &attribute_list_size); |
| 937 | if (!result) { |
| 938 | err = GetLastError(); |
| 939 | |
| 940 | /* So that we won't call DeleteProcThreadAttributeList */ |
| 941 | PyMem_Free(attribute_list->attribute_list); |
| 942 | attribute_list->attribute_list = NULL; |
| 943 | |
| 944 | ret = -1; |
| 945 | PyErr_SetFromWindowsErr(err); |
| 946 | goto cleanup; |
| 947 | } |
| 948 | |
| 949 | if (attribute_list->handle_list != NULL) { |
| 950 | result = UpdateProcThreadAttribute( |
| 951 | attribute_list->attribute_list, |
| 952 | 0, |
| 953 | PROC_THREAD_ATTRIBUTE_HANDLE_LIST, |
| 954 | attribute_list->handle_list, |
| 955 | handle_list_size, |
| 956 | NULL, |
| 957 | NULL); |
| 958 | if (!result) { |
| 959 | ret = -1; |
| 960 | PyErr_SetFromWindowsErr(GetLastError()); |
| 961 | goto cleanup; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | cleanup: |
| 966 | Py_DECREF(value); |
| 967 | |
| 968 | if (ret < 0) |
| 969 | freeattributelist(attribute_list); |
| 970 | |
| 971 | return ret; |
| 972 | } |
| 973 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 974 | /*[clinic input] |
| 975 | _winapi.CreateProcess |
| 976 | |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 977 | application_name: Py_UNICODE(accept={str, NoneType}) |
| 978 | command_line: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 979 | proc_attrs: object |
| 980 | Ignored internally, can be None. |
| 981 | thread_attrs: object |
| 982 | Ignored internally, can be None. |
| 983 | inherit_handles: BOOL |
| 984 | creation_flags: DWORD |
| 985 | env_mapping: object |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 986 | current_directory: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 987 | startup_info: object |
| 988 | / |
| 989 | |
| 990 | Create a new process and its primary thread. |
| 991 | |
| 992 | The return value is a tuple of the process handle, thread handle, |
| 993 | process ID, and thread ID. |
| 994 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 995 | |
| 996 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 997 | _winapi_CreateProcess_impl(PyObject *module, Py_UNICODE *application_name, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 998 | Py_UNICODE *command_line, PyObject *proc_attrs, |
| 999 | PyObject *thread_attrs, BOOL inherit_handles, |
| 1000 | DWORD creation_flags, PyObject *env_mapping, |
| 1001 | Py_UNICODE *current_directory, |
| 1002 | PyObject *startup_info) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1003 | /*[clinic end generated code: output=4652a33aff4b0ae1 input=4a43b05038d639bb]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1004 | { |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1005 | PyObject *ret = NULL; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1006 | BOOL result; |
| 1007 | PROCESS_INFORMATION pi; |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1008 | STARTUPINFOEXW si; |
| 1009 | PyObject *environment = NULL; |
Serhiy Storchaka | 0ee32c1 | 2017-06-24 16:14:08 +0300 | [diff] [blame] | 1010 | wchar_t *wenvironment; |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1011 | AttributeList attribute_list = {0}; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1012 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1013 | ZeroMemory(&si, sizeof(si)); |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1014 | si.StartupInfo.cb = sizeof(si); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1015 | |
| 1016 | /* note: we only support a small subset of all SI attributes */ |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1017 | si.StartupInfo.dwFlags = getulong(startup_info, "dwFlags"); |
| 1018 | si.StartupInfo.wShowWindow = (WORD)getulong(startup_info, "wShowWindow"); |
| 1019 | si.StartupInfo.hStdInput = gethandle(startup_info, "hStdInput"); |
| 1020 | si.StartupInfo.hStdOutput = gethandle(startup_info, "hStdOutput"); |
| 1021 | si.StartupInfo.hStdError = gethandle(startup_info, "hStdError"); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1022 | if (PyErr_Occurred()) |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1023 | goto cleanup; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1024 | |
| 1025 | if (env_mapping != Py_None) { |
| 1026 | environment = getenvironment(env_mapping); |
Serhiy Storchaka | d174d24 | 2017-06-23 19:39:27 +0300 | [diff] [blame] | 1027 | if (environment == NULL) { |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1028 | goto cleanup; |
Serhiy Storchaka | d174d24 | 2017-06-23 19:39:27 +0300 | [diff] [blame] | 1029 | } |
| 1030 | /* contains embedded null characters */ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1031 | wenvironment = PyUnicode_AsUnicode(environment); |
Serhiy Storchaka | d174d24 | 2017-06-23 19:39:27 +0300 | [diff] [blame] | 1032 | if (wenvironment == NULL) { |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1033 | goto cleanup; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | else { |
| 1037 | environment = NULL; |
| 1038 | wenvironment = NULL; |
| 1039 | } |
| 1040 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1041 | if (getattributelist(startup_info, "lpAttributeList", &attribute_list) < 0) |
| 1042 | goto cleanup; |
| 1043 | |
| 1044 | si.lpAttributeList = attribute_list.attribute_list; |
| 1045 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1046 | Py_BEGIN_ALLOW_THREADS |
| 1047 | result = CreateProcessW(application_name, |
| 1048 | command_line, |
| 1049 | NULL, |
| 1050 | NULL, |
| 1051 | inherit_handles, |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1052 | creation_flags | EXTENDED_STARTUPINFO_PRESENT | |
| 1053 | CREATE_UNICODE_ENVIRONMENT, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1054 | wenvironment, |
| 1055 | current_directory, |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1056 | (LPSTARTUPINFOW)&si, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1057 | &pi); |
| 1058 | Py_END_ALLOW_THREADS |
| 1059 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1060 | if (!result) { |
| 1061 | PyErr_SetFromWindowsErr(GetLastError()); |
| 1062 | goto cleanup; |
| 1063 | } |
| 1064 | |
| 1065 | ret = Py_BuildValue("NNkk", |
| 1066 | HANDLE_TO_PYNUM(pi.hProcess), |
| 1067 | HANDLE_TO_PYNUM(pi.hThread), |
| 1068 | pi.dwProcessId, |
| 1069 | pi.dwThreadId); |
| 1070 | |
| 1071 | cleanup: |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1072 | Py_XDECREF(environment); |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1073 | freeattributelist(&attribute_list); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1074 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1075 | return ret; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1078 | /*[clinic input] |
| 1079 | _winapi.DuplicateHandle -> HANDLE |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1080 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1081 | source_process_handle: HANDLE |
| 1082 | source_handle: HANDLE |
| 1083 | target_process_handle: HANDLE |
| 1084 | desired_access: DWORD |
| 1085 | inherit_handle: BOOL |
| 1086 | options: DWORD = 0 |
| 1087 | / |
| 1088 | |
| 1089 | Return a duplicate handle object. |
| 1090 | |
| 1091 | The duplicate handle refers to the same object as the original |
| 1092 | handle. Therefore, any changes to the object are reflected |
| 1093 | through both handles. |
| 1094 | [clinic start generated code]*/ |
| 1095 | |
| 1096 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1097 | _winapi_DuplicateHandle_impl(PyObject *module, HANDLE source_process_handle, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1098 | HANDLE source_handle, |
| 1099 | HANDLE target_process_handle, |
| 1100 | DWORD desired_access, BOOL inherit_handle, |
| 1101 | DWORD options) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1102 | /*[clinic end generated code: output=ad9711397b5dcd4e input=b933e3f2356a8c12]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1103 | { |
| 1104 | HANDLE target_handle; |
| 1105 | BOOL result; |
| 1106 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1107 | Py_BEGIN_ALLOW_THREADS |
| 1108 | result = DuplicateHandle( |
| 1109 | source_process_handle, |
| 1110 | source_handle, |
| 1111 | target_process_handle, |
| 1112 | &target_handle, |
| 1113 | desired_access, |
| 1114 | inherit_handle, |
| 1115 | options |
| 1116 | ); |
| 1117 | Py_END_ALLOW_THREADS |
| 1118 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1119 | if (! result) { |
| 1120 | PyErr_SetFromWindowsErr(GetLastError()); |
| 1121 | return INVALID_HANDLE_VALUE; |
| 1122 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1123 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1124 | return target_handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1127 | /*[clinic input] |
| 1128 | _winapi.ExitProcess |
| 1129 | |
| 1130 | ExitCode: UINT |
| 1131 | / |
| 1132 | |
| 1133 | [clinic start generated code]*/ |
| 1134 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1135 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1136 | _winapi_ExitProcess_impl(PyObject *module, UINT ExitCode) |
| 1137 | /*[clinic end generated code: output=a387deb651175301 input=4f05466a9406c558]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1138 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1139 | #if defined(Py_DEBUG) |
| 1140 | SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT| |
| 1141 | SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX); |
| 1142 | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
| 1143 | #endif |
| 1144 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1145 | ExitProcess(ExitCode); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1146 | |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1150 | /*[clinic input] |
| 1151 | _winapi.GetCurrentProcess -> HANDLE |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1152 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1153 | Return a handle object for the current process. |
| 1154 | [clinic start generated code]*/ |
| 1155 | |
| 1156 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1157 | _winapi_GetCurrentProcess_impl(PyObject *module) |
| 1158 | /*[clinic end generated code: output=ddeb4dd2ffadf344 input=b213403fd4b96b41]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1159 | { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1160 | return GetCurrentProcess(); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1161 | } |
| 1162 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1163 | /*[clinic input] |
| 1164 | _winapi.GetExitCodeProcess -> DWORD |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1165 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1166 | process: HANDLE |
| 1167 | / |
| 1168 | |
| 1169 | Return the termination status of the specified process. |
| 1170 | [clinic start generated code]*/ |
| 1171 | |
| 1172 | static DWORD |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1173 | _winapi_GetExitCodeProcess_impl(PyObject *module, HANDLE process) |
| 1174 | /*[clinic end generated code: output=b4620bdf2bccf36b input=61b6bfc7dc2ee374]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1175 | { |
| 1176 | DWORD exit_code; |
| 1177 | BOOL result; |
| 1178 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1179 | result = GetExitCodeProcess(process, &exit_code); |
| 1180 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1181 | if (! result) { |
| 1182 | PyErr_SetFromWindowsErr(GetLastError()); |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 1183 | exit_code = PY_DWORD_MAX; |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1184 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1185 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1186 | return exit_code; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1189 | /*[clinic input] |
| 1190 | _winapi.GetLastError -> DWORD |
| 1191 | [clinic start generated code]*/ |
| 1192 | |
| 1193 | static DWORD |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1194 | _winapi_GetLastError_impl(PyObject *module) |
| 1195 | /*[clinic end generated code: output=8585b827cb1a92c5 input=62d47fb9bce038ba]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1196 | { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1197 | return GetLastError(); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1200 | /*[clinic input] |
| 1201 | _winapi.GetModuleFileName |
| 1202 | |
| 1203 | module_handle: HMODULE |
| 1204 | / |
| 1205 | |
| 1206 | Return the fully-qualified path for the file that contains module. |
| 1207 | |
| 1208 | The module must have been loaded by the current process. |
| 1209 | |
| 1210 | The module parameter should be a handle to the loaded module |
| 1211 | whose path is being requested. If this parameter is 0, |
| 1212 | GetModuleFileName retrieves the path of the executable file |
| 1213 | of the current process. |
| 1214 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1215 | |
| 1216 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1217 | _winapi_GetModuleFileName_impl(PyObject *module, HMODULE module_handle) |
| 1218 | /*[clinic end generated code: output=85b4b728c5160306 input=6d66ff7deca5d11f]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1219 | { |
| 1220 | BOOL result; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1221 | WCHAR filename[MAX_PATH]; |
| 1222 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1223 | result = GetModuleFileNameW(module_handle, filename, MAX_PATH); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1224 | filename[MAX_PATH-1] = '\0'; |
| 1225 | |
| 1226 | if (! result) |
| 1227 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 1228 | |
| 1229 | return PyUnicode_FromWideChar(filename, wcslen(filename)); |
| 1230 | } |
| 1231 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1232 | /*[clinic input] |
| 1233 | _winapi.GetStdHandle -> HANDLE |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1234 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1235 | std_handle: DWORD |
| 1236 | One of STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE. |
| 1237 | / |
| 1238 | |
| 1239 | Return a handle to the specified standard device. |
| 1240 | |
| 1241 | The integer associated with the handle object is returned. |
| 1242 | [clinic start generated code]*/ |
| 1243 | |
| 1244 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1245 | _winapi_GetStdHandle_impl(PyObject *module, DWORD std_handle) |
| 1246 | /*[clinic end generated code: output=0e613001e73ab614 input=07016b06a2fc8826]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1247 | { |
| 1248 | HANDLE handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1249 | |
| 1250 | Py_BEGIN_ALLOW_THREADS |
| 1251 | handle = GetStdHandle(std_handle); |
| 1252 | Py_END_ALLOW_THREADS |
| 1253 | |
| 1254 | if (handle == INVALID_HANDLE_VALUE) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1255 | PyErr_SetFromWindowsErr(GetLastError()); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1256 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1257 | return handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1260 | /*[clinic input] |
| 1261 | _winapi.GetVersion -> long |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1262 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1263 | Return the version number of the current operating system. |
| 1264 | [clinic start generated code]*/ |
| 1265 | |
| 1266 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1267 | _winapi_GetVersion_impl(PyObject *module) |
| 1268 | /*[clinic end generated code: output=e41f0db5a3b82682 input=e21dff8d0baeded2]*/ |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 1269 | /* Disable deprecation warnings about GetVersionEx as the result is |
| 1270 | being passed straight through to the caller, who is responsible for |
| 1271 | using it correctly. */ |
| 1272 | #pragma warning(push) |
| 1273 | #pragma warning(disable:4996) |
| 1274 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1275 | { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1276 | return GetVersion(); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 1279 | #pragma warning(pop) |
| 1280 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1281 | /*[clinic input] |
| 1282 | _winapi.OpenProcess -> HANDLE |
| 1283 | |
| 1284 | desired_access: DWORD |
| 1285 | inherit_handle: BOOL |
| 1286 | process_id: DWORD |
| 1287 | / |
| 1288 | [clinic start generated code]*/ |
| 1289 | |
| 1290 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1291 | _winapi_OpenProcess_impl(PyObject *module, DWORD desired_access, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1292 | BOOL inherit_handle, DWORD process_id) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1293 | /*[clinic end generated code: output=b42b6b81ea5a0fc3 input=ec98c4cf4ea2ec36]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1294 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1295 | HANDLE handle; |
| 1296 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1297 | handle = OpenProcess(desired_access, inherit_handle, process_id); |
| 1298 | if (handle == NULL) { |
| 1299 | PyErr_SetFromWindowsErr(0); |
| 1300 | handle = INVALID_HANDLE_VALUE; |
| 1301 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1302 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1303 | return handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1304 | } |
| 1305 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1306 | /*[clinic input] |
| 1307 | _winapi.PeekNamedPipe |
| 1308 | |
| 1309 | handle: HANDLE |
| 1310 | size: int = 0 |
| 1311 | / |
| 1312 | [clinic start generated code]*/ |
| 1313 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1314 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1315 | _winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size) |
| 1316 | /*[clinic end generated code: output=d0c3e29e49d323dd input=c7aa53bfbce69d70]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1317 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1318 | PyObject *buf = NULL; |
| 1319 | DWORD nread, navail, nleft; |
| 1320 | BOOL ret; |
| 1321 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1322 | if (size < 0) { |
| 1323 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | |
| 1327 | if (size) { |
| 1328 | buf = PyBytes_FromStringAndSize(NULL, size); |
| 1329 | if (!buf) |
| 1330 | return NULL; |
| 1331 | Py_BEGIN_ALLOW_THREADS |
| 1332 | ret = PeekNamedPipe(handle, PyBytes_AS_STRING(buf), size, &nread, |
| 1333 | &navail, &nleft); |
| 1334 | Py_END_ALLOW_THREADS |
| 1335 | if (!ret) { |
| 1336 | Py_DECREF(buf); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1337 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1338 | } |
| 1339 | if (_PyBytes_Resize(&buf, nread)) |
| 1340 | return NULL; |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1341 | return Py_BuildValue("NII", buf, navail, nleft); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1342 | } |
| 1343 | else { |
| 1344 | Py_BEGIN_ALLOW_THREADS |
| 1345 | ret = PeekNamedPipe(handle, NULL, 0, NULL, &navail, &nleft); |
| 1346 | Py_END_ALLOW_THREADS |
| 1347 | if (!ret) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1348 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1349 | } |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1350 | return Py_BuildValue("II", navail, nleft); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1354 | /*[clinic input] |
| 1355 | _winapi.ReadFile |
| 1356 | |
| 1357 | handle: HANDLE |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1358 | size: DWORD |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1359 | overlapped as use_overlapped: bool(accept={int}) = False |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1360 | [clinic start generated code]*/ |
| 1361 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1362 | static PyObject * |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1363 | _winapi_ReadFile_impl(PyObject *module, HANDLE handle, DWORD size, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1364 | int use_overlapped) |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1365 | /*[clinic end generated code: output=d3d5b44a8201b944 input=08c439d03a11aac5]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1366 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1367 | DWORD nread; |
| 1368 | PyObject *buf; |
| 1369 | BOOL ret; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1370 | DWORD err; |
| 1371 | OverlappedObject *overlapped = NULL; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1372 | |
| 1373 | buf = PyBytes_FromStringAndSize(NULL, size); |
| 1374 | if (!buf) |
| 1375 | return NULL; |
| 1376 | if (use_overlapped) { |
| 1377 | overlapped = new_overlapped(handle); |
| 1378 | if (!overlapped) { |
| 1379 | Py_DECREF(buf); |
| 1380 | return NULL; |
| 1381 | } |
| 1382 | /* Steals reference to buf */ |
| 1383 | overlapped->read_buffer = buf; |
| 1384 | } |
| 1385 | |
| 1386 | Py_BEGIN_ALLOW_THREADS |
| 1387 | ret = ReadFile(handle, PyBytes_AS_STRING(buf), size, &nread, |
| 1388 | overlapped ? &overlapped->overlapped : NULL); |
| 1389 | Py_END_ALLOW_THREADS |
| 1390 | |
| 1391 | err = ret ? 0 : GetLastError(); |
| 1392 | |
| 1393 | if (overlapped) { |
| 1394 | if (!ret) { |
| 1395 | if (err == ERROR_IO_PENDING) |
| 1396 | overlapped->pending = 1; |
| 1397 | else if (err != ERROR_MORE_DATA) { |
| 1398 | Py_DECREF(overlapped); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1399 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1400 | } |
| 1401 | } |
| 1402 | return Py_BuildValue("NI", (PyObject *) overlapped, err); |
| 1403 | } |
| 1404 | |
| 1405 | if (!ret && err != ERROR_MORE_DATA) { |
| 1406 | Py_DECREF(buf); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1407 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1408 | } |
| 1409 | if (_PyBytes_Resize(&buf, nread)) |
| 1410 | return NULL; |
| 1411 | return Py_BuildValue("NI", buf, err); |
| 1412 | } |
| 1413 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1414 | /*[clinic input] |
| 1415 | _winapi.SetNamedPipeHandleState |
| 1416 | |
| 1417 | named_pipe: HANDLE |
| 1418 | mode: object |
| 1419 | max_collection_count: object |
| 1420 | collect_data_timeout: object |
| 1421 | / |
| 1422 | [clinic start generated code]*/ |
| 1423 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1424 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1425 | _winapi_SetNamedPipeHandleState_impl(PyObject *module, HANDLE named_pipe, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1426 | PyObject *mode, |
| 1427 | PyObject *max_collection_count, |
| 1428 | PyObject *collect_data_timeout) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1429 | /*[clinic end generated code: output=f2129d222cbfa095 input=9142d72163d0faa6]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1430 | { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1431 | PyObject *oArgs[3] = {mode, max_collection_count, collect_data_timeout}; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1432 | DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL}; |
| 1433 | int i; |
| 1434 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1435 | for (i = 0 ; i < 3 ; i++) { |
| 1436 | if (oArgs[i] != Py_None) { |
| 1437 | dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]); |
| 1438 | if (PyErr_Occurred()) |
| 1439 | return NULL; |
| 1440 | pArgs[i] = &dwArgs[i]; |
| 1441 | } |
| 1442 | } |
| 1443 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1444 | if (!SetNamedPipeHandleState(named_pipe, pArgs[0], pArgs[1], pArgs[2])) |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1445 | return PyErr_SetFromWindowsErr(0); |
| 1446 | |
| 1447 | Py_RETURN_NONE; |
| 1448 | } |
| 1449 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1450 | |
| 1451 | /*[clinic input] |
| 1452 | _winapi.TerminateProcess |
| 1453 | |
| 1454 | handle: HANDLE |
| 1455 | exit_code: UINT |
| 1456 | / |
| 1457 | |
| 1458 | Terminate the specified process and all of its threads. |
| 1459 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1460 | |
| 1461 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1462 | _winapi_TerminateProcess_impl(PyObject *module, HANDLE handle, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1463 | UINT exit_code) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1464 | /*[clinic end generated code: output=f4e99ac3f0b1f34a input=d6bc0aa1ee3bb4df]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1465 | { |
| 1466 | BOOL result; |
| 1467 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1468 | result = TerminateProcess(handle, exit_code); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1469 | |
| 1470 | if (! result) |
| 1471 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 1472 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1473 | Py_RETURN_NONE; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1474 | } |
| 1475 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1476 | /*[clinic input] |
| 1477 | _winapi.WaitNamedPipe |
| 1478 | |
| 1479 | name: LPCTSTR |
| 1480 | timeout: DWORD |
| 1481 | / |
| 1482 | [clinic start generated code]*/ |
| 1483 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1484 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1485 | _winapi_WaitNamedPipe_impl(PyObject *module, LPCTSTR name, DWORD timeout) |
| 1486 | /*[clinic end generated code: output=c2866f4439b1fe38 input=36fc781291b1862c]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1487 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1488 | BOOL success; |
| 1489 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1490 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1491 | success = WaitNamedPipe(name, timeout); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1492 | Py_END_ALLOW_THREADS |
| 1493 | |
| 1494 | if (!success) |
| 1495 | return PyErr_SetFromWindowsErr(0); |
| 1496 | |
| 1497 | Py_RETURN_NONE; |
| 1498 | } |
| 1499 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1500 | /*[clinic input] |
| 1501 | _winapi.WaitForMultipleObjects |
| 1502 | |
| 1503 | handle_seq: object |
| 1504 | wait_flag: BOOL |
| 1505 | milliseconds: DWORD(c_default='INFINITE') = _winapi.INFINITE |
| 1506 | / |
| 1507 | [clinic start generated code]*/ |
| 1508 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1509 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1510 | _winapi_WaitForMultipleObjects_impl(PyObject *module, PyObject *handle_seq, |
| 1511 | BOOL wait_flag, DWORD milliseconds) |
| 1512 | /*[clinic end generated code: output=295e3f00b8e45899 input=36f76ca057cd28a0]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1513 | { |
| 1514 | DWORD result; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1515 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 1516 | HANDLE sigint_event = NULL; |
| 1517 | Py_ssize_t nhandles, i; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1518 | |
| 1519 | if (!PySequence_Check(handle_seq)) { |
| 1520 | PyErr_Format(PyExc_TypeError, |
| 1521 | "sequence type expected, got '%s'", |
Richard Oudkerk | 6733927 | 2012-08-21 14:54:22 +0100 | [diff] [blame] | 1522 | Py_TYPE(handle_seq)->tp_name); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1523 | return NULL; |
| 1524 | } |
| 1525 | nhandles = PySequence_Length(handle_seq); |
| 1526 | if (nhandles == -1) |
| 1527 | return NULL; |
| 1528 | if (nhandles < 0 || nhandles >= MAXIMUM_WAIT_OBJECTS - 1) { |
| 1529 | PyErr_Format(PyExc_ValueError, |
| 1530 | "need at most %zd handles, got a sequence of length %zd", |
| 1531 | MAXIMUM_WAIT_OBJECTS - 1, nhandles); |
| 1532 | return NULL; |
| 1533 | } |
| 1534 | for (i = 0; i < nhandles; i++) { |
| 1535 | HANDLE h; |
| 1536 | PyObject *v = PySequence_GetItem(handle_seq, i); |
| 1537 | if (v == NULL) |
| 1538 | return NULL; |
| 1539 | if (!PyArg_Parse(v, F_HANDLE, &h)) { |
| 1540 | Py_DECREF(v); |
| 1541 | return NULL; |
| 1542 | } |
| 1543 | handles[i] = h; |
| 1544 | Py_DECREF(v); |
| 1545 | } |
| 1546 | /* If this is the main thread then make the wait interruptible |
| 1547 | by Ctrl-C unless we are waiting for *all* handles */ |
| 1548 | if (!wait_flag && _PyOS_IsMainThread()) { |
| 1549 | sigint_event = _PyOS_SigintEvent(); |
| 1550 | assert(sigint_event != NULL); |
| 1551 | handles[nhandles++] = sigint_event; |
| 1552 | } |
| 1553 | |
| 1554 | Py_BEGIN_ALLOW_THREADS |
| 1555 | if (sigint_event != NULL) |
| 1556 | ResetEvent(sigint_event); |
| 1557 | result = WaitForMultipleObjects((DWORD) nhandles, handles, |
| 1558 | wait_flag, milliseconds); |
| 1559 | Py_END_ALLOW_THREADS |
| 1560 | |
| 1561 | if (result == WAIT_FAILED) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1562 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1563 | else if (sigint_event != NULL && result == WAIT_OBJECT_0 + nhandles - 1) { |
| 1564 | errno = EINTR; |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1565 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | return PyLong_FromLong((int) result); |
| 1569 | } |
| 1570 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1571 | /*[clinic input] |
| 1572 | _winapi.WaitForSingleObject -> long |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1573 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1574 | handle: HANDLE |
| 1575 | milliseconds: DWORD |
| 1576 | / |
| 1577 | |
| 1578 | Wait for a single object. |
| 1579 | |
| 1580 | Wait until the specified object is in the signaled state or |
| 1581 | the time-out interval elapses. The timeout value is specified |
| 1582 | in milliseconds. |
| 1583 | [clinic start generated code]*/ |
| 1584 | |
| 1585 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1586 | _winapi_WaitForSingleObject_impl(PyObject *module, HANDLE handle, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1587 | DWORD milliseconds) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1588 | /*[clinic end generated code: output=3c4715d8f1b39859 input=443d1ab076edc7b1]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1589 | { |
| 1590 | DWORD result; |
| 1591 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1592 | Py_BEGIN_ALLOW_THREADS |
| 1593 | result = WaitForSingleObject(handle, milliseconds); |
| 1594 | Py_END_ALLOW_THREADS |
| 1595 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1596 | if (result == WAIT_FAILED) { |
| 1597 | PyErr_SetFromWindowsErr(GetLastError()); |
| 1598 | return -1; |
| 1599 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1600 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1601 | return result; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1602 | } |
| 1603 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1604 | /*[clinic input] |
| 1605 | _winapi.WriteFile |
| 1606 | |
| 1607 | handle: HANDLE |
| 1608 | buffer: object |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1609 | overlapped as use_overlapped: bool(accept={int}) = False |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1610 | [clinic start generated code]*/ |
| 1611 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1612 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1613 | _winapi_WriteFile_impl(PyObject *module, HANDLE handle, PyObject *buffer, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1614 | int use_overlapped) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1615 | /*[clinic end generated code: output=2ca80f6bf3fa92e3 input=11eae2a03aa32731]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1616 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1617 | Py_buffer _buf, *buf; |
Victor Stinner | 7176577 | 2013-06-24 23:13:24 +0200 | [diff] [blame] | 1618 | DWORD len, written; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1619 | BOOL ret; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1620 | DWORD err; |
| 1621 | OverlappedObject *overlapped = NULL; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1622 | |
| 1623 | if (use_overlapped) { |
| 1624 | overlapped = new_overlapped(handle); |
| 1625 | if (!overlapped) |
| 1626 | return NULL; |
| 1627 | buf = &overlapped->write_buffer; |
| 1628 | } |
| 1629 | else |
| 1630 | buf = &_buf; |
| 1631 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1632 | if (!PyArg_Parse(buffer, "y*", buf)) { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1633 | Py_XDECREF(overlapped); |
| 1634 | return NULL; |
| 1635 | } |
| 1636 | |
| 1637 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 1638 | len = (DWORD)Py_MIN(buf->len, PY_DWORD_MAX); |
Victor Stinner | 7176577 | 2013-06-24 23:13:24 +0200 | [diff] [blame] | 1639 | ret = WriteFile(handle, buf->buf, len, &written, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1640 | overlapped ? &overlapped->overlapped : NULL); |
| 1641 | Py_END_ALLOW_THREADS |
| 1642 | |
| 1643 | err = ret ? 0 : GetLastError(); |
| 1644 | |
| 1645 | if (overlapped) { |
| 1646 | if (!ret) { |
| 1647 | if (err == ERROR_IO_PENDING) |
| 1648 | overlapped->pending = 1; |
| 1649 | else { |
| 1650 | Py_DECREF(overlapped); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1651 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1652 | } |
| 1653 | } |
| 1654 | return Py_BuildValue("NI", (PyObject *) overlapped, err); |
| 1655 | } |
| 1656 | |
| 1657 | PyBuffer_Release(buf); |
| 1658 | if (!ret) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1659 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1660 | return Py_BuildValue("II", written, err); |
| 1661 | } |
| 1662 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1663 | /*[clinic input] |
| 1664 | _winapi.GetACP |
| 1665 | |
| 1666 | Get the current Windows ANSI code page identifier. |
| 1667 | [clinic start generated code]*/ |
| 1668 | |
| 1669 | static PyObject * |
| 1670 | _winapi_GetACP_impl(PyObject *module) |
| 1671 | /*[clinic end generated code: output=f7ee24bf705dbb88 input=1433c96d03a05229]*/ |
| 1672 | { |
| 1673 | return PyLong_FromUnsignedLong(GetACP()); |
| 1674 | } |
| 1675 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1676 | /*[clinic input] |
| 1677 | _winapi.GetFileType -> DWORD |
| 1678 | |
| 1679 | handle: HANDLE |
| 1680 | [clinic start generated code]*/ |
| 1681 | |
| 1682 | static DWORD |
| 1683 | _winapi_GetFileType_impl(PyObject *module, HANDLE handle) |
| 1684 | /*[clinic end generated code: output=92b8466ac76ecc17 input=0058366bc40bbfbf]*/ |
| 1685 | { |
| 1686 | DWORD result; |
| 1687 | |
| 1688 | Py_BEGIN_ALLOW_THREADS |
| 1689 | result = GetFileType(handle); |
| 1690 | Py_END_ALLOW_THREADS |
| 1691 | |
| 1692 | if (result == FILE_TYPE_UNKNOWN && GetLastError() != NO_ERROR) { |
| 1693 | PyErr_SetFromWindowsErr(0); |
| 1694 | return -1; |
| 1695 | } |
| 1696 | |
| 1697 | return result; |
| 1698 | } |
| 1699 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1700 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1701 | static PyMethodDef winapi_functions[] = { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1702 | _WINAPI_CLOSEHANDLE_METHODDEF |
| 1703 | _WINAPI_CONNECTNAMEDPIPE_METHODDEF |
| 1704 | _WINAPI_CREATEFILE_METHODDEF |
| 1705 | _WINAPI_CREATENAMEDPIPE_METHODDEF |
| 1706 | _WINAPI_CREATEPIPE_METHODDEF |
| 1707 | _WINAPI_CREATEPROCESS_METHODDEF |
| 1708 | _WINAPI_CREATEJUNCTION_METHODDEF |
| 1709 | _WINAPI_DUPLICATEHANDLE_METHODDEF |
| 1710 | _WINAPI_EXITPROCESS_METHODDEF |
| 1711 | _WINAPI_GETCURRENTPROCESS_METHODDEF |
| 1712 | _WINAPI_GETEXITCODEPROCESS_METHODDEF |
| 1713 | _WINAPI_GETLASTERROR_METHODDEF |
| 1714 | _WINAPI_GETMODULEFILENAME_METHODDEF |
| 1715 | _WINAPI_GETSTDHANDLE_METHODDEF |
| 1716 | _WINAPI_GETVERSION_METHODDEF |
| 1717 | _WINAPI_OPENPROCESS_METHODDEF |
| 1718 | _WINAPI_PEEKNAMEDPIPE_METHODDEF |
| 1719 | _WINAPI_READFILE_METHODDEF |
| 1720 | _WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF |
| 1721 | _WINAPI_TERMINATEPROCESS_METHODDEF |
| 1722 | _WINAPI_WAITNAMEDPIPE_METHODDEF |
| 1723 | _WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF |
| 1724 | _WINAPI_WAITFORSINGLEOBJECT_METHODDEF |
| 1725 | _WINAPI_WRITEFILE_METHODDEF |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1726 | _WINAPI_GETACP_METHODDEF |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1727 | _WINAPI_GETFILETYPE_METHODDEF |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1728 | {NULL, NULL} |
| 1729 | }; |
| 1730 | |
| 1731 | static struct PyModuleDef winapi_module = { |
| 1732 | PyModuleDef_HEAD_INIT, |
| 1733 | "_winapi", |
| 1734 | NULL, |
| 1735 | -1, |
| 1736 | winapi_functions, |
| 1737 | NULL, |
| 1738 | NULL, |
| 1739 | NULL, |
| 1740 | NULL |
| 1741 | }; |
| 1742 | |
| 1743 | #define WINAPI_CONSTANT(fmt, con) \ |
| 1744 | PyDict_SetItemString(d, #con, Py_BuildValue(fmt, con)) |
| 1745 | |
| 1746 | PyMODINIT_FUNC |
| 1747 | PyInit__winapi(void) |
| 1748 | { |
| 1749 | PyObject *d; |
| 1750 | PyObject *m; |
| 1751 | |
| 1752 | if (PyType_Ready(&OverlappedType) < 0) |
| 1753 | return NULL; |
| 1754 | |
| 1755 | m = PyModule_Create(&winapi_module); |
| 1756 | if (m == NULL) |
| 1757 | return NULL; |
| 1758 | d = PyModule_GetDict(m); |
| 1759 | |
| 1760 | PyDict_SetItemString(d, "Overlapped", (PyObject *) &OverlappedType); |
| 1761 | |
| 1762 | /* constants */ |
| 1763 | WINAPI_CONSTANT(F_DWORD, CREATE_NEW_CONSOLE); |
| 1764 | WINAPI_CONSTANT(F_DWORD, CREATE_NEW_PROCESS_GROUP); |
| 1765 | WINAPI_CONSTANT(F_DWORD, DUPLICATE_SAME_ACCESS); |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 1766 | WINAPI_CONSTANT(F_DWORD, DUPLICATE_CLOSE_SOURCE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1767 | WINAPI_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS); |
| 1768 | WINAPI_CONSTANT(F_DWORD, ERROR_BROKEN_PIPE); |
| 1769 | WINAPI_CONSTANT(F_DWORD, ERROR_IO_PENDING); |
| 1770 | WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA); |
| 1771 | WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED); |
| 1772 | WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1773 | WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA); |
| 1774 | WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED); |
Richard Oudkerk | fdb8dcf | 2012-05-05 19:45:37 +0100 | [diff] [blame] | 1775 | WINAPI_CONSTANT(F_DWORD, ERROR_NO_DATA); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1776 | WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES); |
| 1777 | WINAPI_CONSTANT(F_DWORD, ERROR_OPERATION_ABORTED); |
| 1778 | WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_BUSY); |
| 1779 | WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED); |
| 1780 | WINAPI_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT); |
| 1781 | WINAPI_CONSTANT(F_DWORD, FILE_FLAG_FIRST_PIPE_INSTANCE); |
| 1782 | WINAPI_CONSTANT(F_DWORD, FILE_FLAG_OVERLAPPED); |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 1783 | WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_READ); |
| 1784 | WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_WRITE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1785 | WINAPI_CONSTANT(F_DWORD, GENERIC_READ); |
| 1786 | WINAPI_CONSTANT(F_DWORD, GENERIC_WRITE); |
| 1787 | WINAPI_CONSTANT(F_DWORD, INFINITE); |
| 1788 | WINAPI_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER); |
| 1789 | WINAPI_CONSTANT(F_DWORD, OPEN_EXISTING); |
| 1790 | WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX); |
| 1791 | WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND); |
| 1792 | WINAPI_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE); |
| 1793 | WINAPI_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE); |
| 1794 | WINAPI_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES); |
| 1795 | WINAPI_CONSTANT(F_DWORD, PIPE_WAIT); |
| 1796 | WINAPI_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS); |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 1797 | WINAPI_CONSTANT(F_DWORD, PROCESS_DUP_HANDLE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1798 | WINAPI_CONSTANT(F_DWORD, STARTF_USESHOWWINDOW); |
| 1799 | WINAPI_CONSTANT(F_DWORD, STARTF_USESTDHANDLES); |
| 1800 | WINAPI_CONSTANT(F_DWORD, STD_INPUT_HANDLE); |
| 1801 | WINAPI_CONSTANT(F_DWORD, STD_OUTPUT_HANDLE); |
| 1802 | WINAPI_CONSTANT(F_DWORD, STD_ERROR_HANDLE); |
| 1803 | WINAPI_CONSTANT(F_DWORD, STILL_ACTIVE); |
| 1804 | WINAPI_CONSTANT(F_DWORD, SW_HIDE); |
| 1805 | WINAPI_CONSTANT(F_DWORD, WAIT_OBJECT_0); |
Victor Stinner | 373f0a9 | 2014-03-20 09:26:55 +0100 | [diff] [blame] | 1806 | WINAPI_CONSTANT(F_DWORD, WAIT_ABANDONED_0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1807 | WINAPI_CONSTANT(F_DWORD, WAIT_TIMEOUT); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1808 | |
James | b5d9e08 | 2017-11-08 14:18:59 +0000 | [diff] [blame] | 1809 | WINAPI_CONSTANT(F_DWORD, ABOVE_NORMAL_PRIORITY_CLASS); |
| 1810 | WINAPI_CONSTANT(F_DWORD, BELOW_NORMAL_PRIORITY_CLASS); |
| 1811 | WINAPI_CONSTANT(F_DWORD, HIGH_PRIORITY_CLASS); |
| 1812 | WINAPI_CONSTANT(F_DWORD, IDLE_PRIORITY_CLASS); |
| 1813 | WINAPI_CONSTANT(F_DWORD, NORMAL_PRIORITY_CLASS); |
| 1814 | WINAPI_CONSTANT(F_DWORD, REALTIME_PRIORITY_CLASS); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1815 | |
James | b5d9e08 | 2017-11-08 14:18:59 +0000 | [diff] [blame] | 1816 | WINAPI_CONSTANT(F_DWORD, CREATE_NO_WINDOW); |
| 1817 | WINAPI_CONSTANT(F_DWORD, DETACHED_PROCESS); |
| 1818 | WINAPI_CONSTANT(F_DWORD, CREATE_DEFAULT_ERROR_MODE); |
| 1819 | WINAPI_CONSTANT(F_DWORD, CREATE_BREAKAWAY_FROM_JOB); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1820 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1821 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_UNKNOWN); |
| 1822 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_DISK); |
| 1823 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_CHAR); |
| 1824 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_PIPE); |
| 1825 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_REMOTE); |
| 1826 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1827 | WINAPI_CONSTANT("i", NULL); |
| 1828 | |
| 1829 | return m; |
| 1830 | } |