blob: c492224ecbcf32a4172f6ac4502b2cde220ca1ad [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 {
Georg Brandl381c2802014-01-25 09:11:13 +01001382 timeout = (int)(dtimeout * 1000.0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 if (maxevents == -1) {
1386 maxevents = FD_SETSIZE-1;
1387 }
1388 else if (maxevents < 1) {
1389 PyErr_Format(PyExc_ValueError,
1390 "maxevents must be greater than 0, got %d",
1391 maxevents);
1392 return NULL;
1393 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 evs = PyMem_New(struct epoll_event, maxevents);
1396 if (evs == NULL) {
1397 Py_DECREF(self);
1398 PyErr_NoMemory();
1399 return NULL;
1400 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 Py_BEGIN_ALLOW_THREADS
1403 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
1404 Py_END_ALLOW_THREADS
1405 if (nfds < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001406 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 goto error;
1408 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 elist = PyList_New(nfds);
1411 if (elist == NULL) {
1412 goto error;
1413 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 for (i = 0; i < nfds; i++) {
1416 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
1417 if (etuple == NULL) {
1418 Py_CLEAR(elist);
1419 goto error;
1420 }
1421 PyList_SET_ITEM(elist, i, etuple);
1422 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001423
Christian Heimesf6cd9672008-03-26 13:45:42 +00001424 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyMem_Free(evs);
1426 return elist;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001427}
1428
1429PyDoc_STRVAR(pyepoll_poll_doc,
1430"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1431\n\
1432Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1433in seconds (as float). -1 makes poll wait indefinitely.\n\
1434Up to maxevents are returned to the caller.");
1435
1436static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 {"fromfd", (PyCFunction)pyepoll_fromfd,
1438 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1439 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1440 pyepoll_close_doc},
1441 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1442 pyepoll_fileno_doc},
1443 {"modify", (PyCFunction)pyepoll_modify,
1444 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1445 {"register", (PyCFunction)pyepoll_register,
1446 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1447 {"unregister", (PyCFunction)pyepoll_unregister,
1448 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1449 {"poll", (PyCFunction)pyepoll_poll,
1450 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
1451 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001452};
1453
1454static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 {"closed", (getter)pyepoll_get_closed, NULL,
1456 "True if the epoll handler is closed"},
1457 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001458};
1459
1460PyDoc_STRVAR(pyepoll_doc,
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001461"select.epoll(sizehint=-1, flags=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001462\n\
1463Returns an epolling object\n\
1464\n\
1465sizehint must be a positive integer or -1 for the default size. The\n\
1466sizehint is used to optimize internal data structures. It doesn't limit\n\
1467the maximum number of monitored events.");
1468
1469static PyTypeObject pyEpoll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 PyVarObject_HEAD_INIT(NULL, 0)
1471 "select.epoll", /* tp_name */
1472 sizeof(pyEpoll_Object), /* tp_basicsize */
1473 0, /* tp_itemsize */
1474 (destructor)pyepoll_dealloc, /* tp_dealloc */
1475 0, /* tp_print */
1476 0, /* tp_getattr */
1477 0, /* tp_setattr */
1478 0, /* tp_reserved */
1479 0, /* tp_repr */
1480 0, /* tp_as_number */
1481 0, /* tp_as_sequence */
1482 0, /* tp_as_mapping */
1483 0, /* tp_hash */
1484 0, /* tp_call */
1485 0, /* tp_str */
1486 PyObject_GenericGetAttr, /* tp_getattro */
1487 0, /* tp_setattro */
1488 0, /* tp_as_buffer */
1489 Py_TPFLAGS_DEFAULT, /* tp_flags */
1490 pyepoll_doc, /* tp_doc */
1491 0, /* tp_traverse */
1492 0, /* tp_clear */
1493 0, /* tp_richcompare */
1494 0, /* tp_weaklistoffset */
1495 0, /* tp_iter */
1496 0, /* tp_iternext */
1497 pyepoll_methods, /* tp_methods */
1498 0, /* tp_members */
1499 pyepoll_getsetlist, /* tp_getset */
1500 0, /* tp_base */
1501 0, /* tp_dict */
1502 0, /* tp_descr_get */
1503 0, /* tp_descr_set */
1504 0, /* tp_dictoffset */
1505 0, /* tp_init */
1506 0, /* tp_alloc */
1507 pyepoll_new, /* tp_new */
1508 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001509};
1510
1511#endif /* HAVE_EPOLL */
1512
1513#ifdef HAVE_KQUEUE
1514/* **************************************************************************
1515 * kqueue interface for BSD
1516 *
1517 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1518 * All rights reserved.
1519 *
1520 * Redistribution and use in source and binary forms, with or without
1521 * modification, are permitted provided that the following conditions
1522 * are met:
1523 * 1. Redistributions of source code must retain the above copyright
1524 * notice, this list of conditions and the following disclaimer.
1525 * 2. Redistributions in binary form must reproduce the above copyright
1526 * notice, this list of conditions and the following disclaimer in the
1527 * documentation and/or other materials provided with the distribution.
1528 *
1529 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1530 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1531 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1532 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1533 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1534 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1535 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1536 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1537 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1538 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1539 * SUCH DAMAGE.
1540 */
1541
1542#ifdef HAVE_SYS_EVENT_H
1543#include <sys/event.h>
1544#endif
1545
1546PyDoc_STRVAR(kqueue_event_doc,
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001547"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001548\n\
1549This object is the equivalent of the struct kevent for the C API.\n\
1550\n\
1551See the kqueue manpage for more detailed information about the meaning\n\
1552of the arguments.\n\
1553\n\
1554One minor note: while you might hope that udata could store a\n\
1555reference to a python object, it cannot, because it is impossible to\n\
1556keep a proper reference count of the object once it's passed into the\n\
1557kernel. Therefore, I have restricted it to only storing an integer. I\n\
1558recommend ignoring it and simply using the 'ident' field to key off\n\
1559of. You could also set up a dictionary on the python side to store a\n\
1560udata->object mapping.");
1561
1562typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 PyObject_HEAD
1564 struct kevent e;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001565} kqueue_event_Object;
1566
1567static PyTypeObject kqueue_event_Type;
1568
1569#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1570
1571typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 PyObject_HEAD
1573 SOCKET kqfd; /* kqueue control fd */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001574} kqueue_queue_Object;
1575
1576static PyTypeObject kqueue_queue_Type;
1577
1578#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1579
Antoine Pitroud83f1e62009-11-04 21:10:38 +00001580#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1581# error uintptr_t does not match void *!
1582#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1583# define T_UINTPTRT T_ULONGLONG
1584# define T_INTPTRT T_LONGLONG
1585# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1586# define UINTPTRT_FMT_UNIT "K"
1587# define INTPTRT_FMT_UNIT "L"
1588#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1589# define T_UINTPTRT T_ULONG
1590# define T_INTPTRT T_LONG
1591# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1592# define UINTPTRT_FMT_UNIT "k"
1593# define INTPTRT_FMT_UNIT "l"
1594#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1595# define T_UINTPTRT T_UINT
1596# define T_INTPTRT T_INT
1597# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1598# define UINTPTRT_FMT_UNIT "I"
1599# define INTPTRT_FMT_UNIT "i"
1600#else
1601# error uintptr_t does not match int, long, or long long!
1602#endif
1603
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001604/*
1605 * kevent is not standard and its members vary across BSDs.
1606 */
1607#if !defined(__OpenBSD__)
1608# define IDENT_TYPE T_UINTPTRT
1609# define IDENT_CAST Py_intptr_t
1610# define DATA_TYPE T_INTPTRT
1611# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
1612# define IDENT_AsType PyLong_AsUintptr_t
1613#else
1614# define IDENT_TYPE T_UINT
1615# define IDENT_CAST int
1616# define DATA_TYPE T_INT
1617# define DATA_FMT_UNIT "i"
1618# define IDENT_AsType PyLong_AsUnsignedLong
1619#endif
1620
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001621/* Unfortunately, we can't store python objects in udata, because
1622 * kevents in the kernel can be removed without warning, which would
1623 * forever lose the refcount on the object stored with it.
1624 */
1625
1626#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1627static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001628 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 {"filter", T_SHORT, KQ_OFF(e.filter)},
1630 {"flags", T_USHORT, KQ_OFF(e.flags)},
1631 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001632 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1634 {NULL} /* Sentinel */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001635};
1636#undef KQ_OFF
1637
1638static PyObject *
Georg Brandlc0e22b72010-03-14 10:51:01 +00001639
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001640kqueue_event_repr(kqueue_event_Object *s)
1641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 char buf[1024];
1643 PyOS_snprintf(
1644 buf, sizeof(buf),
1645 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1646 "data=0x%zd udata=%p>",
1647 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1648 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1649 return PyUnicode_FromString(buf);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001650}
1651
1652static int
1653kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 PyObject *pfd;
1656 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1657 "data", "udata", NULL};
Christian Heimesf1fe1592013-08-25 14:57:00 +02001658 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1663 &pfd, &(self->e.filter), &(self->e.flags),
1664 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1665 return -1;
1666 }
1667
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001668 if (PyLong_Check(pfd)
1669#if IDENT_TYPE == T_UINT
1670 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
1671#endif
1672 ) {
1673 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 }
1675 else {
1676 self->e.ident = PyObject_AsFileDescriptor(pfd);
1677 }
1678 if (PyErr_Occurred()) {
1679 return -1;
1680 }
1681 return 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001682}
1683
1684static PyObject *
1685kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 int op)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 Py_intptr_t result = 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 if (!kqueue_event_Check(o)) {
1691 if (op == Py_EQ || op == Py_NE) {
1692 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1693 Py_INCREF(res);
1694 return res;
1695 }
1696 PyErr_Format(PyExc_TypeError,
1697 "can't compare %.200s to %.200s",
1698 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1699 return NULL;
1700 }
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001701 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 ((result = s->e.filter - o->e.filter) == 0) &&
1703 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001704 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 ((result = s->e.data - o->e.data) == 0) &&
1706 ((result = s->e.udata - o->e.udata) == 0)
1707 ) {
1708 result = 0;
1709 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 switch (op) {
1712 case Py_EQ:
1713 result = (result == 0);
1714 break;
1715 case Py_NE:
1716 result = (result != 0);
1717 break;
1718 case Py_LE:
1719 result = (result <= 0);
1720 break;
1721 case Py_GE:
1722 result = (result >= 0);
1723 break;
1724 case Py_LT:
1725 result = (result < 0);
1726 break;
1727 case Py_GT:
1728 result = (result > 0);
1729 break;
1730 }
1731 return PyBool_FromLong((long)result);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001732}
1733
1734static PyTypeObject kqueue_event_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 PyVarObject_HEAD_INIT(NULL, 0)
1736 "select.kevent", /* tp_name */
1737 sizeof(kqueue_event_Object), /* tp_basicsize */
1738 0, /* tp_itemsize */
1739 0, /* tp_dealloc */
1740 0, /* tp_print */
1741 0, /* tp_getattr */
1742 0, /* tp_setattr */
1743 0, /* tp_reserved */
1744 (reprfunc)kqueue_event_repr, /* tp_repr */
1745 0, /* tp_as_number */
1746 0, /* tp_as_sequence */
1747 0, /* tp_as_mapping */
1748 0, /* tp_hash */
1749 0, /* tp_call */
1750 0, /* tp_str */
1751 0, /* tp_getattro */
1752 0, /* tp_setattro */
1753 0, /* tp_as_buffer */
1754 Py_TPFLAGS_DEFAULT, /* tp_flags */
1755 kqueue_event_doc, /* tp_doc */
1756 0, /* tp_traverse */
1757 0, /* tp_clear */
1758 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1759 0, /* tp_weaklistoffset */
1760 0, /* tp_iter */
1761 0, /* tp_iternext */
1762 0, /* tp_methods */
1763 kqueue_event_members, /* tp_members */
1764 0, /* tp_getset */
1765 0, /* tp_base */
1766 0, /* tp_dict */
1767 0, /* tp_descr_get */
1768 0, /* tp_descr_set */
1769 0, /* tp_dictoffset */
1770 (initproc)kqueue_event_init, /* tp_init */
1771 0, /* tp_alloc */
1772 0, /* tp_new */
1773 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001774};
1775
1776static PyObject *
1777kqueue_queue_err_closed(void)
1778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue fd");
1780 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001781}
1782
1783static int
1784kqueue_queue_internal_close(kqueue_queue_Object *self)
1785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 int save_errno = 0;
1787 if (self->kqfd >= 0) {
1788 int kqfd = self->kqfd;
1789 self->kqfd = -1;
1790 Py_BEGIN_ALLOW_THREADS
1791 if (close(kqfd) < 0)
1792 save_errno = errno;
1793 Py_END_ALLOW_THREADS
1794 }
1795 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001796}
1797
1798static PyObject *
1799newKqueue_Object(PyTypeObject *type, SOCKET fd)
1800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 kqueue_queue_Object *self;
1802 assert(type != NULL && type->tp_alloc != NULL);
1803 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1804 if (self == NULL) {
1805 return NULL;
1806 }
1807
1808 if (fd == -1) {
1809 Py_BEGIN_ALLOW_THREADS
1810 self->kqfd = kqueue();
1811 Py_END_ALLOW_THREADS
1812 }
1813 else {
1814 self->kqfd = fd;
1815 }
1816 if (self->kqfd < 0) {
1817 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001818 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 return NULL;
1820 }
1821 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001822}
1823
1824static PyObject *
1825kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1826{
1827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 if ((args != NULL && PyObject_Size(args)) ||
1829 (kwds != NULL && PyObject_Size(kwds))) {
1830 PyErr_SetString(PyExc_ValueError,
1831 "select.kqueue doesn't accept arguments");
1832 return NULL;
1833 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 return newKqueue_Object(type, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001836}
1837
1838static void
1839kqueue_queue_dealloc(kqueue_queue_Object *self)
1840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 kqueue_queue_internal_close(self);
1842 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001843}
1844
1845static PyObject*
1846kqueue_queue_close(kqueue_queue_Object *self)
1847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 errno = kqueue_queue_internal_close(self);
1849 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001850 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 return NULL;
1852 }
1853 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001854}
1855
1856PyDoc_STRVAR(kqueue_queue_close_doc,
1857"close() -> None\n\
1858\n\
1859Close the kqueue control file descriptor. Further operations on the kqueue\n\
1860object will raise an exception.");
1861
1862static PyObject*
1863kqueue_queue_get_closed(kqueue_queue_Object *self)
1864{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 if (self->kqfd < 0)
1866 Py_RETURN_TRUE;
1867 else
1868 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001869}
1870
1871static PyObject*
1872kqueue_queue_fileno(kqueue_queue_Object *self)
1873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (self->kqfd < 0)
1875 return kqueue_queue_err_closed();
1876 return PyLong_FromLong(self->kqfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001877}
1878
1879PyDoc_STRVAR(kqueue_queue_fileno_doc,
1880"fileno() -> int\n\
1881\n\
1882Return the kqueue control file descriptor.");
1883
1884static PyObject*
1885kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1890 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001893}
1894
1895PyDoc_STRVAR(kqueue_queue_fromfd_doc,
1896"fromfd(fd) -> kqueue\n\
1897\n\
1898Create a kqueue object from a given control fd.");
1899
1900static PyObject *
1901kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
1902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 int nevents = 0;
1904 int gotevents = 0;
1905 int nchanges = 0;
1906 int i = 0;
1907 PyObject *otimeout = NULL;
1908 PyObject *ch = NULL;
1909 PyObject *it = NULL, *ei = NULL;
1910 PyObject *result = NULL;
1911 struct kevent *evl = NULL;
1912 struct kevent *chl = NULL;
Victor Stinnerd327f9d2012-03-13 15:29:08 +01001913 struct timespec timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 struct timespec *ptimeoutspec;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 if (self->kqfd < 0)
1917 return kqueue_queue_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
1920 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 if (nevents < 0) {
1923 PyErr_Format(PyExc_ValueError,
1924 "Length of eventlist must be 0 or positive, got %d",
1925 nevents);
1926 return NULL;
1927 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 if (otimeout == Py_None || otimeout == NULL) {
1930 ptimeoutspec = NULL;
1931 }
1932 else if (PyNumber_Check(otimeout)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +01001933 if (_PyTime_ObjectToTimespec(otimeout,
1934 &timeout.tv_sec, &timeout.tv_nsec) == -1)
1935 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001936
Victor Stinner5d272cc2012-03-13 13:35:55 +01001937 if (timeout.tv_sec < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 PyErr_SetString(PyExc_ValueError,
1939 "timeout must be positive or None");
1940 return NULL;
1941 }
Victor Stinnerd528b012012-03-13 16:25:35 +01001942 ptimeoutspec = &timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 }
1944 else {
1945 PyErr_Format(PyExc_TypeError,
1946 "timeout argument must be an number "
1947 "or None, got %.200s",
1948 Py_TYPE(otimeout)->tp_name);
1949 return NULL;
1950 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (ch != NULL && ch != Py_None) {
1953 it = PyObject_GetIter(ch);
1954 if (it == NULL) {
1955 PyErr_SetString(PyExc_TypeError,
1956 "changelist is not iterable");
1957 return NULL;
1958 }
1959 nchanges = PyObject_Size(ch);
1960 if (nchanges < 0) {
1961 goto error;
1962 }
Georg Brandlc0e22b72010-03-14 10:51:01 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 chl = PyMem_New(struct kevent, nchanges);
1965 if (chl == NULL) {
1966 PyErr_NoMemory();
1967 goto error;
1968 }
1969 i = 0;
1970 while ((ei = PyIter_Next(it)) != NULL) {
1971 if (!kqueue_event_Check(ei)) {
1972 Py_DECREF(ei);
1973 PyErr_SetString(PyExc_TypeError,
1974 "changelist must be an iterable of "
1975 "select.kevent objects");
1976 goto error;
1977 } else {
1978 chl[i++] = ((kqueue_event_Object *)ei)->e;
1979 }
1980 Py_DECREF(ei);
1981 }
1982 }
1983 Py_CLEAR(it);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 /* event list */
1986 if (nevents) {
1987 evl = PyMem_New(struct kevent, nevents);
1988 if (evl == NULL) {
1989 PyErr_NoMemory();
1990 goto error;
1991 }
1992 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 Py_BEGIN_ALLOW_THREADS
1995 gotevents = kevent(self->kqfd, chl, nchanges,
1996 evl, nevents, ptimeoutspec);
1997 Py_END_ALLOW_THREADS
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 if (gotevents == -1) {
2000 PyErr_SetFromErrno(PyExc_OSError);
2001 goto error;
2002 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004 result = PyList_New(gotevents);
2005 if (result == NULL) {
2006 goto error;
2007 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 for (i = 0; i < gotevents; i++) {
2010 kqueue_event_Object *ch;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
2013 if (ch == NULL) {
2014 goto error;
2015 }
2016 ch->e = evl[i];
2017 PyList_SET_ITEM(result, i, (PyObject *)ch);
2018 }
2019 PyMem_Free(chl);
2020 PyMem_Free(evl);
2021 return result;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002022
2023 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 PyMem_Free(chl);
2025 PyMem_Free(evl);
2026 Py_XDECREF(result);
2027 Py_XDECREF(it);
2028 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002029}
2030
2031PyDoc_STRVAR(kqueue_queue_control_doc,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00002032"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002033\n\
2034Calls the kernel kevent function.\n\
2035- changelist must be a list of kevent objects describing the changes\n\
2036 to be made to the kernel's watch list or None.\n\
2037- max_events lets you specify the maximum number of events that the\n\
2038 kernel will return.\n\
2039- timeout is the maximum time to wait in seconds, or else None,\n\
2040 to wait forever. timeout accepts floats for smaller timeouts, too.");
2041
2042
2043static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
2045 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
2046 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
2047 kqueue_queue_close_doc},
2048 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
2049 kqueue_queue_fileno_doc},
2050 {"control", (PyCFunction)kqueue_queue_control,
2051 METH_VARARGS , kqueue_queue_control_doc},
2052 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002053};
2054
2055static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 {"closed", (getter)kqueue_queue_get_closed, NULL,
2057 "True if the kqueue handler is closed"},
2058 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002059};
2060
2061PyDoc_STRVAR(kqueue_queue_doc,
2062"Kqueue syscall wrapper.\n\
2063\n\
2064For example, to start watching a socket for input:\n\
2065>>> kq = kqueue()\n\
2066>>> sock = socket()\n\
2067>>> sock.connect((host, port))\n\
2068>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
2069\n\
2070To wait one second for it to become writeable:\n\
2071>>> kq.control(None, 1, 1000)\n\
2072\n\
2073To stop listening:\n\
2074>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
2075
2076static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 PyVarObject_HEAD_INIT(NULL, 0)
2078 "select.kqueue", /* tp_name */
2079 sizeof(kqueue_queue_Object), /* tp_basicsize */
2080 0, /* tp_itemsize */
2081 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
2082 0, /* tp_print */
2083 0, /* tp_getattr */
2084 0, /* tp_setattr */
2085 0, /* tp_reserved */
2086 0, /* tp_repr */
2087 0, /* tp_as_number */
2088 0, /* tp_as_sequence */
2089 0, /* tp_as_mapping */
2090 0, /* tp_hash */
2091 0, /* tp_call */
2092 0, /* tp_str */
2093 0, /* tp_getattro */
2094 0, /* tp_setattro */
2095 0, /* tp_as_buffer */
2096 Py_TPFLAGS_DEFAULT, /* tp_flags */
2097 kqueue_queue_doc, /* tp_doc */
2098 0, /* tp_traverse */
2099 0, /* tp_clear */
2100 0, /* tp_richcompare */
2101 0, /* tp_weaklistoffset */
2102 0, /* tp_iter */
2103 0, /* tp_iternext */
2104 kqueue_queue_methods, /* tp_methods */
2105 0, /* tp_members */
2106 kqueue_queue_getsetlist, /* tp_getset */
2107 0, /* tp_base */
2108 0, /* tp_dict */
2109 0, /* tp_descr_get */
2110 0, /* tp_descr_set */
2111 0, /* tp_dictoffset */
2112 0, /* tp_init */
2113 0, /* tp_alloc */
2114 kqueue_queue_new, /* tp_new */
2115 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002116};
2117
2118#endif /* HAVE_KQUEUE */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002119
2120
2121
2122
2123
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002124/* ************************************************************************ */
2125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002126PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002127"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
2128\n\
2129Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00002130The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002131rlist -- wait until ready for reading\n\
2132wlist -- wait until ready for writing\n\
2133xlist -- wait for an ``exceptional condition''\n\
2134If only one kind of condition is required, pass [] for the other lists.\n\
2135A file descriptor is either a socket or file object, or a small integer\n\
2136gotten from a fileno() method call on one of those.\n\
2137\n\
2138The optional 4th argument specifies a timeout in seconds; it may be\n\
2139a floating point number to specify fractions of seconds. If it is absent\n\
2140or None, the call will never time out.\n\
2141\n\
2142The return value is a tuple of three lists corresponding to the first three\n\
2143arguments; each contains the subset of the corresponding file descriptors\n\
2144that are ready.\n\
2145\n\
2146*** IMPORTANT NOTICE ***\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002147On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00002148descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002149
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002150static PyMethodDef select_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natali986a56c2013-01-19 12:19:10 +01002152#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002153 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002154#endif /* HAVE_POLL */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002155#ifdef HAVE_SYS_DEVPOLL_H
2156 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2157#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00002159};
2160
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002161PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002162"This module supports asynchronous I/O on multiple file descriptors.\n\
2163\n\
2164*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002165On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00002166
Martin v. Löwis1a214512008-06-11 05:26:20 +00002167
2168static struct PyModuleDef selectmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 PyModuleDef_HEAD_INIT,
2170 "select",
2171 module_doc,
2172 -1,
2173 select_methods,
2174 NULL,
2175 NULL,
2176 NULL,
2177 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002178};
2179
Jesus Cead8b9ae62011-11-14 19:07:41 +01002180
2181
2182
Mark Hammond62b1ab12002-07-23 06:31:15 +00002183PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002184PyInit_select(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00002185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 PyObject *m;
2187 m = PyModule_Create(&selectmodule);
2188 if (m == NULL)
2189 return NULL;
Fred Drake4baedc12002-04-01 14:53:37 +00002190
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002191 Py_INCREF(PyExc_OSError);
2192 PyModule_AddObject(m, "error", PyExc_OSError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002193
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002194#ifdef PIPE_BUF
R. David Murraye16cda92010-10-15 23:12:57 +00002195#ifdef HAVE_BROKEN_PIPE_BUF
2196#undef PIPE_BUF
2197#define PIPE_BUF 512
2198#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF);
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002200#endif
Gregory P. Smithb970b862009-07-04 02:28:47 +00002201
Charles-François Natali986a56c2013-01-19 12:19:10 +01002202#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002203#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 if (select_have_broken_poll()) {
2205 if (PyObject_DelAttrString(m, "poll") == -1) {
2206 PyErr_Clear();
2207 }
2208 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002209#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002211#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 if (PyType_Ready(&poll_Type) < 0)
2213 return NULL;
2214 PyModule_AddIntConstant(m, "POLLIN", POLLIN);
2215 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
2216 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
2217 PyModule_AddIntConstant(m, "POLLERR", POLLERR);
2218 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
2219 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00002220
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002221#ifdef POLLRDNORM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002223#endif
2224#ifdef POLLRDBAND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002226#endif
2227#ifdef POLLWRNORM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002229#endif
2230#ifdef POLLWRBAND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002232#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002233#ifdef POLLMSG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002235#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002237#endif /* HAVE_POLL */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002238
Jesus Cead8b9ae62011-11-14 19:07:41 +01002239#ifdef HAVE_SYS_DEVPOLL_H
2240 if (PyType_Ready(&devpoll_Type) < 0)
2241 return NULL;
2242#endif
2243
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002244#ifdef HAVE_EPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
2246 if (PyType_Ready(&pyEpoll_Type) < 0)
2247 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 Py_INCREF(&pyEpoll_Type);
2250 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);
2253 PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);
2254 PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);
2255 PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);
2256 PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);
2257 PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002258#ifdef EPOLLONESHOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 /* Kernel 2.6.2+ */
2260 PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002261#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
2263 PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);
2264 PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);
2265 PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);
2266 PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);
2267 PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002268
Benjamin Peterson95c16622011-12-27 15:36:32 -06002269#ifdef EPOLL_CLOEXEC
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002270 PyModule_AddIntConstant(m, "EPOLL_CLOEXEC", EPOLL_CLOEXEC);
Benjamin Peterson95c16622011-12-27 15:36:32 -06002271#endif
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002272#endif /* HAVE_EPOLL */
2273
2274#ifdef HAVE_KQUEUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 kqueue_event_Type.tp_new = PyType_GenericNew;
2276 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
2277 if(PyType_Ready(&kqueue_event_Type) < 0)
2278 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 Py_INCREF(&kqueue_event_Type);
2281 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
2284 if(PyType_Ready(&kqueue_queue_Type) < 0)
2285 return NULL;
2286 Py_INCREF(&kqueue_queue_Type);
2287 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
2288
2289 /* event filters */
2290 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
2291 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
2292 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
2293 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
2294 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002295#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002297#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
2299 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 /* event flags */
2302 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
2303 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
2304 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
2305 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
2306 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
2307 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
2310 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
2313 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 /* READ WRITE filter flag */
2316 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 /* VNODE filter flags */
2319 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
2320 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
2321 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
2322 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
2323 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
2324 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
2325 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 /* PROC filter flags */
2328 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
2329 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
2330 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
2331 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
2332 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
2335 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
2336 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
2337
2338 /* NETDEV filter flags */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002339#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
2341 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
2342 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002343#endif
2344
2345#endif /* HAVE_KQUEUE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 return m;
Guido van Rossumed233a51992-06-23 09:07:03 +00002347}