blob: 0c9b9d9f8c98743a3dd824c5c87777827c107406 [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 {
Victor Stinnerdcd97402014-01-31 12:12:53 +01001461 /* epoll_wait() has a resolution of 1 millisecond, round away from zero
1462 to wait *at least* dtimeout seconds. */
1463 timeout = (int)ceil(dtimeout * 1000.0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 if (maxevents == -1) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001467 maxevents = FD_SETSIZE-1;
1468 }
1469 else if (maxevents < 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 PyErr_Format(PyExc_ValueError,
1471 "maxevents must be greater than 0, got %d",
1472 maxevents);
1473 return NULL;
1474 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001475
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001476 evs = PyMem_New(struct epoll_event, maxevents);
1477 if (evs == NULL) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001478 PyErr_NoMemory();
1479 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 Py_BEGIN_ALLOW_THREADS
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001483 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 Py_END_ALLOW_THREADS
1485 if (nfds < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001486 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 goto error;
1488 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 elist = PyList_New(nfds);
1491 if (elist == NULL) {
1492 goto error;
1493 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 for (i = 0; i < nfds; i++) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001496 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (etuple == NULL) {
1498 Py_CLEAR(elist);
1499 goto error;
1500 }
1501 PyList_SET_ITEM(elist, i, etuple);
1502 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001503
Christian Heimesf6cd9672008-03-26 13:45:42 +00001504 error:
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001505 PyMem_Free(evs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 return elist;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001507}
1508
1509PyDoc_STRVAR(pyepoll_poll_doc,
1510"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1511\n\
1512Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1513in seconds (as float). -1 makes poll wait indefinitely.\n\
1514Up to maxevents are returned to the caller.");
1515
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001516static PyObject *
1517pyepoll_enter(pyEpoll_Object *self, PyObject *args)
1518{
1519 if (self->epfd < 0)
1520 return pyepoll_err_closed();
1521
1522 Py_INCREF(self);
1523 return (PyObject *)self;
1524}
1525
1526static PyObject *
1527pyepoll_exit(PyObject *self, PyObject *args)
1528{
1529 _Py_IDENTIFIER(close);
1530
1531 return _PyObject_CallMethodId(self, &PyId_close, NULL);
1532}
1533
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001534static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 {"fromfd", (PyCFunction)pyepoll_fromfd,
1536 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1537 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1538 pyepoll_close_doc},
1539 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1540 pyepoll_fileno_doc},
1541 {"modify", (PyCFunction)pyepoll_modify,
1542 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1543 {"register", (PyCFunction)pyepoll_register,
1544 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1545 {"unregister", (PyCFunction)pyepoll_unregister,
1546 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1547 {"poll", (PyCFunction)pyepoll_poll,
1548 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001549 {"__enter__", (PyCFunction)pyepoll_enter, METH_NOARGS,
1550 NULL},
1551 {"__exit__", (PyCFunction)pyepoll_exit, METH_VARARGS,
1552 NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001554};
1555
1556static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 {"closed", (getter)pyepoll_get_closed, NULL,
1558 "True if the epoll handler is closed"},
1559 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001560};
1561
1562PyDoc_STRVAR(pyepoll_doc,
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001563"select.epoll(sizehint=-1, flags=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001564\n\
1565Returns an epolling object\n\
1566\n\
1567sizehint must be a positive integer or -1 for the default size. The\n\
1568sizehint is used to optimize internal data structures. It doesn't limit\n\
1569the maximum number of monitored events.");
1570
1571static PyTypeObject pyEpoll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 PyVarObject_HEAD_INIT(NULL, 0)
1573 "select.epoll", /* tp_name */
1574 sizeof(pyEpoll_Object), /* tp_basicsize */
1575 0, /* tp_itemsize */
1576 (destructor)pyepoll_dealloc, /* tp_dealloc */
1577 0, /* tp_print */
1578 0, /* tp_getattr */
1579 0, /* tp_setattr */
1580 0, /* tp_reserved */
1581 0, /* tp_repr */
1582 0, /* tp_as_number */
1583 0, /* tp_as_sequence */
1584 0, /* tp_as_mapping */
1585 0, /* tp_hash */
1586 0, /* tp_call */
1587 0, /* tp_str */
1588 PyObject_GenericGetAttr, /* tp_getattro */
1589 0, /* tp_setattro */
1590 0, /* tp_as_buffer */
1591 Py_TPFLAGS_DEFAULT, /* tp_flags */
1592 pyepoll_doc, /* tp_doc */
1593 0, /* tp_traverse */
1594 0, /* tp_clear */
1595 0, /* tp_richcompare */
1596 0, /* tp_weaklistoffset */
1597 0, /* tp_iter */
1598 0, /* tp_iternext */
1599 pyepoll_methods, /* tp_methods */
1600 0, /* tp_members */
1601 pyepoll_getsetlist, /* tp_getset */
1602 0, /* tp_base */
1603 0, /* tp_dict */
1604 0, /* tp_descr_get */
1605 0, /* tp_descr_set */
1606 0, /* tp_dictoffset */
1607 0, /* tp_init */
1608 0, /* tp_alloc */
1609 pyepoll_new, /* tp_new */
1610 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001611};
1612
1613#endif /* HAVE_EPOLL */
1614
1615#ifdef HAVE_KQUEUE
1616/* **************************************************************************
1617 * kqueue interface for BSD
1618 *
1619 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1620 * All rights reserved.
1621 *
1622 * Redistribution and use in source and binary forms, with or without
1623 * modification, are permitted provided that the following conditions
1624 * are met:
1625 * 1. Redistributions of source code must retain the above copyright
1626 * notice, this list of conditions and the following disclaimer.
1627 * 2. Redistributions in binary form must reproduce the above copyright
1628 * notice, this list of conditions and the following disclaimer in the
1629 * documentation and/or other materials provided with the distribution.
1630 *
1631 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1632 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1633 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1634 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1635 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1636 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1637 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1638 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1639 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1640 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1641 * SUCH DAMAGE.
1642 */
1643
1644#ifdef HAVE_SYS_EVENT_H
1645#include <sys/event.h>
1646#endif
1647
1648PyDoc_STRVAR(kqueue_event_doc,
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001649"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001650\n\
1651This object is the equivalent of the struct kevent for the C API.\n\
1652\n\
1653See the kqueue manpage for more detailed information about the meaning\n\
1654of the arguments.\n\
1655\n\
1656One minor note: while you might hope that udata could store a\n\
1657reference to a python object, it cannot, because it is impossible to\n\
1658keep a proper reference count of the object once it's passed into the\n\
1659kernel. Therefore, I have restricted it to only storing an integer. I\n\
1660recommend ignoring it and simply using the 'ident' field to key off\n\
1661of. You could also set up a dictionary on the python side to store a\n\
1662udata->object mapping.");
1663
1664typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 PyObject_HEAD
1666 struct kevent e;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001667} kqueue_event_Object;
1668
1669static PyTypeObject kqueue_event_Type;
1670
1671#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1672
1673typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 PyObject_HEAD
1675 SOCKET kqfd; /* kqueue control fd */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001676} kqueue_queue_Object;
1677
1678static PyTypeObject kqueue_queue_Type;
1679
1680#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1681
Antoine Pitroud83f1e62009-11-04 21:10:38 +00001682#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1683# error uintptr_t does not match void *!
1684#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1685# define T_UINTPTRT T_ULONGLONG
1686# define T_INTPTRT T_LONGLONG
1687# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1688# define UINTPTRT_FMT_UNIT "K"
1689# define INTPTRT_FMT_UNIT "L"
1690#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1691# define T_UINTPTRT T_ULONG
1692# define T_INTPTRT T_LONG
1693# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1694# define UINTPTRT_FMT_UNIT "k"
1695# define INTPTRT_FMT_UNIT "l"
1696#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1697# define T_UINTPTRT T_UINT
1698# define T_INTPTRT T_INT
1699# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1700# define UINTPTRT_FMT_UNIT "I"
1701# define INTPTRT_FMT_UNIT "i"
1702#else
1703# error uintptr_t does not match int, long, or long long!
1704#endif
1705
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001706/*
1707 * kevent is not standard and its members vary across BSDs.
1708 */
1709#if !defined(__OpenBSD__)
Christian Heimesaf01f662013-12-21 16:19:10 +01001710# define IDENT_TYPE T_UINTPTRT
1711# define IDENT_CAST Py_intptr_t
1712# define DATA_TYPE T_INTPTRT
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001713# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
Christian Heimesaf01f662013-12-21 16:19:10 +01001714# define IDENT_AsType PyLong_AsUintptr_t
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001715#else
Christian Heimesaf01f662013-12-21 16:19:10 +01001716# define IDENT_TYPE T_UINT
1717# define IDENT_CAST int
1718# define DATA_TYPE T_INT
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001719# define DATA_FMT_UNIT "i"
Christian Heimesaf01f662013-12-21 16:19:10 +01001720# define IDENT_AsType PyLong_AsUnsignedLong
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001721#endif
1722
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001723/* Unfortunately, we can't store python objects in udata, because
1724 * kevents in the kernel can be removed without warning, which would
1725 * forever lose the refcount on the object stored with it.
1726 */
1727
1728#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1729static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001730 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 {"filter", T_SHORT, KQ_OFF(e.filter)},
1732 {"flags", T_USHORT, KQ_OFF(e.flags)},
1733 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001734 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1736 {NULL} /* Sentinel */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001737};
1738#undef KQ_OFF
1739
1740static PyObject *
Georg Brandlc0e22b72010-03-14 10:51:01 +00001741
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001742kqueue_event_repr(kqueue_event_Object *s)
1743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 char buf[1024];
1745 PyOS_snprintf(
1746 buf, sizeof(buf),
1747 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1748 "data=0x%zd udata=%p>",
1749 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1750 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1751 return PyUnicode_FromString(buf);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001752}
1753
1754static int
1755kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 PyObject *pfd;
1758 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1759 "data", "udata", NULL};
Christian Heimesf1fe1592013-08-25 14:57:00 +02001760 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1765 &pfd, &(self->e.filter), &(self->e.flags),
1766 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1767 return -1;
1768 }
1769
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001770 if (PyLong_Check(pfd)
1771#if IDENT_TYPE == T_UINT
Christian Heimesaf01f662013-12-21 16:19:10 +01001772 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001773#endif
1774 ) {
1775 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 }
1777 else {
1778 self->e.ident = PyObject_AsFileDescriptor(pfd);
1779 }
1780 if (PyErr_Occurred()) {
1781 return -1;
1782 }
1783 return 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001784}
1785
1786static PyObject *
1787kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 int op)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001789{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 Py_intptr_t result = 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 if (!kqueue_event_Check(o)) {
1793 if (op == Py_EQ || op == Py_NE) {
1794 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1795 Py_INCREF(res);
1796 return res;
1797 }
1798 PyErr_Format(PyExc_TypeError,
1799 "can't compare %.200s to %.200s",
1800 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1801 return NULL;
1802 }
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001803 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 ((result = s->e.filter - o->e.filter) == 0) &&
1805 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001806 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 ((result = s->e.data - o->e.data) == 0) &&
1808 ((result = s->e.udata - o->e.udata) == 0)
1809 ) {
1810 result = 0;
1811 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 switch (op) {
Guido van Rossumee07b942013-12-06 17:46:22 -08001814 case Py_EQ:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 result = (result == 0);
1816 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001817 case Py_NE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 result = (result != 0);
1819 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001820 case Py_LE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 result = (result <= 0);
1822 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001823 case Py_GE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 result = (result >= 0);
1825 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001826 case Py_LT:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 result = (result < 0);
1828 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001829 case Py_GT:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 result = (result > 0);
1831 break;
1832 }
1833 return PyBool_FromLong((long)result);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001834}
1835
1836static PyTypeObject kqueue_event_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 PyVarObject_HEAD_INIT(NULL, 0)
1838 "select.kevent", /* tp_name */
1839 sizeof(kqueue_event_Object), /* tp_basicsize */
1840 0, /* tp_itemsize */
1841 0, /* tp_dealloc */
1842 0, /* tp_print */
1843 0, /* tp_getattr */
1844 0, /* tp_setattr */
1845 0, /* tp_reserved */
1846 (reprfunc)kqueue_event_repr, /* tp_repr */
1847 0, /* tp_as_number */
1848 0, /* tp_as_sequence */
1849 0, /* tp_as_mapping */
1850 0, /* tp_hash */
1851 0, /* tp_call */
1852 0, /* tp_str */
1853 0, /* tp_getattro */
1854 0, /* tp_setattro */
1855 0, /* tp_as_buffer */
1856 Py_TPFLAGS_DEFAULT, /* tp_flags */
1857 kqueue_event_doc, /* tp_doc */
1858 0, /* tp_traverse */
1859 0, /* tp_clear */
1860 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1861 0, /* tp_weaklistoffset */
1862 0, /* tp_iter */
1863 0, /* tp_iternext */
1864 0, /* tp_methods */
1865 kqueue_event_members, /* tp_members */
1866 0, /* tp_getset */
1867 0, /* tp_base */
1868 0, /* tp_dict */
1869 0, /* tp_descr_get */
1870 0, /* tp_descr_set */
1871 0, /* tp_dictoffset */
1872 (initproc)kqueue_event_init, /* tp_init */
1873 0, /* tp_alloc */
1874 0, /* tp_new */
1875 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001876};
1877
1878static PyObject *
1879kqueue_queue_err_closed(void)
1880{
Victor Stinner13423c32013-08-22 00:19:50 +02001881 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001883}
1884
1885static int
1886kqueue_queue_internal_close(kqueue_queue_Object *self)
1887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 int save_errno = 0;
1889 if (self->kqfd >= 0) {
1890 int kqfd = self->kqfd;
1891 self->kqfd = -1;
1892 Py_BEGIN_ALLOW_THREADS
1893 if (close(kqfd) < 0)
1894 save_errno = errno;
1895 Py_END_ALLOW_THREADS
1896 }
1897 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001898}
1899
1900static PyObject *
1901newKqueue_Object(PyTypeObject *type, SOCKET fd)
1902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 kqueue_queue_Object *self;
1904 assert(type != NULL && type->tp_alloc != NULL);
1905 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1906 if (self == NULL) {
1907 return NULL;
1908 }
1909
1910 if (fd == -1) {
1911 Py_BEGIN_ALLOW_THREADS
1912 self->kqfd = kqueue();
1913 Py_END_ALLOW_THREADS
1914 }
1915 else {
1916 self->kqfd = fd;
1917 }
1918 if (self->kqfd < 0) {
1919 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001920 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 return NULL;
1922 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001923
1924 if (fd == -1) {
1925 if (_Py_set_inheritable(self->kqfd, 0, NULL) < 0) {
1926 Py_DECREF(self);
1927 return NULL;
1928 }
1929 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001931}
1932
1933static PyObject *
1934kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 if ((args != NULL && PyObject_Size(args)) ||
1937 (kwds != NULL && PyObject_Size(kwds))) {
1938 PyErr_SetString(PyExc_ValueError,
1939 "select.kqueue doesn't accept arguments");
1940 return NULL;
1941 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 return newKqueue_Object(type, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001944}
1945
1946static void
1947kqueue_queue_dealloc(kqueue_queue_Object *self)
1948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 kqueue_queue_internal_close(self);
1950 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001951}
1952
1953static PyObject*
1954kqueue_queue_close(kqueue_queue_Object *self)
1955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 errno = kqueue_queue_internal_close(self);
1957 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001958 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 return NULL;
1960 }
1961 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001962}
1963
1964PyDoc_STRVAR(kqueue_queue_close_doc,
1965"close() -> None\n\
1966\n\
1967Close the kqueue control file descriptor. Further operations on the kqueue\n\
1968object will raise an exception.");
1969
1970static PyObject*
1971kqueue_queue_get_closed(kqueue_queue_Object *self)
1972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 if (self->kqfd < 0)
1974 Py_RETURN_TRUE;
1975 else
1976 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001977}
1978
1979static PyObject*
1980kqueue_queue_fileno(kqueue_queue_Object *self)
1981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 if (self->kqfd < 0)
1983 return kqueue_queue_err_closed();
1984 return PyLong_FromLong(self->kqfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001985}
1986
1987PyDoc_STRVAR(kqueue_queue_fileno_doc,
1988"fileno() -> int\n\
1989\n\
1990Return the kqueue control file descriptor.");
1991
1992static PyObject*
1993kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1998 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002001}
2002
2003PyDoc_STRVAR(kqueue_queue_fromfd_doc,
2004"fromfd(fd) -> kqueue\n\
2005\n\
2006Create a kqueue object from a given control fd.");
2007
2008static PyObject *
2009kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
2010{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 int nevents = 0;
2012 int gotevents = 0;
2013 int nchanges = 0;
2014 int i = 0;
2015 PyObject *otimeout = NULL;
2016 PyObject *ch = NULL;
2017 PyObject *it = NULL, *ei = NULL;
2018 PyObject *result = NULL;
2019 struct kevent *evl = NULL;
2020 struct kevent *chl = NULL;
Victor Stinnerd327f9d2012-03-13 15:29:08 +01002021 struct timespec timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 struct timespec *ptimeoutspec;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 if (self->kqfd < 0)
2025 return kqueue_queue_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
2028 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 if (nevents < 0) {
2031 PyErr_Format(PyExc_ValueError,
2032 "Length of eventlist must be 0 or positive, got %d",
2033 nevents);
2034 return NULL;
2035 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 if (otimeout == Py_None || otimeout == NULL) {
2038 ptimeoutspec = NULL;
2039 }
2040 else if (PyNumber_Check(otimeout)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +01002041 if (_PyTime_ObjectToTimespec(otimeout,
2042 &timeout.tv_sec, &timeout.tv_nsec) == -1)
2043 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002044
Victor Stinner5d272cc2012-03-13 13:35:55 +01002045 if (timeout.tv_sec < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 PyErr_SetString(PyExc_ValueError,
2047 "timeout must be positive or None");
2048 return NULL;
2049 }
Victor Stinnerd528b012012-03-13 16:25:35 +01002050 ptimeoutspec = &timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 }
2052 else {
2053 PyErr_Format(PyExc_TypeError,
2054 "timeout argument must be an number "
2055 "or None, got %.200s",
2056 Py_TYPE(otimeout)->tp_name);
2057 return NULL;
2058 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 if (ch != NULL && ch != Py_None) {
2061 it = PyObject_GetIter(ch);
2062 if (it == NULL) {
2063 PyErr_SetString(PyExc_TypeError,
2064 "changelist is not iterable");
2065 return NULL;
2066 }
2067 nchanges = PyObject_Size(ch);
2068 if (nchanges < 0) {
2069 goto error;
2070 }
Georg Brandlc0e22b72010-03-14 10:51:01 +00002071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 chl = PyMem_New(struct kevent, nchanges);
2073 if (chl == NULL) {
2074 PyErr_NoMemory();
2075 goto error;
2076 }
2077 i = 0;
2078 while ((ei = PyIter_Next(it)) != NULL) {
2079 if (!kqueue_event_Check(ei)) {
2080 Py_DECREF(ei);
2081 PyErr_SetString(PyExc_TypeError,
2082 "changelist must be an iterable of "
2083 "select.kevent objects");
2084 goto error;
2085 } else {
2086 chl[i++] = ((kqueue_event_Object *)ei)->e;
2087 }
2088 Py_DECREF(ei);
2089 }
2090 }
2091 Py_CLEAR(it);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 /* event list */
2094 if (nevents) {
2095 evl = PyMem_New(struct kevent, nevents);
2096 if (evl == NULL) {
2097 PyErr_NoMemory();
2098 goto error;
2099 }
2100 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 Py_BEGIN_ALLOW_THREADS
2103 gotevents = kevent(self->kqfd, chl, nchanges,
2104 evl, nevents, ptimeoutspec);
2105 Py_END_ALLOW_THREADS
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 if (gotevents == -1) {
2108 PyErr_SetFromErrno(PyExc_OSError);
2109 goto error;
2110 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 result = PyList_New(gotevents);
2113 if (result == NULL) {
2114 goto error;
2115 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 for (i = 0; i < gotevents; i++) {
2118 kqueue_event_Object *ch;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
2121 if (ch == NULL) {
2122 goto error;
2123 }
2124 ch->e = evl[i];
2125 PyList_SET_ITEM(result, i, (PyObject *)ch);
2126 }
2127 PyMem_Free(chl);
2128 PyMem_Free(evl);
2129 return result;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002130
2131 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 PyMem_Free(chl);
2133 PyMem_Free(evl);
2134 Py_XDECREF(result);
2135 Py_XDECREF(it);
2136 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002137}
2138
2139PyDoc_STRVAR(kqueue_queue_control_doc,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00002140"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002141\n\
2142Calls the kernel kevent function.\n\
2143- changelist must be a list of kevent objects describing the changes\n\
2144 to be made to the kernel's watch list or None.\n\
2145- max_events lets you specify the maximum number of events that the\n\
2146 kernel will return.\n\
2147- timeout is the maximum time to wait in seconds, or else None,\n\
2148 to wait forever. timeout accepts floats for smaller timeouts, too.");
2149
2150
2151static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
2153 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
2154 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
2155 kqueue_queue_close_doc},
2156 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
2157 kqueue_queue_fileno_doc},
2158 {"control", (PyCFunction)kqueue_queue_control,
2159 METH_VARARGS , kqueue_queue_control_doc},
2160 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002161};
2162
2163static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 {"closed", (getter)kqueue_queue_get_closed, NULL,
2165 "True if the kqueue handler is closed"},
2166 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002167};
2168
2169PyDoc_STRVAR(kqueue_queue_doc,
2170"Kqueue syscall wrapper.\n\
2171\n\
2172For example, to start watching a socket for input:\n\
2173>>> kq = kqueue()\n\
2174>>> sock = socket()\n\
2175>>> sock.connect((host, port))\n\
2176>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
2177\n\
2178To wait one second for it to become writeable:\n\
2179>>> kq.control(None, 1, 1000)\n\
2180\n\
2181To stop listening:\n\
2182>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
2183
2184static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 PyVarObject_HEAD_INIT(NULL, 0)
2186 "select.kqueue", /* tp_name */
2187 sizeof(kqueue_queue_Object), /* tp_basicsize */
2188 0, /* tp_itemsize */
2189 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
2190 0, /* tp_print */
2191 0, /* tp_getattr */
2192 0, /* tp_setattr */
2193 0, /* tp_reserved */
2194 0, /* tp_repr */
2195 0, /* tp_as_number */
2196 0, /* tp_as_sequence */
2197 0, /* tp_as_mapping */
2198 0, /* tp_hash */
2199 0, /* tp_call */
2200 0, /* tp_str */
2201 0, /* tp_getattro */
2202 0, /* tp_setattro */
2203 0, /* tp_as_buffer */
2204 Py_TPFLAGS_DEFAULT, /* tp_flags */
2205 kqueue_queue_doc, /* tp_doc */
2206 0, /* tp_traverse */
2207 0, /* tp_clear */
2208 0, /* tp_richcompare */
2209 0, /* tp_weaklistoffset */
2210 0, /* tp_iter */
2211 0, /* tp_iternext */
2212 kqueue_queue_methods, /* tp_methods */
2213 0, /* tp_members */
2214 kqueue_queue_getsetlist, /* tp_getset */
2215 0, /* tp_base */
2216 0, /* tp_dict */
2217 0, /* tp_descr_get */
2218 0, /* tp_descr_set */
2219 0, /* tp_dictoffset */
2220 0, /* tp_init */
2221 0, /* tp_alloc */
2222 kqueue_queue_new, /* tp_new */
2223 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002224};
2225
2226#endif /* HAVE_KQUEUE */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002227
2228
2229
2230
2231
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002232/* ************************************************************************ */
2233
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002234PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002235"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
2236\n\
2237Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00002238The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002239rlist -- wait until ready for reading\n\
2240wlist -- wait until ready for writing\n\
2241xlist -- wait for an ``exceptional condition''\n\
2242If only one kind of condition is required, pass [] for the other lists.\n\
2243A file descriptor is either a socket or file object, or a small integer\n\
2244gotten from a fileno() method call on one of those.\n\
2245\n\
2246The optional 4th argument specifies a timeout in seconds; it may be\n\
2247a floating point number to specify fractions of seconds. If it is absent\n\
2248or None, the call will never time out.\n\
2249\n\
2250The return value is a tuple of three lists corresponding to the first three\n\
2251arguments; each contains the subset of the corresponding file descriptors\n\
2252that are ready.\n\
2253\n\
2254*** IMPORTANT NOTICE ***\n\
Christian Heimesaf01f662013-12-21 16:19:10 +01002255On Windows only sockets are supported; on Unix, all file\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00002256descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002257
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002258static PyMethodDef select_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natali986a56c2013-01-19 12:19:10 +01002260#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002262#endif /* HAVE_POLL */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002263#ifdef HAVE_SYS_DEVPOLL_H
2264 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2265#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00002267};
2268
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002269PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002270"This module supports asynchronous I/O on multiple file descriptors.\n\
2271\n\
2272*** IMPORTANT NOTICE ***\n\
Christian Heimesaf01f662013-12-21 16:19:10 +01002273On Windows only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00002274
Martin v. Löwis1a214512008-06-11 05:26:20 +00002275
2276static struct PyModuleDef selectmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 PyModuleDef_HEAD_INIT,
2278 "select",
2279 module_doc,
2280 -1,
2281 select_methods,
2282 NULL,
2283 NULL,
2284 NULL,
2285 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002286};
2287
Jesus Cead8b9ae62011-11-14 19:07:41 +01002288
2289
2290
Mark Hammond62b1ab12002-07-23 06:31:15 +00002291PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002292PyInit_select(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00002293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 PyObject *m;
2295 m = PyModule_Create(&selectmodule);
2296 if (m == NULL)
2297 return NULL;
Fred Drake4baedc12002-04-01 14:53:37 +00002298
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002299 Py_INCREF(PyExc_OSError);
2300 PyModule_AddObject(m, "error", PyExc_OSError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002301
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002302#ifdef PIPE_BUF
R. David Murraye16cda92010-10-15 23:12:57 +00002303#ifdef HAVE_BROKEN_PIPE_BUF
2304#undef PIPE_BUF
2305#define PIPE_BUF 512
2306#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002307 PyModule_AddIntMacro(m, PIPE_BUF);
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002308#endif
Gregory P. Smithb970b862009-07-04 02:28:47 +00002309
Charles-François Natali986a56c2013-01-19 12:19:10 +01002310#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002311#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 if (select_have_broken_poll()) {
2313 if (PyObject_DelAttrString(m, "poll") == -1) {
2314 PyErr_Clear();
2315 }
2316 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002317#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002319#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 if (PyType_Ready(&poll_Type) < 0)
2321 return NULL;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002322 PyModule_AddIntMacro(m, POLLIN);
2323 PyModule_AddIntMacro(m, POLLPRI);
2324 PyModule_AddIntMacro(m, POLLOUT);
2325 PyModule_AddIntMacro(m, POLLERR);
2326 PyModule_AddIntMacro(m, POLLHUP);
2327 PyModule_AddIntMacro(m, POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00002328
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002329#ifdef POLLRDNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002330 PyModule_AddIntMacro(m, POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002331#endif
2332#ifdef POLLRDBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002333 PyModule_AddIntMacro(m, POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002334#endif
2335#ifdef POLLWRNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002336 PyModule_AddIntMacro(m, POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002337#endif
2338#ifdef POLLWRBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002339 PyModule_AddIntMacro(m, POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002340#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002341#ifdef POLLMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002342 PyModule_AddIntMacro(m, POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002343#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002345#endif /* HAVE_POLL */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002346
Jesus Cead8b9ae62011-11-14 19:07:41 +01002347#ifdef HAVE_SYS_DEVPOLL_H
2348 if (PyType_Ready(&devpoll_Type) < 0)
2349 return NULL;
2350#endif
2351
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002352#ifdef HAVE_EPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
2354 if (PyType_Ready(&pyEpoll_Type) < 0)
2355 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 Py_INCREF(&pyEpoll_Type);
2358 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002359
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002360 PyModule_AddIntMacro(m, EPOLLIN);
2361 PyModule_AddIntMacro(m, EPOLLOUT);
2362 PyModule_AddIntMacro(m, EPOLLPRI);
2363 PyModule_AddIntMacro(m, EPOLLERR);
2364 PyModule_AddIntMacro(m, EPOLLHUP);
2365 PyModule_AddIntMacro(m, EPOLLET);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002366#ifdef EPOLLONESHOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 /* Kernel 2.6.2+ */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002368 PyModule_AddIntMacro(m, EPOLLONESHOT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002369#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002371 PyModule_AddIntMacro(m, EPOLLRDNORM);
2372 PyModule_AddIntMacro(m, EPOLLRDBAND);
2373 PyModule_AddIntMacro(m, EPOLLWRNORM);
2374 PyModule_AddIntMacro(m, EPOLLWRBAND);
2375 PyModule_AddIntMacro(m, EPOLLMSG);
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002376
Benjamin Peterson95c16622011-12-27 15:36:32 -06002377#ifdef EPOLL_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002378 PyModule_AddIntMacro(m, EPOLL_CLOEXEC);
Benjamin Peterson95c16622011-12-27 15:36:32 -06002379#endif
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002380#endif /* HAVE_EPOLL */
2381
2382#ifdef HAVE_KQUEUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 kqueue_event_Type.tp_new = PyType_GenericNew;
2384 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
2385 if(PyType_Ready(&kqueue_event_Type) < 0)
2386 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 Py_INCREF(&kqueue_event_Type);
2389 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
2392 if(PyType_Ready(&kqueue_queue_Type) < 0)
2393 return NULL;
2394 Py_INCREF(&kqueue_queue_Type);
2395 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
2396
2397 /* event filters */
2398 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
2399 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
2400 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
2401 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
2402 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002403#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002405#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
2407 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* event flags */
2410 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
2411 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
2412 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
2413 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
2414 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
2415 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
2418 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
2421 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 /* READ WRITE filter flag */
2424 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 /* VNODE filter flags */
2427 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
2428 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
2429 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
2430 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
2431 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
2432 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
2433 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 /* PROC filter flags */
2436 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
2437 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
2438 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
2439 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
2440 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
2443 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
2444 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
2445
2446 /* NETDEV filter flags */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002447#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
2449 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
2450 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002451#endif
2452
2453#endif /* HAVE_KQUEUE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 return m;
Guido van Rossumed233a51992-06-23 09:07:03 +00002455}