blob: 168197bfccd9071eb4b720436d9a220d65143f11 [file] [log] [blame]
Guido van Rossum4f0fbf81996-06-12 04:22:53 +00001/* select - Module containing unix select(2) call.
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002 Under Unix, the file descriptors are small integers.
3 Under Win32, select only exists for sockets, and sockets may
4 have any value except INVALID_SOCKET.
Guido van Rossum4f0fbf81996-06-12 04:22:53 +00005*/
Guido van Rossumed233a51992-06-23 09:07:03 +00006
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00007#include "Python.h"
Guido van Rossumed233a51992-06-23 09:07:03 +00008
Thomas Wouters477c8d52006-05-27 19:21:47 +00009#ifdef __APPLE__
10 /* Perform runtime testing for a broken poll on OSX to make it easier
11 * to use the same binary on multiple releases of the OS.
12 */
13#undef HAVE_BROKEN_POLL
14#endif
15
Tim Petersd92dfe02000-12-12 01:18:41 +000016/* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
17 64 is too small (too many people have bumped into that limit).
18 Here we boost it.
19 Users who want even more than the boosted limit should #define
20 FD_SETSIZE higher before this; e.g., via compiler /D switch.
21*/
22#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
23#define FD_SETSIZE 512
24#endif
25
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000026#if defined(HAVE_POLL_H)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000027#include <poll.h>
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000028#elif defined(HAVE_SYS_POLL_H)
29#include <sys/poll.h>
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000030#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000031
Guido van Rossum37273171996-12-09 18:47:43 +000032#ifdef __sgi
33/* This is missing from unistd.h */
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000034extern void bzero(void *, int);
Guido van Rossum37273171996-12-09 18:47:43 +000035#endif
36
Thomas Wouters0e3f5912006-08-11 14:57:12 +000037#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000038#include <sys/types.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000039#endif
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000040
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000041#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000042#include <sys/time.h>
43#include <utils.h>
44#endif
45
Guido van Rossum6f489d91996-06-28 20:15:15 +000046#ifdef MS_WINDOWS
Christian Heimesc36625b2008-01-04 13:33:00 +000047# define WIN32_LEAN_AND_MEAN
Thomas Wouters0e3f5912006-08-11 14:57:12 +000048# include <winsock.h>
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000049#else
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050# define SOCKET int
Skip Montanaroeb33e5a2007-08-17 12:57:41 +000051# if defined(__VMS)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052# include <socket.h>
53# endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000054#endif
Guido van Rossumed233a51992-06-23 09:07:03 +000055
Guido van Rossum1ca8bb32001-03-02 06:28:17 +000056
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000057static PyObject *SelectError;
Guido van Rossumed233a51992-06-23 09:07:03 +000058
Barry Warsawc1cb3601996-12-12 22:16:21 +000059/* list of Python objects and their file descriptor */
60typedef struct {
61 PyObject *obj; /* owned reference */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000062 SOCKET fd;
Barry Warsawc1cb3601996-12-12 22:16:21 +000063 int sentinel; /* -1 == sentinel */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000064} pylist;
65
Barry Warsawc1cb3601996-12-12 22:16:21 +000066static void
Tim Peters4b046c22001-08-16 21:59:46 +000067reap_obj(pylist fd2obj[FD_SETSIZE + 1])
Barry Warsawc1cb3601996-12-12 22:16:21 +000068{
69 int i;
Tim Peters4b046c22001-08-16 21:59:46 +000070 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
Barry Warsawc1cb3601996-12-12 22:16:21 +000071 Py_XDECREF(fd2obj[i].obj);
72 fd2obj[i].obj = NULL;
73 }
74 fd2obj[0].sentinel = -1;
75}
76
77
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000078/* returns -1 and sets the Python exception if an error occurred, otherwise
79 returns a number >= 0
80*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000081static int
Brett Cannon62dba4c2003-09-10 19:37:42 +000082seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +000083{
Barry Warsawc1cb3601996-12-12 22:16:21 +000084 int i;
85 int max = -1;
86 int index = 0;
Brett Cannon62dba4c2003-09-10 19:37:42 +000087 int len = -1;
88 PyObject* fast_seq = NULL;
Barry Warsawc1cb3601996-12-12 22:16:21 +000089 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000090
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000091 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000092 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000093
Brett Cannon62dba4c2003-09-10 19:37:42 +000094 fast_seq=PySequence_Fast(seq, "arguments 1-3 must be sequences");
95 if (!fast_seq)
96 return -1;
97
98 len = PySequence_Fast_GET_SIZE(fast_seq);
99
Barry Warsawc1cb3601996-12-12 22:16:21 +0000100 for (i = 0; i < len; i++) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000101 SOCKET v;
102
103 /* any intervening fileno() calls could decr this refcnt */
Brett Cannon62dba4c2003-09-10 19:37:42 +0000104 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
Barry Warsaw529fcfe1996-12-16 18:15:34 +0000105 return -1;
Barry Warsaw24c4b3d1996-12-13 23:22:42 +0000106
Barry Warsawc1cb3601996-12-12 22:16:21 +0000107 Py_INCREF(o);
Andrew M. Kuchling9f28a032000-07-13 23:59:35 +0000108 v = PyObject_AsFileDescriptor( o );
109 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000110
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000111#if defined(_MSC_VER)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000112 max = 0; /* not used for Win32 */
113#else /* !_MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000114 if (v < 0 || v >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000115 PyErr_SetString(PyExc_ValueError,
116 "filedescriptor out of range in select()");
117 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000118 }
119 if (v > max)
120 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000121#endif /* _MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000122 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000123
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000124 /* add object and its file descriptor to the list */
125 if (index >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000126 PyErr_SetString(PyExc_ValueError,
127 "too many file descriptors in select()");
128 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000129 }
130 fd2obj[index].obj = o;
131 fd2obj[index].fd = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000132 fd2obj[index].sentinel = 0;
133 fd2obj[++index].sentinel = -1;
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000134 }
Brett Cannon62dba4c2003-09-10 19:37:42 +0000135 Py_DECREF(fast_seq);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000136 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000137
138 finally:
139 Py_XDECREF(o);
Brett Cannon62dba4c2003-09-10 19:37:42 +0000140 Py_DECREF(fast_seq);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000141 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000142}
143
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000144/* returns NULL and sets the Python exception if an error occurred */
145static PyObject *
Tim Peters4b046c22001-08-16 21:59:46 +0000146set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +0000147{
Barry Warsawc1cb3601996-12-12 22:16:21 +0000148 int i, j, count=0;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000149 PyObject *list, *o;
150 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000151
Barry Warsawc1cb3601996-12-12 22:16:21 +0000152 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000153 if (FD_ISSET(fd2obj[j].fd, set))
Barry Warsawc1cb3601996-12-12 22:16:21 +0000154 count++;
155 }
156 list = PyList_New(count);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000157 if (!list)
158 return NULL;
159
Barry Warsawc1cb3601996-12-12 22:16:21 +0000160 i = 0;
161 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000162 fd = fd2obj[j].fd;
163 if (FD_ISSET(fd, set)) {
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000164#ifndef _MSC_VER
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000165 if (fd > FD_SETSIZE) {
166 PyErr_SetString(PyExc_SystemError,
167 "filedescriptor out of range returned in select()");
Barry Warsawc1cb3601996-12-12 22:16:21 +0000168 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000169 }
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000170#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000171 o = fd2obj[j].obj;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000172 fd2obj[j].obj = NULL;
173 /* transfer ownership */
174 if (PyList_SetItem(list, i, o) < 0)
175 goto finally;
176
177 i++;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000178 }
179 }
180 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000181 finally:
182 Py_DECREF(list);
183 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000184}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000185
Barry Warsawb44740f2001-08-16 16:52:59 +0000186#undef SELECT_USES_HEAP
187#if FD_SETSIZE > 1024
188#define SELECT_USES_HEAP
189#endif /* FD_SETSIZE > 1024 */
190
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000191static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000192select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000193{
Barry Warsawb44740f2001-08-16 16:52:59 +0000194#ifdef SELECT_USES_HEAP
Guido van Rossumd20781b1998-07-02 02:53:36 +0000195 pylist *rfd2obj, *wfd2obj, *efd2obj;
Barry Warsawb44740f2001-08-16 16:52:59 +0000196#else /* !SELECT_USES_HEAP */
Tim Peters4b046c22001-08-16 21:59:46 +0000197 /* XXX: All this should probably be implemented as follows:
Barry Warsawb44740f2001-08-16 16:52:59 +0000198 * - find the highest descriptor we're interested in
199 * - add one
200 * - that's the size
201 * See: Stevens, APitUE, $12.5.1
202 */
Tim Peters4b046c22001-08-16 21:59:46 +0000203 pylist rfd2obj[FD_SETSIZE + 1];
204 pylist wfd2obj[FD_SETSIZE + 1];
205 pylist efd2obj[FD_SETSIZE + 1];
Barry Warsawb44740f2001-08-16 16:52:59 +0000206#endif /* SELECT_USES_HEAP */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000207 PyObject *ifdlist, *ofdlist, *efdlist;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000208 PyObject *ret = NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000209 PyObject *tout = Py_None;
210 fd_set ifdset, ofdset, efdset;
211 double timeout;
212 struct timeval tv, *tvp;
Guido van Rossum3262e162000-06-28 21:18:13 +0000213 long seconds;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000214 int imax, omax, emax, max;
215 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000216
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000217 /* convert arguments */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000218 if (!PyArg_UnpackTuple(args, "select", 3, 4,
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000219 &ifdlist, &ofdlist, &efdlist, &tout))
220 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000221
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000222 if (tout == Py_None)
223 tvp = (struct timeval *)0;
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000224 else if (!PyNumber_Check(tout)) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000225 PyErr_SetString(PyExc_TypeError,
226 "timeout must be a float or None");
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000227 return NULL;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000228 }
Guido van Rossumc7a22701993-11-01 16:27:16 +0000229 else {
Neil Schemenauer47ec6c02002-11-18 16:02:29 +0000230 timeout = PyFloat_AsDouble(tout);
231 if (timeout == -1 && PyErr_Occurred())
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000232 return NULL;
Guido van Rossum3262e162000-06-28 21:18:13 +0000233 if (timeout > (double)LONG_MAX) {
Barry Warsaw2f704552001-08-16 16:55:10 +0000234 PyErr_SetString(PyExc_OverflowError,
235 "timeout period too long");
Guido van Rossum3262e162000-06-28 21:18:13 +0000236 return NULL;
237 }
238 seconds = (long)timeout;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000239 timeout = timeout - (double)seconds;
240 tv.tv_sec = seconds;
Guido van Rossum3262e162000-06-28 21:18:13 +0000241 tv.tv_usec = (long)(timeout*1000000.0);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000242 tvp = &tv;
Guido van Rossumc7a22701993-11-01 16:27:16 +0000243 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000244
Guido van Rossumed233a51992-06-23 09:07:03 +0000245
Barry Warsawb44740f2001-08-16 16:52:59 +0000246#ifdef SELECT_USES_HEAP
Guido van Rossumd20781b1998-07-02 02:53:36 +0000247 /* Allocate memory for the lists */
Tim Peters4b046c22001-08-16 21:59:46 +0000248 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
249 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
250 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
Guido van Rossumd20781b1998-07-02 02:53:36 +0000251 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000252 if (rfd2obj) PyMem_DEL(rfd2obj);
253 if (wfd2obj) PyMem_DEL(wfd2obj);
254 if (efd2obj) PyMem_DEL(efd2obj);
Tim Peters5f322d32003-02-11 17:18:58 +0000255 return PyErr_NoMemory();
Guido van Rossumd20781b1998-07-02 02:53:36 +0000256 }
Barry Warsawb44740f2001-08-16 16:52:59 +0000257#endif /* SELECT_USES_HEAP */
Brett Cannon62dba4c2003-09-10 19:37:42 +0000258 /* Convert sequences to fd_sets, and get maximum fd number
259 * propagates the Python exception set in seq2set()
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000260 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000261 rfd2obj[0].sentinel = -1;
262 wfd2obj[0].sentinel = -1;
263 efd2obj[0].sentinel = -1;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000264 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000265 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000266 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000267 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000268 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000269 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000270 max = imax;
271 if (omax > max) max = omax;
272 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000273
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000274 Py_BEGIN_ALLOW_THREADS
275 n = select(max, &ifdset, &ofdset, &efdset, tvp);
276 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000277
Thomas Heller106f4c72002-09-24 16:51:00 +0000278#ifdef MS_WINDOWS
279 if (n == SOCKET_ERROR) {
280 PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());
281 }
282#else
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000283 if (n < 0) {
284 PyErr_SetFromErrno(SelectError);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000285 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000286#endif
Barry Warsawc1cb3601996-12-12 22:16:21 +0000287 else if (n == 0) {
288 /* optimization */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000289 ifdlist = PyList_New(0);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000290 if (ifdlist) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000291 ret = PyTuple_Pack(3, ifdlist, ifdlist, ifdlist);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000292 Py_DECREF(ifdlist);
293 }
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000294 }
Barry Warsawc1cb3601996-12-12 22:16:21 +0000295 else {
296 /* any of these three calls can raise an exception. it's more
297 convenient to test for this after all three calls... but
298 is that acceptable?
299 */
300 ifdlist = set2list(&ifdset, rfd2obj);
301 ofdlist = set2list(&ofdset, wfd2obj);
302 efdlist = set2list(&efdset, efd2obj);
303 if (PyErr_Occurred())
304 ret = NULL;
305 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000306 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000307
Barry Warsawc1cb3601996-12-12 22:16:21 +0000308 Py_DECREF(ifdlist);
309 Py_DECREF(ofdlist);
310 Py_DECREF(efdlist);
311 }
312
313 finally:
314 reap_obj(rfd2obj);
315 reap_obj(wfd2obj);
316 reap_obj(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000317#ifdef SELECT_USES_HEAP
Guido van Rossumd20781b1998-07-02 02:53:36 +0000318 PyMem_DEL(rfd2obj);
319 PyMem_DEL(wfd2obj);
320 PyMem_DEL(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000321#endif /* SELECT_USES_HEAP */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000322 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000323}
324
Nicholas Bastine62c5c82004-03-21 23:45:42 +0000325#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000326/*
327 * poll() support
328 */
329
330typedef struct {
331 PyObject_HEAD
332 PyObject *dict;
333 int ufd_uptodate;
334 int ufd_len;
335 struct pollfd *ufds;
336} pollObject;
337
Jeremy Hylton938ace62002-07-17 16:30:39 +0000338static PyTypeObject poll_Type;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000339
340/* Update the malloc'ed array of pollfds to match the dictionary
341 contained within a pollObject. Return 1 on success, 0 on an error.
342*/
343
344static int
345update_ufd_array(pollObject *self)
346{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000347 Py_ssize_t i, pos;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000348 PyObject *key, *value;
349
350 self->ufd_len = PyDict_Size(self->dict);
351 PyMem_Resize(self->ufds, struct pollfd, self->ufd_len);
352 if (self->ufds == NULL) {
353 PyErr_NoMemory();
354 return 0;
355 }
356
357 i = pos = 0;
Fred Drakedff3a372001-07-19 21:29:49 +0000358 while (PyDict_Next(self->dict, &pos, &key, &value)) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000359 self->ufds[i].fd = PyLong_AsLong(key);
360 self->ufds[i].events = (short)PyLong_AsLong(value);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000361 i++;
362 }
363 self->ufd_uptodate = 1;
364 return 1;
365}
366
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000367PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000368"register(fd [, eventmask] ) -> None\n\n\
369Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000370fd -- either an integer, or an object with a fileno() method returning an\n\
371 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000372events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000373
374static PyObject *
375poll_register(pollObject *self, PyObject *args)
376{
377 PyObject *o, *key, *value;
378 int fd, events = POLLIN | POLLPRI | POLLOUT;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000379 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000380
Fred Drake7b87f852001-05-21 03:29:05 +0000381 if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000382 return NULL;
383 }
384
385 fd = PyObject_AsFileDescriptor(o);
386 if (fd == -1) return NULL;
387
388 /* Add entry to the internal dictionary: the key is the
389 file descriptor, and the value is the event mask. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000390 key = PyLong_FromLong(fd);
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000391 if (key == NULL)
392 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +0000393 value = PyLong_FromLong(events);
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000394 if (value == NULL) {
395 Py_DECREF(key);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000396 return NULL;
397 }
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000398 err = PyDict_SetItem(self->dict, key, value);
399 Py_DECREF(key);
400 Py_DECREF(value);
401 if (err < 0)
402 return NULL;
403
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000404 self->ufd_uptodate = 0;
405
406 Py_INCREF(Py_None);
407 return Py_None;
408}
409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000410PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000411"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000412Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000413
414static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000415poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000416{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000417 PyObject *key;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000418 int fd;
419
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000420 fd = PyObject_AsFileDescriptor( o );
421 if (fd == -1)
422 return NULL;
423
424 /* Check whether the fd is already in the array */
Christian Heimes217cfd12007-12-02 14:31:20 +0000425 key = PyLong_FromLong(fd);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000426 if (key == NULL)
427 return NULL;
428
429 if (PyDict_DelItem(self->dict, key) == -1) {
430 Py_DECREF(key);
431 /* This will simply raise the KeyError set by PyDict_DelItem
432 if the file descriptor isn't registered. */
433 return NULL;
434 }
435
436 Py_DECREF(key);
437 self->ufd_uptodate = 0;
438
439 Py_INCREF(Py_None);
440 return Py_None;
441}
442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000443PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000444"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
445Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000446any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000447
448static PyObject *
449poll_poll(pollObject *self, PyObject *args)
450{
451 PyObject *result_list = NULL, *tout = NULL;
452 int timeout = 0, poll_result, i, j;
453 PyObject *value = NULL, *num = NULL;
454
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000455 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000456 return NULL;
457 }
458
459 /* Check values for timeout */
460 if (tout == NULL || tout == Py_None)
461 timeout = -1;
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000462 else if (!PyNumber_Check(tout)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000463 PyErr_SetString(PyExc_TypeError,
464 "timeout must be an integer or None");
465 return NULL;
466 }
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000467 else {
468 tout = PyNumber_Int(tout);
469 if (!tout)
470 return NULL;
Christian Heimes217cfd12007-12-02 14:31:20 +0000471 timeout = PyLong_AsLong(tout);
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000472 Py_DECREF(tout);
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000473 if (timeout == -1 && PyErr_Occurred())
474 return NULL;
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000475 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000476
477 /* Ensure the ufd array is up to date */
478 if (!self->ufd_uptodate)
479 if (update_ufd_array(self) == 0)
480 return NULL;
481
482 /* call poll() */
483 Py_BEGIN_ALLOW_THREADS;
484 poll_result = poll(self->ufds, self->ufd_len, timeout);
485 Py_END_ALLOW_THREADS;
486
487 if (poll_result < 0) {
488 PyErr_SetFromErrno(SelectError);
489 return NULL;
490 }
491
492 /* build the result list */
493
494 result_list = PyList_New(poll_result);
495 if (!result_list)
496 return NULL;
497 else {
498 for (i = 0, j = 0; j < poll_result; j++) {
499 /* skip to the next fired descriptor */
500 while (!self->ufds[i].revents) {
501 i++;
502 }
503 /* if we hit a NULL return, set value to NULL
504 and break out of loop; code at end will
505 clean up result_list */
506 value = PyTuple_New(2);
507 if (value == NULL)
508 goto error;
Christian Heimes217cfd12007-12-02 14:31:20 +0000509 num = PyLong_FromLong(self->ufds[i].fd);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000510 if (num == NULL) {
511 Py_DECREF(value);
512 goto error;
513 }
514 PyTuple_SET_ITEM(value, 0, num);
515
Andrew M. Kuchlinge5dd1622004-08-07 17:21:27 +0000516 /* The &0xffff is a workaround for AIX. 'revents'
517 is a 16-bit short, and IBM assigned POLLNVAL
518 to be 0x8000, so the conversion to int results
519 in a negative number. See SF bug #923315. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000520 num = PyLong_FromLong(self->ufds[i].revents & 0xffff);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000521 if (num == NULL) {
522 Py_DECREF(value);
523 goto error;
524 }
525 PyTuple_SET_ITEM(value, 1, num);
526 if ((PyList_SetItem(result_list, j, value)) == -1) {
527 Py_DECREF(value);
528 goto error;
529 }
530 i++;
531 }
532 }
533 return result_list;
534
535 error:
536 Py_DECREF(result_list);
537 return NULL;
538}
539
540static PyMethodDef poll_methods[] = {
541 {"register", (PyCFunction)poll_register,
542 METH_VARARGS, poll_register_doc},
543 {"unregister", (PyCFunction)poll_unregister,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000544 METH_O, poll_unregister_doc},
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000545 {"poll", (PyCFunction)poll_poll,
546 METH_VARARGS, poll_poll_doc},
547 {NULL, NULL} /* sentinel */
548};
549
550static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000551newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000552{
553 pollObject *self;
554 self = PyObject_New(pollObject, &poll_Type);
555 if (self == NULL)
556 return NULL;
557 /* ufd_uptodate is a Boolean, denoting whether the
558 array pointed to by ufds matches the contents of the dictionary. */
559 self->ufd_uptodate = 0;
560 self->ufds = NULL;
561 self->dict = PyDict_New();
562 if (self->dict == NULL) {
563 Py_DECREF(self);
564 return NULL;
565 }
566 return self;
567}
568
569static void
570poll_dealloc(pollObject *self)
571{
572 if (self->ufds != NULL)
573 PyMem_DEL(self->ufds);
574 Py_XDECREF(self->dict);
575 PyObject_Del(self);
576}
577
578static PyObject *
579poll_getattr(pollObject *self, char *name)
580{
581 return Py_FindMethod(poll_methods, (PyObject *)self, name);
582}
583
Tim Peters0c322792002-07-17 16:49:03 +0000584static PyTypeObject poll_Type = {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000585 /* The ob_type field must be initialized in the module init function
586 * to be portable to Windows without using C++. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000587 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000588 "select.poll", /*tp_name*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000589 sizeof(pollObject), /*tp_basicsize*/
590 0, /*tp_itemsize*/
591 /* methods */
592 (destructor)poll_dealloc, /*tp_dealloc*/
593 0, /*tp_print*/
594 (getattrfunc)poll_getattr, /*tp_getattr*/
595 0, /*tp_setattr*/
596 0, /*tp_compare*/
597 0, /*tp_repr*/
598 0, /*tp_as_number*/
599 0, /*tp_as_sequence*/
600 0, /*tp_as_mapping*/
601 0, /*tp_hash*/
602};
603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000604PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000605"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000606unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000607
608static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000609select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000610{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000611 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000612}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000613
614#ifdef __APPLE__
615/*
616 * On some systems poll() sets errno on invalid file descriptors. We test
617 * for this at runtime because this bug may be fixed or introduced between
618 * OS releases.
619 */
620static int select_have_broken_poll(void)
621{
622 int poll_test;
623 int filedes[2];
624
625 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
626
627 /* Create a file descriptor to make invalid */
628 if (pipe(filedes) < 0) {
629 return 1;
630 }
631 poll_struct.fd = filedes[0];
632 close(filedes[0]);
633 close(filedes[1]);
634 poll_test = poll(&poll_struct, 1, 0);
635 if (poll_test < 0) {
636 return 1;
637 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
638 return 1;
639 }
640 return 0;
641}
642#endif /* __APPLE__ */
643
644#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000646PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000647"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
648\n\
649Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +0000650The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000651rlist -- wait until ready for reading\n\
652wlist -- wait until ready for writing\n\
653xlist -- wait for an ``exceptional condition''\n\
654If only one kind of condition is required, pass [] for the other lists.\n\
655A file descriptor is either a socket or file object, or a small integer\n\
656gotten from a fileno() method call on one of those.\n\
657\n\
658The optional 4th argument specifies a timeout in seconds; it may be\n\
659a floating point number to specify fractions of seconds. If it is absent\n\
660or None, the call will never time out.\n\
661\n\
662The return value is a tuple of three lists corresponding to the first three\n\
663arguments; each contains the subset of the corresponding file descriptors\n\
664that are ready.\n\
665\n\
666*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000667On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000668
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000669static PyMethodDef select_methods[] = {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000670 {"select", select_select, METH_VARARGS, select_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000671#if defined(HAVE_POLL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000672 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000673#endif /* HAVE_POLL */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000674 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +0000675};
676
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000677PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000678"This module supports asynchronous I/O on multiple file descriptors.\n\
679\n\
680*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000681On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +0000682
Mark Hammond62b1ab12002-07-23 06:31:15 +0000683PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000684initselect(void)
Guido van Rossumed233a51992-06-23 09:07:03 +0000685{
Fred Drake4baedc12002-04-01 14:53:37 +0000686 PyObject *m;
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000687 m = Py_InitModule3("select", select_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000688 if (m == NULL)
689 return;
Fred Drake4baedc12002-04-01 14:53:37 +0000690
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000691 SelectError = PyErr_NewException("select.error", NULL, NULL);
Fred Drake4baedc12002-04-01 14:53:37 +0000692 Py_INCREF(SelectError);
693 PyModule_AddObject(m, "error", SelectError);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694#if defined(HAVE_POLL)
695
696#ifdef __APPLE__
697 if (select_have_broken_poll()) {
698 if (PyObject_DelAttrString(m, "poll") == -1) {
699 PyErr_Clear();
700 }
701 } else {
702#else
703 {
704#endif
Christian Heimes90aa7642007-12-19 02:45:37 +0000705 Py_TYPE(&poll_Type) = &PyType_Type;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000706 PyModule_AddIntConstant(m, "POLLIN", POLLIN);
707 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
708 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
709 PyModule_AddIntConstant(m, "POLLERR", POLLERR);
710 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
711 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000712
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000713#ifdef POLLRDNORM
Thomas Wouters477c8d52006-05-27 19:21:47 +0000714 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000715#endif
716#ifdef POLLRDBAND
Thomas Wouters477c8d52006-05-27 19:21:47 +0000717 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000718#endif
719#ifdef POLLWRNORM
Thomas Wouters477c8d52006-05-27 19:21:47 +0000720 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000721#endif
722#ifdef POLLWRBAND
Thomas Wouters477c8d52006-05-27 19:21:47 +0000723 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000724#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000725#ifdef POLLMSG
Thomas Wouters477c8d52006-05-27 19:21:47 +0000726 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000727#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000728 }
729#endif /* HAVE_POLL */
Guido van Rossumed233a51992-06-23 09:07:03 +0000730}