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> |
| 43 | |
| 44 | #if defined(MS_WIN32) && !defined(MS_WIN64) |
| 45 | #define HANDLE_TO_PYNUM(handle) \ |
| 46 | PyLong_FromUnsignedLong((unsigned long) handle) |
| 47 | #define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLong(obj)) |
| 48 | #define F_POINTER "k" |
| 49 | #define T_POINTER T_ULONG |
| 50 | #else |
| 51 | #define HANDLE_TO_PYNUM(handle) \ |
| 52 | PyLong_FromUnsignedLongLong((unsigned long long) handle) |
| 53 | #define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLongLong(obj)) |
| 54 | #define F_POINTER "K" |
| 55 | #define T_POINTER T_ULONGLONG |
| 56 | #endif |
| 57 | |
| 58 | #define F_HANDLE F_POINTER |
| 59 | #define F_DWORD "k" |
| 60 | #define F_BOOL "i" |
| 61 | #define F_UINT "I" |
| 62 | |
| 63 | #define T_HANDLE T_POINTER |
| 64 | |
Victor Stinner | 7176577 | 2013-06-24 23:13:24 +0200 | [diff] [blame] | 65 | #define DWORD_MAX 4294967295U |
| 66 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 67 | /* Grab CancelIoEx dynamically from kernel32 */ |
| 68 | static int has_CancelIoEx = -1; |
| 69 | static BOOL (CALLBACK *Py_CancelIoEx)(HANDLE, LPOVERLAPPED); |
| 70 | |
| 71 | static int |
| 72 | check_CancelIoEx() |
| 73 | { |
| 74 | if (has_CancelIoEx == -1) |
| 75 | { |
| 76 | HINSTANCE hKernel32 = GetModuleHandle("KERNEL32"); |
| 77 | * (FARPROC *) &Py_CancelIoEx = GetProcAddress(hKernel32, |
| 78 | "CancelIoEx"); |
| 79 | has_CancelIoEx = (Py_CancelIoEx != NULL); |
| 80 | } |
| 81 | return has_CancelIoEx; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /* |
| 86 | * A Python object wrapping an OVERLAPPED structure and other useful data |
| 87 | * for overlapped I/O |
| 88 | */ |
| 89 | |
| 90 | typedef struct { |
| 91 | PyObject_HEAD |
| 92 | OVERLAPPED overlapped; |
| 93 | /* For convenience, we store the file handle too */ |
| 94 | HANDLE handle; |
| 95 | /* Whether there's I/O in flight */ |
| 96 | int pending; |
| 97 | /* Whether I/O completed successfully */ |
| 98 | int completed; |
| 99 | /* Buffer used for reading (optional) */ |
| 100 | PyObject *read_buffer; |
| 101 | /* Buffer used for writing (optional) */ |
| 102 | Py_buffer write_buffer; |
| 103 | } OverlappedObject; |
| 104 | |
| 105 | static void |
| 106 | overlapped_dealloc(OverlappedObject *self) |
| 107 | { |
| 108 | DWORD bytes; |
| 109 | int err = GetLastError(); |
Richard Oudkerk | 633db6f | 2013-11-17 13:15:51 +0000 | [diff] [blame] | 110 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 111 | if (self->pending) { |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 112 | if (check_CancelIoEx() && |
Richard Oudkerk | 633db6f | 2013-11-17 13:15:51 +0000 | [diff] [blame] | 113 | Py_CancelIoEx(self->handle, &self->overlapped) && |
| 114 | GetOverlappedResult(self->handle, &self->overlapped, &bytes, TRUE)) |
| 115 | { |
| 116 | /* The operation is no longer pending -- nothing to do. */ |
| 117 | } |
| 118 | else if (_Py_Finalizing == NULL) |
| 119 | { |
| 120 | /* The operation is still pending -- give a warning. This |
| 121 | will probably only happen on Windows XP. */ |
| 122 | PyErr_SetString(PyExc_RuntimeError, |
| 123 | "I/O operations still in flight while destroying " |
| 124 | "Overlapped object, the process may crash"); |
| 125 | PyErr_WriteUnraisable(NULL); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | /* The operation is still pending, but the process is |
| 130 | probably about to exit, so we need not worry too much |
| 131 | about memory leaks. Leaking self prevents a potential |
| 132 | crash. This can happen when a daemon thread is cleaned |
| 133 | up at exit -- see #19565. We only expect to get here |
| 134 | on Windows XP. */ |
| 135 | CloseHandle(self->overlapped.hEvent); |
| 136 | SetLastError(err); |
| 137 | return; |
| 138 | } |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 139 | } |
Richard Oudkerk | 633db6f | 2013-11-17 13:15:51 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 141 | CloseHandle(self->overlapped.hEvent); |
| 142 | SetLastError(err); |
| 143 | if (self->write_buffer.obj) |
| 144 | PyBuffer_Release(&self->write_buffer); |
| 145 | Py_CLEAR(self->read_buffer); |
| 146 | PyObject_Del(self); |
| 147 | } |
| 148 | |
| 149 | static PyObject * |
| 150 | overlapped_GetOverlappedResult(OverlappedObject *self, PyObject *waitobj) |
| 151 | { |
| 152 | int wait; |
| 153 | BOOL res; |
| 154 | DWORD transferred = 0; |
| 155 | DWORD err; |
| 156 | |
| 157 | wait = PyObject_IsTrue(waitobj); |
| 158 | if (wait < 0) |
| 159 | return NULL; |
| 160 | Py_BEGIN_ALLOW_THREADS |
| 161 | res = GetOverlappedResult(self->handle, &self->overlapped, &transferred, |
| 162 | wait != 0); |
| 163 | Py_END_ALLOW_THREADS |
| 164 | |
| 165 | err = res ? ERROR_SUCCESS : GetLastError(); |
| 166 | switch (err) { |
| 167 | case ERROR_SUCCESS: |
| 168 | case ERROR_MORE_DATA: |
| 169 | case ERROR_OPERATION_ABORTED: |
| 170 | self->completed = 1; |
| 171 | self->pending = 0; |
| 172 | break; |
| 173 | case ERROR_IO_INCOMPLETE: |
| 174 | break; |
| 175 | default: |
| 176 | self->pending = 0; |
| 177 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, err); |
| 178 | } |
| 179 | if (self->completed && self->read_buffer != NULL) { |
| 180 | assert(PyBytes_CheckExact(self->read_buffer)); |
| 181 | if (transferred != PyBytes_GET_SIZE(self->read_buffer) && |
| 182 | _PyBytes_Resize(&self->read_buffer, transferred)) |
| 183 | return NULL; |
| 184 | } |
| 185 | return Py_BuildValue("II", (unsigned) transferred, (unsigned) err); |
| 186 | } |
| 187 | |
| 188 | static PyObject * |
| 189 | overlapped_getbuffer(OverlappedObject *self) |
| 190 | { |
| 191 | PyObject *res; |
| 192 | if (!self->completed) { |
| 193 | PyErr_SetString(PyExc_ValueError, |
| 194 | "can't get read buffer before GetOverlappedResult() " |
| 195 | "signals the operation completed"); |
| 196 | return NULL; |
| 197 | } |
| 198 | res = self->read_buffer ? self->read_buffer : Py_None; |
| 199 | Py_INCREF(res); |
| 200 | return res; |
| 201 | } |
| 202 | |
| 203 | static PyObject * |
| 204 | overlapped_cancel(OverlappedObject *self) |
| 205 | { |
| 206 | BOOL res = TRUE; |
| 207 | |
| 208 | if (self->pending) { |
| 209 | Py_BEGIN_ALLOW_THREADS |
| 210 | if (check_CancelIoEx()) |
| 211 | res = Py_CancelIoEx(self->handle, &self->overlapped); |
| 212 | else |
| 213 | res = CancelIo(self->handle); |
| 214 | Py_END_ALLOW_THREADS |
| 215 | } |
| 216 | |
| 217 | /* CancelIoEx returns ERROR_NOT_FOUND if the I/O completed in-between */ |
| 218 | if (!res && GetLastError() != ERROR_NOT_FOUND) |
| 219 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
| 220 | self->pending = 0; |
| 221 | Py_RETURN_NONE; |
| 222 | } |
| 223 | |
| 224 | static PyMethodDef overlapped_methods[] = { |
| 225 | {"GetOverlappedResult", (PyCFunction) overlapped_GetOverlappedResult, |
| 226 | METH_O, NULL}, |
| 227 | {"getbuffer", (PyCFunction) overlapped_getbuffer, METH_NOARGS, NULL}, |
| 228 | {"cancel", (PyCFunction) overlapped_cancel, METH_NOARGS, NULL}, |
| 229 | {NULL} |
| 230 | }; |
| 231 | |
| 232 | static PyMemberDef overlapped_members[] = { |
| 233 | {"event", T_HANDLE, |
| 234 | offsetof(OverlappedObject, overlapped) + offsetof(OVERLAPPED, hEvent), |
| 235 | READONLY, "overlapped event handle"}, |
| 236 | {NULL} |
| 237 | }; |
| 238 | |
| 239 | PyTypeObject OverlappedType = { |
| 240 | PyVarObject_HEAD_INIT(NULL, 0) |
| 241 | /* tp_name */ "_winapi.Overlapped", |
| 242 | /* tp_basicsize */ sizeof(OverlappedObject), |
| 243 | /* tp_itemsize */ 0, |
| 244 | /* tp_dealloc */ (destructor) overlapped_dealloc, |
| 245 | /* tp_print */ 0, |
| 246 | /* tp_getattr */ 0, |
| 247 | /* tp_setattr */ 0, |
| 248 | /* tp_reserved */ 0, |
| 249 | /* tp_repr */ 0, |
| 250 | /* tp_as_number */ 0, |
| 251 | /* tp_as_sequence */ 0, |
| 252 | /* tp_as_mapping */ 0, |
| 253 | /* tp_hash */ 0, |
| 254 | /* tp_call */ 0, |
| 255 | /* tp_str */ 0, |
| 256 | /* tp_getattro */ 0, |
| 257 | /* tp_setattro */ 0, |
| 258 | /* tp_as_buffer */ 0, |
| 259 | /* tp_flags */ Py_TPFLAGS_DEFAULT, |
| 260 | /* tp_doc */ "OVERLAPPED structure wrapper", |
| 261 | /* tp_traverse */ 0, |
| 262 | /* tp_clear */ 0, |
| 263 | /* tp_richcompare */ 0, |
| 264 | /* tp_weaklistoffset */ 0, |
| 265 | /* tp_iter */ 0, |
| 266 | /* tp_iternext */ 0, |
| 267 | /* tp_methods */ overlapped_methods, |
| 268 | /* tp_members */ overlapped_members, |
| 269 | /* tp_getset */ 0, |
| 270 | /* tp_base */ 0, |
| 271 | /* tp_dict */ 0, |
| 272 | /* tp_descr_get */ 0, |
| 273 | /* tp_descr_set */ 0, |
| 274 | /* tp_dictoffset */ 0, |
| 275 | /* tp_init */ 0, |
| 276 | /* tp_alloc */ 0, |
| 277 | /* tp_new */ 0, |
| 278 | }; |
| 279 | |
| 280 | static OverlappedObject * |
| 281 | new_overlapped(HANDLE handle) |
| 282 | { |
| 283 | OverlappedObject *self; |
| 284 | |
| 285 | self = PyObject_New(OverlappedObject, &OverlappedType); |
| 286 | if (!self) |
| 287 | return NULL; |
| 288 | self->handle = handle; |
| 289 | self->read_buffer = NULL; |
| 290 | self->pending = 0; |
| 291 | self->completed = 0; |
| 292 | memset(&self->overlapped, 0, sizeof(OVERLAPPED)); |
| 293 | memset(&self->write_buffer, 0, sizeof(Py_buffer)); |
| 294 | /* Manual reset, initially non-signalled */ |
| 295 | self->overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 296 | return self; |
| 297 | } |
| 298 | |
| 299 | /* -------------------------------------------------------------------- */ |
| 300 | /* windows API functions */ |
| 301 | |
| 302 | PyDoc_STRVAR(CloseHandle_doc, |
| 303 | "CloseHandle(handle) -> None\n\ |
| 304 | \n\ |
| 305 | Close handle."); |
| 306 | |
| 307 | static PyObject * |
| 308 | winapi_CloseHandle(PyObject *self, PyObject *args) |
| 309 | { |
| 310 | HANDLE hObject; |
| 311 | BOOL success; |
| 312 | |
| 313 | if (!PyArg_ParseTuple(args, F_HANDLE ":CloseHandle", &hObject)) |
| 314 | return NULL; |
| 315 | |
| 316 | Py_BEGIN_ALLOW_THREADS |
| 317 | success = CloseHandle(hObject); |
| 318 | Py_END_ALLOW_THREADS |
| 319 | |
| 320 | if (!success) |
| 321 | return PyErr_SetFromWindowsErr(0); |
| 322 | |
| 323 | Py_RETURN_NONE; |
| 324 | } |
| 325 | |
| 326 | static PyObject * |
| 327 | winapi_ConnectNamedPipe(PyObject *self, PyObject *args, PyObject *kwds) |
| 328 | { |
| 329 | HANDLE hNamedPipe; |
| 330 | int use_overlapped = 0; |
| 331 | BOOL success; |
| 332 | OverlappedObject *overlapped = NULL; |
| 333 | static char *kwlist[] = {"handle", "overlapped", NULL}; |
| 334 | |
| 335 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 336 | F_HANDLE "|" F_BOOL, kwlist, |
| 337 | &hNamedPipe, &use_overlapped)) |
| 338 | return NULL; |
| 339 | |
| 340 | if (use_overlapped) { |
| 341 | overlapped = new_overlapped(hNamedPipe); |
| 342 | if (!overlapped) |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | Py_BEGIN_ALLOW_THREADS |
| 347 | success = ConnectNamedPipe(hNamedPipe, |
| 348 | overlapped ? &overlapped->overlapped : NULL); |
| 349 | Py_END_ALLOW_THREADS |
| 350 | |
| 351 | if (overlapped) { |
| 352 | int err = GetLastError(); |
| 353 | /* Overlapped ConnectNamedPipe never returns a success code */ |
| 354 | assert(success == 0); |
| 355 | if (err == ERROR_IO_PENDING) |
| 356 | overlapped->pending = 1; |
| 357 | else if (err == ERROR_PIPE_CONNECTED) |
| 358 | SetEvent(overlapped->overlapped.hEvent); |
| 359 | else { |
| 360 | Py_DECREF(overlapped); |
| 361 | return PyErr_SetFromWindowsErr(err); |
| 362 | } |
| 363 | return (PyObject *) overlapped; |
| 364 | } |
| 365 | if (!success) |
| 366 | return PyErr_SetFromWindowsErr(0); |
| 367 | |
| 368 | Py_RETURN_NONE; |
| 369 | } |
| 370 | |
| 371 | static PyObject * |
| 372 | winapi_CreateFile(PyObject *self, PyObject *args) |
| 373 | { |
| 374 | LPCTSTR lpFileName; |
| 375 | DWORD dwDesiredAccess; |
| 376 | DWORD dwShareMode; |
| 377 | LPSECURITY_ATTRIBUTES lpSecurityAttributes; |
| 378 | DWORD dwCreationDisposition; |
| 379 | DWORD dwFlagsAndAttributes; |
| 380 | HANDLE hTemplateFile; |
| 381 | HANDLE handle; |
| 382 | |
| 383 | if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_POINTER |
| 384 | F_DWORD F_DWORD F_HANDLE, |
| 385 | &lpFileName, &dwDesiredAccess, &dwShareMode, |
| 386 | &lpSecurityAttributes, &dwCreationDisposition, |
| 387 | &dwFlagsAndAttributes, &hTemplateFile)) |
| 388 | return NULL; |
| 389 | |
| 390 | Py_BEGIN_ALLOW_THREADS |
| 391 | handle = CreateFile(lpFileName, dwDesiredAccess, |
| 392 | dwShareMode, lpSecurityAttributes, |
| 393 | dwCreationDisposition, |
| 394 | dwFlagsAndAttributes, hTemplateFile); |
| 395 | Py_END_ALLOW_THREADS |
| 396 | |
| 397 | if (handle == INVALID_HANDLE_VALUE) |
| 398 | return PyErr_SetFromWindowsErr(0); |
| 399 | |
| 400 | return Py_BuildValue(F_HANDLE, handle); |
| 401 | } |
| 402 | |
| 403 | static PyObject * |
| 404 | winapi_CreateNamedPipe(PyObject *self, PyObject *args) |
| 405 | { |
| 406 | LPCTSTR lpName; |
| 407 | DWORD dwOpenMode; |
| 408 | DWORD dwPipeMode; |
| 409 | DWORD nMaxInstances; |
| 410 | DWORD nOutBufferSize; |
| 411 | DWORD nInBufferSize; |
| 412 | DWORD nDefaultTimeOut; |
| 413 | LPSECURITY_ATTRIBUTES lpSecurityAttributes; |
| 414 | HANDLE handle; |
| 415 | |
| 416 | if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_DWORD |
| 417 | F_DWORD F_DWORD F_DWORD F_POINTER, |
| 418 | &lpName, &dwOpenMode, &dwPipeMode, |
| 419 | &nMaxInstances, &nOutBufferSize, |
| 420 | &nInBufferSize, &nDefaultTimeOut, |
| 421 | &lpSecurityAttributes)) |
| 422 | return NULL; |
| 423 | |
| 424 | Py_BEGIN_ALLOW_THREADS |
| 425 | handle = CreateNamedPipe(lpName, dwOpenMode, dwPipeMode, |
| 426 | nMaxInstances, nOutBufferSize, |
| 427 | nInBufferSize, nDefaultTimeOut, |
| 428 | lpSecurityAttributes); |
| 429 | Py_END_ALLOW_THREADS |
| 430 | |
| 431 | if (handle == INVALID_HANDLE_VALUE) |
| 432 | return PyErr_SetFromWindowsErr(0); |
| 433 | |
| 434 | return Py_BuildValue(F_HANDLE, handle); |
| 435 | } |
| 436 | |
| 437 | PyDoc_STRVAR(CreatePipe_doc, |
| 438 | "CreatePipe(pipe_attrs, size) -> (read_handle, write_handle)\n\ |
| 439 | \n\ |
| 440 | Create an anonymous pipe, and return handles to the read and\n\ |
| 441 | write ends of the pipe.\n\ |
| 442 | \n\ |
| 443 | pipe_attrs is ignored internally and can be None."); |
| 444 | |
| 445 | static PyObject * |
| 446 | winapi_CreatePipe(PyObject* self, PyObject* args) |
| 447 | { |
| 448 | HANDLE read_pipe; |
| 449 | HANDLE write_pipe; |
| 450 | BOOL result; |
| 451 | |
| 452 | PyObject* pipe_attributes; /* ignored */ |
| 453 | DWORD size; |
| 454 | |
| 455 | if (! PyArg_ParseTuple(args, "O" F_DWORD ":CreatePipe", |
| 456 | &pipe_attributes, &size)) |
| 457 | return NULL; |
| 458 | |
| 459 | Py_BEGIN_ALLOW_THREADS |
| 460 | result = CreatePipe(&read_pipe, &write_pipe, NULL, size); |
| 461 | Py_END_ALLOW_THREADS |
| 462 | |
| 463 | if (! result) |
| 464 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 465 | |
| 466 | return Py_BuildValue( |
| 467 | "NN", HANDLE_TO_PYNUM(read_pipe), HANDLE_TO_PYNUM(write_pipe)); |
| 468 | } |
| 469 | |
| 470 | /* helpers for createprocess */ |
| 471 | |
| 472 | static unsigned long |
| 473 | getulong(PyObject* obj, char* name) |
| 474 | { |
| 475 | PyObject* value; |
| 476 | unsigned long ret; |
| 477 | |
| 478 | value = PyObject_GetAttrString(obj, name); |
| 479 | if (! value) { |
| 480 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 481 | return 0; |
| 482 | } |
| 483 | ret = PyLong_AsUnsignedLong(value); |
| 484 | Py_DECREF(value); |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | static HANDLE |
| 489 | gethandle(PyObject* obj, char* name) |
| 490 | { |
| 491 | PyObject* value; |
| 492 | HANDLE ret; |
| 493 | |
| 494 | value = PyObject_GetAttrString(obj, name); |
| 495 | if (! value) { |
| 496 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 497 | return NULL; |
| 498 | } |
| 499 | if (value == Py_None) |
| 500 | ret = NULL; |
| 501 | else |
| 502 | ret = PYNUM_TO_HANDLE(value); |
| 503 | Py_DECREF(value); |
| 504 | return ret; |
| 505 | } |
| 506 | |
| 507 | static PyObject* |
| 508 | getenvironment(PyObject* environment) |
| 509 | { |
| 510 | Py_ssize_t i, envsize, totalsize; |
| 511 | Py_UCS4 *buffer = NULL, *p, *end; |
| 512 | PyObject *keys, *values, *res; |
| 513 | |
Ezio Melotti | 85a8629 | 2013-08-17 16:57:41 +0300 | [diff] [blame] | 514 | /* convert environment dictionary to windows environment string */ |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 515 | if (! PyMapping_Check(environment)) { |
| 516 | PyErr_SetString( |
| 517 | PyExc_TypeError, "environment must be dictionary or None"); |
| 518 | return NULL; |
| 519 | } |
| 520 | |
| 521 | envsize = PyMapping_Length(environment); |
| 522 | |
| 523 | keys = PyMapping_Keys(environment); |
| 524 | values = PyMapping_Values(environment); |
| 525 | if (!keys || !values) |
| 526 | goto error; |
| 527 | |
| 528 | totalsize = 1; /* trailing null character */ |
| 529 | for (i = 0; i < envsize; i++) { |
| 530 | PyObject* key = PyList_GET_ITEM(keys, i); |
| 531 | PyObject* value = PyList_GET_ITEM(values, i); |
| 532 | |
| 533 | if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) { |
| 534 | PyErr_SetString(PyExc_TypeError, |
| 535 | "environment can only contain strings"); |
| 536 | goto error; |
| 537 | } |
| 538 | totalsize += PyUnicode_GET_LENGTH(key) + 1; /* +1 for '=' */ |
| 539 | totalsize += PyUnicode_GET_LENGTH(value) + 1; /* +1 for '\0' */ |
| 540 | } |
| 541 | |
| 542 | buffer = PyMem_Malloc(totalsize * sizeof(Py_UCS4)); |
| 543 | if (! buffer) |
| 544 | goto error; |
| 545 | p = buffer; |
| 546 | end = buffer + totalsize; |
| 547 | |
| 548 | for (i = 0; i < envsize; i++) { |
| 549 | PyObject* key = PyList_GET_ITEM(keys, i); |
| 550 | PyObject* value = PyList_GET_ITEM(values, i); |
| 551 | if (!PyUnicode_AsUCS4(key, p, end - p, 0)) |
| 552 | goto error; |
| 553 | p += PyUnicode_GET_LENGTH(key); |
| 554 | *p++ = '='; |
| 555 | if (!PyUnicode_AsUCS4(value, p, end - p, 0)) |
| 556 | goto error; |
| 557 | p += PyUnicode_GET_LENGTH(value); |
| 558 | *p++ = '\0'; |
| 559 | } |
| 560 | |
| 561 | /* add trailing null byte */ |
| 562 | *p++ = '\0'; |
| 563 | assert(p == end); |
| 564 | |
| 565 | Py_XDECREF(keys); |
| 566 | Py_XDECREF(values); |
| 567 | |
| 568 | res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, p - buffer); |
| 569 | PyMem_Free(buffer); |
| 570 | return res; |
| 571 | |
| 572 | error: |
| 573 | PyMem_Free(buffer); |
| 574 | Py_XDECREF(keys); |
| 575 | Py_XDECREF(values); |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | PyDoc_STRVAR(CreateProcess_doc, |
| 580 | "CreateProcess(app_name, cmd_line, proc_attrs, thread_attrs,\n\ |
| 581 | inherit, flags, env_mapping, curdir,\n\ |
| 582 | startup_info) -> (proc_handle, thread_handle,\n\ |
| 583 | pid, tid)\n\ |
| 584 | \n\ |
| 585 | Create a new process and its primary thread. The return\n\ |
| 586 | value is a tuple of the process handle, thread handle,\n\ |
| 587 | process ID, and thread ID.\n\ |
| 588 | \n\ |
| 589 | proc_attrs and thread_attrs are ignored internally and can be None."); |
| 590 | |
| 591 | static PyObject * |
| 592 | winapi_CreateProcess(PyObject* self, PyObject* args) |
| 593 | { |
| 594 | BOOL result; |
| 595 | PROCESS_INFORMATION pi; |
| 596 | STARTUPINFOW si; |
| 597 | PyObject* environment; |
| 598 | wchar_t *wenvironment; |
| 599 | |
| 600 | wchar_t* application_name; |
| 601 | wchar_t* command_line; |
| 602 | PyObject* process_attributes; /* ignored */ |
| 603 | PyObject* thread_attributes; /* ignored */ |
| 604 | BOOL inherit_handles; |
| 605 | DWORD creation_flags; |
| 606 | PyObject* env_mapping; |
| 607 | wchar_t* current_directory; |
| 608 | PyObject* startup_info; |
| 609 | |
| 610 | if (! PyArg_ParseTuple(args, "ZZOO" F_BOOL F_DWORD "OZO:CreateProcess", |
| 611 | &application_name, |
| 612 | &command_line, |
| 613 | &process_attributes, |
| 614 | &thread_attributes, |
| 615 | &inherit_handles, |
| 616 | &creation_flags, |
| 617 | &env_mapping, |
| 618 | ¤t_directory, |
| 619 | &startup_info)) |
| 620 | return NULL; |
| 621 | |
| 622 | ZeroMemory(&si, sizeof(si)); |
| 623 | si.cb = sizeof(si); |
| 624 | |
| 625 | /* note: we only support a small subset of all SI attributes */ |
| 626 | si.dwFlags = getulong(startup_info, "dwFlags"); |
| 627 | si.wShowWindow = (WORD)getulong(startup_info, "wShowWindow"); |
| 628 | si.hStdInput = gethandle(startup_info, "hStdInput"); |
| 629 | si.hStdOutput = gethandle(startup_info, "hStdOutput"); |
| 630 | si.hStdError = gethandle(startup_info, "hStdError"); |
| 631 | if (PyErr_Occurred()) |
| 632 | return NULL; |
| 633 | |
| 634 | if (env_mapping != Py_None) { |
| 635 | environment = getenvironment(env_mapping); |
| 636 | if (! environment) |
| 637 | return NULL; |
| 638 | wenvironment = PyUnicode_AsUnicode(environment); |
| 639 | if (wenvironment == NULL) |
| 640 | { |
| 641 | Py_XDECREF(environment); |
| 642 | return NULL; |
| 643 | } |
| 644 | } |
| 645 | else { |
| 646 | environment = NULL; |
| 647 | wenvironment = NULL; |
| 648 | } |
| 649 | |
| 650 | Py_BEGIN_ALLOW_THREADS |
| 651 | result = CreateProcessW(application_name, |
| 652 | command_line, |
| 653 | NULL, |
| 654 | NULL, |
| 655 | inherit_handles, |
| 656 | creation_flags | CREATE_UNICODE_ENVIRONMENT, |
| 657 | wenvironment, |
| 658 | current_directory, |
| 659 | &si, |
| 660 | &pi); |
| 661 | Py_END_ALLOW_THREADS |
| 662 | |
| 663 | Py_XDECREF(environment); |
| 664 | |
| 665 | if (! result) |
| 666 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 667 | |
| 668 | return Py_BuildValue("NNkk", |
| 669 | HANDLE_TO_PYNUM(pi.hProcess), |
| 670 | HANDLE_TO_PYNUM(pi.hThread), |
| 671 | pi.dwProcessId, |
| 672 | pi.dwThreadId); |
| 673 | } |
| 674 | |
| 675 | PyDoc_STRVAR(DuplicateHandle_doc, |
| 676 | "DuplicateHandle(source_proc_handle, source_handle,\n\ |
| 677 | target_proc_handle, target_handle, access,\n\ |
| 678 | inherit[, options]) -> handle\n\ |
| 679 | \n\ |
| 680 | Return a duplicate handle object.\n\ |
| 681 | \n\ |
| 682 | The duplicate handle refers to the same object as the original\n\ |
| 683 | handle. Therefore, any changes to the object are reflected\n\ |
| 684 | through both handles."); |
| 685 | |
| 686 | static PyObject * |
| 687 | winapi_DuplicateHandle(PyObject* self, PyObject* args) |
| 688 | { |
| 689 | HANDLE target_handle; |
| 690 | BOOL result; |
| 691 | |
| 692 | HANDLE source_process_handle; |
| 693 | HANDLE source_handle; |
| 694 | HANDLE target_process_handle; |
| 695 | DWORD desired_access; |
| 696 | BOOL inherit_handle; |
| 697 | DWORD options = 0; |
| 698 | |
| 699 | if (! PyArg_ParseTuple(args, |
| 700 | F_HANDLE F_HANDLE F_HANDLE F_DWORD F_BOOL F_DWORD |
| 701 | ":DuplicateHandle", |
| 702 | &source_process_handle, |
| 703 | &source_handle, |
| 704 | &target_process_handle, |
| 705 | &desired_access, |
| 706 | &inherit_handle, |
| 707 | &options)) |
| 708 | return NULL; |
| 709 | |
| 710 | Py_BEGIN_ALLOW_THREADS |
| 711 | result = DuplicateHandle( |
| 712 | source_process_handle, |
| 713 | source_handle, |
| 714 | target_process_handle, |
| 715 | &target_handle, |
| 716 | desired_access, |
| 717 | inherit_handle, |
| 718 | options |
| 719 | ); |
| 720 | Py_END_ALLOW_THREADS |
| 721 | |
| 722 | if (! result) |
| 723 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 724 | |
| 725 | return HANDLE_TO_PYNUM(target_handle); |
| 726 | } |
| 727 | |
| 728 | static PyObject * |
| 729 | winapi_ExitProcess(PyObject *self, PyObject *args) |
| 730 | { |
| 731 | UINT uExitCode; |
| 732 | |
| 733 | if (!PyArg_ParseTuple(args, F_UINT, &uExitCode)) |
| 734 | return NULL; |
| 735 | |
| 736 | #if defined(Py_DEBUG) |
| 737 | SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT| |
| 738 | SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX); |
| 739 | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
| 740 | #endif |
| 741 | |
| 742 | ExitProcess(uExitCode); |
| 743 | |
| 744 | return NULL; |
| 745 | } |
| 746 | |
| 747 | PyDoc_STRVAR(GetCurrentProcess_doc, |
| 748 | "GetCurrentProcess() -> handle\n\ |
| 749 | \n\ |
| 750 | Return a handle object for the current process."); |
| 751 | |
| 752 | static PyObject * |
| 753 | winapi_GetCurrentProcess(PyObject* self, PyObject* args) |
| 754 | { |
| 755 | if (! PyArg_ParseTuple(args, ":GetCurrentProcess")) |
| 756 | return NULL; |
| 757 | |
| 758 | return HANDLE_TO_PYNUM(GetCurrentProcess()); |
| 759 | } |
| 760 | |
| 761 | PyDoc_STRVAR(GetExitCodeProcess_doc, |
| 762 | "GetExitCodeProcess(handle) -> Exit code\n\ |
| 763 | \n\ |
| 764 | Return the termination status of the specified process."); |
| 765 | |
| 766 | static PyObject * |
| 767 | winapi_GetExitCodeProcess(PyObject* self, PyObject* args) |
| 768 | { |
| 769 | DWORD exit_code; |
| 770 | BOOL result; |
| 771 | |
| 772 | HANDLE process; |
| 773 | if (! PyArg_ParseTuple(args, F_HANDLE ":GetExitCodeProcess", &process)) |
| 774 | return NULL; |
| 775 | |
| 776 | result = GetExitCodeProcess(process, &exit_code); |
| 777 | |
| 778 | if (! result) |
| 779 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 780 | |
| 781 | return PyLong_FromUnsignedLong(exit_code); |
| 782 | } |
| 783 | |
| 784 | static PyObject * |
| 785 | winapi_GetLastError(PyObject *self, PyObject *args) |
| 786 | { |
| 787 | return Py_BuildValue(F_DWORD, GetLastError()); |
| 788 | } |
| 789 | |
| 790 | PyDoc_STRVAR(GetModuleFileName_doc, |
| 791 | "GetModuleFileName(module) -> path\n\ |
| 792 | \n\ |
| 793 | Return the fully-qualified path for the file that contains\n\ |
| 794 | the specified module. The module must have been loaded by the\n\ |
| 795 | current process.\n\ |
| 796 | \n\ |
| 797 | The module parameter should be a handle to the loaded module\n\ |
| 798 | whose path is being requested. If this parameter is 0, \n\ |
| 799 | GetModuleFileName retrieves the path of the executable file\n\ |
| 800 | of the current process."); |
| 801 | |
| 802 | static PyObject * |
| 803 | winapi_GetModuleFileName(PyObject* self, PyObject* args) |
| 804 | { |
| 805 | BOOL result; |
| 806 | HMODULE module; |
| 807 | WCHAR filename[MAX_PATH]; |
| 808 | |
| 809 | if (! PyArg_ParseTuple(args, F_HANDLE ":GetModuleFileName", |
| 810 | &module)) |
| 811 | return NULL; |
| 812 | |
| 813 | result = GetModuleFileNameW(module, filename, MAX_PATH); |
| 814 | filename[MAX_PATH-1] = '\0'; |
| 815 | |
| 816 | if (! result) |
| 817 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 818 | |
| 819 | return PyUnicode_FromWideChar(filename, wcslen(filename)); |
| 820 | } |
| 821 | |
| 822 | PyDoc_STRVAR(GetStdHandle_doc, |
| 823 | "GetStdHandle(handle) -> integer\n\ |
| 824 | \n\ |
| 825 | Return a handle to the specified standard device\n\ |
| 826 | (STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE).\n\ |
| 827 | The integer associated with the handle object is returned."); |
| 828 | |
| 829 | static PyObject * |
| 830 | winapi_GetStdHandle(PyObject* self, PyObject* args) |
| 831 | { |
| 832 | HANDLE handle; |
| 833 | DWORD std_handle; |
| 834 | |
| 835 | if (! PyArg_ParseTuple(args, F_DWORD ":GetStdHandle", &std_handle)) |
| 836 | return NULL; |
| 837 | |
| 838 | Py_BEGIN_ALLOW_THREADS |
| 839 | handle = GetStdHandle(std_handle); |
| 840 | Py_END_ALLOW_THREADS |
| 841 | |
| 842 | if (handle == INVALID_HANDLE_VALUE) |
| 843 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 844 | |
| 845 | if (! handle) { |
| 846 | Py_INCREF(Py_None); |
| 847 | return Py_None; |
| 848 | } |
| 849 | |
| 850 | /* note: returns integer, not handle object */ |
| 851 | return HANDLE_TO_PYNUM(handle); |
| 852 | } |
| 853 | |
| 854 | PyDoc_STRVAR(GetVersion_doc, |
| 855 | "GetVersion() -> version\n\ |
| 856 | \n\ |
| 857 | Return the version number of the current operating system."); |
| 858 | |
| 859 | static PyObject * |
| 860 | winapi_GetVersion(PyObject* self, PyObject* args) |
| 861 | { |
| 862 | if (! PyArg_ParseTuple(args, ":GetVersion")) |
| 863 | return NULL; |
| 864 | |
| 865 | return PyLong_FromUnsignedLong(GetVersion()); |
| 866 | } |
| 867 | |
| 868 | static PyObject * |
| 869 | winapi_OpenProcess(PyObject *self, PyObject *args) |
| 870 | { |
| 871 | DWORD dwDesiredAccess; |
| 872 | BOOL bInheritHandle; |
| 873 | DWORD dwProcessId; |
| 874 | HANDLE handle; |
| 875 | |
| 876 | if (!PyArg_ParseTuple(args, F_DWORD F_BOOL F_DWORD, |
| 877 | &dwDesiredAccess, &bInheritHandle, &dwProcessId)) |
| 878 | return NULL; |
| 879 | |
| 880 | handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); |
| 881 | if (handle == NULL) |
| 882 | return PyErr_SetFromWindowsErr(0); |
| 883 | |
| 884 | return Py_BuildValue(F_HANDLE, handle); |
| 885 | } |
| 886 | |
| 887 | static PyObject * |
| 888 | winapi_PeekNamedPipe(PyObject *self, PyObject *args) |
| 889 | { |
| 890 | HANDLE handle; |
| 891 | int size = 0; |
| 892 | PyObject *buf = NULL; |
| 893 | DWORD nread, navail, nleft; |
| 894 | BOOL ret; |
| 895 | |
| 896 | if (!PyArg_ParseTuple(args, F_HANDLE "|i:PeekNamedPipe" , &handle, &size)) |
| 897 | return NULL; |
| 898 | |
| 899 | if (size < 0) { |
| 900 | PyErr_SetString(PyExc_ValueError, "negative size"); |
| 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | if (size) { |
| 905 | buf = PyBytes_FromStringAndSize(NULL, size); |
| 906 | if (!buf) |
| 907 | return NULL; |
| 908 | Py_BEGIN_ALLOW_THREADS |
| 909 | ret = PeekNamedPipe(handle, PyBytes_AS_STRING(buf), size, &nread, |
| 910 | &navail, &nleft); |
| 911 | Py_END_ALLOW_THREADS |
| 912 | if (!ret) { |
| 913 | Py_DECREF(buf); |
| 914 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
| 915 | } |
| 916 | if (_PyBytes_Resize(&buf, nread)) |
| 917 | return NULL; |
| 918 | return Py_BuildValue("Nii", buf, navail, nleft); |
| 919 | } |
| 920 | else { |
| 921 | Py_BEGIN_ALLOW_THREADS |
| 922 | ret = PeekNamedPipe(handle, NULL, 0, NULL, &navail, &nleft); |
| 923 | Py_END_ALLOW_THREADS |
| 924 | if (!ret) { |
| 925 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
| 926 | } |
| 927 | return Py_BuildValue("ii", navail, nleft); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | static PyObject * |
| 932 | winapi_ReadFile(PyObject *self, PyObject *args, PyObject *kwds) |
| 933 | { |
| 934 | HANDLE handle; |
| 935 | int size; |
| 936 | DWORD nread; |
| 937 | PyObject *buf; |
| 938 | BOOL ret; |
| 939 | int use_overlapped = 0; |
| 940 | DWORD err; |
| 941 | OverlappedObject *overlapped = NULL; |
| 942 | static char *kwlist[] = {"handle", "size", "overlapped", NULL}; |
| 943 | |
| 944 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 945 | F_HANDLE "i|i:ReadFile", kwlist, |
| 946 | &handle, &size, &use_overlapped)) |
| 947 | return NULL; |
| 948 | |
| 949 | buf = PyBytes_FromStringAndSize(NULL, size); |
| 950 | if (!buf) |
| 951 | return NULL; |
| 952 | if (use_overlapped) { |
| 953 | overlapped = new_overlapped(handle); |
| 954 | if (!overlapped) { |
| 955 | Py_DECREF(buf); |
| 956 | return NULL; |
| 957 | } |
| 958 | /* Steals reference to buf */ |
| 959 | overlapped->read_buffer = buf; |
| 960 | } |
| 961 | |
| 962 | Py_BEGIN_ALLOW_THREADS |
| 963 | ret = ReadFile(handle, PyBytes_AS_STRING(buf), size, &nread, |
| 964 | overlapped ? &overlapped->overlapped : NULL); |
| 965 | Py_END_ALLOW_THREADS |
| 966 | |
| 967 | err = ret ? 0 : GetLastError(); |
| 968 | |
| 969 | if (overlapped) { |
| 970 | if (!ret) { |
| 971 | if (err == ERROR_IO_PENDING) |
| 972 | overlapped->pending = 1; |
| 973 | else if (err != ERROR_MORE_DATA) { |
| 974 | Py_DECREF(overlapped); |
| 975 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
| 976 | } |
| 977 | } |
| 978 | return Py_BuildValue("NI", (PyObject *) overlapped, err); |
| 979 | } |
| 980 | |
| 981 | if (!ret && err != ERROR_MORE_DATA) { |
| 982 | Py_DECREF(buf); |
| 983 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
| 984 | } |
| 985 | if (_PyBytes_Resize(&buf, nread)) |
| 986 | return NULL; |
| 987 | return Py_BuildValue("NI", buf, err); |
| 988 | } |
| 989 | |
| 990 | static PyObject * |
| 991 | winapi_SetNamedPipeHandleState(PyObject *self, PyObject *args) |
| 992 | { |
| 993 | HANDLE hNamedPipe; |
| 994 | PyObject *oArgs[3]; |
| 995 | DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL}; |
| 996 | int i; |
| 997 | |
| 998 | if (!PyArg_ParseTuple(args, F_HANDLE "OOO", |
| 999 | &hNamedPipe, &oArgs[0], &oArgs[1], &oArgs[2])) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | PyErr_Clear(); |
| 1003 | |
| 1004 | for (i = 0 ; i < 3 ; i++) { |
| 1005 | if (oArgs[i] != Py_None) { |
| 1006 | dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]); |
| 1007 | if (PyErr_Occurred()) |
| 1008 | return NULL; |
| 1009 | pArgs[i] = &dwArgs[i]; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2])) |
| 1014 | return PyErr_SetFromWindowsErr(0); |
| 1015 | |
| 1016 | Py_RETURN_NONE; |
| 1017 | } |
| 1018 | |
| 1019 | PyDoc_STRVAR(TerminateProcess_doc, |
| 1020 | "TerminateProcess(handle, exit_code) -> None\n\ |
| 1021 | \n\ |
| 1022 | Terminate the specified process and all of its threads."); |
| 1023 | |
| 1024 | static PyObject * |
| 1025 | winapi_TerminateProcess(PyObject* self, PyObject* args) |
| 1026 | { |
| 1027 | BOOL result; |
| 1028 | |
| 1029 | HANDLE process; |
| 1030 | UINT exit_code; |
| 1031 | if (! PyArg_ParseTuple(args, F_HANDLE F_UINT ":TerminateProcess", |
| 1032 | &process, &exit_code)) |
| 1033 | return NULL; |
| 1034 | |
| 1035 | result = TerminateProcess(process, exit_code); |
| 1036 | |
| 1037 | if (! result) |
| 1038 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 1039 | |
| 1040 | Py_INCREF(Py_None); |
| 1041 | return Py_None; |
| 1042 | } |
| 1043 | |
| 1044 | static PyObject * |
| 1045 | winapi_WaitNamedPipe(PyObject *self, PyObject *args) |
| 1046 | { |
| 1047 | LPCTSTR lpNamedPipeName; |
| 1048 | DWORD nTimeOut; |
| 1049 | BOOL success; |
| 1050 | |
| 1051 | if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut)) |
| 1052 | return NULL; |
| 1053 | |
| 1054 | Py_BEGIN_ALLOW_THREADS |
| 1055 | success = WaitNamedPipe(lpNamedPipeName, nTimeOut); |
| 1056 | Py_END_ALLOW_THREADS |
| 1057 | |
| 1058 | if (!success) |
| 1059 | return PyErr_SetFromWindowsErr(0); |
| 1060 | |
| 1061 | Py_RETURN_NONE; |
| 1062 | } |
| 1063 | |
| 1064 | static PyObject * |
| 1065 | winapi_WaitForMultipleObjects(PyObject* self, PyObject* args) |
| 1066 | { |
| 1067 | DWORD result; |
| 1068 | PyObject *handle_seq; |
| 1069 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 1070 | HANDLE sigint_event = NULL; |
| 1071 | Py_ssize_t nhandles, i; |
| 1072 | BOOL wait_flag; |
| 1073 | DWORD milliseconds = INFINITE; |
| 1074 | |
| 1075 | if (!PyArg_ParseTuple(args, "O" F_BOOL "|" F_DWORD |
| 1076 | ":WaitForMultipleObjects", |
| 1077 | &handle_seq, &wait_flag, &milliseconds)) |
| 1078 | return NULL; |
| 1079 | |
| 1080 | if (!PySequence_Check(handle_seq)) { |
| 1081 | PyErr_Format(PyExc_TypeError, |
| 1082 | "sequence type expected, got '%s'", |
Richard Oudkerk | 6733927 | 2012-08-21 14:54:22 +0100 | [diff] [blame] | 1083 | Py_TYPE(handle_seq)->tp_name); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1084 | return NULL; |
| 1085 | } |
| 1086 | nhandles = PySequence_Length(handle_seq); |
| 1087 | if (nhandles == -1) |
| 1088 | return NULL; |
| 1089 | if (nhandles < 0 || nhandles >= MAXIMUM_WAIT_OBJECTS - 1) { |
| 1090 | PyErr_Format(PyExc_ValueError, |
| 1091 | "need at most %zd handles, got a sequence of length %zd", |
| 1092 | MAXIMUM_WAIT_OBJECTS - 1, nhandles); |
| 1093 | return NULL; |
| 1094 | } |
| 1095 | for (i = 0; i < nhandles; i++) { |
| 1096 | HANDLE h; |
| 1097 | PyObject *v = PySequence_GetItem(handle_seq, i); |
| 1098 | if (v == NULL) |
| 1099 | return NULL; |
| 1100 | if (!PyArg_Parse(v, F_HANDLE, &h)) { |
| 1101 | Py_DECREF(v); |
| 1102 | return NULL; |
| 1103 | } |
| 1104 | handles[i] = h; |
| 1105 | Py_DECREF(v); |
| 1106 | } |
| 1107 | /* If this is the main thread then make the wait interruptible |
| 1108 | by Ctrl-C unless we are waiting for *all* handles */ |
| 1109 | if (!wait_flag && _PyOS_IsMainThread()) { |
| 1110 | sigint_event = _PyOS_SigintEvent(); |
| 1111 | assert(sigint_event != NULL); |
| 1112 | handles[nhandles++] = sigint_event; |
| 1113 | } |
| 1114 | |
| 1115 | Py_BEGIN_ALLOW_THREADS |
| 1116 | if (sigint_event != NULL) |
| 1117 | ResetEvent(sigint_event); |
| 1118 | result = WaitForMultipleObjects((DWORD) nhandles, handles, |
| 1119 | wait_flag, milliseconds); |
| 1120 | Py_END_ALLOW_THREADS |
| 1121 | |
| 1122 | if (result == WAIT_FAILED) |
| 1123 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
| 1124 | else if (sigint_event != NULL && result == WAIT_OBJECT_0 + nhandles - 1) { |
| 1125 | errno = EINTR; |
| 1126 | return PyErr_SetFromErrno(PyExc_IOError); |
| 1127 | } |
| 1128 | |
| 1129 | return PyLong_FromLong((int) result); |
| 1130 | } |
| 1131 | |
| 1132 | PyDoc_STRVAR(WaitForSingleObject_doc, |
| 1133 | "WaitForSingleObject(handle, timeout) -> result\n\ |
| 1134 | \n\ |
| 1135 | Wait until the specified object is in the signaled state or\n\ |
| 1136 | the time-out interval elapses. The timeout value is specified\n\ |
| 1137 | in milliseconds."); |
| 1138 | |
| 1139 | static PyObject * |
| 1140 | winapi_WaitForSingleObject(PyObject* self, PyObject* args) |
| 1141 | { |
| 1142 | DWORD result; |
| 1143 | |
| 1144 | HANDLE handle; |
| 1145 | DWORD milliseconds; |
| 1146 | if (! PyArg_ParseTuple(args, F_HANDLE F_DWORD ":WaitForSingleObject", |
| 1147 | &handle, |
| 1148 | &milliseconds)) |
| 1149 | return NULL; |
| 1150 | |
| 1151 | Py_BEGIN_ALLOW_THREADS |
| 1152 | result = WaitForSingleObject(handle, milliseconds); |
| 1153 | Py_END_ALLOW_THREADS |
| 1154 | |
| 1155 | if (result == WAIT_FAILED) |
| 1156 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 1157 | |
| 1158 | return PyLong_FromUnsignedLong(result); |
| 1159 | } |
| 1160 | |
| 1161 | static PyObject * |
| 1162 | winapi_WriteFile(PyObject *self, PyObject *args, PyObject *kwds) |
| 1163 | { |
| 1164 | HANDLE handle; |
| 1165 | Py_buffer _buf, *buf; |
| 1166 | PyObject *bufobj; |
Victor Stinner | 7176577 | 2013-06-24 23:13:24 +0200 | [diff] [blame] | 1167 | DWORD len, written; |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1168 | BOOL ret; |
| 1169 | int use_overlapped = 0; |
| 1170 | DWORD err; |
| 1171 | OverlappedObject *overlapped = NULL; |
| 1172 | static char *kwlist[] = {"handle", "buffer", "overlapped", NULL}; |
| 1173 | |
| 1174 | /* First get handle and use_overlapped to know which Py_buffer to use */ |
| 1175 | if (!PyArg_ParseTupleAndKeywords(args, kwds, |
| 1176 | F_HANDLE "O|i:WriteFile", kwlist, |
| 1177 | &handle, &bufobj, &use_overlapped)) |
| 1178 | return NULL; |
| 1179 | |
| 1180 | if (use_overlapped) { |
| 1181 | overlapped = new_overlapped(handle); |
| 1182 | if (!overlapped) |
| 1183 | return NULL; |
| 1184 | buf = &overlapped->write_buffer; |
| 1185 | } |
| 1186 | else |
| 1187 | buf = &_buf; |
| 1188 | |
| 1189 | if (!PyArg_Parse(bufobj, "y*", buf)) { |
| 1190 | Py_XDECREF(overlapped); |
| 1191 | return NULL; |
| 1192 | } |
| 1193 | |
| 1194 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7176577 | 2013-06-24 23:13:24 +0200 | [diff] [blame] | 1195 | len = (DWORD)Py_MIN(buf->len, DWORD_MAX); |
| 1196 | ret = WriteFile(handle, buf->buf, len, &written, |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1197 | overlapped ? &overlapped->overlapped : NULL); |
| 1198 | Py_END_ALLOW_THREADS |
| 1199 | |
| 1200 | err = ret ? 0 : GetLastError(); |
| 1201 | |
| 1202 | if (overlapped) { |
| 1203 | if (!ret) { |
| 1204 | if (err == ERROR_IO_PENDING) |
| 1205 | overlapped->pending = 1; |
| 1206 | else { |
| 1207 | Py_DECREF(overlapped); |
| 1208 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
| 1209 | } |
| 1210 | } |
| 1211 | return Py_BuildValue("NI", (PyObject *) overlapped, err); |
| 1212 | } |
| 1213 | |
| 1214 | PyBuffer_Release(buf); |
| 1215 | if (!ret) |
| 1216 | return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0); |
| 1217 | return Py_BuildValue("II", written, err); |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | static PyMethodDef winapi_functions[] = { |
| 1222 | {"CloseHandle", winapi_CloseHandle, METH_VARARGS, |
| 1223 | CloseHandle_doc}, |
| 1224 | {"ConnectNamedPipe", (PyCFunction)winapi_ConnectNamedPipe, |
| 1225 | METH_VARARGS | METH_KEYWORDS, ""}, |
| 1226 | {"CreateFile", winapi_CreateFile, METH_VARARGS, |
| 1227 | ""}, |
| 1228 | {"CreateNamedPipe", winapi_CreateNamedPipe, METH_VARARGS, |
| 1229 | ""}, |
| 1230 | {"CreatePipe", winapi_CreatePipe, METH_VARARGS, |
| 1231 | CreatePipe_doc}, |
| 1232 | {"CreateProcess", winapi_CreateProcess, METH_VARARGS, |
| 1233 | CreateProcess_doc}, |
| 1234 | {"DuplicateHandle", winapi_DuplicateHandle, METH_VARARGS, |
| 1235 | DuplicateHandle_doc}, |
| 1236 | {"ExitProcess", winapi_ExitProcess, METH_VARARGS, |
| 1237 | ""}, |
| 1238 | {"GetCurrentProcess", winapi_GetCurrentProcess, METH_VARARGS, |
| 1239 | GetCurrentProcess_doc}, |
| 1240 | {"GetExitCodeProcess", winapi_GetExitCodeProcess, METH_VARARGS, |
| 1241 | GetExitCodeProcess_doc}, |
| 1242 | {"GetLastError", winapi_GetLastError, METH_NOARGS, |
| 1243 | GetCurrentProcess_doc}, |
| 1244 | {"GetModuleFileName", winapi_GetModuleFileName, METH_VARARGS, |
| 1245 | GetModuleFileName_doc}, |
| 1246 | {"GetStdHandle", winapi_GetStdHandle, METH_VARARGS, |
| 1247 | GetStdHandle_doc}, |
| 1248 | {"GetVersion", winapi_GetVersion, METH_VARARGS, |
| 1249 | GetVersion_doc}, |
| 1250 | {"OpenProcess", winapi_OpenProcess, METH_VARARGS, |
| 1251 | ""}, |
| 1252 | {"PeekNamedPipe", winapi_PeekNamedPipe, METH_VARARGS, |
| 1253 | ""}, |
| 1254 | {"ReadFile", (PyCFunction)winapi_ReadFile, METH_VARARGS | METH_KEYWORDS, |
| 1255 | ""}, |
| 1256 | {"SetNamedPipeHandleState", winapi_SetNamedPipeHandleState, METH_VARARGS, |
| 1257 | ""}, |
| 1258 | {"TerminateProcess", winapi_TerminateProcess, METH_VARARGS, |
| 1259 | TerminateProcess_doc}, |
| 1260 | {"WaitNamedPipe", winapi_WaitNamedPipe, METH_VARARGS, |
| 1261 | ""}, |
| 1262 | {"WaitForMultipleObjects", winapi_WaitForMultipleObjects, METH_VARARGS, |
| 1263 | ""}, |
| 1264 | {"WaitForSingleObject", winapi_WaitForSingleObject, METH_VARARGS, |
| 1265 | WaitForSingleObject_doc}, |
| 1266 | {"WriteFile", (PyCFunction)winapi_WriteFile, METH_VARARGS | METH_KEYWORDS, |
| 1267 | ""}, |
| 1268 | {NULL, NULL} |
| 1269 | }; |
| 1270 | |
| 1271 | static struct PyModuleDef winapi_module = { |
| 1272 | PyModuleDef_HEAD_INIT, |
| 1273 | "_winapi", |
| 1274 | NULL, |
| 1275 | -1, |
| 1276 | winapi_functions, |
| 1277 | NULL, |
| 1278 | NULL, |
| 1279 | NULL, |
| 1280 | NULL |
| 1281 | }; |
| 1282 | |
| 1283 | #define WINAPI_CONSTANT(fmt, con) \ |
| 1284 | PyDict_SetItemString(d, #con, Py_BuildValue(fmt, con)) |
| 1285 | |
| 1286 | PyMODINIT_FUNC |
| 1287 | PyInit__winapi(void) |
| 1288 | { |
| 1289 | PyObject *d; |
| 1290 | PyObject *m; |
| 1291 | |
| 1292 | if (PyType_Ready(&OverlappedType) < 0) |
| 1293 | return NULL; |
| 1294 | |
| 1295 | m = PyModule_Create(&winapi_module); |
| 1296 | if (m == NULL) |
| 1297 | return NULL; |
| 1298 | d = PyModule_GetDict(m); |
| 1299 | |
| 1300 | PyDict_SetItemString(d, "Overlapped", (PyObject *) &OverlappedType); |
| 1301 | |
| 1302 | /* constants */ |
| 1303 | WINAPI_CONSTANT(F_DWORD, CREATE_NEW_CONSOLE); |
| 1304 | WINAPI_CONSTANT(F_DWORD, CREATE_NEW_PROCESS_GROUP); |
| 1305 | WINAPI_CONSTANT(F_DWORD, DUPLICATE_SAME_ACCESS); |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 1306 | WINAPI_CONSTANT(F_DWORD, DUPLICATE_CLOSE_SOURCE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1307 | WINAPI_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS); |
| 1308 | WINAPI_CONSTANT(F_DWORD, ERROR_BROKEN_PIPE); |
| 1309 | WINAPI_CONSTANT(F_DWORD, ERROR_IO_PENDING); |
| 1310 | WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA); |
| 1311 | WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED); |
| 1312 | WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1313 | WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA); |
| 1314 | WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED); |
Richard Oudkerk | fdb8dcf | 2012-05-05 19:45:37 +0100 | [diff] [blame] | 1315 | WINAPI_CONSTANT(F_DWORD, ERROR_NO_DATA); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1316 | WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES); |
| 1317 | WINAPI_CONSTANT(F_DWORD, ERROR_OPERATION_ABORTED); |
| 1318 | WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_BUSY); |
| 1319 | WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED); |
| 1320 | WINAPI_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT); |
| 1321 | WINAPI_CONSTANT(F_DWORD, FILE_FLAG_FIRST_PIPE_INSTANCE); |
| 1322 | WINAPI_CONSTANT(F_DWORD, FILE_FLAG_OVERLAPPED); |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 1323 | WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_READ); |
| 1324 | WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_WRITE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1325 | WINAPI_CONSTANT(F_DWORD, GENERIC_READ); |
| 1326 | WINAPI_CONSTANT(F_DWORD, GENERIC_WRITE); |
| 1327 | WINAPI_CONSTANT(F_DWORD, INFINITE); |
| 1328 | WINAPI_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER); |
| 1329 | WINAPI_CONSTANT(F_DWORD, OPEN_EXISTING); |
| 1330 | WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX); |
| 1331 | WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND); |
| 1332 | WINAPI_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE); |
| 1333 | WINAPI_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE); |
| 1334 | WINAPI_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES); |
| 1335 | WINAPI_CONSTANT(F_DWORD, PIPE_WAIT); |
| 1336 | WINAPI_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS); |
Antoine Pitrou | 5438ed1 | 2012-04-24 22:56:57 +0200 | [diff] [blame] | 1337 | WINAPI_CONSTANT(F_DWORD, PROCESS_DUP_HANDLE); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1338 | WINAPI_CONSTANT(F_DWORD, STARTF_USESHOWWINDOW); |
| 1339 | WINAPI_CONSTANT(F_DWORD, STARTF_USESTDHANDLES); |
| 1340 | WINAPI_CONSTANT(F_DWORD, STD_INPUT_HANDLE); |
| 1341 | WINAPI_CONSTANT(F_DWORD, STD_OUTPUT_HANDLE); |
| 1342 | WINAPI_CONSTANT(F_DWORD, STD_ERROR_HANDLE); |
| 1343 | WINAPI_CONSTANT(F_DWORD, STILL_ACTIVE); |
| 1344 | WINAPI_CONSTANT(F_DWORD, SW_HIDE); |
| 1345 | WINAPI_CONSTANT(F_DWORD, WAIT_OBJECT_0); |
Victor Stinner | 373f0a9 | 2014-03-20 09:26:55 +0100 | [diff] [blame] | 1346 | WINAPI_CONSTANT(F_DWORD, WAIT_ABANDONED_0); |
Antoine Pitrou | 23bba4c | 2012-04-18 20:51:15 +0200 | [diff] [blame] | 1347 | WINAPI_CONSTANT(F_DWORD, WAIT_TIMEOUT); |
| 1348 | |
| 1349 | WINAPI_CONSTANT("i", NULL); |
| 1350 | |
| 1351 | return m; |
| 1352 | } |