blob: fc9aaa461132cab47fe1edcecadaf340186174d8 [file] [log] [blame]
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00001/*
2 * support routines for subprocess module
3 *
4 * Currently, this extension module is only required when using the
5 * subprocess module on Windows, but in the future, stubs for other
Tim Petersf3250b02004-10-12 21:38:22 +00006 * platforms might be added here as well.
Fredrik Lundh5b3687d2004-10-12 15:26:28 +00007 *
8 * Copyright (c) 2004 by Fredrik Lundh <fredrik@pythonware.com>
9 * Copyright (c) 2004 by Secret Labs AB, http://www.pythonware.com
10 * Copyright (c) 2004 by Peter Astrand <astrand@lysator.liu.se>
Tim Petersf3250b02004-10-12 21:38:22 +000011 *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000012 * By obtaining, using, and/or copying this software and/or its
13 * associated documentation, you agree that you have read, understood,
14 * and will comply with the following terms and conditions:
Tim Petersf3250b02004-10-12 21:38:22 +000015 *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000016 * Permission to use, copy, modify, and distribute this software and
17 * its associated documentation for any purpose and without fee is
18 * hereby granted, provided that the above copyright notice appears in
19 * all copies, and that both that copyright notice and this permission
20 * notice appear in supporting documentation, and that the name of the
21 * authors not be used in advertising or publicity pertaining to
22 * distribution of the software without specific, written prior
23 * permission.
Tim Petersf3250b02004-10-12 21:38:22 +000024 *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000025 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
26 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
28 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
29 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
30 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
31 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Tim Petersf3250b02004-10-12 21:38:22 +000032 *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000033 */
34
Fredrik Lundh63168a52005-12-14 22:29:34 +000035/* Licensed to PSF under a Contributor Agreement. */
36/* See http://www.python.org/2.4/license for licensing details. */
37
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000038/* TODO: handle unicode command lines? */
39/* TODO: handle unicode environment? */
40
41#include "Python.h"
42
43#define WINDOWS_LEAN_AND_MEAN
44#include "windows.h"
45
46/* -------------------------------------------------------------------- */
47/* handle wrapper. note that this library uses integers when passing
48 handles to a function, and handle wrappers when returning handles.
49 the wrapper is used to provide Detach and Close methods */
50
51typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000052 PyObject_HEAD
53 HANDLE handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000054} sp_handle_object;
55
56staticforward PyTypeObject sp_handle_type;
57
58static PyObject*
59sp_handle_new(HANDLE handle)
60{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000061 sp_handle_object* self;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000062
Antoine Pitrouc83ea132010-05-09 14:46:46 +000063 self = PyObject_NEW(sp_handle_object, &sp_handle_type);
64 if (self == NULL)
65 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000066
Antoine Pitrouc83ea132010-05-09 14:46:46 +000067 self->handle = handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000068
Antoine Pitrouc83ea132010-05-09 14:46:46 +000069 return (PyObject*) self;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000070}
71
Gregory P. Smithb90f4e82008-07-20 00:22:08 +000072#if defined(MS_WIN32) && !defined(MS_WIN64)
Antoine Pitrouc83ea132010-05-09 14:46:46 +000073#define HANDLE_TO_PYNUM(handle) PyInt_FromLong((long) handle)
74#define PY_HANDLE_PARAM "l"
Gregory P. Smithb90f4e82008-07-20 00:22:08 +000075#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +000076#define HANDLE_TO_PYNUM(handle) PyLong_FromLongLong((long long) handle)
77#define PY_HANDLE_PARAM "L"
Gregory P. Smithb90f4e82008-07-20 00:22:08 +000078#endif
79
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000080static PyObject*
81sp_handle_detach(sp_handle_object* self, PyObject* args)
82{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000083 HANDLE handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000084
Antoine Pitrouc83ea132010-05-09 14:46:46 +000085 if (! PyArg_ParseTuple(args, ":Detach"))
86 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000087
Antoine Pitrouc83ea132010-05-09 14:46:46 +000088 handle = self->handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000089
Antoine Pitrouc83ea132010-05-09 14:46:46 +000090 self->handle = INVALID_HANDLE_VALUE;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000091
Antoine Pitrouc83ea132010-05-09 14:46:46 +000092 /* note: return the current handle, as an integer */
93 return HANDLE_TO_PYNUM(handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000094}
95
96static PyObject*
97sp_handle_close(sp_handle_object* self, PyObject* args)
98{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000099 if (! PyArg_ParseTuple(args, ":Close"))
100 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000101
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000102 if (self->handle != INVALID_HANDLE_VALUE) {
103 CloseHandle(self->handle);
104 self->handle = INVALID_HANDLE_VALUE;
105 }
106 Py_INCREF(Py_None);
107 return Py_None;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000108}
109
110static void
111sp_handle_dealloc(sp_handle_object* self)
112{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000113 if (self->handle != INVALID_HANDLE_VALUE)
114 CloseHandle(self->handle);
115 PyObject_FREE(self);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000116}
117
118static PyMethodDef sp_handle_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 {"Detach", (PyCFunction) sp_handle_detach, METH_VARARGS},
120 {"Close", (PyCFunction) sp_handle_close, METH_VARARGS},
121 {NULL, NULL}
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000122};
123
Tim Petersf3250b02004-10-12 21:38:22 +0000124static PyObject*
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000125sp_handle_getattr(sp_handle_object* self, char* name)
126{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000127 return Py_FindMethod(sp_handle_methods, (PyObject*) self, name);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000128}
129
130static PyObject*
131sp_handle_as_int(sp_handle_object* self)
132{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000133 return HANDLE_TO_PYNUM(self->handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000134}
135
136static PyNumberMethods sp_handle_as_number;
137
138statichere PyTypeObject sp_handle_type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000139 PyObject_HEAD_INIT(NULL)
140 0, /*ob_size*/
141 "_subprocess_handle", sizeof(sp_handle_object), 0,
142 (destructor) sp_handle_dealloc, /*tp_dealloc*/
143 0, /*tp_print*/
144 (getattrfunc) sp_handle_getattr,/*tp_getattr*/
145 0, /*tp_setattr*/
146 0, /*tp_compare*/
147 0, /*tp_repr*/
148 &sp_handle_as_number, /*tp_as_number */
149 0, /*tp_as_sequence */
150 0, /*tp_as_mapping */
151 0 /*tp_hash*/
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000152};
153
154/* -------------------------------------------------------------------- */
155/* windows API functions */
156
Brian Curtina2936cf2010-04-24 15:40:11 +0000157PyDoc_STRVAR(GetStdHandle_doc,
158"GetStdHandle(handle) -> integer\n\
159\n\
160Return a handle to the specified standard device\n\
161(STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE).\n\
162The integer associated with the handle object is returned.");
163
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000164static PyObject *
165sp_GetStdHandle(PyObject* self, PyObject* args)
166{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 HANDLE handle;
168 int std_handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000169
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle))
171 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000172
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000173 Py_BEGIN_ALLOW_THREADS
174 handle = GetStdHandle((DWORD) std_handle);
175 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000176
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000177 if (handle == INVALID_HANDLE_VALUE)
178 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000179
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000180 if (! handle) {
181 Py_INCREF(Py_None);
182 return Py_None;
183 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000184
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000185 /* note: returns integer, not handle object */
186 return HANDLE_TO_PYNUM(handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000187}
188
Brian Curtina2936cf2010-04-24 15:40:11 +0000189PyDoc_STRVAR(GetCurrentProcess_doc,
190"GetCurrentProcess() -> handle\n\
191\n\
192Return a handle object for the current process.");
193
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000194static PyObject *
195sp_GetCurrentProcess(PyObject* self, PyObject* args)
196{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 if (! PyArg_ParseTuple(args, ":GetCurrentProcess"))
198 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000199
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000200 return sp_handle_new(GetCurrentProcess());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000201}
202
Brian Curtina2936cf2010-04-24 15:40:11 +0000203PyDoc_STRVAR(DuplicateHandle_doc,
204"DuplicateHandle(source_proc_handle, source_handle,\n\
205 target_proc_handle, target_handle, access,\n\
206 inherit[, options]) -> handle\n\
207\n\
208Return a duplicate handle object.\n\
209\n\
210The duplicate handle refers to the same object as the original\n\
211handle. Therefore, any changes to the object are reflected\n\
212through both handles.");
213
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000214static PyObject *
215sp_DuplicateHandle(PyObject* self, PyObject* args)
216{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000217 HANDLE target_handle;
218 BOOL result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000219
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 HANDLE source_process_handle;
221 HANDLE source_handle;
222 HANDLE target_process_handle;
223 int desired_access;
224 int inherit_handle;
225 int options = 0;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000226
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000227 if (! PyArg_ParseTuple(args,
228 PY_HANDLE_PARAM PY_HANDLE_PARAM PY_HANDLE_PARAM
229 "ii|i:DuplicateHandle",
230 &source_process_handle,
231 &source_handle,
232 &target_process_handle,
233 &desired_access,
234 &inherit_handle,
235 &options))
236 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000237
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000238 Py_BEGIN_ALLOW_THREADS
239 result = DuplicateHandle(
240 source_process_handle,
241 source_handle,
242 target_process_handle,
243 &target_handle,
244 desired_access,
245 inherit_handle,
246 options
247 );
248 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000249
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000250 if (! result)
251 return PyErr_SetFromWindowsErr(GetLastError());
Tim Petersf3250b02004-10-12 21:38:22 +0000252
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000253 return sp_handle_new(target_handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000254}
255
Brian Curtina2936cf2010-04-24 15:40:11 +0000256PyDoc_STRVAR(CreatePipe_doc,
257"CreatePipe(pipe_attrs, size) -> (read_handle, write_handle)\n\
258\n\
259Create an anonymous pipe, and return handles to the read and\n\
260write ends of the pipe.\n\
261\n\
262pipe_attrs is ignored internally and can be None.");
263
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000264static PyObject *
265sp_CreatePipe(PyObject* self, PyObject* args)
266{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 HANDLE read_pipe;
268 HANDLE write_pipe;
269 BOOL result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000270
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000271 PyObject* pipe_attributes; /* ignored */
272 int size;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000273
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000274 if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size))
275 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000276
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000277 Py_BEGIN_ALLOW_THREADS
278 result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
279 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000280
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000281 if (! result)
282 return PyErr_SetFromWindowsErr(GetLastError());
Tim Petersf3250b02004-10-12 21:38:22 +0000283
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 return Py_BuildValue(
285 "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe));
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000286}
287
288/* helpers for createprocess */
289
290static int
291getint(PyObject* obj, char* name)
292{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000293 PyObject* value;
294 int ret;
Tim Petersf3250b02004-10-12 21:38:22 +0000295
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000296 value = PyObject_GetAttrString(obj, name);
297 if (! value) {
298 PyErr_Clear(); /* FIXME: propagate error? */
299 return 0;
300 }
301 ret = (int) PyInt_AsLong(value);
302 Py_DECREF(value);
303 return ret;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000304}
Tim Petersf3250b02004-10-12 21:38:22 +0000305
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000306static HANDLE
307gethandle(PyObject* obj, char* name)
308{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 sp_handle_object* value;
310 HANDLE ret;
Tim Petersf3250b02004-10-12 21:38:22 +0000311
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000312 value = (sp_handle_object*) PyObject_GetAttrString(obj, name);
313 if (! value) {
314 PyErr_Clear(); /* FIXME: propagate error? */
315 return NULL;
316 }
317 if (value->ob_type != &sp_handle_type)
318 ret = NULL;
319 else
320 ret = value->handle;
321 Py_DECREF(value);
322 return ret;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000323}
324
325static PyObject*
326getenvironment(PyObject* environment)
327{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000328 int i, envsize;
329 PyObject* out = NULL;
330 PyObject* keys;
331 PyObject* values;
332 char* p;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000333
Ezio Melotti419e23c2013-08-17 16:56:09 +0300334 /* convert environment dictionary to windows environment string */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000335 if (! PyMapping_Check(environment)) {
336 PyErr_SetString(
337 PyExc_TypeError, "environment must be dictionary or None");
338 return NULL;
339 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000340
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 envsize = PyMapping_Length(environment);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000342
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000343 keys = PyMapping_Keys(environment);
Oren Milmanc7f165f2017-09-15 10:20:11 +0300344 if (!keys) {
345 return NULL;
346 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347 values = PyMapping_Values(environment);
Oren Milmanc7f165f2017-09-15 10:20:11 +0300348 if (!values) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349 goto error;
Oren Milmanc7f165f2017-09-15 10:20:11 +0300350 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000351
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000352 out = PyString_FromStringAndSize(NULL, 2048);
353 if (! out)
354 goto error;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000356 p = PyString_AS_STRING(out);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000357
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000358 for (i = 0; i < envsize; i++) {
Serhiy Storchaka9dda2ca2017-06-24 11:49:00 +0300359 size_t ksize, vsize, totalsize;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000360 PyObject* key = PyList_GET_ITEM(keys, i);
361 PyObject* value = PyList_GET_ITEM(values, i);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000362
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 if (! PyString_Check(key) || ! PyString_Check(value)) {
364 PyErr_SetString(PyExc_TypeError,
365 "environment can only contain strings");
366 goto error;
367 }
368 ksize = PyString_GET_SIZE(key);
369 vsize = PyString_GET_SIZE(value);
Serhiy Storchaka9dda2ca2017-06-24 11:49:00 +0300370 if (strlen(PyString_AS_STRING(key)) != ksize ||
371 strlen(PyString_AS_STRING(value)) != vsize)
372 {
373 PyErr_SetString(PyExc_TypeError, "embedded null character");
374 goto error;
375 }
376 /* Search from index 1 because on Windows starting '=' is allowed for
377 defining hidden environment variables. */
378 if (ksize == 0 || strchr(PyString_AS_STRING(key) + 1, '=') != NULL) {
379 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
380 goto error;
381 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000382 totalsize = (p - PyString_AS_STRING(out)) + ksize + 1 +
383 vsize + 1 + 1;
384 if (totalsize > PyString_GET_SIZE(out)) {
Serhiy Storchaka9dda2ca2017-06-24 11:49:00 +0300385 size_t offset = p - PyString_AS_STRING(out);
Kristján Valur Jónssonbe580f22014-04-25 09:51:21 +0000386 if (_PyString_Resize(&out, totalsize + 1024))
387 goto exit;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000388 p = PyString_AS_STRING(out) + offset;
389 }
390 memcpy(p, PyString_AS_STRING(key), ksize);
391 p += ksize;
392 *p++ = '=';
393 memcpy(p, PyString_AS_STRING(value), vsize);
394 p += vsize;
395 *p++ = '\0';
396 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000397
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 /* add trailing null byte */
399 *p++ = '\0';
400 _PyString_Resize(&out, p - PyString_AS_STRING(out));
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000401
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000402 /* PyObject_Print(out, stdout, 0); */
Kristján Valur Jónssonbe580f22014-04-25 09:51:21 +0000403exit:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 Py_XDECREF(keys);
405 Py_XDECREF(values);
Fredrik Lundhbb4692b2005-11-12 10:15:03 +0000406
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000407 return out;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000408
Tim Petersf3250b02004-10-12 21:38:22 +0000409 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000410 Py_XDECREF(out);
411 Py_XDECREF(keys);
412 Py_XDECREF(values);
413 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000414}
415
Brian Curtina2936cf2010-04-24 15:40:11 +0000416PyDoc_STRVAR(CreateProcess_doc,
417"CreateProcess(app_name, cmd_line, proc_attrs, thread_attrs,\n\
418 inherit, flags, env_mapping, curdir,\n\
419 startup_info) -> (proc_handle, thread_handle,\n\
420 pid, tid)\n\
421\n\
422Create a new process and its primary thread. The return\n\
423value is a tuple of the process handle, thread handle,\n\
424process ID, and thread ID.\n\
425\n\
426proc_attrs and thread_attrs are ignored internally and can be None.");
427
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000428static PyObject *
429sp_CreateProcess(PyObject* self, PyObject* args)
430{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000431 BOOL result;
432 PROCESS_INFORMATION pi;
433 STARTUPINFO si;
434 PyObject* environment;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000435
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000436 char* application_name;
437 char* command_line;
438 PyObject* process_attributes; /* ignored */
439 PyObject* thread_attributes; /* ignored */
440 int inherit_handles;
441 int creation_flags;
442 PyObject* env_mapping;
443 char* current_directory;
444 PyObject* startup_info;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000445
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000446 if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess",
447 &application_name,
448 &command_line,
449 &process_attributes,
450 &thread_attributes,
451 &inherit_handles,
452 &creation_flags,
453 &env_mapping,
454 &current_directory,
455 &startup_info))
456 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000457
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000458 ZeroMemory(&si, sizeof(si));
459 si.cb = sizeof(si);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000460
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000461 /* note: we only support a small subset of all SI attributes */
462 si.dwFlags = getint(startup_info, "dwFlags");
463 si.wShowWindow = getint(startup_info, "wShowWindow");
464 si.hStdInput = gethandle(startup_info, "hStdInput");
465 si.hStdOutput = gethandle(startup_info, "hStdOutput");
466 si.hStdError = gethandle(startup_info, "hStdError");
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000467
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 if (PyErr_Occurred())
469 return NULL;
Fredrik Lundh3a49e922005-11-12 10:15:14 +0000470
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000471 if (env_mapping == Py_None)
472 environment = NULL;
473 else {
474 environment = getenvironment(env_mapping);
475 if (! environment)
476 return NULL;
477 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000478
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000479 Py_BEGIN_ALLOW_THREADS
480 result = CreateProcess(application_name,
481 command_line,
482 NULL,
483 NULL,
484 inherit_handles,
485 creation_flags,
486 environment ? PyString_AS_STRING(environment) : NULL,
487 current_directory,
488 &si,
489 &pi);
490 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000491
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000492 Py_XDECREF(environment);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000493
Tim Golden431774f2010-08-08 11:17:56 +0000494 if (! result)
495 return PyErr_SetFromWindowsErr(GetLastError());
Tim Petersf3250b02004-10-12 21:38:22 +0000496
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000497 return Py_BuildValue("NNii",
498 sp_handle_new(pi.hProcess),
499 sp_handle_new(pi.hThread),
500 pi.dwProcessId,
501 pi.dwThreadId);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000502}
503
Brian Curtina2936cf2010-04-24 15:40:11 +0000504PyDoc_STRVAR(TerminateProcess_doc,
505"TerminateProcess(handle, exit_code) -> None\n\
506\n\
507Terminate the specified process and all of its threads.");
508
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000509static PyObject *
Fredrik Lundhe5152932005-12-18 21:06:46 +0000510sp_TerminateProcess(PyObject* self, PyObject* args)
511{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000512 BOOL result;
Fredrik Lundhe5152932005-12-18 21:06:46 +0000513
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000514 HANDLE process;
515 int exit_code;
516 if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:TerminateProcess",
517 &process, &exit_code))
518 return NULL;
Fredrik Lundhe5152932005-12-18 21:06:46 +0000519
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 result = TerminateProcess(process, exit_code);
Fredrik Lundhe5152932005-12-18 21:06:46 +0000521
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000522 if (! result)
523 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundhe5152932005-12-18 21:06:46 +0000524
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000525 Py_INCREF(Py_None);
526 return Py_None;
Fredrik Lundhe5152932005-12-18 21:06:46 +0000527}
528
Brian Curtina2936cf2010-04-24 15:40:11 +0000529PyDoc_STRVAR(GetExitCodeProcess_doc,
530"GetExitCodeProcess(handle) -> Exit code\n\
531\n\
532Return the termination status of the specified process.");
533
Fredrik Lundhe5152932005-12-18 21:06:46 +0000534static PyObject *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000535sp_GetExitCodeProcess(PyObject* self, PyObject* args)
536{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000537 DWORD exit_code;
538 BOOL result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000539
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000540 HANDLE process;
541 if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetExitCodeProcess", &process))
542 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000543
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000544 result = GetExitCodeProcess(process, &exit_code);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000545
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000546 if (! result)
547 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000548
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000549 return PyInt_FromLong(exit_code);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000550}
551
Brian Curtina2936cf2010-04-24 15:40:11 +0000552PyDoc_STRVAR(WaitForSingleObject_doc,
553"WaitForSingleObject(handle, timeout) -> result\n\
554\n\
555Wait until the specified object is in the signaled state or\n\
556the time-out interval elapses. The timeout value is specified\n\
557in milliseconds.");
558
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000559static PyObject *
560sp_WaitForSingleObject(PyObject* self, PyObject* args)
561{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000562 DWORD result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000563
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000564 HANDLE handle;
565 int milliseconds;
566 if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:WaitForSingleObject",
567 &handle,
568 &milliseconds))
569 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000570
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000571 Py_BEGIN_ALLOW_THREADS
572 result = WaitForSingleObject(handle, (DWORD) milliseconds);
573 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000574
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000575 if (result == WAIT_FAILED)
576 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000577
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000578 return PyInt_FromLong((int) result);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000579}
580
Brian Curtina2936cf2010-04-24 15:40:11 +0000581PyDoc_STRVAR(GetVersion_doc,
582"GetVersion() -> version\n\
583\n\
584Return the version number of the current operating system.");
585
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000586static PyObject *
587sp_GetVersion(PyObject* self, PyObject* args)
588{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000589 if (! PyArg_ParseTuple(args, ":GetVersion"))
590 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000591
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000592 return PyInt_FromLong((int) GetVersion());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000593}
594
Brian Curtina2936cf2010-04-24 15:40:11 +0000595PyDoc_STRVAR(GetModuleFileName_doc,
596"GetModuleFileName(module) -> path\n\
597\n\
598Return the fully-qualified path for the file that contains\n\
599the specified module. The module must have been loaded by the\n\
600current process.\n\
601\n\
602The module parameter should be a handle to the loaded module\n\
603whose path is being requested. If this parameter is 0, \n\
604GetModuleFileName retrieves the path of the executable file\n\
605of the current process.");
606
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000607static PyObject *
608sp_GetModuleFileName(PyObject* self, PyObject* args)
609{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000610 BOOL result;
611 HMODULE module;
612 TCHAR filename[MAX_PATH];
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000613
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000614 if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetModuleFileName",
615 &module))
616 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000617
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000618 result = GetModuleFileName(module, filename, MAX_PATH);
619 filename[MAX_PATH-1] = '\0';
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000620
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000621 if (! result)
622 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000623
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000624 return PyString_FromString(filename);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000625}
626
627static PyMethodDef sp_functions[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000628 {"GetStdHandle", sp_GetStdHandle, METH_VARARGS, GetStdHandle_doc},
629 {"GetCurrentProcess", sp_GetCurrentProcess, METH_VARARGS,
630 GetCurrentProcess_doc},
631 {"DuplicateHandle", sp_DuplicateHandle, METH_VARARGS,
632 DuplicateHandle_doc},
633 {"CreatePipe", sp_CreatePipe, METH_VARARGS, CreatePipe_doc},
634 {"CreateProcess", sp_CreateProcess, METH_VARARGS, CreateProcess_doc},
635 {"TerminateProcess", sp_TerminateProcess, METH_VARARGS,
636 TerminateProcess_doc},
637 {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS,
638 GetExitCodeProcess_doc},
639 {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS,
640 WaitForSingleObject_doc},
641 {"GetVersion", sp_GetVersion, METH_VARARGS, GetVersion_doc},
642 {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS,
643 GetModuleFileName_doc},
644 {NULL, NULL}
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000645};
646
647/* -------------------------------------------------------------------- */
648
649static void
650defint(PyObject* d, const char* name, int value)
651{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000652 PyObject* v = PyInt_FromLong((long) value);
653 if (v) {
654 PyDict_SetItemString(d, (char*) name, v);
655 Py_DECREF(v);
656 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000657}
658
659#if PY_VERSION_HEX >= 0x02030000
660PyMODINIT_FUNC
661#else
662DL_EXPORT(void)
663#endif
664init_subprocess()
665{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000666 PyObject *d;
667 PyObject *m;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000668
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000669 /* patch up object descriptors */
670 sp_handle_type.ob_type = &PyType_Type;
671 sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000672
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000673 m = Py_InitModule("_subprocess", sp_functions);
674 if (m == NULL)
675 return;
676 d = PyModule_GetDict(m);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000677
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000678 /* constants */
679 defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE);
680 defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE);
681 defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE);
682 defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS);
683 defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES);
684 defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW);
685 defint(d, "SW_HIDE", SW_HIDE);
686 defint(d, "INFINITE", INFINITE);
687 defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0);
688 defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE);
689 defint(d, "CREATE_NEW_PROCESS_GROUP", CREATE_NEW_PROCESS_GROUP);
Antoine Pitrouf60845b2012-03-11 19:29:12 +0100690 defint(d, "STILL_ACTIVE", STILL_ACTIVE);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000691}