Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1 | /* |
| 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 6 | * platforms might be added here as well. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 7 | * |
| 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 11 | * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 12 | * 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 15 | * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 16 | * 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 24 | * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 25 | * 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 Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 32 | * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
Fredrik Lundh | 63168a5 | 2005-12-14 22:29:34 +0000 | [diff] [blame] | 35 | /* Licensed to PSF under a Contributor Agreement. */ |
| 36 | /* See http://www.python.org/2.4/license for licensing details. */ |
| 37 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 38 | /* 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 | |
| 51 | typedef struct { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 52 | PyObject_HEAD |
| 53 | HANDLE handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 54 | } sp_handle_object; |
| 55 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 56 | static PyTypeObject sp_handle_type; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 57 | |
| 58 | static PyObject* |
| 59 | sp_handle_new(HANDLE handle) |
| 60 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 61 | sp_handle_object* self; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 62 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 63 | self = PyObject_NEW(sp_handle_object, &sp_handle_type); |
| 64 | if (self == NULL) |
| 65 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 66 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 67 | self->handle = handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 68 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 69 | return (PyObject*) self; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static PyObject* |
| 73 | sp_handle_detach(sp_handle_object* self, PyObject* args) |
| 74 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 75 | HANDLE handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 76 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 77 | if (! PyArg_ParseTuple(args, ":Detach")) |
| 78 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 79 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 80 | handle = self->handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 81 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 82 | self->handle = NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 83 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 84 | /* note: return the current handle, as an integer */ |
| 85 | return PyInt_FromLong((long) handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static PyObject* |
| 89 | sp_handle_close(sp_handle_object* self, PyObject* args) |
| 90 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 91 | if (! PyArg_ParseTuple(args, ":Close")) |
| 92 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 93 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 94 | if (self->handle != INVALID_HANDLE_VALUE) { |
| 95 | CloseHandle(self->handle); |
| 96 | self->handle = INVALID_HANDLE_VALUE; |
| 97 | } |
| 98 | Py_INCREF(Py_None); |
| 99 | return Py_None; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void |
| 103 | sp_handle_dealloc(sp_handle_object* self) |
| 104 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 105 | if (self->handle != INVALID_HANDLE_VALUE) |
| 106 | CloseHandle(self->handle); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 107 | PyObject_FREE(self); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static PyMethodDef sp_handle_methods[] = { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 111 | {"Detach", (PyCFunction) sp_handle_detach, METH_VARARGS}, |
| 112 | {"Close", (PyCFunction) sp_handle_close, METH_VARARGS}, |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 113 | {NULL, NULL} |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 116 | static PyObject* |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 117 | sp_handle_getattr(sp_handle_object* self, char* name) |
| 118 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 119 | return Py_FindMethod(sp_handle_methods, (PyObject*) self, name); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static PyObject* |
| 123 | sp_handle_as_int(sp_handle_object* self) |
| 124 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 125 | return PyInt_FromLong((long) self->handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static PyNumberMethods sp_handle_as_number; |
| 129 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 130 | static PyTypeObject sp_handle_type = { |
Martin v. Löwis | 95c95ce | 2007-07-22 14:41:55 +0000 | [diff] [blame^] | 131 | PyVarObject_HEAD_INIT(NULL, 0) |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 132 | "_subprocess_handle", sizeof(sp_handle_object), 0, |
| 133 | (destructor) sp_handle_dealloc, /*tp_dealloc*/ |
| 134 | 0, /*tp_print*/ |
| 135 | (getattrfunc) sp_handle_getattr,/*tp_getattr*/ |
| 136 | 0, /*tp_setattr*/ |
| 137 | 0, /*tp_compare*/ |
| 138 | 0, /*tp_repr*/ |
| 139 | &sp_handle_as_number, /*tp_as_number */ |
| 140 | 0, /*tp_as_sequence */ |
| 141 | 0, /*tp_as_mapping */ |
| 142 | 0 /*tp_hash*/ |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /* -------------------------------------------------------------------- */ |
| 146 | /* windows API functions */ |
| 147 | |
| 148 | static PyObject * |
| 149 | sp_GetStdHandle(PyObject* self, PyObject* args) |
| 150 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 151 | HANDLE handle; |
| 152 | int std_handle; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 153 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 154 | if (! PyArg_ParseTuple(args, "i:GetStdHandle", &std_handle)) |
| 155 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 156 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 157 | Py_BEGIN_ALLOW_THREADS |
| 158 | handle = GetStdHandle((DWORD) std_handle); |
| 159 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 160 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 161 | if (handle == INVALID_HANDLE_VALUE) |
| 162 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 163 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 164 | if (! handle) { |
| 165 | Py_INCREF(Py_None); |
| 166 | return Py_None; |
| 167 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 168 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 169 | /* note: returns integer, not handle object */ |
| 170 | return PyInt_FromLong((long) handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static PyObject * |
| 174 | sp_GetCurrentProcess(PyObject* self, PyObject* args) |
| 175 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 176 | if (! PyArg_ParseTuple(args, ":GetCurrentProcess")) |
| 177 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 178 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 179 | return sp_handle_new(GetCurrentProcess()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static PyObject * |
| 183 | sp_DuplicateHandle(PyObject* self, PyObject* args) |
| 184 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 185 | HANDLE target_handle; |
| 186 | BOOL result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 187 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 188 | long source_process_handle; |
| 189 | long source_handle; |
| 190 | long target_process_handle; |
| 191 | int desired_access; |
| 192 | int inherit_handle; |
| 193 | int options = 0; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 194 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 195 | if (! PyArg_ParseTuple(args, "lllii|i:DuplicateHandle", |
| 196 | &source_process_handle, |
| 197 | &source_handle, |
| 198 | &target_process_handle, |
| 199 | &desired_access, |
| 200 | &inherit_handle, |
| 201 | &options)) |
| 202 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 203 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 204 | Py_BEGIN_ALLOW_THREADS |
| 205 | result = DuplicateHandle( |
| 206 | (HANDLE) source_process_handle, |
| 207 | (HANDLE) source_handle, |
| 208 | (HANDLE) target_process_handle, |
| 209 | &target_handle, |
| 210 | desired_access, |
| 211 | inherit_handle, |
| 212 | options |
| 213 | ); |
| 214 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 215 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 216 | if (! result) |
| 217 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 218 | |
| 219 | return sp_handle_new(target_handle); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static PyObject * |
| 223 | sp_CreatePipe(PyObject* self, PyObject* args) |
| 224 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 225 | HANDLE read_pipe; |
| 226 | HANDLE write_pipe; |
| 227 | BOOL result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 228 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 229 | PyObject* pipe_attributes; /* ignored */ |
| 230 | int size; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 231 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 232 | if (! PyArg_ParseTuple(args, "Oi:CreatePipe", &pipe_attributes, &size)) |
| 233 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 234 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 235 | Py_BEGIN_ALLOW_THREADS |
| 236 | result = CreatePipe(&read_pipe, &write_pipe, NULL, size); |
| 237 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 238 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 239 | if (! result) |
| 240 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 241 | |
| 242 | return Py_BuildValue( |
| 243 | "NN", sp_handle_new(read_pipe), sp_handle_new(write_pipe)); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /* helpers for createprocess */ |
| 247 | |
| 248 | static int |
| 249 | getint(PyObject* obj, char* name) |
| 250 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 251 | PyObject* value; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 252 | int ret; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 253 | |
| 254 | value = PyObject_GetAttrString(obj, name); |
| 255 | if (! value) { |
| 256 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 257 | return 0; |
| 258 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 259 | ret = (int) PyInt_AsLong(value); |
| 260 | Py_DECREF(value); |
| 261 | return ret; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 262 | } |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 263 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 264 | static HANDLE |
| 265 | gethandle(PyObject* obj, char* name) |
| 266 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 267 | sp_handle_object* value; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 268 | HANDLE ret; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 269 | |
| 270 | value = (sp_handle_object*) PyObject_GetAttrString(obj, name); |
| 271 | if (! value) { |
| 272 | PyErr_Clear(); /* FIXME: propagate error? */ |
| 273 | return NULL; |
| 274 | } |
Martin v. Löwis | 95c95ce | 2007-07-22 14:41:55 +0000 | [diff] [blame^] | 275 | if (Py_Type(&value) != &sp_handle_type) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 276 | ret = NULL; |
| 277 | else |
| 278 | ret = value->handle; |
| 279 | Py_DECREF(value); |
| 280 | return ret; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static PyObject* |
| 284 | getenvironment(PyObject* environment) |
| 285 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 286 | int i, envsize; |
| 287 | PyObject* out = NULL; |
| 288 | PyObject* keys; |
| 289 | PyObject* values; |
| 290 | char* p; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 291 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 292 | /* convert environment dictionary to windows enviroment string */ |
| 293 | if (! PyMapping_Check(environment)) { |
| 294 | PyErr_SetString( |
| 295 | PyExc_TypeError, "environment must be dictionary or None"); |
| 296 | return NULL; |
| 297 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 298 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 299 | envsize = PyMapping_Length(environment); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 300 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 301 | keys = PyMapping_Keys(environment); |
| 302 | values = PyMapping_Values(environment); |
| 303 | if (!keys || !values) |
| 304 | goto error; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 305 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 306 | out = PyString_FromStringAndSize(NULL, 2048); |
| 307 | if (! out) |
| 308 | goto error; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 309 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 310 | p = PyString_AS_STRING(out); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 311 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 312 | for (i = 0; i < envsize; i++) { |
| 313 | int ksize, vsize, totalsize; |
| 314 | PyObject* key = PyList_GET_ITEM(keys, i); |
| 315 | PyObject* value = PyList_GET_ITEM(values, i); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 316 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 317 | if (! PyString_Check(key) || ! PyString_Check(value)) { |
| 318 | PyErr_SetString(PyExc_TypeError, |
| 319 | "environment can only contain strings"); |
| 320 | goto error; |
| 321 | } |
| 322 | ksize = PyString_GET_SIZE(key); |
| 323 | vsize = PyString_GET_SIZE(value); |
| 324 | totalsize = (p - PyString_AS_STRING(out)) + ksize + 1 + |
| 325 | vsize + 1 + 1; |
| 326 | if (totalsize > PyString_GET_SIZE(out)) { |
| 327 | int offset = p - PyString_AS_STRING(out); |
| 328 | _PyString_Resize(&out, totalsize + 1024); |
| 329 | p = PyString_AS_STRING(out) + offset; |
| 330 | } |
| 331 | memcpy(p, PyString_AS_STRING(key), ksize); |
| 332 | p += ksize; |
| 333 | *p++ = '='; |
| 334 | memcpy(p, PyString_AS_STRING(value), vsize); |
| 335 | p += vsize; |
| 336 | *p++ = '\0'; |
| 337 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 338 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 339 | /* add trailing null byte */ |
| 340 | *p++ = '\0'; |
| 341 | _PyString_Resize(&out, p - PyString_AS_STRING(out)); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 342 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 343 | /* PyObject_Print(out, stdout, 0); */ |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 344 | |
Fredrik Lundh | bb4692b | 2005-11-12 10:15:03 +0000 | [diff] [blame] | 345 | Py_XDECREF(keys); |
| 346 | Py_XDECREF(values); |
| 347 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 348 | return out; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 349 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 350 | error: |
| 351 | Py_XDECREF(out); |
| 352 | Py_XDECREF(keys); |
| 353 | Py_XDECREF(values); |
| 354 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static PyObject * |
| 358 | sp_CreateProcess(PyObject* self, PyObject* args) |
| 359 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 360 | BOOL result; |
| 361 | PROCESS_INFORMATION pi; |
| 362 | STARTUPINFO si; |
| 363 | PyObject* environment; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 364 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 365 | char* application_name; |
| 366 | char* command_line; |
| 367 | PyObject* process_attributes; /* ignored */ |
| 368 | PyObject* thread_attributes; /* ignored */ |
| 369 | int inherit_handles; |
| 370 | int creation_flags; |
| 371 | PyObject* env_mapping; |
| 372 | char* current_directory; |
| 373 | PyObject* startup_info; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 374 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 375 | if (! PyArg_ParseTuple(args, "zzOOiiOzO:CreateProcess", |
| 376 | &application_name, |
| 377 | &command_line, |
| 378 | &process_attributes, |
| 379 | &thread_attributes, |
| 380 | &inherit_handles, |
| 381 | &creation_flags, |
| 382 | &env_mapping, |
| 383 | ¤t_directory, |
| 384 | &startup_info)) |
| 385 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 386 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 387 | ZeroMemory(&si, sizeof(si)); |
| 388 | si.cb = sizeof(si); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 389 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 390 | /* note: we only support a small subset of all SI attributes */ |
| 391 | si.dwFlags = getint(startup_info, "dwFlags"); |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 392 | si.wShowWindow = getint(startup_info, "wShowWindow"); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 393 | si.hStdInput = gethandle(startup_info, "hStdInput"); |
| 394 | si.hStdOutput = gethandle(startup_info, "hStdOutput"); |
| 395 | si.hStdError = gethandle(startup_info, "hStdError"); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 396 | |
Fredrik Lundh | 3a49e92 | 2005-11-12 10:15:14 +0000 | [diff] [blame] | 397 | if (PyErr_Occurred()) |
| 398 | return NULL; |
| 399 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 400 | if (env_mapping == Py_None) |
| 401 | environment = NULL; |
| 402 | else { |
| 403 | environment = getenvironment(env_mapping); |
| 404 | if (! environment) |
| 405 | return NULL; |
| 406 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 407 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 408 | Py_BEGIN_ALLOW_THREADS |
| 409 | result = CreateProcess(application_name, |
| 410 | command_line, |
| 411 | NULL, |
| 412 | NULL, |
| 413 | inherit_handles, |
| 414 | creation_flags, |
| 415 | environment ? PyString_AS_STRING(environment) : NULL, |
| 416 | current_directory, |
| 417 | &si, |
| 418 | &pi); |
| 419 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 420 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 421 | Py_XDECREF(environment); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 422 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 423 | if (! result) |
| 424 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 425 | |
| 426 | return Py_BuildValue("NNii", |
| 427 | sp_handle_new(pi.hProcess), |
| 428 | sp_handle_new(pi.hThread), |
| 429 | pi.dwProcessId, |
| 430 | pi.dwThreadId); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static PyObject * |
Fredrik Lundh | e515293 | 2005-12-18 21:06:46 +0000 | [diff] [blame] | 434 | sp_TerminateProcess(PyObject* self, PyObject* args) |
| 435 | { |
| 436 | BOOL result; |
| 437 | |
| 438 | long process; |
| 439 | int exit_code; |
| 440 | if (! PyArg_ParseTuple(args, "li:TerminateProcess", &process, |
| 441 | &exit_code)) |
| 442 | return NULL; |
| 443 | |
| 444 | result = TerminateProcess((HANDLE) process, exit_code); |
| 445 | |
| 446 | if (! result) |
| 447 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 448 | |
| 449 | Py_INCREF(Py_None); |
| 450 | return Py_None; |
| 451 | } |
| 452 | |
| 453 | static PyObject * |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 454 | sp_GetExitCodeProcess(PyObject* self, PyObject* args) |
| 455 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 456 | DWORD exit_code; |
| 457 | BOOL result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 458 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 459 | long process; |
| 460 | if (! PyArg_ParseTuple(args, "l:GetExitCodeProcess", &process)) |
| 461 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 462 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 463 | result = GetExitCodeProcess((HANDLE) process, &exit_code); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 464 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 465 | if (! result) |
| 466 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 467 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 468 | return PyInt_FromLong(exit_code); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | static PyObject * |
| 472 | sp_WaitForSingleObject(PyObject* self, PyObject* args) |
| 473 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 474 | DWORD result; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 475 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 476 | long handle; |
| 477 | int milliseconds; |
| 478 | if (! PyArg_ParseTuple(args, "li:WaitForSingleObject", |
| 479 | &handle, |
| 480 | &milliseconds)) |
| 481 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 482 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 483 | Py_BEGIN_ALLOW_THREADS |
| 484 | result = WaitForSingleObject((HANDLE) handle, (DWORD) milliseconds); |
| 485 | Py_END_ALLOW_THREADS |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 486 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 487 | if (result == WAIT_FAILED) |
| 488 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 489 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 490 | return PyInt_FromLong((int) result); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static PyObject * |
| 494 | sp_GetVersion(PyObject* self, PyObject* args) |
| 495 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 496 | if (! PyArg_ParseTuple(args, ":GetVersion")) |
| 497 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 498 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 499 | return PyInt_FromLong((int) GetVersion()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static PyObject * |
| 503 | sp_GetModuleFileName(PyObject* self, PyObject* args) |
| 504 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 505 | BOOL result; |
| 506 | long module; |
| 507 | TCHAR filename[MAX_PATH]; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 508 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 509 | if (! PyArg_ParseTuple(args, "l:GetModuleFileName", &module)) |
| 510 | return NULL; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 511 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 512 | result = GetModuleFileName((HMODULE)module, filename, MAX_PATH); |
| 513 | filename[MAX_PATH-1] = '\0'; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 514 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 515 | if (! result) |
| 516 | return PyErr_SetFromWindowsErr(GetLastError()); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 517 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 518 | return PyString_FromString(filename); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static PyMethodDef sp_functions[] = { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 522 | {"GetStdHandle", sp_GetStdHandle, METH_VARARGS}, |
| 523 | {"GetCurrentProcess", sp_GetCurrentProcess, METH_VARARGS}, |
| 524 | {"DuplicateHandle", sp_DuplicateHandle, METH_VARARGS}, |
| 525 | {"CreatePipe", sp_CreatePipe, METH_VARARGS}, |
| 526 | {"CreateProcess", sp_CreateProcess, METH_VARARGS}, |
Fredrik Lundh | e515293 | 2005-12-18 21:06:46 +0000 | [diff] [blame] | 527 | {"TerminateProcess", sp_TerminateProcess, METH_VARARGS}, |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 528 | {"GetExitCodeProcess", sp_GetExitCodeProcess, METH_VARARGS}, |
| 529 | {"WaitForSingleObject", sp_WaitForSingleObject, METH_VARARGS}, |
| 530 | {"GetVersion", sp_GetVersion, METH_VARARGS}, |
| 531 | {"GetModuleFileName", sp_GetModuleFileName, METH_VARARGS}, |
| 532 | {NULL, NULL} |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | /* -------------------------------------------------------------------- */ |
| 536 | |
| 537 | static void |
| 538 | defint(PyObject* d, const char* name, int value) |
| 539 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 540 | PyObject* v = PyInt_FromLong((long) value); |
| 541 | if (v) { |
| 542 | PyDict_SetItemString(d, (char*) name, v); |
| 543 | Py_DECREF(v); |
| 544 | } |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | #if PY_VERSION_HEX >= 0x02030000 |
| 548 | PyMODINIT_FUNC |
| 549 | #else |
| 550 | DL_EXPORT(void) |
| 551 | #endif |
| 552 | init_subprocess() |
| 553 | { |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 554 | PyObject *d; |
| 555 | PyObject *m; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 556 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 557 | /* patch up object descriptors */ |
Martin v. Löwis | 95c95ce | 2007-07-22 14:41:55 +0000 | [diff] [blame^] | 558 | Py_Type(&sp_handle_type) = &PyType_Type; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 559 | sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int; |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 560 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 561 | m = Py_InitModule("_subprocess", sp_functions); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 562 | if (m == NULL) |
| 563 | return; |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 564 | d = PyModule_GetDict(m); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 565 | |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 566 | /* constants */ |
| 567 | defint(d, "STD_INPUT_HANDLE", STD_INPUT_HANDLE); |
| 568 | defint(d, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE); |
| 569 | defint(d, "STD_ERROR_HANDLE", STD_ERROR_HANDLE); |
| 570 | defint(d, "DUPLICATE_SAME_ACCESS", DUPLICATE_SAME_ACCESS); |
| 571 | defint(d, "STARTF_USESTDHANDLES", STARTF_USESTDHANDLES); |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 572 | defint(d, "STARTF_USESHOWWINDOW", STARTF_USESHOWWINDOW); |
| 573 | defint(d, "SW_HIDE", SW_HIDE); |
Tim Peters | f3250b0 | 2004-10-12 21:38:22 +0000 | [diff] [blame] | 574 | defint(d, "INFINITE", INFINITE); |
| 575 | defint(d, "WAIT_OBJECT_0", WAIT_OBJECT_0); |
| 576 | defint(d, "CREATE_NEW_CONSOLE", CREATE_NEW_CONSOLE); |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 577 | } |