blob: da03366bb91602a89fad86338b33790eaeb50821 [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"
Christian Heimes0e9ab5f2008-03-21 23:49:44 +000010#include <structmember.h>
Guido van Rossumed233a51992-06-23 09:07:03 +000011
Ronald Oussoren32fd16e2006-04-23 12:36:23 +000012#ifdef __APPLE__
13 /* Perform runtime testing for a broken poll on OSX to make it easier
14 * to use the same binary on multiple releases of the OS.
15 */
16#undef HAVE_BROKEN_POLL
17#endif
18
Tim Petersd92dfe02000-12-12 01:18:41 +000019/* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
20 64 is too small (too many people have bumped into that limit).
21 Here we boost it.
22 Users who want even more than the boosted limit should #define
23 FD_SETSIZE higher before this; e.g., via compiler /D switch.
24*/
25#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
26#define FD_SETSIZE 512
Antoine Pitrouc83ea132010-05-09 14:46:46 +000027#endif
Tim Petersd92dfe02000-12-12 01:18:41 +000028
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000029#if defined(HAVE_POLL_H)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000030#include <poll.h>
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000031#elif defined(HAVE_SYS_POLL_H)
32#include <sys/poll.h>
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000033#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000034
Guido van Rossum37273171996-12-09 18:47:43 +000035#ifdef __sgi
36/* This is missing from unistd.h */
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000037extern void bzero(void *, int);
Guido van Rossum37273171996-12-09 18:47:43 +000038#endif
39
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +000040#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000041#include <sys/types.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000042#endif
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000043
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000044#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000045#include <sys/time.h>
46#include <utils.h>
47#endif
48
Guido van Rossum6f489d91996-06-28 20:15:15 +000049#ifdef MS_WINDOWS
Kristján Valur Jónssonf6f3c4a2010-11-03 13:27:33 +000050# include <winsock2.h>
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000051#else
Neal Norwitz2a30cd02006-07-10 01:18:57 +000052# define SOCKET int
53# ifdef __BEOS__
54# include <net/socket.h>
55# elif defined(__VMS)
56# include <socket.h>
57# endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000058#endif
Guido van Rossumed233a51992-06-23 09:07:03 +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 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000064 PyObject *obj; /* owned reference */
65 SOCKET fd;
66 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000072 int i;
73 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
Serhiy Storchaka98a97222014-02-09 13:14:04 +020074 Py_CLEAR(fd2obj[i].obj);
Antoine Pitrouc83ea132010-05-09 14:46:46 +000075 }
76 fd2obj[0].sentinel = -1;
Barry Warsawc1cb3601996-12-12 22:16:21 +000077}
78
79
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000080/* returns -1 and sets the Python exception if an error occurred, otherwise
81 returns a number >= 0
82*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000083static int
Brett Cannon62dba4c2003-09-10 19:37:42 +000084seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +000085{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000086 int i;
87 int max = -1;
88 int index = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089 PyObject* fast_seq = NULL;
90 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000091
Antoine Pitrouc83ea132010-05-09 14:46:46 +000092 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
93 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000094
Antoine Pitrouc83ea132010-05-09 14:46:46 +000095 fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences");
96 if (!fast_seq)
97 return -1;
98
Antoine Pitrou0552fc22012-11-01 20:13:54 +010099 for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000100 SOCKET v;
101
102 /* any intervening fileno() calls could decr this refcnt */
103 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
Brett Cannon62dba4c2003-09-10 19:37:42 +0000104 return -1;
105
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000106 Py_INCREF(o);
107 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)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000111 max = 0; /* not used for Win32 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000112#else /* !_MSC_VER */
Charles-François Natalifda7b372011-08-28 16:22:33 +0200113 if (!_PyIsSelectable_fd(v)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000114 PyErr_SetString(PyExc_ValueError,
115 "filedescriptor out of range in select()");
116 goto finally;
117 }
118 if (v > max)
119 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000120#endif /* _MSC_VER */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000122
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000123 /* add object and its file descriptor to the list */
124 if (index >= FD_SETSIZE) {
125 PyErr_SetString(PyExc_ValueError,
126 "too many file descriptors in select()");
127 goto finally;
128 }
129 fd2obj[index].obj = o;
130 fd2obj[index].fd = v;
131 fd2obj[index].sentinel = 0;
132 fd2obj[++index].sentinel = -1;
133 }
134 Py_DECREF(fast_seq);
135 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000136
137 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000138 Py_XDECREF(o);
139 Py_DECREF(fast_seq);
140 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 int i, j, count=0;
148 PyObject *list, *o;
149 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000150
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000151 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
152 if (FD_ISSET(fd2obj[j].fd, set))
153 count++;
154 }
155 list = PyList_New(count);
156 if (!list)
157 return NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000158
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 i = 0;
160 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
161 fd = fd2obj[j].fd;
162 if (FD_ISSET(fd, set)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163 o = fd2obj[j].obj;
164 fd2obj[j].obj = NULL;
165 /* transfer ownership */
166 if (PyList_SetItem(list, i, o) < 0)
167 goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000168
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000169 i++;
170 }
171 }
172 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000173 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000174 Py_DECREF(list);
175 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000176}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000177
Barry Warsawb44740f2001-08-16 16:52:59 +0000178#undef SELECT_USES_HEAP
179#if FD_SETSIZE > 1024
180#define SELECT_USES_HEAP
181#endif /* FD_SETSIZE > 1024 */
182
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000183static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000184select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000185{
Barry Warsawb44740f2001-08-16 16:52:59 +0000186#ifdef SELECT_USES_HEAP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 pylist *rfd2obj, *wfd2obj, *efd2obj;
Barry Warsawb44740f2001-08-16 16:52:59 +0000188#else /* !SELECT_USES_HEAP */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 /* XXX: All this should probably be implemented as follows:
190 * - find the highest descriptor we're interested in
191 * - add one
192 * - that's the size
193 * See: Stevens, APitUE, $12.5.1
194 */
195 pylist rfd2obj[FD_SETSIZE + 1];
196 pylist wfd2obj[FD_SETSIZE + 1];
197 pylist efd2obj[FD_SETSIZE + 1];
Barry Warsawb44740f2001-08-16 16:52:59 +0000198#endif /* SELECT_USES_HEAP */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 PyObject *ifdlist, *ofdlist, *efdlist;
200 PyObject *ret = NULL;
201 PyObject *tout = Py_None;
202 fd_set ifdset, ofdset, efdset;
203 double timeout;
204 struct timeval tv, *tvp;
205 long seconds;
206 int imax, omax, emax, max;
207 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000208
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000209 /* convert arguments */
210 if (!PyArg_UnpackTuple(args, "select", 3, 4,
211 &ifdlist, &ofdlist, &efdlist, &tout))
212 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000213
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000214 if (tout == Py_None)
215 tvp = (struct timeval *)0;
216 else if (!PyNumber_Check(tout)) {
217 PyErr_SetString(PyExc_TypeError,
218 "timeout must be a float or None");
219 return NULL;
220 }
221 else {
222 timeout = PyFloat_AsDouble(tout);
223 if (timeout == -1 && PyErr_Occurred())
224 return NULL;
225 if (timeout > (double)LONG_MAX) {
226 PyErr_SetString(PyExc_OverflowError,
227 "timeout period too long");
228 return NULL;
229 }
230 seconds = (long)timeout;
231 timeout = timeout - (double)seconds;
232 tv.tv_sec = seconds;
233 tv.tv_usec = (long)(timeout * 1E6);
234 tvp = &tv;
235 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000236
Guido van Rossumed233a51992-06-23 09:07:03 +0000237
Barry Warsawb44740f2001-08-16 16:52:59 +0000238#ifdef SELECT_USES_HEAP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000239 /* Allocate memory for the lists */
240 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
241 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
242 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
243 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
244 if (rfd2obj) PyMem_DEL(rfd2obj);
245 if (wfd2obj) PyMem_DEL(wfd2obj);
246 if (efd2obj) PyMem_DEL(efd2obj);
247 return PyErr_NoMemory();
248 }
Barry Warsawb44740f2001-08-16 16:52:59 +0000249#endif /* SELECT_USES_HEAP */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000250 /* Convert sequences to fd_sets, and get maximum fd number
251 * propagates the Python exception set in seq2set()
252 */
253 rfd2obj[0].sentinel = -1;
254 wfd2obj[0].sentinel = -1;
255 efd2obj[0].sentinel = -1;
256 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
257 goto finally;
258 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
259 goto finally;
260 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
261 goto finally;
262 max = imax;
263 if (omax > max) max = omax;
264 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000265
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000266 Py_BEGIN_ALLOW_THREADS
267 n = select(max, &ifdset, &ofdset, &efdset, tvp);
268 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000269
Thomas Heller106f4c72002-09-24 16:51:00 +0000270#ifdef MS_WINDOWS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000271 if (n == SOCKET_ERROR) {
272 PyErr_SetExcFromWindowsErr(SelectError, WSAGetLastError());
273 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000274#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000275 if (n < 0) {
276 PyErr_SetFromErrno(SelectError);
277 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000278#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000279 else {
280 /* any of these three calls can raise an exception. it's more
281 convenient to test for this after all three calls... but
282 is that acceptable?
283 */
284 ifdlist = set2list(&ifdset, rfd2obj);
285 ofdlist = set2list(&ofdset, wfd2obj);
286 efdlist = set2list(&efdset, efd2obj);
287 if (PyErr_Occurred())
288 ret = NULL;
289 else
290 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000291
Xiang Zhange6a55dd2017-03-22 12:46:14 +0800292 Py_XDECREF(ifdlist);
293 Py_XDECREF(ofdlist);
294 Py_XDECREF(efdlist);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000295 }
296
Barry Warsawc1cb3601996-12-12 22:16:21 +0000297 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 reap_obj(rfd2obj);
299 reap_obj(wfd2obj);
300 reap_obj(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000301#ifdef SELECT_USES_HEAP
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000302 PyMem_DEL(rfd2obj);
303 PyMem_DEL(wfd2obj);
304 PyMem_DEL(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000305#endif /* SELECT_USES_HEAP */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000306 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000307}
308
Nicholas Bastine62c5c82004-03-21 23:45:42 +0000309#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000310/*
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000311 * poll() support
312 */
313
314typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000315 PyObject_HEAD
316 PyObject *dict;
317 int ufd_uptodate;
318 int ufd_len;
319 struct pollfd *ufds;
Serhiy Storchakac3603892013-08-20 20:38:21 +0300320 int poll_running;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000321} pollObject;
322
Jeremy Hylton938ace62002-07-17 16:30:39 +0000323static PyTypeObject poll_Type;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000324
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000325/* Update the malloc'ed array of pollfds to match the dictionary
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000326 contained within a pollObject. Return 1 on success, 0 on an error.
327*/
328
329static int
330update_ufd_array(pollObject *self)
331{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000332 Py_ssize_t i, pos;
333 PyObject *key, *value;
334 struct pollfd *old_ufds = self->ufds;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000335
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000336 self->ufd_len = PyDict_Size(self->dict);
337 PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
338 if (self->ufds == NULL) {
339 self->ufds = old_ufds;
340 PyErr_NoMemory();
341 return 0;
342 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000343
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 i = pos = 0;
345 while (PyDict_Next(self->dict, &pos, &key, &value)) {
Serhiy Storchaka74f49ab2013-01-19 12:55:39 +0200346 assert(i < self->ufd_len);
347 /* Never overflow */
348 self->ufds[i].fd = (int)PyInt_AsLong(key);
Serhiy Storchakaa92cc912013-12-14 19:11:04 +0200349 self->ufds[i].events = (short)(unsigned short)PyInt_AsLong(value);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000350 i++;
351 }
Serhiy Storchaka74f49ab2013-01-19 12:55:39 +0200352 assert(i == self->ufd_len);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000353 self->ufd_uptodate = 1;
354 return 1;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000355}
356
Serhiy Storchakaa92cc912013-12-14 19:11:04 +0200357static int
358ushort_converter(PyObject *obj, void *ptr)
359{
360 unsigned long uval;
361
362 uval = PyLong_AsUnsignedLong(obj);
363 if (uval == (unsigned long)-1 && PyErr_Occurred())
364 return 0;
365 if (uval > USHRT_MAX) {
366 PyErr_SetString(PyExc_OverflowError,
367 "Python int too large for C unsigned short");
368 return 0;
369 }
370
371 *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short);
372 return 1;
373}
374
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000375PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000376"register(fd [, eventmask] ) -> None\n\n\
377Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000378fd -- either an integer, or an object with a fileno() method returning an\n\
379 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000380events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000381
382static PyObject *
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000383poll_register(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000384{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000385 PyObject *o, *key, *value;
Serhiy Storchakaa92cc912013-12-14 19:11:04 +0200386 int fd;
387 unsigned short events = POLLIN | POLLPRI | POLLOUT;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000388 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000389
Serhiy Storchakaa92cc912013-12-14 19:11:04 +0200390 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000391 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000392
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000393 fd = PyObject_AsFileDescriptor(o);
394 if (fd == -1) return NULL;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000395
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000396 /* Add entry to the internal dictionary: the key is the
397 file descriptor, and the value is the event mask. */
398 key = PyInt_FromLong(fd);
399 if (key == NULL)
400 return NULL;
401 value = PyInt_FromLong(events);
402 if (value == NULL) {
403 Py_DECREF(key);
404 return NULL;
405 }
406 err = PyDict_SetItem(self->dict, key, value);
407 Py_DECREF(key);
408 Py_DECREF(value);
409 if (err < 0)
410 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000411
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000412 self->ufd_uptodate = 0;
413
414 Py_INCREF(Py_None);
415 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000416}
417
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000418PyDoc_STRVAR(poll_modify_doc,
419"modify(fd, eventmask) -> None\n\n\
Andrew M. Kuchlinga8c3f2b2008-03-26 00:16:50 +0000420Modify an already registered file descriptor.\n\
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000421fd -- either an integer, or an object with a fileno() method returning an\n\
422 int.\n\
423events -- an optional bitmask describing the type of events to check for");
424
425static PyObject *
426poll_modify(pollObject *self, PyObject *args)
427{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000428 PyObject *o, *key, *value;
Serhiy Storchakaa92cc912013-12-14 19:11:04 +0200429 int fd;
430 unsigned short events;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000431 int err;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000432
Serhiy Storchakaa92cc912013-12-14 19:11:04 +0200433 if (!PyArg_ParseTuple(args, "OO&:modify", &o, ushort_converter, &events))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000434 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000435
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000436 fd = PyObject_AsFileDescriptor(o);
437 if (fd == -1) return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000438
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000439 /* Modify registered fd */
440 key = PyInt_FromLong(fd);
441 if (key == NULL)
442 return NULL;
443 if (PyDict_GetItem(self->dict, key) == NULL) {
444 errno = ENOENT;
445 PyErr_SetFromErrno(PyExc_IOError);
446 return NULL;
447 }
448 value = PyInt_FromLong(events);
449 if (value == NULL) {
450 Py_DECREF(key);
451 return NULL;
452 }
453 err = PyDict_SetItem(self->dict, key, value);
454 Py_DECREF(key);
455 Py_DECREF(value);
456 if (err < 0)
457 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000458
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000459 self->ufd_uptodate = 0;
460
461 Py_INCREF(Py_None);
462 return Py_None;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000463}
464
465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000466PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000467"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000468Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000469
470static PyObject *
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000471poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000472{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000473 PyObject *key;
474 int fd;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000475
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000476 fd = PyObject_AsFileDescriptor( o );
477 if (fd == -1)
478 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000479
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000480 /* Check whether the fd is already in the array */
481 key = PyInt_FromLong(fd);
482 if (key == NULL)
483 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000484
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000485 if (PyDict_DelItem(self->dict, key) == -1) {
486 Py_DECREF(key);
487 /* This will simply raise the KeyError set by PyDict_DelItem
488 if the file descriptor isn't registered. */
489 return NULL;
490 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000491
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000492 Py_DECREF(key);
493 self->ufd_uptodate = 0;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000494
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000495 Py_INCREF(Py_None);
496 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000497}
498
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000499PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000500"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
501Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000502any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000503
504static PyObject *
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000505poll_poll(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000506{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000507 PyObject *result_list = NULL, *tout = NULL;
508 int timeout = 0, poll_result, i, j;
509 PyObject *value = NULL, *num = NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000510
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000511 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
512 return NULL;
513 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000514
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 /* Check values for timeout */
516 if (tout == NULL || tout == Py_None)
517 timeout = -1;
518 else if (!PyNumber_Check(tout)) {
519 PyErr_SetString(PyExc_TypeError,
520 "timeout must be an integer or None");
521 return NULL;
522 }
523 else {
524 tout = PyNumber_Int(tout);
525 if (!tout)
526 return NULL;
Serhiy Storchaka74f49ab2013-01-19 12:55:39 +0200527 timeout = _PyInt_AsInt(tout);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000528 Py_DECREF(tout);
529 if (timeout == -1 && PyErr_Occurred())
530 return NULL;
531 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000532
Serhiy Storchakac3603892013-08-20 20:38:21 +0300533 /* Avoid concurrent poll() invocation, issue 8865 */
534 if (self->poll_running) {
535 PyErr_SetString(PyExc_RuntimeError,
536 "concurrent poll() invocation");
537 return NULL;
538 }
539
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000540 /* Ensure the ufd array is up to date */
541 if (!self->ufd_uptodate)
542 if (update_ufd_array(self) == 0)
543 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000544
Serhiy Storchakac3603892013-08-20 20:38:21 +0300545 self->poll_running = 1;
546
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000547 /* call poll() */
548 Py_BEGIN_ALLOW_THREADS
549 poll_result = poll(self->ufds, self->ufd_len, timeout);
550 Py_END_ALLOW_THREADS
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000551
Serhiy Storchakac3603892013-08-20 20:38:21 +0300552 self->poll_running = 0;
553
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000554 if (poll_result < 0) {
555 PyErr_SetFromErrno(SelectError);
556 return NULL;
557 }
558
559 /* build the result list */
560
561 result_list = PyList_New(poll_result);
562 if (!result_list)
563 return NULL;
564 else {
565 for (i = 0, j = 0; j < poll_result; j++) {
566 /* skip to the next fired descriptor */
567 while (!self->ufds[i].revents) {
568 i++;
569 }
570 /* if we hit a NULL return, set value to NULL
571 and break out of loop; code at end will
572 clean up result_list */
573 value = PyTuple_New(2);
574 if (value == NULL)
575 goto error;
576 num = PyInt_FromLong(self->ufds[i].fd);
577 if (num == NULL) {
578 Py_DECREF(value);
579 goto error;
580 }
581 PyTuple_SET_ITEM(value, 0, num);
582
583 /* The &0xffff is a workaround for AIX. 'revents'
584 is a 16-bit short, and IBM assigned POLLNVAL
585 to be 0x8000, so the conversion to int results
586 in a negative number. See SF bug #923315. */
587 num = PyInt_FromLong(self->ufds[i].revents & 0xffff);
588 if (num == NULL) {
589 Py_DECREF(value);
590 goto error;
591 }
592 PyTuple_SET_ITEM(value, 1, num);
593 if ((PyList_SetItem(result_list, j, value)) == -1) {
594 Py_DECREF(value);
595 goto error;
596 }
597 i++;
598 }
599 }
600 return result_list;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000601
602 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000603 Py_DECREF(result_list);
604 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000605}
606
607static PyMethodDef poll_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000608 {"register", (PyCFunction)poll_register,
609 METH_VARARGS, poll_register_doc},
610 {"modify", (PyCFunction)poll_modify,
611 METH_VARARGS, poll_modify_doc},
612 {"unregister", (PyCFunction)poll_unregister,
613 METH_O, poll_unregister_doc},
614 {"poll", (PyCFunction)poll_poll,
615 METH_VARARGS, poll_poll_doc},
616 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000617};
618
619static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000620newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000621{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 pollObject *self;
623 self = PyObject_New(pollObject, &poll_Type);
624 if (self == NULL)
625 return NULL;
626 /* ufd_uptodate is a Boolean, denoting whether the
627 array pointed to by ufds matches the contents of the dictionary. */
628 self->ufd_uptodate = 0;
629 self->ufds = NULL;
Serhiy Storchakac3603892013-08-20 20:38:21 +0300630 self->poll_running = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000631 self->dict = PyDict_New();
632 if (self->dict == NULL) {
633 Py_DECREF(self);
634 return NULL;
635 }
636 return self;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000637}
638
639static void
640poll_dealloc(pollObject *self)
641{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000642 if (self->ufds != NULL)
643 PyMem_DEL(self->ufds);
644 Py_XDECREF(self->dict);
645 PyObject_Del(self);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000646}
647
648static PyObject *
649poll_getattr(pollObject *self, char *name)
650{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000651 return Py_FindMethod(poll_methods, (PyObject *)self, name);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000652}
653
Tim Peters0c322792002-07-17 16:49:03 +0000654static PyTypeObject poll_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000655 /* The ob_type field must be initialized in the module init function
656 * to be portable to Windows without using C++. */
657 PyVarObject_HEAD_INIT(NULL, 0)
658 "select.poll", /*tp_name*/
659 sizeof(pollObject), /*tp_basicsize*/
660 0, /*tp_itemsize*/
661 /* methods */
662 (destructor)poll_dealloc, /*tp_dealloc*/
663 0, /*tp_print*/
664 (getattrfunc)poll_getattr, /*tp_getattr*/
665 0, /*tp_setattr*/
666 0, /*tp_compare*/
667 0, /*tp_repr*/
668 0, /*tp_as_number*/
669 0, /*tp_as_sequence*/
670 0, /*tp_as_mapping*/
671 0, /*tp_hash*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000672};
673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000674PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000675"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000676unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000677
678static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000679select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000680{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000681 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000682}
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000683
684#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000685/*
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000686 * On some systems poll() sets errno on invalid file descriptors. We test
687 * for this at runtime because this bug may be fixed or introduced between
688 * OS releases.
689 */
690static int select_have_broken_poll(void)
691{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000692 int poll_test;
693 int filedes[2];
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000694
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000695 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000696
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000697 /* Create a file descriptor to make invalid */
698 if (pipe(filedes) < 0) {
699 return 1;
700 }
701 poll_struct.fd = filedes[0];
702 close(filedes[0]);
703 close(filedes[1]);
704 poll_test = poll(&poll_struct, 1, 0);
705 if (poll_test < 0) {
706 return 1;
707 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
708 return 1;
709 }
710 return 0;
Ronald Oussoren32fd16e2006-04-23 12:36:23 +0000711}
712#endif /* __APPLE__ */
713
714#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000715
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000716#ifdef HAVE_EPOLL
717/* **************************************************************************
718 * epoll interface for Linux 2.6
719 *
720 * Written by Christian Heimes
721 * Inspired by Twisted's _epoll.pyx and select.poll()
722 */
723
724#ifdef HAVE_SYS_EPOLL_H
725#include <sys/epoll.h>
726#endif
727
728typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000729 PyObject_HEAD
730 SOCKET epfd; /* epoll control file descriptor */
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000731} pyEpoll_Object;
732
733static PyTypeObject pyEpoll_Type;
734#define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))
735
736static PyObject *
737pyepoll_err_closed(void)
738{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000739 PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll fd");
740 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000741}
742
743static int
744pyepoll_internal_close(pyEpoll_Object *self)
745{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000746 int save_errno = 0;
747 if (self->epfd >= 0) {
748 int epfd = self->epfd;
749 self->epfd = -1;
750 Py_BEGIN_ALLOW_THREADS
751 if (close(epfd) < 0)
752 save_errno = errno;
753 Py_END_ALLOW_THREADS
754 }
755 return save_errno;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000756}
757
758static PyObject *
759newPyEpoll_Object(PyTypeObject *type, int sizehint, SOCKET fd)
760{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000761 pyEpoll_Object *self;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000762
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000763 if (sizehint == -1) {
764 sizehint = FD_SETSIZE-1;
765 }
766 else if (sizehint < 1) {
767 PyErr_Format(PyExc_ValueError,
768 "sizehint must be greater zero, got %d",
769 sizehint);
770 return NULL;
771 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000772
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000773 assert(type != NULL && type->tp_alloc != NULL);
774 self = (pyEpoll_Object *) type->tp_alloc(type, 0);
775 if (self == NULL)
776 return NULL;
777
778 if (fd == -1) {
779 Py_BEGIN_ALLOW_THREADS
780 self->epfd = epoll_create(sizehint);
781 Py_END_ALLOW_THREADS
782 }
783 else {
784 self->epfd = fd;
785 }
786 if (self->epfd < 0) {
787 Py_DECREF(self);
788 PyErr_SetFromErrno(PyExc_IOError);
789 return NULL;
790 }
791 return (PyObject *)self;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000792}
793
794
795static PyObject *
796pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
797{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000798 int sizehint = -1;
799 static char *kwlist[] = {"sizehint", NULL};
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000800
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000801 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:epoll", kwlist,
802 &sizehint))
803 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000804
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000805 return newPyEpoll_Object(type, sizehint, -1);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000806}
807
808
809static void
810pyepoll_dealloc(pyEpoll_Object *self)
811{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000812 (void)pyepoll_internal_close(self);
813 Py_TYPE(self)->tp_free(self);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000814}
815
816static PyObject*
817pyepoll_close(pyEpoll_Object *self)
818{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000819 errno = pyepoll_internal_close(self);
820 if (errno < 0) {
821 PyErr_SetFromErrno(PyExc_IOError);
822 return NULL;
823 }
824 Py_RETURN_NONE;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000825}
826
827PyDoc_STRVAR(pyepoll_close_doc,
828"close() -> None\n\
829\n\
830Close the epoll control file descriptor. Further operations on the epoll\n\
831object will raise an exception.");
832
833static PyObject*
834pyepoll_get_closed(pyEpoll_Object *self)
835{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000836 if (self->epfd < 0)
837 Py_RETURN_TRUE;
838 else
839 Py_RETURN_FALSE;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000840}
841
842static PyObject*
843pyepoll_fileno(pyEpoll_Object *self)
844{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000845 if (self->epfd < 0)
846 return pyepoll_err_closed();
847 return PyInt_FromLong(self->epfd);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000848}
849
850PyDoc_STRVAR(pyepoll_fileno_doc,
851"fileno() -> int\n\
852\n\
853Return the epoll control file descriptor.");
854
855static PyObject*
856pyepoll_fromfd(PyObject *cls, PyObject *args)
857{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000858 SOCKET fd;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000859
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000860 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
861 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000862
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000863 return newPyEpoll_Object((PyTypeObject*)cls, -1, fd);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000864}
865
866PyDoc_STRVAR(pyepoll_fromfd_doc,
867"fromfd(fd) -> epoll\n\
868\n\
869Create an epoll object from a given control fd.");
870
871static PyObject *
872pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
873{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000874 struct epoll_event ev;
875 int result;
876 int fd;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000877
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000878 if (epfd < 0)
879 return pyepoll_err_closed();
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000880
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000881 fd = PyObject_AsFileDescriptor(pfd);
882 if (fd == -1) {
883 return NULL;
884 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000885
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000886 switch(op) {
887 case EPOLL_CTL_ADD:
888 case EPOLL_CTL_MOD:
889 ev.events = events;
890 ev.data.fd = fd;
891 Py_BEGIN_ALLOW_THREADS
892 result = epoll_ctl(epfd, op, fd, &ev);
893 Py_END_ALLOW_THREADS
894 break;
895 case EPOLL_CTL_DEL:
896 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
897 * operation required a non-NULL pointer in event, even
898 * though this argument is ignored. */
899 Py_BEGIN_ALLOW_THREADS
900 result = epoll_ctl(epfd, op, fd, &ev);
901 if (errno == EBADF) {
902 /* fd already closed */
903 result = 0;
904 errno = 0;
905 }
906 Py_END_ALLOW_THREADS
907 break;
908 default:
909 result = -1;
910 errno = EINVAL;
911 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000912
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 if (result < 0) {
914 PyErr_SetFromErrno(PyExc_IOError);
915 return NULL;
916 }
917 Py_RETURN_NONE;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000918}
919
920static PyObject *
921pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
922{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000923 PyObject *pfd;
924 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
925 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000926
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
928 &pfd, &events)) {
929 return NULL;
930 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000931
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000932 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000933}
934
935PyDoc_STRVAR(pyepoll_register_doc,
Georg Brandl7d4bfb32010-08-02 21:44:25 +0000936"register(fd[, eventmask]) -> None\n\
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000937\n\
Senthil Kumaran2bd91002011-06-26 23:50:35 -0700938Registers a new fd or raises an IOError if the fd is already registered.\n\
Andrew M. Kuchlinga8c3f2b2008-03-26 00:16:50 +0000939fd is the target file descriptor of the operation.\n\
940events is a bit set composed of the various EPOLL constants; the default\n\
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000941is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
942\n\
943The epoll interface supports all file descriptors that support poll.");
944
945static PyObject *
946pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
947{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 PyObject *pfd;
949 unsigned int events;
950 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000951
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000952 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
953 &pfd, &events)) {
954 return NULL;
955 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000956
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000957 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000958}
959
960PyDoc_STRVAR(pyepoll_modify_doc,
961"modify(fd, eventmask) -> None\n\
962\n\
963fd is the target file descriptor of the operation\n\
964events is a bit set composed of the various EPOLL constants");
965
966static PyObject *
967pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
968{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000969 PyObject *pfd;
970 static char *kwlist[] = {"fd", NULL};
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000971
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000972 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
973 &pfd)) {
974 return NULL;
975 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000976
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000977 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000978}
979
980PyDoc_STRVAR(pyepoll_unregister_doc,
981"unregister(fd) -> None\n\
982\n\
983fd is the target file descriptor of the operation.");
984
985static PyObject *
986pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
987{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000988 double dtimeout = -1.;
989 int timeout;
990 int maxevents = -1;
991 int nfds, i;
992 PyObject *elist = NULL, *etuple = NULL;
993 struct epoll_event *evs = NULL;
994 static char *kwlist[] = {"timeout", "maxevents", NULL};
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000995
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000996 if (self->epfd < 0)
997 return pyepoll_err_closed();
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000998
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000999 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
1000 &dtimeout, &maxevents)) {
1001 return NULL;
1002 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001003
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001004 if (dtimeout < 0) {
1005 timeout = -1;
1006 }
1007 else if (dtimeout * 1000.0 > INT_MAX) {
1008 PyErr_SetString(PyExc_OverflowError,
1009 "timeout is too large");
1010 return NULL;
1011 }
1012 else {
1013 timeout = (int)(dtimeout * 1000.0);
1014 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001015
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001016 if (maxevents == -1) {
1017 maxevents = FD_SETSIZE-1;
1018 }
1019 else if (maxevents < 1) {
1020 PyErr_Format(PyExc_ValueError,
1021 "maxevents must be greater than 0, got %d",
1022 maxevents);
1023 return NULL;
1024 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001025
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001026 evs = PyMem_New(struct epoll_event, maxevents);
1027 if (evs == NULL) {
1028 Py_DECREF(self);
1029 PyErr_NoMemory();
1030 return NULL;
1031 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001032
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001033 Py_BEGIN_ALLOW_THREADS
1034 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
1035 Py_END_ALLOW_THREADS
1036 if (nfds < 0) {
1037 PyErr_SetFromErrno(PyExc_IOError);
1038 goto error;
1039 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001040
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001041 elist = PyList_New(nfds);
1042 if (elist == NULL) {
1043 goto error;
1044 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001045
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001046 for (i = 0; i < nfds; i++) {
1047 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
1048 if (etuple == NULL) {
1049 Py_CLEAR(elist);
1050 goto error;
1051 }
1052 PyList_SET_ITEM(elist, i, etuple);
1053 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001054
Georg Brandl018a3622008-03-26 12:57:47 +00001055 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001056 PyMem_Free(evs);
1057 return elist;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001058}
1059
1060PyDoc_STRVAR(pyepoll_poll_doc,
1061"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1062\n\
1063Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1064in seconds (as float). -1 makes poll wait indefinitely.\n\
1065Up to maxevents are returned to the caller.");
1066
1067static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001068 {"fromfd", (PyCFunction)pyepoll_fromfd,
1069 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1070 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1071 pyepoll_close_doc},
1072 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1073 pyepoll_fileno_doc},
1074 {"modify", (PyCFunction)pyepoll_modify,
1075 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1076 {"register", (PyCFunction)pyepoll_register,
1077 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1078 {"unregister", (PyCFunction)pyepoll_unregister,
1079 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1080 {"poll", (PyCFunction)pyepoll_poll,
1081 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
1082 {NULL, NULL},
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001083};
1084
1085static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001086 {"closed", (getter)pyepoll_get_closed, NULL,
1087 "True if the epoll handler is closed"},
1088 {0},
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001089};
1090
1091PyDoc_STRVAR(pyepoll_doc,
1092"select.epoll([sizehint=-1])\n\
1093\n\
1094Returns an epolling object\n\
1095\n\
1096sizehint must be a positive integer or -1 for the default size. The\n\
1097sizehint is used to optimize internal data structures. It doesn't limit\n\
1098the maximum number of monitored events.");
1099
1100static PyTypeObject pyEpoll_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001101 PyVarObject_HEAD_INIT(NULL, 0)
1102 "select.epoll", /* tp_name */
1103 sizeof(pyEpoll_Object), /* tp_basicsize */
1104 0, /* tp_itemsize */
1105 (destructor)pyepoll_dealloc, /* tp_dealloc */
1106 0, /* tp_print */
1107 0, /* tp_getattr */
1108 0, /* tp_setattr */
1109 0, /* tp_compare */
1110 0, /* tp_repr */
1111 0, /* tp_as_number */
1112 0, /* tp_as_sequence */
1113 0, /* tp_as_mapping */
1114 0, /* tp_hash */
1115 0, /* tp_call */
1116 0, /* tp_str */
1117 PyObject_GenericGetAttr, /* tp_getattro */
1118 0, /* tp_setattro */
1119 0, /* tp_as_buffer */
1120 Py_TPFLAGS_DEFAULT, /* tp_flags */
1121 pyepoll_doc, /* tp_doc */
1122 0, /* tp_traverse */
1123 0, /* tp_clear */
1124 0, /* tp_richcompare */
1125 0, /* tp_weaklistoffset */
1126 0, /* tp_iter */
1127 0, /* tp_iternext */
1128 pyepoll_methods, /* tp_methods */
1129 0, /* tp_members */
1130 pyepoll_getsetlist, /* tp_getset */
1131 0, /* tp_base */
1132 0, /* tp_dict */
1133 0, /* tp_descr_get */
1134 0, /* tp_descr_set */
1135 0, /* tp_dictoffset */
1136 0, /* tp_init */
1137 0, /* tp_alloc */
1138 pyepoll_new, /* tp_new */
1139 0, /* tp_free */
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001140};
1141
1142#endif /* HAVE_EPOLL */
1143
1144#ifdef HAVE_KQUEUE
1145/* **************************************************************************
1146 * kqueue interface for BSD
1147 *
1148 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1149 * All rights reserved.
1150 *
1151 * Redistribution and use in source and binary forms, with or without
1152 * modification, are permitted provided that the following conditions
1153 * are met:
1154 * 1. Redistributions of source code must retain the above copyright
1155 * notice, this list of conditions and the following disclaimer.
1156 * 2. Redistributions in binary form must reproduce the above copyright
1157 * notice, this list of conditions and the following disclaimer in the
1158 * documentation and/or other materials provided with the distribution.
1159 *
1160 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1161 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1162 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1163 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1164 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1165 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1166 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1167 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1168 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1169 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1170 * SUCH DAMAGE.
1171 */
1172
1173#ifdef HAVE_SYS_EVENT_H
1174#include <sys/event.h>
1175#endif
1176
1177PyDoc_STRVAR(kqueue_event_doc,
Georg Brandlfa1ffb62009-12-29 21:09:17 +00001178"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001179\n\
1180This object is the equivalent of the struct kevent for the C API.\n\
1181\n\
1182See the kqueue manpage for more detailed information about the meaning\n\
1183of the arguments.\n\
1184\n\
1185One minor note: while you might hope that udata could store a\n\
1186reference to a python object, it cannot, because it is impossible to\n\
1187keep a proper reference count of the object once it's passed into the\n\
1188kernel. Therefore, I have restricted it to only storing an integer. I\n\
1189recommend ignoring it and simply using the 'ident' field to key off\n\
1190of. You could also set up a dictionary on the python side to store a\n\
1191udata->object mapping.");
1192
1193typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001194 PyObject_HEAD
1195 struct kevent e;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001196} kqueue_event_Object;
1197
1198static PyTypeObject kqueue_event_Type;
1199
1200#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1201
1202typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001203 PyObject_HEAD
1204 SOCKET kqfd; /* kqueue control fd */
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001205} kqueue_queue_Object;
1206
1207static PyTypeObject kqueue_queue_Type;
1208
1209#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1210
Antoine Pitrou323b9da2009-11-04 19:25:14 +00001211#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1212# error uintptr_t does not match void *!
1213#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1214# define T_UINTPTRT T_ULONGLONG
1215# define T_INTPTRT T_LONGLONG
1216# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1217# define UINTPTRT_FMT_UNIT "K"
1218# define INTPTRT_FMT_UNIT "L"
1219#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1220# define T_UINTPTRT T_ULONG
1221# define T_INTPTRT T_LONG
1222# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1223# define UINTPTRT_FMT_UNIT "k"
1224# define INTPTRT_FMT_UNIT "l"
1225#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1226# define T_UINTPTRT T_UINT
1227# define T_INTPTRT T_INT
1228# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1229# define UINTPTRT_FMT_UNIT "I"
1230# define INTPTRT_FMT_UNIT "i"
1231#else
1232# error uintptr_t does not match int, long, or long long!
1233#endif
1234
Charles-Francois Natali880359c2013-05-06 21:21:57 +02001235/*
1236 * kevent is not standard and its members vary across BSDs.
1237 */
1238#if !defined(__OpenBSD__)
1239# define IDENT_TYPE T_UINTPTRT
1240# define IDENT_CAST Py_intptr_t
1241# define DATA_TYPE T_INTPTRT
1242# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
1243# define IDENT_AsType PyLong_AsUintptr_t
1244#else
1245# define IDENT_TYPE T_UINT
1246# define IDENT_CAST int
1247# define DATA_TYPE T_INT
1248# define DATA_FMT_UNIT "i"
1249# define IDENT_AsType PyLong_AsUnsignedLong
1250#endif
1251
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001252/* Unfortunately, we can't store python objects in udata, because
1253 * kevents in the kernel can be removed without warning, which would
1254 * forever lose the refcount on the object stored with it.
1255 */
1256
1257#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1258static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali880359c2013-05-06 21:21:57 +02001259 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001260 {"filter", T_SHORT, KQ_OFF(e.filter)},
1261 {"flags", T_USHORT, KQ_OFF(e.flags)},
1262 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali880359c2013-05-06 21:21:57 +02001263 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001264 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1265 {NULL} /* Sentinel */
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001266};
1267#undef KQ_OFF
1268
1269static PyObject *
Georg Brandlea370a92010-02-23 21:48:57 +00001270
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001271kqueue_event_repr(kqueue_event_Object *s)
1272{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001273 char buf[1024];
1274 PyOS_snprintf(
1275 buf, sizeof(buf),
1276 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1277 "data=0x%zd udata=%p>",
1278 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1279 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1280 return PyString_FromString(buf);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001281}
1282
1283static int
1284kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1285{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001286 PyObject *pfd;
1287 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1288 "data", "udata", NULL};
Christian Heimes42831fe2013-08-25 14:57:00 +02001289 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001290
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001291 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001292
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001293 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1294 &pfd, &(self->e.filter), &(self->e.flags),
1295 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1296 return -1;
1297 }
1298
Charles-Francois Natali880359c2013-05-06 21:21:57 +02001299 if (PyLong_Check(pfd)
1300#if IDENT_TYPE == T_UINT
Xiang Zhange6a55dd2017-03-22 12:46:14 +08001301 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
Charles-Francois Natali880359c2013-05-06 21:21:57 +02001302#endif
1303 ) {
1304 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001305 }
1306 else {
1307 self->e.ident = PyObject_AsFileDescriptor(pfd);
1308 }
1309 if (PyErr_Occurred()) {
1310 return -1;
1311 }
1312 return 0;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001313}
1314
1315static PyObject *
1316kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001317 int op)
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001318{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001319 Py_intptr_t result = 0;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001320
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001321 if (!kqueue_event_Check(o)) {
1322 if (op == Py_EQ || op == Py_NE) {
1323 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1324 Py_INCREF(res);
1325 return res;
1326 }
1327 PyErr_Format(PyExc_TypeError,
1328 "can't compare %.200s to %.200s",
1329 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1330 return NULL;
1331 }
Charles-Francois Natali880359c2013-05-06 21:21:57 +02001332 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001333 ((result = s->e.filter - o->e.filter) == 0) &&
1334 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali880359c2013-05-06 21:21:57 +02001335 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001336 ((result = s->e.data - o->e.data) == 0) &&
1337 ((result = s->e.udata - o->e.udata) == 0)
1338 ) {
1339 result = 0;
1340 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001341
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001342 switch (op) {
1343 case Py_EQ:
1344 result = (result == 0);
1345 break;
1346 case Py_NE:
1347 result = (result != 0);
1348 break;
1349 case Py_LE:
1350 result = (result <= 0);
1351 break;
1352 case Py_GE:
1353 result = (result >= 0);
1354 break;
1355 case Py_LT:
1356 result = (result < 0);
1357 break;
1358 case Py_GT:
1359 result = (result > 0);
1360 break;
1361 }
1362 return PyBool_FromLong((long)result);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001363}
1364
1365static PyTypeObject kqueue_event_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001366 PyVarObject_HEAD_INIT(NULL, 0)
1367 "select.kevent", /* tp_name */
1368 sizeof(kqueue_event_Object), /* tp_basicsize */
1369 0, /* tp_itemsize */
1370 0, /* tp_dealloc */
1371 0, /* tp_print */
1372 0, /* tp_getattr */
1373 0, /* tp_setattr */
1374 0, /* tp_compare */
1375 (reprfunc)kqueue_event_repr, /* tp_repr */
1376 0, /* tp_as_number */
1377 0, /* tp_as_sequence */
1378 0, /* tp_as_mapping */
1379 0, /* tp_hash */
1380 0, /* tp_call */
1381 0, /* tp_str */
1382 0, /* tp_getattro */
1383 0, /* tp_setattro */
1384 0, /* tp_as_buffer */
1385 Py_TPFLAGS_DEFAULT, /* tp_flags */
1386 kqueue_event_doc, /* tp_doc */
1387 0, /* tp_traverse */
1388 0, /* tp_clear */
1389 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1390 0, /* tp_weaklistoffset */
1391 0, /* tp_iter */
1392 0, /* tp_iternext */
1393 0, /* tp_methods */
1394 kqueue_event_members, /* tp_members */
1395 0, /* tp_getset */
1396 0, /* tp_base */
1397 0, /* tp_dict */
1398 0, /* tp_descr_get */
1399 0, /* tp_descr_set */
1400 0, /* tp_dictoffset */
1401 (initproc)kqueue_event_init, /* tp_init */
1402 0, /* tp_alloc */
1403 0, /* tp_new */
1404 0, /* tp_free */
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001405};
1406
1407static PyObject *
1408kqueue_queue_err_closed(void)
1409{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001410 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue fd");
1411 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001412}
1413
1414static int
1415kqueue_queue_internal_close(kqueue_queue_Object *self)
1416{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001417 int save_errno = 0;
1418 if (self->kqfd >= 0) {
1419 int kqfd = self->kqfd;
1420 self->kqfd = -1;
1421 Py_BEGIN_ALLOW_THREADS
1422 if (close(kqfd) < 0)
1423 save_errno = errno;
1424 Py_END_ALLOW_THREADS
1425 }
1426 return save_errno;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001427}
1428
1429static PyObject *
1430newKqueue_Object(PyTypeObject *type, SOCKET fd)
1431{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001432 kqueue_queue_Object *self;
1433 assert(type != NULL && type->tp_alloc != NULL);
1434 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1435 if (self == NULL) {
1436 return NULL;
1437 }
1438
1439 if (fd == -1) {
1440 Py_BEGIN_ALLOW_THREADS
1441 self->kqfd = kqueue();
1442 Py_END_ALLOW_THREADS
1443 }
1444 else {
1445 self->kqfd = fd;
1446 }
1447 if (self->kqfd < 0) {
1448 Py_DECREF(self);
1449 PyErr_SetFromErrno(PyExc_IOError);
1450 return NULL;
1451 }
1452 return (PyObject *)self;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001453}
1454
1455static PyObject *
1456kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1457{
1458
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001459 if ((args != NULL && PyObject_Size(args)) ||
1460 (kwds != NULL && PyObject_Size(kwds))) {
1461 PyErr_SetString(PyExc_ValueError,
1462 "select.kqueue doesn't accept arguments");
1463 return NULL;
1464 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001465
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001466 return newKqueue_Object(type, -1);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001467}
1468
1469static void
1470kqueue_queue_dealloc(kqueue_queue_Object *self)
1471{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001472 kqueue_queue_internal_close(self);
1473 Py_TYPE(self)->tp_free(self);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001474}
1475
1476static PyObject*
1477kqueue_queue_close(kqueue_queue_Object *self)
1478{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001479 errno = kqueue_queue_internal_close(self);
1480 if (errno < 0) {
1481 PyErr_SetFromErrno(PyExc_IOError);
1482 return NULL;
1483 }
1484 Py_RETURN_NONE;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001485}
1486
1487PyDoc_STRVAR(kqueue_queue_close_doc,
1488"close() -> None\n\
1489\n\
1490Close the kqueue control file descriptor. Further operations on the kqueue\n\
1491object will raise an exception.");
1492
1493static PyObject*
1494kqueue_queue_get_closed(kqueue_queue_Object *self)
1495{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001496 if (self->kqfd < 0)
1497 Py_RETURN_TRUE;
1498 else
1499 Py_RETURN_FALSE;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001500}
1501
1502static PyObject*
1503kqueue_queue_fileno(kqueue_queue_Object *self)
1504{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001505 if (self->kqfd < 0)
1506 return kqueue_queue_err_closed();
1507 return PyInt_FromLong(self->kqfd);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001508}
1509
1510PyDoc_STRVAR(kqueue_queue_fileno_doc,
1511"fileno() -> int\n\
1512\n\
1513Return the kqueue control file descriptor.");
1514
1515static PyObject*
1516kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1517{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001518 SOCKET fd;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001519
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001520 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1521 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001522
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001523 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001524}
1525
1526PyDoc_STRVAR(kqueue_queue_fromfd_doc,
1527"fromfd(fd) -> kqueue\n\
1528\n\
1529Create a kqueue object from a given control fd.");
1530
1531static PyObject *
1532kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
1533{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001534 int nevents = 0;
1535 int gotevents = 0;
1536 int nchanges = 0;
1537 int i = 0;
1538 PyObject *otimeout = NULL;
1539 PyObject *ch = NULL;
1540 PyObject *it = NULL, *ei = NULL;
1541 PyObject *result = NULL;
1542 struct kevent *evl = NULL;
1543 struct kevent *chl = NULL;
1544 struct timespec timeoutspec;
1545 struct timespec *ptimeoutspec;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001546
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001547 if (self->kqfd < 0)
1548 return kqueue_queue_err_closed();
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001549
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001550 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
1551 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001552
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001553 if (nevents < 0) {
1554 PyErr_Format(PyExc_ValueError,
1555 "Length of eventlist must be 0 or positive, got %d",
1556 nevents);
1557 return NULL;
1558 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001559
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001560 if (otimeout == Py_None || otimeout == NULL) {
1561 ptimeoutspec = NULL;
1562 }
1563 else if (PyNumber_Check(otimeout)) {
1564 double timeout;
1565 long seconds;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001566
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001567 timeout = PyFloat_AsDouble(otimeout);
1568 if (timeout == -1 && PyErr_Occurred())
1569 return NULL;
1570 if (timeout > (double)LONG_MAX) {
1571 PyErr_SetString(PyExc_OverflowError,
1572 "timeout period too long");
1573 return NULL;
1574 }
1575 if (timeout < 0) {
1576 PyErr_SetString(PyExc_ValueError,
1577 "timeout must be positive or None");
1578 return NULL;
1579 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001580
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001581 seconds = (long)timeout;
1582 timeout = timeout - (double)seconds;
1583 timeoutspec.tv_sec = seconds;
1584 timeoutspec.tv_nsec = (long)(timeout * 1E9);
1585 ptimeoutspec = &timeoutspec;
1586 }
1587 else {
1588 PyErr_Format(PyExc_TypeError,
1589 "timeout argument must be an number "
1590 "or None, got %.200s",
1591 Py_TYPE(otimeout)->tp_name);
1592 return NULL;
1593 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001594
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001595 if (ch != NULL && ch != Py_None) {
1596 it = PyObject_GetIter(ch);
1597 if (it == NULL) {
1598 PyErr_SetString(PyExc_TypeError,
1599 "changelist is not iterable");
1600 return NULL;
1601 }
1602 nchanges = PyObject_Size(ch);
1603 if (nchanges < 0) {
1604 goto error;
1605 }
Georg Brandlea370a92010-02-23 21:48:57 +00001606
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001607 chl = PyMem_New(struct kevent, nchanges);
1608 if (chl == NULL) {
1609 PyErr_NoMemory();
1610 goto error;
1611 }
1612 i = 0;
1613 while ((ei = PyIter_Next(it)) != NULL) {
1614 if (!kqueue_event_Check(ei)) {
1615 Py_DECREF(ei);
1616 PyErr_SetString(PyExc_TypeError,
1617 "changelist must be an iterable of "
1618 "select.kevent objects");
1619 goto error;
1620 } else {
1621 chl[i++] = ((kqueue_event_Object *)ei)->e;
1622 }
1623 Py_DECREF(ei);
1624 }
1625 }
1626 Py_CLEAR(it);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001627
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001628 /* event list */
1629 if (nevents) {
1630 evl = PyMem_New(struct kevent, nevents);
1631 if (evl == NULL) {
1632 PyErr_NoMemory();
1633 goto error;
1634 }
1635 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001636
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001637 Py_BEGIN_ALLOW_THREADS
1638 gotevents = kevent(self->kqfd, chl, nchanges,
1639 evl, nevents, ptimeoutspec);
1640 Py_END_ALLOW_THREADS
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001641
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001642 if (gotevents == -1) {
1643 PyErr_SetFromErrno(PyExc_OSError);
1644 goto error;
1645 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001646
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001647 result = PyList_New(gotevents);
1648 if (result == NULL) {
1649 goto error;
1650 }
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001651
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001652 for (i = 0; i < gotevents; i++) {
1653 kqueue_event_Object *ch;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001654
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001655 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
1656 if (ch == NULL) {
1657 goto error;
1658 }
1659 ch->e = evl[i];
1660 PyList_SET_ITEM(result, i, (PyObject *)ch);
1661 }
1662 PyMem_Free(chl);
1663 PyMem_Free(evl);
1664 return result;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001665
1666 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001667 PyMem_Free(chl);
1668 PyMem_Free(evl);
1669 Py_XDECREF(result);
1670 Py_XDECREF(it);
1671 return NULL;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001672}
1673
1674PyDoc_STRVAR(kqueue_queue_control_doc,
Georg Brandl2f3bd832008-09-21 07:14:44 +00001675"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001676\n\
1677Calls the kernel kevent function.\n\
1678- changelist must be a list of kevent objects describing the changes\n\
1679 to be made to the kernel's watch list or None.\n\
1680- max_events lets you specify the maximum number of events that the\n\
1681 kernel will return.\n\
1682- timeout is the maximum time to wait in seconds, or else None,\n\
1683 to wait forever. timeout accepts floats for smaller timeouts, too.");
1684
1685
1686static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001687 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
1688 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
1689 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
1690 kqueue_queue_close_doc},
1691 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
1692 kqueue_queue_fileno_doc},
1693 {"control", (PyCFunction)kqueue_queue_control,
1694 METH_VARARGS , kqueue_queue_control_doc},
1695 {NULL, NULL},
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001696};
1697
1698static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001699 {"closed", (getter)kqueue_queue_get_closed, NULL,
1700 "True if the kqueue handler is closed"},
1701 {0},
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001702};
1703
1704PyDoc_STRVAR(kqueue_queue_doc,
1705"Kqueue syscall wrapper.\n\
1706\n\
1707For example, to start watching a socket for input:\n\
1708>>> kq = kqueue()\n\
1709>>> sock = socket()\n\
1710>>> sock.connect((host, port))\n\
1711>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
1712\n\
1713To wait one second for it to become writeable:\n\
1714>>> kq.control(None, 1, 1000)\n\
1715\n\
1716To stop listening:\n\
1717>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
1718
1719static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001720 PyVarObject_HEAD_INIT(NULL, 0)
1721 "select.kqueue", /* tp_name */
1722 sizeof(kqueue_queue_Object), /* tp_basicsize */
1723 0, /* tp_itemsize */
1724 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
1725 0, /* tp_print */
1726 0, /* tp_getattr */
1727 0, /* tp_setattr */
1728 0, /* tp_compare */
1729 0, /* tp_repr */
1730 0, /* tp_as_number */
1731 0, /* tp_as_sequence */
1732 0, /* tp_as_mapping */
1733 0, /* tp_hash */
1734 0, /* tp_call */
1735 0, /* tp_str */
1736 0, /* tp_getattro */
1737 0, /* tp_setattro */
1738 0, /* tp_as_buffer */
1739 Py_TPFLAGS_DEFAULT, /* tp_flags */
1740 kqueue_queue_doc, /* tp_doc */
1741 0, /* tp_traverse */
1742 0, /* tp_clear */
1743 0, /* tp_richcompare */
1744 0, /* tp_weaklistoffset */
1745 0, /* tp_iter */
1746 0, /* tp_iternext */
1747 kqueue_queue_methods, /* tp_methods */
1748 0, /* tp_members */
1749 kqueue_queue_getsetlist, /* tp_getset */
1750 0, /* tp_base */
1751 0, /* tp_dict */
1752 0, /* tp_descr_get */
1753 0, /* tp_descr_set */
1754 0, /* tp_dictoffset */
1755 0, /* tp_init */
1756 0, /* tp_alloc */
1757 kqueue_queue_new, /* tp_new */
1758 0, /* tp_free */
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001759};
1760
1761#endif /* HAVE_KQUEUE */
1762/* ************************************************************************ */
1763
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001764PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001765"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
1766\n\
1767Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00001768The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001769rlist -- wait until ready for reading\n\
1770wlist -- wait until ready for writing\n\
1771xlist -- wait for an ``exceptional condition''\n\
1772If only one kind of condition is required, pass [] for the other lists.\n\
1773A file descriptor is either a socket or file object, or a small integer\n\
1774gotten from a fileno() method call on one of those.\n\
1775\n\
1776The optional 4th argument specifies a timeout in seconds; it may be\n\
1777a floating point number to specify fractions of seconds. If it is absent\n\
1778or None, the call will never time out.\n\
1779\n\
1780The return value is a tuple of three lists corresponding to the first three\n\
1781arguments; each contains the subset of the corresponding file descriptors\n\
1782that are ready.\n\
1783\n\
1784*** IMPORTANT NOTICE ***\n\
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001785On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
Andrew M. Kuchlinga8c3f2b2008-03-26 00:16:50 +00001786descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001787
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00001788static PyMethodDef select_methods[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001789 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natalia41cf292013-01-19 12:15:56 +01001790#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001791 {"poll", select_poll, METH_NOARGS, poll_doc},
Ronald Oussoren32fd16e2006-04-23 12:36:23 +00001792#endif /* HAVE_POLL */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001793 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00001794};
1795
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001796PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00001797"This module supports asynchronous I/O on multiple file descriptors.\n\
1798\n\
1799*** IMPORTANT NOTICE ***\n\
Neal Norwitz2a30cd02006-07-10 01:18:57 +00001800On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00001801
Mark Hammond62b1ab12002-07-23 06:31:15 +00001802PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001803initselect(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00001804{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001805 PyObject *m;
1806 m = Py_InitModule3("select", select_methods, module_doc);
1807 if (m == NULL)
1808 return;
Fred Drake4baedc12002-04-01 14:53:37 +00001809
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001810 SelectError = PyErr_NewException("select.error", NULL, NULL);
1811 Py_INCREF(SelectError);
1812 PyModule_AddObject(m, "error", SelectError);
Ronald Oussoren32fd16e2006-04-23 12:36:23 +00001813
Amaury Forgeot d'Arcce32eb72009-07-09 22:37:22 +00001814#ifdef PIPE_BUF
R. David Murray1d9d16e2010-10-16 00:43:13 +00001815#ifdef HAVE_BROKEN_PIPE_BUF
1816#undef PIPE_BUF
1817#define PIPE_BUF 512
1818#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001819 PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF);
Amaury Forgeot d'Arcce32eb72009-07-09 22:37:22 +00001820#endif
Gregory P. Smith9d36fd22009-07-03 20:48:31 +00001821
Charles-François Natalia41cf292013-01-19 12:15:56 +01001822#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Ronald Oussoren32fd16e2006-04-23 12:36:23 +00001823#ifdef __APPLE__
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001824 if (select_have_broken_poll()) {
1825 if (PyObject_DelAttrString(m, "poll") == -1) {
1826 PyErr_Clear();
1827 }
1828 } else {
Ronald Oussoren32fd16e2006-04-23 12:36:23 +00001829#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001830 {
Ronald Oussoren32fd16e2006-04-23 12:36:23 +00001831#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001832 Py_TYPE(&poll_Type) = &PyType_Type;
1833 PyModule_AddIntConstant(m, "POLLIN", POLLIN);
1834 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
1835 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
1836 PyModule_AddIntConstant(m, "POLLERR", POLLERR);
1837 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
1838 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001839
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00001840#ifdef POLLRDNORM
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001841 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00001842#endif
1843#ifdef POLLRDBAND
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001844 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00001845#endif
1846#ifdef POLLWRNORM
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001847 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00001848#endif
1849#ifdef POLLWRBAND
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001850 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00001851#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00001852#ifdef POLLMSG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001853 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00001854#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001855 }
Ronald Oussoren32fd16e2006-04-23 12:36:23 +00001856#endif /* HAVE_POLL */
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001857
1858#ifdef HAVE_EPOLL
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001859 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
1860 if (PyType_Ready(&pyEpoll_Type) < 0)
1861 return;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001862
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001863 Py_INCREF(&pyEpoll_Type);
1864 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001865
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001866 PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);
1867 PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);
1868 PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);
1869 PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);
1870 PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);
1871 PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001872#ifdef EPOLLONESHOT
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001873 /* Kernel 2.6.2+ */
1874 PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001875#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001876 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
Zachary Ware977be3e2015-08-01 21:30:11 -05001877#ifdef EPOLLRDNORM
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001878 PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);
Zachary Ware977be3e2015-08-01 21:30:11 -05001879#endif
1880#ifdef EPOLLRDBAND
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001881 PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);
Zachary Ware977be3e2015-08-01 21:30:11 -05001882#endif
1883#ifdef EPOLLWRNORM
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001884 PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);
Zachary Ware977be3e2015-08-01 21:30:11 -05001885#endif
1886#ifdef EPOLLWRBAND
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001887 PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);
Zachary Ware977be3e2015-08-01 21:30:11 -05001888#endif
1889#ifdef EPOLLMSG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001890 PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);
Zachary Ware977be3e2015-08-01 21:30:11 -05001891#endif
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001892#endif /* HAVE_EPOLL */
1893
1894#ifdef HAVE_KQUEUE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001895 kqueue_event_Type.tp_new = PyType_GenericNew;
1896 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
1897 if(PyType_Ready(&kqueue_event_Type) < 0)
1898 return;
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001899
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001900 Py_INCREF(&kqueue_event_Type);
1901 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001902
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001903 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
1904 if(PyType_Ready(&kqueue_queue_Type) < 0)
1905 return;
1906 Py_INCREF(&kqueue_queue_Type);
1907 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
1908
1909 /* event filters */
1910 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
1911 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
1912 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
1913 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
1914 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001915#ifdef EVFILT_NETDEV
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001916 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001917#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001918 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
1919 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001920
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001921 /* event flags */
1922 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
1923 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
1924 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
1925 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
1926 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
1927 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001928
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001929 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
1930 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001931
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001932 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
1933 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001935 /* READ WRITE filter flag */
1936 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001937
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001938 /* VNODE filter flags */
1939 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
1940 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
1941 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
1942 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
1943 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
1944 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
1945 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001946
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001947 /* PROC filter flags */
1948 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
1949 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
1950 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
1951 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
1952 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001953
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001954 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
1955 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
1956 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
1957
1958 /* NETDEV filter flags */
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001959#ifdef EVFILT_NETDEV
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001960 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
1961 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
1962 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes0e9ab5f2008-03-21 23:49:44 +00001963#endif
1964
1965#endif /* HAVE_KQUEUE */
Guido van Rossumed233a51992-06-23 09:07:03 +00001966}