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 "') |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 162 | create_converter('LPCVOID', '" F_POINTER "') |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 163 | |
| 164 | create_converter('BOOL', 'i') # F_BOOL used previously (always 'i') |
| 165 | create_converter('DWORD', 'k') # F_DWORD is always "k" (which is much shorter) |
| 166 | create_converter('LPCTSTR', 's') |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 167 | create_converter('LPCWSTR', 'u') |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 168 | create_converter('LPWSTR', 'u') |
| 169 | create_converter('UINT', 'I') # F_UINT used previously (always 'I') |
| 170 | |
| 171 | class 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 Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 178 | 'if (_return_value == NULL) {\n Py_RETURN_NONE;\n}\n') |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 179 | data.return_conversion.append( |
| 180 | 'return_value = HANDLE_TO_PYNUM(_return_value);\n') |
| 181 | |
| 182 | class DWORD_return_converter(CReturnConverter): |
| 183 | type = 'DWORD' |
| 184 | |
| 185 | def render(self, function, data): |
| 186 | self.declare(data) |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 187 | self.err_occurred_if("_return_value == PY_DWORD_MAX", data) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 188 | data.return_conversion.append( |
| 189 | 'return_value = Py_BuildValue("k", _return_value);\n') |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 190 | |
| 191 | class 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 Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 199 | [python start generated code]*/ |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 200 | /*[python end generated code: output=da39a3ee5e6b4b0d input=79464c61a31ae932]*/ |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 201 | |
| 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 Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 211 | static PyObject * |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 212 | _winapi_Overlapped_GetOverlappedResult_impl(OverlappedObject *self, int wait) |
| 213 | /*[clinic end generated code: output=bdd0c1ed6518cd03 input=194505ee8e0e3565]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 214 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 215 | BOOL res; |
| 216 | DWORD transferred = 0; |
| 217 | DWORD err; |
| 218 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 219 | 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 Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 236 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, err); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 237 | } |
| 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 Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 247 | /*[clinic input] |
| 248 | _winapi.Overlapped.getbuffer |
| 249 | [clinic start generated code]*/ |
| 250 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 251 | static PyObject * |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 252 | _winapi_Overlapped_getbuffer_impl(OverlappedObject *self) |
| 253 | /*[clinic end generated code: output=95a3eceefae0f748 input=347fcfd56b4ceabd]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 254 | { |
| 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 Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 267 | /*[clinic input] |
| 268 | _winapi.Overlapped.cancel |
| 269 | [clinic start generated code]*/ |
| 270 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 271 | static PyObject * |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 272 | _winapi_Overlapped_cancel_impl(OverlappedObject *self) |
| 273 | /*[clinic end generated code: output=fcb9ab5df4ebdae5 input=cbf3da142290039f]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 274 | { |
| 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 Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 288 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 289 | self->pending = 0; |
| 290 | Py_RETURN_NONE; |
| 291 | } |
| 292 | |
| 293 | static PyMethodDef overlapped_methods[] = { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 294 | _WINAPI_OVERLAPPED_GETOVERLAPPEDRESULT_METHODDEF |
| 295 | _WINAPI_OVERLAPPED_GETBUFFER_METHODDEF |
| 296 | _WINAPI_OVERLAPPED_CANCEL_METHODDEF |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 297 | {NULL} |
| 298 | }; |
| 299 | |
| 300 | static PyMemberDef overlapped_members[] = { |
| 301 | {"event", T_HANDLE, |
| 302 | offsetof(OverlappedObject, overlapped) + offsetof(OVERLAPPED, hEvent), |
| 303 | READONLY, "overlapped event handle"}, |
| 304 | {NULL} |
| 305 | }; |
| 306 | |
| 307 | PyTypeObject 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 | |
| 348 | static OverlappedObject * |
| 349 | new_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 Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 370 | /*[clinic input] |
| 371 | _winapi.CloseHandle |
| 372 | |
| 373 | handle: HANDLE |
| 374 | / |
| 375 | |
| 376 | Close handle. |
| 377 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 378 | |
| 379 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 380 | _winapi_CloseHandle_impl(PyObject *module, HANDLE handle) |
| 381 | /*[clinic end generated code: output=7ad37345f07bd782 input=7f0e4ac36e0352b8]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 382 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 383 | BOOL success; |
| 384 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 385 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 386 | success = CloseHandle(handle); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 387 | Py_END_ALLOW_THREADS |
| 388 | |
| 389 | if (!success) |
| 390 | return PyErr_SetFromWindowsErr(0); |
| 391 | |
| 392 | Py_RETURN_NONE; |
| 393 | } |
| 394 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 395 | /*[clinic input] |
| 396 | _winapi.ConnectNamedPipe |
| 397 | |
| 398 | handle: HANDLE |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 399 | overlapped as use_overlapped: bool(accept={int}) = False |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 400 | [clinic start generated code]*/ |
| 401 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 402 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 403 | _winapi_ConnectNamedPipe_impl(PyObject *module, HANDLE handle, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 404 | int use_overlapped) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 405 | /*[clinic end generated code: output=335a0e7086800671 input=34f937c1c86e5e68]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 406 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 407 | BOOL success; |
| 408 | OverlappedObject *overlapped = NULL; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 409 | |
| 410 | if (use_overlapped) { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 411 | overlapped = new_overlapped(handle); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 412 | if (!overlapped) |
| 413 | return NULL; |
| 414 | } |
| 415 | |
| 416 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 417 | success = ConnectNamedPipe(handle, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 418 | 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 Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 441 | /*[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 | |
| 454 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 455 | _winapi_CreateFile_impl(PyObject *module, LPCTSTR file_name, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 456 | DWORD desired_access, DWORD share_mode, |
| 457 | LPSECURITY_ATTRIBUTES security_attributes, |
| 458 | DWORD creation_disposition, |
| 459 | DWORD flags_and_attributes, HANDLE template_file) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 460 | /*[clinic end generated code: output=417ddcebfc5a3d53 input=6423c3e40372dbd5]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 461 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 462 | HANDLE handle; |
| 463 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 464 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 465 | handle = CreateFile(file_name, desired_access, |
| 466 | share_mode, security_attributes, |
| 467 | creation_disposition, |
| 468 | flags_and_attributes, template_file); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 469 | Py_END_ALLOW_THREADS |
| 470 | |
| 471 | if (handle == INVALID_HANDLE_VALUE) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 472 | PyErr_SetFromWindowsErr(0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 473 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 474 | return handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 475 | } |
| 476 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 477 | /*[clinic input] |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 478 | _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 | |
| 489 | static 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 Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 513 | _winapi.CreateJunction |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 514 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 515 | src_path: LPWSTR |
| 516 | dst_path: LPWSTR |
| 517 | / |
| 518 | [clinic start generated code]*/ |
| 519 | |
| 520 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 521 | _winapi_CreateJunction_impl(PyObject *module, LPWSTR src_path, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 522 | LPWSTR dst_path) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 523 | /*[clinic end generated code: output=66b7eb746e1dfa25 input=8cd1f9964b6e3d36]*/ |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 524 | { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 525 | /* 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 Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 533 | _Py_PREPARSE_DATA_BUFFER rdb = NULL; |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 534 | |
| 535 | /* Junction point creation */ |
| 536 | HANDLE junction = NULL; |
| 537 | DWORD ret = 0; |
| 538 | |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 539 | 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 Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 589 | rdb_size = _Py_REPARSE_DATA_BUFFER_HEADER_SIZE + |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 590 | 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 Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 594 | rdb = (_Py_PREPARSE_DATA_BUFFER)PyMem_RawMalloc(rdb_size); |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 595 | if (rdb == NULL) |
| 596 | goto cleanup; |
| 597 | |
| 598 | memset(rdb, 0, rdb_size); |
| 599 | rdb->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 600 | rdb->ReparseDataLength = rdb_size - _Py_REPARSE_DATA_BUFFER_HEADER_SIZE; |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 601 | 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 | |
| 636 | cleanup: |
| 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 Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 649 | /*[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 | |
| 663 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 664 | _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 Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 668 | LPSECURITY_ATTRIBUTES security_attributes) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 669 | /*[clinic end generated code: output=80f8c07346a94fbc input=5a73530b84d8bc37]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 670 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 671 | HANDLE handle; |
| 672 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 673 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 674 | handle = CreateNamedPipe(name, open_mode, pipe_mode, |
| 675 | max_instances, out_buffer_size, |
| 676 | in_buffer_size, default_timeout, |
| 677 | security_attributes); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 678 | Py_END_ALLOW_THREADS |
| 679 | |
| 680 | if (handle == INVALID_HANDLE_VALUE) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 681 | PyErr_SetFromWindowsErr(0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 682 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 683 | return handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 686 | /*[clinic input] |
| 687 | _winapi.CreatePipe |
| 688 | |
| 689 | pipe_attrs: object |
| 690 | Ignored internally, can be None. |
| 691 | size: DWORD |
| 692 | / |
| 693 | |
| 694 | Create an anonymous pipe. |
| 695 | |
| 696 | Returns a 2-tuple of handles, to the read and write ends of the pipe. |
| 697 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 698 | |
| 699 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 700 | _winapi_CreatePipe_impl(PyObject *module, PyObject *pipe_attrs, DWORD size) |
| 701 | /*[clinic end generated code: output=1c4411d8699f0925 input=c4f2cfa56ef68d90]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 702 | { |
| 703 | HANDLE read_pipe; |
| 704 | HANDLE write_pipe; |
| 705 | BOOL result; |
| 706 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 707 | 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 | |
| 720 | static unsigned long |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 721 | getulong(PyObject* obj, const char* name) |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 722 | { |
| 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 | |
| 736 | static HANDLE |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 737 | gethandle(PyObject* obj, const char* name) |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 738 | { |
| 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 | |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 755 | static wchar_t * |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 756 | getenvironment(PyObject* environment) |
| 757 | { |
| 758 | Py_ssize_t i, envsize, totalsize; |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 759 | wchar_t *buffer = NULL, *p, *end; |
| 760 | PyObject *keys, *values; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 761 | |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 762 | /* convert environment dictionary to windows environment string */ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 763 | if (! PyMapping_Check(environment)) { |
| 764 | PyErr_SetString( |
| 765 | PyExc_TypeError, "environment must be dictionary or None"); |
| 766 | return NULL; |
| 767 | } |
| 768 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 769 | keys = PyMapping_Keys(environment); |
Oren Milman | 0b3a87e | 2017-09-14 22:30:28 +0300 | [diff] [blame] | 770 | if (!keys) { |
| 771 | return NULL; |
| 772 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 773 | values = PyMapping_Values(environment); |
Oren Milman | 0b3a87e | 2017-09-14 22:30:28 +0300 | [diff] [blame] | 774 | if (!values) { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 775 | goto error; |
Oren Milman | 0b3a87e | 2017-09-14 22:30:28 +0300 | [diff] [blame] | 776 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 777 | |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 778 | envsize = PyList_GET_SIZE(keys); |
| 779 | if (PyList_GET_SIZE(values) != envsize) { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 780 | PyErr_SetString(PyExc_RuntimeError, |
| 781 | "environment changed size during iteration"); |
| 782 | goto error; |
| 783 | } |
| 784 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 785 | totalsize = 1; /* trailing null character */ |
| 786 | for (i = 0; i < envsize; i++) { |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 787 | PyObject* key = PyList_GET_ITEM(keys, i); |
| 788 | PyObject* value = PyList_GET_ITEM(values, i); |
| 789 | Py_ssize_t size; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 790 | |
| 791 | if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) { |
| 792 | PyErr_SetString(PyExc_TypeError, |
| 793 | "environment can only contain strings"); |
| 794 | goto error; |
| 795 | } |
Serhiy Storchaka | d174d24 | 2017-06-23 19:39:27 +0300 | [diff] [blame] | 796 | if (PyUnicode_FindChar(key, '\0', 0, PyUnicode_GET_LENGTH(key), 1) != -1 || |
| 797 | PyUnicode_FindChar(value, '\0', 0, PyUnicode_GET_LENGTH(value), 1) != -1) |
| 798 | { |
| 799 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 800 | goto error; |
| 801 | } |
| 802 | /* Search from index 1 because on Windows starting '=' is allowed for |
| 803 | defining hidden environment variables. */ |
| 804 | if (PyUnicode_GET_LENGTH(key) == 0 || |
| 805 | PyUnicode_FindChar(key, '=', 1, PyUnicode_GET_LENGTH(key), 1) != -1) |
| 806 | { |
| 807 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); |
| 808 | goto error; |
| 809 | } |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 810 | |
| 811 | size = PyUnicode_AsWideChar(key, NULL, 0); |
| 812 | assert(size > 1); |
| 813 | if (totalsize > PY_SSIZE_T_MAX - size) { |
Benjamin Peterson | 8ce6806 | 2015-02-09 20:58:12 -0500 | [diff] [blame] | 814 | PyErr_SetString(PyExc_OverflowError, "environment too long"); |
| 815 | goto error; |
| 816 | } |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 817 | totalsize += size; /* including '=' */ |
| 818 | |
| 819 | size = PyUnicode_AsWideChar(value, NULL, 0); |
| 820 | assert(size > 0); |
| 821 | if (totalsize > PY_SSIZE_T_MAX - size) { |
Benjamin Peterson | 8ce6806 | 2015-02-09 20:58:12 -0500 | [diff] [blame] | 822 | PyErr_SetString(PyExc_OverflowError, "environment too long"); |
| 823 | goto error; |
| 824 | } |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 825 | totalsize += size; /* including trailing '\0' */ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 826 | } |
| 827 | |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 828 | buffer = PyMem_NEW(wchar_t, totalsize); |
Benjamin Peterson | 8ce6806 | 2015-02-09 20:58:12 -0500 | [diff] [blame] | 829 | if (! buffer) { |
| 830 | PyErr_NoMemory(); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 831 | goto error; |
Benjamin Peterson | 8ce6806 | 2015-02-09 20:58:12 -0500 | [diff] [blame] | 832 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 833 | p = buffer; |
| 834 | end = buffer + totalsize; |
| 835 | |
| 836 | for (i = 0; i < envsize; i++) { |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 837 | PyObject* key = PyList_GET_ITEM(keys, i); |
| 838 | PyObject* value = PyList_GET_ITEM(values, i); |
| 839 | Py_ssize_t size = PyUnicode_AsWideChar(key, p, end - p); |
| 840 | assert(1 <= size && size < end - p); |
| 841 | p += size; |
| 842 | *p++ = L'='; |
| 843 | size = PyUnicode_AsWideChar(value, p, end - p); |
| 844 | assert(0 <= size && size < end - p); |
| 845 | p += size + 1; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 846 | } |
| 847 | |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 848 | /* add trailing null character */ |
| 849 | *p++ = L'\0'; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 850 | assert(p == end); |
| 851 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 852 | error: |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 853 | Py_XDECREF(keys); |
| 854 | Py_XDECREF(values); |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 855 | return buffer; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 856 | } |
| 857 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 858 | static LPHANDLE |
| 859 | gethandlelist(PyObject *mapping, const char *name, Py_ssize_t *size) |
| 860 | { |
| 861 | LPHANDLE ret = NULL; |
| 862 | PyObject *value_fast = NULL; |
| 863 | PyObject *value; |
| 864 | Py_ssize_t i; |
| 865 | |
| 866 | value = PyMapping_GetItemString(mapping, name); |
| 867 | if (!value) { |
| 868 | PyErr_Clear(); |
| 869 | return NULL; |
| 870 | } |
| 871 | |
| 872 | if (value == Py_None) { |
| 873 | goto cleanup; |
| 874 | } |
| 875 | |
| 876 | value_fast = PySequence_Fast(value, "handle_list must be a sequence or None"); |
| 877 | if (value_fast == NULL) |
| 878 | goto cleanup; |
| 879 | |
| 880 | *size = PySequence_Fast_GET_SIZE(value_fast) * sizeof(HANDLE); |
| 881 | |
| 882 | /* Passing an empty array causes CreateProcess to fail so just don't set it */ |
| 883 | if (*size == 0) { |
| 884 | goto cleanup; |
| 885 | } |
| 886 | |
| 887 | ret = PyMem_Malloc(*size); |
| 888 | if (ret == NULL) |
| 889 | goto cleanup; |
| 890 | |
| 891 | for (i = 0; i < PySequence_Fast_GET_SIZE(value_fast); i++) { |
| 892 | ret[i] = PYNUM_TO_HANDLE(PySequence_Fast_GET_ITEM(value_fast, i)); |
| 893 | if (ret[i] == (HANDLE)-1 && PyErr_Occurred()) { |
| 894 | PyMem_Free(ret); |
| 895 | ret = NULL; |
| 896 | goto cleanup; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | cleanup: |
| 901 | Py_DECREF(value); |
| 902 | Py_XDECREF(value_fast); |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | typedef struct { |
| 907 | LPPROC_THREAD_ATTRIBUTE_LIST attribute_list; |
| 908 | LPHANDLE handle_list; |
| 909 | } AttributeList; |
| 910 | |
| 911 | static void |
| 912 | freeattributelist(AttributeList *attribute_list) |
| 913 | { |
| 914 | if (attribute_list->attribute_list != NULL) { |
| 915 | DeleteProcThreadAttributeList(attribute_list->attribute_list); |
| 916 | PyMem_Free(attribute_list->attribute_list); |
| 917 | } |
| 918 | |
| 919 | PyMem_Free(attribute_list->handle_list); |
| 920 | |
| 921 | memset(attribute_list, 0, sizeof(*attribute_list)); |
| 922 | } |
| 923 | |
| 924 | static int |
| 925 | getattributelist(PyObject *obj, const char *name, AttributeList *attribute_list) |
| 926 | { |
| 927 | int ret = 0; |
| 928 | DWORD err; |
| 929 | BOOL result; |
| 930 | PyObject *value; |
| 931 | Py_ssize_t handle_list_size; |
| 932 | DWORD attribute_count = 0; |
| 933 | SIZE_T attribute_list_size = 0; |
| 934 | |
| 935 | value = PyObject_GetAttrString(obj, name); |
| 936 | if (!value) { |
| 937 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | if (value == Py_None) { |
| 942 | ret = 0; |
| 943 | goto cleanup; |
| 944 | } |
| 945 | |
| 946 | if (!PyMapping_Check(value)) { |
| 947 | ret = -1; |
| 948 | PyErr_Format(PyExc_TypeError, "%s must be a mapping or None", name); |
| 949 | goto cleanup; |
| 950 | } |
| 951 | |
| 952 | attribute_list->handle_list = gethandlelist(value, "handle_list", &handle_list_size); |
| 953 | if (attribute_list->handle_list == NULL && PyErr_Occurred()) { |
| 954 | ret = -1; |
| 955 | goto cleanup; |
| 956 | } |
| 957 | |
| 958 | if (attribute_list->handle_list != NULL) |
| 959 | ++attribute_count; |
| 960 | |
| 961 | /* Get how many bytes we need for the attribute list */ |
| 962 | result = InitializeProcThreadAttributeList(NULL, attribute_count, 0, &attribute_list_size); |
| 963 | if (result || GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
| 964 | ret = -1; |
| 965 | PyErr_SetFromWindowsErr(GetLastError()); |
| 966 | goto cleanup; |
| 967 | } |
| 968 | |
| 969 | attribute_list->attribute_list = PyMem_Malloc(attribute_list_size); |
| 970 | if (attribute_list->attribute_list == NULL) { |
| 971 | ret = -1; |
| 972 | goto cleanup; |
| 973 | } |
| 974 | |
| 975 | result = InitializeProcThreadAttributeList( |
| 976 | attribute_list->attribute_list, |
| 977 | attribute_count, |
| 978 | 0, |
| 979 | &attribute_list_size); |
| 980 | if (!result) { |
| 981 | err = GetLastError(); |
| 982 | |
| 983 | /* So that we won't call DeleteProcThreadAttributeList */ |
| 984 | PyMem_Free(attribute_list->attribute_list); |
| 985 | attribute_list->attribute_list = NULL; |
| 986 | |
| 987 | ret = -1; |
| 988 | PyErr_SetFromWindowsErr(err); |
| 989 | goto cleanup; |
| 990 | } |
| 991 | |
| 992 | if (attribute_list->handle_list != NULL) { |
| 993 | result = UpdateProcThreadAttribute( |
| 994 | attribute_list->attribute_list, |
| 995 | 0, |
| 996 | PROC_THREAD_ATTRIBUTE_HANDLE_LIST, |
| 997 | attribute_list->handle_list, |
| 998 | handle_list_size, |
| 999 | NULL, |
| 1000 | NULL); |
| 1001 | if (!result) { |
| 1002 | ret = -1; |
| 1003 | PyErr_SetFromWindowsErr(GetLastError()); |
| 1004 | goto cleanup; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | cleanup: |
| 1009 | Py_DECREF(value); |
| 1010 | |
| 1011 | if (ret < 0) |
| 1012 | freeattributelist(attribute_list); |
| 1013 | |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1017 | /*[clinic input] |
| 1018 | _winapi.CreateProcess |
| 1019 | |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1020 | application_name: Py_UNICODE(accept={str, NoneType}) |
Vladimir Matveev | 7b36016 | 2018-12-14 00:30:51 -0800 | [diff] [blame] | 1021 | command_line: object |
| 1022 | Can be str or None |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1023 | proc_attrs: object |
| 1024 | Ignored internally, can be None. |
| 1025 | thread_attrs: object |
| 1026 | Ignored internally, can be None. |
| 1027 | inherit_handles: BOOL |
| 1028 | creation_flags: DWORD |
| 1029 | env_mapping: object |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1030 | current_directory: Py_UNICODE(accept={str, NoneType}) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1031 | startup_info: object |
| 1032 | / |
| 1033 | |
| 1034 | Create a new process and its primary thread. |
| 1035 | |
| 1036 | The return value is a tuple of the process handle, thread handle, |
| 1037 | process ID, and thread ID. |
| 1038 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1039 | |
| 1040 | static PyObject * |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1041 | _winapi_CreateProcess_impl(PyObject *module, |
| 1042 | const Py_UNICODE *application_name, |
Vladimir Matveev | 7b36016 | 2018-12-14 00:30:51 -0800 | [diff] [blame] | 1043 | PyObject *command_line, PyObject *proc_attrs, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1044 | PyObject *thread_attrs, BOOL inherit_handles, |
| 1045 | DWORD creation_flags, PyObject *env_mapping, |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1046 | const Py_UNICODE *current_directory, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1047 | PyObject *startup_info) |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 1048 | /*[clinic end generated code: output=9b2423a609230132 input=42ac293eaea03fc4]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1049 | { |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1050 | PyObject *ret = NULL; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1051 | BOOL result; |
| 1052 | PROCESS_INFORMATION pi; |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1053 | STARTUPINFOEXW si; |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 1054 | wchar_t *wenvironment = NULL; |
Vladimir Matveev | 7b36016 | 2018-12-14 00:30:51 -0800 | [diff] [blame] | 1055 | wchar_t *command_line_copy = NULL; |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1056 | AttributeList attribute_list = {0}; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1057 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1058 | ZeroMemory(&si, sizeof(si)); |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1059 | si.StartupInfo.cb = sizeof(si); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1060 | |
| 1061 | /* note: we only support a small subset of all SI attributes */ |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1062 | si.StartupInfo.dwFlags = getulong(startup_info, "dwFlags"); |
| 1063 | si.StartupInfo.wShowWindow = (WORD)getulong(startup_info, "wShowWindow"); |
| 1064 | si.StartupInfo.hStdInput = gethandle(startup_info, "hStdInput"); |
| 1065 | si.StartupInfo.hStdOutput = gethandle(startup_info, "hStdOutput"); |
| 1066 | si.StartupInfo.hStdError = gethandle(startup_info, "hStdError"); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1067 | if (PyErr_Occurred()) |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1068 | goto cleanup; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1069 | |
| 1070 | if (env_mapping != Py_None) { |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 1071 | wenvironment = getenvironment(env_mapping); |
Serhiy Storchaka | d174d24 | 2017-06-23 19:39:27 +0300 | [diff] [blame] | 1072 | if (wenvironment == NULL) { |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1073 | goto cleanup; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1074 | } |
| 1075 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1076 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1077 | if (getattributelist(startup_info, "lpAttributeList", &attribute_list) < 0) |
| 1078 | goto cleanup; |
| 1079 | |
| 1080 | si.lpAttributeList = attribute_list.attribute_list; |
Vladimir Matveev | 7b36016 | 2018-12-14 00:30:51 -0800 | [diff] [blame] | 1081 | if (PyUnicode_Check(command_line)) { |
| 1082 | command_line_copy = PyUnicode_AsWideCharString(command_line, NULL); |
| 1083 | if (command_line_copy == NULL) { |
| 1084 | goto cleanup; |
| 1085 | } |
| 1086 | } |
| 1087 | else if (command_line != Py_None) { |
| 1088 | PyErr_Format(PyExc_TypeError, |
| 1089 | "CreateProcess() argument 2 must be str or None, not %s", |
| 1090 | Py_TYPE(command_line)->tp_name); |
| 1091 | goto cleanup; |
| 1092 | } |
| 1093 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1094 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1095 | Py_BEGIN_ALLOW_THREADS |
| 1096 | result = CreateProcessW(application_name, |
Vladimir Matveev | 7b36016 | 2018-12-14 00:30:51 -0800 | [diff] [blame] | 1097 | command_line_copy, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1098 | NULL, |
| 1099 | NULL, |
| 1100 | inherit_handles, |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1101 | creation_flags | EXTENDED_STARTUPINFO_PRESENT | |
| 1102 | CREATE_UNICODE_ENVIRONMENT, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1103 | wenvironment, |
| 1104 | current_directory, |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1105 | (LPSTARTUPINFOW)&si, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1106 | &pi); |
| 1107 | Py_END_ALLOW_THREADS |
| 1108 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1109 | if (!result) { |
| 1110 | PyErr_SetFromWindowsErr(GetLastError()); |
| 1111 | goto cleanup; |
| 1112 | } |
| 1113 | |
| 1114 | ret = Py_BuildValue("NNkk", |
| 1115 | HANDLE_TO_PYNUM(pi.hProcess), |
| 1116 | HANDLE_TO_PYNUM(pi.hThread), |
| 1117 | pi.dwProcessId, |
| 1118 | pi.dwThreadId); |
| 1119 | |
| 1120 | cleanup: |
Vladimir Matveev | 7b36016 | 2018-12-14 00:30:51 -0800 | [diff] [blame] | 1121 | PyMem_Free(command_line_copy); |
Serhiy Storchaka | 8abd7c7 | 2019-03-28 16:01:34 +0200 | [diff] [blame] | 1122 | PyMem_Free(wenvironment); |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1123 | freeattributelist(&attribute_list); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1124 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1125 | return ret; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1126 | } |
| 1127 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1128 | /*[clinic input] |
| 1129 | _winapi.DuplicateHandle -> HANDLE |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1130 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1131 | source_process_handle: HANDLE |
| 1132 | source_handle: HANDLE |
| 1133 | target_process_handle: HANDLE |
| 1134 | desired_access: DWORD |
| 1135 | inherit_handle: BOOL |
| 1136 | options: DWORD = 0 |
| 1137 | / |
| 1138 | |
| 1139 | Return a duplicate handle object. |
| 1140 | |
| 1141 | The duplicate handle refers to the same object as the original |
| 1142 | handle. Therefore, any changes to the object are reflected |
| 1143 | through both handles. |
| 1144 | [clinic start generated code]*/ |
| 1145 | |
| 1146 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1147 | _winapi_DuplicateHandle_impl(PyObject *module, HANDLE source_process_handle, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1148 | HANDLE source_handle, |
| 1149 | HANDLE target_process_handle, |
| 1150 | DWORD desired_access, BOOL inherit_handle, |
| 1151 | DWORD options) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1152 | /*[clinic end generated code: output=ad9711397b5dcd4e input=b933e3f2356a8c12]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1153 | { |
| 1154 | HANDLE target_handle; |
| 1155 | BOOL result; |
| 1156 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1157 | Py_BEGIN_ALLOW_THREADS |
| 1158 | result = DuplicateHandle( |
| 1159 | source_process_handle, |
| 1160 | source_handle, |
| 1161 | target_process_handle, |
| 1162 | &target_handle, |
| 1163 | desired_access, |
| 1164 | inherit_handle, |
| 1165 | options |
| 1166 | ); |
| 1167 | Py_END_ALLOW_THREADS |
| 1168 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1169 | if (! result) { |
| 1170 | PyErr_SetFromWindowsErr(GetLastError()); |
| 1171 | return INVALID_HANDLE_VALUE; |
| 1172 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1173 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1174 | return target_handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1177 | /*[clinic input] |
| 1178 | _winapi.ExitProcess |
| 1179 | |
| 1180 | ExitCode: UINT |
| 1181 | / |
| 1182 | |
| 1183 | [clinic start generated code]*/ |
| 1184 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1185 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1186 | _winapi_ExitProcess_impl(PyObject *module, UINT ExitCode) |
| 1187 | /*[clinic end generated code: output=a387deb651175301 input=4f05466a9406c558]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1188 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1189 | #if defined(Py_DEBUG) |
| 1190 | SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT| |
| 1191 | SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX); |
| 1192 | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
| 1193 | #endif |
| 1194 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1195 | ExitProcess(ExitCode); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1196 | |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1200 | /*[clinic input] |
| 1201 | _winapi.GetCurrentProcess -> HANDLE |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1202 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1203 | Return a handle object for the current process. |
| 1204 | [clinic start generated code]*/ |
| 1205 | |
| 1206 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1207 | _winapi_GetCurrentProcess_impl(PyObject *module) |
| 1208 | /*[clinic end generated code: output=ddeb4dd2ffadf344 input=b213403fd4b96b41]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1209 | { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1210 | return GetCurrentProcess(); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1211 | } |
| 1212 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1213 | /*[clinic input] |
| 1214 | _winapi.GetExitCodeProcess -> DWORD |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1215 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1216 | process: HANDLE |
| 1217 | / |
| 1218 | |
| 1219 | Return the termination status of the specified process. |
| 1220 | [clinic start generated code]*/ |
| 1221 | |
| 1222 | static DWORD |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1223 | _winapi_GetExitCodeProcess_impl(PyObject *module, HANDLE process) |
| 1224 | /*[clinic end generated code: output=b4620bdf2bccf36b input=61b6bfc7dc2ee374]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1225 | { |
| 1226 | DWORD exit_code; |
| 1227 | BOOL result; |
| 1228 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1229 | result = GetExitCodeProcess(process, &exit_code); |
| 1230 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1231 | if (! result) { |
| 1232 | PyErr_SetFromWindowsErr(GetLastError()); |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 1233 | exit_code = PY_DWORD_MAX; |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1234 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1235 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1236 | return exit_code; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1237 | } |
| 1238 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1239 | /*[clinic input] |
| 1240 | _winapi.GetLastError -> DWORD |
| 1241 | [clinic start generated code]*/ |
| 1242 | |
| 1243 | static DWORD |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1244 | _winapi_GetLastError_impl(PyObject *module) |
| 1245 | /*[clinic end generated code: output=8585b827cb1a92c5 input=62d47fb9bce038ba]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1246 | { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1247 | return GetLastError(); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1250 | /*[clinic input] |
| 1251 | _winapi.GetModuleFileName |
| 1252 | |
| 1253 | module_handle: HMODULE |
| 1254 | / |
| 1255 | |
| 1256 | Return the fully-qualified path for the file that contains module. |
| 1257 | |
| 1258 | The module must have been loaded by the current process. |
| 1259 | |
| 1260 | The module parameter should be a handle to the loaded module |
| 1261 | whose path is being requested. If this parameter is 0, |
| 1262 | GetModuleFileName retrieves the path of the executable file |
| 1263 | of the current process. |
| 1264 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1265 | |
| 1266 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1267 | _winapi_GetModuleFileName_impl(PyObject *module, HMODULE module_handle) |
| 1268 | /*[clinic end generated code: output=85b4b728c5160306 input=6d66ff7deca5d11f]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1269 | { |
| 1270 | BOOL result; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1271 | WCHAR filename[MAX_PATH]; |
| 1272 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1273 | result = GetModuleFileNameW(module_handle, filename, MAX_PATH); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1274 | filename[MAX_PATH-1] = '\0'; |
| 1275 | |
| 1276 | if (! result) |
| 1277 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 1278 | |
| 1279 | return PyUnicode_FromWideChar(filename, wcslen(filename)); |
| 1280 | } |
| 1281 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1282 | /*[clinic input] |
| 1283 | _winapi.GetStdHandle -> HANDLE |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1284 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1285 | std_handle: DWORD |
| 1286 | One of STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE. |
| 1287 | / |
| 1288 | |
| 1289 | Return a handle to the specified standard device. |
| 1290 | |
| 1291 | The integer associated with the handle object is returned. |
| 1292 | [clinic start generated code]*/ |
| 1293 | |
| 1294 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1295 | _winapi_GetStdHandle_impl(PyObject *module, DWORD std_handle) |
| 1296 | /*[clinic end generated code: output=0e613001e73ab614 input=07016b06a2fc8826]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1297 | { |
| 1298 | HANDLE handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1299 | |
| 1300 | Py_BEGIN_ALLOW_THREADS |
| 1301 | handle = GetStdHandle(std_handle); |
| 1302 | Py_END_ALLOW_THREADS |
| 1303 | |
| 1304 | if (handle == INVALID_HANDLE_VALUE) |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1305 | PyErr_SetFromWindowsErr(GetLastError()); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1306 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1307 | return handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1310 | /*[clinic input] |
| 1311 | _winapi.GetVersion -> long |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1312 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1313 | Return the version number of the current operating system. |
| 1314 | [clinic start generated code]*/ |
| 1315 | |
| 1316 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1317 | _winapi_GetVersion_impl(PyObject *module) |
| 1318 | /*[clinic end generated code: output=e41f0db5a3b82682 input=e21dff8d0baeded2]*/ |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 1319 | /* Disable deprecation warnings about GetVersionEx as the result is |
| 1320 | being passed straight through to the caller, who is responsible for |
| 1321 | using it correctly. */ |
| 1322 | #pragma warning(push) |
| 1323 | #pragma warning(disable:4996) |
| 1324 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1325 | { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1326 | return GetVersion(); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Steve Dower | 3e96f32 | 2015-03-02 08:01:10 -0800 | [diff] [blame] | 1329 | #pragma warning(pop) |
| 1330 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1331 | /*[clinic input] |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 1332 | _winapi.MapViewOfFile -> LPVOID |
| 1333 | |
| 1334 | file_map: HANDLE |
| 1335 | desired_access: DWORD |
| 1336 | file_offset_high: DWORD |
| 1337 | file_offset_low: DWORD |
| 1338 | number_bytes: size_t |
| 1339 | / |
| 1340 | [clinic start generated code]*/ |
| 1341 | |
| 1342 | static LPVOID |
| 1343 | _winapi_MapViewOfFile_impl(PyObject *module, HANDLE file_map, |
| 1344 | DWORD desired_access, DWORD file_offset_high, |
| 1345 | DWORD file_offset_low, size_t number_bytes) |
| 1346 | /*[clinic end generated code: output=f23b1ee4823663e3 input=177471073be1a103]*/ |
| 1347 | { |
| 1348 | LPVOID address; |
| 1349 | |
| 1350 | Py_BEGIN_ALLOW_THREADS |
| 1351 | address = MapViewOfFile(file_map, desired_access, file_offset_high, |
| 1352 | file_offset_low, number_bytes); |
| 1353 | Py_END_ALLOW_THREADS |
| 1354 | |
| 1355 | if (address == NULL) |
| 1356 | PyErr_SetFromWindowsErr(0); |
| 1357 | |
| 1358 | return address; |
| 1359 | } |
| 1360 | |
| 1361 | /*[clinic input] |
| 1362 | _winapi.OpenFileMapping -> HANDLE |
| 1363 | |
| 1364 | desired_access: DWORD |
| 1365 | inherit_handle: BOOL |
| 1366 | name: LPCWSTR |
| 1367 | / |
| 1368 | [clinic start generated code]*/ |
| 1369 | |
| 1370 | static HANDLE |
| 1371 | _winapi_OpenFileMapping_impl(PyObject *module, DWORD desired_access, |
| 1372 | BOOL inherit_handle, LPCWSTR name) |
| 1373 | /*[clinic end generated code: output=08cc44def1cb11f1 input=131f2a405359de7f]*/ |
| 1374 | { |
| 1375 | HANDLE handle; |
| 1376 | |
| 1377 | Py_BEGIN_ALLOW_THREADS |
| 1378 | handle = OpenFileMappingW(desired_access, inherit_handle, name); |
| 1379 | Py_END_ALLOW_THREADS |
| 1380 | |
| 1381 | if (handle == NULL) { |
| 1382 | PyErr_SetFromWindowsErrWithUnicodeFilename(0, name); |
| 1383 | handle = INVALID_HANDLE_VALUE; |
| 1384 | } |
| 1385 | |
| 1386 | return handle; |
| 1387 | } |
| 1388 | |
| 1389 | /*[clinic input] |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1390 | _winapi.OpenProcess -> HANDLE |
| 1391 | |
| 1392 | desired_access: DWORD |
| 1393 | inherit_handle: BOOL |
| 1394 | process_id: DWORD |
| 1395 | / |
| 1396 | [clinic start generated code]*/ |
| 1397 | |
| 1398 | static HANDLE |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1399 | _winapi_OpenProcess_impl(PyObject *module, DWORD desired_access, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1400 | BOOL inherit_handle, DWORD process_id) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1401 | /*[clinic end generated code: output=b42b6b81ea5a0fc3 input=ec98c4cf4ea2ec36]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1402 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1403 | HANDLE handle; |
| 1404 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1405 | handle = OpenProcess(desired_access, inherit_handle, process_id); |
| 1406 | if (handle == NULL) { |
| 1407 | PyErr_SetFromWindowsErr(0); |
| 1408 | handle = INVALID_HANDLE_VALUE; |
| 1409 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1410 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1411 | return handle; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1412 | } |
| 1413 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1414 | /*[clinic input] |
| 1415 | _winapi.PeekNamedPipe |
| 1416 | |
| 1417 | handle: HANDLE |
| 1418 | size: int = 0 |
| 1419 | / |
| 1420 | [clinic start generated code]*/ |
| 1421 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1422 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1423 | _winapi_PeekNamedPipe_impl(PyObject *module, HANDLE handle, int size) |
| 1424 | /*[clinic end generated code: output=d0c3e29e49d323dd input=c7aa53bfbce69d70]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1425 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1426 | PyObject *buf = NULL; |
| 1427 | DWORD nread, navail, nleft; |
| 1428 | BOOL ret; |
| 1429 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1430 | if (size < 0) { |
| 1431 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 1432 | return NULL; |
| 1433 | } |
| 1434 | |
| 1435 | if (size) { |
| 1436 | buf = PyBytes_FromStringAndSize(NULL, size); |
| 1437 | if (!buf) |
| 1438 | return NULL; |
| 1439 | Py_BEGIN_ALLOW_THREADS |
| 1440 | ret = PeekNamedPipe(handle, PyBytes_AS_STRING(buf), size, &nread, |
| 1441 | &navail, &nleft); |
| 1442 | Py_END_ALLOW_THREADS |
| 1443 | if (!ret) { |
| 1444 | Py_DECREF(buf); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1445 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1446 | } |
| 1447 | if (_PyBytes_Resize(&buf, nread)) |
| 1448 | return NULL; |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1449 | return Py_BuildValue("NII", buf, navail, nleft); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1450 | } |
| 1451 | else { |
| 1452 | Py_BEGIN_ALLOW_THREADS |
| 1453 | ret = PeekNamedPipe(handle, NULL, 0, NULL, &navail, &nleft); |
| 1454 | Py_END_ALLOW_THREADS |
| 1455 | if (!ret) { |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1456 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1457 | } |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1458 | return Py_BuildValue("II", navail, nleft); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1462 | /*[clinic input] |
| 1463 | _winapi.ReadFile |
| 1464 | |
| 1465 | handle: HANDLE |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1466 | size: DWORD |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1467 | overlapped as use_overlapped: bool(accept={int}) = False |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1468 | [clinic start generated code]*/ |
| 1469 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1470 | static PyObject * |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1471 | _winapi_ReadFile_impl(PyObject *module, HANDLE handle, DWORD size, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1472 | int use_overlapped) |
Alexander Buchkovsky | 266f490 | 2018-09-04 19:10:28 +0300 | [diff] [blame] | 1473 | /*[clinic end generated code: output=d3d5b44a8201b944 input=08c439d03a11aac5]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1474 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1475 | DWORD nread; |
| 1476 | PyObject *buf; |
| 1477 | BOOL ret; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1478 | DWORD err; |
| 1479 | OverlappedObject *overlapped = NULL; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1480 | |
| 1481 | buf = PyBytes_FromStringAndSize(NULL, size); |
| 1482 | if (!buf) |
| 1483 | return NULL; |
| 1484 | if (use_overlapped) { |
| 1485 | overlapped = new_overlapped(handle); |
| 1486 | if (!overlapped) { |
| 1487 | Py_DECREF(buf); |
| 1488 | return NULL; |
| 1489 | } |
| 1490 | /* Steals reference to buf */ |
| 1491 | overlapped->read_buffer = buf; |
| 1492 | } |
| 1493 | |
| 1494 | Py_BEGIN_ALLOW_THREADS |
| 1495 | ret = ReadFile(handle, PyBytes_AS_STRING(buf), size, &nread, |
| 1496 | overlapped ? &overlapped->overlapped : NULL); |
| 1497 | Py_END_ALLOW_THREADS |
| 1498 | |
| 1499 | err = ret ? 0 : GetLastError(); |
| 1500 | |
| 1501 | if (overlapped) { |
| 1502 | if (!ret) { |
| 1503 | if (err == ERROR_IO_PENDING) |
| 1504 | overlapped->pending = 1; |
| 1505 | else if (err != ERROR_MORE_DATA) { |
| 1506 | Py_DECREF(overlapped); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1507 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | return Py_BuildValue("NI", (PyObject *) overlapped, err); |
| 1511 | } |
| 1512 | |
| 1513 | if (!ret && err != ERROR_MORE_DATA) { |
| 1514 | Py_DECREF(buf); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1515 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1516 | } |
| 1517 | if (_PyBytes_Resize(&buf, nread)) |
| 1518 | return NULL; |
| 1519 | return Py_BuildValue("NI", buf, err); |
| 1520 | } |
| 1521 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1522 | /*[clinic input] |
| 1523 | _winapi.SetNamedPipeHandleState |
| 1524 | |
| 1525 | named_pipe: HANDLE |
| 1526 | mode: object |
| 1527 | max_collection_count: object |
| 1528 | collect_data_timeout: object |
| 1529 | / |
| 1530 | [clinic start generated code]*/ |
| 1531 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1532 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1533 | _winapi_SetNamedPipeHandleState_impl(PyObject *module, HANDLE named_pipe, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1534 | PyObject *mode, |
| 1535 | PyObject *max_collection_count, |
| 1536 | PyObject *collect_data_timeout) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1537 | /*[clinic end generated code: output=f2129d222cbfa095 input=9142d72163d0faa6]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1538 | { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1539 | PyObject *oArgs[3] = {mode, max_collection_count, collect_data_timeout}; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1540 | DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL}; |
| 1541 | int i; |
| 1542 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1543 | for (i = 0 ; i < 3 ; i++) { |
| 1544 | if (oArgs[i] != Py_None) { |
| 1545 | dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]); |
| 1546 | if (PyErr_Occurred()) |
| 1547 | return NULL; |
| 1548 | pArgs[i] = &dwArgs[i]; |
| 1549 | } |
| 1550 | } |
| 1551 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1552 | if (!SetNamedPipeHandleState(named_pipe, pArgs[0], pArgs[1], pArgs[2])) |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1553 | return PyErr_SetFromWindowsErr(0); |
| 1554 | |
| 1555 | Py_RETURN_NONE; |
| 1556 | } |
| 1557 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1558 | |
| 1559 | /*[clinic input] |
| 1560 | _winapi.TerminateProcess |
| 1561 | |
| 1562 | handle: HANDLE |
| 1563 | exit_code: UINT |
| 1564 | / |
| 1565 | |
| 1566 | Terminate the specified process and all of its threads. |
| 1567 | [clinic start generated code]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1568 | |
| 1569 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1570 | _winapi_TerminateProcess_impl(PyObject *module, HANDLE handle, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1571 | UINT exit_code) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1572 | /*[clinic end generated code: output=f4e99ac3f0b1f34a input=d6bc0aa1ee3bb4df]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1573 | { |
| 1574 | BOOL result; |
| 1575 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1576 | result = TerminateProcess(handle, exit_code); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1577 | |
| 1578 | if (! result) |
| 1579 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 1580 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1581 | Py_RETURN_NONE; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1584 | /*[clinic input] |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 1585 | _winapi.VirtualQuerySize -> size_t |
| 1586 | |
| 1587 | address: LPCVOID |
| 1588 | / |
| 1589 | [clinic start generated code]*/ |
| 1590 | |
| 1591 | static size_t |
| 1592 | _winapi_VirtualQuerySize_impl(PyObject *module, LPCVOID address) |
| 1593 | /*[clinic end generated code: output=40c8e0ff5ec964df input=6b784a69755d0bb6]*/ |
| 1594 | { |
| 1595 | SIZE_T size_of_buf; |
| 1596 | MEMORY_BASIC_INFORMATION mem_basic_info; |
| 1597 | SIZE_T region_size; |
| 1598 | |
| 1599 | Py_BEGIN_ALLOW_THREADS |
| 1600 | size_of_buf = VirtualQuery(address, &mem_basic_info, sizeof(mem_basic_info)); |
| 1601 | Py_END_ALLOW_THREADS |
| 1602 | |
| 1603 | if (size_of_buf == 0) |
| 1604 | PyErr_SetFromWindowsErr(0); |
| 1605 | |
| 1606 | region_size = mem_basic_info.RegionSize; |
| 1607 | return region_size; |
| 1608 | } |
| 1609 | |
| 1610 | /*[clinic input] |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1611 | _winapi.WaitNamedPipe |
| 1612 | |
| 1613 | name: LPCTSTR |
| 1614 | timeout: DWORD |
| 1615 | / |
| 1616 | [clinic start generated code]*/ |
| 1617 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1618 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1619 | _winapi_WaitNamedPipe_impl(PyObject *module, LPCTSTR name, DWORD timeout) |
| 1620 | /*[clinic end generated code: output=c2866f4439b1fe38 input=36fc781291b1862c]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1621 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1622 | BOOL success; |
| 1623 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1624 | Py_BEGIN_ALLOW_THREADS |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1625 | success = WaitNamedPipe(name, timeout); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1626 | Py_END_ALLOW_THREADS |
| 1627 | |
| 1628 | if (!success) |
| 1629 | return PyErr_SetFromWindowsErr(0); |
| 1630 | |
| 1631 | Py_RETURN_NONE; |
| 1632 | } |
| 1633 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1634 | /*[clinic input] |
| 1635 | _winapi.WaitForMultipleObjects |
| 1636 | |
| 1637 | handle_seq: object |
| 1638 | wait_flag: BOOL |
| 1639 | milliseconds: DWORD(c_default='INFINITE') = _winapi.INFINITE |
| 1640 | / |
| 1641 | [clinic start generated code]*/ |
| 1642 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1643 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1644 | _winapi_WaitForMultipleObjects_impl(PyObject *module, PyObject *handle_seq, |
| 1645 | BOOL wait_flag, DWORD milliseconds) |
| 1646 | /*[clinic end generated code: output=295e3f00b8e45899 input=36f76ca057cd28a0]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1647 | { |
| 1648 | DWORD result; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1649 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 1650 | HANDLE sigint_event = NULL; |
| 1651 | Py_ssize_t nhandles, i; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1652 | |
| 1653 | if (!PySequence_Check(handle_seq)) { |
| 1654 | PyErr_Format(PyExc_TypeError, |
| 1655 | "sequence type expected, got '%s'", |
Richard Oudkerk | 6733927 | 2012-08-21 14:54:22 +0100 | [diff] [blame] | 1656 | Py_TYPE(handle_seq)->tp_name); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1657 | return NULL; |
| 1658 | } |
| 1659 | nhandles = PySequence_Length(handle_seq); |
| 1660 | if (nhandles == -1) |
| 1661 | return NULL; |
| 1662 | if (nhandles < 0 || nhandles >= MAXIMUM_WAIT_OBJECTS - 1) { |
| 1663 | PyErr_Format(PyExc_ValueError, |
| 1664 | "need at most %zd handles, got a sequence of length %zd", |
| 1665 | MAXIMUM_WAIT_OBJECTS - 1, nhandles); |
| 1666 | return NULL; |
| 1667 | } |
| 1668 | for (i = 0; i < nhandles; i++) { |
| 1669 | HANDLE h; |
| 1670 | PyObject *v = PySequence_GetItem(handle_seq, i); |
| 1671 | if (v == NULL) |
| 1672 | return NULL; |
| 1673 | if (!PyArg_Parse(v, F_HANDLE, &h)) { |
| 1674 | Py_DECREF(v); |
| 1675 | return NULL; |
| 1676 | } |
| 1677 | handles[i] = h; |
| 1678 | Py_DECREF(v); |
| 1679 | } |
| 1680 | /* If this is the main thread then make the wait interruptible |
| 1681 | by Ctrl-C unless we are waiting for *all* handles */ |
| 1682 | if (!wait_flag && _PyOS_IsMainThread()) { |
| 1683 | sigint_event = _PyOS_SigintEvent(); |
| 1684 | assert(sigint_event != NULL); |
| 1685 | handles[nhandles++] = sigint_event; |
| 1686 | } |
| 1687 | |
| 1688 | Py_BEGIN_ALLOW_THREADS |
| 1689 | if (sigint_event != NULL) |
| 1690 | ResetEvent(sigint_event); |
| 1691 | result = WaitForMultipleObjects((DWORD) nhandles, handles, |
| 1692 | wait_flag, milliseconds); |
| 1693 | Py_END_ALLOW_THREADS |
| 1694 | |
| 1695 | if (result == WAIT_FAILED) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1696 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1697 | else if (sigint_event != NULL && result == WAIT_OBJECT_0 + nhandles - 1) { |
| 1698 | errno = EINTR; |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1699 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | return PyLong_FromLong((int) result); |
| 1703 | } |
| 1704 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1705 | /*[clinic input] |
| 1706 | _winapi.WaitForSingleObject -> long |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1707 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1708 | handle: HANDLE |
| 1709 | milliseconds: DWORD |
| 1710 | / |
| 1711 | |
| 1712 | Wait for a single object. |
| 1713 | |
| 1714 | Wait until the specified object is in the signaled state or |
| 1715 | the time-out interval elapses. The timeout value is specified |
| 1716 | in milliseconds. |
| 1717 | [clinic start generated code]*/ |
| 1718 | |
| 1719 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1720 | _winapi_WaitForSingleObject_impl(PyObject *module, HANDLE handle, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1721 | DWORD milliseconds) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1722 | /*[clinic end generated code: output=3c4715d8f1b39859 input=443d1ab076edc7b1]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1723 | { |
| 1724 | DWORD result; |
| 1725 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1726 | Py_BEGIN_ALLOW_THREADS |
| 1727 | result = WaitForSingleObject(handle, milliseconds); |
| 1728 | Py_END_ALLOW_THREADS |
| 1729 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1730 | if (result == WAIT_FAILED) { |
| 1731 | PyErr_SetFromWindowsErr(GetLastError()); |
| 1732 | return -1; |
| 1733 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1734 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1735 | return result; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1736 | } |
| 1737 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1738 | /*[clinic input] |
| 1739 | _winapi.WriteFile |
| 1740 | |
| 1741 | handle: HANDLE |
| 1742 | buffer: object |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1743 | overlapped as use_overlapped: bool(accept={int}) = False |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1744 | [clinic start generated code]*/ |
| 1745 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1746 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1747 | _winapi_WriteFile_impl(PyObject *module, HANDLE handle, PyObject *buffer, |
Zachary Ware | 77772c0 | 2015-05-13 10:58:35 -0500 | [diff] [blame] | 1748 | int use_overlapped) |
Serhiy Storchaka | 202fda5 | 2017-03-12 10:10:47 +0200 | [diff] [blame] | 1749 | /*[clinic end generated code: output=2ca80f6bf3fa92e3 input=11eae2a03aa32731]*/ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1750 | { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1751 | Py_buffer _buf, *buf; |
Victor Stinner | 7176577 | 2013-06-24 23:13:24 +0200 | [diff] [blame] | 1752 | DWORD len, written; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1753 | BOOL ret; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1754 | DWORD err; |
| 1755 | OverlappedObject *overlapped = NULL; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1756 | |
| 1757 | if (use_overlapped) { |
| 1758 | overlapped = new_overlapped(handle); |
| 1759 | if (!overlapped) |
| 1760 | return NULL; |
| 1761 | buf = &overlapped->write_buffer; |
| 1762 | } |
| 1763 | else |
| 1764 | buf = &_buf; |
| 1765 | |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1766 | if (!PyArg_Parse(buffer, "y*", buf)) { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1767 | Py_XDECREF(overlapped); |
| 1768 | return NULL; |
| 1769 | } |
| 1770 | |
| 1771 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 1772 | len = (DWORD)Py_MIN(buf->len, PY_DWORD_MAX); |
Victor Stinner | 7176577 | 2013-06-24 23:13:24 +0200 | [diff] [blame] | 1773 | ret = WriteFile(handle, buf->buf, len, &written, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1774 | overlapped ? &overlapped->overlapped : NULL); |
| 1775 | Py_END_ALLOW_THREADS |
| 1776 | |
| 1777 | err = ret ? 0 : GetLastError(); |
| 1778 | |
| 1779 | if (overlapped) { |
| 1780 | if (!ret) { |
| 1781 | if (err == ERROR_IO_PENDING) |
| 1782 | overlapped->pending = 1; |
| 1783 | else { |
| 1784 | Py_DECREF(overlapped); |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1785 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | return Py_BuildValue("NI", (PyObject *) overlapped, err); |
| 1789 | } |
| 1790 | |
| 1791 | PyBuffer_Release(buf); |
| 1792 | if (!ret) |
Serhiy Storchaka | 55fe1ae | 2017-04-16 10:46:38 +0300 | [diff] [blame] | 1793 | return PyErr_SetExcFromWindowsErr(PyExc_OSError, 0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1794 | return Py_BuildValue("II", written, err); |
| 1795 | } |
| 1796 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1797 | /*[clinic input] |
| 1798 | _winapi.GetACP |
| 1799 | |
| 1800 | Get the current Windows ANSI code page identifier. |
| 1801 | [clinic start generated code]*/ |
| 1802 | |
| 1803 | static PyObject * |
| 1804 | _winapi_GetACP_impl(PyObject *module) |
| 1805 | /*[clinic end generated code: output=f7ee24bf705dbb88 input=1433c96d03a05229]*/ |
| 1806 | { |
| 1807 | return PyLong_FromUnsignedLong(GetACP()); |
| 1808 | } |
| 1809 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1810 | /*[clinic input] |
| 1811 | _winapi.GetFileType -> DWORD |
| 1812 | |
| 1813 | handle: HANDLE |
| 1814 | [clinic start generated code]*/ |
| 1815 | |
| 1816 | static DWORD |
| 1817 | _winapi_GetFileType_impl(PyObject *module, HANDLE handle) |
| 1818 | /*[clinic end generated code: output=92b8466ac76ecc17 input=0058366bc40bbfbf]*/ |
| 1819 | { |
| 1820 | DWORD result; |
| 1821 | |
| 1822 | Py_BEGIN_ALLOW_THREADS |
| 1823 | result = GetFileType(handle); |
| 1824 | Py_END_ALLOW_THREADS |
| 1825 | |
| 1826 | if (result == FILE_TYPE_UNKNOWN && GetLastError() != NO_ERROR) { |
| 1827 | PyErr_SetFromWindowsErr(0); |
| 1828 | return -1; |
| 1829 | } |
| 1830 | |
| 1831 | return result; |
| 1832 | } |
| 1833 | |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1834 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1835 | static PyMethodDef winapi_functions[] = { |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1836 | _WINAPI_CLOSEHANDLE_METHODDEF |
| 1837 | _WINAPI_CONNECTNAMEDPIPE_METHODDEF |
| 1838 | _WINAPI_CREATEFILE_METHODDEF |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 1839 | _WINAPI_CREATEFILEMAPPING_METHODDEF |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1840 | _WINAPI_CREATENAMEDPIPE_METHODDEF |
| 1841 | _WINAPI_CREATEPIPE_METHODDEF |
| 1842 | _WINAPI_CREATEPROCESS_METHODDEF |
| 1843 | _WINAPI_CREATEJUNCTION_METHODDEF |
| 1844 | _WINAPI_DUPLICATEHANDLE_METHODDEF |
| 1845 | _WINAPI_EXITPROCESS_METHODDEF |
| 1846 | _WINAPI_GETCURRENTPROCESS_METHODDEF |
| 1847 | _WINAPI_GETEXITCODEPROCESS_METHODDEF |
| 1848 | _WINAPI_GETLASTERROR_METHODDEF |
| 1849 | _WINAPI_GETMODULEFILENAME_METHODDEF |
| 1850 | _WINAPI_GETSTDHANDLE_METHODDEF |
| 1851 | _WINAPI_GETVERSION_METHODDEF |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 1852 | _WINAPI_MAPVIEWOFFILE_METHODDEF |
| 1853 | _WINAPI_OPENFILEMAPPING_METHODDEF |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1854 | _WINAPI_OPENPROCESS_METHODDEF |
| 1855 | _WINAPI_PEEKNAMEDPIPE_METHODDEF |
| 1856 | _WINAPI_READFILE_METHODDEF |
| 1857 | _WINAPI_SETNAMEDPIPEHANDLESTATE_METHODDEF |
| 1858 | _WINAPI_TERMINATEPROCESS_METHODDEF |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 1859 | _WINAPI_VIRTUALQUERYSIZE_METHODDEF |
Zachary Ware | f2244ea | 2015-05-13 01:22:54 -0500 | [diff] [blame] | 1860 | _WINAPI_WAITNAMEDPIPE_METHODDEF |
| 1861 | _WINAPI_WAITFORMULTIPLEOBJECTS_METHODDEF |
| 1862 | _WINAPI_WAITFORSINGLEOBJECT_METHODDEF |
| 1863 | _WINAPI_WRITEFILE_METHODDEF |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1864 | _WINAPI_GETACP_METHODDEF |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1865 | _WINAPI_GETFILETYPE_METHODDEF |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1866 | {NULL, NULL} |
| 1867 | }; |
| 1868 | |
| 1869 | static struct PyModuleDef winapi_module = { |
| 1870 | PyModuleDef_HEAD_INIT, |
| 1871 | "_winapi", |
| 1872 | NULL, |
| 1873 | -1, |
| 1874 | winapi_functions, |
| 1875 | NULL, |
| 1876 | NULL, |
| 1877 | NULL, |
| 1878 | NULL |
| 1879 | }; |
| 1880 | |
| 1881 | #define WINAPI_CONSTANT(fmt, con) \ |
| 1882 | PyDict_SetItemString(d, #con, Py_BuildValue(fmt, con)) |
| 1883 | |
| 1884 | PyMODINIT_FUNC |
| 1885 | PyInit__winapi(void) |
| 1886 | { |
| 1887 | PyObject *d; |
| 1888 | PyObject *m; |
| 1889 | |
| 1890 | if (PyType_Ready(&OverlappedType) < 0) |
| 1891 | return NULL; |
| 1892 | |
| 1893 | m = PyModule_Create(&winapi_module); |
| 1894 | if (m == NULL) |
| 1895 | return NULL; |
| 1896 | d = PyModule_GetDict(m); |
| 1897 | |
| 1898 | PyDict_SetItemString(d, "Overlapped", (PyObject *) &OverlappedType); |
| 1899 | |
| 1900 | /* constants */ |
| 1901 | WINAPI_CONSTANT(F_DWORD, CREATE_NEW_CONSOLE); |
| 1902 | WINAPI_CONSTANT(F_DWORD, CREATE_NEW_PROCESS_GROUP); |
| 1903 | WINAPI_CONSTANT(F_DWORD, DUPLICATE_SAME_ACCESS); |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 1904 | WINAPI_CONSTANT(F_DWORD, DUPLICATE_CLOSE_SOURCE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1905 | WINAPI_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS); |
| 1906 | WINAPI_CONSTANT(F_DWORD, ERROR_BROKEN_PIPE); |
| 1907 | WINAPI_CONSTANT(F_DWORD, ERROR_IO_PENDING); |
| 1908 | WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA); |
| 1909 | WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED); |
| 1910 | WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1911 | WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA); |
| 1912 | WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED); |
Richard Oudkerk | fdb8dcf | 2012-05-05 19:45:37 +0100 | [diff] [blame] | 1913 | WINAPI_CONSTANT(F_DWORD, ERROR_NO_DATA); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1914 | WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES); |
| 1915 | WINAPI_CONSTANT(F_DWORD, ERROR_OPERATION_ABORTED); |
| 1916 | WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_BUSY); |
| 1917 | WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED); |
| 1918 | WINAPI_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT); |
| 1919 | WINAPI_CONSTANT(F_DWORD, FILE_FLAG_FIRST_PIPE_INSTANCE); |
| 1920 | WINAPI_CONSTANT(F_DWORD, FILE_FLAG_OVERLAPPED); |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 1921 | WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_READ); |
| 1922 | WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_WRITE); |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 1923 | WINAPI_CONSTANT(F_DWORD, FILE_MAP_ALL_ACCESS); |
| 1924 | WINAPI_CONSTANT(F_DWORD, FILE_MAP_COPY); |
| 1925 | WINAPI_CONSTANT(F_DWORD, FILE_MAP_EXECUTE); |
| 1926 | WINAPI_CONSTANT(F_DWORD, FILE_MAP_READ); |
| 1927 | WINAPI_CONSTANT(F_DWORD, FILE_MAP_WRITE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1928 | WINAPI_CONSTANT(F_DWORD, GENERIC_READ); |
| 1929 | WINAPI_CONSTANT(F_DWORD, GENERIC_WRITE); |
| 1930 | WINAPI_CONSTANT(F_DWORD, INFINITE); |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 1931 | WINAPI_CONSTANT(F_HANDLE, INVALID_HANDLE_VALUE); |
| 1932 | WINAPI_CONSTANT(F_DWORD, MEM_COMMIT); |
| 1933 | WINAPI_CONSTANT(F_DWORD, MEM_FREE); |
| 1934 | WINAPI_CONSTANT(F_DWORD, MEM_IMAGE); |
| 1935 | WINAPI_CONSTANT(F_DWORD, MEM_MAPPED); |
| 1936 | WINAPI_CONSTANT(F_DWORD, MEM_PRIVATE); |
| 1937 | WINAPI_CONSTANT(F_DWORD, MEM_RESERVE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1938 | WINAPI_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER); |
| 1939 | WINAPI_CONSTANT(F_DWORD, OPEN_EXISTING); |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 1940 | WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE); |
| 1941 | WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE_READ); |
| 1942 | WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE_READWRITE); |
| 1943 | WINAPI_CONSTANT(F_DWORD, PAGE_EXECUTE_WRITECOPY); |
| 1944 | WINAPI_CONSTANT(F_DWORD, PAGE_GUARD); |
| 1945 | WINAPI_CONSTANT(F_DWORD, PAGE_NOACCESS); |
| 1946 | WINAPI_CONSTANT(F_DWORD, PAGE_NOCACHE); |
| 1947 | WINAPI_CONSTANT(F_DWORD, PAGE_READONLY); |
| 1948 | WINAPI_CONSTANT(F_DWORD, PAGE_READWRITE); |
| 1949 | WINAPI_CONSTANT(F_DWORD, PAGE_WRITECOMBINE); |
| 1950 | WINAPI_CONSTANT(F_DWORD, PAGE_WRITECOPY); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1951 | WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX); |
| 1952 | WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND); |
| 1953 | WINAPI_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE); |
| 1954 | WINAPI_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE); |
| 1955 | WINAPI_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES); |
| 1956 | WINAPI_CONSTANT(F_DWORD, PIPE_WAIT); |
| 1957 | WINAPI_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS); |
Thomas Moreau | c09a9f5 | 2019-05-20 21:37:05 +0200 | [diff] [blame] | 1958 | WINAPI_CONSTANT(F_DWORD, SYNCHRONIZE); |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 1959 | WINAPI_CONSTANT(F_DWORD, PROCESS_DUP_HANDLE); |
Davin Potts | e895de3 | 2019-02-23 22:08:16 -0600 | [diff] [blame] | 1960 | WINAPI_CONSTANT(F_DWORD, SEC_COMMIT); |
| 1961 | WINAPI_CONSTANT(F_DWORD, SEC_IMAGE); |
| 1962 | WINAPI_CONSTANT(F_DWORD, SEC_LARGE_PAGES); |
| 1963 | WINAPI_CONSTANT(F_DWORD, SEC_NOCACHE); |
| 1964 | WINAPI_CONSTANT(F_DWORD, SEC_RESERVE); |
| 1965 | WINAPI_CONSTANT(F_DWORD, SEC_WRITECOMBINE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1966 | WINAPI_CONSTANT(F_DWORD, STARTF_USESHOWWINDOW); |
| 1967 | WINAPI_CONSTANT(F_DWORD, STARTF_USESTDHANDLES); |
| 1968 | WINAPI_CONSTANT(F_DWORD, STD_INPUT_HANDLE); |
| 1969 | WINAPI_CONSTANT(F_DWORD, STD_OUTPUT_HANDLE); |
| 1970 | WINAPI_CONSTANT(F_DWORD, STD_ERROR_HANDLE); |
| 1971 | WINAPI_CONSTANT(F_DWORD, STILL_ACTIVE); |
| 1972 | WINAPI_CONSTANT(F_DWORD, SW_HIDE); |
| 1973 | WINAPI_CONSTANT(F_DWORD, WAIT_OBJECT_0); |
Victor Stinner | 373f0a9 | 2014-03-20 09:26:55 +0100 | [diff] [blame] | 1974 | WINAPI_CONSTANT(F_DWORD, WAIT_ABANDONED_0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1975 | WINAPI_CONSTANT(F_DWORD, WAIT_TIMEOUT); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1976 | |
James | b5d9e08 | 2017-11-08 14:18:59 +0000 | [diff] [blame] | 1977 | WINAPI_CONSTANT(F_DWORD, ABOVE_NORMAL_PRIORITY_CLASS); |
| 1978 | WINAPI_CONSTANT(F_DWORD, BELOW_NORMAL_PRIORITY_CLASS); |
| 1979 | WINAPI_CONSTANT(F_DWORD, HIGH_PRIORITY_CLASS); |
| 1980 | WINAPI_CONSTANT(F_DWORD, IDLE_PRIORITY_CLASS); |
| 1981 | WINAPI_CONSTANT(F_DWORD, NORMAL_PRIORITY_CLASS); |
| 1982 | WINAPI_CONSTANT(F_DWORD, REALTIME_PRIORITY_CLASS); |
Victor Stinner | 91106cd | 2017-12-13 12:29:09 +0100 | [diff] [blame] | 1983 | |
James | b5d9e08 | 2017-11-08 14:18:59 +0000 | [diff] [blame] | 1984 | WINAPI_CONSTANT(F_DWORD, CREATE_NO_WINDOW); |
| 1985 | WINAPI_CONSTANT(F_DWORD, DETACHED_PROCESS); |
| 1986 | WINAPI_CONSTANT(F_DWORD, CREATE_DEFAULT_ERROR_MODE); |
| 1987 | WINAPI_CONSTANT(F_DWORD, CREATE_BREAKAWAY_FROM_JOB); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1988 | |
Segev Finer | b2a6083 | 2017-12-18 11:28:19 +0200 | [diff] [blame] | 1989 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_UNKNOWN); |
| 1990 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_DISK); |
| 1991 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_CHAR); |
| 1992 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_PIPE); |
| 1993 | WINAPI_CONSTANT(F_DWORD, FILE_TYPE_REMOTE); |
| 1994 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1995 | WINAPI_CONSTANT("i", NULL); |
| 1996 | |
| 1997 | return m; |
| 1998 | } |