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