blob: d44e8de929caf30d15f0bd8720efa45c7805fe71 [file] [log] [blame]
Guido van Rossum4f0fbf81996-06-12 04:22:53 +00001/* select - Module containing unix select(2) call.
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002 Under Unix, the file descriptors are small integers.
3 Under Win32, select only exists for sockets, and sockets may
4 have any value except INVALID_SOCKET.
Guido van Rossum4f0fbf81996-06-12 04:22:53 +00005*/
Guido van Rossumed233a51992-06-23 09:07:03 +00006
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00007#include "Python.h"
Christian Heimes4fbc72b2008-03-22 00:47:35 +00008#include <structmember.h>
Guido van Rossumed233a51992-06-23 09:07:03 +00009
Jesus Cead8b9ae62011-11-14 19:07:41 +010010#ifdef HAVE_SYS_DEVPOLL_H
11#include <sys/resource.h>
12#include <sys/devpoll.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <fcntl.h>
16#endif
17
Thomas Wouters477c8d52006-05-27 19:21:47 +000018#ifdef __APPLE__
19 /* Perform runtime testing for a broken poll on OSX to make it easier
20 * to use the same binary on multiple releases of the OS.
21 */
22#undef HAVE_BROKEN_POLL
23#endif
24
Tim Petersd92dfe02000-12-12 01:18:41 +000025/* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
26 64 is too small (too many people have bumped into that limit).
27 Here we boost it.
28 Users who want even more than the boosted limit should #define
29 FD_SETSIZE higher before this; e.g., via compiler /D switch.
30*/
31#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
32#define FD_SETSIZE 512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033#endif
Tim Petersd92dfe02000-12-12 01:18:41 +000034
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000035#if defined(HAVE_POLL_H)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000036#include <poll.h>
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000037#elif defined(HAVE_SYS_POLL_H)
38#include <sys/poll.h>
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000039#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000040
Guido van Rossum37273171996-12-09 18:47:43 +000041#ifdef __sgi
42/* This is missing from unistd.h */
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000043extern void bzero(void *, int);
Guido van Rossum37273171996-12-09 18:47:43 +000044#endif
45
Thomas Wouters0e3f5912006-08-11 14:57:12 +000046#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000047#include <sys/types.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000048#endif
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000049
Guido van Rossum6f489d91996-06-28 20:15:15 +000050#ifdef MS_WINDOWS
Christian Heimesc36625b2008-01-04 13:33:00 +000051# define WIN32_LEAN_AND_MEAN
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052# include <winsock.h>
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000053#else
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054# define SOCKET int
Guido van Rossumbcc20741998-08-04 22:53:56 +000055#endif
Guido van Rossumed233a51992-06-23 09:07:03 +000056
Barry Warsawc1cb3601996-12-12 22:16:21 +000057/* list of Python objects and their file descriptor */
58typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 PyObject *obj; /* owned reference */
60 SOCKET fd;
61 int sentinel; /* -1 == sentinel */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000062} pylist;
63
Barry Warsawc1cb3601996-12-12 22:16:21 +000064static void
Tim Peters4b046c22001-08-16 21:59:46 +000065reap_obj(pylist fd2obj[FD_SETSIZE + 1])
Barry Warsawc1cb3601996-12-12 22:16:21 +000066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 int i;
68 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
69 Py_XDECREF(fd2obj[i].obj);
70 fd2obj[i].obj = NULL;
71 }
72 fd2obj[0].sentinel = -1;
Barry Warsawc1cb3601996-12-12 22:16:21 +000073}
74
75
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000076/* returns -1 and sets the Python exception if an error occurred, otherwise
77 returns a number >= 0
78*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000079static int
Brett Cannon62dba4c2003-09-10 19:37:42 +000080seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 int max = -1;
83 int index = 0;
Antoine Pitroue4ad37e2012-11-01 20:13:54 +010084 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 PyObject* fast_seq = NULL;
86 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
89 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000090
Benjamin Petersone0edb8b2010-06-27 23:49:45 +000091 fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 if (!fast_seq)
93 return -1;
94
Antoine Pitroue4ad37e2012-11-01 20:13:54 +010095 for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 SOCKET v;
97
98 /* any intervening fileno() calls could decr this refcnt */
99 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
Jesus Cea62a5c322012-07-19 21:31:26 +0200100 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 Py_INCREF(o);
103 v = PyObject_AsFileDescriptor( o );
104 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000105
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000106#if defined(_MSC_VER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 max = 0; /* not used for Win32 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000108#else /* !_MSC_VER */
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200109 if (!_PyIsSelectable_fd(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 PyErr_SetString(PyExc_ValueError,
111 "filedescriptor out of range in select()");
112 goto finally;
113 }
114 if (v > max)
115 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000116#endif /* _MSC_VER */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 /* add object and its file descriptor to the list */
120 if (index >= FD_SETSIZE) {
121 PyErr_SetString(PyExc_ValueError,
122 "too many file descriptors in select()");
123 goto finally;
124 }
125 fd2obj[index].obj = o;
126 fd2obj[index].fd = v;
127 fd2obj[index].sentinel = 0;
128 fd2obj[++index].sentinel = -1;
129 }
130 Py_DECREF(fast_seq);
131 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000132
133 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 Py_XDECREF(o);
135 Py_DECREF(fast_seq);
136 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000137}
138
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000139/* returns NULL and sets the Python exception if an error occurred */
140static PyObject *
Tim Peters4b046c22001-08-16 21:59:46 +0000141set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +0000142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 int i, j, count=0;
144 PyObject *list, *o;
145 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
148 if (FD_ISSET(fd2obj[j].fd, set))
149 count++;
150 }
151 list = PyList_New(count);
152 if (!list)
153 return NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 i = 0;
156 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
157 fd = fd2obj[j].fd;
158 if (FD_ISSET(fd, set)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 o = fd2obj[j].obj;
160 fd2obj[j].obj = NULL;
161 /* transfer ownership */
162 if (PyList_SetItem(list, i, o) < 0)
163 goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 i++;
166 }
167 }
168 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000169 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 Py_DECREF(list);
171 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000172}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000173
Barry Warsawb44740f2001-08-16 16:52:59 +0000174#undef SELECT_USES_HEAP
175#if FD_SETSIZE > 1024
176#define SELECT_USES_HEAP
177#endif /* FD_SETSIZE > 1024 */
178
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000179static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000180select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000181{
Barry Warsawb44740f2001-08-16 16:52:59 +0000182#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 pylist *rfd2obj, *wfd2obj, *efd2obj;
Barry Warsawb44740f2001-08-16 16:52:59 +0000184#else /* !SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 /* XXX: All this should probably be implemented as follows:
186 * - find the highest descriptor we're interested in
187 * - add one
188 * - that's the size
189 * See: Stevens, APitUE, $12.5.1
190 */
191 pylist rfd2obj[FD_SETSIZE + 1];
192 pylist wfd2obj[FD_SETSIZE + 1];
193 pylist efd2obj[FD_SETSIZE + 1];
Barry Warsawb44740f2001-08-16 16:52:59 +0000194#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 PyObject *ifdlist, *ofdlist, *efdlist;
196 PyObject *ret = NULL;
197 PyObject *tout = Py_None;
198 fd_set ifdset, ofdset, efdset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 struct timeval tv, *tvp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 int imax, omax, emax, max;
201 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 /* convert arguments */
204 if (!PyArg_UnpackTuple(args, "select", 3, 4,
205 &ifdlist, &ofdlist, &efdlist, &tout))
206 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 if (tout == Py_None)
209 tvp = (struct timeval *)0;
210 else if (!PyNumber_Check(tout)) {
211 PyErr_SetString(PyExc_TypeError,
212 "timeout must be a float or None");
213 return NULL;
214 }
215 else {
Victor Stinnerb2a37732012-03-14 00:20:51 +0100216#ifdef MS_WINDOWS
217 time_t sec;
218 if (_PyTime_ObjectToTimeval(tout, &sec, &tv.tv_usec) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 return NULL;
Victor Stinnerb2a37732012-03-14 00:20:51 +0100220 assert(sizeof(tv.tv_sec) == sizeof(long));
221#if SIZEOF_TIME_T > SIZEOF_LONG
222 if (sec > LONG_MAX) {
223 PyErr_SetString(PyExc_OverflowError,
224 "timeout is too large");
225 return NULL;
226 }
227#endif
228 tv.tv_sec = (long)sec;
229#else
Brett Cannon8798ad32012-04-07 14:59:29 -0400230 /* 64-bit OS X has struct timeval.tv_usec as an int (and thus still 4
231 bytes as required), but no longer defined by a long. */
Benjamin Peterson6f3e5e42012-09-11 12:05:05 -0400232 long tv_usec;
Brett Cannon8798ad32012-04-07 14:59:29 -0400233 if (_PyTime_ObjectToTimeval(tout, &tv.tv_sec, &tv_usec) == -1)
Victor Stinnerb2a37732012-03-14 00:20:51 +0100234 return NULL;
Brett Cannon8798ad32012-04-07 14:59:29 -0400235 tv.tv_usec = tv_usec;
Victor Stinnerb2a37732012-03-14 00:20:51 +0100236#endif
Victor Stinner5d272cc2012-03-13 13:35:55 +0100237 if (tv.tv_sec < 0) {
238 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 return NULL;
240 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 tvp = &tv;
242 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000243
Guido van Rossumed233a51992-06-23 09:07:03 +0000244
Barry Warsawb44740f2001-08-16 16:52:59 +0000245#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 /* Allocate memory for the lists */
247 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
248 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
249 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
250 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
251 if (rfd2obj) PyMem_DEL(rfd2obj);
252 if (wfd2obj) PyMem_DEL(wfd2obj);
253 if (efd2obj) PyMem_DEL(efd2obj);
254 return PyErr_NoMemory();
255 }
Barry Warsawb44740f2001-08-16 16:52:59 +0000256#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 /* Convert sequences to fd_sets, and get maximum fd number
258 * propagates the Python exception set in seq2set()
259 */
260 rfd2obj[0].sentinel = -1;
261 wfd2obj[0].sentinel = -1;
262 efd2obj[0].sentinel = -1;
263 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
264 goto finally;
265 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
266 goto finally;
267 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
268 goto finally;
269 max = imax;
270 if (omax > max) max = omax;
271 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 Py_BEGIN_ALLOW_THREADS
274 n = select(max, &ifdset, &ofdset, &efdset, tvp);
275 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000276
Thomas Heller106f4c72002-09-24 16:51:00 +0000277#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (n == SOCKET_ERROR) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200279 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000281#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 if (n < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200283 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000285#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 else {
287 /* any of these three calls can raise an exception. it's more
288 convenient to test for this after all three calls... but
289 is that acceptable?
290 */
291 ifdlist = set2list(&ifdset, rfd2obj);
292 ofdlist = set2list(&ofdset, wfd2obj);
293 efdlist = set2list(&efdset, efd2obj);
294 if (PyErr_Occurred())
295 ret = NULL;
296 else
297 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000298
Victor Stinnerbbf8ce52013-07-09 00:49:03 +0200299 Py_XDECREF(ifdlist);
300 Py_XDECREF(ofdlist);
301 Py_XDECREF(efdlist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 }
303
Barry Warsawc1cb3601996-12-12 22:16:21 +0000304 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 reap_obj(rfd2obj);
306 reap_obj(wfd2obj);
307 reap_obj(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000308#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 PyMem_DEL(rfd2obj);
310 PyMem_DEL(wfd2obj);
311 PyMem_DEL(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000312#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000314}
315
Nicholas Bastine62c5c82004-03-21 23:45:42 +0000316#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317/*
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000318 * poll() support
319 */
320
321typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 PyObject_HEAD
323 PyObject *dict;
324 int ufd_uptodate;
325 int ufd_len;
326 struct pollfd *ufds;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300327 int poll_running;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000328} pollObject;
329
Jeremy Hylton938ace62002-07-17 16:30:39 +0000330static PyTypeObject poll_Type;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332/* Update the malloc'ed array of pollfds to match the dictionary
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000333 contained within a pollObject. Return 1 on success, 0 on an error.
334*/
335
336static int
337update_ufd_array(pollObject *self)
338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 Py_ssize_t i, pos;
340 PyObject *key, *value;
341 struct pollfd *old_ufds = self->ufds;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 self->ufd_len = PyDict_Size(self->dict);
344 PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
345 if (self->ufds == NULL) {
346 self->ufds = old_ufds;
347 PyErr_NoMemory();
348 return 0;
349 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 i = pos = 0;
352 while (PyDict_Next(self->dict, &pos, &key, &value)) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200353 assert(i < self->ufd_len);
354 /* Never overflow */
355 self->ufds[i].fd = (int)PyLong_AsLong(key);
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200356 self->ufds[i].events = (short)(unsigned short)PyLong_AsLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 i++;
358 }
Serhiy Storchaka78980432013-01-15 01:12:17 +0200359 assert(i == self->ufd_len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 self->ufd_uptodate = 1;
361 return 1;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000362}
363
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200364static int
365ushort_converter(PyObject *obj, void *ptr)
366{
367 unsigned long uval;
368
369 uval = PyLong_AsUnsignedLong(obj);
370 if (uval == (unsigned long)-1 && PyErr_Occurred())
371 return 0;
372 if (uval > USHRT_MAX) {
373 PyErr_SetString(PyExc_OverflowError,
374 "Python int too large for C unsigned short");
375 return 0;
376 }
377
378 *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short);
379 return 1;
380}
381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000382PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000383"register(fd [, eventmask] ) -> None\n\n\
384Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000385fd -- either an integer, or an object with a fileno() method returning an\n\
386 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000388
389static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390poll_register(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 PyObject *o, *key, *value;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200393 int fd;
394 unsigned short events = POLLIN | POLLPRI | POLLOUT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000396
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200397 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 fd = PyObject_AsFileDescriptor(o);
401 if (fd == -1) return NULL;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 /* Add entry to the internal dictionary: the key is the
404 file descriptor, and the value is the event mask. */
405 key = PyLong_FromLong(fd);
406 if (key == NULL)
407 return NULL;
408 value = PyLong_FromLong(events);
409 if (value == NULL) {
410 Py_DECREF(key);
411 return NULL;
412 }
413 err = PyDict_SetItem(self->dict, key, value);
414 Py_DECREF(key);
415 Py_DECREF(value);
416 if (err < 0)
417 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 self->ufd_uptodate = 0;
420
421 Py_INCREF(Py_None);
422 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000423}
424
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000425PyDoc_STRVAR(poll_modify_doc,
426"modify(fd, eventmask) -> None\n\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +0000427Modify an already registered file descriptor.\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000428fd -- either an integer, or an object with a fileno() method returning an\n\
429 int.\n\
430events -- an optional bitmask describing the type of events to check for");
431
432static PyObject *
433poll_modify(pollObject *self, PyObject *args)
434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 PyObject *o, *key, *value;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200436 int fd;
437 unsigned short events;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 int err;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000439
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200440 if (!PyArg_ParseTuple(args, "OO&:modify", &o, ushort_converter, &events))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 fd = PyObject_AsFileDescriptor(o);
444 if (fd == -1) return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 /* Modify registered fd */
447 key = PyLong_FromLong(fd);
448 if (key == NULL)
449 return NULL;
450 if (PyDict_GetItem(self->dict, key) == NULL) {
451 errno = ENOENT;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200452 PyErr_SetFromErrno(PyExc_OSError);
Jesus Cea62a5c322012-07-19 21:31:26 +0200453 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 return NULL;
455 }
456 value = PyLong_FromLong(events);
457 if (value == NULL) {
458 Py_DECREF(key);
459 return NULL;
460 }
461 err = PyDict_SetItem(self->dict, key, value);
462 Py_DECREF(key);
463 Py_DECREF(value);
464 if (err < 0)
465 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 self->ufd_uptodate = 0;
468
469 Py_INCREF(Py_None);
470 return Py_None;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000471}
472
473
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000474PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000475"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000476Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000477
478static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 PyObject *key;
482 int fd;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 fd = PyObject_AsFileDescriptor( o );
485 if (fd == -1)
486 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 /* Check whether the fd is already in the array */
489 key = PyLong_FromLong(fd);
490 if (key == NULL)
491 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 if (PyDict_DelItem(self->dict, key) == -1) {
494 Py_DECREF(key);
495 /* This will simply raise the KeyError set by PyDict_DelItem
496 if the file descriptor isn't registered. */
497 return NULL;
498 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 Py_DECREF(key);
501 self->ufd_uptodate = 0;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 Py_INCREF(Py_None);
504 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000505}
506
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000507PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000508"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
509Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000510any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000511
512static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513poll_poll(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 PyObject *result_list = NULL, *tout = NULL;
516 int timeout = 0, poll_result, i, j;
517 PyObject *value = NULL, *num = NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
520 return NULL;
521 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 /* Check values for timeout */
524 if (tout == NULL || tout == Py_None)
525 timeout = -1;
526 else if (!PyNumber_Check(tout)) {
527 PyErr_SetString(PyExc_TypeError,
528 "timeout must be an integer or None");
529 return NULL;
530 }
531 else {
532 tout = PyNumber_Long(tout);
533 if (!tout)
534 return NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200535 timeout = _PyLong_AsInt(tout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_DECREF(tout);
537 if (timeout == -1 && PyErr_Occurred())
538 return NULL;
539 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000540
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300541 /* Avoid concurrent poll() invocation, issue 8865 */
542 if (self->poll_running) {
543 PyErr_SetString(PyExc_RuntimeError,
544 "concurrent poll() invocation");
545 return NULL;
546 }
547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 /* Ensure the ufd array is up to date */
549 if (!self->ufd_uptodate)
550 if (update_ufd_array(self) == 0)
551 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000552
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300553 self->poll_running = 1;
554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* call poll() */
556 Py_BEGIN_ALLOW_THREADS
557 poll_result = poll(self->ufds, self->ufd_len, timeout);
558 Py_END_ALLOW_THREADS
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000559
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300560 self->poll_running = 0;
561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 if (poll_result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200563 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 return NULL;
565 }
566
567 /* build the result list */
568
569 result_list = PyList_New(poll_result);
570 if (!result_list)
571 return NULL;
572 else {
573 for (i = 0, j = 0; j < poll_result; j++) {
574 /* skip to the next fired descriptor */
575 while (!self->ufds[i].revents) {
576 i++;
577 }
578 /* if we hit a NULL return, set value to NULL
579 and break out of loop; code at end will
580 clean up result_list */
581 value = PyTuple_New(2);
582 if (value == NULL)
583 goto error;
584 num = PyLong_FromLong(self->ufds[i].fd);
585 if (num == NULL) {
586 Py_DECREF(value);
587 goto error;
588 }
589 PyTuple_SET_ITEM(value, 0, num);
590
591 /* The &0xffff is a workaround for AIX. 'revents'
592 is a 16-bit short, and IBM assigned POLLNVAL
593 to be 0x8000, so the conversion to int results
594 in a negative number. See SF bug #923315. */
595 num = PyLong_FromLong(self->ufds[i].revents & 0xffff);
596 if (num == NULL) {
597 Py_DECREF(value);
598 goto error;
599 }
600 PyTuple_SET_ITEM(value, 1, num);
601 if ((PyList_SetItem(result_list, j, value)) == -1) {
602 Py_DECREF(value);
603 goto error;
604 }
605 i++;
606 }
607 }
608 return result_list;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000609
610 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 Py_DECREF(result_list);
612 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000613}
614
615static PyMethodDef poll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 {"register", (PyCFunction)poll_register,
617 METH_VARARGS, poll_register_doc},
618 {"modify", (PyCFunction)poll_modify,
619 METH_VARARGS, poll_modify_doc},
620 {"unregister", (PyCFunction)poll_unregister,
621 METH_O, poll_unregister_doc},
622 {"poll", (PyCFunction)poll_poll,
623 METH_VARARGS, poll_poll_doc},
624 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000625};
626
627static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000628newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 pollObject *self;
631 self = PyObject_New(pollObject, &poll_Type);
632 if (self == NULL)
633 return NULL;
634 /* ufd_uptodate is a Boolean, denoting whether the
635 array pointed to by ufds matches the contents of the dictionary. */
636 self->ufd_uptodate = 0;
637 self->ufds = NULL;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300638 self->poll_running = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 self->dict = PyDict_New();
640 if (self->dict == NULL) {
641 Py_DECREF(self);
642 return NULL;
643 }
644 return self;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000645}
646
647static void
648poll_dealloc(pollObject *self)
649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 if (self->ufds != NULL)
651 PyMem_DEL(self->ufds);
652 Py_XDECREF(self->dict);
653 PyObject_Del(self);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000654}
655
Tim Peters0c322792002-07-17 16:49:03 +0000656static PyTypeObject poll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 /* The ob_type field must be initialized in the module init function
658 * to be portable to Windows without using C++. */
659 PyVarObject_HEAD_INIT(NULL, 0)
660 "select.poll", /*tp_name*/
661 sizeof(pollObject), /*tp_basicsize*/
662 0, /*tp_itemsize*/
663 /* methods */
664 (destructor)poll_dealloc, /*tp_dealloc*/
665 0, /*tp_print*/
666 0, /*tp_getattr*/
667 0, /*tp_setattr*/
668 0, /*tp_reserved*/
669 0, /*tp_repr*/
670 0, /*tp_as_number*/
671 0, /*tp_as_sequence*/
672 0, /*tp_as_mapping*/
673 0, /*tp_hash*/
674 0, /*tp_call*/
675 0, /*tp_str*/
676 0, /*tp_getattro*/
677 0, /*tp_setattro*/
678 0, /*tp_as_buffer*/
679 Py_TPFLAGS_DEFAULT, /*tp_flags*/
680 0, /*tp_doc*/
681 0, /*tp_traverse*/
682 0, /*tp_clear*/
683 0, /*tp_richcompare*/
684 0, /*tp_weaklistoffset*/
685 0, /*tp_iter*/
686 0, /*tp_iternext*/
687 poll_methods, /*tp_methods*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000688};
689
Jesus Cead8b9ae62011-11-14 19:07:41 +0100690#ifdef HAVE_SYS_DEVPOLL_H
691typedef struct {
692 PyObject_HEAD
693 int fd_devpoll;
694 int max_n_fds;
695 int n_fds;
696 struct pollfd *fds;
697} devpollObject;
698
699static PyTypeObject devpoll_Type;
700
Victor Stinner13423c32013-08-22 00:19:50 +0200701static PyObject *
702devpoll_err_closed(void)
703{
704 PyErr_SetString(PyExc_ValueError, "I/O operation on closed devpoll object");
705 return NULL;
706}
707
Jesus Cead8b9ae62011-11-14 19:07:41 +0100708static int devpoll_flush(devpollObject *self)
709{
710 int size, n;
711
712 if (!self->n_fds) return 0;
713
714 size = sizeof(struct pollfd)*self->n_fds;
715 self->n_fds = 0;
716
717 Py_BEGIN_ALLOW_THREADS
718 n = write(self->fd_devpoll, self->fds, size);
719 Py_END_ALLOW_THREADS
720
721 if (n == -1 ) {
722 PyErr_SetFromErrno(PyExc_IOError);
723 return -1;
724 }
725 if (n < size) {
726 /*
727 ** Data writed to /dev/poll is a binary data structure. It is not
728 ** clear what to do if a partial write occurred. For now, raise
729 ** an exception and see if we actually found this problem in
730 ** the wild.
731 ** See http://bugs.python.org/issue6397.
732 */
733 PyErr_Format(PyExc_IOError, "failed to write all pollfds. "
734 "Please, report at http://bugs.python.org/. "
735 "Data to report: Size tried: %d, actual size written: %d.",
736 size, n);
737 return -1;
738 }
739 return 0;
740}
741
742static PyObject *
743internal_devpoll_register(devpollObject *self, PyObject *args, int remove)
744{
745 PyObject *o;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200746 int fd;
747 unsigned short events = POLLIN | POLLPRI | POLLOUT;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100748
Victor Stinner13423c32013-08-22 00:19:50 +0200749 if (self->fd_devpoll < 0)
750 return devpoll_err_closed();
751
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200752 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
Jesus Cead8b9ae62011-11-14 19:07:41 +0100753 return NULL;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100754
755 fd = PyObject_AsFileDescriptor(o);
756 if (fd == -1) return NULL;
757
758 if (remove) {
759 self->fds[self->n_fds].fd = fd;
760 self->fds[self->n_fds].events = POLLREMOVE;
761
762 if (++self->n_fds == self->max_n_fds) {
763 if (devpoll_flush(self))
764 return NULL;
765 }
766 }
767
768 self->fds[self->n_fds].fd = fd;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200769 self->fds[self->n_fds].events = (signed short)events;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100770
771 if (++self->n_fds == self->max_n_fds) {
772 if (devpoll_flush(self))
773 return NULL;
774 }
775
776 Py_RETURN_NONE;
777}
778
779PyDoc_STRVAR(devpoll_register_doc,
780"register(fd [, eventmask] ) -> None\n\n\
781Register a file descriptor with the polling object.\n\
782fd -- either an integer, or an object with a fileno() method returning an\n\
783 int.\n\
784events -- an optional bitmask describing the type of events to check for");
785
786static PyObject *
787devpoll_register(devpollObject *self, PyObject *args)
788{
789 return internal_devpoll_register(self, args, 0);
790}
791
792PyDoc_STRVAR(devpoll_modify_doc,
793"modify(fd[, eventmask]) -> None\n\n\
794Modify a possible already registered file descriptor.\n\
795fd -- either an integer, or an object with a fileno() method returning an\n\
796 int.\n\
797events -- an optional bitmask describing the type of events to check for");
798
799static PyObject *
800devpoll_modify(devpollObject *self, PyObject *args)
801{
802 return internal_devpoll_register(self, args, 1);
803}
804
805
806PyDoc_STRVAR(devpoll_unregister_doc,
807"unregister(fd) -> None\n\n\
808Remove a file descriptor being tracked by the polling object.");
809
810static PyObject *
811devpoll_unregister(devpollObject *self, PyObject *o)
812{
813 int fd;
814
Victor Stinner13423c32013-08-22 00:19:50 +0200815 if (self->fd_devpoll < 0)
816 return devpoll_err_closed();
817
Jesus Cead8b9ae62011-11-14 19:07:41 +0100818 fd = PyObject_AsFileDescriptor( o );
819 if (fd == -1)
820 return NULL;
821
822 self->fds[self->n_fds].fd = fd;
823 self->fds[self->n_fds].events = POLLREMOVE;
824
825 if (++self->n_fds == self->max_n_fds) {
826 if (devpoll_flush(self))
827 return NULL;
828 }
829
830 Py_RETURN_NONE;
831}
832
833PyDoc_STRVAR(devpoll_poll_doc,
834"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
835Polls the set of registered file descriptors, returning a list containing \n\
836any descriptors that have events or errors to report.");
837
838static PyObject *
839devpoll_poll(devpollObject *self, PyObject *args)
840{
841 struct dvpoll dvp;
842 PyObject *result_list = NULL, *tout = NULL;
843 int poll_result, i;
844 long timeout;
845 PyObject *value, *num1, *num2;
846
Victor Stinner13423c32013-08-22 00:19:50 +0200847 if (self->fd_devpoll < 0)
848 return devpoll_err_closed();
849
Jesus Cead8b9ae62011-11-14 19:07:41 +0100850 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
851 return NULL;
852 }
853
854 /* Check values for timeout */
855 if (tout == NULL || tout == Py_None)
856 timeout = -1;
857 else if (!PyNumber_Check(tout)) {
858 PyErr_SetString(PyExc_TypeError,
859 "timeout must be an integer or None");
860 return NULL;
861 }
862 else {
863 tout = PyNumber_Long(tout);
864 if (!tout)
865 return NULL;
866 timeout = PyLong_AsLong(tout);
867 Py_DECREF(tout);
868 if (timeout == -1 && PyErr_Occurred())
869 return NULL;
870 }
871
872 if ((timeout < -1) || (timeout > INT_MAX)) {
873 PyErr_SetString(PyExc_OverflowError,
874 "timeout is out of range");
875 return NULL;
876 }
877
878 if (devpoll_flush(self))
879 return NULL;
880
881 dvp.dp_fds = self->fds;
882 dvp.dp_nfds = self->max_n_fds;
883 dvp.dp_timeout = timeout;
884
885 /* call devpoll() */
886 Py_BEGIN_ALLOW_THREADS
887 poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp);
888 Py_END_ALLOW_THREADS
889
890 if (poll_result < 0) {
891 PyErr_SetFromErrno(PyExc_IOError);
892 return NULL;
893 }
894
895 /* build the result list */
896
897 result_list = PyList_New(poll_result);
898 if (!result_list)
899 return NULL;
900 else {
901 for (i = 0; i < poll_result; i++) {
902 num1 = PyLong_FromLong(self->fds[i].fd);
903 num2 = PyLong_FromLong(self->fds[i].revents);
904 if ((num1 == NULL) || (num2 == NULL)) {
905 Py_XDECREF(num1);
906 Py_XDECREF(num2);
907 goto error;
908 }
909 value = PyTuple_Pack(2, num1, num2);
910 Py_DECREF(num1);
911 Py_DECREF(num2);
912 if (value == NULL)
913 goto error;
914 if ((PyList_SetItem(result_list, i, value)) == -1) {
915 Py_DECREF(value);
916 goto error;
917 }
918 }
919 }
920
921 return result_list;
922
923 error:
924 Py_DECREF(result_list);
925 return NULL;
926}
927
Richard Oudkerk168d59b2013-08-22 13:31:15 +0100928static int
929devpoll_internal_close(devpollObject *self)
930{
931 int save_errno = 0;
932 if (self->fd_devpoll >= 0) {
933 int fd = self->fd_devpoll;
934 self->fd_devpoll = -1;
935 Py_BEGIN_ALLOW_THREADS
936 if (close(fd) < 0)
937 save_errno = errno;
938 Py_END_ALLOW_THREADS
939 }
940 return save_errno;
941}
942
Victor Stinner13423c32013-08-22 00:19:50 +0200943static PyObject*
944devpoll_close(devpollObject *self)
945{
946 errno = devpoll_internal_close(self);
947 if (errno < 0) {
948 PyErr_SetFromErrno(PyExc_OSError);
949 return NULL;
950 }
951 Py_RETURN_NONE;
952}
953
954PyDoc_STRVAR(devpoll_close_doc,
955"close() -> None\n\
956\n\
957Close the devpoll file descriptor. Further operations on the devpoll\n\
958object will raise an exception.");
959
960static PyObject*
961devpoll_get_closed(devpollObject *self)
962{
963 if (self->fd_devpoll < 0)
964 Py_RETURN_TRUE;
965 else
966 Py_RETURN_FALSE;
967}
968
969static PyObject*
970devpoll_fileno(devpollObject *self)
971{
972 if (self->fd_devpoll < 0)
973 return devpoll_err_closed();
974 return PyLong_FromLong(self->fd_devpoll);
975}
976
977PyDoc_STRVAR(devpoll_fileno_doc,
978"fileno() -> int\n\
979\n\
980Return the file descriptor.");
981
Jesus Cead8b9ae62011-11-14 19:07:41 +0100982static PyMethodDef devpoll_methods[] = {
983 {"register", (PyCFunction)devpoll_register,
984 METH_VARARGS, devpoll_register_doc},
985 {"modify", (PyCFunction)devpoll_modify,
986 METH_VARARGS, devpoll_modify_doc},
987 {"unregister", (PyCFunction)devpoll_unregister,
988 METH_O, devpoll_unregister_doc},
989 {"poll", (PyCFunction)devpoll_poll,
990 METH_VARARGS, devpoll_poll_doc},
Victor Stinner13423c32013-08-22 00:19:50 +0200991 {"close", (PyCFunction)devpoll_close, METH_NOARGS,
992 devpoll_close_doc},
993 {"fileno", (PyCFunction)devpoll_fileno, METH_NOARGS,
994 devpoll_fileno_doc},
Jesus Cead8b9ae62011-11-14 19:07:41 +0100995 {NULL, NULL} /* sentinel */
996};
997
Victor Stinner13423c32013-08-22 00:19:50 +0200998static PyGetSetDef devpoll_getsetlist[] = {
999 {"closed", (getter)devpoll_get_closed, NULL,
1000 "True if the devpoll object is closed"},
1001 {0},
1002};
1003
Jesus Cead8b9ae62011-11-14 19:07:41 +01001004static devpollObject *
1005newDevPollObject(void)
1006{
1007 devpollObject *self;
1008 int fd_devpoll, limit_result;
1009 struct pollfd *fds;
1010 struct rlimit limit;
1011
1012 Py_BEGIN_ALLOW_THREADS
1013 /*
1014 ** If we try to process more that getrlimit()
1015 ** fds, the kernel will give an error, so
1016 ** we set the limit here. It is a dynamic
1017 ** value, because we can change rlimit() anytime.
1018 */
1019 limit_result = getrlimit(RLIMIT_NOFILE, &limit);
1020 if (limit_result != -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001021 fd_devpoll = _Py_open("/dev/poll", O_RDWR);
Jesus Cead8b9ae62011-11-14 19:07:41 +01001022 Py_END_ALLOW_THREADS
1023
1024 if (limit_result == -1) {
1025 PyErr_SetFromErrno(PyExc_OSError);
1026 return NULL;
1027 }
1028 if (fd_devpoll == -1) {
1029 PyErr_SetFromErrnoWithFilename(PyExc_IOError, "/dev/poll");
1030 return NULL;
1031 }
1032
1033 fds = PyMem_NEW(struct pollfd, limit.rlim_cur);
1034 if (fds == NULL) {
1035 close(fd_devpoll);
1036 PyErr_NoMemory();
1037 return NULL;
1038 }
1039
1040 self = PyObject_New(devpollObject, &devpoll_Type);
1041 if (self == NULL) {
1042 close(fd_devpoll);
1043 PyMem_DEL(fds);
1044 return NULL;
1045 }
1046 self->fd_devpoll = fd_devpoll;
1047 self->max_n_fds = limit.rlim_cur;
1048 self->n_fds = 0;
1049 self->fds = fds;
1050
1051 return self;
1052}
1053
1054static void
1055devpoll_dealloc(devpollObject *self)
1056{
Richard Oudkerka93bf7b2013-08-22 14:03:44 +01001057 (void)devpoll_internal_close(self);
Jesus Cead8b9ae62011-11-14 19:07:41 +01001058 PyMem_DEL(self->fds);
Jesus Cead8b9ae62011-11-14 19:07:41 +01001059 PyObject_Del(self);
1060}
1061
1062static PyTypeObject devpoll_Type = {
1063 /* The ob_type field must be initialized in the module init function
1064 * to be portable to Windows without using C++. */
1065 PyVarObject_HEAD_INIT(NULL, 0)
1066 "select.devpoll", /*tp_name*/
1067 sizeof(devpollObject), /*tp_basicsize*/
1068 0, /*tp_itemsize*/
1069 /* methods */
1070 (destructor)devpoll_dealloc, /*tp_dealloc*/
1071 0, /*tp_print*/
1072 0, /*tp_getattr*/
1073 0, /*tp_setattr*/
1074 0, /*tp_reserved*/
1075 0, /*tp_repr*/
1076 0, /*tp_as_number*/
1077 0, /*tp_as_sequence*/
1078 0, /*tp_as_mapping*/
1079 0, /*tp_hash*/
1080 0, /*tp_call*/
1081 0, /*tp_str*/
1082 0, /*tp_getattro*/
1083 0, /*tp_setattro*/
1084 0, /*tp_as_buffer*/
1085 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1086 0, /*tp_doc*/
1087 0, /*tp_traverse*/
1088 0, /*tp_clear*/
1089 0, /*tp_richcompare*/
1090 0, /*tp_weaklistoffset*/
1091 0, /*tp_iter*/
1092 0, /*tp_iternext*/
1093 devpoll_methods, /*tp_methods*/
Victor Stinner13423c32013-08-22 00:19:50 +02001094 0, /* tp_members */
1095 devpoll_getsetlist, /* tp_getset */
Jesus Cead8b9ae62011-11-14 19:07:41 +01001096};
1097#endif /* HAVE_SYS_DEVPOLL_H */
1098
1099
1100
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001101PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001102"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001103unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001104
1105static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001106select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001109}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110
Jesus Cead8b9ae62011-11-14 19:07:41 +01001111#ifdef HAVE_SYS_DEVPOLL_H
1112PyDoc_STRVAR(devpoll_doc,
1113"Returns a polling object, which supports registering and\n\
1114unregistering file descriptors, and then polling them for I/O events.");
1115
1116static PyObject *
1117select_devpoll(PyObject *self, PyObject *unused)
1118{
1119 return (PyObject *)newDevPollObject();
1120}
1121#endif
1122
1123
Thomas Wouters477c8d52006-05-27 19:21:47 +00001124#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125/*
Thomas Wouters477c8d52006-05-27 19:21:47 +00001126 * On some systems poll() sets errno on invalid file descriptors. We test
1127 * for this at runtime because this bug may be fixed or introduced between
1128 * OS releases.
1129 */
1130static int select_have_broken_poll(void)
1131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 int poll_test;
1133 int filedes[2];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
Thomas Wouters477c8d52006-05-27 19:21:47 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 /* Create a file descriptor to make invalid */
1138 if (pipe(filedes) < 0) {
1139 return 1;
1140 }
1141 poll_struct.fd = filedes[0];
1142 close(filedes[0]);
1143 close(filedes[1]);
1144 poll_test = poll(&poll_struct, 1, 0);
1145 if (poll_test < 0) {
1146 return 1;
1147 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
1148 return 1;
1149 }
1150 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001151}
1152#endif /* __APPLE__ */
1153
1154#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001155
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001156#ifdef HAVE_EPOLL
1157/* **************************************************************************
1158 * epoll interface for Linux 2.6
1159 *
1160 * Written by Christian Heimes
1161 * Inspired by Twisted's _epoll.pyx and select.poll()
1162 */
1163
1164#ifdef HAVE_SYS_EPOLL_H
1165#include <sys/epoll.h>
1166#endif
1167
1168typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 PyObject_HEAD
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001170 SOCKET epfd; /* epoll control file descriptor */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001171} pyEpoll_Object;
1172
1173static PyTypeObject pyEpoll_Type;
1174#define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))
1175
1176static PyObject *
1177pyepoll_err_closed(void)
1178{
Victor Stinner13423c32013-08-22 00:19:50 +02001179 PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001181}
1182
1183static int
1184pyepoll_internal_close(pyEpoll_Object *self)
1185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 int save_errno = 0;
1187 if (self->epfd >= 0) {
1188 int epfd = self->epfd;
1189 self->epfd = -1;
1190 Py_BEGIN_ALLOW_THREADS
1191 if (close(epfd) < 0)
1192 save_errno = errno;
1193 Py_END_ALLOW_THREADS
1194 }
1195 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001196}
1197
1198static PyObject *
Benjamin Peterson95c16622011-12-27 15:36:32 -06001199newPyEpoll_Object(PyTypeObject *type, int sizehint, int flags, SOCKET fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 pyEpoll_Object *self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 assert(type != NULL && type->tp_alloc != NULL);
1204 self = (pyEpoll_Object *) type->tp_alloc(type, 0);
1205 if (self == NULL)
1206 return NULL;
1207
1208 if (fd == -1) {
1209 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson95c16622011-12-27 15:36:32 -06001210#ifdef HAVE_EPOLL_CREATE1
Victor Stinnerdaf45552013-08-28 00:53:59 +02001211 flags |= EPOLL_CLOEXEC;
Benjamin Peterson83251c12011-12-27 16:01:21 -06001212 if (flags)
1213 self->epfd = epoll_create1(flags);
1214 else
Benjamin Peterson95c16622011-12-27 15:36:32 -06001215#endif
Benjamin Peterson83251c12011-12-27 16:01:21 -06001216 self->epfd = epoll_create(sizehint);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 Py_END_ALLOW_THREADS
1218 }
1219 else {
1220 self->epfd = fd;
1221 }
1222 if (self->epfd < 0) {
1223 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001224 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 return NULL;
1226 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001227
1228#ifndef HAVE_EPOLL_CREATE1
Victor Stinnerd72fe892013-08-28 12:22:39 +02001229 if (fd == -1 && _Py_set_inheritable(self->epfd, 0, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001230 Py_DECREF(self);
1231 return NULL;
1232 }
1233#endif
1234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001236}
1237
1238
1239static PyObject *
1240pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1241{
Benjamin Peterson83251c12011-12-27 16:01:21 -06001242 int flags = 0, sizehint = FD_SETSIZE - 1;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001243 static char *kwlist[] = {"sizehint", "flags", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001244
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001245 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
1246 &sizehint, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 return NULL;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001248 if (sizehint < 0) {
1249 PyErr_SetString(PyExc_ValueError, "negative sizehint");
1250 return NULL;
1251 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001252
Benjamin Peterson95c16622011-12-27 15:36:32 -06001253 return newPyEpoll_Object(type, sizehint, flags, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001254}
1255
1256
1257static void
1258pyepoll_dealloc(pyEpoll_Object *self)
1259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 (void)pyepoll_internal_close(self);
1261 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001262}
1263
1264static PyObject*
1265pyepoll_close(pyEpoll_Object *self)
1266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 errno = pyepoll_internal_close(self);
1268 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001269 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 return NULL;
1271 }
1272 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001273}
1274
1275PyDoc_STRVAR(pyepoll_close_doc,
1276"close() -> None\n\
1277\n\
1278Close the epoll control file descriptor. Further operations on the epoll\n\
1279object will raise an exception.");
1280
1281static PyObject*
1282pyepoll_get_closed(pyEpoll_Object *self)
1283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if (self->epfd < 0)
1285 Py_RETURN_TRUE;
1286 else
1287 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001288}
1289
1290static PyObject*
1291pyepoll_fileno(pyEpoll_Object *self)
1292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (self->epfd < 0)
1294 return pyepoll_err_closed();
1295 return PyLong_FromLong(self->epfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001296}
1297
1298PyDoc_STRVAR(pyepoll_fileno_doc,
1299"fileno() -> int\n\
1300\n\
1301Return the epoll control file descriptor.");
1302
1303static PyObject*
1304pyepoll_fromfd(PyObject *cls, PyObject *args)
1305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1309 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001310
Benjamin Peterson95c16622011-12-27 15:36:32 -06001311 return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001312}
1313
1314PyDoc_STRVAR(pyepoll_fromfd_doc,
1315"fromfd(fd) -> epoll\n\
1316\n\
1317Create an epoll object from a given control fd.");
1318
1319static PyObject *
1320pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
1321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 struct epoll_event ev;
1323 int result;
1324 int fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 if (epfd < 0)
1327 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 fd = PyObject_AsFileDescriptor(pfd);
1330 if (fd == -1) {
1331 return NULL;
1332 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001333
Guido van Rossumee07b942013-12-06 17:46:22 -08001334 switch (op) {
1335 case EPOLL_CTL_ADD:
1336 case EPOLL_CTL_MOD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 ev.events = events;
1338 ev.data.fd = fd;
1339 Py_BEGIN_ALLOW_THREADS
1340 result = epoll_ctl(epfd, op, fd, &ev);
1341 Py_END_ALLOW_THREADS
1342 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001343 case EPOLL_CTL_DEL:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
1345 * operation required a non-NULL pointer in event, even
1346 * though this argument is ignored. */
1347 Py_BEGIN_ALLOW_THREADS
1348 result = epoll_ctl(epfd, op, fd, &ev);
1349 if (errno == EBADF) {
1350 /* fd already closed */
1351 result = 0;
1352 errno = 0;
1353 }
1354 Py_END_ALLOW_THREADS
1355 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001356 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 result = -1;
1358 errno = EINVAL;
1359 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 if (result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001362 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 return NULL;
1364 }
1365 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001366}
1367
1368static PyObject *
1369pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 PyObject *pfd;
1372 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
1373 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
1376 &pfd, &events)) {
1377 return NULL;
1378 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001381}
1382
1383PyDoc_STRVAR(pyepoll_register_doc,
Georg Brandl222569d2010-08-02 20:47:56 +00001384"register(fd[, eventmask]) -> None\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001385\n\
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001386Registers a new fd or raises an OSError if the fd is already registered.\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00001387fd is the target file descriptor of the operation.\n\
1388events is a bit set composed of the various EPOLL constants; the default\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001389is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
1390\n\
1391The epoll interface supports all file descriptors that support poll.");
1392
1393static PyObject *
1394pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 PyObject *pfd;
1397 unsigned int events;
1398 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
1401 &pfd, &events)) {
1402 return NULL;
1403 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001406}
1407
1408PyDoc_STRVAR(pyepoll_modify_doc,
1409"modify(fd, eventmask) -> None\n\
1410\n\
1411fd is the target file descriptor of the operation\n\
1412events is a bit set composed of the various EPOLL constants");
1413
1414static PyObject *
1415pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 PyObject *pfd;
1418 static char *kwlist[] = {"fd", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
1421 &pfd)) {
1422 return NULL;
1423 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001426}
1427
1428PyDoc_STRVAR(pyepoll_unregister_doc,
1429"unregister(fd) -> None\n\
1430\n\
1431fd is the target file descriptor of the operation.");
1432
1433static PyObject *
1434pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 double dtimeout = -1.;
1437 int timeout;
1438 int maxevents = -1;
1439 int nfds, i;
1440 PyObject *elist = NULL, *etuple = NULL;
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001441 struct epoll_event *evs = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 static char *kwlist[] = {"timeout", "maxevents", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 if (self->epfd < 0)
1445 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
1448 &dtimeout, &maxevents)) {
1449 return NULL;
1450 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (dtimeout < 0) {
1453 timeout = -1;
1454 }
1455 else if (dtimeout * 1000.0 > INT_MAX) {
1456 PyErr_SetString(PyExc_OverflowError,
1457 "timeout is too large");
1458 return NULL;
1459 }
1460 else {
1461 timeout = (int)(dtimeout * 1000.0);
1462 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (maxevents == -1) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001465 maxevents = FD_SETSIZE-1;
1466 }
1467 else if (maxevents < 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 PyErr_Format(PyExc_ValueError,
1469 "maxevents must be greater than 0, got %d",
1470 maxevents);
1471 return NULL;
1472 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001473
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001474 evs = PyMem_New(struct epoll_event, maxevents);
1475 if (evs == NULL) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001476 PyErr_NoMemory();
1477 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 Py_BEGIN_ALLOW_THREADS
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001481 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 Py_END_ALLOW_THREADS
1483 if (nfds < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001484 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 goto error;
1486 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 elist = PyList_New(nfds);
1489 if (elist == NULL) {
1490 goto error;
1491 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 for (i = 0; i < nfds; i++) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001494 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 if (etuple == NULL) {
1496 Py_CLEAR(elist);
1497 goto error;
1498 }
1499 PyList_SET_ITEM(elist, i, etuple);
1500 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001501
Christian Heimesf6cd9672008-03-26 13:45:42 +00001502 error:
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001503 PyMem_Free(evs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 return elist;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001505}
1506
1507PyDoc_STRVAR(pyepoll_poll_doc,
1508"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1509\n\
1510Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1511in seconds (as float). -1 makes poll wait indefinitely.\n\
1512Up to maxevents are returned to the caller.");
1513
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001514static PyObject *
1515pyepoll_enter(pyEpoll_Object *self, PyObject *args)
1516{
1517 if (self->epfd < 0)
1518 return pyepoll_err_closed();
1519
1520 Py_INCREF(self);
1521 return (PyObject *)self;
1522}
1523
1524static PyObject *
1525pyepoll_exit(PyObject *self, PyObject *args)
1526{
1527 _Py_IDENTIFIER(close);
1528
1529 return _PyObject_CallMethodId(self, &PyId_close, NULL);
1530}
1531
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001532static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 {"fromfd", (PyCFunction)pyepoll_fromfd,
1534 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1535 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1536 pyepoll_close_doc},
1537 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1538 pyepoll_fileno_doc},
1539 {"modify", (PyCFunction)pyepoll_modify,
1540 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1541 {"register", (PyCFunction)pyepoll_register,
1542 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1543 {"unregister", (PyCFunction)pyepoll_unregister,
1544 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1545 {"poll", (PyCFunction)pyepoll_poll,
1546 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001547 {"__enter__", (PyCFunction)pyepoll_enter, METH_NOARGS,
1548 NULL},
1549 {"__exit__", (PyCFunction)pyepoll_exit, METH_VARARGS,
1550 NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001552};
1553
1554static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 {"closed", (getter)pyepoll_get_closed, NULL,
1556 "True if the epoll handler is closed"},
1557 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001558};
1559
1560PyDoc_STRVAR(pyepoll_doc,
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001561"select.epoll(sizehint=-1, flags=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001562\n\
1563Returns an epolling object\n\
1564\n\
1565sizehint must be a positive integer or -1 for the default size. The\n\
1566sizehint is used to optimize internal data structures. It doesn't limit\n\
1567the maximum number of monitored events.");
1568
1569static PyTypeObject pyEpoll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 PyVarObject_HEAD_INIT(NULL, 0)
1571 "select.epoll", /* tp_name */
1572 sizeof(pyEpoll_Object), /* tp_basicsize */
1573 0, /* tp_itemsize */
1574 (destructor)pyepoll_dealloc, /* tp_dealloc */
1575 0, /* tp_print */
1576 0, /* tp_getattr */
1577 0, /* tp_setattr */
1578 0, /* tp_reserved */
1579 0, /* tp_repr */
1580 0, /* tp_as_number */
1581 0, /* tp_as_sequence */
1582 0, /* tp_as_mapping */
1583 0, /* tp_hash */
1584 0, /* tp_call */
1585 0, /* tp_str */
1586 PyObject_GenericGetAttr, /* tp_getattro */
1587 0, /* tp_setattro */
1588 0, /* tp_as_buffer */
1589 Py_TPFLAGS_DEFAULT, /* tp_flags */
1590 pyepoll_doc, /* tp_doc */
1591 0, /* tp_traverse */
1592 0, /* tp_clear */
1593 0, /* tp_richcompare */
1594 0, /* tp_weaklistoffset */
1595 0, /* tp_iter */
1596 0, /* tp_iternext */
1597 pyepoll_methods, /* tp_methods */
1598 0, /* tp_members */
1599 pyepoll_getsetlist, /* tp_getset */
1600 0, /* tp_base */
1601 0, /* tp_dict */
1602 0, /* tp_descr_get */
1603 0, /* tp_descr_set */
1604 0, /* tp_dictoffset */
1605 0, /* tp_init */
1606 0, /* tp_alloc */
1607 pyepoll_new, /* tp_new */
1608 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001609};
1610
1611#endif /* HAVE_EPOLL */
1612
1613#ifdef HAVE_KQUEUE
1614/* **************************************************************************
1615 * kqueue interface for BSD
1616 *
1617 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1618 * All rights reserved.
1619 *
1620 * Redistribution and use in source and binary forms, with or without
1621 * modification, are permitted provided that the following conditions
1622 * are met:
1623 * 1. Redistributions of source code must retain the above copyright
1624 * notice, this list of conditions and the following disclaimer.
1625 * 2. Redistributions in binary form must reproduce the above copyright
1626 * notice, this list of conditions and the following disclaimer in the
1627 * documentation and/or other materials provided with the distribution.
1628 *
1629 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1630 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1631 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1632 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1633 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1634 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1635 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1636 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1637 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1638 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1639 * SUCH DAMAGE.
1640 */
1641
1642#ifdef HAVE_SYS_EVENT_H
1643#include <sys/event.h>
1644#endif
1645
1646PyDoc_STRVAR(kqueue_event_doc,
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001647"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001648\n\
1649This object is the equivalent of the struct kevent for the C API.\n\
1650\n\
1651See the kqueue manpage for more detailed information about the meaning\n\
1652of the arguments.\n\
1653\n\
1654One minor note: while you might hope that udata could store a\n\
1655reference to a python object, it cannot, because it is impossible to\n\
1656keep a proper reference count of the object once it's passed into the\n\
1657kernel. Therefore, I have restricted it to only storing an integer. I\n\
1658recommend ignoring it and simply using the 'ident' field to key off\n\
1659of. You could also set up a dictionary on the python side to store a\n\
1660udata->object mapping.");
1661
1662typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 PyObject_HEAD
1664 struct kevent e;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001665} kqueue_event_Object;
1666
1667static PyTypeObject kqueue_event_Type;
1668
1669#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1670
1671typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 PyObject_HEAD
1673 SOCKET kqfd; /* kqueue control fd */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001674} kqueue_queue_Object;
1675
1676static PyTypeObject kqueue_queue_Type;
1677
1678#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1679
Antoine Pitroud83f1e62009-11-04 21:10:38 +00001680#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1681# error uintptr_t does not match void *!
1682#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1683# define T_UINTPTRT T_ULONGLONG
1684# define T_INTPTRT T_LONGLONG
1685# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1686# define UINTPTRT_FMT_UNIT "K"
1687# define INTPTRT_FMT_UNIT "L"
1688#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1689# define T_UINTPTRT T_ULONG
1690# define T_INTPTRT T_LONG
1691# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1692# define UINTPTRT_FMT_UNIT "k"
1693# define INTPTRT_FMT_UNIT "l"
1694#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1695# define T_UINTPTRT T_UINT
1696# define T_INTPTRT T_INT
1697# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1698# define UINTPTRT_FMT_UNIT "I"
1699# define INTPTRT_FMT_UNIT "i"
1700#else
1701# error uintptr_t does not match int, long, or long long!
1702#endif
1703
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001704/*
1705 * kevent is not standard and its members vary across BSDs.
1706 */
1707#if !defined(__OpenBSD__)
Christian Heimesaf01f662013-12-21 16:19:10 +01001708# define IDENT_TYPE T_UINTPTRT
1709# define IDENT_CAST Py_intptr_t
1710# define DATA_TYPE T_INTPTRT
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001711# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
Christian Heimesaf01f662013-12-21 16:19:10 +01001712# define IDENT_AsType PyLong_AsUintptr_t
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001713#else
Christian Heimesaf01f662013-12-21 16:19:10 +01001714# define IDENT_TYPE T_UINT
1715# define IDENT_CAST int
1716# define DATA_TYPE T_INT
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001717# define DATA_FMT_UNIT "i"
Christian Heimesaf01f662013-12-21 16:19:10 +01001718# define IDENT_AsType PyLong_AsUnsignedLong
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001719#endif
1720
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001721/* Unfortunately, we can't store python objects in udata, because
1722 * kevents in the kernel can be removed without warning, which would
1723 * forever lose the refcount on the object stored with it.
1724 */
1725
1726#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1727static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001728 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 {"filter", T_SHORT, KQ_OFF(e.filter)},
1730 {"flags", T_USHORT, KQ_OFF(e.flags)},
1731 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001732 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1734 {NULL} /* Sentinel */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001735};
1736#undef KQ_OFF
1737
1738static PyObject *
Georg Brandlc0e22b72010-03-14 10:51:01 +00001739
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001740kqueue_event_repr(kqueue_event_Object *s)
1741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 char buf[1024];
1743 PyOS_snprintf(
1744 buf, sizeof(buf),
1745 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1746 "data=0x%zd udata=%p>",
1747 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1748 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1749 return PyUnicode_FromString(buf);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001750}
1751
1752static int
1753kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 PyObject *pfd;
1756 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1757 "data", "udata", NULL};
Christian Heimesf1fe1592013-08-25 14:57:00 +02001758 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1763 &pfd, &(self->e.filter), &(self->e.flags),
1764 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1765 return -1;
1766 }
1767
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001768 if (PyLong_Check(pfd)
1769#if IDENT_TYPE == T_UINT
Christian Heimesaf01f662013-12-21 16:19:10 +01001770 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001771#endif
1772 ) {
1773 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 }
1775 else {
1776 self->e.ident = PyObject_AsFileDescriptor(pfd);
1777 }
1778 if (PyErr_Occurred()) {
1779 return -1;
1780 }
1781 return 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001782}
1783
1784static PyObject *
1785kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 int op)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 Py_intptr_t result = 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 if (!kqueue_event_Check(o)) {
1791 if (op == Py_EQ || op == Py_NE) {
1792 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1793 Py_INCREF(res);
1794 return res;
1795 }
1796 PyErr_Format(PyExc_TypeError,
1797 "can't compare %.200s to %.200s",
1798 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1799 return NULL;
1800 }
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001801 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 ((result = s->e.filter - o->e.filter) == 0) &&
1803 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001804 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 ((result = s->e.data - o->e.data) == 0) &&
1806 ((result = s->e.udata - o->e.udata) == 0)
1807 ) {
1808 result = 0;
1809 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 switch (op) {
Guido van Rossumee07b942013-12-06 17:46:22 -08001812 case Py_EQ:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 result = (result == 0);
1814 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001815 case Py_NE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 result = (result != 0);
1817 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001818 case Py_LE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 result = (result <= 0);
1820 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001821 case Py_GE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 result = (result >= 0);
1823 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001824 case Py_LT:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 result = (result < 0);
1826 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001827 case Py_GT:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 result = (result > 0);
1829 break;
1830 }
1831 return PyBool_FromLong((long)result);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001832}
1833
1834static PyTypeObject kqueue_event_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 PyVarObject_HEAD_INIT(NULL, 0)
1836 "select.kevent", /* tp_name */
1837 sizeof(kqueue_event_Object), /* tp_basicsize */
1838 0, /* tp_itemsize */
1839 0, /* tp_dealloc */
1840 0, /* tp_print */
1841 0, /* tp_getattr */
1842 0, /* tp_setattr */
1843 0, /* tp_reserved */
1844 (reprfunc)kqueue_event_repr, /* tp_repr */
1845 0, /* tp_as_number */
1846 0, /* tp_as_sequence */
1847 0, /* tp_as_mapping */
1848 0, /* tp_hash */
1849 0, /* tp_call */
1850 0, /* tp_str */
1851 0, /* tp_getattro */
1852 0, /* tp_setattro */
1853 0, /* tp_as_buffer */
1854 Py_TPFLAGS_DEFAULT, /* tp_flags */
1855 kqueue_event_doc, /* tp_doc */
1856 0, /* tp_traverse */
1857 0, /* tp_clear */
1858 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1859 0, /* tp_weaklistoffset */
1860 0, /* tp_iter */
1861 0, /* tp_iternext */
1862 0, /* tp_methods */
1863 kqueue_event_members, /* tp_members */
1864 0, /* tp_getset */
1865 0, /* tp_base */
1866 0, /* tp_dict */
1867 0, /* tp_descr_get */
1868 0, /* tp_descr_set */
1869 0, /* tp_dictoffset */
1870 (initproc)kqueue_event_init, /* tp_init */
1871 0, /* tp_alloc */
1872 0, /* tp_new */
1873 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001874};
1875
1876static PyObject *
1877kqueue_queue_err_closed(void)
1878{
Victor Stinner13423c32013-08-22 00:19:50 +02001879 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001881}
1882
1883static int
1884kqueue_queue_internal_close(kqueue_queue_Object *self)
1885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 int save_errno = 0;
1887 if (self->kqfd >= 0) {
1888 int kqfd = self->kqfd;
1889 self->kqfd = -1;
1890 Py_BEGIN_ALLOW_THREADS
1891 if (close(kqfd) < 0)
1892 save_errno = errno;
1893 Py_END_ALLOW_THREADS
1894 }
1895 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001896}
1897
1898static PyObject *
1899newKqueue_Object(PyTypeObject *type, SOCKET fd)
1900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 kqueue_queue_Object *self;
1902 assert(type != NULL && type->tp_alloc != NULL);
1903 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1904 if (self == NULL) {
1905 return NULL;
1906 }
1907
1908 if (fd == -1) {
1909 Py_BEGIN_ALLOW_THREADS
1910 self->kqfd = kqueue();
1911 Py_END_ALLOW_THREADS
1912 }
1913 else {
1914 self->kqfd = fd;
1915 }
1916 if (self->kqfd < 0) {
1917 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001918 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 return NULL;
1920 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001921
1922 if (fd == -1) {
1923 if (_Py_set_inheritable(self->kqfd, 0, NULL) < 0) {
1924 Py_DECREF(self);
1925 return NULL;
1926 }
1927 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001929}
1930
1931static PyObject *
1932kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 if ((args != NULL && PyObject_Size(args)) ||
1935 (kwds != NULL && PyObject_Size(kwds))) {
1936 PyErr_SetString(PyExc_ValueError,
1937 "select.kqueue doesn't accept arguments");
1938 return NULL;
1939 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 return newKqueue_Object(type, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001942}
1943
1944static void
1945kqueue_queue_dealloc(kqueue_queue_Object *self)
1946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 kqueue_queue_internal_close(self);
1948 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001949}
1950
1951static PyObject*
1952kqueue_queue_close(kqueue_queue_Object *self)
1953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 errno = kqueue_queue_internal_close(self);
1955 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001956 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 return NULL;
1958 }
1959 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001960}
1961
1962PyDoc_STRVAR(kqueue_queue_close_doc,
1963"close() -> None\n\
1964\n\
1965Close the kqueue control file descriptor. Further operations on the kqueue\n\
1966object will raise an exception.");
1967
1968static PyObject*
1969kqueue_queue_get_closed(kqueue_queue_Object *self)
1970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 if (self->kqfd < 0)
1972 Py_RETURN_TRUE;
1973 else
1974 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001975}
1976
1977static PyObject*
1978kqueue_queue_fileno(kqueue_queue_Object *self)
1979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 if (self->kqfd < 0)
1981 return kqueue_queue_err_closed();
1982 return PyLong_FromLong(self->kqfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001983}
1984
1985PyDoc_STRVAR(kqueue_queue_fileno_doc,
1986"fileno() -> int\n\
1987\n\
1988Return the kqueue control file descriptor.");
1989
1990static PyObject*
1991kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1996 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001999}
2000
2001PyDoc_STRVAR(kqueue_queue_fromfd_doc,
2002"fromfd(fd) -> kqueue\n\
2003\n\
2004Create a kqueue object from a given control fd.");
2005
2006static PyObject *
2007kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
2008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 int nevents = 0;
2010 int gotevents = 0;
2011 int nchanges = 0;
2012 int i = 0;
2013 PyObject *otimeout = NULL;
2014 PyObject *ch = NULL;
2015 PyObject *it = NULL, *ei = NULL;
2016 PyObject *result = NULL;
2017 struct kevent *evl = NULL;
2018 struct kevent *chl = NULL;
Victor Stinnerd327f9d2012-03-13 15:29:08 +01002019 struct timespec timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 struct timespec *ptimeoutspec;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 if (self->kqfd < 0)
2023 return kqueue_queue_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
2026 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 if (nevents < 0) {
2029 PyErr_Format(PyExc_ValueError,
2030 "Length of eventlist must be 0 or positive, got %d",
2031 nevents);
2032 return NULL;
2033 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 if (otimeout == Py_None || otimeout == NULL) {
2036 ptimeoutspec = NULL;
2037 }
2038 else if (PyNumber_Check(otimeout)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +01002039 if (_PyTime_ObjectToTimespec(otimeout,
2040 &timeout.tv_sec, &timeout.tv_nsec) == -1)
2041 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002042
Victor Stinner5d272cc2012-03-13 13:35:55 +01002043 if (timeout.tv_sec < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 PyErr_SetString(PyExc_ValueError,
2045 "timeout must be positive or None");
2046 return NULL;
2047 }
Victor Stinnerd528b012012-03-13 16:25:35 +01002048 ptimeoutspec = &timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 }
2050 else {
2051 PyErr_Format(PyExc_TypeError,
2052 "timeout argument must be an number "
2053 "or None, got %.200s",
2054 Py_TYPE(otimeout)->tp_name);
2055 return NULL;
2056 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 if (ch != NULL && ch != Py_None) {
2059 it = PyObject_GetIter(ch);
2060 if (it == NULL) {
2061 PyErr_SetString(PyExc_TypeError,
2062 "changelist is not iterable");
2063 return NULL;
2064 }
2065 nchanges = PyObject_Size(ch);
2066 if (nchanges < 0) {
2067 goto error;
2068 }
Georg Brandlc0e22b72010-03-14 10:51:01 +00002069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 chl = PyMem_New(struct kevent, nchanges);
2071 if (chl == NULL) {
2072 PyErr_NoMemory();
2073 goto error;
2074 }
2075 i = 0;
2076 while ((ei = PyIter_Next(it)) != NULL) {
2077 if (!kqueue_event_Check(ei)) {
2078 Py_DECREF(ei);
2079 PyErr_SetString(PyExc_TypeError,
2080 "changelist must be an iterable of "
2081 "select.kevent objects");
2082 goto error;
2083 } else {
2084 chl[i++] = ((kqueue_event_Object *)ei)->e;
2085 }
2086 Py_DECREF(ei);
2087 }
2088 }
2089 Py_CLEAR(it);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 /* event list */
2092 if (nevents) {
2093 evl = PyMem_New(struct kevent, nevents);
2094 if (evl == NULL) {
2095 PyErr_NoMemory();
2096 goto error;
2097 }
2098 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 Py_BEGIN_ALLOW_THREADS
2101 gotevents = kevent(self->kqfd, chl, nchanges,
2102 evl, nevents, ptimeoutspec);
2103 Py_END_ALLOW_THREADS
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 if (gotevents == -1) {
2106 PyErr_SetFromErrno(PyExc_OSError);
2107 goto error;
2108 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 result = PyList_New(gotevents);
2111 if (result == NULL) {
2112 goto error;
2113 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 for (i = 0; i < gotevents; i++) {
2116 kqueue_event_Object *ch;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
2119 if (ch == NULL) {
2120 goto error;
2121 }
2122 ch->e = evl[i];
2123 PyList_SET_ITEM(result, i, (PyObject *)ch);
2124 }
2125 PyMem_Free(chl);
2126 PyMem_Free(evl);
2127 return result;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002128
2129 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 PyMem_Free(chl);
2131 PyMem_Free(evl);
2132 Py_XDECREF(result);
2133 Py_XDECREF(it);
2134 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002135}
2136
2137PyDoc_STRVAR(kqueue_queue_control_doc,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00002138"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002139\n\
2140Calls the kernel kevent function.\n\
2141- changelist must be a list of kevent objects describing the changes\n\
2142 to be made to the kernel's watch list or None.\n\
2143- max_events lets you specify the maximum number of events that the\n\
2144 kernel will return.\n\
2145- timeout is the maximum time to wait in seconds, or else None,\n\
2146 to wait forever. timeout accepts floats for smaller timeouts, too.");
2147
2148
2149static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
2151 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
2152 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
2153 kqueue_queue_close_doc},
2154 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
2155 kqueue_queue_fileno_doc},
2156 {"control", (PyCFunction)kqueue_queue_control,
2157 METH_VARARGS , kqueue_queue_control_doc},
2158 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002159};
2160
2161static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 {"closed", (getter)kqueue_queue_get_closed, NULL,
2163 "True if the kqueue handler is closed"},
2164 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002165};
2166
2167PyDoc_STRVAR(kqueue_queue_doc,
2168"Kqueue syscall wrapper.\n\
2169\n\
2170For example, to start watching a socket for input:\n\
2171>>> kq = kqueue()\n\
2172>>> sock = socket()\n\
2173>>> sock.connect((host, port))\n\
2174>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
2175\n\
2176To wait one second for it to become writeable:\n\
2177>>> kq.control(None, 1, 1000)\n\
2178\n\
2179To stop listening:\n\
2180>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
2181
2182static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 PyVarObject_HEAD_INIT(NULL, 0)
2184 "select.kqueue", /* tp_name */
2185 sizeof(kqueue_queue_Object), /* tp_basicsize */
2186 0, /* tp_itemsize */
2187 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
2188 0, /* tp_print */
2189 0, /* tp_getattr */
2190 0, /* tp_setattr */
2191 0, /* tp_reserved */
2192 0, /* tp_repr */
2193 0, /* tp_as_number */
2194 0, /* tp_as_sequence */
2195 0, /* tp_as_mapping */
2196 0, /* tp_hash */
2197 0, /* tp_call */
2198 0, /* tp_str */
2199 0, /* tp_getattro */
2200 0, /* tp_setattro */
2201 0, /* tp_as_buffer */
2202 Py_TPFLAGS_DEFAULT, /* tp_flags */
2203 kqueue_queue_doc, /* tp_doc */
2204 0, /* tp_traverse */
2205 0, /* tp_clear */
2206 0, /* tp_richcompare */
2207 0, /* tp_weaklistoffset */
2208 0, /* tp_iter */
2209 0, /* tp_iternext */
2210 kqueue_queue_methods, /* tp_methods */
2211 0, /* tp_members */
2212 kqueue_queue_getsetlist, /* tp_getset */
2213 0, /* tp_base */
2214 0, /* tp_dict */
2215 0, /* tp_descr_get */
2216 0, /* tp_descr_set */
2217 0, /* tp_dictoffset */
2218 0, /* tp_init */
2219 0, /* tp_alloc */
2220 kqueue_queue_new, /* tp_new */
2221 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002222};
2223
2224#endif /* HAVE_KQUEUE */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002225
2226
2227
2228
2229
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002230/* ************************************************************************ */
2231
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002232PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002233"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
2234\n\
2235Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00002236The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002237rlist -- wait until ready for reading\n\
2238wlist -- wait until ready for writing\n\
2239xlist -- wait for an ``exceptional condition''\n\
2240If only one kind of condition is required, pass [] for the other lists.\n\
2241A file descriptor is either a socket or file object, or a small integer\n\
2242gotten from a fileno() method call on one of those.\n\
2243\n\
2244The optional 4th argument specifies a timeout in seconds; it may be\n\
2245a floating point number to specify fractions of seconds. If it is absent\n\
2246or None, the call will never time out.\n\
2247\n\
2248The return value is a tuple of three lists corresponding to the first three\n\
2249arguments; each contains the subset of the corresponding file descriptors\n\
2250that are ready.\n\
2251\n\
2252*** IMPORTANT NOTICE ***\n\
Christian Heimesaf01f662013-12-21 16:19:10 +01002253On Windows only sockets are supported; on Unix, all file\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00002254descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002255
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002256static PyMethodDef select_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natali986a56c2013-01-19 12:19:10 +01002258#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002260#endif /* HAVE_POLL */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002261#ifdef HAVE_SYS_DEVPOLL_H
2262 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2263#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00002265};
2266
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002267PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002268"This module supports asynchronous I/O on multiple file descriptors.\n\
2269\n\
2270*** IMPORTANT NOTICE ***\n\
Christian Heimesaf01f662013-12-21 16:19:10 +01002271On Windows only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00002272
Martin v. Löwis1a214512008-06-11 05:26:20 +00002273
2274static struct PyModuleDef selectmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 PyModuleDef_HEAD_INIT,
2276 "select",
2277 module_doc,
2278 -1,
2279 select_methods,
2280 NULL,
2281 NULL,
2282 NULL,
2283 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002284};
2285
Jesus Cead8b9ae62011-11-14 19:07:41 +01002286
2287
2288
Mark Hammond62b1ab12002-07-23 06:31:15 +00002289PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002290PyInit_select(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00002291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 PyObject *m;
2293 m = PyModule_Create(&selectmodule);
2294 if (m == NULL)
2295 return NULL;
Fred Drake4baedc12002-04-01 14:53:37 +00002296
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002297 Py_INCREF(PyExc_OSError);
2298 PyModule_AddObject(m, "error", PyExc_OSError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002299
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002300#ifdef PIPE_BUF
R. David Murraye16cda92010-10-15 23:12:57 +00002301#ifdef HAVE_BROKEN_PIPE_BUF
2302#undef PIPE_BUF
2303#define PIPE_BUF 512
2304#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002305 PyModule_AddIntMacro(m, PIPE_BUF);
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002306#endif
Gregory P. Smithb970b862009-07-04 02:28:47 +00002307
Charles-François Natali986a56c2013-01-19 12:19:10 +01002308#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002309#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (select_have_broken_poll()) {
2311 if (PyObject_DelAttrString(m, "poll") == -1) {
2312 PyErr_Clear();
2313 }
2314 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002315#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002317#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 if (PyType_Ready(&poll_Type) < 0)
2319 return NULL;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002320 PyModule_AddIntMacro(m, POLLIN);
2321 PyModule_AddIntMacro(m, POLLPRI);
2322 PyModule_AddIntMacro(m, POLLOUT);
2323 PyModule_AddIntMacro(m, POLLERR);
2324 PyModule_AddIntMacro(m, POLLHUP);
2325 PyModule_AddIntMacro(m, POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00002326
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002327#ifdef POLLRDNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002328 PyModule_AddIntMacro(m, POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002329#endif
2330#ifdef POLLRDBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002331 PyModule_AddIntMacro(m, POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002332#endif
2333#ifdef POLLWRNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002334 PyModule_AddIntMacro(m, POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002335#endif
2336#ifdef POLLWRBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002337 PyModule_AddIntMacro(m, POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002338#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002339#ifdef POLLMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002340 PyModule_AddIntMacro(m, POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002341#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002343#endif /* HAVE_POLL */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002344
Jesus Cead8b9ae62011-11-14 19:07:41 +01002345#ifdef HAVE_SYS_DEVPOLL_H
2346 if (PyType_Ready(&devpoll_Type) < 0)
2347 return NULL;
2348#endif
2349
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002350#ifdef HAVE_EPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
2352 if (PyType_Ready(&pyEpoll_Type) < 0)
2353 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 Py_INCREF(&pyEpoll_Type);
2356 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002357
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002358 PyModule_AddIntMacro(m, EPOLLIN);
2359 PyModule_AddIntMacro(m, EPOLLOUT);
2360 PyModule_AddIntMacro(m, EPOLLPRI);
2361 PyModule_AddIntMacro(m, EPOLLERR);
2362 PyModule_AddIntMacro(m, EPOLLHUP);
2363 PyModule_AddIntMacro(m, EPOLLET);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002364#ifdef EPOLLONESHOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* Kernel 2.6.2+ */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002366 PyModule_AddIntMacro(m, EPOLLONESHOT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002367#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002369 PyModule_AddIntMacro(m, EPOLLRDNORM);
2370 PyModule_AddIntMacro(m, EPOLLRDBAND);
2371 PyModule_AddIntMacro(m, EPOLLWRNORM);
2372 PyModule_AddIntMacro(m, EPOLLWRBAND);
2373 PyModule_AddIntMacro(m, EPOLLMSG);
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002374
Benjamin Peterson95c16622011-12-27 15:36:32 -06002375#ifdef EPOLL_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002376 PyModule_AddIntMacro(m, EPOLL_CLOEXEC);
Benjamin Peterson95c16622011-12-27 15:36:32 -06002377#endif
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002378#endif /* HAVE_EPOLL */
2379
2380#ifdef HAVE_KQUEUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 kqueue_event_Type.tp_new = PyType_GenericNew;
2382 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
2383 if(PyType_Ready(&kqueue_event_Type) < 0)
2384 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 Py_INCREF(&kqueue_event_Type);
2387 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
2390 if(PyType_Ready(&kqueue_queue_Type) < 0)
2391 return NULL;
2392 Py_INCREF(&kqueue_queue_Type);
2393 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
2394
2395 /* event filters */
2396 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
2397 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
2398 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
2399 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
2400 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002401#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002403#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
2405 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 /* event flags */
2408 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
2409 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
2410 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
2411 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
2412 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
2413 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
2416 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
2419 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 /* READ WRITE filter flag */
2422 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 /* VNODE filter flags */
2425 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
2426 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
2427 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
2428 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
2429 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
2430 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
2431 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 /* PROC filter flags */
2434 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
2435 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
2436 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
2437 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
2438 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
2441 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
2442 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
2443
2444 /* NETDEV filter flags */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002445#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
2447 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
2448 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002449#endif
2450
2451#endif /* HAVE_KQUEUE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 return m;
Guido van Rossumed233a51992-06-23 09:07:03 +00002453}