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