blob: 4deab6fb84b03e786577705774143972e6d0be78 [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 Rossumbcc20741998-08-04 22:53:56 +00005 Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything
6 >= 0.
Guido van Rossum4f0fbf81996-06-12 04:22:53 +00007*/
Guido van Rossumed233a51992-06-23 09:07:03 +00008
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00009#include "Python.h"
Guido van Rossumed233a51992-06-23 09:07:03 +000010
Ronald Oussoren32fd16e2006-04-23 12:36:23 +000011#ifdef __APPLE__
12 /* Perform runtime testing for a broken poll on OSX to make it easier
13 * to use the same binary on multiple releases of the OS.
14 */
15#undef HAVE_BROKEN_POLL
16#endif
17
Tim Petersd92dfe02000-12-12 01:18:41 +000018/* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
19 64 is too small (too many people have bumped into that limit).
20 Here we boost it.
21 Users who want even more than the boosted limit should #define
22 FD_SETSIZE higher before this; e.g., via compiler /D switch.
23*/
24#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
25#define FD_SETSIZE 512
26#endif
27
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000028#if defined(HAVE_POLL_H)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000029#include <poll.h>
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000030#elif defined(HAVE_SYS_POLL_H)
31#include <sys/poll.h>
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000032#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000033
Guido van Rossum37273171996-12-09 18:47:43 +000034#ifdef __sgi
35/* This is missing from unistd.h */
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000036extern void bzero(void *, int);
Guido van Rossum37273171996-12-09 18:47:43 +000037#endif
38
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000039#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000040#include <sys/types.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000041#endif
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000042
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000043#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000044#include <sys/time.h>
45#include <utils.h>
46#endif
47
Guido van Rossum6f489d91996-06-28 20:15:15 +000048#ifdef MS_WINDOWS
Neal Norwitz2a30cd02006-07-10 01:18:57 +000049# include <winsock.h>
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000050#else
Neal Norwitz2a30cd02006-07-10 01:18:57 +000051# define SOCKET int
52# ifdef __BEOS__
53# include <net/socket.h>
54# elif defined(__VMS)
55# include <socket.h>
56# endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000057#endif
Guido van Rossumed233a51992-06-23 09:07:03 +000058
Guido van Rossum1ca8bb32001-03-02 06:28:17 +000059
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000060static PyObject *SelectError;
Guido van Rossumed233a51992-06-23 09:07:03 +000061
Barry Warsawc1cb3601996-12-12 22:16:21 +000062/* list of Python objects and their file descriptor */
63typedef struct {
64 PyObject *obj; /* owned reference */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000065 SOCKET fd;
Barry Warsawc1cb3601996-12-12 22:16:21 +000066 int sentinel; /* -1 == sentinel */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000067} pylist;
68
Barry Warsawc1cb3601996-12-12 22:16:21 +000069static void
Tim Peters4b046c22001-08-16 21:59:46 +000070reap_obj(pylist fd2obj[FD_SETSIZE + 1])
Barry Warsawc1cb3601996-12-12 22:16:21 +000071{
72 int i;
Tim Peters4b046c22001-08-16 21:59:46 +000073 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
Barry Warsawc1cb3601996-12-12 22:16:21 +000074 Py_XDECREF(fd2obj[i].obj);
75 fd2obj[i].obj = NULL;
76 }
77 fd2obj[0].sentinel = -1;
78}
79
80
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000081/* returns -1 and sets the Python exception if an error occurred, otherwise
82 returns a number >= 0
83*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000084static int
Brett Cannon62dba4c2003-09-10 19:37:42 +000085seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +000086{
Barry Warsawc1cb3601996-12-12 22:16:21 +000087 int i;
88 int max = -1;
89 int index = 0;
Brett Cannon62dba4c2003-09-10 19:37:42 +000090 int len = -1;
91 PyObject* fast_seq = NULL;
Barry Warsawc1cb3601996-12-12 22:16:21 +000092 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000093
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000094 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000095 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000096
Brett Cannon62dba4c2003-09-10 19:37:42 +000097 fast_seq=PySequence_Fast(seq, "arguments 1-3 must be sequences");
98 if (!fast_seq)
99 return -1;
100
101 len = PySequence_Fast_GET_SIZE(fast_seq);
102
Barry Warsawc1cb3601996-12-12 22:16:21 +0000103 for (i = 0; i < len; i++) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000104 SOCKET v;
105
106 /* any intervening fileno() calls could decr this refcnt */
Brett Cannon62dba4c2003-09-10 19:37:42 +0000107 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
Barry Warsaw529fcfe1996-12-16 18:15:34 +0000108 return -1;
Barry Warsaw24c4b3d1996-12-13 23:22:42 +0000109
Barry Warsawc1cb3601996-12-12 22:16:21 +0000110 Py_INCREF(o);
Andrew M. Kuchling9f28a032000-07-13 23:59:35 +0000111 v = PyObject_AsFileDescriptor( o );
112 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000113
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000114#if defined(_MSC_VER)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000115 max = 0; /* not used for Win32 */
116#else /* !_MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000117 if (v < 0 || v >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000118 PyErr_SetString(PyExc_ValueError,
119 "filedescriptor out of range in select()");
120 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000121 }
122 if (v > max)
123 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000124#endif /* _MSC_VER */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000125 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000126
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000127 /* add object and its file descriptor to the list */
128 if (index >= FD_SETSIZE) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000129 PyErr_SetString(PyExc_ValueError,
130 "too many file descriptors in select()");
131 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000132 }
133 fd2obj[index].obj = o;
134 fd2obj[index].fd = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000135 fd2obj[index].sentinel = 0;
136 fd2obj[++index].sentinel = -1;
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000137 }
Brett Cannon62dba4c2003-09-10 19:37:42 +0000138 Py_DECREF(fast_seq);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000139 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000140
141 finally:
142 Py_XDECREF(o);
Brett Cannon62dba4c2003-09-10 19:37:42 +0000143 Py_DECREF(fast_seq);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000144 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000145}
146
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000147/* returns NULL and sets the Python exception if an error occurred */
148static PyObject *
Tim Peters4b046c22001-08-16 21:59:46 +0000149set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +0000150{
Barry Warsawc1cb3601996-12-12 22:16:21 +0000151 int i, j, count=0;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000152 PyObject *list, *o;
153 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000154
Barry Warsawc1cb3601996-12-12 22:16:21 +0000155 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000156 if (FD_ISSET(fd2obj[j].fd, set))
Barry Warsawc1cb3601996-12-12 22:16:21 +0000157 count++;
158 }
159 list = PyList_New(count);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000160 if (!list)
161 return NULL;
162
Barry Warsawc1cb3601996-12-12 22:16:21 +0000163 i = 0;
164 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000165 fd = fd2obj[j].fd;
166 if (FD_ISSET(fd, set)) {
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000167#ifndef _MSC_VER
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000168 if (fd > FD_SETSIZE) {
169 PyErr_SetString(PyExc_SystemError,
170 "filedescriptor out of range returned in select()");
Barry Warsawc1cb3601996-12-12 22:16:21 +0000171 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000172 }
Guido van Rossum4f0fbf81996-06-12 04:22:53 +0000173#endif
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000174 o = fd2obj[j].obj;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000175 fd2obj[j].obj = NULL;
176 /* transfer ownership */
177 if (PyList_SetItem(list, i, o) < 0)
178 goto finally;
179
180 i++;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000181 }
182 }
183 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000184 finally:
185 Py_DECREF(list);
186 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000187}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000188
Barry Warsawb44740f2001-08-16 16:52:59 +0000189#undef SELECT_USES_HEAP
190#if FD_SETSIZE > 1024
191#define SELECT_USES_HEAP
192#endif /* FD_SETSIZE > 1024 */
193
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000194static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000195select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000196{
Barry Warsawb44740f2001-08-16 16:52:59 +0000197#ifdef SELECT_USES_HEAP
Guido van Rossumd20781b1998-07-02 02:53:36 +0000198 pylist *rfd2obj, *wfd2obj, *efd2obj;
Barry Warsawb44740f2001-08-16 16:52:59 +0000199#else /* !SELECT_USES_HEAP */
Tim Peters4b046c22001-08-16 21:59:46 +0000200 /* XXX: All this should probably be implemented as follows:
Barry Warsawb44740f2001-08-16 16:52:59 +0000201 * - find the highest descriptor we're interested in
202 * - add one
203 * - that's the size
204 * See: Stevens, APitUE, $12.5.1
205 */
Tim Peters4b046c22001-08-16 21:59:46 +0000206 pylist rfd2obj[FD_SETSIZE + 1];
207 pylist wfd2obj[FD_SETSIZE + 1];
208 pylist efd2obj[FD_SETSIZE + 1];
Barry Warsawb44740f2001-08-16 16:52:59 +0000209#endif /* SELECT_USES_HEAP */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000210 PyObject *ifdlist, *ofdlist, *efdlist;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000211 PyObject *ret = NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000212 PyObject *tout = Py_None;
213 fd_set ifdset, ofdset, efdset;
214 double timeout;
215 struct timeval tv, *tvp;
Guido van Rossum3262e162000-06-28 21:18:13 +0000216 long seconds;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000217 int imax, omax, emax, max;
218 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000219
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000220 /* convert arguments */
Georg Brandl96a8c392006-05-29 21:04:52 +0000221 if (!PyArg_UnpackTuple(args, "select", 3, 4,
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000222 &ifdlist, &ofdlist, &efdlist, &tout))
223 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000224
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000225 if (tout == Py_None)
226 tvp = (struct timeval *)0;
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000227 else if (!PyNumber_Check(tout)) {
Barry Warsawc1cb3601996-12-12 22:16:21 +0000228 PyErr_SetString(PyExc_TypeError,
229 "timeout must be a float or None");
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000230 return NULL;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000231 }
Guido van Rossumc7a22701993-11-01 16:27:16 +0000232 else {
Neil Schemenauer47ec6c02002-11-18 16:02:29 +0000233 timeout = PyFloat_AsDouble(tout);
234 if (timeout == -1 && PyErr_Occurred())
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000235 return NULL;
Guido van Rossum3262e162000-06-28 21:18:13 +0000236 if (timeout > (double)LONG_MAX) {
Barry Warsaw2f704552001-08-16 16:55:10 +0000237 PyErr_SetString(PyExc_OverflowError,
238 "timeout period too long");
Guido van Rossum3262e162000-06-28 21:18:13 +0000239 return NULL;
240 }
241 seconds = (long)timeout;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000242 timeout = timeout - (double)seconds;
243 tv.tv_sec = seconds;
Guido van Rossum3262e162000-06-28 21:18:13 +0000244 tv.tv_usec = (long)(timeout*1000000.0);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000245 tvp = &tv;
Guido van Rossumc7a22701993-11-01 16:27:16 +0000246 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000247
Guido van Rossumed233a51992-06-23 09:07:03 +0000248
Barry Warsawb44740f2001-08-16 16:52:59 +0000249#ifdef SELECT_USES_HEAP
Guido van Rossumd20781b1998-07-02 02:53:36 +0000250 /* Allocate memory for the lists */
Tim Peters4b046c22001-08-16 21:59:46 +0000251 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
252 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
253 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
Guido van Rossumd20781b1998-07-02 02:53:36 +0000254 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000255 if (rfd2obj) PyMem_DEL(rfd2obj);
256 if (wfd2obj) PyMem_DEL(wfd2obj);
257 if (efd2obj) PyMem_DEL(efd2obj);
Tim Peters5f322d32003-02-11 17:18:58 +0000258 return PyErr_NoMemory();
Guido van Rossumd20781b1998-07-02 02:53:36 +0000259 }
Barry Warsawb44740f2001-08-16 16:52:59 +0000260#endif /* SELECT_USES_HEAP */
Brett Cannon62dba4c2003-09-10 19:37:42 +0000261 /* Convert sequences to fd_sets, and get maximum fd number
262 * propagates the Python exception set in seq2set()
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000263 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000264 rfd2obj[0].sentinel = -1;
265 wfd2obj[0].sentinel = -1;
266 efd2obj[0].sentinel = -1;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000267 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000268 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000269 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000270 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000271 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
Barry Warsawc1cb3601996-12-12 22:16:21 +0000272 goto finally;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000273 max = imax;
274 if (omax > max) max = omax;
275 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000276
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000277 Py_BEGIN_ALLOW_THREADS
278 n = select(max, &ifdset, &ofdset, &efdset, tvp);
279 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000280
Thomas Heller106f4c72002-09-24 16:51:00 +0000281#ifdef MS_WINDOWS
282 if (n == SOCKET_ERROR) {
283 PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());
284 }
285#else
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000286 if (n < 0) {
287 PyErr_SetFromErrno(SelectError);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000288 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000289#endif
Barry Warsawc1cb3601996-12-12 22:16:21 +0000290 else if (n == 0) {
291 /* optimization */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000292 ifdlist = PyList_New(0);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000293 if (ifdlist) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000294 ret = PyTuple_Pack(3, ifdlist, ifdlist, ifdlist);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000295 Py_DECREF(ifdlist);
296 }
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000297 }
Barry Warsawc1cb3601996-12-12 22:16:21 +0000298 else {
299 /* any of these three calls can raise an exception. it's more
300 convenient to test for this after all three calls... but
301 is that acceptable?
302 */
303 ifdlist = set2list(&ifdset, rfd2obj);
304 ofdlist = set2list(&ofdset, wfd2obj);
305 efdlist = set2list(&efdset, efd2obj);
306 if (PyErr_Occurred())
307 ret = NULL;
308 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000309 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000310
Barry Warsawc1cb3601996-12-12 22:16:21 +0000311 Py_DECREF(ifdlist);
312 Py_DECREF(ofdlist);
313 Py_DECREF(efdlist);
314 }
315
316 finally:
317 reap_obj(rfd2obj);
318 reap_obj(wfd2obj);
319 reap_obj(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000320#ifdef SELECT_USES_HEAP
Guido van Rossumd20781b1998-07-02 02:53:36 +0000321 PyMem_DEL(rfd2obj);
322 PyMem_DEL(wfd2obj);
323 PyMem_DEL(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000324#endif /* SELECT_USES_HEAP */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000325 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000326}
327
Nicholas Bastine62c5c82004-03-21 23:45:42 +0000328#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000329/*
330 * poll() support
331 */
332
333typedef struct {
334 PyObject_HEAD
335 PyObject *dict;
336 int ufd_uptodate;
337 int ufd_len;
338 struct pollfd *ufds;
339} pollObject;
340
Jeremy Hylton938ace62002-07-17 16:30:39 +0000341static PyTypeObject poll_Type;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000342
343/* Update the malloc'ed array of pollfds to match the dictionary
344 contained within a pollObject. Return 1 on success, 0 on an error.
345*/
346
347static int
348update_ufd_array(pollObject *self)
349{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000350 Py_ssize_t i, pos;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000351 PyObject *key, *value;
Neal Norwitz83ac0142008-07-28 05:06:20 +0000352 struct pollfd *old_ufds = self->ufds;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000353
354 self->ufd_len = PyDict_Size(self->dict);
Neal Norwitz83ac0142008-07-28 05:06:20 +0000355 PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000356 if (self->ufds == NULL) {
Neal Norwitz83ac0142008-07-28 05:06:20 +0000357 self->ufds = old_ufds;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000358 PyErr_NoMemory();
359 return 0;
360 }
361
362 i = pos = 0;
Fred Drakedff3a372001-07-19 21:29:49 +0000363 while (PyDict_Next(self->dict, &pos, &key, &value)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000364 self->ufds[i].fd = PyInt_AsLong(key);
Fred Drakedff3a372001-07-19 21:29:49 +0000365 self->ufds[i].events = (short)PyInt_AsLong(value);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000366 i++;
367 }
368 self->ufd_uptodate = 1;
369 return 1;
370}
371
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000372PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000373"register(fd [, eventmask] ) -> None\n\n\
374Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000375fd -- either an integer, or an object with a fileno() method returning an\n\
376 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000377events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000378
379static PyObject *
380poll_register(pollObject *self, PyObject *args)
381{
382 PyObject *o, *key, *value;
383 int fd, events = POLLIN | POLLPRI | POLLOUT;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000384 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000385
Fred Drake7b87f852001-05-21 03:29:05 +0000386 if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000387 return NULL;
388 }
389
390 fd = PyObject_AsFileDescriptor(o);
391 if (fd == -1) return NULL;
392
393 /* Add entry to the internal dictionary: the key is the
394 file descriptor, and the value is the event mask. */
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000395 key = PyInt_FromLong(fd);
396 if (key == NULL)
397 return NULL;
398 value = PyInt_FromLong(events);
399 if (value == NULL) {
400 Py_DECREF(key);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000401 return NULL;
402 }
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000403 err = PyDict_SetItem(self->dict, key, value);
404 Py_DECREF(key);
405 Py_DECREF(value);
406 if (err < 0)
407 return NULL;
408
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000409 self->ufd_uptodate = 0;
410
411 Py_INCREF(Py_None);
412 return Py_None;
413}
414
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000415PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000416"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000417Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000418
419static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000420poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000421{
Georg Brandl96a8c392006-05-29 21:04:52 +0000422 PyObject *key;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000423 int fd;
424
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000425 fd = PyObject_AsFileDescriptor( o );
426 if (fd == -1)
427 return NULL;
428
429 /* Check whether the fd is already in the array */
430 key = PyInt_FromLong(fd);
431 if (key == NULL)
432 return NULL;
433
434 if (PyDict_DelItem(self->dict, key) == -1) {
435 Py_DECREF(key);
436 /* This will simply raise the KeyError set by PyDict_DelItem
437 if the file descriptor isn't registered. */
438 return NULL;
439 }
440
441 Py_DECREF(key);
442 self->ufd_uptodate = 0;
443
444 Py_INCREF(Py_None);
445 return Py_None;
446}
447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000448PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000449"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
450Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000451any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000452
453static PyObject *
454poll_poll(pollObject *self, PyObject *args)
455{
456 PyObject *result_list = NULL, *tout = NULL;
457 int timeout = 0, poll_result, i, j;
458 PyObject *value = NULL, *num = NULL;
459
Georg Brandl96a8c392006-05-29 21:04:52 +0000460 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000461 return NULL;
462 }
463
464 /* Check values for timeout */
465 if (tout == NULL || tout == Py_None)
466 timeout = -1;
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000467 else if (!PyNumber_Check(tout)) {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000468 PyErr_SetString(PyExc_TypeError,
469 "timeout must be an integer or None");
470 return NULL;
471 }
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000472 else {
473 tout = PyNumber_Int(tout);
474 if (!tout)
475 return NULL;
Walter Dörwald08c4cc42002-11-12 11:42:20 +0000476 timeout = PyInt_AsLong(tout);
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000477 Py_DECREF(tout);
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000478 if (timeout == -1 && PyErr_Occurred())
479 return NULL;
Neal Norwitz77c72bb2002-07-28 15:12:10 +0000480 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000481
482 /* Ensure the ufd array is up to date */
483 if (!self->ufd_uptodate)
484 if (update_ufd_array(self) == 0)
485 return NULL;
486
487 /* call poll() */
488 Py_BEGIN_ALLOW_THREADS;
489 poll_result = poll(self->ufds, self->ufd_len, timeout);
490 Py_END_ALLOW_THREADS;
491
492 if (poll_result < 0) {
493 PyErr_SetFromErrno(SelectError);
494 return NULL;
495 }
496
497 /* build the result list */
498
499 result_list = PyList_New(poll_result);
500 if (!result_list)
501 return NULL;
502 else {
503 for (i = 0, j = 0; j < poll_result; j++) {
504 /* skip to the next fired descriptor */
505 while (!self->ufds[i].revents) {
506 i++;
507 }
508 /* if we hit a NULL return, set value to NULL
509 and break out of loop; code at end will
510 clean up result_list */
511 value = PyTuple_New(2);
512 if (value == NULL)
513 goto error;
514 num = PyInt_FromLong(self->ufds[i].fd);
515 if (num == NULL) {
516 Py_DECREF(value);
517 goto error;
518 }
519 PyTuple_SET_ITEM(value, 0, num);
520
Andrew M. Kuchlinge5dd1622004-08-07 17:21:27 +0000521 /* The &0xffff is a workaround for AIX. 'revents'
522 is a 16-bit short, and IBM assigned POLLNVAL
523 to be 0x8000, so the conversion to int results
524 in a negative number. See SF bug #923315. */
525 num = PyInt_FromLong(self->ufds[i].revents & 0xffff);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000526 if (num == NULL) {
527 Py_DECREF(value);
528 goto error;
529 }
530 PyTuple_SET_ITEM(value, 1, num);
531 if ((PyList_SetItem(result_list, j, value)) == -1) {
532 Py_DECREF(value);
533 goto error;
534 }
535 i++;
536 }
537 }
538 return result_list;
539
540 error:
541 Py_DECREF(result_list);
542 return NULL;
543}
544
545static PyMethodDef poll_methods[] = {
546 {"register", (PyCFunction)poll_register,
547 METH_VARARGS, poll_register_doc},
548 {"unregister", (PyCFunction)poll_unregister,
Georg Brandl96a8c392006-05-29 21:04:52 +0000549 METH_O, poll_unregister_doc},
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000550 {"poll", (PyCFunction)poll_poll,
551 METH_VARARGS, poll_poll_doc},
552 {NULL, NULL} /* sentinel */
553};
554
555static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000556newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000557{
558 pollObject *self;
559 self = PyObject_New(pollObject, &poll_Type);
560 if (self == NULL)
561 return NULL;
562 /* ufd_uptodate is a Boolean, denoting whether the
563 array pointed to by ufds matches the contents of the dictionary. */
564 self->ufd_uptodate = 0;
565 self->ufds = NULL;
566 self->dict = PyDict_New();
567 if (self->dict == NULL) {
568 Py_DECREF(self);
569 return NULL;
570 }
571 return self;
572}
573
574static void
575poll_dealloc(pollObject *self)
576{
577 if (self->ufds != NULL)
578 PyMem_DEL(self->ufds);
579 Py_XDECREF(self->dict);
580 PyObject_Del(self);
581}
582
583static PyObject *
584poll_getattr(pollObject *self, char *name)
585{
586 return Py_FindMethod(poll_methods, (PyObject *)self, name);
587}
588
Tim Peters0c322792002-07-17 16:49:03 +0000589static PyTypeObject poll_Type = {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000590 /* The ob_type field must be initialized in the module init function
591 * to be portable to Windows without using C++. */
592 PyObject_HEAD_INIT(NULL)
593 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000594 "select.poll", /*tp_name*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000595 sizeof(pollObject), /*tp_basicsize*/
596 0, /*tp_itemsize*/
597 /* methods */
598 (destructor)poll_dealloc, /*tp_dealloc*/
599 0, /*tp_print*/
600 (getattrfunc)poll_getattr, /*tp_getattr*/
601 0, /*tp_setattr*/
602 0, /*tp_compare*/
603 0, /*tp_repr*/
604 0, /*tp_as_number*/
605 0, /*tp_as_sequence*/
606 0, /*tp_as_mapping*/
607 0, /*tp_hash*/
608};
609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000610PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000611"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000612unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000613
614static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000615select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000616{
Georg Brandl96a8c392006-05-29 21:04:52 +0000617 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000618}
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000619
620#ifdef __APPLE__
621/*
622 * On some systems poll() sets errno on invalid file descriptors. We test
623 * for this at runtime because this bug may be fixed or introduced between
624 * OS releases.
625 */
626static int select_have_broken_poll(void)
627{
628 int poll_test;
629 int filedes[2];
630
631 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
632
633 /* Create a file descriptor to make invalid */
634 if (pipe(filedes) < 0) {
635 return 1;
636 }
637 poll_struct.fd = filedes[0];
638 close(filedes[0]);
639 close(filedes[1]);
640 poll_test = poll(&poll_struct, 1, 0);
641 if (poll_test < 0) {
642 return 1;
643 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
644 return 1;
645 }
646 return 0;
647}
648#endif /* __APPLE__ */
649
650#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000652PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000653"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
654\n\
655Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +0000656The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000657rlist -- wait until ready for reading\n\
658wlist -- wait until ready for writing\n\
659xlist -- wait for an ``exceptional condition''\n\
660If only one kind of condition is required, pass [] for the other lists.\n\
661A file descriptor is either a socket or file object, or a small integer\n\
662gotten from a fileno() method call on one of those.\n\
663\n\
664The optional 4th argument specifies a timeout in seconds; it may be\n\
665a floating point number to specify fractions of seconds. If it is absent\n\
666or None, the call will never time out.\n\
667\n\
668The return value is a tuple of three lists corresponding to the first three\n\
669arguments; each contains the subset of the corresponding file descriptors\n\
670that are ready.\n\
671\n\
672*** IMPORTANT NOTICE ***\n\
Neal Norwitz2a30cd02006-07-10 01:18:57 +0000673On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000674
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000675static PyMethodDef select_methods[] = {
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000676 {"select", select_select, METH_VARARGS, select_doc},
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000677#if defined(HAVE_POLL)
Georg Brandl96a8c392006-05-29 21:04:52 +0000678 {"poll", select_poll, METH_NOARGS, poll_doc},
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000679#endif /* HAVE_POLL */
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000680 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +0000681};
682
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000683PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000684"This module supports asynchronous I/O on multiple file descriptors.\n\
685\n\
686*** IMPORTANT NOTICE ***\n\
Neal Norwitz2a30cd02006-07-10 01:18:57 +0000687On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +0000688
Mark Hammond62b1ab12002-07-23 06:31:15 +0000689PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000690initselect(void)
Guido van Rossumed233a51992-06-23 09:07:03 +0000691{
Fred Drake4baedc12002-04-01 14:53:37 +0000692 PyObject *m;
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +0000693 m = Py_InitModule3("select", select_methods, module_doc);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000694 if (m == NULL)
695 return;
Fred Drake4baedc12002-04-01 14:53:37 +0000696
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000697 SelectError = PyErr_NewException("select.error", NULL, NULL);
Fred Drake4baedc12002-04-01 14:53:37 +0000698 Py_INCREF(SelectError);
699 PyModule_AddObject(m, "error", SelectError);
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000700#if defined(HAVE_POLL)
701
702#ifdef __APPLE__
703 if (select_have_broken_poll()) {
704 if (PyObject_DelAttrString(m, "poll") == -1) {
705 PyErr_Clear();
706 }
707 } else {
708#else
709 {
710#endif
711 poll_Type.ob_type = &PyType_Type;
712 PyModule_AddIntConstant(m, "POLLIN", POLLIN);
713 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
714 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
715 PyModule_AddIntConstant(m, "POLLERR", POLLERR);
716 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
717 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000718
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000719#ifdef POLLRDNORM
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000720 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000721#endif
722#ifdef POLLRDBAND
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000723 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000724#endif
725#ifdef POLLWRNORM
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000726 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000727#endif
728#ifdef POLLWRBAND
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000729 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +0000730#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000731#ifdef POLLMSG
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000732 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +0000733#endif
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000734 }
735#endif /* HAVE_POLL */
Guido van Rossumed233a51992-06-23 09:07:03 +0000736}