blob: ab2016a981912efad5bc507c4fa2c3e07735180e [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);
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200364 self->ufds[i].events = (short)(unsigned short)PyLong_AsLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 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
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200372static int
373ushort_converter(PyObject *obj, void *ptr)
374{
375 unsigned long uval;
376
377 uval = PyLong_AsUnsignedLong(obj);
378 if (uval == (unsigned long)-1 && PyErr_Occurred())
379 return 0;
380 if (uval > USHRT_MAX) {
381 PyErr_SetString(PyExc_OverflowError,
382 "Python int too large for C unsigned short");
383 return 0;
384 }
385
386 *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short);
387 return 1;
388}
389
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000390PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000391"register(fd [, eventmask] ) -> None\n\n\
392Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000393fd -- either an integer, or an object with a fileno() method returning an\n\
394 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000395events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000396
397static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398poll_register(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 PyObject *o, *key, *value;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200401 int fd;
402 unsigned short events = POLLIN | POLLPRI | POLLOUT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000404
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200405 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 fd = PyObject_AsFileDescriptor(o);
409 if (fd == -1) return NULL;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 /* Add entry to the internal dictionary: the key is the
412 file descriptor, and the value is the event mask. */
413 key = PyLong_FromLong(fd);
414 if (key == NULL)
415 return NULL;
416 value = PyLong_FromLong(events);
417 if (value == NULL) {
418 Py_DECREF(key);
419 return NULL;
420 }
421 err = PyDict_SetItem(self->dict, key, value);
422 Py_DECREF(key);
423 Py_DECREF(value);
424 if (err < 0)
425 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 self->ufd_uptodate = 0;
428
429 Py_INCREF(Py_None);
430 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000431}
432
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000433PyDoc_STRVAR(poll_modify_doc,
434"modify(fd, eventmask) -> None\n\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +0000435Modify an already registered file descriptor.\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000436fd -- either an integer, or an object with a fileno() method returning an\n\
437 int.\n\
438events -- an optional bitmask describing the type of events to check for");
439
440static PyObject *
441poll_modify(pollObject *self, PyObject *args)
442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 PyObject *o, *key, *value;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200444 int fd;
445 unsigned short events;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 int err;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000447
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200448 if (!PyArg_ParseTuple(args, "OO&:modify", &o, ushort_converter, &events))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 fd = PyObject_AsFileDescriptor(o);
452 if (fd == -1) return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 /* Modify registered fd */
455 key = PyLong_FromLong(fd);
456 if (key == NULL)
457 return NULL;
458 if (PyDict_GetItem(self->dict, key) == NULL) {
459 errno = ENOENT;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200460 PyErr_SetFromErrno(PyExc_OSError);
Jesus Cea62a5c322012-07-19 21:31:26 +0200461 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 return NULL;
463 }
464 value = PyLong_FromLong(events);
465 if (value == NULL) {
466 Py_DECREF(key);
467 return NULL;
468 }
469 err = PyDict_SetItem(self->dict, key, value);
470 Py_DECREF(key);
471 Py_DECREF(value);
472 if (err < 0)
473 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 self->ufd_uptodate = 0;
476
477 Py_INCREF(Py_None);
478 return Py_None;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000479}
480
481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000482PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000483"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000484Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000485
486static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyObject *key;
490 int fd;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 fd = PyObject_AsFileDescriptor( o );
493 if (fd == -1)
494 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 /* Check whether the fd is already in the array */
497 key = PyLong_FromLong(fd);
498 if (key == NULL)
499 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (PyDict_DelItem(self->dict, key) == -1) {
502 Py_DECREF(key);
503 /* This will simply raise the KeyError set by PyDict_DelItem
504 if the file descriptor isn't registered. */
505 return NULL;
506 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 Py_DECREF(key);
509 self->ufd_uptodate = 0;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 Py_INCREF(Py_None);
512 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000513}
514
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000515PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000516"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
517Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000518any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000519
520static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521poll_poll(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 PyObject *result_list = NULL, *tout = NULL;
524 int timeout = 0, poll_result, i, j;
525 PyObject *value = NULL, *num = NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
528 return NULL;
529 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 /* Check values for timeout */
532 if (tout == NULL || tout == Py_None)
533 timeout = -1;
534 else if (!PyNumber_Check(tout)) {
535 PyErr_SetString(PyExc_TypeError,
536 "timeout must be an integer or None");
537 return NULL;
538 }
539 else {
540 tout = PyNumber_Long(tout);
541 if (!tout)
542 return NULL;
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200543 timeout = _PyLong_AsInt(tout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 Py_DECREF(tout);
545 if (timeout == -1 && PyErr_Occurred())
546 return NULL;
547 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000548
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300549 /* Avoid concurrent poll() invocation, issue 8865 */
550 if (self->poll_running) {
551 PyErr_SetString(PyExc_RuntimeError,
552 "concurrent poll() invocation");
553 return NULL;
554 }
555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* Ensure the ufd array is up to date */
557 if (!self->ufd_uptodate)
558 if (update_ufd_array(self) == 0)
559 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000560
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300561 self->poll_running = 1;
562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* call poll() */
564 Py_BEGIN_ALLOW_THREADS
565 poll_result = poll(self->ufds, self->ufd_len, timeout);
566 Py_END_ALLOW_THREADS
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000567
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300568 self->poll_running = 0;
569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 if (poll_result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200571 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 return NULL;
573 }
574
575 /* build the result list */
576
577 result_list = PyList_New(poll_result);
578 if (!result_list)
579 return NULL;
580 else {
581 for (i = 0, j = 0; j < poll_result; j++) {
582 /* skip to the next fired descriptor */
583 while (!self->ufds[i].revents) {
584 i++;
585 }
586 /* if we hit a NULL return, set value to NULL
587 and break out of loop; code at end will
588 clean up result_list */
589 value = PyTuple_New(2);
590 if (value == NULL)
591 goto error;
592 num = PyLong_FromLong(self->ufds[i].fd);
593 if (num == NULL) {
594 Py_DECREF(value);
595 goto error;
596 }
597 PyTuple_SET_ITEM(value, 0, num);
598
599 /* The &0xffff is a workaround for AIX. 'revents'
600 is a 16-bit short, and IBM assigned POLLNVAL
601 to be 0x8000, so the conversion to int results
602 in a negative number. See SF bug #923315. */
603 num = PyLong_FromLong(self->ufds[i].revents & 0xffff);
604 if (num == NULL) {
605 Py_DECREF(value);
606 goto error;
607 }
608 PyTuple_SET_ITEM(value, 1, num);
609 if ((PyList_SetItem(result_list, j, value)) == -1) {
610 Py_DECREF(value);
611 goto error;
612 }
613 i++;
614 }
615 }
616 return result_list;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000617
618 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 Py_DECREF(result_list);
620 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000621}
622
623static PyMethodDef poll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 {"register", (PyCFunction)poll_register,
625 METH_VARARGS, poll_register_doc},
626 {"modify", (PyCFunction)poll_modify,
627 METH_VARARGS, poll_modify_doc},
628 {"unregister", (PyCFunction)poll_unregister,
629 METH_O, poll_unregister_doc},
630 {"poll", (PyCFunction)poll_poll,
631 METH_VARARGS, poll_poll_doc},
632 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000633};
634
635static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000636newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 pollObject *self;
639 self = PyObject_New(pollObject, &poll_Type);
640 if (self == NULL)
641 return NULL;
642 /* ufd_uptodate is a Boolean, denoting whether the
643 array pointed to by ufds matches the contents of the dictionary. */
644 self->ufd_uptodate = 0;
645 self->ufds = NULL;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300646 self->poll_running = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 self->dict = PyDict_New();
648 if (self->dict == NULL) {
649 Py_DECREF(self);
650 return NULL;
651 }
652 return self;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000653}
654
655static void
656poll_dealloc(pollObject *self)
657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 if (self->ufds != NULL)
659 PyMem_DEL(self->ufds);
660 Py_XDECREF(self->dict);
661 PyObject_Del(self);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000662}
663
Tim Peters0c322792002-07-17 16:49:03 +0000664static PyTypeObject poll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 /* The ob_type field must be initialized in the module init function
666 * to be portable to Windows without using C++. */
667 PyVarObject_HEAD_INIT(NULL, 0)
668 "select.poll", /*tp_name*/
669 sizeof(pollObject), /*tp_basicsize*/
670 0, /*tp_itemsize*/
671 /* methods */
672 (destructor)poll_dealloc, /*tp_dealloc*/
673 0, /*tp_print*/
674 0, /*tp_getattr*/
675 0, /*tp_setattr*/
676 0, /*tp_reserved*/
677 0, /*tp_repr*/
678 0, /*tp_as_number*/
679 0, /*tp_as_sequence*/
680 0, /*tp_as_mapping*/
681 0, /*tp_hash*/
682 0, /*tp_call*/
683 0, /*tp_str*/
684 0, /*tp_getattro*/
685 0, /*tp_setattro*/
686 0, /*tp_as_buffer*/
687 Py_TPFLAGS_DEFAULT, /*tp_flags*/
688 0, /*tp_doc*/
689 0, /*tp_traverse*/
690 0, /*tp_clear*/
691 0, /*tp_richcompare*/
692 0, /*tp_weaklistoffset*/
693 0, /*tp_iter*/
694 0, /*tp_iternext*/
695 poll_methods, /*tp_methods*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000696};
697
Jesus Cead8b9ae62011-11-14 19:07:41 +0100698#ifdef HAVE_SYS_DEVPOLL_H
699typedef struct {
700 PyObject_HEAD
701 int fd_devpoll;
702 int max_n_fds;
703 int n_fds;
704 struct pollfd *fds;
705} devpollObject;
706
707static PyTypeObject devpoll_Type;
708
709static int devpoll_flush(devpollObject *self)
710{
711 int size, n;
712
713 if (!self->n_fds) return 0;
714
715 size = sizeof(struct pollfd)*self->n_fds;
716 self->n_fds = 0;
717
718 Py_BEGIN_ALLOW_THREADS
719 n = write(self->fd_devpoll, self->fds, size);
720 Py_END_ALLOW_THREADS
721
722 if (n == -1 ) {
723 PyErr_SetFromErrno(PyExc_IOError);
724 return -1;
725 }
726 if (n < size) {
727 /*
728 ** Data writed to /dev/poll is a binary data structure. It is not
729 ** clear what to do if a partial write occurred. For now, raise
730 ** an exception and see if we actually found this problem in
731 ** the wild.
732 ** See http://bugs.python.org/issue6397.
733 */
734 PyErr_Format(PyExc_IOError, "failed to write all pollfds. "
735 "Please, report at http://bugs.python.org/. "
736 "Data to report: Size tried: %d, actual size written: %d.",
737 size, n);
738 return -1;
739 }
740 return 0;
741}
742
743static PyObject *
744internal_devpoll_register(devpollObject *self, PyObject *args, int remove)
745{
746 PyObject *o;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200747 int fd;
748 unsigned short events = POLLIN | POLLPRI | POLLOUT;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100749
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200750 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
Jesus Cead8b9ae62011-11-14 19:07:41 +0100751 return NULL;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100752
753 fd = PyObject_AsFileDescriptor(o);
754 if (fd == -1) return NULL;
755
756 if (remove) {
757 self->fds[self->n_fds].fd = fd;
758 self->fds[self->n_fds].events = POLLREMOVE;
759
760 if (++self->n_fds == self->max_n_fds) {
761 if (devpoll_flush(self))
762 return NULL;
763 }
764 }
765
766 self->fds[self->n_fds].fd = fd;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200767 self->fds[self->n_fds].events = (signed short)events;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100768
769 if (++self->n_fds == self->max_n_fds) {
770 if (devpoll_flush(self))
771 return NULL;
772 }
773
774 Py_RETURN_NONE;
775}
776
777PyDoc_STRVAR(devpoll_register_doc,
778"register(fd [, eventmask] ) -> None\n\n\
779Register a file descriptor with the polling object.\n\
780fd -- either an integer, or an object with a fileno() method returning an\n\
781 int.\n\
782events -- an optional bitmask describing the type of events to check for");
783
784static PyObject *
785devpoll_register(devpollObject *self, PyObject *args)
786{
787 return internal_devpoll_register(self, args, 0);
788}
789
790PyDoc_STRVAR(devpoll_modify_doc,
791"modify(fd[, eventmask]) -> None\n\n\
792Modify a possible already registered file descriptor.\n\
793fd -- either an integer, or an object with a fileno() method returning an\n\
794 int.\n\
795events -- an optional bitmask describing the type of events to check for");
796
797static PyObject *
798devpoll_modify(devpollObject *self, PyObject *args)
799{
800 return internal_devpoll_register(self, args, 1);
801}
802
803
804PyDoc_STRVAR(devpoll_unregister_doc,
805"unregister(fd) -> None\n\n\
806Remove a file descriptor being tracked by the polling object.");
807
808static PyObject *
809devpoll_unregister(devpollObject *self, PyObject *o)
810{
811 int fd;
812
813 fd = PyObject_AsFileDescriptor( o );
814 if (fd == -1)
815 return NULL;
816
817 self->fds[self->n_fds].fd = fd;
818 self->fds[self->n_fds].events = POLLREMOVE;
819
820 if (++self->n_fds == self->max_n_fds) {
821 if (devpoll_flush(self))
822 return NULL;
823 }
824
825 Py_RETURN_NONE;
826}
827
828PyDoc_STRVAR(devpoll_poll_doc,
829"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
830Polls the set of registered file descriptors, returning a list containing \n\
831any descriptors that have events or errors to report.");
832
833static PyObject *
834devpoll_poll(devpollObject *self, PyObject *args)
835{
836 struct dvpoll dvp;
837 PyObject *result_list = NULL, *tout = NULL;
838 int poll_result, i;
839 long timeout;
840 PyObject *value, *num1, *num2;
841
842 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
843 return NULL;
844 }
845
846 /* Check values for timeout */
847 if (tout == NULL || tout == Py_None)
848 timeout = -1;
849 else if (!PyNumber_Check(tout)) {
850 PyErr_SetString(PyExc_TypeError,
851 "timeout must be an integer or None");
852 return NULL;
853 }
854 else {
855 tout = PyNumber_Long(tout);
856 if (!tout)
857 return NULL;
858 timeout = PyLong_AsLong(tout);
859 Py_DECREF(tout);
860 if (timeout == -1 && PyErr_Occurred())
861 return NULL;
862 }
863
864 if ((timeout < -1) || (timeout > INT_MAX)) {
865 PyErr_SetString(PyExc_OverflowError,
866 "timeout is out of range");
867 return NULL;
868 }
869
870 if (devpoll_flush(self))
871 return NULL;
872
873 dvp.dp_fds = self->fds;
874 dvp.dp_nfds = self->max_n_fds;
875 dvp.dp_timeout = timeout;
876
877 /* call devpoll() */
878 Py_BEGIN_ALLOW_THREADS
879 poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp);
880 Py_END_ALLOW_THREADS
881
882 if (poll_result < 0) {
883 PyErr_SetFromErrno(PyExc_IOError);
884 return NULL;
885 }
886
887 /* build the result list */
888
889 result_list = PyList_New(poll_result);
890 if (!result_list)
891 return NULL;
892 else {
893 for (i = 0; i < poll_result; i++) {
894 num1 = PyLong_FromLong(self->fds[i].fd);
895 num2 = PyLong_FromLong(self->fds[i].revents);
896 if ((num1 == NULL) || (num2 == NULL)) {
897 Py_XDECREF(num1);
898 Py_XDECREF(num2);
899 goto error;
900 }
901 value = PyTuple_Pack(2, num1, num2);
902 Py_DECREF(num1);
903 Py_DECREF(num2);
904 if (value == NULL)
905 goto error;
906 if ((PyList_SetItem(result_list, i, value)) == -1) {
907 Py_DECREF(value);
908 goto error;
909 }
910 }
911 }
912
913 return result_list;
914
915 error:
916 Py_DECREF(result_list);
917 return NULL;
918}
919
920static PyMethodDef devpoll_methods[] = {
921 {"register", (PyCFunction)devpoll_register,
922 METH_VARARGS, devpoll_register_doc},
923 {"modify", (PyCFunction)devpoll_modify,
924 METH_VARARGS, devpoll_modify_doc},
925 {"unregister", (PyCFunction)devpoll_unregister,
926 METH_O, devpoll_unregister_doc},
927 {"poll", (PyCFunction)devpoll_poll,
928 METH_VARARGS, devpoll_poll_doc},
929 {NULL, NULL} /* sentinel */
930};
931
932static devpollObject *
933newDevPollObject(void)
934{
935 devpollObject *self;
936 int fd_devpoll, limit_result;
937 struct pollfd *fds;
938 struct rlimit limit;
939
940 Py_BEGIN_ALLOW_THREADS
941 /*
942 ** If we try to process more that getrlimit()
943 ** fds, the kernel will give an error, so
944 ** we set the limit here. It is a dynamic
945 ** value, because we can change rlimit() anytime.
946 */
947 limit_result = getrlimit(RLIMIT_NOFILE, &limit);
948 if (limit_result != -1)
949 fd_devpoll = open("/dev/poll", O_RDWR);
950 Py_END_ALLOW_THREADS
951
952 if (limit_result == -1) {
953 PyErr_SetFromErrno(PyExc_OSError);
954 return NULL;
955 }
956 if (fd_devpoll == -1) {
957 PyErr_SetFromErrnoWithFilename(PyExc_IOError, "/dev/poll");
958 return NULL;
959 }
960
961 fds = PyMem_NEW(struct pollfd, limit.rlim_cur);
962 if (fds == NULL) {
963 close(fd_devpoll);
964 PyErr_NoMemory();
965 return NULL;
966 }
967
968 self = PyObject_New(devpollObject, &devpoll_Type);
969 if (self == NULL) {
970 close(fd_devpoll);
971 PyMem_DEL(fds);
972 return NULL;
973 }
974 self->fd_devpoll = fd_devpoll;
975 self->max_n_fds = limit.rlim_cur;
976 self->n_fds = 0;
977 self->fds = fds;
978
979 return self;
980}
981
982static void
983devpoll_dealloc(devpollObject *self)
984{
985 Py_BEGIN_ALLOW_THREADS
986 close(self->fd_devpoll);
987 Py_END_ALLOW_THREADS
988
989 PyMem_DEL(self->fds);
990
991 PyObject_Del(self);
992}
993
994static PyTypeObject devpoll_Type = {
995 /* The ob_type field must be initialized in the module init function
996 * to be portable to Windows without using C++. */
997 PyVarObject_HEAD_INIT(NULL, 0)
998 "select.devpoll", /*tp_name*/
999 sizeof(devpollObject), /*tp_basicsize*/
1000 0, /*tp_itemsize*/
1001 /* methods */
1002 (destructor)devpoll_dealloc, /*tp_dealloc*/
1003 0, /*tp_print*/
1004 0, /*tp_getattr*/
1005 0, /*tp_setattr*/
1006 0, /*tp_reserved*/
1007 0, /*tp_repr*/
1008 0, /*tp_as_number*/
1009 0, /*tp_as_sequence*/
1010 0, /*tp_as_mapping*/
1011 0, /*tp_hash*/
1012 0, /*tp_call*/
1013 0, /*tp_str*/
1014 0, /*tp_getattro*/
1015 0, /*tp_setattro*/
1016 0, /*tp_as_buffer*/
1017 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1018 0, /*tp_doc*/
1019 0, /*tp_traverse*/
1020 0, /*tp_clear*/
1021 0, /*tp_richcompare*/
1022 0, /*tp_weaklistoffset*/
1023 0, /*tp_iter*/
1024 0, /*tp_iternext*/
1025 devpoll_methods, /*tp_methods*/
1026};
1027#endif /* HAVE_SYS_DEVPOLL_H */
1028
1029
1030
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001031PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001032"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001033unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001034
1035static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001036select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001039}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001040
Jesus Cead8b9ae62011-11-14 19:07:41 +01001041#ifdef HAVE_SYS_DEVPOLL_H
1042PyDoc_STRVAR(devpoll_doc,
1043"Returns a polling object, which supports registering and\n\
1044unregistering file descriptors, and then polling them for I/O events.");
1045
1046static PyObject *
1047select_devpoll(PyObject *self, PyObject *unused)
1048{
1049 return (PyObject *)newDevPollObject();
1050}
1051#endif
1052
1053
Thomas Wouters477c8d52006-05-27 19:21:47 +00001054#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055/*
Thomas Wouters477c8d52006-05-27 19:21:47 +00001056 * On some systems poll() sets errno on invalid file descriptors. We test
1057 * for this at runtime because this bug may be fixed or introduced between
1058 * OS releases.
1059 */
1060static int select_have_broken_poll(void)
1061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 int poll_test;
1063 int filedes[2];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
Thomas Wouters477c8d52006-05-27 19:21:47 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 /* Create a file descriptor to make invalid */
1068 if (pipe(filedes) < 0) {
1069 return 1;
1070 }
1071 poll_struct.fd = filedes[0];
1072 close(filedes[0]);
1073 close(filedes[1]);
1074 poll_test = poll(&poll_struct, 1, 0);
1075 if (poll_test < 0) {
1076 return 1;
1077 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
1078 return 1;
1079 }
1080 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001081}
1082#endif /* __APPLE__ */
1083
1084#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001085
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001086#ifdef HAVE_EPOLL
1087/* **************************************************************************
1088 * epoll interface for Linux 2.6
1089 *
1090 * Written by Christian Heimes
1091 * Inspired by Twisted's _epoll.pyx and select.poll()
1092 */
1093
1094#ifdef HAVE_SYS_EPOLL_H
1095#include <sys/epoll.h>
1096#endif
1097
1098typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 PyObject_HEAD
1100 SOCKET epfd; /* epoll control file descriptor */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001101} pyEpoll_Object;
1102
1103static PyTypeObject pyEpoll_Type;
1104#define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))
1105
1106static PyObject *
1107pyepoll_err_closed(void)
1108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll fd");
1110 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001111}
1112
1113static int
1114pyepoll_internal_close(pyEpoll_Object *self)
1115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 int save_errno = 0;
1117 if (self->epfd >= 0) {
1118 int epfd = self->epfd;
1119 self->epfd = -1;
1120 Py_BEGIN_ALLOW_THREADS
1121 if (close(epfd) < 0)
1122 save_errno = errno;
1123 Py_END_ALLOW_THREADS
1124 }
1125 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001126}
1127
1128static PyObject *
Benjamin Peterson95c16622011-12-27 15:36:32 -06001129newPyEpoll_Object(PyTypeObject *type, int sizehint, int flags, SOCKET fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 pyEpoll_Object *self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 assert(type != NULL && type->tp_alloc != NULL);
1134 self = (pyEpoll_Object *) type->tp_alloc(type, 0);
1135 if (self == NULL)
1136 return NULL;
1137
1138 if (fd == -1) {
1139 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson95c16622011-12-27 15:36:32 -06001140#ifdef HAVE_EPOLL_CREATE1
Benjamin Peterson83251c12011-12-27 16:01:21 -06001141 if (flags)
1142 self->epfd = epoll_create1(flags);
1143 else
Benjamin Peterson95c16622011-12-27 15:36:32 -06001144#endif
Benjamin Peterson83251c12011-12-27 16:01:21 -06001145 self->epfd = epoll_create(sizehint);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 Py_END_ALLOW_THREADS
1147 }
1148 else {
1149 self->epfd = fd;
1150 }
1151 if (self->epfd < 0) {
1152 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001153 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 return NULL;
1155 }
1156 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001157}
1158
1159
1160static PyObject *
1161pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1162{
Benjamin Peterson83251c12011-12-27 16:01:21 -06001163 int flags = 0, sizehint = FD_SETSIZE - 1;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001164 static char *kwlist[] = {"sizehint", "flags", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001165
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001166 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
1167 &sizehint, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 return NULL;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001169 if (sizehint < 0) {
1170 PyErr_SetString(PyExc_ValueError, "negative sizehint");
1171 return NULL;
1172 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001173
Benjamin Peterson95c16622011-12-27 15:36:32 -06001174 return newPyEpoll_Object(type, sizehint, flags, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001175}
1176
1177
1178static void
1179pyepoll_dealloc(pyEpoll_Object *self)
1180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 (void)pyepoll_internal_close(self);
1182 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001183}
1184
1185static PyObject*
1186pyepoll_close(pyEpoll_Object *self)
1187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 errno = pyepoll_internal_close(self);
1189 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001190 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 return NULL;
1192 }
1193 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001194}
1195
1196PyDoc_STRVAR(pyepoll_close_doc,
1197"close() -> None\n\
1198\n\
1199Close the epoll control file descriptor. Further operations on the epoll\n\
1200object will raise an exception.");
1201
1202static PyObject*
1203pyepoll_get_closed(pyEpoll_Object *self)
1204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 if (self->epfd < 0)
1206 Py_RETURN_TRUE;
1207 else
1208 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001209}
1210
1211static PyObject*
1212pyepoll_fileno(pyEpoll_Object *self)
1213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (self->epfd < 0)
1215 return pyepoll_err_closed();
1216 return PyLong_FromLong(self->epfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001217}
1218
1219PyDoc_STRVAR(pyepoll_fileno_doc,
1220"fileno() -> int\n\
1221\n\
1222Return the epoll control file descriptor.");
1223
1224static PyObject*
1225pyepoll_fromfd(PyObject *cls, PyObject *args)
1226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1230 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001231
Benjamin Peterson95c16622011-12-27 15:36:32 -06001232 return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001233}
1234
1235PyDoc_STRVAR(pyepoll_fromfd_doc,
1236"fromfd(fd) -> epoll\n\
1237\n\
1238Create an epoll object from a given control fd.");
1239
1240static PyObject *
1241pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
1242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 struct epoll_event ev;
1244 int result;
1245 int fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (epfd < 0)
1248 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 fd = PyObject_AsFileDescriptor(pfd);
1251 if (fd == -1) {
1252 return NULL;
1253 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 switch(op) {
1256 case EPOLL_CTL_ADD:
1257 case EPOLL_CTL_MOD:
1258 ev.events = events;
1259 ev.data.fd = fd;
1260 Py_BEGIN_ALLOW_THREADS
1261 result = epoll_ctl(epfd, op, fd, &ev);
1262 Py_END_ALLOW_THREADS
1263 break;
1264 case EPOLL_CTL_DEL:
1265 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
1266 * operation required a non-NULL pointer in event, even
1267 * though this argument is ignored. */
1268 Py_BEGIN_ALLOW_THREADS
1269 result = epoll_ctl(epfd, op, fd, &ev);
1270 if (errno == EBADF) {
1271 /* fd already closed */
1272 result = 0;
1273 errno = 0;
1274 }
1275 Py_END_ALLOW_THREADS
1276 break;
1277 default:
1278 result = -1;
1279 errno = EINVAL;
1280 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001283 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 return NULL;
1285 }
1286 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001287}
1288
1289static PyObject *
1290pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 PyObject *pfd;
1293 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
1294 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
1297 &pfd, &events)) {
1298 return NULL;
1299 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001302}
1303
1304PyDoc_STRVAR(pyepoll_register_doc,
Georg Brandl222569d2010-08-02 20:47:56 +00001305"register(fd[, eventmask]) -> None\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001306\n\
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001307Registers a new fd or raises an OSError if the fd is already registered.\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00001308fd is the target file descriptor of the operation.\n\
1309events is a bit set composed of the various EPOLL constants; the default\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001310is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
1311\n\
1312The epoll interface supports all file descriptors that support poll.");
1313
1314static PyObject *
1315pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 PyObject *pfd;
1318 unsigned int events;
1319 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
1322 &pfd, &events)) {
1323 return NULL;
1324 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001327}
1328
1329PyDoc_STRVAR(pyepoll_modify_doc,
1330"modify(fd, eventmask) -> None\n\
1331\n\
1332fd is the target file descriptor of the operation\n\
1333events is a bit set composed of the various EPOLL constants");
1334
1335static PyObject *
1336pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 PyObject *pfd;
1339 static char *kwlist[] = {"fd", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
1342 &pfd)) {
1343 return NULL;
1344 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001347}
1348
1349PyDoc_STRVAR(pyepoll_unregister_doc,
1350"unregister(fd) -> None\n\
1351\n\
1352fd is the target file descriptor of the operation.");
1353
1354static PyObject *
1355pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 double dtimeout = -1.;
1358 int timeout;
1359 int maxevents = -1;
1360 int nfds, i;
1361 PyObject *elist = NULL, *etuple = NULL;
1362 struct epoll_event *evs = NULL;
1363 static char *kwlist[] = {"timeout", "maxevents", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 if (self->epfd < 0)
1366 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
1369 &dtimeout, &maxevents)) {
1370 return NULL;
1371 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 if (dtimeout < 0) {
1374 timeout = -1;
1375 }
1376 else if (dtimeout * 1000.0 > INT_MAX) {
1377 PyErr_SetString(PyExc_OverflowError,
1378 "timeout is too large");
1379 return NULL;
1380 }
1381 else {
Victor Stinner665486e2014-01-21 01:41:00 +01001382 /* epoll_wait() has a resolution of 1 millisecond, round away from zero
1383 to wait *at least* dtimeout seconds. */
1384 timeout = (int)ceil(dtimeout * 1000.0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 if (maxevents == -1) {
1388 maxevents = FD_SETSIZE-1;
1389 }
1390 else if (maxevents < 1) {
1391 PyErr_Format(PyExc_ValueError,
1392 "maxevents must be greater than 0, got %d",
1393 maxevents);
1394 return NULL;
1395 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 evs = PyMem_New(struct epoll_event, maxevents);
1398 if (evs == NULL) {
1399 Py_DECREF(self);
1400 PyErr_NoMemory();
1401 return NULL;
1402 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 Py_BEGIN_ALLOW_THREADS
1405 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
1406 Py_END_ALLOW_THREADS
1407 if (nfds < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001408 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 goto error;
1410 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 elist = PyList_New(nfds);
1413 if (elist == NULL) {
1414 goto error;
1415 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 for (i = 0; i < nfds; i++) {
1418 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
1419 if (etuple == NULL) {
1420 Py_CLEAR(elist);
1421 goto error;
1422 }
1423 PyList_SET_ITEM(elist, i, etuple);
1424 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001425
Christian Heimesf6cd9672008-03-26 13:45:42 +00001426 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 PyMem_Free(evs);
1428 return elist;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001429}
1430
1431PyDoc_STRVAR(pyepoll_poll_doc,
1432"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1433\n\
1434Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1435in seconds (as float). -1 makes poll wait indefinitely.\n\
1436Up to maxevents are returned to the caller.");
1437
1438static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 {"fromfd", (PyCFunction)pyepoll_fromfd,
1440 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1441 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1442 pyepoll_close_doc},
1443 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1444 pyepoll_fileno_doc},
1445 {"modify", (PyCFunction)pyepoll_modify,
1446 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1447 {"register", (PyCFunction)pyepoll_register,
1448 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1449 {"unregister", (PyCFunction)pyepoll_unregister,
1450 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1451 {"poll", (PyCFunction)pyepoll_poll,
1452 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
1453 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001454};
1455
1456static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 {"closed", (getter)pyepoll_get_closed, NULL,
1458 "True if the epoll handler is closed"},
1459 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001460};
1461
1462PyDoc_STRVAR(pyepoll_doc,
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001463"select.epoll(sizehint=-1, flags=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001464\n\
1465Returns an epolling object\n\
1466\n\
1467sizehint must be a positive integer or -1 for the default size. The\n\
1468sizehint is used to optimize internal data structures. It doesn't limit\n\
1469the maximum number of monitored events.");
1470
1471static PyTypeObject pyEpoll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 PyVarObject_HEAD_INIT(NULL, 0)
1473 "select.epoll", /* tp_name */
1474 sizeof(pyEpoll_Object), /* tp_basicsize */
1475 0, /* tp_itemsize */
1476 (destructor)pyepoll_dealloc, /* tp_dealloc */
1477 0, /* tp_print */
1478 0, /* tp_getattr */
1479 0, /* tp_setattr */
1480 0, /* tp_reserved */
1481 0, /* tp_repr */
1482 0, /* tp_as_number */
1483 0, /* tp_as_sequence */
1484 0, /* tp_as_mapping */
1485 0, /* tp_hash */
1486 0, /* tp_call */
1487 0, /* tp_str */
1488 PyObject_GenericGetAttr, /* tp_getattro */
1489 0, /* tp_setattro */
1490 0, /* tp_as_buffer */
1491 Py_TPFLAGS_DEFAULT, /* tp_flags */
1492 pyepoll_doc, /* tp_doc */
1493 0, /* tp_traverse */
1494 0, /* tp_clear */
1495 0, /* tp_richcompare */
1496 0, /* tp_weaklistoffset */
1497 0, /* tp_iter */
1498 0, /* tp_iternext */
1499 pyepoll_methods, /* tp_methods */
1500 0, /* tp_members */
1501 pyepoll_getsetlist, /* tp_getset */
1502 0, /* tp_base */
1503 0, /* tp_dict */
1504 0, /* tp_descr_get */
1505 0, /* tp_descr_set */
1506 0, /* tp_dictoffset */
1507 0, /* tp_init */
1508 0, /* tp_alloc */
1509 pyepoll_new, /* tp_new */
1510 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001511};
1512
1513#endif /* HAVE_EPOLL */
1514
1515#ifdef HAVE_KQUEUE
1516/* **************************************************************************
1517 * kqueue interface for BSD
1518 *
1519 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1520 * All rights reserved.
1521 *
1522 * Redistribution and use in source and binary forms, with or without
1523 * modification, are permitted provided that the following conditions
1524 * are met:
1525 * 1. Redistributions of source code must retain the above copyright
1526 * notice, this list of conditions and the following disclaimer.
1527 * 2. Redistributions in binary form must reproduce the above copyright
1528 * notice, this list of conditions and the following disclaimer in the
1529 * documentation and/or other materials provided with the distribution.
1530 *
1531 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1532 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1533 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1534 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1535 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1536 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1537 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1538 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1539 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1540 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1541 * SUCH DAMAGE.
1542 */
1543
1544#ifdef HAVE_SYS_EVENT_H
1545#include <sys/event.h>
1546#endif
1547
1548PyDoc_STRVAR(kqueue_event_doc,
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001549"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001550\n\
1551This object is the equivalent of the struct kevent for the C API.\n\
1552\n\
1553See the kqueue manpage for more detailed information about the meaning\n\
1554of the arguments.\n\
1555\n\
1556One minor note: while you might hope that udata could store a\n\
1557reference to a python object, it cannot, because it is impossible to\n\
1558keep a proper reference count of the object once it's passed into the\n\
1559kernel. Therefore, I have restricted it to only storing an integer. I\n\
1560recommend ignoring it and simply using the 'ident' field to key off\n\
1561of. You could also set up a dictionary on the python side to store a\n\
1562udata->object mapping.");
1563
1564typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 PyObject_HEAD
1566 struct kevent e;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001567} kqueue_event_Object;
1568
1569static PyTypeObject kqueue_event_Type;
1570
1571#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1572
1573typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 PyObject_HEAD
1575 SOCKET kqfd; /* kqueue control fd */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001576} kqueue_queue_Object;
1577
1578static PyTypeObject kqueue_queue_Type;
1579
1580#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1581
Antoine Pitroud83f1e62009-11-04 21:10:38 +00001582#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1583# error uintptr_t does not match void *!
1584#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1585# define T_UINTPTRT T_ULONGLONG
1586# define T_INTPTRT T_LONGLONG
1587# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1588# define UINTPTRT_FMT_UNIT "K"
1589# define INTPTRT_FMT_UNIT "L"
1590#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1591# define T_UINTPTRT T_ULONG
1592# define T_INTPTRT T_LONG
1593# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1594# define UINTPTRT_FMT_UNIT "k"
1595# define INTPTRT_FMT_UNIT "l"
1596#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1597# define T_UINTPTRT T_UINT
1598# define T_INTPTRT T_INT
1599# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1600# define UINTPTRT_FMT_UNIT "I"
1601# define INTPTRT_FMT_UNIT "i"
1602#else
1603# error uintptr_t does not match int, long, or long long!
1604#endif
1605
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001606/*
1607 * kevent is not standard and its members vary across BSDs.
1608 */
1609#if !defined(__OpenBSD__)
1610# define IDENT_TYPE T_UINTPTRT
1611# define IDENT_CAST Py_intptr_t
1612# define DATA_TYPE T_INTPTRT
1613# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
1614# define IDENT_AsType PyLong_AsUintptr_t
1615#else
1616# define IDENT_TYPE T_UINT
1617# define IDENT_CAST int
1618# define DATA_TYPE T_INT
1619# define DATA_FMT_UNIT "i"
1620# define IDENT_AsType PyLong_AsUnsignedLong
1621#endif
1622
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001623/* Unfortunately, we can't store python objects in udata, because
1624 * kevents in the kernel can be removed without warning, which would
1625 * forever lose the refcount on the object stored with it.
1626 */
1627
1628#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1629static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001630 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 {"filter", T_SHORT, KQ_OFF(e.filter)},
1632 {"flags", T_USHORT, KQ_OFF(e.flags)},
1633 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001634 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1636 {NULL} /* Sentinel */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001637};
1638#undef KQ_OFF
1639
1640static PyObject *
Georg Brandlc0e22b72010-03-14 10:51:01 +00001641
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001642kqueue_event_repr(kqueue_event_Object *s)
1643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 char buf[1024];
1645 PyOS_snprintf(
1646 buf, sizeof(buf),
1647 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1648 "data=0x%zd udata=%p>",
1649 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1650 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1651 return PyUnicode_FromString(buf);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001652}
1653
1654static int
1655kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 PyObject *pfd;
1658 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1659 "data", "udata", NULL};
Christian Heimesf1fe1592013-08-25 14:57:00 +02001660 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1665 &pfd, &(self->e.filter), &(self->e.flags),
1666 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1667 return -1;
1668 }
1669
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001670 if (PyLong_Check(pfd)
1671#if IDENT_TYPE == T_UINT
1672 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
1673#endif
1674 ) {
1675 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 }
1677 else {
1678 self->e.ident = PyObject_AsFileDescriptor(pfd);
1679 }
1680 if (PyErr_Occurred()) {
1681 return -1;
1682 }
1683 return 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001684}
1685
1686static PyObject *
1687kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 int op)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 Py_intptr_t result = 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 if (!kqueue_event_Check(o)) {
1693 if (op == Py_EQ || op == Py_NE) {
1694 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1695 Py_INCREF(res);
1696 return res;
1697 }
1698 PyErr_Format(PyExc_TypeError,
1699 "can't compare %.200s to %.200s",
1700 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1701 return NULL;
1702 }
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001703 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 ((result = s->e.filter - o->e.filter) == 0) &&
1705 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001706 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 ((result = s->e.data - o->e.data) == 0) &&
1708 ((result = s->e.udata - o->e.udata) == 0)
1709 ) {
1710 result = 0;
1711 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 switch (op) {
1714 case Py_EQ:
1715 result = (result == 0);
1716 break;
1717 case Py_NE:
1718 result = (result != 0);
1719 break;
1720 case Py_LE:
1721 result = (result <= 0);
1722 break;
1723 case Py_GE:
1724 result = (result >= 0);
1725 break;
1726 case Py_LT:
1727 result = (result < 0);
1728 break;
1729 case Py_GT:
1730 result = (result > 0);
1731 break;
1732 }
1733 return PyBool_FromLong((long)result);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001734}
1735
1736static PyTypeObject kqueue_event_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 PyVarObject_HEAD_INIT(NULL, 0)
1738 "select.kevent", /* tp_name */
1739 sizeof(kqueue_event_Object), /* tp_basicsize */
1740 0, /* tp_itemsize */
1741 0, /* tp_dealloc */
1742 0, /* tp_print */
1743 0, /* tp_getattr */
1744 0, /* tp_setattr */
1745 0, /* tp_reserved */
1746 (reprfunc)kqueue_event_repr, /* tp_repr */
1747 0, /* tp_as_number */
1748 0, /* tp_as_sequence */
1749 0, /* tp_as_mapping */
1750 0, /* tp_hash */
1751 0, /* tp_call */
1752 0, /* tp_str */
1753 0, /* tp_getattro */
1754 0, /* tp_setattro */
1755 0, /* tp_as_buffer */
1756 Py_TPFLAGS_DEFAULT, /* tp_flags */
1757 kqueue_event_doc, /* tp_doc */
1758 0, /* tp_traverse */
1759 0, /* tp_clear */
1760 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1761 0, /* tp_weaklistoffset */
1762 0, /* tp_iter */
1763 0, /* tp_iternext */
1764 0, /* tp_methods */
1765 kqueue_event_members, /* tp_members */
1766 0, /* tp_getset */
1767 0, /* tp_base */
1768 0, /* tp_dict */
1769 0, /* tp_descr_get */
1770 0, /* tp_descr_set */
1771 0, /* tp_dictoffset */
1772 (initproc)kqueue_event_init, /* tp_init */
1773 0, /* tp_alloc */
1774 0, /* tp_new */
1775 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001776};
1777
1778static PyObject *
1779kqueue_queue_err_closed(void)
1780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue fd");
1782 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001783}
1784
1785static int
1786kqueue_queue_internal_close(kqueue_queue_Object *self)
1787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 int save_errno = 0;
1789 if (self->kqfd >= 0) {
1790 int kqfd = self->kqfd;
1791 self->kqfd = -1;
1792 Py_BEGIN_ALLOW_THREADS
1793 if (close(kqfd) < 0)
1794 save_errno = errno;
1795 Py_END_ALLOW_THREADS
1796 }
1797 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001798}
1799
1800static PyObject *
1801newKqueue_Object(PyTypeObject *type, SOCKET fd)
1802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 kqueue_queue_Object *self;
1804 assert(type != NULL && type->tp_alloc != NULL);
1805 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1806 if (self == NULL) {
1807 return NULL;
1808 }
1809
1810 if (fd == -1) {
1811 Py_BEGIN_ALLOW_THREADS
1812 self->kqfd = kqueue();
1813 Py_END_ALLOW_THREADS
1814 }
1815 else {
1816 self->kqfd = fd;
1817 }
1818 if (self->kqfd < 0) {
1819 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001820 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 return NULL;
1822 }
1823 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001824}
1825
1826static PyObject *
1827kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1828{
1829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 if ((args != NULL && PyObject_Size(args)) ||
1831 (kwds != NULL && PyObject_Size(kwds))) {
1832 PyErr_SetString(PyExc_ValueError,
1833 "select.kqueue doesn't accept arguments");
1834 return NULL;
1835 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 return newKqueue_Object(type, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001838}
1839
1840static void
1841kqueue_queue_dealloc(kqueue_queue_Object *self)
1842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 kqueue_queue_internal_close(self);
1844 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001845}
1846
1847static PyObject*
1848kqueue_queue_close(kqueue_queue_Object *self)
1849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 errno = kqueue_queue_internal_close(self);
1851 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001852 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 return NULL;
1854 }
1855 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001856}
1857
1858PyDoc_STRVAR(kqueue_queue_close_doc,
1859"close() -> None\n\
1860\n\
1861Close the kqueue control file descriptor. Further operations on the kqueue\n\
1862object will raise an exception.");
1863
1864static PyObject*
1865kqueue_queue_get_closed(kqueue_queue_Object *self)
1866{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 if (self->kqfd < 0)
1868 Py_RETURN_TRUE;
1869 else
1870 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001871}
1872
1873static PyObject*
1874kqueue_queue_fileno(kqueue_queue_Object *self)
1875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 if (self->kqfd < 0)
1877 return kqueue_queue_err_closed();
1878 return PyLong_FromLong(self->kqfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001879}
1880
1881PyDoc_STRVAR(kqueue_queue_fileno_doc,
1882"fileno() -> int\n\
1883\n\
1884Return the kqueue control file descriptor.");
1885
1886static PyObject*
1887kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1892 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001895}
1896
1897PyDoc_STRVAR(kqueue_queue_fromfd_doc,
1898"fromfd(fd) -> kqueue\n\
1899\n\
1900Create a kqueue object from a given control fd.");
1901
1902static PyObject *
1903kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
1904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 int nevents = 0;
1906 int gotevents = 0;
1907 int nchanges = 0;
1908 int i = 0;
1909 PyObject *otimeout = NULL;
1910 PyObject *ch = NULL;
1911 PyObject *it = NULL, *ei = NULL;
1912 PyObject *result = NULL;
1913 struct kevent *evl = NULL;
1914 struct kevent *chl = NULL;
Victor Stinnerd327f9d2012-03-13 15:29:08 +01001915 struct timespec timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 struct timespec *ptimeoutspec;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 if (self->kqfd < 0)
1919 return kqueue_queue_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
1922 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 if (nevents < 0) {
1925 PyErr_Format(PyExc_ValueError,
1926 "Length of eventlist must be 0 or positive, got %d",
1927 nevents);
1928 return NULL;
1929 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 if (otimeout == Py_None || otimeout == NULL) {
1932 ptimeoutspec = NULL;
1933 }
1934 else if (PyNumber_Check(otimeout)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +01001935 if (_PyTime_ObjectToTimespec(otimeout,
1936 &timeout.tv_sec, &timeout.tv_nsec) == -1)
1937 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001938
Victor Stinner5d272cc2012-03-13 13:35:55 +01001939 if (timeout.tv_sec < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 PyErr_SetString(PyExc_ValueError,
1941 "timeout must be positive or None");
1942 return NULL;
1943 }
Victor Stinnerd528b012012-03-13 16:25:35 +01001944 ptimeoutspec = &timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 }
1946 else {
1947 PyErr_Format(PyExc_TypeError,
1948 "timeout argument must be an number "
1949 "or None, got %.200s",
1950 Py_TYPE(otimeout)->tp_name);
1951 return NULL;
1952 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 if (ch != NULL && ch != Py_None) {
1955 it = PyObject_GetIter(ch);
1956 if (it == NULL) {
1957 PyErr_SetString(PyExc_TypeError,
1958 "changelist is not iterable");
1959 return NULL;
1960 }
1961 nchanges = PyObject_Size(ch);
1962 if (nchanges < 0) {
1963 goto error;
1964 }
Georg Brandlc0e22b72010-03-14 10:51:01 +00001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 chl = PyMem_New(struct kevent, nchanges);
1967 if (chl == NULL) {
1968 PyErr_NoMemory();
1969 goto error;
1970 }
1971 i = 0;
1972 while ((ei = PyIter_Next(it)) != NULL) {
1973 if (!kqueue_event_Check(ei)) {
1974 Py_DECREF(ei);
1975 PyErr_SetString(PyExc_TypeError,
1976 "changelist must be an iterable of "
1977 "select.kevent objects");
1978 goto error;
1979 } else {
1980 chl[i++] = ((kqueue_event_Object *)ei)->e;
1981 }
1982 Py_DECREF(ei);
1983 }
1984 }
1985 Py_CLEAR(it);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 /* event list */
1988 if (nevents) {
1989 evl = PyMem_New(struct kevent, nevents);
1990 if (evl == NULL) {
1991 PyErr_NoMemory();
1992 goto error;
1993 }
1994 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996 Py_BEGIN_ALLOW_THREADS
1997 gotevents = kevent(self->kqfd, chl, nchanges,
1998 evl, nevents, ptimeoutspec);
1999 Py_END_ALLOW_THREADS
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 if (gotevents == -1) {
2002 PyErr_SetFromErrno(PyExc_OSError);
2003 goto error;
2004 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 result = PyList_New(gotevents);
2007 if (result == NULL) {
2008 goto error;
2009 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 for (i = 0; i < gotevents; i++) {
2012 kqueue_event_Object *ch;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
2015 if (ch == NULL) {
2016 goto error;
2017 }
2018 ch->e = evl[i];
2019 PyList_SET_ITEM(result, i, (PyObject *)ch);
2020 }
2021 PyMem_Free(chl);
2022 PyMem_Free(evl);
2023 return result;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002024
2025 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 PyMem_Free(chl);
2027 PyMem_Free(evl);
2028 Py_XDECREF(result);
2029 Py_XDECREF(it);
2030 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002031}
2032
2033PyDoc_STRVAR(kqueue_queue_control_doc,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00002034"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002035\n\
2036Calls the kernel kevent function.\n\
2037- changelist must be a list of kevent objects describing the changes\n\
2038 to be made to the kernel's watch list or None.\n\
2039- max_events lets you specify the maximum number of events that the\n\
2040 kernel will return.\n\
2041- timeout is the maximum time to wait in seconds, or else None,\n\
2042 to wait forever. timeout accepts floats for smaller timeouts, too.");
2043
2044
2045static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
2047 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
2048 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
2049 kqueue_queue_close_doc},
2050 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
2051 kqueue_queue_fileno_doc},
2052 {"control", (PyCFunction)kqueue_queue_control,
2053 METH_VARARGS , kqueue_queue_control_doc},
2054 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002055};
2056
2057static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 {"closed", (getter)kqueue_queue_get_closed, NULL,
2059 "True if the kqueue handler is closed"},
2060 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002061};
2062
2063PyDoc_STRVAR(kqueue_queue_doc,
2064"Kqueue syscall wrapper.\n\
2065\n\
2066For example, to start watching a socket for input:\n\
2067>>> kq = kqueue()\n\
2068>>> sock = socket()\n\
2069>>> sock.connect((host, port))\n\
2070>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
2071\n\
2072To wait one second for it to become writeable:\n\
2073>>> kq.control(None, 1, 1000)\n\
2074\n\
2075To stop listening:\n\
2076>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
2077
2078static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 PyVarObject_HEAD_INIT(NULL, 0)
2080 "select.kqueue", /* tp_name */
2081 sizeof(kqueue_queue_Object), /* tp_basicsize */
2082 0, /* tp_itemsize */
2083 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
2084 0, /* tp_print */
2085 0, /* tp_getattr */
2086 0, /* tp_setattr */
2087 0, /* tp_reserved */
2088 0, /* tp_repr */
2089 0, /* tp_as_number */
2090 0, /* tp_as_sequence */
2091 0, /* tp_as_mapping */
2092 0, /* tp_hash */
2093 0, /* tp_call */
2094 0, /* tp_str */
2095 0, /* tp_getattro */
2096 0, /* tp_setattro */
2097 0, /* tp_as_buffer */
2098 Py_TPFLAGS_DEFAULT, /* tp_flags */
2099 kqueue_queue_doc, /* tp_doc */
2100 0, /* tp_traverse */
2101 0, /* tp_clear */
2102 0, /* tp_richcompare */
2103 0, /* tp_weaklistoffset */
2104 0, /* tp_iter */
2105 0, /* tp_iternext */
2106 kqueue_queue_methods, /* tp_methods */
2107 0, /* tp_members */
2108 kqueue_queue_getsetlist, /* tp_getset */
2109 0, /* tp_base */
2110 0, /* tp_dict */
2111 0, /* tp_descr_get */
2112 0, /* tp_descr_set */
2113 0, /* tp_dictoffset */
2114 0, /* tp_init */
2115 0, /* tp_alloc */
2116 kqueue_queue_new, /* tp_new */
2117 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002118};
2119
2120#endif /* HAVE_KQUEUE */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002121
2122
2123
2124
2125
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002126/* ************************************************************************ */
2127
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002128PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002129"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
2130\n\
2131Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00002132The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002133rlist -- wait until ready for reading\n\
2134wlist -- wait until ready for writing\n\
2135xlist -- wait for an ``exceptional condition''\n\
2136If only one kind of condition is required, pass [] for the other lists.\n\
2137A file descriptor is either a socket or file object, or a small integer\n\
2138gotten from a fileno() method call on one of those.\n\
2139\n\
2140The optional 4th argument specifies a timeout in seconds; it may be\n\
2141a floating point number to specify fractions of seconds. If it is absent\n\
2142or None, the call will never time out.\n\
2143\n\
2144The return value is a tuple of three lists corresponding to the first three\n\
2145arguments; each contains the subset of the corresponding file descriptors\n\
2146that are ready.\n\
2147\n\
2148*** IMPORTANT NOTICE ***\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002149On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00002150descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002151
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002152static PyMethodDef select_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natali986a56c2013-01-19 12:19:10 +01002154#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002156#endif /* HAVE_POLL */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002157#ifdef HAVE_SYS_DEVPOLL_H
2158 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2159#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00002161};
2162
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002163PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002164"This module supports asynchronous I/O on multiple file descriptors.\n\
2165\n\
2166*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002167On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00002168
Martin v. Löwis1a214512008-06-11 05:26:20 +00002169
2170static struct PyModuleDef selectmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 PyModuleDef_HEAD_INIT,
2172 "select",
2173 module_doc,
2174 -1,
2175 select_methods,
2176 NULL,
2177 NULL,
2178 NULL,
2179 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002180};
2181
Jesus Cead8b9ae62011-11-14 19:07:41 +01002182
2183
2184
Mark Hammond62b1ab12002-07-23 06:31:15 +00002185PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002186PyInit_select(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00002187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 PyObject *m;
2189 m = PyModule_Create(&selectmodule);
2190 if (m == NULL)
2191 return NULL;
Fred Drake4baedc12002-04-01 14:53:37 +00002192
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002193 Py_INCREF(PyExc_OSError);
2194 PyModule_AddObject(m, "error", PyExc_OSError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002195
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002196#ifdef PIPE_BUF
R. David Murraye16cda92010-10-15 23:12:57 +00002197#ifdef HAVE_BROKEN_PIPE_BUF
2198#undef PIPE_BUF
2199#define PIPE_BUF 512
2200#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF);
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002202#endif
Gregory P. Smithb970b862009-07-04 02:28:47 +00002203
Charles-François Natali986a56c2013-01-19 12:19:10 +01002204#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002205#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 if (select_have_broken_poll()) {
2207 if (PyObject_DelAttrString(m, "poll") == -1) {
2208 PyErr_Clear();
2209 }
2210 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002211#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002213#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 if (PyType_Ready(&poll_Type) < 0)
2215 return NULL;
2216 PyModule_AddIntConstant(m, "POLLIN", POLLIN);
2217 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
2218 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
2219 PyModule_AddIntConstant(m, "POLLERR", POLLERR);
2220 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
2221 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00002222
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002223#ifdef POLLRDNORM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002225#endif
2226#ifdef POLLRDBAND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002228#endif
2229#ifdef POLLWRNORM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002231#endif
2232#ifdef POLLWRBAND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002234#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002235#ifdef POLLMSG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002237#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002239#endif /* HAVE_POLL */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002240
Jesus Cead8b9ae62011-11-14 19:07:41 +01002241#ifdef HAVE_SYS_DEVPOLL_H
2242 if (PyType_Ready(&devpoll_Type) < 0)
2243 return NULL;
2244#endif
2245
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002246#ifdef HAVE_EPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
2248 if (PyType_Ready(&pyEpoll_Type) < 0)
2249 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 Py_INCREF(&pyEpoll_Type);
2252 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);
2255 PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);
2256 PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);
2257 PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);
2258 PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);
2259 PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002260#ifdef EPOLLONESHOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 /* Kernel 2.6.2+ */
2262 PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002263#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
2265 PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);
2266 PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);
2267 PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);
2268 PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);
2269 PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002270
Benjamin Peterson95c16622011-12-27 15:36:32 -06002271#ifdef EPOLL_CLOEXEC
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002272 PyModule_AddIntConstant(m, "EPOLL_CLOEXEC", EPOLL_CLOEXEC);
Benjamin Peterson95c16622011-12-27 15:36:32 -06002273#endif
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002274#endif /* HAVE_EPOLL */
2275
2276#ifdef HAVE_KQUEUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 kqueue_event_Type.tp_new = PyType_GenericNew;
2278 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
2279 if(PyType_Ready(&kqueue_event_Type) < 0)
2280 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 Py_INCREF(&kqueue_event_Type);
2283 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002285 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
2286 if(PyType_Ready(&kqueue_queue_Type) < 0)
2287 return NULL;
2288 Py_INCREF(&kqueue_queue_Type);
2289 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
2290
2291 /* event filters */
2292 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
2293 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
2294 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
2295 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
2296 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002297#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002299#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
2301 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 /* event flags */
2304 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
2305 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
2306 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
2307 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
2308 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
2309 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
2312 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
2315 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 /* READ WRITE filter flag */
2318 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 /* VNODE filter flags */
2321 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
2322 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
2323 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
2324 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
2325 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
2326 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
2327 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002329 /* PROC filter flags */
2330 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
2331 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
2332 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
2333 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
2334 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
2337 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
2338 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
2339
2340 /* NETDEV filter flags */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002341#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
2343 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
2344 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002345#endif
2346
2347#endif /* HAVE_KQUEUE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 return m;
Guido van Rossumed233a51992-06-23 09:07:03 +00002349}