blob: bc635da74da7553ce5eb750b44ada96e38b7202b [file] [log] [blame]
Benjamin Petersonfa268032008-06-13 19:28:21 +00001/*
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 Peterson965ce872009-04-05 21:24:58 +000011#ifdef SCM_RIGHTS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000012 #define HAVE_FD_TRANSFER 1
Benjamin Peterson965ce872009-04-05 21:24:58 +000013#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 #define HAVE_FD_TRANSFER 0
Benjamin Peterson965ce872009-04-05 21:24:58 +000015#endif
16
Benjamin Petersonfa268032008-06-13 19:28:21 +000017PyObject *create_win32_namespace(void);
18
19PyObject *pickle_dumps, *pickle_loads, *pickle_protocol;
20PyObject *ProcessError, *BufferTooShort;
21
22/*
23 * Function which raises exceptions based on error codes
24 */
25
26PyObject *
27mp_SetError(PyObject *Type, int num)
28{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 switch (num) {
Benjamin Petersonfa268032008-06-13 19:28:21 +000030#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 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 Petersonfa268032008-06-13 19:28:21 +000041#else /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 case MP_STANDARD_ERROR:
43 case MP_SOCKET_ERROR:
44 if (Type == NULL)
45 Type = PyExc_OSError;
46 PyErr_SetFromErrno(Type);
47 break;
Benjamin Petersonfa268032008-06-13 19:28:21 +000048#endif /* !MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 case MP_MEMORY_ERROR:
50 PyErr_NoMemory();
51 break;
52 case MP_END_OF_FILE:
53 PyErr_SetNone(PyExc_EOFError);
54 break;
55 case MP_EARLY_END_OF_FILE:
56 PyErr_SetString(PyExc_IOError,
57 "got end of file during message");
58 break;
59 case MP_BAD_MESSAGE_LENGTH:
60 PyErr_SetString(PyExc_IOError, "bad message length");
61 break;
62 case MP_EXCEPTION_HAS_BEEN_SET:
63 break;
64 default:
65 PyErr_Format(PyExc_RuntimeError,
66 "unkown error number %d", num);
67 }
68 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +000069}
70
71
72/*
73 * Windows only
74 */
75
76#ifdef MS_WINDOWS
77
78/* On Windows we set an event to signal Ctrl-C; compare with timemodule.c */
79
80HANDLE sigint_event = NULL;
81
82static BOOL WINAPI
83ProcessingCtrlHandler(DWORD dwCtrlType)
84{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 SetEvent(sigint_event);
86 return FALSE;
Benjamin Petersonfa268032008-06-13 19:28:21 +000087}
88
89/*
90 * Unix only
91 */
92
93#else /* !MS_WINDOWS */
94
95#if HAVE_FD_TRANSFER
96
97/* Functions for transferring file descriptors between processes.
98 Reimplements some of the functionality of the fdcred
99 module at http://www.mca-ltd.com/resources/fdcred_1.tgz. */
Jesus Ceaab7060e2011-09-10 04:04:09 +0200100/* Based in http://resin.csoft.net/cgi-bin/man.cgi?section=3&topic=CMSG_DATA */
Benjamin Petersonfa268032008-06-13 19:28:21 +0000101
102static PyObject *
103multiprocessing_sendfd(PyObject *self, PyObject *args)
104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 int conn, fd, res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 struct iovec dummy_iov;
Jesus Ceaab7060e2011-09-10 04:04:09 +0200107 char dummy_char;
108 struct msghdr msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 struct cmsghdr *cmsg;
Jesus Ceaab7060e2011-09-10 04:04:09 +0200110 union {
111 struct cmsghdr hdr;
112 unsigned char buf[CMSG_SPACE(sizeof(int))];
113 } cmsgbuf;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 if (!PyArg_ParseTuple(args, "ii", &conn, &fd))
116 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 dummy_iov.iov_base = &dummy_char;
119 dummy_iov.iov_len = 1;
Jesus Ceaab7060e2011-09-10 04:04:09 +0200120
121 memset(&msg, 0, sizeof(msg));
122 msg.msg_control = &cmsgbuf.buf;
123 msg.msg_controllen = sizeof(cmsgbuf.buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 msg.msg_iov = &dummy_iov;
125 msg.msg_iovlen = 1;
Jesus Ceaab7060e2011-09-10 04:04:09 +0200126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 cmsg = CMSG_FIRSTHDR(&msg);
Jesus Ceaab7060e2011-09-10 04:04:09 +0200128 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 cmsg->cmsg_level = SOL_SOCKET;
130 cmsg->cmsg_type = SCM_RIGHTS;
Antoine Pitroubcb39d42011-08-23 19:46:22 +0200131 * (int *) CMSG_DATA(cmsg) = fd;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 Py_BEGIN_ALLOW_THREADS
134 res = sendmsg(conn, &msg, 0);
135 Py_END_ALLOW_THREADS
Benjamin Petersonfa268032008-06-13 19:28:21 +0000136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 if (res < 0)
138 return PyErr_SetFromErrno(PyExc_OSError);
139 Py_RETURN_NONE;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000140}
141
142static PyObject *
143multiprocessing_recvfd(PyObject *self, PyObject *args)
144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 int conn, fd, res;
146 char dummy_char;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 struct iovec dummy_iov;
Jesus Ceaab7060e2011-09-10 04:04:09 +0200148 struct msghdr msg = {0};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 struct cmsghdr *cmsg;
Jesus Ceaab7060e2011-09-10 04:04:09 +0200150 union {
151 struct cmsghdr hdr;
152 unsigned char buf[CMSG_SPACE(sizeof(int))];
153 } cmsgbuf;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 if (!PyArg_ParseTuple(args, "i", &conn))
156 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 dummy_iov.iov_base = &dummy_char;
159 dummy_iov.iov_len = 1;
Jesus Ceaab7060e2011-09-10 04:04:09 +0200160
161 memset(&msg, 0, sizeof(msg));
162 msg.msg_control = &cmsgbuf.buf;
163 msg.msg_controllen = sizeof(cmsgbuf.buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 msg.msg_iov = &dummy_iov;
165 msg.msg_iovlen = 1;
Jesus Ceaab7060e2011-09-10 04:04:09 +0200166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 cmsg = CMSG_FIRSTHDR(&msg);
168 cmsg->cmsg_level = SOL_SOCKET;
169 cmsg->cmsg_type = SCM_RIGHTS;
170 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
171 msg.msg_controllen = cmsg->cmsg_len;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 Py_BEGIN_ALLOW_THREADS
174 res = recvmsg(conn, &msg, 0);
175 Py_END_ALLOW_THREADS
Benjamin Petersonfa268032008-06-13 19:28:21 +0000176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 if (res < 0)
178 return PyErr_SetFromErrno(PyExc_OSError);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000179
Antoine Pitroubcb39d42011-08-23 19:46:22 +0200180 fd = * (int *) CMSG_DATA(cmsg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 return Py_BuildValue("i", fd);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000182}
183
184#endif /* HAVE_FD_TRANSFER */
185
186#endif /* !MS_WINDOWS */
187
188
189/*
190 * All platforms
191 */
192
193static PyObject*
194multiprocessing_address_of_buffer(PyObject *self, PyObject *obj)
195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 void *buffer;
197 Py_ssize_t buffer_len;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0)
200 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return Py_BuildValue("N" F_PY_SSIZE_T,
203 PyLong_FromVoidPtr(buffer), buffer_len);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000204}
205
206
207/*
208 * Function table
209 */
210
211static PyMethodDef module_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 {"address_of_buffer", multiprocessing_address_of_buffer, METH_O,
213 "address_of_buffer(obj) -> int\n"
214 "Return address of obj assuming obj supports buffer inteface"},
Benjamin Petersonfa268032008-06-13 19:28:21 +0000215#if HAVE_FD_TRANSFER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 {"sendfd", multiprocessing_sendfd, METH_VARARGS,
217 "sendfd(sockfd, fd) -> None\n"
218 "Send file descriptor given by fd over the unix domain socket\n"
219 "whose file decriptor is sockfd"},
220 {"recvfd", multiprocessing_recvfd, METH_VARARGS,
221 "recvfd(sockfd) -> fd\n"
222 "Receive a file descriptor over a unix domain socket\n"
223 "whose file decriptor is sockfd"},
Benjamin Petersonfa268032008-06-13 19:28:21 +0000224#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 {NULL}
Benjamin Petersonfa268032008-06-13 19:28:21 +0000226};
227
228
229/*
230 * Initialize
231 */
232
233static struct PyModuleDef multiprocessing_module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 PyModuleDef_HEAD_INIT,
235 "_multiprocessing",
236 NULL,
237 -1,
238 module_methods,
239 NULL,
240 NULL,
241 NULL,
242 NULL
Benjamin Petersonfa268032008-06-13 19:28:21 +0000243};
244
245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246PyMODINIT_FUNC
Benjamin Petersonfa268032008-06-13 19:28:21 +0000247PyInit__multiprocessing(void)
248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 PyObject *module, *temp, *value;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 /* Initialize module */
252 module = PyModule_Create(&multiprocessing_module);
253 if (!module)
254 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 /* Get copy of objects from pickle */
257 temp = PyImport_ImportModule(PICKLE_MODULE);
258 if (!temp)
259 return NULL;
260 pickle_dumps = PyObject_GetAttrString(temp, "dumps");
261 pickle_loads = PyObject_GetAttrString(temp, "loads");
262 pickle_protocol = PyObject_GetAttrString(temp, "HIGHEST_PROTOCOL");
263 Py_XDECREF(temp);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 /* Get copy of BufferTooShort */
266 temp = PyImport_ImportModule("multiprocessing");
267 if (!temp)
268 return NULL;
269 BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort");
270 Py_XDECREF(temp);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* Add connection type to module */
273 if (PyType_Ready(&ConnectionType) < 0)
274 return NULL;
275 Py_INCREF(&ConnectionType);
276 PyModule_AddObject(module, "Connection", (PyObject*)&ConnectionType);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278#if defined(MS_WINDOWS) || \
Mark Dickinsona614f042009-11-28 12:48:43 +0000279 (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 /* Add SemLock type to module */
281 if (PyType_Ready(&SemLockType) < 0)
282 return NULL;
283 Py_INCREF(&SemLockType);
Gregory P. Smith68e01352010-10-17 02:14:36 +0000284 {
285 PyObject *py_sem_value_max;
286 /* Some systems define SEM_VALUE_MAX as an unsigned value that
287 * causes it to be negative when used as an int (NetBSD). */
288 if ((int)(SEM_VALUE_MAX) < 0)
289 py_sem_value_max = PyLong_FromLong(INT_MAX);
290 else
291 py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
292 if (py_sem_value_max == NULL)
293 return NULL;
294 PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX",
295 py_sem_value_max);
296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000298#endif
299
300#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 /* Add PipeConnection to module */
302 if (PyType_Ready(&PipeConnectionType) < 0)
303 return NULL;
304 Py_INCREF(&PipeConnectionType);
305 PyModule_AddObject(module, "PipeConnection",
306 (PyObject*)&PipeConnectionType);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 /* Initialize win32 class and add to multiprocessing */
309 temp = create_win32_namespace();
310 if (!temp)
311 return NULL;
312 PyModule_AddObject(module, "win32", temp);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 /* Initialize the event handle used to signal Ctrl-C */
315 sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL);
316 if (!sigint_event) {
317 PyErr_SetFromWindowsErr(0);
318 return NULL;
319 }
320 if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) {
321 PyErr_SetFromWindowsErr(0);
322 return NULL;
323 }
Benjamin Petersonfa268032008-06-13 19:28:21 +0000324#endif
325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 /* Add configuration macros */
327 temp = PyDict_New();
328 if (!temp)
329 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331#define ADD_FLAG(name) \
332 value = Py_BuildValue("i", name); \
333 if (value == NULL) { Py_DECREF(temp); return NULL; } \
334 if (PyDict_SetItemString(temp, #name, value) < 0) { \
335 Py_DECREF(temp); Py_DECREF(value); return NULL; } \
336 Py_DECREF(value)
337
Mark Dickinsona614f042009-11-28 12:48:43 +0000338#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 ADD_FLAG(HAVE_SEM_OPEN);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000340#endif
341#ifdef HAVE_SEM_TIMEDWAIT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 ADD_FLAG(HAVE_SEM_TIMEDWAIT);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000343#endif
344#ifdef HAVE_FD_TRANSFER
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 ADD_FLAG(HAVE_FD_TRANSFER);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000346#endif
347#ifdef HAVE_BROKEN_SEM_GETVALUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000349#endif
350#ifdef HAVE_BROKEN_SEM_UNLINK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
Benjamin Petersonfa268032008-06-13 19:28:21 +0000352#endif
353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 if (PyModule_AddObject(module, "flags", temp) < 0)
355 return NULL;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return module;
Benjamin Petersonfa268032008-06-13 19:28:21 +0000358}