blob: 8bb2df85dfc346262b5837e05a45366ad5ee20f5 [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000047# include <winsock.h>
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000048#else
Thomas Wouters0e3f5912006-08-11 14:57:12 +000049# define SOCKET int
Skip Montanaroeb33e5a2007-08-17 12:57:41 +000050# if defined(__VMS)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000051# include <socket.h>
52# endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000053#endif
Guido van Rossumed233a51992-06-23 09:07:03 +000054
Guido van Rossum1ca8bb32001-03-02 06:28:17 +000055
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000056static PyObject *SelectError;
Guido van Rossumed233a51992-06-23 09:07:03 +000057
Barry Warsawc1cb3601996-12-12 22:16:21 +000058/* list of Python objects and their file descriptor */
59typedef struct {
60 PyObject *obj; /* owned reference */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000061 SOCKET fd;
Barry Warsawc1cb3601996-12-12 22:16:21 +000062 int sentinel; /* -1 == sentinel */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000063} pylist;
64
Barry Warsawc1cb3601996-12-12 22:16:21 +000065static void
Tim Peters4b046c22001-08-16 21:59:46 +000066reap_obj(pylist fd2obj[FD_SETSIZE + 1])
Barry Warsawc1cb3601996-12-12 22:16:21 +000067{
68 int i;
Tim Peters4b046c22001-08-16 21:59:46 +000069 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
Barry Warsawc1cb3601996-12-12 22:16:21 +000070 Py_XDECREF(fd2obj[i].obj);
71 fd2obj[i].obj = NULL;
72 }
73 fd2obj[0].sentinel = -1;
74}
75
76
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000077/* returns -1 and sets the Python exception if an error occurred, otherwise
78 returns a number >= 0
79*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000080static int
Brett Cannon62dba4c2003-09-10 19:37:42 +000081seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +000082{
Barry Warsawc1cb3601996-12-12 22:16:21 +000083 int i;
84 int max = -1;
85 int index = 0;
Brett Cannon62dba4c2003-09-10 19:37:42 +000086 int len = -1;
87 PyObject* fast_seq = NULL;
Barry Warsawc1cb3601996-12-12 22:16:21 +000088 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000089
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000090 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000091 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000092
Brett Cannon62dba4c2003-09-10 19:37:42 +000093 fast_seq=PySequence_Fast(seq, "arguments 1-3 must be sequences");
94 if (!fast_seq)
95 return -1;
96
97 len = PySequence_Fast_GET_SIZE(fast_seq);
98
Barry Warsawc1cb3601996-12-12 22:16:21 +000099 for (i = 0; i < len; i++) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000100 SOCKET v;
101
102 /* any intervening fileno() calls could decr this refcnt */
Brett Cannon62dba4c2003-09-10 19:37:42 +0000103 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
Barry Warsaw529fcfe1996-12-16 18:15:34 +0000104 return -1;
Barry Warsaw24c4b3d1996-12-13 23:22:42 +0000105
Barry Warsawc1cb3601996-12-12 22:16:21 +0000106 Py_INCREF(o);
Andrew M. Kuchling9f28a032000-07-13 23:59:35 +0000107 v = PyObject_AsFileDescriptor( o );
108 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000109
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000110#if defined(_MSC_VER)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000111 max = 0; /* not used for Win32 */
112#else /* !_MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000113 if (v < 0 || v >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000114 PyErr_SetString(PyExc_ValueError,
115 "filedescriptor out of range in select()");
116 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000117 }
118 if (v > max)
119 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000120#endif /* _MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000121 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000122
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000123 /* add object and its file descriptor to the list */
124 if (index >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000125 PyErr_SetString(PyExc_ValueError,
126 "too many file descriptors in select()");
127 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000128 }
129 fd2obj[index].obj = o;
130 fd2obj[index].fd = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000131 fd2obj[index].sentinel = 0;
132 fd2obj[++index].sentinel = -1;
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000133 }
Brett Cannon62dba4c2003-09-10 19:37:42 +0000134 Py_DECREF(fast_seq);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000135 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000136
137 finally:
138 Py_XDECREF(o);
Brett Cannon62dba4c2003-09-10 19:37:42 +0000139 Py_DECREF(fast_seq);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000140 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000141}
142
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000143/* returns NULL and sets the Python exception if an error occurred */
144static PyObject *
Tim Peters4b046c22001-08-16 21:59:46 +0000145set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +0000146{
Barry Warsawc1cb3601996-12-12 22:16:21 +0000147 int i, j, count=0;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000148 PyObject *list, *o;
149 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000150
Barry Warsawc1cb3601996-12-12 22:16:21 +0000151 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000152 if (FD_ISSET(fd2obj[j].fd, set))
Barry Warsawc1cb3601996-12-12 22:16:21 +0000153 count++;
154 }
155 list = PyList_New(count);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000156 if (!list)
157 return NULL;
158
Barry Warsawc1cb3601996-12-12 22:16:21 +0000159 i = 0;
160 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000161 fd = fd2obj[j].fd;
162 if (FD_ISSET(fd, set)) {
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000163#ifndef _MSC_VER
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000164 if (fd > FD_SETSIZE) {
165 PyErr_SetString(PyExc_SystemError,
166 "filedescriptor out of range returned in select()");
Barry Warsawc1cb3601996-12-12 22:16:21 +0000167 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000168 }
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000169#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000170 o = fd2obj[j].obj;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000171 fd2obj[j].obj = NULL;
172 /* transfer ownership */
173 if (PyList_SetItem(list, i, o) < 0)
174 goto finally;
175
176 i++;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000177 }
178 }
179 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000180 finally:
181 Py_DECREF(list);
182 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000183}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000184
Barry Warsawb44740f2001-08-16 16:52:59 +0000185#undef SELECT_USES_HEAP
186#if FD_SETSIZE > 1024
187#define SELECT_USES_HEAP
188#endif /* FD_SETSIZE > 1024 */
189
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000190static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000191select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000192{
Barry Warsawb44740f2001-08-16 16:52:59 +0000193#ifdef SELECT_USES_HEAP
Guido van Rossumd20781b1998-07-02 02:53:36 +0000194 pylist *rfd2obj, *wfd2obj, *efd2obj;
Barry Warsawb44740f2001-08-16 16:52:59 +0000195#else /* !SELECT_USES_HEAP */
Tim Peters4b046c22001-08-16 21:59:46 +0000196 /* XXX: All this should probably be implemented as follows:
Barry Warsawb44740f2001-08-16 16:52:59 +0000197 * - find the highest descriptor we're interested in
198 * - add one
199 * - that's the size
200 * See: Stevens, APitUE, $12.5.1
201 */
Tim Peters4b046c22001-08-16 21:59:46 +0000202 pylist rfd2obj[FD_SETSIZE + 1];
203 pylist wfd2obj[FD_SETSIZE + 1];
204 pylist efd2obj[FD_SETSIZE + 1];
Barry Warsawb44740f2001-08-16 16:52:59 +0000205#endif /* SELECT_USES_HEAP */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000206 PyObject *ifdlist, *ofdlist, *efdlist;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000207 PyObject *ret = NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000208 PyObject *tout = Py_None;
209 fd_set ifdset, ofdset, efdset;
210 double timeout;
211 struct timeval tv, *tvp;
Guido van Rossum3262e162000-06-28 21:18:13 +0000212 long seconds;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000213 int imax, omax, emax, max;
214 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000215
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000216 /* convert arguments */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000217 if (!PyArg_UnpackTuple(args, "select", 3, 4,
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000218 &ifdlist, &ofdlist, &efdlist, &tout))
219 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000220
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000221 if (tout == Py_None)
222 tvp = (struct timeval *)0;
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000223 else if (!PyNumber_Check(tout)) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000224 PyErr_SetString(PyExc_TypeError,
225 "timeout must be a float or None");
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000226 return NULL;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000227 }
Guido van Rossumc7a22701993-11-01 16:27:16 +0000228 else {
Neil Schemenauer47ec6c02002-11-18 16:02:29 +0000229 timeout = PyFloat_AsDouble(tout);
230 if (timeout == -1 && PyErr_Occurred())
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000231 return NULL;
Guido van Rossum3262e162000-06-28 21:18:13 +0000232 if (timeout > (double)LONG_MAX) {
Barry Warsaw2f704552001-08-16 16:55:10 +0000233 PyErr_SetString(PyExc_OverflowError,
234 "timeout period too long");
Guido van Rossum3262e162000-06-28 21:18:13 +0000235 return NULL;
236 }
237 seconds = (long)timeout;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000238 timeout = timeout - (double)seconds;
239 tv.tv_sec = seconds;
Guido van Rossum3262e162000-06-28 21:18:13 +0000240 tv.tv_usec = (long)(timeout*1000000.0);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000241 tvp = &tv;
Guido van Rossumc7a22701993-11-01 16:27:16 +0000242 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000243
Guido van Rossumed233a51992-06-23 09:07:03 +0000244
Barry Warsawb44740f2001-08-16 16:52:59 +0000245#ifdef SELECT_USES_HEAP
Guido van Rossumd20781b1998-07-02 02:53:36 +0000246 /* Allocate memory for the lists */
Tim Peters4b046c22001-08-16 21:59:46 +0000247 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
248 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
249 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
Guido van Rossumd20781b1998-07-02 02:53:36 +0000250 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000251 if (rfd2obj) PyMem_DEL(rfd2obj);
252 if (wfd2obj) PyMem_DEL(wfd2obj);
253 if (efd2obj) PyMem_DEL(efd2obj);
Tim Peters5f322d32003-02-11 17:18:58 +0000254 return PyErr_NoMemory();
Guido van Rossumd20781b1998-07-02 02:53:36 +0000255 }
Barry Warsawb44740f2001-08-16 16:52:59 +0000256#endif /* SELECT_USES_HEAP */
Brett Cannon62dba4c2003-09-10 19:37:42 +0000257 /* Convert sequences to fd_sets, and get maximum fd number
258 * propagates the Python exception set in seq2set()
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000259 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000260 rfd2obj[0].sentinel = -1;
261 wfd2obj[0].sentinel = -1;
262 efd2obj[0].sentinel = -1;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000263 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000264 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000265 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000266 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000267 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000268 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000269 max = imax;
270 if (omax > max) max = omax;
271 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000272
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000273 Py_BEGIN_ALLOW_THREADS
274 n = select(max, &ifdset, &ofdset, &efdset, tvp);
275 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000276
Thomas Heller106f4c72002-09-24 16:51:00 +0000277#ifdef MS_WINDOWS
278 if (n == SOCKET_ERROR) {
279 PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());
280 }
281#else
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000282 if (n < 0) {
283 PyErr_SetFromErrno(SelectError);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000284 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000285#endif
Barry Warsawc1cb3601996-12-12 22:16:21 +0000286 else if (n == 0) {
287 /* optimization */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000288 ifdlist = PyList_New(0);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000289 if (ifdlist) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000290 ret = PyTuple_Pack(3, ifdlist, ifdlist, ifdlist);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000291 Py_DECREF(ifdlist);
292 }
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000293 }
Barry Warsawc1cb3601996-12-12 22:16:21 +0000294 else {
295 /* any of these three calls can raise an exception. it's more
296 convenient to test for this after all three calls... but
297 is that acceptable?
298 */
299 ifdlist = set2list(&ifdset, rfd2obj);
300 ofdlist = set2list(&ofdset, wfd2obj);
301 efdlist = set2list(&efdset, efd2obj);
302 if (PyErr_Occurred())
303 ret = NULL;
304 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000305 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000306
Barry Warsawc1cb3601996-12-12 22:16:21 +0000307 Py_DECREF(ifdlist);
308 Py_DECREF(ofdlist);
309 Py_DECREF(efdlist);
310 }
311
312 finally:
313 reap_obj(rfd2obj);
314 reap_obj(wfd2obj);
315 reap_obj(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000316#ifdef SELECT_USES_HEAP
Guido van Rossumd20781b1998-07-02 02:53:36 +0000317 PyMem_DEL(rfd2obj);
318 PyMem_DEL(wfd2obj);
319 PyMem_DEL(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000320#endif /* SELECT_USES_HEAP */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000321 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000322}
323
Nicholas Bastine62c5c82004-03-21 23:45:42 +0000324#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000325/*
326 * poll() support
327 */
328
329typedef struct {
330 PyObject_HEAD
331 PyObject *dict;
332 int ufd_uptodate;
333 int ufd_len;
334 struct pollfd *ufds;
335} pollObject;
336
Jeremy Hylton938ace62002-07-17 16:30:39 +0000337static PyTypeObject poll_Type;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000338
339/* Update the malloc'ed array of pollfds to match the dictionary
340 contained within a pollObject. Return 1 on success, 0 on an error.
341*/
342
343static int
344update_ufd_array(pollObject *self)
345{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000346 Py_ssize_t i, pos;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000347 PyObject *key, *value;
348
349 self->ufd_len = PyDict_Size(self->dict);
350 PyMem_Resize(self->ufds, struct pollfd, self->ufd_len);
351 if (self->ufds == NULL) {
352 PyErr_NoMemory();
353 return 0;
354 }
355
356 i = pos = 0;
Fred Drakedff3a372001-07-19 21:29:49 +0000357 while (PyDict_Next(self->dict, &pos, &key, &value)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000358 self->ufds[i].fd = PyInt_AsLong(key);
Fred Drakedff3a372001-07-19 21:29:49 +0000359 self->ufds[i].events = (short)PyInt_AsLong(value);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000360 i++;
361 }
362 self->ufd_uptodate = 1;
363 return 1;
364}
365
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000366PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000367"register(fd [, eventmask] ) -> None\n\n\
368Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000369fd -- either an integer, or an object with a fileno() method returning an\n\
370 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000371events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000372
373static PyObject *
374poll_register(pollObject *self, PyObject *args)
375{
376 PyObject *o, *key, *value;
377 int fd, events = POLLIN | POLLPRI | POLLOUT;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000378 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000379
Fred Drake7b87f852001-05-21 03:29:05 +0000380 if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000381 return NULL;
382 }
383
384 fd = PyObject_AsFileDescriptor(o);
385 if (fd == -1) return NULL;
386
387 /* Add entry to the internal dictionary: the key is the
388 file descriptor, and the value is the event mask. */
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000389 key = PyInt_FromLong(fd);
390 if (key == NULL)
391 return NULL;
392 value = PyInt_FromLong(events);
393 if (value == NULL) {
394 Py_DECREF(key);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000395 return NULL;
396 }
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000397 err = PyDict_SetItem(self->dict, key, value);
398 Py_DECREF(key);
399 Py_DECREF(value);
400 if (err < 0)
401 return NULL;
402
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000403 self->ufd_uptodate = 0;
404
405 Py_INCREF(Py_None);
406 return Py_None;
407}
408
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000409PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000410"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000411Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000412
413static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000414poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000415{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000416 PyObject *key;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000417 int fd;
418
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000419 fd = PyObject_AsFileDescriptor( o );
420 if (fd == -1)
421 return NULL;
422
423 /* Check whether the fd is already in the array */
424 key = PyInt_FromLong(fd);
425 if (key == NULL)
426 return NULL;
427
428 if (PyDict_DelItem(self->dict, key) == -1) {
429 Py_DECREF(key);
430 /* This will simply raise the KeyError set by PyDict_DelItem
431 if the file descriptor isn't registered. */
432 return NULL;
433 }
434
435 Py_DECREF(key);
436 self->ufd_uptodate = 0;
437
438 Py_INCREF(Py_None);
439 return Py_None;
440}
441
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000442PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000443"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
444Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000445any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000446
447static PyObject *
448poll_poll(pollObject *self, PyObject *args)
449{
450 PyObject *result_list = NULL, *tout = NULL;
451 int timeout = 0, poll_result, i, j;
452 PyObject *value = NULL, *num = NULL;
453
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000454 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000455 return NULL;
456 }
457
458 /* Check values for timeout */
459 if (tout == NULL || tout == Py_None)
460 timeout = -1;
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000461 else if (!PyNumber_Check(tout)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000462 PyErr_SetString(PyExc_TypeError,
463 "timeout must be an integer or None");
464 return NULL;
465 }
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000466 else {
467 tout = PyNumber_Int(tout);
468 if (!tout)
469 return NULL;
Walter Dörwald08c4cc42002-11-12 11:42:20 +0000470 timeout = PyInt_AsLong(tout);
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000471 Py_DECREF(tout);
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000472 if (timeout == -1 && PyErr_Occurred())
473 return NULL;
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000474 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000475
476 /* Ensure the ufd array is up to date */
477 if (!self->ufd_uptodate)
478 if (update_ufd_array(self) == 0)
479 return NULL;
480
481 /* call poll() */
482 Py_BEGIN_ALLOW_THREADS;
483 poll_result = poll(self->ufds, self->ufd_len, timeout);
484 Py_END_ALLOW_THREADS;
485
486 if (poll_result < 0) {
487 PyErr_SetFromErrno(SelectError);
488 return NULL;
489 }
490
491 /* build the result list */
492
493 result_list = PyList_New(poll_result);
494 if (!result_list)
495 return NULL;
496 else {
497 for (i = 0, j = 0; j < poll_result; j++) {
498 /* skip to the next fired descriptor */
499 while (!self->ufds[i].revents) {
500 i++;
501 }
502 /* if we hit a NULL return, set value to NULL
503 and break out of loop; code at end will
504 clean up result_list */
505 value = PyTuple_New(2);
506 if (value == NULL)
507 goto error;
508 num = PyInt_FromLong(self->ufds[i].fd);
509 if (num == NULL) {
510 Py_DECREF(value);
511 goto error;
512 }
513 PyTuple_SET_ITEM(value, 0, num);
514
Andrew M. Kuchlinge5dd1622004-08-07 17:21:27 +0000515 /* The &0xffff is a workaround for AIX. 'revents'
516 is a 16-bit short, and IBM assigned POLLNVAL
517 to be 0x8000, so the conversion to int results
518 in a negative number. See SF bug #923315. */
519 num = PyInt_FromLong(self->ufds[i].revents & 0xffff);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000520 if (num == NULL) {
521 Py_DECREF(value);
522 goto error;
523 }
524 PyTuple_SET_ITEM(value, 1, num);
525 if ((PyList_SetItem(result_list, j, value)) == -1) {
526 Py_DECREF(value);
527 goto error;
528 }
529 i++;
530 }
531 }
532 return result_list;
533
534 error:
535 Py_DECREF(result_list);
536 return NULL;
537}
538
539static PyMethodDef poll_methods[] = {
540 {"register", (PyCFunction)poll_register,
541 METH_VARARGS, poll_register_doc},
542 {"unregister", (PyCFunction)poll_unregister,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000543 METH_O, poll_unregister_doc},
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000544 {"poll", (PyCFunction)poll_poll,
545 METH_VARARGS, poll_poll_doc},
546 {NULL, NULL} /* sentinel */
547};
548
549static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000550newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000551{
552 pollObject *self;
553 self = PyObject_New(pollObject, &poll_Type);
554 if (self == NULL)
555 return NULL;
556 /* ufd_uptodate is a Boolean, denoting whether the
557 array pointed to by ufds matches the contents of the dictionary. */
558 self->ufd_uptodate = 0;
559 self->ufds = NULL;
560 self->dict = PyDict_New();
561 if (self->dict == NULL) {
562 Py_DECREF(self);
563 return NULL;
564 }
565 return self;
566}
567
568static void
569poll_dealloc(pollObject *self)
570{
571 if (self->ufds != NULL)
572 PyMem_DEL(self->ufds);
573 Py_XDECREF(self->dict);
574 PyObject_Del(self);
575}
576
577static PyObject *
578poll_getattr(pollObject *self, char *name)
579{
580 return Py_FindMethod(poll_methods, (PyObject *)self, name);
581}
582
Tim Peters0c322792002-07-17 16:49:03 +0000583static PyTypeObject poll_Type = {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000584 /* The ob_type field must be initialized in the module init function
585 * to be portable to Windows without using C++. */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000586 PyVarObject_HEAD_INIT(NULL, 0)
Guido van Rossum14648392001-12-08 18:02:58 +0000587 "select.poll", /*tp_name*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000588 sizeof(pollObject), /*tp_basicsize*/
589 0, /*tp_itemsize*/
590 /* methods */
591 (destructor)poll_dealloc, /*tp_dealloc*/
592 0, /*tp_print*/
593 (getattrfunc)poll_getattr, /*tp_getattr*/
594 0, /*tp_setattr*/
595 0, /*tp_compare*/
596 0, /*tp_repr*/
597 0, /*tp_as_number*/
598 0, /*tp_as_sequence*/
599 0, /*tp_as_mapping*/
600 0, /*tp_hash*/
601};
602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000603PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000604"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000605unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000606
607static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000608select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000609{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000610 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000611}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000612
613#ifdef __APPLE__
614/*
615 * On some systems poll() sets errno on invalid file descriptors. We test
616 * for this at runtime because this bug may be fixed or introduced between
617 * OS releases.
618 */
619static int select_have_broken_poll(void)
620{
621 int poll_test;
622 int filedes[2];
623
624 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
625
626 /* Create a file descriptor to make invalid */
627 if (pipe(filedes) < 0) {
628 return 1;
629 }
630 poll_struct.fd = filedes[0];
631 close(filedes[0]);
632 close(filedes[1]);
633 poll_test = poll(&poll_struct, 1, 0);
634 if (poll_test < 0) {
635 return 1;
636 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
637 return 1;
638 }
639 return 0;
640}
641#endif /* __APPLE__ */
642
643#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000645PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000646"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
647\n\
648Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +0000649The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000650rlist -- wait until ready for reading\n\
651wlist -- wait until ready for writing\n\
652xlist -- wait for an ``exceptional condition''\n\
653If only one kind of condition is required, pass [] for the other lists.\n\
654A file descriptor is either a socket or file object, or a small integer\n\
655gotten from a fileno() method call on one of those.\n\
656\n\
657The optional 4th argument specifies a timeout in seconds; it may be\n\
658a floating point number to specify fractions of seconds. If it is absent\n\
659or None, the call will never time out.\n\
660\n\
661The return value is a tuple of three lists corresponding to the first three\n\
662arguments; each contains the subset of the corresponding file descriptors\n\
663that are ready.\n\
664\n\
665*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000666On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000667
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000668static PyMethodDef select_methods[] = {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000669 {"select", select_select, METH_VARARGS, select_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000670#if defined(HAVE_POLL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000671 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672#endif /* HAVE_POLL */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000673 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +0000674};
675
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000676PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000677"This module supports asynchronous I/O on multiple file descriptors.\n\
678\n\
679*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000680On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +0000681
Mark Hammond62b1ab12002-07-23 06:31:15 +0000682PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000683initselect(void)
Guido van Rossumed233a51992-06-23 09:07:03 +0000684{
Fred Drake4baedc12002-04-01 14:53:37 +0000685 PyObject *m;
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000686 m = Py_InitModule3("select", select_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000687 if (m == NULL)
688 return;
Fred Drake4baedc12002-04-01 14:53:37 +0000689
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000690 SelectError = PyErr_NewException("select.error", NULL, NULL);
Fred Drake4baedc12002-04-01 14:53:37 +0000691 Py_INCREF(SelectError);
692 PyModule_AddObject(m, "error", SelectError);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000693#if defined(HAVE_POLL)
694
695#ifdef __APPLE__
696 if (select_have_broken_poll()) {
697 if (PyObject_DelAttrString(m, "poll") == -1) {
698 PyErr_Clear();
699 }
700 } else {
701#else
702 {
703#endif
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000704 Py_Type(&poll_Type) = &PyType_Type;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705 PyModule_AddIntConstant(m, "POLLIN", POLLIN);
706 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
707 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
708 PyModule_AddIntConstant(m, "POLLERR", POLLERR);
709 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
710 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000711
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000712#ifdef POLLRDNORM
Thomas Wouters477c8d52006-05-27 19:21:47 +0000713 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000714#endif
715#ifdef POLLRDBAND
Thomas Wouters477c8d52006-05-27 19:21:47 +0000716 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000717#endif
718#ifdef POLLWRNORM
Thomas Wouters477c8d52006-05-27 19:21:47 +0000719 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000720#endif
721#ifdef POLLWRBAND
Thomas Wouters477c8d52006-05-27 19:21:47 +0000722 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000723#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000724#ifdef POLLMSG
Thomas Wouters477c8d52006-05-27 19:21:47 +0000725 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000726#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000727 }
728#endif /* HAVE_POLL */
Guido van Rossumed233a51992-06-23 09:07:03 +0000729}