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