PageRenderTime 176ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 1ms

/Modules/_winapi.c

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