blob: 603a2b670b083d763f9c1dfaf9ce50d37c268f9a [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
Andrew MacIntyre7bf68332002-03-03 02:59:16 +000050#if defined(PYOS_OS2) && !defined(PYCC_GCC)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000051#include <sys/time.h>
52#include <utils.h>
53#endif
54
Guido van Rossum6f489d91996-06-28 20:15:15 +000055#ifdef MS_WINDOWS
Christian Heimesc36625b2008-01-04 13:33:00 +000056# define WIN32_LEAN_AND_MEAN
Thomas Wouters0e3f5912006-08-11 14:57:12 +000057# include <winsock.h>
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000058#else
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059# define SOCKET int
Skip Montanaroeb33e5a2007-08-17 12:57:41 +000060# if defined(__VMS)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061# include <socket.h>
62# endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000063#endif
Guido van Rossumed233a51992-06-23 09:07:03 +000064
Barry Warsawc1cb3601996-12-12 22:16:21 +000065/* list of Python objects and their file descriptor */
66typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 PyObject *obj; /* owned reference */
68 SOCKET fd;
69 int sentinel; /* -1 == sentinel */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000070} pylist;
71
Barry Warsawc1cb3601996-12-12 22:16:21 +000072static void
Tim Peters4b046c22001-08-16 21:59:46 +000073reap_obj(pylist fd2obj[FD_SETSIZE + 1])
Barry Warsawc1cb3601996-12-12 22:16:21 +000074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 int i;
76 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
77 Py_XDECREF(fd2obj[i].obj);
78 fd2obj[i].obj = NULL;
79 }
80 fd2obj[0].sentinel = -1;
Barry Warsawc1cb3601996-12-12 22:16:21 +000081}
82
83
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000084/* returns -1 and sets the Python exception if an error occurred, otherwise
85 returns a number >= 0
86*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000087static int
Brett Cannon62dba4c2003-09-10 19:37:42 +000088seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +000089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 int max = -1;
91 int index = 0;
Antoine Pitroue4ad37e2012-11-01 20:13:54 +010092 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 PyObject* fast_seq = NULL;
94 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
97 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000098
Benjamin Petersone0edb8b2010-06-27 23:49:45 +000099 fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 if (!fast_seq)
101 return -1;
102
Antoine Pitroue4ad37e2012-11-01 20:13:54 +0100103 for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 SOCKET v;
105
106 /* any intervening fileno() calls could decr this refcnt */
107 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
Jesus Cea62a5c322012-07-19 21:31:26 +0200108 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 Py_INCREF(o);
111 v = PyObject_AsFileDescriptor( o );
112 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000113
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000114#if defined(_MSC_VER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 max = 0; /* not used for Win32 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000116#else /* !_MSC_VER */
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200117 if (!_PyIsSelectable_fd(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 PyErr_SetString(PyExc_ValueError,
119 "filedescriptor out of range in select()");
120 goto finally;
121 }
122 if (v > max)
123 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000124#endif /* _MSC_VER */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 /* add object and its file descriptor to the list */
128 if (index >= FD_SETSIZE) {
129 PyErr_SetString(PyExc_ValueError,
130 "too many file descriptors in select()");
131 goto finally;
132 }
133 fd2obj[index].obj = o;
134 fd2obj[index].fd = v;
135 fd2obj[index].sentinel = 0;
136 fd2obj[++index].sentinel = -1;
137 }
138 Py_DECREF(fast_seq);
139 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000140
141 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 Py_XDECREF(o);
143 Py_DECREF(fast_seq);
144 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000145}
146
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000147/* returns NULL and sets the Python exception if an error occurred */
148static PyObject *
Tim Peters4b046c22001-08-16 21:59:46 +0000149set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 int i, j, count=0;
152 PyObject *list, *o;
153 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
156 if (FD_ISSET(fd2obj[j].fd, set))
157 count++;
158 }
159 list = PyList_New(count);
160 if (!list)
161 return NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 i = 0;
164 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
165 fd = fd2obj[j].fd;
166 if (FD_ISSET(fd, set)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 o = fd2obj[j].obj;
168 fd2obj[j].obj = NULL;
169 /* transfer ownership */
170 if (PyList_SetItem(list, i, o) < 0)
171 goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 i++;
174 }
175 }
176 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000177 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 Py_DECREF(list);
179 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000180}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000181
Barry Warsawb44740f2001-08-16 16:52:59 +0000182#undef SELECT_USES_HEAP
183#if FD_SETSIZE > 1024
184#define SELECT_USES_HEAP
185#endif /* FD_SETSIZE > 1024 */
186
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000187static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000188select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000189{
Barry Warsawb44740f2001-08-16 16:52:59 +0000190#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 pylist *rfd2obj, *wfd2obj, *efd2obj;
Barry Warsawb44740f2001-08-16 16:52:59 +0000192#else /* !SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 /* XXX: All this should probably be implemented as follows:
194 * - find the highest descriptor we're interested in
195 * - add one
196 * - that's the size
197 * See: Stevens, APitUE, $12.5.1
198 */
199 pylist rfd2obj[FD_SETSIZE + 1];
200 pylist wfd2obj[FD_SETSIZE + 1];
201 pylist efd2obj[FD_SETSIZE + 1];
Barry Warsawb44740f2001-08-16 16:52:59 +0000202#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 PyObject *ifdlist, *ofdlist, *efdlist;
204 PyObject *ret = NULL;
205 PyObject *tout = Py_None;
206 fd_set ifdset, ofdset, efdset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 struct timeval tv, *tvp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 int imax, omax, emax, max;
209 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 /* convert arguments */
212 if (!PyArg_UnpackTuple(args, "select", 3, 4,
213 &ifdlist, &ofdlist, &efdlist, &tout))
214 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 if (tout == Py_None)
217 tvp = (struct timeval *)0;
218 else if (!PyNumber_Check(tout)) {
219 PyErr_SetString(PyExc_TypeError,
220 "timeout must be a float or None");
221 return NULL;
222 }
223 else {
Victor Stinnerb2a37732012-03-14 00:20:51 +0100224#ifdef MS_WINDOWS
225 time_t sec;
226 if (_PyTime_ObjectToTimeval(tout, &sec, &tv.tv_usec) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 return NULL;
Victor Stinnerb2a37732012-03-14 00:20:51 +0100228 assert(sizeof(tv.tv_sec) == sizeof(long));
229#if SIZEOF_TIME_T > SIZEOF_LONG
230 if (sec > LONG_MAX) {
231 PyErr_SetString(PyExc_OverflowError,
232 "timeout is too large");
233 return NULL;
234 }
235#endif
236 tv.tv_sec = (long)sec;
237#else
Brett Cannon8798ad32012-04-07 14:59:29 -0400238 /* 64-bit OS X has struct timeval.tv_usec as an int (and thus still 4
239 bytes as required), but no longer defined by a long. */
Benjamin Peterson6f3e5e42012-09-11 12:05:05 -0400240 long tv_usec;
Brett Cannon8798ad32012-04-07 14:59:29 -0400241 if (_PyTime_ObjectToTimeval(tout, &tv.tv_sec, &tv_usec) == -1)
Victor Stinnerb2a37732012-03-14 00:20:51 +0100242 return NULL;
Brett Cannon8798ad32012-04-07 14:59:29 -0400243 tv.tv_usec = tv_usec;
Victor Stinnerb2a37732012-03-14 00:20:51 +0100244#endif
Victor Stinner5d272cc2012-03-13 13:35:55 +0100245 if (tv.tv_sec < 0) {
246 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 return NULL;
248 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 tvp = &tv;
250 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000251
Guido van Rossumed233a51992-06-23 09:07:03 +0000252
Barry Warsawb44740f2001-08-16 16:52:59 +0000253#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 /* Allocate memory for the lists */
255 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
256 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
257 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
258 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
259 if (rfd2obj) PyMem_DEL(rfd2obj);
260 if (wfd2obj) PyMem_DEL(wfd2obj);
261 if (efd2obj) PyMem_DEL(efd2obj);
262 return PyErr_NoMemory();
263 }
Barry Warsawb44740f2001-08-16 16:52:59 +0000264#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 /* Convert sequences to fd_sets, and get maximum fd number
266 * propagates the Python exception set in seq2set()
267 */
268 rfd2obj[0].sentinel = -1;
269 wfd2obj[0].sentinel = -1;
270 efd2obj[0].sentinel = -1;
271 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
272 goto finally;
273 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
274 goto finally;
275 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
276 goto finally;
277 max = imax;
278 if (omax > max) max = omax;
279 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 Py_BEGIN_ALLOW_THREADS
282 n = select(max, &ifdset, &ofdset, &efdset, tvp);
283 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000284
Thomas Heller106f4c72002-09-24 16:51:00 +0000285#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 if (n == SOCKET_ERROR) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200287 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000289#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 if (n < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200291 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000293#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 else {
295 /* any of these three calls can raise an exception. it's more
296 convenient to test for this after all three calls... but
297 is that acceptable?
298 */
299 ifdlist = set2list(&ifdset, rfd2obj);
300 ofdlist = set2list(&ofdset, wfd2obj);
301 efdlist = set2list(&efdset, efd2obj);
302 if (PyErr_Occurred())
303 ret = NULL;
304 else
305 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 Py_DECREF(ifdlist);
308 Py_DECREF(ofdlist);
309 Py_DECREF(efdlist);
310 }
311
Barry Warsawc1cb3601996-12-12 22:16:21 +0000312 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 reap_obj(rfd2obj);
314 reap_obj(wfd2obj);
315 reap_obj(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000316#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 PyMem_DEL(rfd2obj);
318 PyMem_DEL(wfd2obj);
319 PyMem_DEL(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000320#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000322}
323
Nicholas Bastine62c5c82004-03-21 23:45:42 +0000324#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325/*
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000326 * poll() support
327 */
328
329typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 PyObject_HEAD
331 PyObject *dict;
332 int ufd_uptodate;
333 int ufd_len;
334 struct pollfd *ufds;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300335 int poll_running;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000336} pollObject;
337
Jeremy Hylton938ace62002-07-17 16:30:39 +0000338static PyTypeObject poll_Type;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340/* Update the malloc'ed array of pollfds to match the dictionary
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000341 contained within a pollObject. Return 1 on success, 0 on an error.
342*/
343
344static int
345update_ufd_array(pollObject *self)
346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 Py_ssize_t i, pos;
348 PyObject *key, *value;
349 struct pollfd *old_ufds = self->ufds;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 self->ufd_len = PyDict_Size(self->dict);
352 PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
353 if (self->ufds == NULL) {
354 self->ufds = old_ufds;
355 PyErr_NoMemory();
356 return 0;
357 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 i = pos = 0;
360 while (PyDict_Next(self->dict, &pos, &key, &value)) {
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200361 assert(i < self->ufd_len);
362 /* Never overflow */
363 self->ufds[i].fd = (int)PyLong_AsLong(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 self->ufds[i].events = (short)PyLong_AsLong(value);
365 i++;
366 }
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200367 assert(i == self->ufd_len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 self->ufd_uptodate = 1;
369 return 1;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000370}
371
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000372PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000373"register(fd [, eventmask] ) -> None\n\n\
374Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000375fd -- either an integer, or an object with a fileno() method returning an\n\
376 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000377events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000378
379static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380poll_register(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 PyObject *o, *key, *value;
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200383 int fd;
384 short events = POLLIN | POLLPRI | POLLOUT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000386
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200387 if (!PyArg_ParseTuple(args, "O|h:register", &o, &events)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 return NULL;
389 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 fd = PyObject_AsFileDescriptor(o);
392 if (fd == -1) return NULL;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 /* Add entry to the internal dictionary: the key is the
395 file descriptor, and the value is the event mask. */
396 key = PyLong_FromLong(fd);
397 if (key == NULL)
398 return NULL;
399 value = PyLong_FromLong(events);
400 if (value == NULL) {
401 Py_DECREF(key);
402 return NULL;
403 }
404 err = PyDict_SetItem(self->dict, key, value);
405 Py_DECREF(key);
406 Py_DECREF(value);
407 if (err < 0)
408 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 self->ufd_uptodate = 0;
411
412 Py_INCREF(Py_None);
413 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000414}
415
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000416PyDoc_STRVAR(poll_modify_doc,
417"modify(fd, eventmask) -> None\n\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +0000418Modify an already registered file descriptor.\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000419fd -- either an integer, or an object with a fileno() method returning an\n\
420 int.\n\
421events -- an optional bitmask describing the type of events to check for");
422
423static PyObject *
424poll_modify(pollObject *self, PyObject *args)
425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 PyObject *o, *key, *value;
427 int fd, events;
428 int err;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (!PyArg_ParseTuple(args, "Oi:modify", &o, &events)) {
431 return NULL;
432 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 fd = PyObject_AsFileDescriptor(o);
435 if (fd == -1) return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 /* Modify registered fd */
438 key = PyLong_FromLong(fd);
439 if (key == NULL)
440 return NULL;
441 if (PyDict_GetItem(self->dict, key) == NULL) {
442 errno = ENOENT;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200443 PyErr_SetFromErrno(PyExc_OSError);
Jesus Cea62a5c322012-07-19 21:31:26 +0200444 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 return NULL;
446 }
447 value = PyLong_FromLong(events);
448 if (value == NULL) {
449 Py_DECREF(key);
450 return NULL;
451 }
452 err = PyDict_SetItem(self->dict, key, value);
453 Py_DECREF(key);
454 Py_DECREF(value);
455 if (err < 0)
456 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 self->ufd_uptodate = 0;
459
460 Py_INCREF(Py_None);
461 return Py_None;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000462}
463
464
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000465PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000466"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000468
469static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 PyObject *key;
473 int fd;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 fd = PyObject_AsFileDescriptor( o );
476 if (fd == -1)
477 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 /* Check whether the fd is already in the array */
480 key = PyLong_FromLong(fd);
481 if (key == NULL)
482 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 if (PyDict_DelItem(self->dict, key) == -1) {
485 Py_DECREF(key);
486 /* This will simply raise the KeyError set by PyDict_DelItem
487 if the file descriptor isn't registered. */
488 return NULL;
489 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 Py_DECREF(key);
492 self->ufd_uptodate = 0;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 Py_INCREF(Py_None);
495 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000496}
497
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000498PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000499"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
500Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000501any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000502
503static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504poll_poll(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PyObject *result_list = NULL, *tout = NULL;
507 int timeout = 0, poll_result, i, j;
508 PyObject *value = NULL, *num = NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
511 return NULL;
512 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 /* Check values for timeout */
515 if (tout == NULL || tout == Py_None)
516 timeout = -1;
517 else if (!PyNumber_Check(tout)) {
518 PyErr_SetString(PyExc_TypeError,
519 "timeout must be an integer or None");
520 return NULL;
521 }
522 else {
523 tout = PyNumber_Long(tout);
524 if (!tout)
525 return NULL;
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200526 timeout = _PyLong_AsInt(tout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 Py_DECREF(tout);
528 if (timeout == -1 && PyErr_Occurred())
529 return NULL;
530 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000531
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300532 /* Avoid concurrent poll() invocation, issue 8865 */
533 if (self->poll_running) {
534 PyErr_SetString(PyExc_RuntimeError,
535 "concurrent poll() invocation");
536 return NULL;
537 }
538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 /* Ensure the ufd array is up to date */
540 if (!self->ufd_uptodate)
541 if (update_ufd_array(self) == 0)
542 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000543
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300544 self->poll_running = 1;
545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 /* call poll() */
547 Py_BEGIN_ALLOW_THREADS
548 poll_result = poll(self->ufds, self->ufd_len, timeout);
549 Py_END_ALLOW_THREADS
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000550
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300551 self->poll_running = 0;
552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (poll_result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200554 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 return NULL;
556 }
557
558 /* build the result list */
559
560 result_list = PyList_New(poll_result);
561 if (!result_list)
562 return NULL;
563 else {
564 for (i = 0, j = 0; j < poll_result; j++) {
565 /* skip to the next fired descriptor */
566 while (!self->ufds[i].revents) {
567 i++;
568 }
569 /* if we hit a NULL return, set value to NULL
570 and break out of loop; code at end will
571 clean up result_list */
572 value = PyTuple_New(2);
573 if (value == NULL)
574 goto error;
575 num = PyLong_FromLong(self->ufds[i].fd);
576 if (num == NULL) {
577 Py_DECREF(value);
578 goto error;
579 }
580 PyTuple_SET_ITEM(value, 0, num);
581
582 /* The &0xffff is a workaround for AIX. 'revents'
583 is a 16-bit short, and IBM assigned POLLNVAL
584 to be 0x8000, so the conversion to int results
585 in a negative number. See SF bug #923315. */
586 num = PyLong_FromLong(self->ufds[i].revents & 0xffff);
587 if (num == NULL) {
588 Py_DECREF(value);
589 goto error;
590 }
591 PyTuple_SET_ITEM(value, 1, num);
592 if ((PyList_SetItem(result_list, j, value)) == -1) {
593 Py_DECREF(value);
594 goto error;
595 }
596 i++;
597 }
598 }
599 return result_list;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000600
601 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 Py_DECREF(result_list);
603 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000604}
605
606static PyMethodDef poll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 {"register", (PyCFunction)poll_register,
608 METH_VARARGS, poll_register_doc},
609 {"modify", (PyCFunction)poll_modify,
610 METH_VARARGS, poll_modify_doc},
611 {"unregister", (PyCFunction)poll_unregister,
612 METH_O, poll_unregister_doc},
613 {"poll", (PyCFunction)poll_poll,
614 METH_VARARGS, poll_poll_doc},
615 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000616};
617
618static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000619newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 pollObject *self;
622 self = PyObject_New(pollObject, &poll_Type);
623 if (self == NULL)
624 return NULL;
625 /* ufd_uptodate is a Boolean, denoting whether the
626 array pointed to by ufds matches the contents of the dictionary. */
627 self->ufd_uptodate = 0;
628 self->ufds = NULL;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300629 self->poll_running = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 self->dict = PyDict_New();
631 if (self->dict == NULL) {
632 Py_DECREF(self);
633 return NULL;
634 }
635 return self;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000636}
637
638static void
639poll_dealloc(pollObject *self)
640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 if (self->ufds != NULL)
642 PyMem_DEL(self->ufds);
643 Py_XDECREF(self->dict);
644 PyObject_Del(self);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000645}
646
Tim Peters0c322792002-07-17 16:49:03 +0000647static PyTypeObject poll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 /* The ob_type field must be initialized in the module init function
649 * to be portable to Windows without using C++. */
650 PyVarObject_HEAD_INIT(NULL, 0)
651 "select.poll", /*tp_name*/
652 sizeof(pollObject), /*tp_basicsize*/
653 0, /*tp_itemsize*/
654 /* methods */
655 (destructor)poll_dealloc, /*tp_dealloc*/
656 0, /*tp_print*/
657 0, /*tp_getattr*/
658 0, /*tp_setattr*/
659 0, /*tp_reserved*/
660 0, /*tp_repr*/
661 0, /*tp_as_number*/
662 0, /*tp_as_sequence*/
663 0, /*tp_as_mapping*/
664 0, /*tp_hash*/
665 0, /*tp_call*/
666 0, /*tp_str*/
667 0, /*tp_getattro*/
668 0, /*tp_setattro*/
669 0, /*tp_as_buffer*/
670 Py_TPFLAGS_DEFAULT, /*tp_flags*/
671 0, /*tp_doc*/
672 0, /*tp_traverse*/
673 0, /*tp_clear*/
674 0, /*tp_richcompare*/
675 0, /*tp_weaklistoffset*/
676 0, /*tp_iter*/
677 0, /*tp_iternext*/
678 poll_methods, /*tp_methods*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000679};
680
Jesus Cead8b9ae62011-11-14 19:07:41 +0100681#ifdef HAVE_SYS_DEVPOLL_H
682typedef struct {
683 PyObject_HEAD
684 int fd_devpoll;
685 int max_n_fds;
686 int n_fds;
687 struct pollfd *fds;
688} devpollObject;
689
690static PyTypeObject devpoll_Type;
691
692static int devpoll_flush(devpollObject *self)
693{
694 int size, n;
695
696 if (!self->n_fds) return 0;
697
698 size = sizeof(struct pollfd)*self->n_fds;
699 self->n_fds = 0;
700
701 Py_BEGIN_ALLOW_THREADS
702 n = write(self->fd_devpoll, self->fds, size);
703 Py_END_ALLOW_THREADS
704
705 if (n == -1 ) {
706 PyErr_SetFromErrno(PyExc_IOError);
707 return -1;
708 }
709 if (n < size) {
710 /*
711 ** Data writed to /dev/poll is a binary data structure. It is not
712 ** clear what to do if a partial write occurred. For now, raise
713 ** an exception and see if we actually found this problem in
714 ** the wild.
715 ** See http://bugs.python.org/issue6397.
716 */
717 PyErr_Format(PyExc_IOError, "failed to write all pollfds. "
718 "Please, report at http://bugs.python.org/. "
719 "Data to report: Size tried: %d, actual size written: %d.",
720 size, n);
721 return -1;
722 }
723 return 0;
724}
725
726static PyObject *
727internal_devpoll_register(devpollObject *self, PyObject *args, int remove)
728{
729 PyObject *o;
730 int fd, events = POLLIN | POLLPRI | POLLOUT;
731
732 if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
733 return NULL;
734 }
735
736 fd = PyObject_AsFileDescriptor(o);
737 if (fd == -1) return NULL;
738
739 if (remove) {
740 self->fds[self->n_fds].fd = fd;
741 self->fds[self->n_fds].events = POLLREMOVE;
742
743 if (++self->n_fds == self->max_n_fds) {
744 if (devpoll_flush(self))
745 return NULL;
746 }
747 }
748
749 self->fds[self->n_fds].fd = fd;
750 self->fds[self->n_fds].events = events;
751
752 if (++self->n_fds == self->max_n_fds) {
753 if (devpoll_flush(self))
754 return NULL;
755 }
756
757 Py_RETURN_NONE;
758}
759
760PyDoc_STRVAR(devpoll_register_doc,
761"register(fd [, eventmask] ) -> None\n\n\
762Register a file descriptor with the polling object.\n\
763fd -- either an integer, or an object with a fileno() method returning an\n\
764 int.\n\
765events -- an optional bitmask describing the type of events to check for");
766
767static PyObject *
768devpoll_register(devpollObject *self, PyObject *args)
769{
770 return internal_devpoll_register(self, args, 0);
771}
772
773PyDoc_STRVAR(devpoll_modify_doc,
774"modify(fd[, eventmask]) -> None\n\n\
775Modify a possible already registered file descriptor.\n\
776fd -- either an integer, or an object with a fileno() method returning an\n\
777 int.\n\
778events -- an optional bitmask describing the type of events to check for");
779
780static PyObject *
781devpoll_modify(devpollObject *self, PyObject *args)
782{
783 return internal_devpoll_register(self, args, 1);
784}
785
786
787PyDoc_STRVAR(devpoll_unregister_doc,
788"unregister(fd) -> None\n\n\
789Remove a file descriptor being tracked by the polling object.");
790
791static PyObject *
792devpoll_unregister(devpollObject *self, PyObject *o)
793{
794 int fd;
795
796 fd = PyObject_AsFileDescriptor( o );
797 if (fd == -1)
798 return NULL;
799
800 self->fds[self->n_fds].fd = fd;
801 self->fds[self->n_fds].events = POLLREMOVE;
802
803 if (++self->n_fds == self->max_n_fds) {
804 if (devpoll_flush(self))
805 return NULL;
806 }
807
808 Py_RETURN_NONE;
809}
810
811PyDoc_STRVAR(devpoll_poll_doc,
812"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
813Polls the set of registered file descriptors, returning a list containing \n\
814any descriptors that have events or errors to report.");
815
816static PyObject *
817devpoll_poll(devpollObject *self, PyObject *args)
818{
819 struct dvpoll dvp;
820 PyObject *result_list = NULL, *tout = NULL;
821 int poll_result, i;
822 long timeout;
823 PyObject *value, *num1, *num2;
824
825 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
826 return NULL;
827 }
828
829 /* Check values for timeout */
830 if (tout == NULL || tout == Py_None)
831 timeout = -1;
832 else if (!PyNumber_Check(tout)) {
833 PyErr_SetString(PyExc_TypeError,
834 "timeout must be an integer or None");
835 return NULL;
836 }
837 else {
838 tout = PyNumber_Long(tout);
839 if (!tout)
840 return NULL;
841 timeout = PyLong_AsLong(tout);
842 Py_DECREF(tout);
843 if (timeout == -1 && PyErr_Occurred())
844 return NULL;
845 }
846
847 if ((timeout < -1) || (timeout > INT_MAX)) {
848 PyErr_SetString(PyExc_OverflowError,
849 "timeout is out of range");
850 return NULL;
851 }
852
853 if (devpoll_flush(self))
854 return NULL;
855
856 dvp.dp_fds = self->fds;
857 dvp.dp_nfds = self->max_n_fds;
858 dvp.dp_timeout = timeout;
859
860 /* call devpoll() */
861 Py_BEGIN_ALLOW_THREADS
862 poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp);
863 Py_END_ALLOW_THREADS
864
865 if (poll_result < 0) {
866 PyErr_SetFromErrno(PyExc_IOError);
867 return NULL;
868 }
869
870 /* build the result list */
871
872 result_list = PyList_New(poll_result);
873 if (!result_list)
874 return NULL;
875 else {
876 for (i = 0; i < poll_result; i++) {
877 num1 = PyLong_FromLong(self->fds[i].fd);
878 num2 = PyLong_FromLong(self->fds[i].revents);
879 if ((num1 == NULL) || (num2 == NULL)) {
880 Py_XDECREF(num1);
881 Py_XDECREF(num2);
882 goto error;
883 }
884 value = PyTuple_Pack(2, num1, num2);
885 Py_DECREF(num1);
886 Py_DECREF(num2);
887 if (value == NULL)
888 goto error;
889 if ((PyList_SetItem(result_list, i, value)) == -1) {
890 Py_DECREF(value);
891 goto error;
892 }
893 }
894 }
895
896 return result_list;
897
898 error:
899 Py_DECREF(result_list);
900 return NULL;
901}
902
903static PyMethodDef devpoll_methods[] = {
904 {"register", (PyCFunction)devpoll_register,
905 METH_VARARGS, devpoll_register_doc},
906 {"modify", (PyCFunction)devpoll_modify,
907 METH_VARARGS, devpoll_modify_doc},
908 {"unregister", (PyCFunction)devpoll_unregister,
909 METH_O, devpoll_unregister_doc},
910 {"poll", (PyCFunction)devpoll_poll,
911 METH_VARARGS, devpoll_poll_doc},
912 {NULL, NULL} /* sentinel */
913};
914
915static devpollObject *
916newDevPollObject(void)
917{
918 devpollObject *self;
919 int fd_devpoll, limit_result;
920 struct pollfd *fds;
921 struct rlimit limit;
922
923 Py_BEGIN_ALLOW_THREADS
924 /*
925 ** If we try to process more that getrlimit()
926 ** fds, the kernel will give an error, so
927 ** we set the limit here. It is a dynamic
928 ** value, because we can change rlimit() anytime.
929 */
930 limit_result = getrlimit(RLIMIT_NOFILE, &limit);
931 if (limit_result != -1)
932 fd_devpoll = open("/dev/poll", O_RDWR);
933 Py_END_ALLOW_THREADS
934
935 if (limit_result == -1) {
936 PyErr_SetFromErrno(PyExc_OSError);
937 return NULL;
938 }
939 if (fd_devpoll == -1) {
940 PyErr_SetFromErrnoWithFilename(PyExc_IOError, "/dev/poll");
941 return NULL;
942 }
943
944 fds = PyMem_NEW(struct pollfd, limit.rlim_cur);
945 if (fds == NULL) {
946 close(fd_devpoll);
947 PyErr_NoMemory();
948 return NULL;
949 }
950
951 self = PyObject_New(devpollObject, &devpoll_Type);
952 if (self == NULL) {
953 close(fd_devpoll);
954 PyMem_DEL(fds);
955 return NULL;
956 }
957 self->fd_devpoll = fd_devpoll;
958 self->max_n_fds = limit.rlim_cur;
959 self->n_fds = 0;
960 self->fds = fds;
961
962 return self;
963}
964
965static void
966devpoll_dealloc(devpollObject *self)
967{
968 Py_BEGIN_ALLOW_THREADS
969 close(self->fd_devpoll);
970 Py_END_ALLOW_THREADS
971
972 PyMem_DEL(self->fds);
973
974 PyObject_Del(self);
975}
976
977static PyTypeObject devpoll_Type = {
978 /* The ob_type field must be initialized in the module init function
979 * to be portable to Windows without using C++. */
980 PyVarObject_HEAD_INIT(NULL, 0)
981 "select.devpoll", /*tp_name*/
982 sizeof(devpollObject), /*tp_basicsize*/
983 0, /*tp_itemsize*/
984 /* methods */
985 (destructor)devpoll_dealloc, /*tp_dealloc*/
986 0, /*tp_print*/
987 0, /*tp_getattr*/
988 0, /*tp_setattr*/
989 0, /*tp_reserved*/
990 0, /*tp_repr*/
991 0, /*tp_as_number*/
992 0, /*tp_as_sequence*/
993 0, /*tp_as_mapping*/
994 0, /*tp_hash*/
995 0, /*tp_call*/
996 0, /*tp_str*/
997 0, /*tp_getattro*/
998 0, /*tp_setattro*/
999 0, /*tp_as_buffer*/
1000 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1001 0, /*tp_doc*/
1002 0, /*tp_traverse*/
1003 0, /*tp_clear*/
1004 0, /*tp_richcompare*/
1005 0, /*tp_weaklistoffset*/
1006 0, /*tp_iter*/
1007 0, /*tp_iternext*/
1008 devpoll_methods, /*tp_methods*/
1009};
1010#endif /* HAVE_SYS_DEVPOLL_H */
1011
1012
1013
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001014PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001015"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001016unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001017
1018static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001019select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001022}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001023
Jesus Cead8b9ae62011-11-14 19:07:41 +01001024#ifdef HAVE_SYS_DEVPOLL_H
1025PyDoc_STRVAR(devpoll_doc,
1026"Returns a polling object, which supports registering and\n\
1027unregistering file descriptors, and then polling them for I/O events.");
1028
1029static PyObject *
1030select_devpoll(PyObject *self, PyObject *unused)
1031{
1032 return (PyObject *)newDevPollObject();
1033}
1034#endif
1035
1036
Thomas Wouters477c8d52006-05-27 19:21:47 +00001037#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038/*
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039 * On some systems poll() sets errno on invalid file descriptors. We test
1040 * for this at runtime because this bug may be fixed or introduced between
1041 * OS releases.
1042 */
1043static int select_have_broken_poll(void)
1044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 int poll_test;
1046 int filedes[2];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
Thomas Wouters477c8d52006-05-27 19:21:47 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 /* Create a file descriptor to make invalid */
1051 if (pipe(filedes) < 0) {
1052 return 1;
1053 }
1054 poll_struct.fd = filedes[0];
1055 close(filedes[0]);
1056 close(filedes[1]);
1057 poll_test = poll(&poll_struct, 1, 0);
1058 if (poll_test < 0) {
1059 return 1;
1060 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
1061 return 1;
1062 }
1063 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001064}
1065#endif /* __APPLE__ */
1066
1067#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001068
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001069#ifdef HAVE_EPOLL
1070/* **************************************************************************
1071 * epoll interface for Linux 2.6
1072 *
1073 * Written by Christian Heimes
1074 * Inspired by Twisted's _epoll.pyx and select.poll()
1075 */
1076
1077#ifdef HAVE_SYS_EPOLL_H
1078#include <sys/epoll.h>
1079#endif
1080
1081typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 PyObject_HEAD
1083 SOCKET epfd; /* epoll control file descriptor */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001084} pyEpoll_Object;
1085
1086static PyTypeObject pyEpoll_Type;
1087#define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))
1088
1089static PyObject *
1090pyepoll_err_closed(void)
1091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll fd");
1093 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001094}
1095
1096static int
1097pyepoll_internal_close(pyEpoll_Object *self)
1098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 int save_errno = 0;
1100 if (self->epfd >= 0) {
1101 int epfd = self->epfd;
1102 self->epfd = -1;
1103 Py_BEGIN_ALLOW_THREADS
1104 if (close(epfd) < 0)
1105 save_errno = errno;
1106 Py_END_ALLOW_THREADS
1107 }
1108 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001109}
1110
1111static PyObject *
Benjamin Peterson95c16622011-12-27 15:36:32 -06001112newPyEpoll_Object(PyTypeObject *type, int sizehint, int flags, SOCKET fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 pyEpoll_Object *self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 assert(type != NULL && type->tp_alloc != NULL);
1117 self = (pyEpoll_Object *) type->tp_alloc(type, 0);
1118 if (self == NULL)
1119 return NULL;
1120
1121 if (fd == -1) {
1122 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson95c16622011-12-27 15:36:32 -06001123#ifdef HAVE_EPOLL_CREATE1
Benjamin Peterson83251c12011-12-27 16:01:21 -06001124 if (flags)
1125 self->epfd = epoll_create1(flags);
1126 else
Benjamin Peterson95c16622011-12-27 15:36:32 -06001127#endif
Benjamin Peterson83251c12011-12-27 16:01:21 -06001128 self->epfd = epoll_create(sizehint);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 Py_END_ALLOW_THREADS
1130 }
1131 else {
1132 self->epfd = fd;
1133 }
1134 if (self->epfd < 0) {
1135 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001136 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 return NULL;
1138 }
1139 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001140}
1141
1142
1143static PyObject *
1144pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1145{
Benjamin Peterson83251c12011-12-27 16:01:21 -06001146 int flags = 0, sizehint = FD_SETSIZE - 1;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001147 static char *kwlist[] = {"sizehint", "flags", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001148
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001149 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
1150 &sizehint, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 return NULL;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001152 if (sizehint < 0) {
1153 PyErr_SetString(PyExc_ValueError, "negative sizehint");
1154 return NULL;
1155 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001156
Benjamin Peterson95c16622011-12-27 15:36:32 -06001157 return newPyEpoll_Object(type, sizehint, flags, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001158}
1159
1160
1161static void
1162pyepoll_dealloc(pyEpoll_Object *self)
1163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 (void)pyepoll_internal_close(self);
1165 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001166}
1167
1168static PyObject*
1169pyepoll_close(pyEpoll_Object *self)
1170{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 errno = pyepoll_internal_close(self);
1172 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001173 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 return NULL;
1175 }
1176 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001177}
1178
1179PyDoc_STRVAR(pyepoll_close_doc,
1180"close() -> None\n\
1181\n\
1182Close the epoll control file descriptor. Further operations on the epoll\n\
1183object will raise an exception.");
1184
1185static PyObject*
1186pyepoll_get_closed(pyEpoll_Object *self)
1187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 if (self->epfd < 0)
1189 Py_RETURN_TRUE;
1190 else
1191 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001192}
1193
1194static PyObject*
1195pyepoll_fileno(pyEpoll_Object *self)
1196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 if (self->epfd < 0)
1198 return pyepoll_err_closed();
1199 return PyLong_FromLong(self->epfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001200}
1201
1202PyDoc_STRVAR(pyepoll_fileno_doc,
1203"fileno() -> int\n\
1204\n\
1205Return the epoll control file descriptor.");
1206
1207static PyObject*
1208pyepoll_fromfd(PyObject *cls, PyObject *args)
1209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1213 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001214
Benjamin Peterson95c16622011-12-27 15:36:32 -06001215 return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001216}
1217
1218PyDoc_STRVAR(pyepoll_fromfd_doc,
1219"fromfd(fd) -> epoll\n\
1220\n\
1221Create an epoll object from a given control fd.");
1222
1223static PyObject *
1224pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
1225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 struct epoll_event ev;
1227 int result;
1228 int fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 if (epfd < 0)
1231 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 fd = PyObject_AsFileDescriptor(pfd);
1234 if (fd == -1) {
1235 return NULL;
1236 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 switch(op) {
1239 case EPOLL_CTL_ADD:
1240 case EPOLL_CTL_MOD:
1241 ev.events = events;
1242 ev.data.fd = fd;
1243 Py_BEGIN_ALLOW_THREADS
1244 result = epoll_ctl(epfd, op, fd, &ev);
1245 Py_END_ALLOW_THREADS
1246 break;
1247 case EPOLL_CTL_DEL:
1248 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
1249 * operation required a non-NULL pointer in event, even
1250 * though this argument is ignored. */
1251 Py_BEGIN_ALLOW_THREADS
1252 result = epoll_ctl(epfd, op, fd, &ev);
1253 if (errno == EBADF) {
1254 /* fd already closed */
1255 result = 0;
1256 errno = 0;
1257 }
1258 Py_END_ALLOW_THREADS
1259 break;
1260 default:
1261 result = -1;
1262 errno = EINVAL;
1263 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 if (result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001266 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 return NULL;
1268 }
1269 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001270}
1271
1272static PyObject *
1273pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 PyObject *pfd;
1276 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
1277 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
1280 &pfd, &events)) {
1281 return NULL;
1282 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001285}
1286
1287PyDoc_STRVAR(pyepoll_register_doc,
Georg Brandl222569d2010-08-02 20:47:56 +00001288"register(fd[, eventmask]) -> None\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001289\n\
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001290Registers a new fd or raises an OSError if the fd is already registered.\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00001291fd is the target file descriptor of the operation.\n\
1292events is a bit set composed of the various EPOLL constants; the default\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001293is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
1294\n\
1295The epoll interface supports all file descriptors that support poll.");
1296
1297static PyObject *
1298pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 PyObject *pfd;
1301 unsigned int events;
1302 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
1305 &pfd, &events)) {
1306 return NULL;
1307 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001310}
1311
1312PyDoc_STRVAR(pyepoll_modify_doc,
1313"modify(fd, eventmask) -> None\n\
1314\n\
1315fd is the target file descriptor of the operation\n\
1316events is a bit set composed of the various EPOLL constants");
1317
1318static PyObject *
1319pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 PyObject *pfd;
1322 static char *kwlist[] = {"fd", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
1325 &pfd)) {
1326 return NULL;
1327 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001330}
1331
1332PyDoc_STRVAR(pyepoll_unregister_doc,
1333"unregister(fd) -> None\n\
1334\n\
1335fd is the target file descriptor of the operation.");
1336
1337static PyObject *
1338pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 double dtimeout = -1.;
1341 int timeout;
1342 int maxevents = -1;
1343 int nfds, i;
1344 PyObject *elist = NULL, *etuple = NULL;
1345 struct epoll_event *evs = NULL;
1346 static char *kwlist[] = {"timeout", "maxevents", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 if (self->epfd < 0)
1349 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
1352 &dtimeout, &maxevents)) {
1353 return NULL;
1354 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 if (dtimeout < 0) {
1357 timeout = -1;
1358 }
1359 else if (dtimeout * 1000.0 > INT_MAX) {
1360 PyErr_SetString(PyExc_OverflowError,
1361 "timeout is too large");
1362 return NULL;
1363 }
1364 else {
1365 timeout = (int)(dtimeout * 1000.0);
1366 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 if (maxevents == -1) {
1369 maxevents = FD_SETSIZE-1;
1370 }
1371 else if (maxevents < 1) {
1372 PyErr_Format(PyExc_ValueError,
1373 "maxevents must be greater than 0, got %d",
1374 maxevents);
1375 return NULL;
1376 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 evs = PyMem_New(struct epoll_event, maxevents);
1379 if (evs == NULL) {
1380 Py_DECREF(self);
1381 PyErr_NoMemory();
1382 return NULL;
1383 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 Py_BEGIN_ALLOW_THREADS
1386 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
1387 Py_END_ALLOW_THREADS
1388 if (nfds < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001389 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 goto error;
1391 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 elist = PyList_New(nfds);
1394 if (elist == NULL) {
1395 goto error;
1396 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 for (i = 0; i < nfds; i++) {
1399 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
1400 if (etuple == NULL) {
1401 Py_CLEAR(elist);
1402 goto error;
1403 }
1404 PyList_SET_ITEM(elist, i, etuple);
1405 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001406
Christian Heimesf6cd9672008-03-26 13:45:42 +00001407 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 PyMem_Free(evs);
1409 return elist;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001410}
1411
1412PyDoc_STRVAR(pyepoll_poll_doc,
1413"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1414\n\
1415Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1416in seconds (as float). -1 makes poll wait indefinitely.\n\
1417Up to maxevents are returned to the caller.");
1418
1419static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 {"fromfd", (PyCFunction)pyepoll_fromfd,
1421 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1422 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1423 pyepoll_close_doc},
1424 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1425 pyepoll_fileno_doc},
1426 {"modify", (PyCFunction)pyepoll_modify,
1427 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1428 {"register", (PyCFunction)pyepoll_register,
1429 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1430 {"unregister", (PyCFunction)pyepoll_unregister,
1431 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1432 {"poll", (PyCFunction)pyepoll_poll,
1433 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
1434 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001435};
1436
1437static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 {"closed", (getter)pyepoll_get_closed, NULL,
1439 "True if the epoll handler is closed"},
1440 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001441};
1442
1443PyDoc_STRVAR(pyepoll_doc,
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001444"select.epoll(sizehint=-1, flags=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001445\n\
1446Returns an epolling object\n\
1447\n\
1448sizehint must be a positive integer or -1 for the default size. The\n\
1449sizehint is used to optimize internal data structures. It doesn't limit\n\
1450the maximum number of monitored events.");
1451
1452static PyTypeObject pyEpoll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 PyVarObject_HEAD_INIT(NULL, 0)
1454 "select.epoll", /* tp_name */
1455 sizeof(pyEpoll_Object), /* tp_basicsize */
1456 0, /* tp_itemsize */
1457 (destructor)pyepoll_dealloc, /* tp_dealloc */
1458 0, /* tp_print */
1459 0, /* tp_getattr */
1460 0, /* tp_setattr */
1461 0, /* tp_reserved */
1462 0, /* tp_repr */
1463 0, /* tp_as_number */
1464 0, /* tp_as_sequence */
1465 0, /* tp_as_mapping */
1466 0, /* tp_hash */
1467 0, /* tp_call */
1468 0, /* tp_str */
1469 PyObject_GenericGetAttr, /* tp_getattro */
1470 0, /* tp_setattro */
1471 0, /* tp_as_buffer */
1472 Py_TPFLAGS_DEFAULT, /* tp_flags */
1473 pyepoll_doc, /* tp_doc */
1474 0, /* tp_traverse */
1475 0, /* tp_clear */
1476 0, /* tp_richcompare */
1477 0, /* tp_weaklistoffset */
1478 0, /* tp_iter */
1479 0, /* tp_iternext */
1480 pyepoll_methods, /* tp_methods */
1481 0, /* tp_members */
1482 pyepoll_getsetlist, /* tp_getset */
1483 0, /* tp_base */
1484 0, /* tp_dict */
1485 0, /* tp_descr_get */
1486 0, /* tp_descr_set */
1487 0, /* tp_dictoffset */
1488 0, /* tp_init */
1489 0, /* tp_alloc */
1490 pyepoll_new, /* tp_new */
1491 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001492};
1493
1494#endif /* HAVE_EPOLL */
1495
1496#ifdef HAVE_KQUEUE
1497/* **************************************************************************
1498 * kqueue interface for BSD
1499 *
1500 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1501 * All rights reserved.
1502 *
1503 * Redistribution and use in source and binary forms, with or without
1504 * modification, are permitted provided that the following conditions
1505 * are met:
1506 * 1. Redistributions of source code must retain the above copyright
1507 * notice, this list of conditions and the following disclaimer.
1508 * 2. Redistributions in binary form must reproduce the above copyright
1509 * notice, this list of conditions and the following disclaimer in the
1510 * documentation and/or other materials provided with the distribution.
1511 *
1512 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1513 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1514 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1515 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1516 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1517 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1518 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1519 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1520 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1521 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1522 * SUCH DAMAGE.
1523 */
1524
1525#ifdef HAVE_SYS_EVENT_H
1526#include <sys/event.h>
1527#endif
1528
1529PyDoc_STRVAR(kqueue_event_doc,
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001530"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001531\n\
1532This object is the equivalent of the struct kevent for the C API.\n\
1533\n\
1534See the kqueue manpage for more detailed information about the meaning\n\
1535of the arguments.\n\
1536\n\
1537One minor note: while you might hope that udata could store a\n\
1538reference to a python object, it cannot, because it is impossible to\n\
1539keep a proper reference count of the object once it's passed into the\n\
1540kernel. Therefore, I have restricted it to only storing an integer. I\n\
1541recommend ignoring it and simply using the 'ident' field to key off\n\
1542of. You could also set up a dictionary on the python side to store a\n\
1543udata->object mapping.");
1544
1545typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 PyObject_HEAD
1547 struct kevent e;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001548} kqueue_event_Object;
1549
1550static PyTypeObject kqueue_event_Type;
1551
1552#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1553
1554typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 PyObject_HEAD
1556 SOCKET kqfd; /* kqueue control fd */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001557} kqueue_queue_Object;
1558
1559static PyTypeObject kqueue_queue_Type;
1560
1561#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1562
Antoine Pitroud83f1e62009-11-04 21:10:38 +00001563#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1564# error uintptr_t does not match void *!
1565#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1566# define T_UINTPTRT T_ULONGLONG
1567# define T_INTPTRT T_LONGLONG
1568# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1569# define UINTPTRT_FMT_UNIT "K"
1570# define INTPTRT_FMT_UNIT "L"
1571#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1572# define T_UINTPTRT T_ULONG
1573# define T_INTPTRT T_LONG
1574# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1575# define UINTPTRT_FMT_UNIT "k"
1576# define INTPTRT_FMT_UNIT "l"
1577#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1578# define T_UINTPTRT T_UINT
1579# define T_INTPTRT T_INT
1580# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1581# define UINTPTRT_FMT_UNIT "I"
1582# define INTPTRT_FMT_UNIT "i"
1583#else
1584# error uintptr_t does not match int, long, or long long!
1585#endif
1586
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001587/*
1588 * kevent is not standard and its members vary across BSDs.
1589 */
1590#if !defined(__OpenBSD__)
1591# define IDENT_TYPE T_UINTPTRT
1592# define IDENT_CAST Py_intptr_t
1593# define DATA_TYPE T_INTPTRT
1594# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
1595# define IDENT_AsType PyLong_AsUintptr_t
1596#else
1597# define IDENT_TYPE T_UINT
1598# define IDENT_CAST int
1599# define DATA_TYPE T_INT
1600# define DATA_FMT_UNIT "i"
1601# define IDENT_AsType PyLong_AsUnsignedLong
1602#endif
1603
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001604/* Unfortunately, we can't store python objects in udata, because
1605 * kevents in the kernel can be removed without warning, which would
1606 * forever lose the refcount on the object stored with it.
1607 */
1608
1609#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1610static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001611 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 {"filter", T_SHORT, KQ_OFF(e.filter)},
1613 {"flags", T_USHORT, KQ_OFF(e.flags)},
1614 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001615 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1617 {NULL} /* Sentinel */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001618};
1619#undef KQ_OFF
1620
1621static PyObject *
Georg Brandlc0e22b72010-03-14 10:51:01 +00001622
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001623kqueue_event_repr(kqueue_event_Object *s)
1624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 char buf[1024];
1626 PyOS_snprintf(
1627 buf, sizeof(buf),
1628 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1629 "data=0x%zd udata=%p>",
1630 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1631 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1632 return PyUnicode_FromString(buf);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001633}
1634
1635static int
1636kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 PyObject *pfd;
1639 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1640 "data", "udata", NULL};
Christian Heimesf1fe1592013-08-25 14:57:00 +02001641 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1646 &pfd, &(self->e.filter), &(self->e.flags),
1647 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1648 return -1;
1649 }
1650
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001651 if (PyLong_Check(pfd)
1652#if IDENT_TYPE == T_UINT
1653 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
1654#endif
1655 ) {
1656 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 }
1658 else {
1659 self->e.ident = PyObject_AsFileDescriptor(pfd);
1660 }
1661 if (PyErr_Occurred()) {
1662 return -1;
1663 }
1664 return 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001665}
1666
1667static PyObject *
1668kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 int op)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 Py_intptr_t result = 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 if (!kqueue_event_Check(o)) {
1674 if (op == Py_EQ || op == Py_NE) {
1675 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1676 Py_INCREF(res);
1677 return res;
1678 }
1679 PyErr_Format(PyExc_TypeError,
1680 "can't compare %.200s to %.200s",
1681 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1682 return NULL;
1683 }
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001684 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 ((result = s->e.filter - o->e.filter) == 0) &&
1686 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001687 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 ((result = s->e.data - o->e.data) == 0) &&
1689 ((result = s->e.udata - o->e.udata) == 0)
1690 ) {
1691 result = 0;
1692 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 switch (op) {
1695 case Py_EQ:
1696 result = (result == 0);
1697 break;
1698 case Py_NE:
1699 result = (result != 0);
1700 break;
1701 case Py_LE:
1702 result = (result <= 0);
1703 break;
1704 case Py_GE:
1705 result = (result >= 0);
1706 break;
1707 case Py_LT:
1708 result = (result < 0);
1709 break;
1710 case Py_GT:
1711 result = (result > 0);
1712 break;
1713 }
1714 return PyBool_FromLong((long)result);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001715}
1716
1717static PyTypeObject kqueue_event_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 PyVarObject_HEAD_INIT(NULL, 0)
1719 "select.kevent", /* tp_name */
1720 sizeof(kqueue_event_Object), /* tp_basicsize */
1721 0, /* tp_itemsize */
1722 0, /* tp_dealloc */
1723 0, /* tp_print */
1724 0, /* tp_getattr */
1725 0, /* tp_setattr */
1726 0, /* tp_reserved */
1727 (reprfunc)kqueue_event_repr, /* tp_repr */
1728 0, /* tp_as_number */
1729 0, /* tp_as_sequence */
1730 0, /* tp_as_mapping */
1731 0, /* tp_hash */
1732 0, /* tp_call */
1733 0, /* tp_str */
1734 0, /* tp_getattro */
1735 0, /* tp_setattro */
1736 0, /* tp_as_buffer */
1737 Py_TPFLAGS_DEFAULT, /* tp_flags */
1738 kqueue_event_doc, /* tp_doc */
1739 0, /* tp_traverse */
1740 0, /* tp_clear */
1741 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1742 0, /* tp_weaklistoffset */
1743 0, /* tp_iter */
1744 0, /* tp_iternext */
1745 0, /* tp_methods */
1746 kqueue_event_members, /* tp_members */
1747 0, /* tp_getset */
1748 0, /* tp_base */
1749 0, /* tp_dict */
1750 0, /* tp_descr_get */
1751 0, /* tp_descr_set */
1752 0, /* tp_dictoffset */
1753 (initproc)kqueue_event_init, /* tp_init */
1754 0, /* tp_alloc */
1755 0, /* tp_new */
1756 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001757};
1758
1759static PyObject *
1760kqueue_queue_err_closed(void)
1761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue fd");
1763 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001764}
1765
1766static int
1767kqueue_queue_internal_close(kqueue_queue_Object *self)
1768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 int save_errno = 0;
1770 if (self->kqfd >= 0) {
1771 int kqfd = self->kqfd;
1772 self->kqfd = -1;
1773 Py_BEGIN_ALLOW_THREADS
1774 if (close(kqfd) < 0)
1775 save_errno = errno;
1776 Py_END_ALLOW_THREADS
1777 }
1778 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001779}
1780
1781static PyObject *
1782newKqueue_Object(PyTypeObject *type, SOCKET fd)
1783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 kqueue_queue_Object *self;
1785 assert(type != NULL && type->tp_alloc != NULL);
1786 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1787 if (self == NULL) {
1788 return NULL;
1789 }
1790
1791 if (fd == -1) {
1792 Py_BEGIN_ALLOW_THREADS
1793 self->kqfd = kqueue();
1794 Py_END_ALLOW_THREADS
1795 }
1796 else {
1797 self->kqfd = fd;
1798 }
1799 if (self->kqfd < 0) {
1800 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001801 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 return NULL;
1803 }
1804 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001805}
1806
1807static PyObject *
1808kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1809{
1810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 if ((args != NULL && PyObject_Size(args)) ||
1812 (kwds != NULL && PyObject_Size(kwds))) {
1813 PyErr_SetString(PyExc_ValueError,
1814 "select.kqueue doesn't accept arguments");
1815 return NULL;
1816 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 return newKqueue_Object(type, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001819}
1820
1821static void
1822kqueue_queue_dealloc(kqueue_queue_Object *self)
1823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 kqueue_queue_internal_close(self);
1825 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001826}
1827
1828static PyObject*
1829kqueue_queue_close(kqueue_queue_Object *self)
1830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 errno = kqueue_queue_internal_close(self);
1832 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001833 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 return NULL;
1835 }
1836 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001837}
1838
1839PyDoc_STRVAR(kqueue_queue_close_doc,
1840"close() -> None\n\
1841\n\
1842Close the kqueue control file descriptor. Further operations on the kqueue\n\
1843object will raise an exception.");
1844
1845static PyObject*
1846kqueue_queue_get_closed(kqueue_queue_Object *self)
1847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 if (self->kqfd < 0)
1849 Py_RETURN_TRUE;
1850 else
1851 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001852}
1853
1854static PyObject*
1855kqueue_queue_fileno(kqueue_queue_Object *self)
1856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 if (self->kqfd < 0)
1858 return kqueue_queue_err_closed();
1859 return PyLong_FromLong(self->kqfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001860}
1861
1862PyDoc_STRVAR(kqueue_queue_fileno_doc,
1863"fileno() -> int\n\
1864\n\
1865Return the kqueue control file descriptor.");
1866
1867static PyObject*
1868kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1873 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001876}
1877
1878PyDoc_STRVAR(kqueue_queue_fromfd_doc,
1879"fromfd(fd) -> kqueue\n\
1880\n\
1881Create a kqueue object from a given control fd.");
1882
1883static PyObject *
1884kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
1885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 int nevents = 0;
1887 int gotevents = 0;
1888 int nchanges = 0;
1889 int i = 0;
1890 PyObject *otimeout = NULL;
1891 PyObject *ch = NULL;
1892 PyObject *it = NULL, *ei = NULL;
1893 PyObject *result = NULL;
1894 struct kevent *evl = NULL;
1895 struct kevent *chl = NULL;
Victor Stinnerd327f9d2012-03-13 15:29:08 +01001896 struct timespec timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 struct timespec *ptimeoutspec;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 if (self->kqfd < 0)
1900 return kqueue_queue_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
1903 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 if (nevents < 0) {
1906 PyErr_Format(PyExc_ValueError,
1907 "Length of eventlist must be 0 or positive, got %d",
1908 nevents);
1909 return NULL;
1910 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 if (otimeout == Py_None || otimeout == NULL) {
1913 ptimeoutspec = NULL;
1914 }
1915 else if (PyNumber_Check(otimeout)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +01001916 if (_PyTime_ObjectToTimespec(otimeout,
1917 &timeout.tv_sec, &timeout.tv_nsec) == -1)
1918 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001919
Victor Stinner5d272cc2012-03-13 13:35:55 +01001920 if (timeout.tv_sec < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 PyErr_SetString(PyExc_ValueError,
1922 "timeout must be positive or None");
1923 return NULL;
1924 }
Victor Stinnerd528b012012-03-13 16:25:35 +01001925 ptimeoutspec = &timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 }
1927 else {
1928 PyErr_Format(PyExc_TypeError,
1929 "timeout argument must be an number "
1930 "or None, got %.200s",
1931 Py_TYPE(otimeout)->tp_name);
1932 return NULL;
1933 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 if (ch != NULL && ch != Py_None) {
1936 it = PyObject_GetIter(ch);
1937 if (it == NULL) {
1938 PyErr_SetString(PyExc_TypeError,
1939 "changelist is not iterable");
1940 return NULL;
1941 }
1942 nchanges = PyObject_Size(ch);
1943 if (nchanges < 0) {
1944 goto error;
1945 }
Georg Brandlc0e22b72010-03-14 10:51:01 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 chl = PyMem_New(struct kevent, nchanges);
1948 if (chl == NULL) {
1949 PyErr_NoMemory();
1950 goto error;
1951 }
1952 i = 0;
1953 while ((ei = PyIter_Next(it)) != NULL) {
1954 if (!kqueue_event_Check(ei)) {
1955 Py_DECREF(ei);
1956 PyErr_SetString(PyExc_TypeError,
1957 "changelist must be an iterable of "
1958 "select.kevent objects");
1959 goto error;
1960 } else {
1961 chl[i++] = ((kqueue_event_Object *)ei)->e;
1962 }
1963 Py_DECREF(ei);
1964 }
1965 }
1966 Py_CLEAR(it);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 /* event list */
1969 if (nevents) {
1970 evl = PyMem_New(struct kevent, nevents);
1971 if (evl == NULL) {
1972 PyErr_NoMemory();
1973 goto error;
1974 }
1975 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 Py_BEGIN_ALLOW_THREADS
1978 gotevents = kevent(self->kqfd, chl, nchanges,
1979 evl, nevents, ptimeoutspec);
1980 Py_END_ALLOW_THREADS
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 if (gotevents == -1) {
1983 PyErr_SetFromErrno(PyExc_OSError);
1984 goto error;
1985 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 result = PyList_New(gotevents);
1988 if (result == NULL) {
1989 goto error;
1990 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 for (i = 0; i < gotevents; i++) {
1993 kqueue_event_Object *ch;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
1996 if (ch == NULL) {
1997 goto error;
1998 }
1999 ch->e = evl[i];
2000 PyList_SET_ITEM(result, i, (PyObject *)ch);
2001 }
2002 PyMem_Free(chl);
2003 PyMem_Free(evl);
2004 return result;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002005
2006 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 PyMem_Free(chl);
2008 PyMem_Free(evl);
2009 Py_XDECREF(result);
2010 Py_XDECREF(it);
2011 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002012}
2013
2014PyDoc_STRVAR(kqueue_queue_control_doc,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00002015"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002016\n\
2017Calls the kernel kevent function.\n\
2018- changelist must be a list of kevent objects describing the changes\n\
2019 to be made to the kernel's watch list or None.\n\
2020- max_events lets you specify the maximum number of events that the\n\
2021 kernel will return.\n\
2022- timeout is the maximum time to wait in seconds, or else None,\n\
2023 to wait forever. timeout accepts floats for smaller timeouts, too.");
2024
2025
2026static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
2028 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
2029 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
2030 kqueue_queue_close_doc},
2031 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
2032 kqueue_queue_fileno_doc},
2033 {"control", (PyCFunction)kqueue_queue_control,
2034 METH_VARARGS , kqueue_queue_control_doc},
2035 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002036};
2037
2038static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 {"closed", (getter)kqueue_queue_get_closed, NULL,
2040 "True if the kqueue handler is closed"},
2041 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002042};
2043
2044PyDoc_STRVAR(kqueue_queue_doc,
2045"Kqueue syscall wrapper.\n\
2046\n\
2047For example, to start watching a socket for input:\n\
2048>>> kq = kqueue()\n\
2049>>> sock = socket()\n\
2050>>> sock.connect((host, port))\n\
2051>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
2052\n\
2053To wait one second for it to become writeable:\n\
2054>>> kq.control(None, 1, 1000)\n\
2055\n\
2056To stop listening:\n\
2057>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
2058
2059static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 PyVarObject_HEAD_INIT(NULL, 0)
2061 "select.kqueue", /* tp_name */
2062 sizeof(kqueue_queue_Object), /* tp_basicsize */
2063 0, /* tp_itemsize */
2064 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
2065 0, /* tp_print */
2066 0, /* tp_getattr */
2067 0, /* tp_setattr */
2068 0, /* tp_reserved */
2069 0, /* tp_repr */
2070 0, /* tp_as_number */
2071 0, /* tp_as_sequence */
2072 0, /* tp_as_mapping */
2073 0, /* tp_hash */
2074 0, /* tp_call */
2075 0, /* tp_str */
2076 0, /* tp_getattro */
2077 0, /* tp_setattro */
2078 0, /* tp_as_buffer */
2079 Py_TPFLAGS_DEFAULT, /* tp_flags */
2080 kqueue_queue_doc, /* tp_doc */
2081 0, /* tp_traverse */
2082 0, /* tp_clear */
2083 0, /* tp_richcompare */
2084 0, /* tp_weaklistoffset */
2085 0, /* tp_iter */
2086 0, /* tp_iternext */
2087 kqueue_queue_methods, /* tp_methods */
2088 0, /* tp_members */
2089 kqueue_queue_getsetlist, /* tp_getset */
2090 0, /* tp_base */
2091 0, /* tp_dict */
2092 0, /* tp_descr_get */
2093 0, /* tp_descr_set */
2094 0, /* tp_dictoffset */
2095 0, /* tp_init */
2096 0, /* tp_alloc */
2097 kqueue_queue_new, /* tp_new */
2098 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002099};
2100
2101#endif /* HAVE_KQUEUE */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002102
2103
2104
2105
2106
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002107/* ************************************************************************ */
2108
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002109PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002110"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
2111\n\
2112Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00002113The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002114rlist -- wait until ready for reading\n\
2115wlist -- wait until ready for writing\n\
2116xlist -- wait for an ``exceptional condition''\n\
2117If only one kind of condition is required, pass [] for the other lists.\n\
2118A file descriptor is either a socket or file object, or a small integer\n\
2119gotten from a fileno() method call on one of those.\n\
2120\n\
2121The optional 4th argument specifies a timeout in seconds; it may be\n\
2122a floating point number to specify fractions of seconds. If it is absent\n\
2123or None, the call will never time out.\n\
2124\n\
2125The return value is a tuple of three lists corresponding to the first three\n\
2126arguments; each contains the subset of the corresponding file descriptors\n\
2127that are ready.\n\
2128\n\
2129*** IMPORTANT NOTICE ***\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002130On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00002131descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002132
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002133static PyMethodDef select_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natali986a56c2013-01-19 12:19:10 +01002135#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002137#endif /* HAVE_POLL */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002138#ifdef HAVE_SYS_DEVPOLL_H
2139 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2140#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00002142};
2143
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002144PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002145"This module supports asynchronous I/O on multiple file descriptors.\n\
2146\n\
2147*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002148On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00002149
Martin v. Löwis1a214512008-06-11 05:26:20 +00002150
2151static struct PyModuleDef selectmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 PyModuleDef_HEAD_INIT,
2153 "select",
2154 module_doc,
2155 -1,
2156 select_methods,
2157 NULL,
2158 NULL,
2159 NULL,
2160 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002161};
2162
Jesus Cead8b9ae62011-11-14 19:07:41 +01002163
2164
2165
Mark Hammond62b1ab12002-07-23 06:31:15 +00002166PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002167PyInit_select(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00002168{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 PyObject *m;
2170 m = PyModule_Create(&selectmodule);
2171 if (m == NULL)
2172 return NULL;
Fred Drake4baedc12002-04-01 14:53:37 +00002173
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002174 Py_INCREF(PyExc_OSError);
2175 PyModule_AddObject(m, "error", PyExc_OSError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002176
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002177#ifdef PIPE_BUF
R. David Murraye16cda92010-10-15 23:12:57 +00002178#ifdef HAVE_BROKEN_PIPE_BUF
2179#undef PIPE_BUF
2180#define PIPE_BUF 512
2181#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF);
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002183#endif
Gregory P. Smithb970b862009-07-04 02:28:47 +00002184
Charles-François Natali986a56c2013-01-19 12:19:10 +01002185#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002186#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 if (select_have_broken_poll()) {
2188 if (PyObject_DelAttrString(m, "poll") == -1) {
2189 PyErr_Clear();
2190 }
2191 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002192#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002194#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 if (PyType_Ready(&poll_Type) < 0)
2196 return NULL;
2197 PyModule_AddIntConstant(m, "POLLIN", POLLIN);
2198 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
2199 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
2200 PyModule_AddIntConstant(m, "POLLERR", POLLERR);
2201 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
2202 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00002203
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002204#ifdef POLLRDNORM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002206#endif
2207#ifdef POLLRDBAND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002209#endif
2210#ifdef POLLWRNORM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002212#endif
2213#ifdef POLLWRBAND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002215#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002216#ifdef POLLMSG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002218#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002220#endif /* HAVE_POLL */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002221
Jesus Cead8b9ae62011-11-14 19:07:41 +01002222#ifdef HAVE_SYS_DEVPOLL_H
2223 if (PyType_Ready(&devpoll_Type) < 0)
2224 return NULL;
2225#endif
2226
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002227#ifdef HAVE_EPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
2229 if (PyType_Ready(&pyEpoll_Type) < 0)
2230 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 Py_INCREF(&pyEpoll_Type);
2233 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);
2236 PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);
2237 PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);
2238 PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);
2239 PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);
2240 PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002241#ifdef EPOLLONESHOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 /* Kernel 2.6.2+ */
2243 PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002244#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
2246 PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);
2247 PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);
2248 PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);
2249 PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);
2250 PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002251
Benjamin Peterson95c16622011-12-27 15:36:32 -06002252#ifdef EPOLL_CLOEXEC
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002253 PyModule_AddIntConstant(m, "EPOLL_CLOEXEC", EPOLL_CLOEXEC);
Benjamin Peterson95c16622011-12-27 15:36:32 -06002254#endif
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002255#endif /* HAVE_EPOLL */
2256
2257#ifdef HAVE_KQUEUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 kqueue_event_Type.tp_new = PyType_GenericNew;
2259 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
2260 if(PyType_Ready(&kqueue_event_Type) < 0)
2261 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 Py_INCREF(&kqueue_event_Type);
2264 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
2267 if(PyType_Ready(&kqueue_queue_Type) < 0)
2268 return NULL;
2269 Py_INCREF(&kqueue_queue_Type);
2270 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
2271
2272 /* event filters */
2273 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
2274 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
2275 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
2276 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
2277 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002278#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002280#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
2282 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 /* event flags */
2285 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
2286 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
2287 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
2288 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
2289 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
2290 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
2293 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
2296 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 /* READ WRITE filter flag */
2299 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 /* VNODE filter flags */
2302 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
2303 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
2304 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
2305 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
2306 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
2307 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
2308 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 /* PROC filter flags */
2311 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
2312 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
2313 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
2314 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
2315 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
2318 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
2319 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
2320
2321 /* NETDEV filter flags */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002322#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
2324 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
2325 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002326#endif
2327
2328#endif /* HAVE_KQUEUE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 return m;
Guido van Rossumed233a51992-06-23 09:07:03 +00002330}