blob: 952a0919d4ba3d2cd170597165ac2b8fa75c47b1 [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++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +020077 Py_CLEAR(fd2obj[i].obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 }
79 fd2obj[0].sentinel = -1;
Barry Warsawc1cb3601996-12-12 22:16:21 +000080}
81
82
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000083/* returns -1 and sets the Python exception if an error occurred, otherwise
84 returns a number >= 0
85*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000086static int
Brett Cannon62dba4c2003-09-10 19:37:42 +000087seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +000088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 int max = -1;
90 int index = 0;
Antoine Pitroue4ad37e2012-11-01 20:13:54 +010091 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 PyObject* fast_seq = NULL;
93 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
96 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000097
Benjamin Petersone0edb8b2010-06-27 23:49:45 +000098 fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 if (!fast_seq)
100 return -1;
101
Antoine Pitroue4ad37e2012-11-01 20:13:54 +0100102 for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 SOCKET v;
104
105 /* any intervening fileno() calls could decr this refcnt */
106 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
Jesus Cea62a5c322012-07-19 21:31:26 +0200107 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 Py_INCREF(o);
110 v = PyObject_AsFileDescriptor( o );
111 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000112
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000113#if defined(_MSC_VER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 max = 0; /* not used for Win32 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000115#else /* !_MSC_VER */
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200116 if (!_PyIsSelectable_fd(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 PyErr_SetString(PyExc_ValueError,
118 "filedescriptor out of range in select()");
119 goto finally;
120 }
121 if (v > max)
122 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000123#endif /* _MSC_VER */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 /* add object and its file descriptor to the list */
127 if (index >= FD_SETSIZE) {
128 PyErr_SetString(PyExc_ValueError,
129 "too many file descriptors in select()");
130 goto finally;
131 }
132 fd2obj[index].obj = o;
133 fd2obj[index].fd = v;
134 fd2obj[index].sentinel = 0;
135 fd2obj[++index].sentinel = -1;
136 }
137 Py_DECREF(fast_seq);
138 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000139
140 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 Py_XDECREF(o);
142 Py_DECREF(fast_seq);
143 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000144}
145
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000146/* returns NULL and sets the Python exception if an error occurred */
147static PyObject *
Tim Peters4b046c22001-08-16 21:59:46 +0000148set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +0000149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 int i, j, count=0;
151 PyObject *list, *o;
152 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
155 if (FD_ISSET(fd2obj[j].fd, set))
156 count++;
157 }
158 list = PyList_New(count);
159 if (!list)
160 return NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 i = 0;
163 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
164 fd = fd2obj[j].fd;
165 if (FD_ISSET(fd, set)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 o = fd2obj[j].obj;
167 fd2obj[j].obj = NULL;
168 /* transfer ownership */
169 if (PyList_SetItem(list, i, o) < 0)
170 goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 i++;
173 }
174 }
175 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000176 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 Py_DECREF(list);
178 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000179}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000180
Barry Warsawb44740f2001-08-16 16:52:59 +0000181#undef SELECT_USES_HEAP
182#if FD_SETSIZE > 1024
183#define SELECT_USES_HEAP
184#endif /* FD_SETSIZE > 1024 */
185
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000186static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000187select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000188{
Barry Warsawb44740f2001-08-16 16:52:59 +0000189#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 pylist *rfd2obj, *wfd2obj, *efd2obj;
Barry Warsawb44740f2001-08-16 16:52:59 +0000191#else /* !SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 /* XXX: All this should probably be implemented as follows:
193 * - find the highest descriptor we're interested in
194 * - add one
195 * - that's the size
196 * See: Stevens, APitUE, $12.5.1
197 */
198 pylist rfd2obj[FD_SETSIZE + 1];
199 pylist wfd2obj[FD_SETSIZE + 1];
200 pylist efd2obj[FD_SETSIZE + 1];
Barry Warsawb44740f2001-08-16 16:52:59 +0000201#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 PyObject *ifdlist, *ofdlist, *efdlist;
203 PyObject *ret = NULL;
204 PyObject *tout = Py_None;
205 fd_set ifdset, ofdset, efdset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 struct timeval tv, *tvp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 int imax, omax, emax, max;
208 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 /* convert arguments */
211 if (!PyArg_UnpackTuple(args, "select", 3, 4,
212 &ifdlist, &ofdlist, &efdlist, &tout))
213 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if (tout == Py_None)
216 tvp = (struct timeval *)0;
217 else if (!PyNumber_Check(tout)) {
218 PyErr_SetString(PyExc_TypeError,
219 "timeout must be a float or None");
220 return NULL;
221 }
222 else {
Victor Stinnerb2a37732012-03-14 00:20:51 +0100223#ifdef MS_WINDOWS
224 time_t sec;
225 if (_PyTime_ObjectToTimeval(tout, &sec, &tv.tv_usec) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 return NULL;
Victor Stinnerb2a37732012-03-14 00:20:51 +0100227 assert(sizeof(tv.tv_sec) == sizeof(long));
228#if SIZEOF_TIME_T > SIZEOF_LONG
229 if (sec > LONG_MAX) {
230 PyErr_SetString(PyExc_OverflowError,
231 "timeout is too large");
232 return NULL;
233 }
234#endif
235 tv.tv_sec = (long)sec;
236#else
Brett Cannon8798ad32012-04-07 14:59:29 -0400237 /* 64-bit OS X has struct timeval.tv_usec as an int (and thus still 4
238 bytes as required), but no longer defined by a long. */
Benjamin Peterson6f3e5e42012-09-11 12:05:05 -0400239 long tv_usec;
Brett Cannon8798ad32012-04-07 14:59:29 -0400240 if (_PyTime_ObjectToTimeval(tout, &tv.tv_sec, &tv_usec) == -1)
Victor Stinnerb2a37732012-03-14 00:20:51 +0100241 return NULL;
Brett Cannon8798ad32012-04-07 14:59:29 -0400242 tv.tv_usec = tv_usec;
Victor Stinnerb2a37732012-03-14 00:20:51 +0100243#endif
Victor Stinner5d272cc2012-03-13 13:35:55 +0100244 if (tv.tv_sec < 0) {
245 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 return NULL;
247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 tvp = &tv;
249 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000250
Guido van Rossumed233a51992-06-23 09:07:03 +0000251
Barry Warsawb44740f2001-08-16 16:52:59 +0000252#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 /* Allocate memory for the lists */
254 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
255 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
256 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
257 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
258 if (rfd2obj) PyMem_DEL(rfd2obj);
259 if (wfd2obj) PyMem_DEL(wfd2obj);
260 if (efd2obj) PyMem_DEL(efd2obj);
261 return PyErr_NoMemory();
262 }
Barry Warsawb44740f2001-08-16 16:52:59 +0000263#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* Convert sequences to fd_sets, and get maximum fd number
265 * propagates the Python exception set in seq2set()
266 */
267 rfd2obj[0].sentinel = -1;
268 wfd2obj[0].sentinel = -1;
269 efd2obj[0].sentinel = -1;
270 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
271 goto finally;
272 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
273 goto finally;
274 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
275 goto finally;
276 max = imax;
277 if (omax > max) max = omax;
278 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 Py_BEGIN_ALLOW_THREADS
281 n = select(max, &ifdset, &ofdset, &efdset, tvp);
282 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000283
Thomas Heller106f4c72002-09-24 16:51:00 +0000284#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (n == SOCKET_ERROR) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200286 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000288#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 if (n < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200290 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000292#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 else {
294 /* any of these three calls can raise an exception. it's more
295 convenient to test for this after all three calls... but
296 is that acceptable?
297 */
298 ifdlist = set2list(&ifdset, rfd2obj);
299 ofdlist = set2list(&ofdset, wfd2obj);
300 efdlist = set2list(&efdset, efd2obj);
301 if (PyErr_Occurred())
302 ret = NULL;
303 else
304 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_DECREF(ifdlist);
307 Py_DECREF(ofdlist);
308 Py_DECREF(efdlist);
309 }
310
Barry Warsawc1cb3601996-12-12 22:16:21 +0000311 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 reap_obj(rfd2obj);
313 reap_obj(wfd2obj);
314 reap_obj(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000315#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 PyMem_DEL(rfd2obj);
317 PyMem_DEL(wfd2obj);
318 PyMem_DEL(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000319#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000321}
322
Nicholas Bastine62c5c82004-03-21 23:45:42 +0000323#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324/*
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000325 * poll() support
326 */
327
328typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 PyObject_HEAD
330 PyObject *dict;
331 int ufd_uptodate;
332 int ufd_len;
333 struct pollfd *ufds;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300334 int poll_running;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000335} pollObject;
336
Jeremy Hylton938ace62002-07-17 16:30:39 +0000337static PyTypeObject poll_Type;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339/* Update the malloc'ed array of pollfds to match the dictionary
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000340 contained within a pollObject. Return 1 on success, 0 on an error.
341*/
342
343static int
344update_ufd_array(pollObject *self)
345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 Py_ssize_t i, pos;
347 PyObject *key, *value;
348 struct pollfd *old_ufds = self->ufds;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 self->ufd_len = PyDict_Size(self->dict);
351 PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
352 if (self->ufds == NULL) {
353 self->ufds = old_ufds;
354 PyErr_NoMemory();
355 return 0;
356 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 i = pos = 0;
359 while (PyDict_Next(self->dict, &pos, &key, &value)) {
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200360 assert(i < self->ufd_len);
361 /* Never overflow */
362 self->ufds[i].fd = (int)PyLong_AsLong(key);
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200363 self->ufds[i].events = (short)(unsigned short)PyLong_AsLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 i++;
365 }
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200366 assert(i == self->ufd_len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 self->ufd_uptodate = 1;
368 return 1;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000369}
370
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200371static int
372ushort_converter(PyObject *obj, void *ptr)
373{
374 unsigned long uval;
375
376 uval = PyLong_AsUnsignedLong(obj);
377 if (uval == (unsigned long)-1 && PyErr_Occurred())
378 return 0;
379 if (uval > USHRT_MAX) {
380 PyErr_SetString(PyExc_OverflowError,
381 "Python int too large for C unsigned short");
382 return 0;
383 }
384
385 *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short);
386 return 1;
387}
388
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000389PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000390"register(fd [, eventmask] ) -> None\n\n\
391Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000392fd -- either an integer, or an object with a fileno() method returning an\n\
393 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000394events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000395
396static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397poll_register(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 PyObject *o, *key, *value;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200400 int fd;
401 unsigned short events = POLLIN | POLLPRI | POLLOUT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000403
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200404 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 fd = PyObject_AsFileDescriptor(o);
408 if (fd == -1) return NULL;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* Add entry to the internal dictionary: the key is the
411 file descriptor, and the value is the event mask. */
412 key = PyLong_FromLong(fd);
413 if (key == NULL)
414 return NULL;
415 value = PyLong_FromLong(events);
416 if (value == NULL) {
417 Py_DECREF(key);
418 return NULL;
419 }
420 err = PyDict_SetItem(self->dict, key, value);
421 Py_DECREF(key);
422 Py_DECREF(value);
423 if (err < 0)
424 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 self->ufd_uptodate = 0;
427
428 Py_INCREF(Py_None);
429 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000430}
431
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000432PyDoc_STRVAR(poll_modify_doc,
433"modify(fd, eventmask) -> None\n\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +0000434Modify an already registered file descriptor.\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000435fd -- either an integer, or an object with a fileno() method returning an\n\
436 int.\n\
437events -- an optional bitmask describing the type of events to check for");
438
439static PyObject *
440poll_modify(pollObject *self, PyObject *args)
441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 PyObject *o, *key, *value;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200443 int fd;
444 unsigned short events;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 int err;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000446
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200447 if (!PyArg_ParseTuple(args, "OO&:modify", &o, ushort_converter, &events))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 fd = PyObject_AsFileDescriptor(o);
451 if (fd == -1) return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 /* Modify registered fd */
454 key = PyLong_FromLong(fd);
455 if (key == NULL)
456 return NULL;
457 if (PyDict_GetItem(self->dict, key) == NULL) {
458 errno = ENOENT;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200459 PyErr_SetFromErrno(PyExc_OSError);
Jesus Cea62a5c322012-07-19 21:31:26 +0200460 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return NULL;
462 }
463 value = PyLong_FromLong(events);
464 if (value == NULL) {
465 Py_DECREF(key);
466 return NULL;
467 }
468 err = PyDict_SetItem(self->dict, key, value);
469 Py_DECREF(key);
470 Py_DECREF(value);
471 if (err < 0)
472 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 self->ufd_uptodate = 0;
475
476 Py_INCREF(Py_None);
477 return Py_None;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000478}
479
480
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000481PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000482"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000483Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000484
485static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyObject *key;
489 int fd;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 fd = PyObject_AsFileDescriptor( o );
492 if (fd == -1)
493 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 /* Check whether the fd is already in the array */
496 key = PyLong_FromLong(fd);
497 if (key == NULL)
498 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (PyDict_DelItem(self->dict, key) == -1) {
501 Py_DECREF(key);
502 /* This will simply raise the KeyError set by PyDict_DelItem
503 if the file descriptor isn't registered. */
504 return NULL;
505 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_DECREF(key);
508 self->ufd_uptodate = 0;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 Py_INCREF(Py_None);
511 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000512}
513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000514PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000515"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
516Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000518
519static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520poll_poll(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 PyObject *result_list = NULL, *tout = NULL;
523 int timeout = 0, poll_result, i, j;
524 PyObject *value = NULL, *num = NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
527 return NULL;
528 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 /* Check values for timeout */
531 if (tout == NULL || tout == Py_None)
532 timeout = -1;
533 else if (!PyNumber_Check(tout)) {
534 PyErr_SetString(PyExc_TypeError,
535 "timeout must be an integer or None");
536 return NULL;
537 }
538 else {
539 tout = PyNumber_Long(tout);
540 if (!tout)
541 return NULL;
Serhiy Storchaka441d30f2013-01-19 12:26:26 +0200542 timeout = _PyLong_AsInt(tout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 Py_DECREF(tout);
544 if (timeout == -1 && PyErr_Occurred())
545 return NULL;
546 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000547
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300548 /* Avoid concurrent poll() invocation, issue 8865 */
549 if (self->poll_running) {
550 PyErr_SetString(PyExc_RuntimeError,
551 "concurrent poll() invocation");
552 return NULL;
553 }
554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* Ensure the ufd array is up to date */
556 if (!self->ufd_uptodate)
557 if (update_ufd_array(self) == 0)
558 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000559
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300560 self->poll_running = 1;
561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* call poll() */
563 Py_BEGIN_ALLOW_THREADS
564 poll_result = poll(self->ufds, self->ufd_len, timeout);
565 Py_END_ALLOW_THREADS
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000566
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300567 self->poll_running = 0;
568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 if (poll_result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200570 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 return NULL;
572 }
573
574 /* build the result list */
575
576 result_list = PyList_New(poll_result);
577 if (!result_list)
578 return NULL;
579 else {
580 for (i = 0, j = 0; j < poll_result; j++) {
581 /* skip to the next fired descriptor */
582 while (!self->ufds[i].revents) {
583 i++;
584 }
585 /* if we hit a NULL return, set value to NULL
586 and break out of loop; code at end will
587 clean up result_list */
588 value = PyTuple_New(2);
589 if (value == NULL)
590 goto error;
591 num = PyLong_FromLong(self->ufds[i].fd);
592 if (num == NULL) {
593 Py_DECREF(value);
594 goto error;
595 }
596 PyTuple_SET_ITEM(value, 0, num);
597
598 /* The &0xffff is a workaround for AIX. 'revents'
599 is a 16-bit short, and IBM assigned POLLNVAL
600 to be 0x8000, so the conversion to int results
601 in a negative number. See SF bug #923315. */
602 num = PyLong_FromLong(self->ufds[i].revents & 0xffff);
603 if (num == NULL) {
604 Py_DECREF(value);
605 goto error;
606 }
607 PyTuple_SET_ITEM(value, 1, num);
608 if ((PyList_SetItem(result_list, j, value)) == -1) {
609 Py_DECREF(value);
610 goto error;
611 }
612 i++;
613 }
614 }
615 return result_list;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000616
617 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 Py_DECREF(result_list);
619 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000620}
621
622static PyMethodDef poll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 {"register", (PyCFunction)poll_register,
624 METH_VARARGS, poll_register_doc},
625 {"modify", (PyCFunction)poll_modify,
626 METH_VARARGS, poll_modify_doc},
627 {"unregister", (PyCFunction)poll_unregister,
628 METH_O, poll_unregister_doc},
629 {"poll", (PyCFunction)poll_poll,
630 METH_VARARGS, poll_poll_doc},
631 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000632};
633
634static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000635newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 pollObject *self;
638 self = PyObject_New(pollObject, &poll_Type);
639 if (self == NULL)
640 return NULL;
641 /* ufd_uptodate is a Boolean, denoting whether the
642 array pointed to by ufds matches the contents of the dictionary. */
643 self->ufd_uptodate = 0;
644 self->ufds = NULL;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300645 self->poll_running = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 self->dict = PyDict_New();
647 if (self->dict == NULL) {
648 Py_DECREF(self);
649 return NULL;
650 }
651 return self;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000652}
653
654static void
655poll_dealloc(pollObject *self)
656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 if (self->ufds != NULL)
658 PyMem_DEL(self->ufds);
659 Py_XDECREF(self->dict);
660 PyObject_Del(self);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000661}
662
Tim Peters0c322792002-07-17 16:49:03 +0000663static PyTypeObject poll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 /* The ob_type field must be initialized in the module init function
665 * to be portable to Windows without using C++. */
666 PyVarObject_HEAD_INIT(NULL, 0)
667 "select.poll", /*tp_name*/
668 sizeof(pollObject), /*tp_basicsize*/
669 0, /*tp_itemsize*/
670 /* methods */
671 (destructor)poll_dealloc, /*tp_dealloc*/
672 0, /*tp_print*/
673 0, /*tp_getattr*/
674 0, /*tp_setattr*/
675 0, /*tp_reserved*/
676 0, /*tp_repr*/
677 0, /*tp_as_number*/
678 0, /*tp_as_sequence*/
679 0, /*tp_as_mapping*/
680 0, /*tp_hash*/
681 0, /*tp_call*/
682 0, /*tp_str*/
683 0, /*tp_getattro*/
684 0, /*tp_setattro*/
685 0, /*tp_as_buffer*/
686 Py_TPFLAGS_DEFAULT, /*tp_flags*/
687 0, /*tp_doc*/
688 0, /*tp_traverse*/
689 0, /*tp_clear*/
690 0, /*tp_richcompare*/
691 0, /*tp_weaklistoffset*/
692 0, /*tp_iter*/
693 0, /*tp_iternext*/
694 poll_methods, /*tp_methods*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000695};
696
Jesus Cead8b9ae62011-11-14 19:07:41 +0100697#ifdef HAVE_SYS_DEVPOLL_H
698typedef struct {
699 PyObject_HEAD
700 int fd_devpoll;
701 int max_n_fds;
702 int n_fds;
703 struct pollfd *fds;
704} devpollObject;
705
706static PyTypeObject devpoll_Type;
707
708static int devpoll_flush(devpollObject *self)
709{
710 int size, n;
711
712 if (!self->n_fds) return 0;
713
714 size = sizeof(struct pollfd)*self->n_fds;
715 self->n_fds = 0;
716
717 Py_BEGIN_ALLOW_THREADS
718 n = write(self->fd_devpoll, self->fds, size);
719 Py_END_ALLOW_THREADS
720
721 if (n == -1 ) {
722 PyErr_SetFromErrno(PyExc_IOError);
723 return -1;
724 }
725 if (n < size) {
726 /*
727 ** Data writed to /dev/poll is a binary data structure. It is not
728 ** clear what to do if a partial write occurred. For now, raise
729 ** an exception and see if we actually found this problem in
730 ** the wild.
731 ** See http://bugs.python.org/issue6397.
732 */
733 PyErr_Format(PyExc_IOError, "failed to write all pollfds. "
734 "Please, report at http://bugs.python.org/. "
735 "Data to report: Size tried: %d, actual size written: %d.",
736 size, n);
737 return -1;
738 }
739 return 0;
740}
741
742static PyObject *
743internal_devpoll_register(devpollObject *self, PyObject *args, int remove)
744{
745 PyObject *o;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200746 int fd;
747 unsigned short events = POLLIN | POLLPRI | POLLOUT;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100748
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200749 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
Jesus Cead8b9ae62011-11-14 19:07:41 +0100750 return NULL;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100751
752 fd = PyObject_AsFileDescriptor(o);
753 if (fd == -1) return NULL;
754
755 if (remove) {
756 self->fds[self->n_fds].fd = fd;
757 self->fds[self->n_fds].events = POLLREMOVE;
758
759 if (++self->n_fds == self->max_n_fds) {
760 if (devpoll_flush(self))
761 return NULL;
762 }
763 }
764
765 self->fds[self->n_fds].fd = fd;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200766 self->fds[self->n_fds].events = (signed short)events;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100767
768 if (++self->n_fds == self->max_n_fds) {
769 if (devpoll_flush(self))
770 return NULL;
771 }
772
773 Py_RETURN_NONE;
774}
775
776PyDoc_STRVAR(devpoll_register_doc,
777"register(fd [, eventmask] ) -> None\n\n\
778Register a file descriptor with the polling object.\n\
779fd -- either an integer, or an object with a fileno() method returning an\n\
780 int.\n\
781events -- an optional bitmask describing the type of events to check for");
782
783static PyObject *
784devpoll_register(devpollObject *self, PyObject *args)
785{
786 return internal_devpoll_register(self, args, 0);
787}
788
789PyDoc_STRVAR(devpoll_modify_doc,
790"modify(fd[, eventmask]) -> None\n\n\
791Modify a possible already registered file descriptor.\n\
792fd -- either an integer, or an object with a fileno() method returning an\n\
793 int.\n\
794events -- an optional bitmask describing the type of events to check for");
795
796static PyObject *
797devpoll_modify(devpollObject *self, PyObject *args)
798{
799 return internal_devpoll_register(self, args, 1);
800}
801
802
803PyDoc_STRVAR(devpoll_unregister_doc,
804"unregister(fd) -> None\n\n\
805Remove a file descriptor being tracked by the polling object.");
806
807static PyObject *
808devpoll_unregister(devpollObject *self, PyObject *o)
809{
810 int fd;
811
812 fd = PyObject_AsFileDescriptor( o );
813 if (fd == -1)
814 return NULL;
815
816 self->fds[self->n_fds].fd = fd;
817 self->fds[self->n_fds].events = POLLREMOVE;
818
819 if (++self->n_fds == self->max_n_fds) {
820 if (devpoll_flush(self))
821 return NULL;
822 }
823
824 Py_RETURN_NONE;
825}
826
827PyDoc_STRVAR(devpoll_poll_doc,
828"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
829Polls the set of registered file descriptors, returning a list containing \n\
830any descriptors that have events or errors to report.");
831
832static PyObject *
833devpoll_poll(devpollObject *self, PyObject *args)
834{
835 struct dvpoll dvp;
836 PyObject *result_list = NULL, *tout = NULL;
837 int poll_result, i;
838 long timeout;
839 PyObject *value, *num1, *num2;
840
841 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
842 return NULL;
843 }
844
845 /* Check values for timeout */
846 if (tout == NULL || tout == Py_None)
847 timeout = -1;
848 else if (!PyNumber_Check(tout)) {
849 PyErr_SetString(PyExc_TypeError,
850 "timeout must be an integer or None");
851 return NULL;
852 }
853 else {
854 tout = PyNumber_Long(tout);
855 if (!tout)
856 return NULL;
857 timeout = PyLong_AsLong(tout);
858 Py_DECREF(tout);
859 if (timeout == -1 && PyErr_Occurred())
860 return NULL;
861 }
862
863 if ((timeout < -1) || (timeout > INT_MAX)) {
864 PyErr_SetString(PyExc_OverflowError,
865 "timeout is out of range");
866 return NULL;
867 }
868
869 if (devpoll_flush(self))
870 return NULL;
871
872 dvp.dp_fds = self->fds;
873 dvp.dp_nfds = self->max_n_fds;
874 dvp.dp_timeout = timeout;
875
876 /* call devpoll() */
877 Py_BEGIN_ALLOW_THREADS
878 poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp);
879 Py_END_ALLOW_THREADS
880
881 if (poll_result < 0) {
882 PyErr_SetFromErrno(PyExc_IOError);
883 return NULL;
884 }
885
886 /* build the result list */
887
888 result_list = PyList_New(poll_result);
889 if (!result_list)
890 return NULL;
891 else {
892 for (i = 0; i < poll_result; i++) {
893 num1 = PyLong_FromLong(self->fds[i].fd);
894 num2 = PyLong_FromLong(self->fds[i].revents);
895 if ((num1 == NULL) || (num2 == NULL)) {
896 Py_XDECREF(num1);
897 Py_XDECREF(num2);
898 goto error;
899 }
900 value = PyTuple_Pack(2, num1, num2);
901 Py_DECREF(num1);
902 Py_DECREF(num2);
903 if (value == NULL)
904 goto error;
905 if ((PyList_SetItem(result_list, i, value)) == -1) {
906 Py_DECREF(value);
907 goto error;
908 }
909 }
910 }
911
912 return result_list;
913
914 error:
915 Py_DECREF(result_list);
916 return NULL;
917}
918
919static PyMethodDef devpoll_methods[] = {
920 {"register", (PyCFunction)devpoll_register,
921 METH_VARARGS, devpoll_register_doc},
922 {"modify", (PyCFunction)devpoll_modify,
923 METH_VARARGS, devpoll_modify_doc},
924 {"unregister", (PyCFunction)devpoll_unregister,
925 METH_O, devpoll_unregister_doc},
926 {"poll", (PyCFunction)devpoll_poll,
927 METH_VARARGS, devpoll_poll_doc},
928 {NULL, NULL} /* sentinel */
929};
930
931static devpollObject *
932newDevPollObject(void)
933{
934 devpollObject *self;
935 int fd_devpoll, limit_result;
936 struct pollfd *fds;
937 struct rlimit limit;
938
939 Py_BEGIN_ALLOW_THREADS
940 /*
941 ** If we try to process more that getrlimit()
942 ** fds, the kernel will give an error, so
943 ** we set the limit here. It is a dynamic
944 ** value, because we can change rlimit() anytime.
945 */
946 limit_result = getrlimit(RLIMIT_NOFILE, &limit);
947 if (limit_result != -1)
948 fd_devpoll = open("/dev/poll", O_RDWR);
949 Py_END_ALLOW_THREADS
950
951 if (limit_result == -1) {
952 PyErr_SetFromErrno(PyExc_OSError);
953 return NULL;
954 }
955 if (fd_devpoll == -1) {
956 PyErr_SetFromErrnoWithFilename(PyExc_IOError, "/dev/poll");
957 return NULL;
958 }
959
960 fds = PyMem_NEW(struct pollfd, limit.rlim_cur);
961 if (fds == NULL) {
962 close(fd_devpoll);
963 PyErr_NoMemory();
964 return NULL;
965 }
966
967 self = PyObject_New(devpollObject, &devpoll_Type);
968 if (self == NULL) {
969 close(fd_devpoll);
970 PyMem_DEL(fds);
971 return NULL;
972 }
973 self->fd_devpoll = fd_devpoll;
974 self->max_n_fds = limit.rlim_cur;
975 self->n_fds = 0;
976 self->fds = fds;
977
978 return self;
979}
980
981static void
982devpoll_dealloc(devpollObject *self)
983{
984 Py_BEGIN_ALLOW_THREADS
985 close(self->fd_devpoll);
986 Py_END_ALLOW_THREADS
987
988 PyMem_DEL(self->fds);
989
990 PyObject_Del(self);
991}
992
993static PyTypeObject devpoll_Type = {
994 /* The ob_type field must be initialized in the module init function
995 * to be portable to Windows without using C++. */
996 PyVarObject_HEAD_INIT(NULL, 0)
997 "select.devpoll", /*tp_name*/
998 sizeof(devpollObject), /*tp_basicsize*/
999 0, /*tp_itemsize*/
1000 /* methods */
1001 (destructor)devpoll_dealloc, /*tp_dealloc*/
1002 0, /*tp_print*/
1003 0, /*tp_getattr*/
1004 0, /*tp_setattr*/
1005 0, /*tp_reserved*/
1006 0, /*tp_repr*/
1007 0, /*tp_as_number*/
1008 0, /*tp_as_sequence*/
1009 0, /*tp_as_mapping*/
1010 0, /*tp_hash*/
1011 0, /*tp_call*/
1012 0, /*tp_str*/
1013 0, /*tp_getattro*/
1014 0, /*tp_setattro*/
1015 0, /*tp_as_buffer*/
1016 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1017 0, /*tp_doc*/
1018 0, /*tp_traverse*/
1019 0, /*tp_clear*/
1020 0, /*tp_richcompare*/
1021 0, /*tp_weaklistoffset*/
1022 0, /*tp_iter*/
1023 0, /*tp_iternext*/
1024 devpoll_methods, /*tp_methods*/
1025};
1026#endif /* HAVE_SYS_DEVPOLL_H */
1027
1028
1029
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001030PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001031"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001032unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001033
1034static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001035select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001038}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001039
Jesus Cead8b9ae62011-11-14 19:07:41 +01001040#ifdef HAVE_SYS_DEVPOLL_H
1041PyDoc_STRVAR(devpoll_doc,
1042"Returns a polling object, which supports registering and\n\
1043unregistering file descriptors, and then polling them for I/O events.");
1044
1045static PyObject *
1046select_devpoll(PyObject *self, PyObject *unused)
1047{
1048 return (PyObject *)newDevPollObject();
1049}
1050#endif
1051
1052
Thomas Wouters477c8d52006-05-27 19:21:47 +00001053#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054/*
Thomas Wouters477c8d52006-05-27 19:21:47 +00001055 * On some systems poll() sets errno on invalid file descriptors. We test
1056 * for this at runtime because this bug may be fixed or introduced between
1057 * OS releases.
1058 */
1059static int select_have_broken_poll(void)
1060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 int poll_test;
1062 int filedes[2];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
Thomas Wouters477c8d52006-05-27 19:21:47 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 /* Create a file descriptor to make invalid */
1067 if (pipe(filedes) < 0) {
1068 return 1;
1069 }
1070 poll_struct.fd = filedes[0];
1071 close(filedes[0]);
1072 close(filedes[1]);
1073 poll_test = poll(&poll_struct, 1, 0);
1074 if (poll_test < 0) {
1075 return 1;
1076 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
1077 return 1;
1078 }
1079 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001080}
1081#endif /* __APPLE__ */
1082
1083#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001084
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001085#ifdef HAVE_EPOLL
1086/* **************************************************************************
1087 * epoll interface for Linux 2.6
1088 *
1089 * Written by Christian Heimes
1090 * Inspired by Twisted's _epoll.pyx and select.poll()
1091 */
1092
1093#ifdef HAVE_SYS_EPOLL_H
1094#include <sys/epoll.h>
1095#endif
1096
1097typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 PyObject_HEAD
1099 SOCKET epfd; /* epoll control file descriptor */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001100} pyEpoll_Object;
1101
1102static PyTypeObject pyEpoll_Type;
1103#define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))
1104
1105static PyObject *
1106pyepoll_err_closed(void)
1107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll fd");
1109 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001110}
1111
1112static int
1113pyepoll_internal_close(pyEpoll_Object *self)
1114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 int save_errno = 0;
1116 if (self->epfd >= 0) {
1117 int epfd = self->epfd;
1118 self->epfd = -1;
1119 Py_BEGIN_ALLOW_THREADS
1120 if (close(epfd) < 0)
1121 save_errno = errno;
1122 Py_END_ALLOW_THREADS
1123 }
1124 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001125}
1126
1127static PyObject *
Benjamin Peterson95c16622011-12-27 15:36:32 -06001128newPyEpoll_Object(PyTypeObject *type, int sizehint, int flags, SOCKET fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 pyEpoll_Object *self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 assert(type != NULL && type->tp_alloc != NULL);
1133 self = (pyEpoll_Object *) type->tp_alloc(type, 0);
1134 if (self == NULL)
1135 return NULL;
1136
1137 if (fd == -1) {
1138 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson95c16622011-12-27 15:36:32 -06001139#ifdef HAVE_EPOLL_CREATE1
Benjamin Peterson83251c12011-12-27 16:01:21 -06001140 if (flags)
1141 self->epfd = epoll_create1(flags);
1142 else
Benjamin Peterson95c16622011-12-27 15:36:32 -06001143#endif
Benjamin Peterson83251c12011-12-27 16:01:21 -06001144 self->epfd = epoll_create(sizehint);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 Py_END_ALLOW_THREADS
1146 }
1147 else {
1148 self->epfd = fd;
1149 }
1150 if (self->epfd < 0) {
1151 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001152 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 return NULL;
1154 }
1155 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001156}
1157
1158
1159static PyObject *
1160pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1161{
Benjamin Peterson83251c12011-12-27 16:01:21 -06001162 int flags = 0, sizehint = FD_SETSIZE - 1;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001163 static char *kwlist[] = {"sizehint", "flags", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001164
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001165 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
1166 &sizehint, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 return NULL;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001168 if (sizehint < 0) {
1169 PyErr_SetString(PyExc_ValueError, "negative sizehint");
1170 return NULL;
1171 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001172
Benjamin Peterson95c16622011-12-27 15:36:32 -06001173 return newPyEpoll_Object(type, sizehint, flags, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001174}
1175
1176
1177static void
1178pyepoll_dealloc(pyEpoll_Object *self)
1179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 (void)pyepoll_internal_close(self);
1181 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001182}
1183
1184static PyObject*
1185pyepoll_close(pyEpoll_Object *self)
1186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 errno = pyepoll_internal_close(self);
1188 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001189 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return NULL;
1191 }
1192 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001193}
1194
1195PyDoc_STRVAR(pyepoll_close_doc,
1196"close() -> None\n\
1197\n\
1198Close the epoll control file descriptor. Further operations on the epoll\n\
1199object will raise an exception.");
1200
1201static PyObject*
1202pyepoll_get_closed(pyEpoll_Object *self)
1203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 if (self->epfd < 0)
1205 Py_RETURN_TRUE;
1206 else
1207 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001208}
1209
1210static PyObject*
1211pyepoll_fileno(pyEpoll_Object *self)
1212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 if (self->epfd < 0)
1214 return pyepoll_err_closed();
1215 return PyLong_FromLong(self->epfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001216}
1217
1218PyDoc_STRVAR(pyepoll_fileno_doc,
1219"fileno() -> int\n\
1220\n\
1221Return the epoll control file descriptor.");
1222
1223static PyObject*
1224pyepoll_fromfd(PyObject *cls, PyObject *args)
1225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1229 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001230
Benjamin Peterson95c16622011-12-27 15:36:32 -06001231 return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001232}
1233
1234PyDoc_STRVAR(pyepoll_fromfd_doc,
1235"fromfd(fd) -> epoll\n\
1236\n\
1237Create an epoll object from a given control fd.");
1238
1239static PyObject *
1240pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
1241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 struct epoll_event ev;
1243 int result;
1244 int fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 if (epfd < 0)
1247 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 fd = PyObject_AsFileDescriptor(pfd);
1250 if (fd == -1) {
1251 return NULL;
1252 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 switch(op) {
1255 case EPOLL_CTL_ADD:
1256 case EPOLL_CTL_MOD:
1257 ev.events = events;
1258 ev.data.fd = fd;
1259 Py_BEGIN_ALLOW_THREADS
1260 result = epoll_ctl(epfd, op, fd, &ev);
1261 Py_END_ALLOW_THREADS
1262 break;
1263 case EPOLL_CTL_DEL:
1264 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
1265 * operation required a non-NULL pointer in event, even
1266 * though this argument is ignored. */
1267 Py_BEGIN_ALLOW_THREADS
1268 result = epoll_ctl(epfd, op, fd, &ev);
1269 if (errno == EBADF) {
1270 /* fd already closed */
1271 result = 0;
1272 errno = 0;
1273 }
1274 Py_END_ALLOW_THREADS
1275 break;
1276 default:
1277 result = -1;
1278 errno = EINVAL;
1279 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001282 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 return NULL;
1284 }
1285 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001286}
1287
1288static PyObject *
1289pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 PyObject *pfd;
1292 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
1293 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
1296 &pfd, &events)) {
1297 return NULL;
1298 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001301}
1302
1303PyDoc_STRVAR(pyepoll_register_doc,
Georg Brandl222569d2010-08-02 20:47:56 +00001304"register(fd[, eventmask]) -> None\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001305\n\
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001306Registers a new fd or raises an OSError if the fd is already registered.\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00001307fd is the target file descriptor of the operation.\n\
1308events is a bit set composed of the various EPOLL constants; the default\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001309is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
1310\n\
1311The epoll interface supports all file descriptors that support poll.");
1312
1313static PyObject *
1314pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 PyObject *pfd;
1317 unsigned int events;
1318 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
1321 &pfd, &events)) {
1322 return NULL;
1323 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001326}
1327
1328PyDoc_STRVAR(pyepoll_modify_doc,
1329"modify(fd, eventmask) -> None\n\
1330\n\
1331fd is the target file descriptor of the operation\n\
1332events is a bit set composed of the various EPOLL constants");
1333
1334static PyObject *
1335pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 PyObject *pfd;
1338 static char *kwlist[] = {"fd", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
1341 &pfd)) {
1342 return NULL;
1343 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001346}
1347
1348PyDoc_STRVAR(pyepoll_unregister_doc,
1349"unregister(fd) -> None\n\
1350\n\
1351fd is the target file descriptor of the operation.");
1352
1353static PyObject *
1354pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 double dtimeout = -1.;
1357 int timeout;
1358 int maxevents = -1;
1359 int nfds, i;
1360 PyObject *elist = NULL, *etuple = NULL;
1361 struct epoll_event *evs = NULL;
1362 static char *kwlist[] = {"timeout", "maxevents", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 if (self->epfd < 0)
1365 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
1368 &dtimeout, &maxevents)) {
1369 return NULL;
1370 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 if (dtimeout < 0) {
1373 timeout = -1;
1374 }
1375 else if (dtimeout * 1000.0 > INT_MAX) {
1376 PyErr_SetString(PyExc_OverflowError,
1377 "timeout is too large");
1378 return NULL;
1379 }
1380 else {
Victor Stinner93320962014-01-25 14:37:50 +01001381 timeout = (int)(dtimeout * 1000.0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 if (maxevents == -1) {
1385 maxevents = FD_SETSIZE-1;
1386 }
1387 else if (maxevents < 1) {
1388 PyErr_Format(PyExc_ValueError,
1389 "maxevents must be greater than 0, got %d",
1390 maxevents);
1391 return NULL;
1392 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 evs = PyMem_New(struct epoll_event, maxevents);
1395 if (evs == NULL) {
1396 Py_DECREF(self);
1397 PyErr_NoMemory();
1398 return NULL;
1399 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 Py_BEGIN_ALLOW_THREADS
1402 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
1403 Py_END_ALLOW_THREADS
1404 if (nfds < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001405 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 goto error;
1407 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 elist = PyList_New(nfds);
1410 if (elist == NULL) {
1411 goto error;
1412 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 for (i = 0; i < nfds; i++) {
1415 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
1416 if (etuple == NULL) {
1417 Py_CLEAR(elist);
1418 goto error;
1419 }
1420 PyList_SET_ITEM(elist, i, etuple);
1421 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001422
Christian Heimesf6cd9672008-03-26 13:45:42 +00001423 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 PyMem_Free(evs);
1425 return elist;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001426}
1427
1428PyDoc_STRVAR(pyepoll_poll_doc,
1429"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1430\n\
1431Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1432in seconds (as float). -1 makes poll wait indefinitely.\n\
1433Up to maxevents are returned to the caller.");
1434
1435static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 {"fromfd", (PyCFunction)pyepoll_fromfd,
1437 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1438 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1439 pyepoll_close_doc},
1440 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1441 pyepoll_fileno_doc},
1442 {"modify", (PyCFunction)pyepoll_modify,
1443 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1444 {"register", (PyCFunction)pyepoll_register,
1445 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1446 {"unregister", (PyCFunction)pyepoll_unregister,
1447 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1448 {"poll", (PyCFunction)pyepoll_poll,
1449 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
1450 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001451};
1452
1453static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 {"closed", (getter)pyepoll_get_closed, NULL,
1455 "True if the epoll handler is closed"},
1456 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001457};
1458
1459PyDoc_STRVAR(pyepoll_doc,
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001460"select.epoll(sizehint=-1, flags=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001461\n\
1462Returns an epolling object\n\
1463\n\
1464sizehint must be a positive integer or -1 for the default size. The\n\
1465sizehint is used to optimize internal data structures. It doesn't limit\n\
1466the maximum number of monitored events.");
1467
1468static PyTypeObject pyEpoll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 PyVarObject_HEAD_INIT(NULL, 0)
1470 "select.epoll", /* tp_name */
1471 sizeof(pyEpoll_Object), /* tp_basicsize */
1472 0, /* tp_itemsize */
1473 (destructor)pyepoll_dealloc, /* tp_dealloc */
1474 0, /* tp_print */
1475 0, /* tp_getattr */
1476 0, /* tp_setattr */
1477 0, /* tp_reserved */
1478 0, /* tp_repr */
1479 0, /* tp_as_number */
1480 0, /* tp_as_sequence */
1481 0, /* tp_as_mapping */
1482 0, /* tp_hash */
1483 0, /* tp_call */
1484 0, /* tp_str */
1485 PyObject_GenericGetAttr, /* tp_getattro */
1486 0, /* tp_setattro */
1487 0, /* tp_as_buffer */
1488 Py_TPFLAGS_DEFAULT, /* tp_flags */
1489 pyepoll_doc, /* tp_doc */
1490 0, /* tp_traverse */
1491 0, /* tp_clear */
1492 0, /* tp_richcompare */
1493 0, /* tp_weaklistoffset */
1494 0, /* tp_iter */
1495 0, /* tp_iternext */
1496 pyepoll_methods, /* tp_methods */
1497 0, /* tp_members */
1498 pyepoll_getsetlist, /* tp_getset */
1499 0, /* tp_base */
1500 0, /* tp_dict */
1501 0, /* tp_descr_get */
1502 0, /* tp_descr_set */
1503 0, /* tp_dictoffset */
1504 0, /* tp_init */
1505 0, /* tp_alloc */
1506 pyepoll_new, /* tp_new */
1507 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001508};
1509
1510#endif /* HAVE_EPOLL */
1511
1512#ifdef HAVE_KQUEUE
1513/* **************************************************************************
1514 * kqueue interface for BSD
1515 *
1516 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1517 * All rights reserved.
1518 *
1519 * Redistribution and use in source and binary forms, with or without
1520 * modification, are permitted provided that the following conditions
1521 * are met:
1522 * 1. Redistributions of source code must retain the above copyright
1523 * notice, this list of conditions and the following disclaimer.
1524 * 2. Redistributions in binary form must reproduce the above copyright
1525 * notice, this list of conditions and the following disclaimer in the
1526 * documentation and/or other materials provided with the distribution.
1527 *
1528 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1529 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1530 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1531 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1532 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1533 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1534 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1535 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1536 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1537 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1538 * SUCH DAMAGE.
1539 */
1540
1541#ifdef HAVE_SYS_EVENT_H
1542#include <sys/event.h>
1543#endif
1544
1545PyDoc_STRVAR(kqueue_event_doc,
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001546"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001547\n\
1548This object is the equivalent of the struct kevent for the C API.\n\
1549\n\
1550See the kqueue manpage for more detailed information about the meaning\n\
1551of the arguments.\n\
1552\n\
1553One minor note: while you might hope that udata could store a\n\
1554reference to a python object, it cannot, because it is impossible to\n\
1555keep a proper reference count of the object once it's passed into the\n\
1556kernel. Therefore, I have restricted it to only storing an integer. I\n\
1557recommend ignoring it and simply using the 'ident' field to key off\n\
1558of. You could also set up a dictionary on the python side to store a\n\
1559udata->object mapping.");
1560
1561typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 PyObject_HEAD
1563 struct kevent e;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001564} kqueue_event_Object;
1565
1566static PyTypeObject kqueue_event_Type;
1567
1568#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1569
1570typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 PyObject_HEAD
1572 SOCKET kqfd; /* kqueue control fd */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001573} kqueue_queue_Object;
1574
1575static PyTypeObject kqueue_queue_Type;
1576
1577#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1578
Antoine Pitroud83f1e62009-11-04 21:10:38 +00001579#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1580# error uintptr_t does not match void *!
1581#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1582# define T_UINTPTRT T_ULONGLONG
1583# define T_INTPTRT T_LONGLONG
1584# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1585# define UINTPTRT_FMT_UNIT "K"
1586# define INTPTRT_FMT_UNIT "L"
1587#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1588# define T_UINTPTRT T_ULONG
1589# define T_INTPTRT T_LONG
1590# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1591# define UINTPTRT_FMT_UNIT "k"
1592# define INTPTRT_FMT_UNIT "l"
1593#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1594# define T_UINTPTRT T_UINT
1595# define T_INTPTRT T_INT
1596# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1597# define UINTPTRT_FMT_UNIT "I"
1598# define INTPTRT_FMT_UNIT "i"
1599#else
1600# error uintptr_t does not match int, long, or long long!
1601#endif
1602
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001603/*
1604 * kevent is not standard and its members vary across BSDs.
1605 */
1606#if !defined(__OpenBSD__)
1607# define IDENT_TYPE T_UINTPTRT
1608# define IDENT_CAST Py_intptr_t
1609# define DATA_TYPE T_INTPTRT
1610# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
1611# define IDENT_AsType PyLong_AsUintptr_t
1612#else
1613# define IDENT_TYPE T_UINT
1614# define IDENT_CAST int
1615# define DATA_TYPE T_INT
1616# define DATA_FMT_UNIT "i"
1617# define IDENT_AsType PyLong_AsUnsignedLong
1618#endif
1619
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001620/* Unfortunately, we can't store python objects in udata, because
1621 * kevents in the kernel can be removed without warning, which would
1622 * forever lose the refcount on the object stored with it.
1623 */
1624
1625#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1626static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001627 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 {"filter", T_SHORT, KQ_OFF(e.filter)},
1629 {"flags", T_USHORT, KQ_OFF(e.flags)},
1630 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001631 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1633 {NULL} /* Sentinel */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001634};
1635#undef KQ_OFF
1636
1637static PyObject *
Georg Brandlc0e22b72010-03-14 10:51:01 +00001638
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001639kqueue_event_repr(kqueue_event_Object *s)
1640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 char buf[1024];
1642 PyOS_snprintf(
1643 buf, sizeof(buf),
1644 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1645 "data=0x%zd udata=%p>",
1646 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1647 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1648 return PyUnicode_FromString(buf);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001649}
1650
1651static int
1652kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 PyObject *pfd;
1655 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1656 "data", "udata", NULL};
Christian Heimesf1fe1592013-08-25 14:57:00 +02001657 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1662 &pfd, &(self->e.filter), &(self->e.flags),
1663 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1664 return -1;
1665 }
1666
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001667 if (PyLong_Check(pfd)
1668#if IDENT_TYPE == T_UINT
1669 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
1670#endif
1671 ) {
1672 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 }
1674 else {
1675 self->e.ident = PyObject_AsFileDescriptor(pfd);
1676 }
1677 if (PyErr_Occurred()) {
1678 return -1;
1679 }
1680 return 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001681}
1682
1683static PyObject *
1684kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 int op)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 Py_intptr_t result = 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 if (!kqueue_event_Check(o)) {
1690 if (op == Py_EQ || op == Py_NE) {
1691 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1692 Py_INCREF(res);
1693 return res;
1694 }
1695 PyErr_Format(PyExc_TypeError,
1696 "can't compare %.200s to %.200s",
1697 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1698 return NULL;
1699 }
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001700 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 ((result = s->e.filter - o->e.filter) == 0) &&
1702 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001703 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 ((result = s->e.data - o->e.data) == 0) &&
1705 ((result = s->e.udata - o->e.udata) == 0)
1706 ) {
1707 result = 0;
1708 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 switch (op) {
1711 case Py_EQ:
1712 result = (result == 0);
1713 break;
1714 case Py_NE:
1715 result = (result != 0);
1716 break;
1717 case Py_LE:
1718 result = (result <= 0);
1719 break;
1720 case Py_GE:
1721 result = (result >= 0);
1722 break;
1723 case Py_LT:
1724 result = (result < 0);
1725 break;
1726 case Py_GT:
1727 result = (result > 0);
1728 break;
1729 }
1730 return PyBool_FromLong((long)result);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001731}
1732
1733static PyTypeObject kqueue_event_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 PyVarObject_HEAD_INIT(NULL, 0)
1735 "select.kevent", /* tp_name */
1736 sizeof(kqueue_event_Object), /* tp_basicsize */
1737 0, /* tp_itemsize */
1738 0, /* tp_dealloc */
1739 0, /* tp_print */
1740 0, /* tp_getattr */
1741 0, /* tp_setattr */
1742 0, /* tp_reserved */
1743 (reprfunc)kqueue_event_repr, /* tp_repr */
1744 0, /* tp_as_number */
1745 0, /* tp_as_sequence */
1746 0, /* tp_as_mapping */
1747 0, /* tp_hash */
1748 0, /* tp_call */
1749 0, /* tp_str */
1750 0, /* tp_getattro */
1751 0, /* tp_setattro */
1752 0, /* tp_as_buffer */
1753 Py_TPFLAGS_DEFAULT, /* tp_flags */
1754 kqueue_event_doc, /* tp_doc */
1755 0, /* tp_traverse */
1756 0, /* tp_clear */
1757 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1758 0, /* tp_weaklistoffset */
1759 0, /* tp_iter */
1760 0, /* tp_iternext */
1761 0, /* tp_methods */
1762 kqueue_event_members, /* tp_members */
1763 0, /* tp_getset */
1764 0, /* tp_base */
1765 0, /* tp_dict */
1766 0, /* tp_descr_get */
1767 0, /* tp_descr_set */
1768 0, /* tp_dictoffset */
1769 (initproc)kqueue_event_init, /* tp_init */
1770 0, /* tp_alloc */
1771 0, /* tp_new */
1772 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001773};
1774
1775static PyObject *
1776kqueue_queue_err_closed(void)
1777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue fd");
1779 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001780}
1781
1782static int
1783kqueue_queue_internal_close(kqueue_queue_Object *self)
1784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 int save_errno = 0;
1786 if (self->kqfd >= 0) {
1787 int kqfd = self->kqfd;
1788 self->kqfd = -1;
1789 Py_BEGIN_ALLOW_THREADS
1790 if (close(kqfd) < 0)
1791 save_errno = errno;
1792 Py_END_ALLOW_THREADS
1793 }
1794 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001795}
1796
1797static PyObject *
1798newKqueue_Object(PyTypeObject *type, SOCKET fd)
1799{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 kqueue_queue_Object *self;
1801 assert(type != NULL && type->tp_alloc != NULL);
1802 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1803 if (self == NULL) {
1804 return NULL;
1805 }
1806
1807 if (fd == -1) {
1808 Py_BEGIN_ALLOW_THREADS
1809 self->kqfd = kqueue();
1810 Py_END_ALLOW_THREADS
1811 }
1812 else {
1813 self->kqfd = fd;
1814 }
1815 if (self->kqfd < 0) {
1816 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001817 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 return NULL;
1819 }
1820 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001821}
1822
1823static PyObject *
1824kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1825{
1826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 if ((args != NULL && PyObject_Size(args)) ||
1828 (kwds != NULL && PyObject_Size(kwds))) {
1829 PyErr_SetString(PyExc_ValueError,
1830 "select.kqueue doesn't accept arguments");
1831 return NULL;
1832 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 return newKqueue_Object(type, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001835}
1836
1837static void
1838kqueue_queue_dealloc(kqueue_queue_Object *self)
1839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 kqueue_queue_internal_close(self);
1841 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001842}
1843
1844static PyObject*
1845kqueue_queue_close(kqueue_queue_Object *self)
1846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 errno = kqueue_queue_internal_close(self);
1848 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001849 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 return NULL;
1851 }
1852 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001853}
1854
1855PyDoc_STRVAR(kqueue_queue_close_doc,
1856"close() -> None\n\
1857\n\
1858Close the kqueue control file descriptor. Further operations on the kqueue\n\
1859object will raise an exception.");
1860
1861static PyObject*
1862kqueue_queue_get_closed(kqueue_queue_Object *self)
1863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 if (self->kqfd < 0)
1865 Py_RETURN_TRUE;
1866 else
1867 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001868}
1869
1870static PyObject*
1871kqueue_queue_fileno(kqueue_queue_Object *self)
1872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 if (self->kqfd < 0)
1874 return kqueue_queue_err_closed();
1875 return PyLong_FromLong(self->kqfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001876}
1877
1878PyDoc_STRVAR(kqueue_queue_fileno_doc,
1879"fileno() -> int\n\
1880\n\
1881Return the kqueue control file descriptor.");
1882
1883static PyObject*
1884kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1889 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001892}
1893
1894PyDoc_STRVAR(kqueue_queue_fromfd_doc,
1895"fromfd(fd) -> kqueue\n\
1896\n\
1897Create a kqueue object from a given control fd.");
1898
1899static PyObject *
1900kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
1901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 int nevents = 0;
1903 int gotevents = 0;
1904 int nchanges = 0;
1905 int i = 0;
1906 PyObject *otimeout = NULL;
1907 PyObject *ch = NULL;
1908 PyObject *it = NULL, *ei = NULL;
1909 PyObject *result = NULL;
1910 struct kevent *evl = NULL;
1911 struct kevent *chl = NULL;
Victor Stinnerd327f9d2012-03-13 15:29:08 +01001912 struct timespec timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 struct timespec *ptimeoutspec;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (self->kqfd < 0)
1916 return kqueue_queue_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
1919 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 if (nevents < 0) {
1922 PyErr_Format(PyExc_ValueError,
1923 "Length of eventlist must be 0 or positive, got %d",
1924 nevents);
1925 return NULL;
1926 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 if (otimeout == Py_None || otimeout == NULL) {
1929 ptimeoutspec = NULL;
1930 }
1931 else if (PyNumber_Check(otimeout)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +01001932 if (_PyTime_ObjectToTimespec(otimeout,
1933 &timeout.tv_sec, &timeout.tv_nsec) == -1)
1934 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001935
Victor Stinner5d272cc2012-03-13 13:35:55 +01001936 if (timeout.tv_sec < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 PyErr_SetString(PyExc_ValueError,
1938 "timeout must be positive or None");
1939 return NULL;
1940 }
Victor Stinnerd528b012012-03-13 16:25:35 +01001941 ptimeoutspec = &timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 }
1943 else {
1944 PyErr_Format(PyExc_TypeError,
1945 "timeout argument must be an number "
1946 "or None, got %.200s",
1947 Py_TYPE(otimeout)->tp_name);
1948 return NULL;
1949 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 if (ch != NULL && ch != Py_None) {
1952 it = PyObject_GetIter(ch);
1953 if (it == NULL) {
1954 PyErr_SetString(PyExc_TypeError,
1955 "changelist is not iterable");
1956 return NULL;
1957 }
1958 nchanges = PyObject_Size(ch);
1959 if (nchanges < 0) {
1960 goto error;
1961 }
Georg Brandlc0e22b72010-03-14 10:51:01 +00001962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 chl = PyMem_New(struct kevent, nchanges);
1964 if (chl == NULL) {
1965 PyErr_NoMemory();
1966 goto error;
1967 }
1968 i = 0;
1969 while ((ei = PyIter_Next(it)) != NULL) {
1970 if (!kqueue_event_Check(ei)) {
1971 Py_DECREF(ei);
1972 PyErr_SetString(PyExc_TypeError,
1973 "changelist must be an iterable of "
1974 "select.kevent objects");
1975 goto error;
1976 } else {
1977 chl[i++] = ((kqueue_event_Object *)ei)->e;
1978 }
1979 Py_DECREF(ei);
1980 }
1981 }
1982 Py_CLEAR(it);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 /* event list */
1985 if (nevents) {
1986 evl = PyMem_New(struct kevent, nevents);
1987 if (evl == NULL) {
1988 PyErr_NoMemory();
1989 goto error;
1990 }
1991 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 Py_BEGIN_ALLOW_THREADS
1994 gotevents = kevent(self->kqfd, chl, nchanges,
1995 evl, nevents, ptimeoutspec);
1996 Py_END_ALLOW_THREADS
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 if (gotevents == -1) {
1999 PyErr_SetFromErrno(PyExc_OSError);
2000 goto error;
2001 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 result = PyList_New(gotevents);
2004 if (result == NULL) {
2005 goto error;
2006 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 for (i = 0; i < gotevents; i++) {
2009 kqueue_event_Object *ch;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
2012 if (ch == NULL) {
2013 goto error;
2014 }
2015 ch->e = evl[i];
2016 PyList_SET_ITEM(result, i, (PyObject *)ch);
2017 }
2018 PyMem_Free(chl);
2019 PyMem_Free(evl);
2020 return result;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002021
2022 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 PyMem_Free(chl);
2024 PyMem_Free(evl);
2025 Py_XDECREF(result);
2026 Py_XDECREF(it);
2027 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002028}
2029
2030PyDoc_STRVAR(kqueue_queue_control_doc,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00002031"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002032\n\
2033Calls the kernel kevent function.\n\
2034- changelist must be a list of kevent objects describing the changes\n\
2035 to be made to the kernel's watch list or None.\n\
2036- max_events lets you specify the maximum number of events that the\n\
2037 kernel will return.\n\
2038- timeout is the maximum time to wait in seconds, or else None,\n\
2039 to wait forever. timeout accepts floats for smaller timeouts, too.");
2040
2041
2042static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
2044 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
2045 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
2046 kqueue_queue_close_doc},
2047 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
2048 kqueue_queue_fileno_doc},
2049 {"control", (PyCFunction)kqueue_queue_control,
2050 METH_VARARGS , kqueue_queue_control_doc},
2051 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002052};
2053
2054static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 {"closed", (getter)kqueue_queue_get_closed, NULL,
2056 "True if the kqueue handler is closed"},
2057 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002058};
2059
2060PyDoc_STRVAR(kqueue_queue_doc,
2061"Kqueue syscall wrapper.\n\
2062\n\
2063For example, to start watching a socket for input:\n\
2064>>> kq = kqueue()\n\
2065>>> sock = socket()\n\
2066>>> sock.connect((host, port))\n\
2067>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
2068\n\
2069To wait one second for it to become writeable:\n\
2070>>> kq.control(None, 1, 1000)\n\
2071\n\
2072To stop listening:\n\
2073>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
2074
2075static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 PyVarObject_HEAD_INIT(NULL, 0)
2077 "select.kqueue", /* tp_name */
2078 sizeof(kqueue_queue_Object), /* tp_basicsize */
2079 0, /* tp_itemsize */
2080 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
2081 0, /* tp_print */
2082 0, /* tp_getattr */
2083 0, /* tp_setattr */
2084 0, /* tp_reserved */
2085 0, /* tp_repr */
2086 0, /* tp_as_number */
2087 0, /* tp_as_sequence */
2088 0, /* tp_as_mapping */
2089 0, /* tp_hash */
2090 0, /* tp_call */
2091 0, /* tp_str */
2092 0, /* tp_getattro */
2093 0, /* tp_setattro */
2094 0, /* tp_as_buffer */
2095 Py_TPFLAGS_DEFAULT, /* tp_flags */
2096 kqueue_queue_doc, /* tp_doc */
2097 0, /* tp_traverse */
2098 0, /* tp_clear */
2099 0, /* tp_richcompare */
2100 0, /* tp_weaklistoffset */
2101 0, /* tp_iter */
2102 0, /* tp_iternext */
2103 kqueue_queue_methods, /* tp_methods */
2104 0, /* tp_members */
2105 kqueue_queue_getsetlist, /* tp_getset */
2106 0, /* tp_base */
2107 0, /* tp_dict */
2108 0, /* tp_descr_get */
2109 0, /* tp_descr_set */
2110 0, /* tp_dictoffset */
2111 0, /* tp_init */
2112 0, /* tp_alloc */
2113 kqueue_queue_new, /* tp_new */
2114 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002115};
2116
2117#endif /* HAVE_KQUEUE */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002118
2119
2120
2121
2122
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002123/* ************************************************************************ */
2124
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002125PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002126"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
2127\n\
2128Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00002129The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002130rlist -- wait until ready for reading\n\
2131wlist -- wait until ready for writing\n\
2132xlist -- wait for an ``exceptional condition''\n\
2133If only one kind of condition is required, pass [] for the other lists.\n\
2134A file descriptor is either a socket or file object, or a small integer\n\
2135gotten from a fileno() method call on one of those.\n\
2136\n\
2137The optional 4th argument specifies a timeout in seconds; it may be\n\
2138a floating point number to specify fractions of seconds. If it is absent\n\
2139or None, the call will never time out.\n\
2140\n\
2141The return value is a tuple of three lists corresponding to the first three\n\
2142arguments; each contains the subset of the corresponding file descriptors\n\
2143that are ready.\n\
2144\n\
2145*** IMPORTANT NOTICE ***\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002146On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00002147descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002148
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002149static PyMethodDef select_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natali986a56c2013-01-19 12:19:10 +01002151#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002153#endif /* HAVE_POLL */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002154#ifdef HAVE_SYS_DEVPOLL_H
2155 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2156#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00002158};
2159
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002160PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002161"This module supports asynchronous I/O on multiple file descriptors.\n\
2162\n\
2163*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002164On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00002165
Martin v. Löwis1a214512008-06-11 05:26:20 +00002166
2167static struct PyModuleDef selectmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 PyModuleDef_HEAD_INIT,
2169 "select",
2170 module_doc,
2171 -1,
2172 select_methods,
2173 NULL,
2174 NULL,
2175 NULL,
2176 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002177};
2178
Jesus Cead8b9ae62011-11-14 19:07:41 +01002179
2180
2181
Mark Hammond62b1ab12002-07-23 06:31:15 +00002182PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002183PyInit_select(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00002184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 PyObject *m;
2186 m = PyModule_Create(&selectmodule);
2187 if (m == NULL)
2188 return NULL;
Fred Drake4baedc12002-04-01 14:53:37 +00002189
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002190 Py_INCREF(PyExc_OSError);
2191 PyModule_AddObject(m, "error", PyExc_OSError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002192
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002193#ifdef PIPE_BUF
R. David Murraye16cda92010-10-15 23:12:57 +00002194#ifdef HAVE_BROKEN_PIPE_BUF
2195#undef PIPE_BUF
2196#define PIPE_BUF 512
2197#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 PyModule_AddIntConstant(m, "PIPE_BUF", PIPE_BUF);
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002199#endif
Gregory P. Smithb970b862009-07-04 02:28:47 +00002200
Charles-François Natali986a56c2013-01-19 12:19:10 +01002201#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002202#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 if (select_have_broken_poll()) {
2204 if (PyObject_DelAttrString(m, "poll") == -1) {
2205 PyErr_Clear();
2206 }
2207 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002208#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002210#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 if (PyType_Ready(&poll_Type) < 0)
2212 return NULL;
2213 PyModule_AddIntConstant(m, "POLLIN", POLLIN);
2214 PyModule_AddIntConstant(m, "POLLPRI", POLLPRI);
2215 PyModule_AddIntConstant(m, "POLLOUT", POLLOUT);
2216 PyModule_AddIntConstant(m, "POLLERR", POLLERR);
2217 PyModule_AddIntConstant(m, "POLLHUP", POLLHUP);
2218 PyModule_AddIntConstant(m, "POLLNVAL", POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00002219
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002220#ifdef POLLRDNORM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 PyModule_AddIntConstant(m, "POLLRDNORM", POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002222#endif
2223#ifdef POLLRDBAND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 PyModule_AddIntConstant(m, "POLLRDBAND", POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002225#endif
2226#ifdef POLLWRNORM
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 PyModule_AddIntConstant(m, "POLLWRNORM", POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002228#endif
2229#ifdef POLLWRBAND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 PyModule_AddIntConstant(m, "POLLWRBAND", POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002231#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002232#ifdef POLLMSG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 PyModule_AddIntConstant(m, "POLLMSG", POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002234#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002236#endif /* HAVE_POLL */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002237
Jesus Cead8b9ae62011-11-14 19:07:41 +01002238#ifdef HAVE_SYS_DEVPOLL_H
2239 if (PyType_Ready(&devpoll_Type) < 0)
2240 return NULL;
2241#endif
2242
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002243#ifdef HAVE_EPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
2245 if (PyType_Ready(&pyEpoll_Type) < 0)
2246 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 Py_INCREF(&pyEpoll_Type);
2249 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 PyModule_AddIntConstant(m, "EPOLLIN", EPOLLIN);
2252 PyModule_AddIntConstant(m, "EPOLLOUT", EPOLLOUT);
2253 PyModule_AddIntConstant(m, "EPOLLPRI", EPOLLPRI);
2254 PyModule_AddIntConstant(m, "EPOLLERR", EPOLLERR);
2255 PyModule_AddIntConstant(m, "EPOLLHUP", EPOLLHUP);
2256 PyModule_AddIntConstant(m, "EPOLLET", EPOLLET);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002257#ifdef EPOLLONESHOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 /* Kernel 2.6.2+ */
2259 PyModule_AddIntConstant(m, "EPOLLONESHOT", EPOLLONESHOT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002260#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
2262 PyModule_AddIntConstant(m, "EPOLLRDNORM", EPOLLRDNORM);
2263 PyModule_AddIntConstant(m, "EPOLLRDBAND", EPOLLRDBAND);
2264 PyModule_AddIntConstant(m, "EPOLLWRNORM", EPOLLWRNORM);
2265 PyModule_AddIntConstant(m, "EPOLLWRBAND", EPOLLWRBAND);
2266 PyModule_AddIntConstant(m, "EPOLLMSG", EPOLLMSG);
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002267
Benjamin Peterson95c16622011-12-27 15:36:32 -06002268#ifdef EPOLL_CLOEXEC
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002269 PyModule_AddIntConstant(m, "EPOLL_CLOEXEC", EPOLL_CLOEXEC);
Benjamin Peterson95c16622011-12-27 15:36:32 -06002270#endif
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002271#endif /* HAVE_EPOLL */
2272
2273#ifdef HAVE_KQUEUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 kqueue_event_Type.tp_new = PyType_GenericNew;
2275 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
2276 if(PyType_Ready(&kqueue_event_Type) < 0)
2277 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 Py_INCREF(&kqueue_event_Type);
2280 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
2283 if(PyType_Ready(&kqueue_queue_Type) < 0)
2284 return NULL;
2285 Py_INCREF(&kqueue_queue_Type);
2286 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
2287
2288 /* event filters */
2289 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
2290 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
2291 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
2292 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
2293 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002294#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002296#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
2298 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 /* event flags */
2301 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
2302 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
2303 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
2304 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
2305 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
2306 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
2309 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
2312 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 /* READ WRITE filter flag */
2315 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 /* VNODE filter flags */
2318 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
2319 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
2320 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
2321 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
2322 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
2323 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
2324 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 /* PROC filter flags */
2327 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
2328 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
2329 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
2330 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
2331 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
2334 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
2335 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
2336
2337 /* NETDEV filter flags */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002338#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
2340 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
2341 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002342#endif
2343
2344#endif /* HAVE_KQUEUE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 return m;
Guido van Rossumed233a51992-06-23 09:07:03 +00002346}