Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Extension module used by multiprocessing package |
| 3 | * |
| 4 | * multiprocessing.c |
| 5 | * |
| 6 | * Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt |
| 7 | */ |
| 8 | |
| 9 | #include "multiprocessing.h" |
| 10 | |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 11 | #ifdef SCM_RIGHTS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 12 | #define HAVE_FD_TRANSFER 1 |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 13 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 14 | #define HAVE_FD_TRANSFER 0 |
Benjamin Peterson | 965ce87 | 2009-04-05 21:24:58 +0000 | [diff] [blame] | 15 | #endif |
| 16 | |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 17 | PyObject *create_win32_namespace(void); |
| 18 | |
| 19 | PyObject *pickle_dumps, *pickle_loads, *pickle_protocol; |
| 20 | PyObject *ProcessError, *BufferTooShort; |
| 21 | |
| 22 | /* |
| 23 | * Function which raises exceptions based on error codes |
| 24 | */ |
| 25 | |
| 26 | PyObject * |
| 27 | mp_SetError(PyObject *Type, int num) |
| 28 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | switch (num) { |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 30 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | case MP_STANDARD_ERROR: |
| 32 | if (Type == NULL) |
| 33 | Type = PyExc_WindowsError; |
| 34 | PyErr_SetExcFromWindowsErr(Type, 0); |
| 35 | break; |
| 36 | case MP_SOCKET_ERROR: |
| 37 | if (Type == NULL) |
| 38 | Type = PyExc_WindowsError; |
| 39 | PyErr_SetExcFromWindowsErr(Type, WSAGetLastError()); |
| 40 | break; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 41 | #else /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | case MP_STANDARD_ERROR: |
| 43 | case MP_SOCKET_ERROR: |
| 44 | if (Type == NULL) |
| 45 | Type = PyExc_OSError; |
| 46 | PyErr_SetFromErrno(Type); |
| 47 | break; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 48 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | case MP_MEMORY_ERROR: |
| 50 | PyErr_NoMemory(); |
| 51 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | case MP_EXCEPTION_HAS_BEEN_SET: |
| 53 | break; |
| 54 | default: |
| 55 | PyErr_Format(PyExc_RuntimeError, |
| 56 | "unkown error number %d", num); |
| 57 | } |
| 58 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
| 62 | /* |
| 63 | * Windows only |
| 64 | */ |
| 65 | |
| 66 | #ifdef MS_WINDOWS |
| 67 | |
| 68 | /* On Windows we set an event to signal Ctrl-C; compare with timemodule.c */ |
| 69 | |
| 70 | HANDLE sigint_event = NULL; |
| 71 | |
| 72 | static BOOL WINAPI |
| 73 | ProcessingCtrlHandler(DWORD dwCtrlType) |
| 74 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | SetEvent(sigint_event); |
| 76 | return FALSE; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Unix only |
| 81 | */ |
| 82 | |
| 83 | #else /* !MS_WINDOWS */ |
| 84 | |
| 85 | #if HAVE_FD_TRANSFER |
| 86 | |
| 87 | /* Functions for transferring file descriptors between processes. |
| 88 | Reimplements some of the functionality of the fdcred |
| 89 | module at http://www.mca-ltd.com/resources/fdcred_1.tgz. */ |
| 90 | |
| 91 | static PyObject * |
| 92 | multiprocessing_sendfd(PyObject *self, PyObject *args) |
| 93 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | int conn, fd, res; |
| 95 | char dummy_char; |
| 96 | char buf[CMSG_SPACE(sizeof(int))]; |
| 97 | struct msghdr msg = {0}; |
| 98 | struct iovec dummy_iov; |
| 99 | struct cmsghdr *cmsg; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | if (!PyArg_ParseTuple(args, "ii", &conn, &fd)) |
| 102 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | dummy_iov.iov_base = &dummy_char; |
| 105 | dummy_iov.iov_len = 1; |
| 106 | msg.msg_control = buf; |
| 107 | msg.msg_controllen = sizeof(buf); |
| 108 | msg.msg_iov = &dummy_iov; |
| 109 | msg.msg_iovlen = 1; |
| 110 | cmsg = CMSG_FIRSTHDR(&msg); |
| 111 | cmsg->cmsg_level = SOL_SOCKET; |
| 112 | cmsg->cmsg_type = SCM_RIGHTS; |
| 113 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 114 | msg.msg_controllen = cmsg->cmsg_len; |
Benjamin Peterson | 223f091 | 2010-10-17 21:12:18 +0000 | [diff] [blame] | 115 | *CMSG_DATA(cmsg) = fd; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 116 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | Py_BEGIN_ALLOW_THREADS |
| 118 | res = sendmsg(conn, &msg, 0); |
| 119 | Py_END_ALLOW_THREADS |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | if (res < 0) |
| 122 | return PyErr_SetFromErrno(PyExc_OSError); |
| 123 | Py_RETURN_NONE; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static PyObject * |
| 127 | multiprocessing_recvfd(PyObject *self, PyObject *args) |
| 128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | int conn, fd, res; |
| 130 | char dummy_char; |
| 131 | char buf[CMSG_SPACE(sizeof(int))]; |
| 132 | struct msghdr msg = {0}; |
| 133 | struct iovec dummy_iov; |
| 134 | struct cmsghdr *cmsg; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | if (!PyArg_ParseTuple(args, "i", &conn)) |
| 137 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | dummy_iov.iov_base = &dummy_char; |
| 140 | dummy_iov.iov_len = 1; |
| 141 | msg.msg_control = buf; |
| 142 | msg.msg_controllen = sizeof(buf); |
| 143 | msg.msg_iov = &dummy_iov; |
| 144 | msg.msg_iovlen = 1; |
| 145 | cmsg = CMSG_FIRSTHDR(&msg); |
| 146 | cmsg->cmsg_level = SOL_SOCKET; |
| 147 | cmsg->cmsg_type = SCM_RIGHTS; |
| 148 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 149 | msg.msg_controllen = cmsg->cmsg_len; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | Py_BEGIN_ALLOW_THREADS |
| 152 | res = recvmsg(conn, &msg, 0); |
| 153 | Py_END_ALLOW_THREADS |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | if (res < 0) |
| 156 | return PyErr_SetFromErrno(PyExc_OSError); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 157 | |
Benjamin Peterson | 223f091 | 2010-10-17 21:12:18 +0000 | [diff] [blame] | 158 | fd = *CMSG_DATA(cmsg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | return Py_BuildValue("i", fd); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | #endif /* HAVE_FD_TRANSFER */ |
| 163 | |
| 164 | #endif /* !MS_WINDOWS */ |
| 165 | |
| 166 | |
| 167 | /* |
| 168 | * All platforms |
| 169 | */ |
| 170 | |
| 171 | static PyObject* |
| 172 | multiprocessing_address_of_buffer(PyObject *self, PyObject *obj) |
| 173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | void *buffer; |
| 175 | Py_ssize_t buffer_len; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0) |
| 178 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | return Py_BuildValue("N" F_PY_SSIZE_T, |
| 181 | PyLong_FromVoidPtr(buffer), buffer_len); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
| 185 | /* |
| 186 | * Function table |
| 187 | */ |
| 188 | |
| 189 | static PyMethodDef module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | {"address_of_buffer", multiprocessing_address_of_buffer, METH_O, |
| 191 | "address_of_buffer(obj) -> int\n" |
| 192 | "Return address of obj assuming obj supports buffer inteface"}, |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 193 | #if HAVE_FD_TRANSFER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | {"sendfd", multiprocessing_sendfd, METH_VARARGS, |
| 195 | "sendfd(sockfd, fd) -> None\n" |
| 196 | "Send file descriptor given by fd over the unix domain socket\n" |
| 197 | "whose file decriptor is sockfd"}, |
| 198 | {"recvfd", multiprocessing_recvfd, METH_VARARGS, |
| 199 | "recvfd(sockfd) -> fd\n" |
| 200 | "Receive a file descriptor over a unix domain socket\n" |
| 201 | "whose file decriptor is sockfd"}, |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 202 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 203 | {NULL} |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | * Initialize |
| 209 | */ |
| 210 | |
| 211 | static struct PyModuleDef multiprocessing_module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | PyModuleDef_HEAD_INIT, |
| 213 | "_multiprocessing", |
| 214 | NULL, |
| 215 | -1, |
| 216 | module_methods, |
| 217 | NULL, |
| 218 | NULL, |
| 219 | NULL, |
| 220 | NULL |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | PyMODINIT_FUNC |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 225 | PyInit__multiprocessing(void) |
| 226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | PyObject *module, *temp, *value; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | /* Initialize module */ |
| 230 | module = PyModule_Create(&multiprocessing_module); |
| 231 | if (!module) |
| 232 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | /* Get copy of objects from pickle */ |
| 235 | temp = PyImport_ImportModule(PICKLE_MODULE); |
| 236 | if (!temp) |
| 237 | return NULL; |
| 238 | pickle_dumps = PyObject_GetAttrString(temp, "dumps"); |
| 239 | pickle_loads = PyObject_GetAttrString(temp, "loads"); |
| 240 | pickle_protocol = PyObject_GetAttrString(temp, "HIGHEST_PROTOCOL"); |
| 241 | Py_XDECREF(temp); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | /* Get copy of BufferTooShort */ |
| 244 | temp = PyImport_ImportModule("multiprocessing"); |
| 245 | if (!temp) |
| 246 | return NULL; |
| 247 | BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort"); |
| 248 | Py_XDECREF(temp); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | #if defined(MS_WINDOWS) || \ |
Mark Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 251 | (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | /* Add SemLock type to module */ |
| 253 | if (PyType_Ready(&SemLockType) < 0) |
| 254 | return NULL; |
| 255 | Py_INCREF(&SemLockType); |
Gregory P. Smith | 68e0135 | 2010-10-17 02:14:36 +0000 | [diff] [blame] | 256 | { |
| 257 | PyObject *py_sem_value_max; |
| 258 | /* Some systems define SEM_VALUE_MAX as an unsigned value that |
| 259 | * causes it to be negative when used as an int (NetBSD). */ |
| 260 | if ((int)(SEM_VALUE_MAX) < 0) |
| 261 | py_sem_value_max = PyLong_FromLong(INT_MAX); |
| 262 | else |
| 263 | py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX); |
| 264 | if (py_sem_value_max == NULL) |
| 265 | return NULL; |
| 266 | PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX", |
| 267 | py_sem_value_max); |
| 268 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 270 | #endif |
| 271 | |
| 272 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | /* Initialize win32 class and add to multiprocessing */ |
| 274 | temp = create_win32_namespace(); |
| 275 | if (!temp) |
| 276 | return NULL; |
| 277 | PyModule_AddObject(module, "win32", temp); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | /* Initialize the event handle used to signal Ctrl-C */ |
| 280 | sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 281 | if (!sigint_event) { |
| 282 | PyErr_SetFromWindowsErr(0); |
| 283 | return NULL; |
| 284 | } |
| 285 | if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) { |
| 286 | PyErr_SetFromWindowsErr(0); |
| 287 | return NULL; |
| 288 | } |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 289 | #endif |
| 290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | /* Add configuration macros */ |
| 292 | temp = PyDict_New(); |
| 293 | if (!temp) |
| 294 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 296 | #define ADD_FLAG(name) \ |
| 297 | value = Py_BuildValue("i", name); \ |
| 298 | if (value == NULL) { Py_DECREF(temp); return NULL; } \ |
| 299 | if (PyDict_SetItemString(temp, #name, value) < 0) { \ |
| 300 | Py_DECREF(temp); Py_DECREF(value); return NULL; } \ |
| 301 | Py_DECREF(value) |
| 302 | |
Mark Dickinson | a614f04 | 2009-11-28 12:48:43 +0000 | [diff] [blame] | 303 | #if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | ADD_FLAG(HAVE_SEM_OPEN); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 305 | #endif |
| 306 | #ifdef HAVE_SEM_TIMEDWAIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | ADD_FLAG(HAVE_SEM_TIMEDWAIT); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 308 | #endif |
| 309 | #ifdef HAVE_FD_TRANSFER |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | ADD_FLAG(HAVE_FD_TRANSFER); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 311 | #endif |
| 312 | #ifdef HAVE_BROKEN_SEM_GETVALUE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 314 | #endif |
| 315 | #ifdef HAVE_BROKEN_SEM_UNLINK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | ADD_FLAG(HAVE_BROKEN_SEM_UNLINK); |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 317 | #endif |
| 318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | if (PyModule_AddObject(module, "flags", temp) < 0) |
| 320 | return NULL; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | return module; |
Benjamin Peterson | fa26803 | 2008-06-13 19:28:21 +0000 | [diff] [blame] | 323 | } |