| 1 |
/* |
| 2 |
* Extension module used by multiprocessing package |
| 3 |
* |
| 4 |
* multiprocessing.c |
| 5 |
* |
| 6 |
* Copyright (c) 2006-2008, R Oudkerk |
| 7 |
* Licensed to PSF under a Contributor Agreement. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "multiprocessing.h" |
| 11 |
|
| 12 |
#ifdef SCM_RIGHTS |
| 13 |
#define HAVE_FD_TRANSFER 1 |
| 14 |
#else |
| 15 |
#define HAVE_FD_TRANSFER 0 |
| 16 |
#endif |
| 17 |
|
| 18 |
/* |
| 19 |
* Function which raises exceptions based on error codes |
| 20 |
*/ |
| 21 |
|
| 22 |
PyObject * |
| 23 |
Billiard_SetError(PyObject *Type, int num) |
| 24 |
{ |
| 25 |
switch (num) { |
| 26 |
case MP_SUCCESS: |
| 27 |
break; |
| 28 |
#ifdef MS_WINDOWS |
| 29 |
case MP_STANDARD_ERROR: |
| 30 |
if (Type == NULL) |
| 31 |
Type = PyExc_OSError; |
| 32 |
PyErr_SetExcFromWindowsErr(Type, 0); |
| 33 |
break; |
| 34 |
case MP_SOCKET_ERROR: |
| 35 |
if (Type == NULL) |
| 36 |
Type = PyExc_OSError; |
| 37 |
PyErr_SetExcFromWindowsErr(Type, WSAGetLastError()); |
| 38 |
break; |
| 39 |
#else /* !MS_WINDOWS */ |
| 40 |
case MP_STANDARD_ERROR: |
| 41 |
case MP_SOCKET_ERROR: |
| 42 |
if (Type == NULL) |
| 43 |
Type = PyExc_OSError; |
| 44 |
PyErr_SetFromErrno(Type); |
| 45 |
break; |
| 46 |
#endif /* !MS_WINDOWS */ |
| 47 |
case MP_MEMORY_ERROR: |
| 48 |
PyErr_NoMemory(); |
| 49 |
break; |
| 50 |
case MP_END_OF_FILE: |
| 51 |
PyErr_SetNone(PyExc_EOFError); |
| 52 |
break; |
| 53 |
case MP_EARLY_END_OF_FILE: |
| 54 |
PyErr_SetString(PyExc_IOError, |
| 55 |
"got end of file during message"); |
| 56 |
break; |
| 57 |
case MP_BAD_MESSAGE_LENGTH: |
| 58 |
PyErr_SetString(PyExc_IOError, "bad message length"); |
| 59 |
break; |
| 60 |
case MP_EXCEPTION_HAS_BEEN_SET: |
| 61 |
break; |
| 62 |
default: |
| 63 |
PyErr_Format(PyExc_RuntimeError, |
| 64 |
"unknown error number %d", num); |
| 65 |
} |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/* |
| 71 |
* Windows only |
| 72 |
*/ |
| 73 |
|
| 74 |
#ifdef MS_WINDOWS |
| 75 |
|
| 76 |
/* On Windows we set an event to signal Ctrl-C; compare with timemodule.c */ |
| 77 |
|
| 78 |
HANDLE sigint_event = NULL; |
| 79 |
|
| 80 |
static BOOL WINAPI |
| 81 |
ProcessingCtrlHandler(DWORD dwCtrlType) |
| 82 |
{ |
| 83 |
SetEvent(sigint_event); |
| 84 |
return FALSE; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
static PyObject * |
| 89 |
Billiard_closesocket(PyObject *self, PyObject *args) |
| 90 |
{ |
| 91 |
HANDLE handle; |
| 92 |
int ret; |
| 93 |
|
| 94 |
if (!PyArg_ParseTuple(args, F_HANDLE ":closesocket" , &handle)) |
| 95 |
return NULL; |
| 96 |
|
| 97 |
Py_BEGIN_ALLOW_THREADS |
| 98 |
ret = closesocket((SOCKET) handle); |
| 99 |
Py_END_ALLOW_THREADS |
| 100 |
|
| 101 |
if (ret) |
| 102 |
return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); |
| 103 |
Py_RETURN_NONE; |
| 104 |
} |
| 105 |
|
| 106 |
static PyObject * |
| 107 |
Billiard_recv(PyObject *self, PyObject *args) |
| 108 |
{ |
| 109 |
HANDLE handle; |
| 110 |
int size, nread; |
| 111 |
PyObject *buf; |
| 112 |
|
| 113 |
if (!PyArg_ParseTuple(args, F_HANDLE "i:recv" , &handle, &size)) |
| 114 |
return NULL; |
| 115 |
|
| 116 |
buf = PyBytes_FromStringAndSize(NULL, size); |
| 117 |
if (!buf) |
| 118 |
return NULL; |
| 119 |
|
| 120 |
Py_BEGIN_ALLOW_THREADS |
| 121 |
nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0); |
| 122 |
Py_END_ALLOW_THREADS |
| 123 |
|
| 124 |
if (nread < 0) { |
| 125 |
Py_DECREF(buf); |
| 126 |
return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); |
| 127 |
} |
| 128 |
_PyBytes_Resize(&buf, nread); |
| 129 |
return buf; |
| 130 |
} |
| 131 |
|
| 132 |
static PyObject * |
| 133 |
Billiard_send(PyObject *self, PyObject *args) |
| 134 |
{ |
| 135 |
HANDLE handle; |
| 136 |
Py_buffer buf; |
| 137 |
int ret, length; |
| 138 |
|
| 139 |
if (!PyArg_ParseTuple(args, F_HANDLE "y*:send" , &handle, &buf)) |
| 140 |
return NULL; |
| 141 |
|
| 142 |
length = (int)Py_MIN(buf.len, INT_MAX); |
| 143 |
|
| 144 |
Py_BEGIN_ALLOW_THREADS |
| 145 |
ret = send((SOCKET) handle, buf.buf, length, 0); |
| 146 |
Py_END_ALLOW_THREADS |
| 147 |
|
| 148 |
PyBuffer_Release(&buf); |
| 149 |
if (ret < 0) |
| 150 |
return PyErr_SetExcFromWindowsErr(PyExc_IOError, WSAGetLastError()); |
| 151 |
return PyLong_FromLong(ret); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/* |
| 156 |
* Unix only |
| 157 |
*/ |
| 158 |
|
| 159 |
#else /* !MS_WINDOWS */ |
| 160 |
|
| 161 |
#if HAVE_FD_TRANSFER |
| 162 |
|
| 163 |
/* Functions for transferring file descriptors between processes. |
| 164 |
Reimplements some of the functionality of the fdcred |
| 165 |
module at http://www.mca-ltd.com/resources/fdcred_1.tgz. */ |
| 166 |
|
| 167 |
static PyObject * |
| 168 |
Billiard_multiprocessing_sendfd(PyObject *self, PyObject *args) |
| 169 |
{ |
| 170 |
int conn, fd, res; |
| 171 |
char dummy_char; |
| 172 |
char buf[CMSG_SPACE(sizeof(int))]; |
| 173 |
struct msghdr msg = {0}; |
| 174 |
struct iovec dummy_iov; |
| 175 |
struct cmsghdr *cmsg; |
| 176 |
|
| 177 |
if (!PyArg_ParseTuple(args, "ii", &conn, &fd)) |
| 178 |
return NULL; |
| 179 |
|
| 180 |
dummy_iov.iov_base = &dummy_char; |
| 181 |
dummy_iov.iov_len = 1; |
| 182 |
msg.msg_control = buf; |
| 183 |
msg.msg_controllen = sizeof(buf); |
| 184 |
msg.msg_iov = &dummy_iov; |
| 185 |
msg.msg_iovlen = 1; |
| 186 |
cmsg = CMSG_FIRSTHDR(&msg); |
| 187 |
cmsg->cmsg_level = SOL_SOCKET; |
| 188 |
cmsg->cmsg_type = SCM_RIGHTS; |
| 189 |
cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 190 |
msg.msg_controllen = cmsg->cmsg_len; |
| 191 |
*(int*)CMSG_DATA(cmsg) = fd; |
| 192 |
|
| 193 |
Py_BEGIN_ALLOW_THREADS |
| 194 |
res = sendmsg(conn, &msg, 0); |
| 195 |
Py_END_ALLOW_THREADS |
| 196 |
|
| 197 |
if (res < 0) |
| 198 |
return PyErr_SetFromErrno(PyExc_OSError); |
| 199 |
Py_RETURN_NONE; |
| 200 |
} |
| 201 |
|
| 202 |
static PyObject * |
| 203 |
Billiard_multiprocessing_recvfd(PyObject *self, PyObject *args) |
| 204 |
{ |
| 205 |
int conn, fd, res; |
| 206 |
char dummy_char; |
| 207 |
char buf[CMSG_SPACE(sizeof(int))]; |
| 208 |
struct msghdr msg = {0}; |
| 209 |
struct iovec dummy_iov; |
| 210 |
struct cmsghdr *cmsg; |
| 211 |
|
| 212 |
if (!PyArg_ParseTuple(args, "i", &conn)) |
| 213 |
return NULL; |
| 214 |
|
| 215 |
dummy_iov.iov_base = &dummy_char; |
| 216 |
dummy_iov.iov_len = 1; |
| 217 |
msg.msg_control = buf; |
| 218 |
msg.msg_controllen = sizeof(buf); |
| 219 |
msg.msg_iov = &dummy_iov; |
| 220 |
msg.msg_iovlen = 1; |
| 221 |
cmsg = CMSG_FIRSTHDR(&msg); |
| 222 |
cmsg->cmsg_level = SOL_SOCKET; |
| 223 |
cmsg->cmsg_type = SCM_RIGHTS; |
| 224 |
cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 225 |
msg.msg_controllen = cmsg->cmsg_len; |
| 226 |
|
| 227 |
Py_BEGIN_ALLOW_THREADS |
| 228 |
res = recvmsg(conn, &msg, 0); |
| 229 |
Py_END_ALLOW_THREADS |
| 230 |
|
| 231 |
if (res < 0) |
| 232 |
return PyErr_SetFromErrno(PyExc_OSError); |
| 233 |
|
| 234 |
fd = *(int*)CMSG_DATA(cmsg); |
| 235 |
return Py_BuildValue("i", fd); |
| 236 |
} |
| 237 |
|
| 238 |
#endif /* HAVE_FD_TRANSFER */ |
| 239 |
|
| 240 |
#endif /* !MS_WINDOWS */ |
| 241 |
|
| 242 |
|
| 243 |
/* |
| 244 |
* All platforms |
| 245 |
*/ |
| 246 |
|
| 247 |
static PyObject* |
| 248 |
Billiard_multiprocessing_address_of_buffer(PyObject *self, PyObject *obj) |
| 249 |
{ |
| 250 |
void *buffer; |
| 251 |
Py_ssize_t buffer_len; |
| 252 |
|
| 253 |
if (PyObject_AsWriteBuffer(obj, &buffer, &buffer_len) < 0) |
| 254 |
return NULL; |
| 255 |
|
| 256 |
return Py_BuildValue("N" F_PY_SSIZE_T, |
| 257 |
PyLong_FromVoidPtr(buffer), buffer_len); |
| 258 |
} |
| 259 |
|
| 260 |
#if !defined(MS_WINDOWS) |
| 261 |
|
| 262 |
static PyObject * |
| 263 |
Billiard_read(PyObject *self, PyObject *args) |
| 264 |
{ |
| 265 |
int fd; |
| 266 |
Py_buffer view; |
| 267 |
Py_ssize_t buflen, recvlen = 0; |
| 268 |
|
| 269 |
char *buf = NULL; |
| 270 |
|
| 271 |
Py_ssize_t n = 0; |
| 272 |
|
| 273 |
if (!PyArg_ParseTuple(args, "iw*|n", &fd, &view, &recvlen)) |
| 274 |
return NULL; |
| 275 |
buflen = view.len; |
| 276 |
buf = view.buf; |
| 277 |
|
| 278 |
if (recvlen < 0) { |
| 279 |
PyBuffer_Release(&view); |
| 280 |
PyErr_SetString(PyExc_ValueError, "negative len for read"); |
| 281 |
return NULL; |
| 282 |
} |
| 283 |
|
| 284 |
if (recvlen == 0) { |
| 285 |
recvlen = buflen; |
| 286 |
} |
| 287 |
|
| 288 |
if (buflen < recvlen) { |
| 289 |
PyBuffer_Release(&view); |
| 290 |
PyErr_SetString(PyExc_ValueError, |
| 291 |
"Buffer too small for requested bytes"); |
| 292 |
return NULL; |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
if (buflen < 0 || buflen == 0) { |
| 297 |
errno = EINVAL; |
| 298 |
goto bail; |
| 299 |
} |
| 300 |
// Requires Python 2.7 |
| 301 |
//if (!_PyVerify_fd(fd)) goto bail; |
| 302 |
|
| 303 |
Py_BEGIN_ALLOW_THREADS |
| 304 |
n = read(fd, buf, recvlen); |
| 305 |
Py_END_ALLOW_THREADS |
| 306 |
if (n < 0) goto bail; |
| 307 |
PyBuffer_Release(&view); |
| 308 |
return PyInt_FromSsize_t(n); |
| 309 |
|
| 310 |
bail: |
| 311 |
PyBuffer_Release(&view); |
| 312 |
return PyErr_SetFromErrno(PyExc_OSError); |
| 313 |
} |
| 314 |
|
| 315 |
# endif /* !MS_WINDOWS */ |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
/* |
| 320 |
* Function table |
| 321 |
*/ |
| 322 |
|
| 323 |
static PyMethodDef Billiard_module_methods[] = { |
| 324 |
{"address_of_buffer", Billiard_multiprocessing_address_of_buffer, METH_O, |
| 325 |
"address_of_buffer(obj) -> int\n\n" |
| 326 |
"Return address of obj assuming obj supports buffer interface"}, |
| 327 |
#if HAVE_FD_TRANSFER |
| 328 |
{"sendfd", Billiard_multiprocessing_sendfd, METH_VARARGS, |
| 329 |
"sendfd(sockfd, fd) -> None\n\n" |
| 330 |
"Send file descriptor given by fd over the unix domain socket\n" |
| 331 |
"whose file descriptor is sockfd"}, |
| 332 |
{"recvfd", Billiard_multiprocessing_recvfd, METH_VARARGS, |
| 333 |
"recvfd(sockfd) -> fd\n\n" |
| 334 |
"Receive a file descriptor over a unix domain socket\n" |
| 335 |
"whose file descriptor is sockfd"}, |
| 336 |
#endif |
| 337 |
#if !defined(MS_WINDOWS) |
| 338 |
{"read", Billiard_read, METH_VARARGS, |
| 339 |
"read(fd, buffer) -> bytes\n\n" |
| 340 |
"Read from file descriptor into buffer."}, |
| 341 |
#endif |
| 342 |
#ifdef MS_WINDOWS |
| 343 |
{"closesocket", Billiard_closesocket, METH_VARARGS, ""}, |
| 344 |
{"recv", Billiard_recv, METH_VARARGS, ""}, |
| 345 |
{"send", Billiard_send, METH_VARARGS, ""}, |
| 346 |
#endif |
| 347 |
#ifndef POSIX_SEMAPHORES_NOT_ENABLED |
| 348 |
{"sem_unlink", Billiard_semlock_unlink, METH_VARARGS, ""}, |
| 349 |
#endif |
| 350 |
{NULL} |
| 351 |
}; |
| 352 |
|
| 353 |
|
| 354 |
/* |
| 355 |
* Initialize |
| 356 |
*/ |
| 357 |
|
| 358 |
PyMODINIT_FUNC |
| 359 |
init_billiard(void) |
| 360 |
{ |
| 361 |
PyObject *module, *temp, *value; |
| 362 |
|
| 363 |
/* Initialize module */ |
| 364 |
module = Py_InitModule("_billiard", Billiard_module_methods); |
| 365 |
if (!module) |
| 366 |
return; |
| 367 |
|
| 368 |
#if defined(MS_WINDOWS) || \ |
| 369 |
(defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)) |
| 370 |
/* Add SemLock type to module */ |
| 371 |
if (PyType_Ready(&BilliardSemLockType) < 0) |
| 372 |
return; |
| 373 |
Py_INCREF(&BilliardSemLockType); |
| 374 |
PyDict_SetItemString(BilliardSemLockType.tp_dict, "SEM_VALUE_MAX", |
| 375 |
Py_BuildValue("i", SEM_VALUE_MAX)); |
| 376 |
PyModule_AddObject(module, "SemLock", (PyObject*)&BilliardSemLockType); |
| 377 |
#endif |
| 378 |
|
| 379 |
#ifdef MS_WINDOWS |
| 380 |
/* Initialize the event handle used to signal Ctrl-C */ |
| 381 |
sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 382 |
if (!sigint_event) { |
| 383 |
PyErr_SetFromWindowsErr(0); |
| 384 |
return; |
| 385 |
} |
| 386 |
if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) { |
| 387 |
PyErr_SetFromWindowsErr(0); |
| 388 |
return; |
| 389 |
} |
| 390 |
#endif |
| 391 |
|
| 392 |
/* Add configuration macros */ |
| 393 |
temp = PyDict_New(); |
| 394 |
if (!temp) |
| 395 |
return; |
| 396 |
#define ADD_FLAG(name) \ |
| 397 |
value = Py_BuildValue("i", name); \ |
| 398 |
if (value == NULL) { Py_DECREF(temp); return; } \ |
| 399 |
if (PyDict_SetItemString(temp, #name, value) < 0) { \ |
| 400 |
Py_DECREF(temp); Py_DECREF(value); return; } \ |
| 401 |
Py_DECREF(value) |
| 402 |
|
| 403 |
#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) |
| 404 |
ADD_FLAG(HAVE_SEM_OPEN); |
| 405 |
#endif |
| 406 |
#ifdef HAVE_SEM_TIMEDWAIT |
| 407 |
ADD_FLAG(HAVE_SEM_TIMEDWAIT); |
| 408 |
#endif |
| 409 |
#ifdef HAVE_FD_TRANSFER |
| 410 |
ADD_FLAG(HAVE_FD_TRANSFER); |
| 411 |
#endif |
| 412 |
#ifdef HAVE_BROKEN_SEM_GETVALUE |
| 413 |
ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE); |
| 414 |
#endif |
| 415 |
#ifdef HAVE_BROKEN_SEM_UNLINK |
| 416 |
ADD_FLAG(HAVE_BROKEN_SEM_UNLINK); |
| 417 |
#endif |
| 418 |
if (PyModule_AddObject(module, "flags", temp) < 0) |
| 419 |
return; |
| 420 |
} |