blob: 2a3207b87dee0f0ab47f68d4ca6b2b2052958c45 [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#include "Python.h"
39
40#define WINDOWS_LEAN_AND_MEAN
41#include "windows.h"
42
43/* -------------------------------------------------------------------- */
44/* handle wrapper. note that this library uses integers when passing
45 handles to a function, and handle wrappers when returning handles.
46 the wrapper is used to provide Detach and Close methods */
47
48typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 PyObject_HEAD
50 HANDLE handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000051} sp_handle_object;
52
Neal Norwitz227b5332006-03-22 09:28:35 +000053static PyTypeObject sp_handle_type;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000054
55static PyObject*
56sp_handle_new(HANDLE handle)
57{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 sp_handle_object* self;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 self = PyObject_NEW(sp_handle_object, &sp_handle_type);
61 if (self == NULL)
62 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 self->handle = handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 return (PyObject*) self;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000067}
68
Georg Brandl3dbca812008-07-23 16:10:53 +000069#if defined(MS_WIN32) && !defined(MS_WIN64)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070#define HANDLE_TO_PYNUM(handle) PyLong_FromLong((long) handle)
71#define PY_HANDLE_PARAM "l"
Georg Brandl3dbca812008-07-23 16:10:53 +000072#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073#define HANDLE_TO_PYNUM(handle) PyLong_FromLongLong((long long) handle)
74#define PY_HANDLE_PARAM "L"
Georg Brandl3dbca812008-07-23 16:10:53 +000075#endif
76
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000077static PyObject*
78sp_handle_detach(sp_handle_object* self, PyObject* args)
79{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 HANDLE handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 if (! PyArg_ParseTuple(args, ":Detach"))
83 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 handle = self->handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 self->handle = INVALID_HANDLE_VALUE;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 /* note: return the current handle, as an integer */
90 return HANDLE_TO_PYNUM(handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000091}
92
93static PyObject*
94sp_handle_close(sp_handle_object* self, PyObject* args)
95{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (! PyArg_ParseTuple(args, ":Close"))
97 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +000098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 if (self->handle != INVALID_HANDLE_VALUE) {
100 CloseHandle(self->handle);
101 self->handle = INVALID_HANDLE_VALUE;
102 }
103 Py_INCREF(Py_None);
104 return Py_None;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000105}
106
107static void
108sp_handle_dealloc(sp_handle_object* self)
109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 if (self->handle != INVALID_HANDLE_VALUE)
111 CloseHandle(self->handle);
112 PyObject_FREE(self);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000113}
114
115static PyMethodDef sp_handle_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 {"Detach", (PyCFunction) sp_handle_detach, METH_VARARGS},
117 {"Close", (PyCFunction) sp_handle_close, METH_VARARGS},
118 {NULL, NULL}
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000119};
120
Tim Petersf3250b02004-10-12 21:38:22 +0000121static PyObject*
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000122sp_handle_as_int(sp_handle_object* self)
123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 return HANDLE_TO_PYNUM(self->handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000125}
126
127static PyNumberMethods sp_handle_as_number;
128
Neal Norwitz227b5332006-03-22 09:28:35 +0000129static PyTypeObject sp_handle_type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 PyVarObject_HEAD_INIT(NULL, 0)
131 "_subprocess_handle", sizeof(sp_handle_object), 0,
132 (destructor) sp_handle_dealloc, /*tp_dealloc*/
133 0, /*tp_print*/
134 0, /*tp_getattr*/
135 0, /*tp_setattr*/
136 0, /*tp_reserved*/
137 0, /*tp_repr*/
138 &sp_handle_as_number, /*tp_as_number */
139 0, /*tp_as_sequence */
140 0, /*tp_as_mapping */
141 0, /*tp_hash*/
142 0, /*tp_call*/
143 0, /*tp_str*/
144 0, /*tp_getattro*/
145 0, /*tp_setattro*/
146 0, /*tp_as_buffer*/
147 Py_TPFLAGS_DEFAULT, /*tp_flags*/
148 0, /*tp_doc*/
149 0, /*tp_traverse*/
150 0, /*tp_clear*/
151 0, /*tp_richcompare*/
152 0, /*tp_weaklistoffset*/
153 0, /*tp_iter*/
154 0, /*tp_iternext*/
155 sp_handle_methods, /*tp_methods*/
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000156};
157
158/* -------------------------------------------------------------------- */
159/* windows API functions */
160
Brian Curtin1ce6b582010-04-24 16:19:22 +0000161PyDoc_STRVAR(GetStdHandle_doc,
162"GetStdHandle(handle) -> integer\n\
163\n\
164Return a handle to the specified standard device\n\
165(STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE).\n\
166The integer associated with the handle object is returned.");
167
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000168static PyObject *
169sp_GetStdHandle(PyObject* self, PyObject* args)
170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 HANDLE handle;
172 int std_handle;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle))
175 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 Py_BEGIN_ALLOW_THREADS
178 handle = GetStdHandle((DWORD) std_handle);
179 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (handle == INVALID_HANDLE_VALUE)
182 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 if (! handle) {
185 Py_INCREF(Py_None);
186 return Py_None;
187 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 /* note: returns integer, not handle object */
190 return HANDLE_TO_PYNUM(handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000191}
192
Brian Curtin1ce6b582010-04-24 16:19:22 +0000193PyDoc_STRVAR(GetCurrentProcess_doc,
194"GetCurrentProcess() -> handle\n\
195\n\
196Return a handle object for the current process.");
197
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000198static PyObject *
199sp_GetCurrentProcess(PyObject* self, PyObject* args)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 if (! PyArg_ParseTuple(args, ":GetCurrentProcess"))
202 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 return sp_handle_new(GetCurrentProcess());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000205}
206
Brian Curtin1ce6b582010-04-24 16:19:22 +0000207PyDoc_STRVAR(DuplicateHandle_doc,
208"DuplicateHandle(source_proc_handle, source_handle,\n\
209 target_proc_handle, target_handle, access,\n\
210 inherit[, options]) -> handle\n\
211\n\
212Return a duplicate handle object.\n\
213\n\
214The duplicate handle refers to the same object as the original\n\
215handle. Therefore, any changes to the object are reflected\n\
216through both handles.");
217
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000218static PyObject *
219sp_DuplicateHandle(PyObject* self, PyObject* args)
220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 HANDLE target_handle;
222 BOOL result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 HANDLE source_process_handle;
225 HANDLE source_handle;
226 HANDLE target_process_handle;
227 int desired_access;
228 int inherit_handle;
229 int options = 0;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (! PyArg_ParseTuple(args,
232 PY_HANDLE_PARAM PY_HANDLE_PARAM PY_HANDLE_PARAM
233 "ii|i:DuplicateHandle",
234 &source_process_handle,
235 &source_handle,
236 &target_process_handle,
237 &desired_access,
238 &inherit_handle,
239 &options))
240 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 Py_BEGIN_ALLOW_THREADS
243 result = DuplicateHandle(
244 source_process_handle,
245 source_handle,
246 target_process_handle,
247 &target_handle,
248 desired_access,
249 inherit_handle,
250 options
251 );
252 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (! result)
255 return PyErr_SetFromWindowsErr(GetLastError());
Tim Petersf3250b02004-10-12 21:38:22 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 return sp_handle_new(target_handle);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000258}
259
Brian Curtin1ce6b582010-04-24 16:19:22 +0000260PyDoc_STRVAR(CreatePipe_doc,
261"CreatePipe(pipe_attrs, size) -> (read_handle, write_handle)\n\
262\n\
263Create an anonymous pipe, and return handles to the read and\n\
264write ends of the pipe.\n\
265\n\
266pipe_attrs is ignored internally and can be None.");
267
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000268static PyObject *
269sp_CreatePipe(PyObject* self, PyObject* args)
270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 HANDLE read_pipe;
272 HANDLE write_pipe;
273 BOOL result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 PyObject* pipe_attributes; /* ignored */
276 int size;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size))
279 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 Py_BEGIN_ALLOW_THREADS
282 result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
283 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (! result)
286 return PyErr_SetFromWindowsErr(GetLastError());
Tim Petersf3250b02004-10-12 21:38:22 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 return Py_BuildValue(
289 "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe));
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000290}
291
292/* helpers for createprocess */
293
294static int
295getint(PyObject* obj, char* name)
296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 PyObject* value;
298 int ret;
Tim Petersf3250b02004-10-12 21:38:22 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 value = PyObject_GetAttrString(obj, name);
301 if (! value) {
302 PyErr_Clear(); /* FIXME: propagate error? */
303 return 0;
304 }
305 ret = (int) PyLong_AsLong(value);
306 Py_DECREF(value);
307 return ret;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000308}
Tim Petersf3250b02004-10-12 21:38:22 +0000309
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000310static HANDLE
311gethandle(PyObject* obj, char* name)
312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 sp_handle_object* value;
314 HANDLE ret;
Tim Petersf3250b02004-10-12 21:38:22 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 value = (sp_handle_object*) PyObject_GetAttrString(obj, name);
317 if (! value) {
318 PyErr_Clear(); /* FIXME: propagate error? */
319 return NULL;
320 }
321 if (Py_TYPE(value) != &sp_handle_type)
322 ret = NULL;
323 else
324 ret = value->handle;
325 Py_DECREF(value);
326 return ret;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000327}
328
329static PyObject*
330getenvironment(PyObject* environment)
331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 int i, envsize;
333 PyObject* out = NULL;
334 PyObject* keys;
335 PyObject* values;
336 Py_UNICODE* p;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 /* convert environment dictionary to windows enviroment string */
339 if (! PyMapping_Check(environment)) {
340 PyErr_SetString(
341 PyExc_TypeError, "environment must be dictionary or None");
342 return NULL;
343 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 envsize = PyMapping_Length(environment);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 keys = PyMapping_Keys(environment);
348 values = PyMapping_Values(environment);
349 if (!keys || !values)
350 goto error;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 out = PyUnicode_FromUnicode(NULL, 2048);
353 if (! out)
354 goto error;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 p = PyUnicode_AS_UNICODE(out);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 for (i = 0; i < envsize; i++) {
359 int ksize, vsize, totalsize;
360 PyObject* key = PyList_GET_ITEM(keys, i);
361 PyObject* value = PyList_GET_ITEM(values, i);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) {
364 PyErr_SetString(PyExc_TypeError,
365 "environment can only contain strings");
366 goto error;
367 }
368 ksize = PyUnicode_GET_SIZE(key);
369 vsize = PyUnicode_GET_SIZE(value);
370 totalsize = (p - PyUnicode_AS_UNICODE(out)) + ksize + 1 +
371 vsize + 1 + 1;
372 if (totalsize > PyUnicode_GET_SIZE(out)) {
373 int offset = p - PyUnicode_AS_UNICODE(out);
374 PyUnicode_Resize(&out, totalsize + 1024);
375 p = PyUnicode_AS_UNICODE(out) + offset;
376 }
377 Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(key), ksize);
378 p += ksize;
379 *p++ = '=';
380 Py_UNICODE_COPY(p, PyUnicode_AS_UNICODE(value), vsize);
381 p += vsize;
382 *p++ = '\0';
383 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 /* add trailing null byte */
386 *p++ = '\0';
387 PyUnicode_Resize(&out, p - PyUnicode_AS_UNICODE(out));
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* PyObject_Print(out, stdout, 0); */
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 Py_XDECREF(keys);
392 Py_XDECREF(values);
Fredrik Lundhbb4692b2005-11-12 10:15:03 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 return out;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000395
Tim Petersf3250b02004-10-12 21:38:22 +0000396 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 Py_XDECREF(out);
398 Py_XDECREF(keys);
399 Py_XDECREF(values);
400 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000401}
402
Brian Curtin1ce6b582010-04-24 16:19:22 +0000403PyDoc_STRVAR(CreateProcess_doc,
404"CreateProcess(app_name, cmd_line, proc_attrs, thread_attrs,\n\
405 inherit, flags, env_mapping, curdir,\n\
406 startup_info) -> (proc_handle, thread_handle,\n\
407 pid, tid)\n\
408\n\
409Create a new process and its primary thread. The return\n\
410value is a tuple of the process handle, thread handle,\n\
411process ID, and thread ID.\n\
412\n\
413proc_attrs and thread_attrs are ignored internally and can be None.");
414
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000415static PyObject *
416sp_CreateProcess(PyObject* self, PyObject* args)
417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 BOOL result;
419 PROCESS_INFORMATION pi;
420 STARTUPINFOW si;
421 PyObject* environment;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 Py_UNICODE* application_name;
424 Py_UNICODE* command_line;
425 PyObject* process_attributes; /* ignored */
426 PyObject* thread_attributes; /* ignored */
427 int inherit_handles;
428 int creation_flags;
429 PyObject* env_mapping;
430 Py_UNICODE* current_directory;
431 PyObject* startup_info;
Tim Goldenaf5ac392010-08-06 13:03:56 +0000432 DWORD error;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (! PyArg_ParseTuple(args, "ZZOOiiOZO:CreateProcess",
435 &application_name,
436 &command_line,
437 &process_attributes,
438 &thread_attributes,
439 &inherit_handles,
440 &creation_flags,
441 &env_mapping,
442 &current_directory,
443 &startup_info))
444 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 ZeroMemory(&si, sizeof(si));
447 si.cb = sizeof(si);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 /* note: we only support a small subset of all SI attributes */
450 si.dwFlags = getint(startup_info, "dwFlags");
451 si.wShowWindow = getint(startup_info, "wShowWindow");
452 si.hStdInput = gethandle(startup_info, "hStdInput");
453 si.hStdOutput = gethandle(startup_info, "hStdOutput");
454 si.hStdError = gethandle(startup_info, "hStdError");
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 if (PyErr_Occurred())
457 return NULL;
Fredrik Lundh3a49e922005-11-12 10:15:14 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (env_mapping == Py_None)
460 environment = NULL;
461 else {
462 environment = getenvironment(env_mapping);
463 if (! environment)
464 return NULL;
465 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 Py_BEGIN_ALLOW_THREADS
468 result = CreateProcessW(application_name,
469 command_line,
470 NULL,
471 NULL,
472 inherit_handles,
473 creation_flags | CREATE_UNICODE_ENVIRONMENT,
474 environment ? PyUnicode_AS_UNICODE(environment) : NULL,
475 current_directory,
476 &si,
477 &pi);
478 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 Py_XDECREF(environment);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000481
Tim Goldenaf5ac392010-08-06 13:03:56 +0000482 if (! result) {
483 error = GetLastError();
484 if(si.hStdInput != INVALID_HANDLE_VALUE) {
485 CloseHandle(si.hStdInput);
486 si.hStdInput = INVALID_HANDLE_VALUE;
487 }
488 if(si.hStdOutput != INVALID_HANDLE_VALUE) {
489 CloseHandle(si.hStdOutput);
490 si.hStdOutput = INVALID_HANDLE_VALUE;
491 }
492 if(si.hStdError != INVALID_HANDLE_VALUE) {
493 CloseHandle(si.hStdError);
494 si.hStdError = INVALID_HANDLE_VALUE;
495 }
496 return PyErr_SetFromWindowsErr(error);
497 }
Tim Petersf3250b02004-10-12 21:38:22 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return Py_BuildValue("NNii",
500 sp_handle_new(pi.hProcess),
501 sp_handle_new(pi.hThread),
502 pi.dwProcessId,
503 pi.dwThreadId);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000504}
505
Brian Curtin1ce6b582010-04-24 16:19:22 +0000506PyDoc_STRVAR(TerminateProcess_doc,
507"TerminateProcess(handle, exit_code) -> None\n\
508\n\
509Terminate the specified process and all of its threads.");
510
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000511static PyObject *
Fredrik Lundhe5152932005-12-18 21:06:46 +0000512sp_TerminateProcess(PyObject* self, PyObject* args)
513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 BOOL result;
Fredrik Lundhe5152932005-12-18 21:06:46 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 HANDLE process;
517 int exit_code;
518 if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:TerminateProcess",
519 &process, &exit_code))
520 return NULL;
Fredrik Lundhe5152932005-12-18 21:06:46 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 result = TerminateProcess(process, exit_code);
Fredrik Lundhe5152932005-12-18 21:06:46 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 if (! result)
525 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundhe5152932005-12-18 21:06:46 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 Py_INCREF(Py_None);
528 return Py_None;
Fredrik Lundhe5152932005-12-18 21:06:46 +0000529}
530
Brian Curtin1ce6b582010-04-24 16:19:22 +0000531PyDoc_STRVAR(GetExitCodeProcess_doc,
532"GetExitCodeProcess(handle) -> Exit code\n\
533\n\
534Return the termination status of the specified process.");
535
Fredrik Lundhe5152932005-12-18 21:06:46 +0000536static PyObject *
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000537sp_GetExitCodeProcess(PyObject* self, PyObject* args)
538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 DWORD exit_code;
540 BOOL result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 HANDLE process;
543 if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetExitCodeProcess", &process))
544 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 result = GetExitCodeProcess(process, &exit_code);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (! result)
549 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 return PyLong_FromLong(exit_code);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000552}
553
Brian Curtin1ce6b582010-04-24 16:19:22 +0000554PyDoc_STRVAR(WaitForSingleObject_doc,
555"WaitForSingleObject(handle, timeout) -> result\n\
556\n\
557Wait until the specified object is in the signaled state or\n\
558the time-out interval elapses. The timeout value is specified\n\
559in milliseconds.");
560
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000561static PyObject *
562sp_WaitForSingleObject(PyObject* self, PyObject* args)
563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 DWORD result;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 HANDLE handle;
567 int milliseconds;
568 if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM "i:WaitForSingleObject",
569 &handle,
570 &milliseconds))
571 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 Py_BEGIN_ALLOW_THREADS
574 result = WaitForSingleObject(handle, (DWORD) milliseconds);
575 Py_END_ALLOW_THREADS
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 if (result == WAIT_FAILED)
578 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 return PyLong_FromLong((int) result);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000581}
582
Brian Curtin1ce6b582010-04-24 16:19:22 +0000583PyDoc_STRVAR(GetVersion_doc,
584"GetVersion() -> version\n\
585\n\
586Return the version number of the current operating system.");
587
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000588static PyObject *
589sp_GetVersion(PyObject* self, PyObject* args)
590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 if (! PyArg_ParseTuple(args, ":GetVersion"))
592 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 return PyLong_FromLong((int) GetVersion());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000595}
596
Brian Curtin1ce6b582010-04-24 16:19:22 +0000597PyDoc_STRVAR(GetModuleFileName_doc,
598"GetModuleFileName(module) -> path\n\
599\n\
600Return the fully-qualified path for the file that contains\n\
601the specified module. The module must have been loaded by the\n\
602current process.\n\
603\n\
604The module parameter should be a handle to the loaded module\n\
605whose path is being requested. If this parameter is 0, \n\
606GetModuleFileName retrieves the path of the executable file\n\
607of the current process.");
608
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000609static PyObject *
610sp_GetModuleFileName(PyObject* self, PyObject* args)
611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 BOOL result;
613 HMODULE module;
614 WCHAR filename[MAX_PATH];
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 if (! PyArg_ParseTuple(args, PY_HANDLE_PARAM ":GetModuleFileName",
617 &module))
618 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 result = GetModuleFileNameW(module, filename, MAX_PATH);
621 filename[MAX_PATH-1] = '\0';
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 if (! result)
624 return PyErr_SetFromWindowsErr(GetLastError());
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 return PyUnicode_FromUnicode(filename, Py_UNICODE_strlen(filename));
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000627}
628
629static PyMethodDef sp_functions[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 {"GetStdHandle", sp_GetStdHandle, METH_VARARGS, GetStdHandle_doc},
631 {"GetCurrentProcess", sp_GetCurrentProcess, METH_VARARGS,
632 GetCurrentProcess_doc},
633 {"DuplicateHandle", sp_DuplicateHandle, METH_VARARGS,
634 DuplicateHandle_doc},
635 {"CreatePipe", sp_CreatePipe, METH_VARARGS, CreatePipe_doc},
636 {"CreateProcess", sp_CreateProcess, METH_VARARGS, CreateProcess_doc},
637 {"TerminateProcess", sp_TerminateProcess, METH_VARARGS,
638 TerminateProcess_doc},
639 {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS,
640 GetExitCodeProcess_doc},
641 {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS,
642 WaitForSingleObject_doc},
643 {"GetVersion", sp_GetVersion, METH_VARARGS, GetVersion_doc},
644 {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS,
645 GetModuleFileName_doc},
646 {NULL, NULL}
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000647};
648
649/* -------------------------------------------------------------------- */
650
651static void
652defint(PyObject* d, const char* name, int value)
653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 PyObject* v = PyLong_FromLong((long) value);
655 if (v) {
656 PyDict_SetItemString(d, (char*) name, v);
657 Py_DECREF(v);
658 }
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000659}
660
Martin v. Löwis1a214512008-06-11 05:26:20 +0000661static struct PyModuleDef _subprocessmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 PyModuleDef_HEAD_INIT,
663 "_subprocess",
664 NULL,
665 -1,
666 sp_functions,
667 NULL,
668 NULL,
669 NULL,
670 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000671};
672
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000673PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000674PyInit__subprocess()
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 PyObject *d;
677 PyObject *m;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 /* patch up object descriptors */
680 sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int;
681 if (PyType_Ready(&sp_handle_type) < 0)
682 return NULL;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 m = PyModule_Create(&_subprocessmodule);
685 if (m == NULL)
686 return NULL;
687 d = PyModule_GetDict(m);
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 /* constants */
690 defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE);
691 defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE);
692 defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE);
693 defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS);
694 defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES);
695 defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW);
696 defint(d, "SW_HIDE", SW_HIDE);
697 defint(d, "INFINITE", INFINITE);
698 defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0);
699 defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE);
700 defint(d, "CREATE_NEW_PROCESS_GROUP", CREATE_NEW_PROCESS_GROUP);
Brian Curtineb24d742010-04-12 17:16:38 +0000701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 return m;
Fredrik Lundh5b3687d2004-10-12 15:26:28 +0000703}