blob: 17d73648a7138ddb866c5102ea3a8c4c0379d281 [file] [log] [blame]
Guido van Rossum4f0fbf81996-06-12 04:22:53 +00001/* select - Module containing unix select(2) call.
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002 Under Unix, the file descriptors are small integers.
3 Under Win32, select only exists for sockets, and sockets may
4 have any value except INVALID_SOCKET.
Guido van Rossum4f0fbf81996-06-12 04:22:53 +00005*/
Guido van Rossumed233a51992-06-23 09:07:03 +00006
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00007#include "Python.h"
Christian Heimes4fbc72b2008-03-22 00:47:35 +00008#include <structmember.h>
Guido van Rossumed233a51992-06-23 09:07:03 +00009
Jesus Cead8b9ae62011-11-14 19:07:41 +010010#ifdef HAVE_SYS_DEVPOLL_H
11#include <sys/resource.h>
12#include <sys/devpoll.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <fcntl.h>
16#endif
17
Thomas Wouters477c8d52006-05-27 19:21:47 +000018#ifdef __APPLE__
19 /* Perform runtime testing for a broken poll on OSX to make it easier
20 * to use the same binary on multiple releases of the OS.
21 */
22#undef HAVE_BROKEN_POLL
23#endif
24
Tim Petersd92dfe02000-12-12 01:18:41 +000025/* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined.
26 64 is too small (too many people have bumped into that limit).
27 Here we boost it.
28 Users who want even more than the boosted limit should #define
29 FD_SETSIZE higher before this; e.g., via compiler /D switch.
30*/
31#if defined(MS_WINDOWS) && !defined(FD_SETSIZE)
32#define FD_SETSIZE 512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033#endif
Tim Petersd92dfe02000-12-12 01:18:41 +000034
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000035#if defined(HAVE_POLL_H)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000036#include <poll.h>
Andrew M. Kuchling737fbb32001-07-14 20:54:37 +000037#elif defined(HAVE_SYS_POLL_H)
38#include <sys/poll.h>
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +000039#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000040
Guido van Rossum37273171996-12-09 18:47:43 +000041#ifdef __sgi
42/* This is missing from unistd.h */
Thomas Woutersbd4bc4e2000-07-22 23:57:55 +000043extern void bzero(void *, int);
Guido van Rossum37273171996-12-09 18:47:43 +000044#endif
45
Thomas Wouters0e3f5912006-08-11 14:57:12 +000046#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000047#include <sys/types.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000048#endif
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000049
Guido van Rossum6f489d91996-06-28 20:15:15 +000050#ifdef MS_WINDOWS
Christian Heimesc36625b2008-01-04 13:33:00 +000051# define WIN32_LEAN_AND_MEAN
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052# include <winsock.h>
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000053#else
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054# define SOCKET int
Skip Montanaroeb33e5a2007-08-17 12:57:41 +000055# if defined(__VMS)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056# include <socket.h>
57# endif
Guido van Rossumbcc20741998-08-04 22:53:56 +000058#endif
Guido van Rossumed233a51992-06-23 09:07:03 +000059
Barry Warsawc1cb3601996-12-12 22:16:21 +000060/* list of Python objects and their file descriptor */
61typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 PyObject *obj; /* owned reference */
63 SOCKET fd;
64 int sentinel; /* -1 == sentinel */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000065} pylist;
66
Barry Warsawc1cb3601996-12-12 22:16:21 +000067static void
Tim Peters4b046c22001-08-16 21:59:46 +000068reap_obj(pylist fd2obj[FD_SETSIZE + 1])
Barry Warsawc1cb3601996-12-12 22:16:21 +000069{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 int i;
71 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
72 Py_XDECREF(fd2obj[i].obj);
73 fd2obj[i].obj = NULL;
74 }
75 fd2obj[0].sentinel = -1;
Barry Warsawc1cb3601996-12-12 22:16:21 +000076}
77
78
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000079/* returns -1 and sets the Python exception if an error occurred, otherwise
80 returns a number >= 0
81*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000082static int
Brett Cannon62dba4c2003-09-10 19:37:42 +000083seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +000084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 int max = -1;
86 int index = 0;
Antoine Pitroue4ad37e2012-11-01 20:13:54 +010087 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 PyObject* fast_seq = NULL;
89 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
92 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000093
Benjamin Petersone0edb8b2010-06-27 23:49:45 +000094 fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 if (!fast_seq)
96 return -1;
97
Antoine Pitroue4ad37e2012-11-01 20:13:54 +010098 for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 SOCKET v;
100
101 /* any intervening fileno() calls could decr this refcnt */
102 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
Jesus Cea62a5c322012-07-19 21:31:26 +0200103 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 Py_INCREF(o);
106 v = PyObject_AsFileDescriptor( o );
107 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000108
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000109#if defined(_MSC_VER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 max = 0; /* not used for Win32 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000111#else /* !_MSC_VER */
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200112 if (!_PyIsSelectable_fd(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 PyErr_SetString(PyExc_ValueError,
114 "filedescriptor out of range in select()");
115 goto finally;
116 }
117 if (v > max)
118 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000119#endif /* _MSC_VER */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 /* add object and its file descriptor to the list */
123 if (index >= FD_SETSIZE) {
124 PyErr_SetString(PyExc_ValueError,
125 "too many file descriptors in select()");
126 goto finally;
127 }
128 fd2obj[index].obj = o;
129 fd2obj[index].fd = v;
130 fd2obj[index].sentinel = 0;
131 fd2obj[++index].sentinel = -1;
132 }
133 Py_DECREF(fast_seq);
134 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000135
136 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 Py_XDECREF(o);
138 Py_DECREF(fast_seq);
139 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000140}
141
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000142/* returns NULL and sets the Python exception if an error occurred */
143static PyObject *
Tim Peters4b046c22001-08-16 21:59:46 +0000144set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +0000145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 int i, j, count=0;
147 PyObject *list, *o;
148 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
151 if (FD_ISSET(fd2obj[j].fd, set))
152 count++;
153 }
154 list = PyList_New(count);
155 if (!list)
156 return NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 i = 0;
159 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
160 fd = fd2obj[j].fd;
161 if (FD_ISSET(fd, set)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 o = fd2obj[j].obj;
163 fd2obj[j].obj = NULL;
164 /* transfer ownership */
165 if (PyList_SetItem(list, i, o) < 0)
166 goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 i++;
169 }
170 }
171 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000172 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 Py_DECREF(list);
174 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000175}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000176
Barry Warsawb44740f2001-08-16 16:52:59 +0000177#undef SELECT_USES_HEAP
178#if FD_SETSIZE > 1024
179#define SELECT_USES_HEAP
180#endif /* FD_SETSIZE > 1024 */
181
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000182static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000183select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000184{
Barry Warsawb44740f2001-08-16 16:52:59 +0000185#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 pylist *rfd2obj, *wfd2obj, *efd2obj;
Barry Warsawb44740f2001-08-16 16:52:59 +0000187#else /* !SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 /* XXX: All this should probably be implemented as follows:
189 * - find the highest descriptor we're interested in
190 * - add one
191 * - that's the size
192 * See: Stevens, APitUE, $12.5.1
193 */
194 pylist rfd2obj[FD_SETSIZE + 1];
195 pylist wfd2obj[FD_SETSIZE + 1];
196 pylist efd2obj[FD_SETSIZE + 1];
Barry Warsawb44740f2001-08-16 16:52:59 +0000197#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 PyObject *ifdlist, *ofdlist, *efdlist;
199 PyObject *ret = NULL;
200 PyObject *tout = Py_None;
201 fd_set ifdset, ofdset, efdset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 struct timeval tv, *tvp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 int imax, omax, emax, max;
204 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 /* convert arguments */
207 if (!PyArg_UnpackTuple(args, "select", 3, 4,
208 &ifdlist, &ofdlist, &efdlist, &tout))
209 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if (tout == Py_None)
212 tvp = (struct timeval *)0;
213 else if (!PyNumber_Check(tout)) {
214 PyErr_SetString(PyExc_TypeError,
215 "timeout must be a float or None");
216 return NULL;
217 }
218 else {
Victor Stinnerb2a37732012-03-14 00:20:51 +0100219#ifdef MS_WINDOWS
220 time_t sec;
221 if (_PyTime_ObjectToTimeval(tout, &sec, &tv.tv_usec) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 return NULL;
Victor Stinnerb2a37732012-03-14 00:20:51 +0100223 assert(sizeof(tv.tv_sec) == sizeof(long));
224#if SIZEOF_TIME_T > SIZEOF_LONG
225 if (sec > LONG_MAX) {
226 PyErr_SetString(PyExc_OverflowError,
227 "timeout is too large");
228 return NULL;
229 }
230#endif
231 tv.tv_sec = (long)sec;
232#else
Brett Cannon8798ad32012-04-07 14:59:29 -0400233 /* 64-bit OS X has struct timeval.tv_usec as an int (and thus still 4
234 bytes as required), but no longer defined by a long. */
Benjamin Peterson6f3e5e42012-09-11 12:05:05 -0400235 long tv_usec;
Brett Cannon8798ad32012-04-07 14:59:29 -0400236 if (_PyTime_ObjectToTimeval(tout, &tv.tv_sec, &tv_usec) == -1)
Victor Stinnerb2a37732012-03-14 00:20:51 +0100237 return NULL;
Brett Cannon8798ad32012-04-07 14:59:29 -0400238 tv.tv_usec = tv_usec;
Victor Stinnerb2a37732012-03-14 00:20:51 +0100239#endif
Victor Stinner5d272cc2012-03-13 13:35:55 +0100240 if (tv.tv_sec < 0) {
241 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 return NULL;
243 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 tvp = &tv;
245 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000246
Guido van Rossumed233a51992-06-23 09:07:03 +0000247
Barry Warsawb44740f2001-08-16 16:52:59 +0000248#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 /* Allocate memory for the lists */
250 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
251 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
252 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
253 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
254 if (rfd2obj) PyMem_DEL(rfd2obj);
255 if (wfd2obj) PyMem_DEL(wfd2obj);
256 if (efd2obj) PyMem_DEL(efd2obj);
257 return PyErr_NoMemory();
258 }
Barry Warsawb44740f2001-08-16 16:52:59 +0000259#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* Convert sequences to fd_sets, and get maximum fd number
261 * propagates the Python exception set in seq2set()
262 */
263 rfd2obj[0].sentinel = -1;
264 wfd2obj[0].sentinel = -1;
265 efd2obj[0].sentinel = -1;
266 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
267 goto finally;
268 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
269 goto finally;
270 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
271 goto finally;
272 max = imax;
273 if (omax > max) max = omax;
274 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 Py_BEGIN_ALLOW_THREADS
277 n = select(max, &ifdset, &ofdset, &efdset, tvp);
278 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000279
Thomas Heller106f4c72002-09-24 16:51:00 +0000280#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 if (n == SOCKET_ERROR) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200282 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000284#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (n < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200286 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000288#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 else {
290 /* any of these three calls can raise an exception. it's more
291 convenient to test for this after all three calls... but
292 is that acceptable?
293 */
294 ifdlist = set2list(&ifdset, rfd2obj);
295 ofdlist = set2list(&ofdset, wfd2obj);
296 efdlist = set2list(&efdset, efd2obj);
297 if (PyErr_Occurred())
298 ret = NULL;
299 else
300 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000301
Victor Stinnerbbf8ce52013-07-09 00:49:03 +0200302 Py_XDECREF(ifdlist);
303 Py_XDECREF(ofdlist);
304 Py_XDECREF(efdlist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 }
306
Barry Warsawc1cb3601996-12-12 22:16:21 +0000307 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 reap_obj(rfd2obj);
309 reap_obj(wfd2obj);
310 reap_obj(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000311#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyMem_DEL(rfd2obj);
313 PyMem_DEL(wfd2obj);
314 PyMem_DEL(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000315#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000317}
318
Nicholas Bastine62c5c82004-03-21 23:45:42 +0000319#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320/*
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000321 * poll() support
322 */
323
324typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 PyObject_HEAD
326 PyObject *dict;
327 int ufd_uptodate;
328 int ufd_len;
329 struct pollfd *ufds;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300330 int poll_running;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000331} pollObject;
332
Jeremy Hylton938ace62002-07-17 16:30:39 +0000333static PyTypeObject poll_Type;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335/* Update the malloc'ed array of pollfds to match the dictionary
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000336 contained within a pollObject. Return 1 on success, 0 on an error.
337*/
338
339static int
340update_ufd_array(pollObject *self)
341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 Py_ssize_t i, pos;
343 PyObject *key, *value;
344 struct pollfd *old_ufds = self->ufds;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 self->ufd_len = PyDict_Size(self->dict);
347 PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
348 if (self->ufds == NULL) {
349 self->ufds = old_ufds;
350 PyErr_NoMemory();
351 return 0;
352 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 i = pos = 0;
355 while (PyDict_Next(self->dict, &pos, &key, &value)) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200356 assert(i < self->ufd_len);
357 /* Never overflow */
358 self->ufds[i].fd = (int)PyLong_AsLong(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 self->ufds[i].events = (short)PyLong_AsLong(value);
360 i++;
361 }
Serhiy Storchaka78980432013-01-15 01:12:17 +0200362 assert(i == self->ufd_len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 self->ufd_uptodate = 1;
364 return 1;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000365}
366
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000367PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000368"register(fd [, eventmask] ) -> None\n\n\
369Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000370fd -- either an integer, or an object with a fileno() method returning an\n\
371 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000372events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000373
374static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375poll_register(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 PyObject *o, *key, *value;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200378 int fd;
379 short events = POLLIN | POLLPRI | POLLOUT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000381
Serhiy Storchaka78980432013-01-15 01:12:17 +0200382 if (!PyArg_ParseTuple(args, "O|h:register", &o, &events)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 return NULL;
384 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 fd = PyObject_AsFileDescriptor(o);
387 if (fd == -1) return NULL;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* Add entry to the internal dictionary: the key is the
390 file descriptor, and the value is the event mask. */
391 key = PyLong_FromLong(fd);
392 if (key == NULL)
393 return NULL;
394 value = PyLong_FromLong(events);
395 if (value == NULL) {
396 Py_DECREF(key);
397 return NULL;
398 }
399 err = PyDict_SetItem(self->dict, key, value);
400 Py_DECREF(key);
401 Py_DECREF(value);
402 if (err < 0)
403 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 self->ufd_uptodate = 0;
406
407 Py_INCREF(Py_None);
408 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000409}
410
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000411PyDoc_STRVAR(poll_modify_doc,
412"modify(fd, eventmask) -> None\n\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +0000413Modify an already registered file descriptor.\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000414fd -- either an integer, or an object with a fileno() method returning an\n\
415 int.\n\
416events -- an optional bitmask describing the type of events to check for");
417
418static PyObject *
419poll_modify(pollObject *self, PyObject *args)
420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 PyObject *o, *key, *value;
422 int fd, events;
423 int err;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 if (!PyArg_ParseTuple(args, "Oi:modify", &o, &events)) {
426 return NULL;
427 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 fd = PyObject_AsFileDescriptor(o);
430 if (fd == -1) return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 /* Modify registered fd */
433 key = PyLong_FromLong(fd);
434 if (key == NULL)
435 return NULL;
436 if (PyDict_GetItem(self->dict, key) == NULL) {
437 errno = ENOENT;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200438 PyErr_SetFromErrno(PyExc_OSError);
Jesus Cea62a5c322012-07-19 21:31:26 +0200439 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 return NULL;
441 }
442 value = PyLong_FromLong(events);
443 if (value == NULL) {
444 Py_DECREF(key);
445 return NULL;
446 }
447 err = PyDict_SetItem(self->dict, key, value);
448 Py_DECREF(key);
449 Py_DECREF(value);
450 if (err < 0)
451 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 self->ufd_uptodate = 0;
454
455 Py_INCREF(Py_None);
456 return Py_None;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000457}
458
459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000460PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000461"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000462Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000463
464static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyObject *key;
468 int fd;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 fd = PyObject_AsFileDescriptor( o );
471 if (fd == -1)
472 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 /* Check whether the fd is already in the array */
475 key = PyLong_FromLong(fd);
476 if (key == NULL)
477 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (PyDict_DelItem(self->dict, key) == -1) {
480 Py_DECREF(key);
481 /* This will simply raise the KeyError set by PyDict_DelItem
482 if the file descriptor isn't registered. */
483 return NULL;
484 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 Py_DECREF(key);
487 self->ufd_uptodate = 0;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 Py_INCREF(Py_None);
490 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000491}
492
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000493PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000494"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
495Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000496any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000497
498static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499poll_poll(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 PyObject *result_list = NULL, *tout = NULL;
502 int timeout = 0, poll_result, i, j;
503 PyObject *value = NULL, *num = NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
506 return NULL;
507 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 /* Check values for timeout */
510 if (tout == NULL || tout == Py_None)
511 timeout = -1;
512 else if (!PyNumber_Check(tout)) {
513 PyErr_SetString(PyExc_TypeError,
514 "timeout must be an integer or None");
515 return NULL;
516 }
517 else {
518 tout = PyNumber_Long(tout);
519 if (!tout)
520 return NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200521 timeout = _PyLong_AsInt(tout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 Py_DECREF(tout);
523 if (timeout == -1 && PyErr_Occurred())
524 return NULL;
525 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000526
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300527 /* Avoid concurrent poll() invocation, issue 8865 */
528 if (self->poll_running) {
529 PyErr_SetString(PyExc_RuntimeError,
530 "concurrent poll() invocation");
531 return NULL;
532 }
533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 /* Ensure the ufd array is up to date */
535 if (!self->ufd_uptodate)
536 if (update_ufd_array(self) == 0)
537 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000538
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300539 self->poll_running = 1;
540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 /* call poll() */
542 Py_BEGIN_ALLOW_THREADS
543 poll_result = poll(self->ufds, self->ufd_len, timeout);
544 Py_END_ALLOW_THREADS
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000545
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300546 self->poll_running = 0;
547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (poll_result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200549 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 return NULL;
551 }
552
553 /* build the result list */
554
555 result_list = PyList_New(poll_result);
556 if (!result_list)
557 return NULL;
558 else {
559 for (i = 0, j = 0; j < poll_result; j++) {
560 /* skip to the next fired descriptor */
561 while (!self->ufds[i].revents) {
562 i++;
563 }
564 /* if we hit a NULL return, set value to NULL
565 and break out of loop; code at end will
566 clean up result_list */
567 value = PyTuple_New(2);
568 if (value == NULL)
569 goto error;
570 num = PyLong_FromLong(self->ufds[i].fd);
571 if (num == NULL) {
572 Py_DECREF(value);
573 goto error;
574 }
575 PyTuple_SET_ITEM(value, 0, num);
576
577 /* The &0xffff is a workaround for AIX. 'revents'
578 is a 16-bit short, and IBM assigned POLLNVAL
579 to be 0x8000, so the conversion to int results
580 in a negative number. See SF bug #923315. */
581 num = PyLong_FromLong(self->ufds[i].revents & 0xffff);
582 if (num == NULL) {
583 Py_DECREF(value);
584 goto error;
585 }
586 PyTuple_SET_ITEM(value, 1, num);
587 if ((PyList_SetItem(result_list, j, value)) == -1) {
588 Py_DECREF(value);
589 goto error;
590 }
591 i++;
592 }
593 }
594 return result_list;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000595
596 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_DECREF(result_list);
598 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000599}
600
601static PyMethodDef poll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 {"register", (PyCFunction)poll_register,
603 METH_VARARGS, poll_register_doc},
604 {"modify", (PyCFunction)poll_modify,
605 METH_VARARGS, poll_modify_doc},
606 {"unregister", (PyCFunction)poll_unregister,
607 METH_O, poll_unregister_doc},
608 {"poll", (PyCFunction)poll_poll,
609 METH_VARARGS, poll_poll_doc},
610 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000611};
612
613static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000614newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 pollObject *self;
617 self = PyObject_New(pollObject, &poll_Type);
618 if (self == NULL)
619 return NULL;
620 /* ufd_uptodate is a Boolean, denoting whether the
621 array pointed to by ufds matches the contents of the dictionary. */
622 self->ufd_uptodate = 0;
623 self->ufds = NULL;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300624 self->poll_running = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 self->dict = PyDict_New();
626 if (self->dict == NULL) {
627 Py_DECREF(self);
628 return NULL;
629 }
630 return self;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000631}
632
633static void
634poll_dealloc(pollObject *self)
635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 if (self->ufds != NULL)
637 PyMem_DEL(self->ufds);
638 Py_XDECREF(self->dict);
639 PyObject_Del(self);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000640}
641
Tim Peters0c322792002-07-17 16:49:03 +0000642static PyTypeObject poll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 /* The ob_type field must be initialized in the module init function
644 * to be portable to Windows without using C++. */
645 PyVarObject_HEAD_INIT(NULL, 0)
646 "select.poll", /*tp_name*/
647 sizeof(pollObject), /*tp_basicsize*/
648 0, /*tp_itemsize*/
649 /* methods */
650 (destructor)poll_dealloc, /*tp_dealloc*/
651 0, /*tp_print*/
652 0, /*tp_getattr*/
653 0, /*tp_setattr*/
654 0, /*tp_reserved*/
655 0, /*tp_repr*/
656 0, /*tp_as_number*/
657 0, /*tp_as_sequence*/
658 0, /*tp_as_mapping*/
659 0, /*tp_hash*/
660 0, /*tp_call*/
661 0, /*tp_str*/
662 0, /*tp_getattro*/
663 0, /*tp_setattro*/
664 0, /*tp_as_buffer*/
665 Py_TPFLAGS_DEFAULT, /*tp_flags*/
666 0, /*tp_doc*/
667 0, /*tp_traverse*/
668 0, /*tp_clear*/
669 0, /*tp_richcompare*/
670 0, /*tp_weaklistoffset*/
671 0, /*tp_iter*/
672 0, /*tp_iternext*/
673 poll_methods, /*tp_methods*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000674};
675
Jesus Cead8b9ae62011-11-14 19:07:41 +0100676#ifdef HAVE_SYS_DEVPOLL_H
677typedef struct {
678 PyObject_HEAD
679 int fd_devpoll;
680 int max_n_fds;
681 int n_fds;
682 struct pollfd *fds;
683} devpollObject;
684
685static PyTypeObject devpoll_Type;
686
Victor Stinner13423c32013-08-22 00:19:50 +0200687static PyObject *
688devpoll_err_closed(void)
689{
690 PyErr_SetString(PyExc_ValueError, "I/O operation on closed devpoll object");
691 return NULL;
692}
693
Jesus Cead8b9ae62011-11-14 19:07:41 +0100694static int devpoll_flush(devpollObject *self)
695{
696 int size, n;
697
698 if (!self->n_fds) return 0;
699
700 size = sizeof(struct pollfd)*self->n_fds;
701 self->n_fds = 0;
702
703 Py_BEGIN_ALLOW_THREADS
704 n = write(self->fd_devpoll, self->fds, size);
705 Py_END_ALLOW_THREADS
706
707 if (n == -1 ) {
708 PyErr_SetFromErrno(PyExc_IOError);
709 return -1;
710 }
711 if (n < size) {
712 /*
713 ** Data writed to /dev/poll is a binary data structure. It is not
714 ** clear what to do if a partial write occurred. For now, raise
715 ** an exception and see if we actually found this problem in
716 ** the wild.
717 ** See http://bugs.python.org/issue6397.
718 */
719 PyErr_Format(PyExc_IOError, "failed to write all pollfds. "
720 "Please, report at http://bugs.python.org/. "
721 "Data to report: Size tried: %d, actual size written: %d.",
722 size, n);
723 return -1;
724 }
725 return 0;
726}
727
728static PyObject *
729internal_devpoll_register(devpollObject *self, PyObject *args, int remove)
730{
731 PyObject *o;
732 int fd, events = POLLIN | POLLPRI | POLLOUT;
733
Victor Stinner13423c32013-08-22 00:19:50 +0200734 if (self->fd_devpoll < 0)
735 return devpoll_err_closed();
736
Jesus Cead8b9ae62011-11-14 19:07:41 +0100737 if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
738 return NULL;
739 }
740
741 fd = PyObject_AsFileDescriptor(o);
742 if (fd == -1) return NULL;
743
744 if (remove) {
745 self->fds[self->n_fds].fd = fd;
746 self->fds[self->n_fds].events = POLLREMOVE;
747
748 if (++self->n_fds == self->max_n_fds) {
749 if (devpoll_flush(self))
750 return NULL;
751 }
752 }
753
754 self->fds[self->n_fds].fd = fd;
755 self->fds[self->n_fds].events = events;
756
757 if (++self->n_fds == self->max_n_fds) {
758 if (devpoll_flush(self))
759 return NULL;
760 }
761
762 Py_RETURN_NONE;
763}
764
765PyDoc_STRVAR(devpoll_register_doc,
766"register(fd [, eventmask] ) -> None\n\n\
767Register a file descriptor with the polling object.\n\
768fd -- either an integer, or an object with a fileno() method returning an\n\
769 int.\n\
770events -- an optional bitmask describing the type of events to check for");
771
772static PyObject *
773devpoll_register(devpollObject *self, PyObject *args)
774{
775 return internal_devpoll_register(self, args, 0);
776}
777
778PyDoc_STRVAR(devpoll_modify_doc,
779"modify(fd[, eventmask]) -> None\n\n\
780Modify a possible already registered file descriptor.\n\
781fd -- either an integer, or an object with a fileno() method returning an\n\
782 int.\n\
783events -- an optional bitmask describing the type of events to check for");
784
785static PyObject *
786devpoll_modify(devpollObject *self, PyObject *args)
787{
788 return internal_devpoll_register(self, args, 1);
789}
790
791
792PyDoc_STRVAR(devpoll_unregister_doc,
793"unregister(fd) -> None\n\n\
794Remove a file descriptor being tracked by the polling object.");
795
796static PyObject *
797devpoll_unregister(devpollObject *self, PyObject *o)
798{
799 int fd;
800
Victor Stinner13423c32013-08-22 00:19:50 +0200801 if (self->fd_devpoll < 0)
802 return devpoll_err_closed();
803
Jesus Cead8b9ae62011-11-14 19:07:41 +0100804 fd = PyObject_AsFileDescriptor( o );
805 if (fd == -1)
806 return NULL;
807
808 self->fds[self->n_fds].fd = fd;
809 self->fds[self->n_fds].events = POLLREMOVE;
810
811 if (++self->n_fds == self->max_n_fds) {
812 if (devpoll_flush(self))
813 return NULL;
814 }
815
816 Py_RETURN_NONE;
817}
818
819PyDoc_STRVAR(devpoll_poll_doc,
820"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
821Polls the set of registered file descriptors, returning a list containing \n\
822any descriptors that have events or errors to report.");
823
824static PyObject *
825devpoll_poll(devpollObject *self, PyObject *args)
826{
827 struct dvpoll dvp;
828 PyObject *result_list = NULL, *tout = NULL;
829 int poll_result, i;
830 long timeout;
831 PyObject *value, *num1, *num2;
832
Victor Stinner13423c32013-08-22 00:19:50 +0200833 if (self->fd_devpoll < 0)
834 return devpoll_err_closed();
835
Jesus Cead8b9ae62011-11-14 19:07:41 +0100836 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
837 return NULL;
838 }
839
840 /* Check values for timeout */
841 if (tout == NULL || tout == Py_None)
842 timeout = -1;
843 else if (!PyNumber_Check(tout)) {
844 PyErr_SetString(PyExc_TypeError,
845 "timeout must be an integer or None");
846 return NULL;
847 }
848 else {
849 tout = PyNumber_Long(tout);
850 if (!tout)
851 return NULL;
852 timeout = PyLong_AsLong(tout);
853 Py_DECREF(tout);
854 if (timeout == -1 && PyErr_Occurred())
855 return NULL;
856 }
857
858 if ((timeout < -1) || (timeout > INT_MAX)) {
859 PyErr_SetString(PyExc_OverflowError,
860 "timeout is out of range");
861 return NULL;
862 }
863
864 if (devpoll_flush(self))
865 return NULL;
866
867 dvp.dp_fds = self->fds;
868 dvp.dp_nfds = self->max_n_fds;
869 dvp.dp_timeout = timeout;
870
871 /* call devpoll() */
872 Py_BEGIN_ALLOW_THREADS
873 poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp);
874 Py_END_ALLOW_THREADS
875
876 if (poll_result < 0) {
877 PyErr_SetFromErrno(PyExc_IOError);
878 return NULL;
879 }
880
881 /* build the result list */
882
883 result_list = PyList_New(poll_result);
884 if (!result_list)
885 return NULL;
886 else {
887 for (i = 0; i < poll_result; i++) {
888 num1 = PyLong_FromLong(self->fds[i].fd);
889 num2 = PyLong_FromLong(self->fds[i].revents);
890 if ((num1 == NULL) || (num2 == NULL)) {
891 Py_XDECREF(num1);
892 Py_XDECREF(num2);
893 goto error;
894 }
895 value = PyTuple_Pack(2, num1, num2);
896 Py_DECREF(num1);
897 Py_DECREF(num2);
898 if (value == NULL)
899 goto error;
900 if ((PyList_SetItem(result_list, i, value)) == -1) {
901 Py_DECREF(value);
902 goto error;
903 }
904 }
905 }
906
907 return result_list;
908
909 error:
910 Py_DECREF(result_list);
911 return NULL;
912}
913
Victor Stinner13423c32013-08-22 00:19:50 +0200914static PyObject*
915devpoll_close(devpollObject *self)
916{
917 errno = devpoll_internal_close(self);
918 if (errno < 0) {
919 PyErr_SetFromErrno(PyExc_OSError);
920 return NULL;
921 }
922 Py_RETURN_NONE;
923}
924
925PyDoc_STRVAR(devpoll_close_doc,
926"close() -> None\n\
927\n\
928Close the devpoll file descriptor. Further operations on the devpoll\n\
929object will raise an exception.");
930
931static PyObject*
932devpoll_get_closed(devpollObject *self)
933{
934 if (self->fd_devpoll < 0)
935 Py_RETURN_TRUE;
936 else
937 Py_RETURN_FALSE;
938}
939
940static PyObject*
941devpoll_fileno(devpollObject *self)
942{
943 if (self->fd_devpoll < 0)
944 return devpoll_err_closed();
945 return PyLong_FromLong(self->fd_devpoll);
946}
947
948PyDoc_STRVAR(devpoll_fileno_doc,
949"fileno() -> int\n\
950\n\
951Return the file descriptor.");
952
Jesus Cead8b9ae62011-11-14 19:07:41 +0100953static PyMethodDef devpoll_methods[] = {
954 {"register", (PyCFunction)devpoll_register,
955 METH_VARARGS, devpoll_register_doc},
956 {"modify", (PyCFunction)devpoll_modify,
957 METH_VARARGS, devpoll_modify_doc},
958 {"unregister", (PyCFunction)devpoll_unregister,
959 METH_O, devpoll_unregister_doc},
960 {"poll", (PyCFunction)devpoll_poll,
961 METH_VARARGS, devpoll_poll_doc},
Victor Stinner13423c32013-08-22 00:19:50 +0200962 {"close", (PyCFunction)devpoll_close, METH_NOARGS,
963 devpoll_close_doc},
964 {"fileno", (PyCFunction)devpoll_fileno, METH_NOARGS,
965 devpoll_fileno_doc},
Jesus Cead8b9ae62011-11-14 19:07:41 +0100966 {NULL, NULL} /* sentinel */
967};
968
Victor Stinner13423c32013-08-22 00:19:50 +0200969static PyGetSetDef devpoll_getsetlist[] = {
970 {"closed", (getter)devpoll_get_closed, NULL,
971 "True if the devpoll object is closed"},
972 {0},
973};
974
Jesus Cead8b9ae62011-11-14 19:07:41 +0100975static devpollObject *
976newDevPollObject(void)
977{
978 devpollObject *self;
979 int fd_devpoll, limit_result;
980 struct pollfd *fds;
981 struct rlimit limit;
982
983 Py_BEGIN_ALLOW_THREADS
984 /*
985 ** If we try to process more that getrlimit()
986 ** fds, the kernel will give an error, so
987 ** we set the limit here. It is a dynamic
988 ** value, because we can change rlimit() anytime.
989 */
990 limit_result = getrlimit(RLIMIT_NOFILE, &limit);
991 if (limit_result != -1)
992 fd_devpoll = open("/dev/poll", O_RDWR);
993 Py_END_ALLOW_THREADS
994
995 if (limit_result == -1) {
996 PyErr_SetFromErrno(PyExc_OSError);
997 return NULL;
998 }
999 if (fd_devpoll == -1) {
1000 PyErr_SetFromErrnoWithFilename(PyExc_IOError, "/dev/poll");
1001 return NULL;
1002 }
1003
1004 fds = PyMem_NEW(struct pollfd, limit.rlim_cur);
1005 if (fds == NULL) {
1006 close(fd_devpoll);
1007 PyErr_NoMemory();
1008 return NULL;
1009 }
1010
1011 self = PyObject_New(devpollObject, &devpoll_Type);
1012 if (self == NULL) {
1013 close(fd_devpoll);
1014 PyMem_DEL(fds);
1015 return NULL;
1016 }
1017 self->fd_devpoll = fd_devpoll;
1018 self->max_n_fds = limit.rlim_cur;
1019 self->n_fds = 0;
1020 self->fds = fds;
1021
1022 return self;
1023}
1024
Victor Stinner13423c32013-08-22 00:19:50 +02001025static int
Richard Oudkerk069d65c2013-08-22 13:04:23 +01001026devpoll_internal_close(devpollObject *self)
Victor Stinner13423c32013-08-22 00:19:50 +02001027{
1028 int save_errno = 0;
1029 if (self->fd_devpoll >= 0) {
1030 int fd = self->fd_devpoll;
1031 self->fd_devpoll = -1;
1032 Py_BEGIN_ALLOW_THREADS
1033 if (close(fd) < 0)
1034 save_errno = errno;
1035 Py_END_ALLOW_THREADS
1036 }
1037 return save_errno;
1038}
1039
Jesus Cead8b9ae62011-11-14 19:07:41 +01001040static void
1041devpoll_dealloc(devpollObject *self)
1042{
Victor Stinner13423c32013-08-22 00:19:50 +02001043 (void)devpoll_internal_close();
Jesus Cead8b9ae62011-11-14 19:07:41 +01001044 PyMem_DEL(self->fds);
Jesus Cead8b9ae62011-11-14 19:07:41 +01001045 PyObject_Del(self);
1046}
1047
1048static PyTypeObject devpoll_Type = {
1049 /* The ob_type field must be initialized in the module init function
1050 * to be portable to Windows without using C++. */
1051 PyVarObject_HEAD_INIT(NULL, 0)
1052 "select.devpoll", /*tp_name*/
1053 sizeof(devpollObject), /*tp_basicsize*/
1054 0, /*tp_itemsize*/
1055 /* methods */
1056 (destructor)devpoll_dealloc, /*tp_dealloc*/
1057 0, /*tp_print*/
1058 0, /*tp_getattr*/
1059 0, /*tp_setattr*/
1060 0, /*tp_reserved*/
1061 0, /*tp_repr*/
1062 0, /*tp_as_number*/
1063 0, /*tp_as_sequence*/
1064 0, /*tp_as_mapping*/
1065 0, /*tp_hash*/
1066 0, /*tp_call*/
1067 0, /*tp_str*/
1068 0, /*tp_getattro*/
1069 0, /*tp_setattro*/
1070 0, /*tp_as_buffer*/
1071 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1072 0, /*tp_doc*/
1073 0, /*tp_traverse*/
1074 0, /*tp_clear*/
1075 0, /*tp_richcompare*/
1076 0, /*tp_weaklistoffset*/
1077 0, /*tp_iter*/
1078 0, /*tp_iternext*/
1079 devpoll_methods, /*tp_methods*/
Victor Stinner13423c32013-08-22 00:19:50 +02001080 0, /* tp_members */
1081 devpoll_getsetlist, /* tp_getset */
Jesus Cead8b9ae62011-11-14 19:07:41 +01001082};
1083#endif /* HAVE_SYS_DEVPOLL_H */
1084
1085
1086
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001087PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001088"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001089unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001090
1091static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001092select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001095}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001096
Jesus Cead8b9ae62011-11-14 19:07:41 +01001097#ifdef HAVE_SYS_DEVPOLL_H
1098PyDoc_STRVAR(devpoll_doc,
1099"Returns a polling object, which supports registering and\n\
1100unregistering file descriptors, and then polling them for I/O events.");
1101
1102static PyObject *
1103select_devpoll(PyObject *self, PyObject *unused)
1104{
1105 return (PyObject *)newDevPollObject();
1106}
1107#endif
1108
1109
Thomas Wouters477c8d52006-05-27 19:21:47 +00001110#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111/*
Thomas Wouters477c8d52006-05-27 19:21:47 +00001112 * On some systems poll() sets errno on invalid file descriptors. We test
1113 * for this at runtime because this bug may be fixed or introduced between
1114 * OS releases.
1115 */
1116static int select_have_broken_poll(void)
1117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 int poll_test;
1119 int filedes[2];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
Thomas Wouters477c8d52006-05-27 19:21:47 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 /* Create a file descriptor to make invalid */
1124 if (pipe(filedes) < 0) {
1125 return 1;
1126 }
1127 poll_struct.fd = filedes[0];
1128 close(filedes[0]);
1129 close(filedes[1]);
1130 poll_test = poll(&poll_struct, 1, 0);
1131 if (poll_test < 0) {
1132 return 1;
1133 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
1134 return 1;
1135 }
1136 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001137}
1138#endif /* __APPLE__ */
1139
1140#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001141
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001142#ifdef HAVE_EPOLL
1143/* **************************************************************************
1144 * epoll interface for Linux 2.6
1145 *
1146 * Written by Christian Heimes
1147 * Inspired by Twisted's _epoll.pyx and select.poll()
1148 */
1149
1150#ifdef HAVE_SYS_EPOLL_H
1151#include <sys/epoll.h>
1152#endif
1153
1154typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 PyObject_HEAD
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001156 SOCKET epfd; /* epoll control file descriptor */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001157} pyEpoll_Object;
1158
1159static PyTypeObject pyEpoll_Type;
1160#define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))
1161
1162static PyObject *
1163pyepoll_err_closed(void)
1164{
Victor Stinner13423c32013-08-22 00:19:50 +02001165 PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001167}
1168
1169static int
1170pyepoll_internal_close(pyEpoll_Object *self)
1171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 int save_errno = 0;
1173 if (self->epfd >= 0) {
1174 int epfd = self->epfd;
1175 self->epfd = -1;
1176 Py_BEGIN_ALLOW_THREADS
1177 if (close(epfd) < 0)
1178 save_errno = errno;
1179 Py_END_ALLOW_THREADS
1180 }
1181 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001182}
1183
1184static PyObject *
Benjamin Peterson95c16622011-12-27 15:36:32 -06001185newPyEpoll_Object(PyTypeObject *type, int sizehint, int flags, SOCKET fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 pyEpoll_Object *self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 assert(type != NULL && type->tp_alloc != NULL);
1190 self = (pyEpoll_Object *) type->tp_alloc(type, 0);
1191 if (self == NULL)
1192 return NULL;
1193
1194 if (fd == -1) {
1195 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson95c16622011-12-27 15:36:32 -06001196#ifdef HAVE_EPOLL_CREATE1
Benjamin Peterson83251c12011-12-27 16:01:21 -06001197 if (flags)
1198 self->epfd = epoll_create1(flags);
1199 else
Benjamin Peterson95c16622011-12-27 15:36:32 -06001200#endif
Benjamin Peterson83251c12011-12-27 16:01:21 -06001201 self->epfd = epoll_create(sizehint);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 Py_END_ALLOW_THREADS
1203 }
1204 else {
1205 self->epfd = fd;
1206 }
1207 if (self->epfd < 0) {
1208 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001209 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 return NULL;
1211 }
1212 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001213}
1214
1215
1216static PyObject *
1217pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1218{
Benjamin Peterson83251c12011-12-27 16:01:21 -06001219 int flags = 0, sizehint = FD_SETSIZE - 1;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001220 static char *kwlist[] = {"sizehint", "flags", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001221
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001222 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
1223 &sizehint, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 return NULL;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001225 if (sizehint < 0) {
1226 PyErr_SetString(PyExc_ValueError, "negative sizehint");
1227 return NULL;
1228 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001229
Benjamin Peterson95c16622011-12-27 15:36:32 -06001230 return newPyEpoll_Object(type, sizehint, flags, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001231}
1232
1233
1234static void
1235pyepoll_dealloc(pyEpoll_Object *self)
1236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 (void)pyepoll_internal_close(self);
1238 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001239}
1240
1241static PyObject*
1242pyepoll_close(pyEpoll_Object *self)
1243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 errno = pyepoll_internal_close(self);
1245 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001246 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 return NULL;
1248 }
1249 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001250}
1251
1252PyDoc_STRVAR(pyepoll_close_doc,
1253"close() -> None\n\
1254\n\
1255Close the epoll control file descriptor. Further operations on the epoll\n\
1256object will raise an exception.");
1257
1258static PyObject*
1259pyepoll_get_closed(pyEpoll_Object *self)
1260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (self->epfd < 0)
1262 Py_RETURN_TRUE;
1263 else
1264 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001265}
1266
1267static PyObject*
1268pyepoll_fileno(pyEpoll_Object *self)
1269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (self->epfd < 0)
1271 return pyepoll_err_closed();
1272 return PyLong_FromLong(self->epfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001273}
1274
1275PyDoc_STRVAR(pyepoll_fileno_doc,
1276"fileno() -> int\n\
1277\n\
1278Return the epoll control file descriptor.");
1279
1280static PyObject*
1281pyepoll_fromfd(PyObject *cls, PyObject *args)
1282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1286 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001287
Benjamin Peterson95c16622011-12-27 15:36:32 -06001288 return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001289}
1290
1291PyDoc_STRVAR(pyepoll_fromfd_doc,
1292"fromfd(fd) -> epoll\n\
1293\n\
1294Create an epoll object from a given control fd.");
1295
1296static PyObject *
1297pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
1298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 struct epoll_event ev;
1300 int result;
1301 int fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if (epfd < 0)
1304 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 fd = PyObject_AsFileDescriptor(pfd);
1307 if (fd == -1) {
1308 return NULL;
1309 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 switch(op) {
1312 case EPOLL_CTL_ADD:
1313 case EPOLL_CTL_MOD:
1314 ev.events = events;
1315 ev.data.fd = fd;
1316 Py_BEGIN_ALLOW_THREADS
1317 result = epoll_ctl(epfd, op, fd, &ev);
1318 Py_END_ALLOW_THREADS
1319 break;
1320 case EPOLL_CTL_DEL:
1321 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
1322 * operation required a non-NULL pointer in event, even
1323 * though this argument is ignored. */
1324 Py_BEGIN_ALLOW_THREADS
1325 result = epoll_ctl(epfd, op, fd, &ev);
1326 if (errno == EBADF) {
1327 /* fd already closed */
1328 result = 0;
1329 errno = 0;
1330 }
1331 Py_END_ALLOW_THREADS
1332 break;
1333 default:
1334 result = -1;
1335 errno = EINVAL;
1336 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001339 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 return NULL;
1341 }
1342 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001343}
1344
1345static PyObject *
1346pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 PyObject *pfd;
1349 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
1350 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
1353 &pfd, &events)) {
1354 return NULL;
1355 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001358}
1359
1360PyDoc_STRVAR(pyepoll_register_doc,
Georg Brandl222569d2010-08-02 20:47:56 +00001361"register(fd[, eventmask]) -> None\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001362\n\
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001363Registers a new fd or raises an OSError if the fd is already registered.\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00001364fd is the target file descriptor of the operation.\n\
1365events is a bit set composed of the various EPOLL constants; the default\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001366is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
1367\n\
1368The epoll interface supports all file descriptors that support poll.");
1369
1370static PyObject *
1371pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 PyObject *pfd;
1374 unsigned int events;
1375 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
1378 &pfd, &events)) {
1379 return NULL;
1380 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001383}
1384
1385PyDoc_STRVAR(pyepoll_modify_doc,
1386"modify(fd, eventmask) -> None\n\
1387\n\
1388fd is the target file descriptor of the operation\n\
1389events is a bit set composed of the various EPOLL constants");
1390
1391static PyObject *
1392pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 PyObject *pfd;
1395 static char *kwlist[] = {"fd", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
1398 &pfd)) {
1399 return NULL;
1400 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001403}
1404
1405PyDoc_STRVAR(pyepoll_unregister_doc,
1406"unregister(fd) -> None\n\
1407\n\
1408fd is the target file descriptor of the operation.");
1409
1410static PyObject *
1411pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 double dtimeout = -1.;
1414 int timeout;
1415 int maxevents = -1;
1416 int nfds, i;
1417 PyObject *elist = NULL, *etuple = NULL;
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001418 struct epoll_event *evs = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 static char *kwlist[] = {"timeout", "maxevents", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 if (self->epfd < 0)
1422 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
1425 &dtimeout, &maxevents)) {
1426 return NULL;
1427 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 if (dtimeout < 0) {
1430 timeout = -1;
1431 }
1432 else if (dtimeout * 1000.0 > INT_MAX) {
1433 PyErr_SetString(PyExc_OverflowError,
1434 "timeout is too large");
1435 return NULL;
1436 }
1437 else {
1438 timeout = (int)(dtimeout * 1000.0);
1439 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 if (maxevents == -1) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001442 maxevents = FD_SETSIZE-1;
1443 }
1444 else if (maxevents < 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 PyErr_Format(PyExc_ValueError,
1446 "maxevents must be greater than 0, got %d",
1447 maxevents);
1448 return NULL;
1449 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001450
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001451 evs = PyMem_New(struct epoll_event, maxevents);
1452 if (evs == NULL) {
1453 Py_DECREF(self);
1454 PyErr_NoMemory();
1455 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 Py_BEGIN_ALLOW_THREADS
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001459 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 Py_END_ALLOW_THREADS
1461 if (nfds < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001462 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 goto error;
1464 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 elist = PyList_New(nfds);
1467 if (elist == NULL) {
1468 goto error;
1469 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 for (i = 0; i < nfds; i++) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001472 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 if (etuple == NULL) {
1474 Py_CLEAR(elist);
1475 goto error;
1476 }
1477 PyList_SET_ITEM(elist, i, etuple);
1478 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001479
Christian Heimesf6cd9672008-03-26 13:45:42 +00001480 error:
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001481 PyMem_Free(evs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 return elist;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001483}
1484
1485PyDoc_STRVAR(pyepoll_poll_doc,
1486"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1487\n\
1488Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1489in seconds (as float). -1 makes poll wait indefinitely.\n\
1490Up to maxevents are returned to the caller.");
1491
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001492static PyObject *
1493pyepoll_enter(pyEpoll_Object *self, PyObject *args)
1494{
1495 if (self->epfd < 0)
1496 return pyepoll_err_closed();
1497
1498 Py_INCREF(self);
1499 return (PyObject *)self;
1500}
1501
1502static PyObject *
1503pyepoll_exit(PyObject *self, PyObject *args)
1504{
1505 _Py_IDENTIFIER(close);
1506
1507 return _PyObject_CallMethodId(self, &PyId_close, NULL);
1508}
1509
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001510static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 {"fromfd", (PyCFunction)pyepoll_fromfd,
1512 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1513 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1514 pyepoll_close_doc},
1515 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1516 pyepoll_fileno_doc},
1517 {"modify", (PyCFunction)pyepoll_modify,
1518 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1519 {"register", (PyCFunction)pyepoll_register,
1520 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1521 {"unregister", (PyCFunction)pyepoll_unregister,
1522 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1523 {"poll", (PyCFunction)pyepoll_poll,
1524 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001525 {"__enter__", (PyCFunction)pyepoll_enter, METH_NOARGS,
1526 NULL},
1527 {"__exit__", (PyCFunction)pyepoll_exit, METH_VARARGS,
1528 NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001530};
1531
1532static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 {"closed", (getter)pyepoll_get_closed, NULL,
1534 "True if the epoll handler is closed"},
1535 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001536};
1537
1538PyDoc_STRVAR(pyepoll_doc,
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001539"select.epoll(sizehint=-1, flags=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001540\n\
1541Returns an epolling object\n\
1542\n\
1543sizehint must be a positive integer or -1 for the default size. The\n\
1544sizehint is used to optimize internal data structures. It doesn't limit\n\
1545the maximum number of monitored events.");
1546
1547static PyTypeObject pyEpoll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 PyVarObject_HEAD_INIT(NULL, 0)
1549 "select.epoll", /* tp_name */
1550 sizeof(pyEpoll_Object), /* tp_basicsize */
1551 0, /* tp_itemsize */
1552 (destructor)pyepoll_dealloc, /* tp_dealloc */
1553 0, /* tp_print */
1554 0, /* tp_getattr */
1555 0, /* tp_setattr */
1556 0, /* tp_reserved */
1557 0, /* tp_repr */
1558 0, /* tp_as_number */
1559 0, /* tp_as_sequence */
1560 0, /* tp_as_mapping */
1561 0, /* tp_hash */
1562 0, /* tp_call */
1563 0, /* tp_str */
1564 PyObject_GenericGetAttr, /* tp_getattro */
1565 0, /* tp_setattro */
1566 0, /* tp_as_buffer */
1567 Py_TPFLAGS_DEFAULT, /* tp_flags */
1568 pyepoll_doc, /* tp_doc */
1569 0, /* tp_traverse */
1570 0, /* tp_clear */
1571 0, /* tp_richcompare */
1572 0, /* tp_weaklistoffset */
1573 0, /* tp_iter */
1574 0, /* tp_iternext */
1575 pyepoll_methods, /* tp_methods */
1576 0, /* tp_members */
1577 pyepoll_getsetlist, /* tp_getset */
1578 0, /* tp_base */
1579 0, /* tp_dict */
1580 0, /* tp_descr_get */
1581 0, /* tp_descr_set */
1582 0, /* tp_dictoffset */
1583 0, /* tp_init */
1584 0, /* tp_alloc */
1585 pyepoll_new, /* tp_new */
1586 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001587};
1588
1589#endif /* HAVE_EPOLL */
1590
1591#ifdef HAVE_KQUEUE
1592/* **************************************************************************
1593 * kqueue interface for BSD
1594 *
1595 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1596 * All rights reserved.
1597 *
1598 * Redistribution and use in source and binary forms, with or without
1599 * modification, are permitted provided that the following conditions
1600 * are met:
1601 * 1. Redistributions of source code must retain the above copyright
1602 * notice, this list of conditions and the following disclaimer.
1603 * 2. Redistributions in binary form must reproduce the above copyright
1604 * notice, this list of conditions and the following disclaimer in the
1605 * documentation and/or other materials provided with the distribution.
1606 *
1607 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1608 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1609 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1610 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1611 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1612 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1613 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1614 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1615 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1616 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1617 * SUCH DAMAGE.
1618 */
1619
1620#ifdef HAVE_SYS_EVENT_H
1621#include <sys/event.h>
1622#endif
1623
1624PyDoc_STRVAR(kqueue_event_doc,
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001625"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001626\n\
1627This object is the equivalent of the struct kevent for the C API.\n\
1628\n\
1629See the kqueue manpage for more detailed information about the meaning\n\
1630of the arguments.\n\
1631\n\
1632One minor note: while you might hope that udata could store a\n\
1633reference to a python object, it cannot, because it is impossible to\n\
1634keep a proper reference count of the object once it's passed into the\n\
1635kernel. Therefore, I have restricted it to only storing an integer. I\n\
1636recommend ignoring it and simply using the 'ident' field to key off\n\
1637of. You could also set up a dictionary on the python side to store a\n\
1638udata->object mapping.");
1639
1640typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 PyObject_HEAD
1642 struct kevent e;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001643} kqueue_event_Object;
1644
1645static PyTypeObject kqueue_event_Type;
1646
1647#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1648
1649typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 PyObject_HEAD
1651 SOCKET kqfd; /* kqueue control fd */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001652} kqueue_queue_Object;
1653
1654static PyTypeObject kqueue_queue_Type;
1655
1656#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1657
Antoine Pitroud83f1e62009-11-04 21:10:38 +00001658#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1659# error uintptr_t does not match void *!
1660#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1661# define T_UINTPTRT T_ULONGLONG
1662# define T_INTPTRT T_LONGLONG
1663# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1664# define UINTPTRT_FMT_UNIT "K"
1665# define INTPTRT_FMT_UNIT "L"
1666#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1667# define T_UINTPTRT T_ULONG
1668# define T_INTPTRT T_LONG
1669# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1670# define UINTPTRT_FMT_UNIT "k"
1671# define INTPTRT_FMT_UNIT "l"
1672#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1673# define T_UINTPTRT T_UINT
1674# define T_INTPTRT T_INT
1675# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1676# define UINTPTRT_FMT_UNIT "I"
1677# define INTPTRT_FMT_UNIT "i"
1678#else
1679# error uintptr_t does not match int, long, or long long!
1680#endif
1681
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001682/*
1683 * kevent is not standard and its members vary across BSDs.
1684 */
1685#if !defined(__OpenBSD__)
1686# define IDENT_TYPE T_UINTPTRT
1687# define IDENT_CAST Py_intptr_t
1688# define DATA_TYPE T_INTPTRT
1689# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
1690# define IDENT_AsType PyLong_AsUintptr_t
1691#else
1692# define IDENT_TYPE T_UINT
1693# define IDENT_CAST int
1694# define DATA_TYPE T_INT
1695# define DATA_FMT_UNIT "i"
1696# define IDENT_AsType PyLong_AsUnsignedLong
1697#endif
1698
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001699/* Unfortunately, we can't store python objects in udata, because
1700 * kevents in the kernel can be removed without warning, which would
1701 * forever lose the refcount on the object stored with it.
1702 */
1703
1704#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1705static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001706 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 {"filter", T_SHORT, KQ_OFF(e.filter)},
1708 {"flags", T_USHORT, KQ_OFF(e.flags)},
1709 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001710 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1712 {NULL} /* Sentinel */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001713};
1714#undef KQ_OFF
1715
1716static PyObject *
Georg Brandlc0e22b72010-03-14 10:51:01 +00001717
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001718kqueue_event_repr(kqueue_event_Object *s)
1719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 char buf[1024];
1721 PyOS_snprintf(
1722 buf, sizeof(buf),
1723 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1724 "data=0x%zd udata=%p>",
1725 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1726 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1727 return PyUnicode_FromString(buf);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001728}
1729
1730static int
1731kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 PyObject *pfd;
1734 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1735 "data", "udata", NULL};
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001736 static char *fmt = "O|hhi" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1741 &pfd, &(self->e.filter), &(self->e.flags),
1742 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1743 return -1;
1744 }
1745
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001746 if (PyLong_Check(pfd)
1747#if IDENT_TYPE == T_UINT
1748 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
1749#endif
1750 ) {
1751 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 }
1753 else {
1754 self->e.ident = PyObject_AsFileDescriptor(pfd);
1755 }
1756 if (PyErr_Occurred()) {
1757 return -1;
1758 }
1759 return 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001760}
1761
1762static PyObject *
1763kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 int op)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 Py_intptr_t result = 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 if (!kqueue_event_Check(o)) {
1769 if (op == Py_EQ || op == Py_NE) {
1770 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1771 Py_INCREF(res);
1772 return res;
1773 }
1774 PyErr_Format(PyExc_TypeError,
1775 "can't compare %.200s to %.200s",
1776 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1777 return NULL;
1778 }
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001779 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 ((result = s->e.filter - o->e.filter) == 0) &&
1781 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001782 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 ((result = s->e.data - o->e.data) == 0) &&
1784 ((result = s->e.udata - o->e.udata) == 0)
1785 ) {
1786 result = 0;
1787 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 switch (op) {
1790 case Py_EQ:
1791 result = (result == 0);
1792 break;
1793 case Py_NE:
1794 result = (result != 0);
1795 break;
1796 case Py_LE:
1797 result = (result <= 0);
1798 break;
1799 case Py_GE:
1800 result = (result >= 0);
1801 break;
1802 case Py_LT:
1803 result = (result < 0);
1804 break;
1805 case Py_GT:
1806 result = (result > 0);
1807 break;
1808 }
1809 return PyBool_FromLong((long)result);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001810}
1811
1812static PyTypeObject kqueue_event_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 PyVarObject_HEAD_INIT(NULL, 0)
1814 "select.kevent", /* tp_name */
1815 sizeof(kqueue_event_Object), /* tp_basicsize */
1816 0, /* tp_itemsize */
1817 0, /* tp_dealloc */
1818 0, /* tp_print */
1819 0, /* tp_getattr */
1820 0, /* tp_setattr */
1821 0, /* tp_reserved */
1822 (reprfunc)kqueue_event_repr, /* tp_repr */
1823 0, /* tp_as_number */
1824 0, /* tp_as_sequence */
1825 0, /* tp_as_mapping */
1826 0, /* tp_hash */
1827 0, /* tp_call */
1828 0, /* tp_str */
1829 0, /* tp_getattro */
1830 0, /* tp_setattro */
1831 0, /* tp_as_buffer */
1832 Py_TPFLAGS_DEFAULT, /* tp_flags */
1833 kqueue_event_doc, /* tp_doc */
1834 0, /* tp_traverse */
1835 0, /* tp_clear */
1836 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1837 0, /* tp_weaklistoffset */
1838 0, /* tp_iter */
1839 0, /* tp_iternext */
1840 0, /* tp_methods */
1841 kqueue_event_members, /* tp_members */
1842 0, /* tp_getset */
1843 0, /* tp_base */
1844 0, /* tp_dict */
1845 0, /* tp_descr_get */
1846 0, /* tp_descr_set */
1847 0, /* tp_dictoffset */
1848 (initproc)kqueue_event_init, /* tp_init */
1849 0, /* tp_alloc */
1850 0, /* tp_new */
1851 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001852};
1853
1854static PyObject *
1855kqueue_queue_err_closed(void)
1856{
Victor Stinner13423c32013-08-22 00:19:50 +02001857 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001859}
1860
1861static int
1862kqueue_queue_internal_close(kqueue_queue_Object *self)
1863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 int save_errno = 0;
1865 if (self->kqfd >= 0) {
1866 int kqfd = self->kqfd;
1867 self->kqfd = -1;
1868 Py_BEGIN_ALLOW_THREADS
1869 if (close(kqfd) < 0)
1870 save_errno = errno;
1871 Py_END_ALLOW_THREADS
1872 }
1873 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001874}
1875
1876static PyObject *
1877newKqueue_Object(PyTypeObject *type, SOCKET fd)
1878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 kqueue_queue_Object *self;
1880 assert(type != NULL && type->tp_alloc != NULL);
1881 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1882 if (self == NULL) {
1883 return NULL;
1884 }
1885
1886 if (fd == -1) {
1887 Py_BEGIN_ALLOW_THREADS
1888 self->kqfd = kqueue();
1889 Py_END_ALLOW_THREADS
1890 }
1891 else {
1892 self->kqfd = fd;
1893 }
1894 if (self->kqfd < 0) {
1895 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001896 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 return NULL;
1898 }
1899 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001900}
1901
1902static PyObject *
1903kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1904{
1905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 if ((args != NULL && PyObject_Size(args)) ||
1907 (kwds != NULL && PyObject_Size(kwds))) {
1908 PyErr_SetString(PyExc_ValueError,
1909 "select.kqueue doesn't accept arguments");
1910 return NULL;
1911 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 return newKqueue_Object(type, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001914}
1915
1916static void
1917kqueue_queue_dealloc(kqueue_queue_Object *self)
1918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 kqueue_queue_internal_close(self);
1920 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001921}
1922
1923static PyObject*
1924kqueue_queue_close(kqueue_queue_Object *self)
1925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 errno = kqueue_queue_internal_close(self);
1927 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001928 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 return NULL;
1930 }
1931 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001932}
1933
1934PyDoc_STRVAR(kqueue_queue_close_doc,
1935"close() -> None\n\
1936\n\
1937Close the kqueue control file descriptor. Further operations on the kqueue\n\
1938object will raise an exception.");
1939
1940static PyObject*
1941kqueue_queue_get_closed(kqueue_queue_Object *self)
1942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 if (self->kqfd < 0)
1944 Py_RETURN_TRUE;
1945 else
1946 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001947}
1948
1949static PyObject*
1950kqueue_queue_fileno(kqueue_queue_Object *self)
1951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (self->kqfd < 0)
1953 return kqueue_queue_err_closed();
1954 return PyLong_FromLong(self->kqfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001955}
1956
1957PyDoc_STRVAR(kqueue_queue_fileno_doc,
1958"fileno() -> int\n\
1959\n\
1960Return the kqueue control file descriptor.");
1961
1962static PyObject*
1963kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1968 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001971}
1972
1973PyDoc_STRVAR(kqueue_queue_fromfd_doc,
1974"fromfd(fd) -> kqueue\n\
1975\n\
1976Create a kqueue object from a given control fd.");
1977
1978static PyObject *
1979kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
1980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 int nevents = 0;
1982 int gotevents = 0;
1983 int nchanges = 0;
1984 int i = 0;
1985 PyObject *otimeout = NULL;
1986 PyObject *ch = NULL;
1987 PyObject *it = NULL, *ei = NULL;
1988 PyObject *result = NULL;
1989 struct kevent *evl = NULL;
1990 struct kevent *chl = NULL;
Victor Stinnerd327f9d2012-03-13 15:29:08 +01001991 struct timespec timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 struct timespec *ptimeoutspec;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 if (self->kqfd < 0)
1995 return kqueue_queue_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
1998 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 if (nevents < 0) {
2001 PyErr_Format(PyExc_ValueError,
2002 "Length of eventlist must be 0 or positive, got %d",
2003 nevents);
2004 return NULL;
2005 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 if (otimeout == Py_None || otimeout == NULL) {
2008 ptimeoutspec = NULL;
2009 }
2010 else if (PyNumber_Check(otimeout)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +01002011 if (_PyTime_ObjectToTimespec(otimeout,
2012 &timeout.tv_sec, &timeout.tv_nsec) == -1)
2013 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002014
Victor Stinner5d272cc2012-03-13 13:35:55 +01002015 if (timeout.tv_sec < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 PyErr_SetString(PyExc_ValueError,
2017 "timeout must be positive or None");
2018 return NULL;
2019 }
Victor Stinnerd528b012012-03-13 16:25:35 +01002020 ptimeoutspec = &timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 }
2022 else {
2023 PyErr_Format(PyExc_TypeError,
2024 "timeout argument must be an number "
2025 "or None, got %.200s",
2026 Py_TYPE(otimeout)->tp_name);
2027 return NULL;
2028 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 if (ch != NULL && ch != Py_None) {
2031 it = PyObject_GetIter(ch);
2032 if (it == NULL) {
2033 PyErr_SetString(PyExc_TypeError,
2034 "changelist is not iterable");
2035 return NULL;
2036 }
2037 nchanges = PyObject_Size(ch);
2038 if (nchanges < 0) {
2039 goto error;
2040 }
Georg Brandlc0e22b72010-03-14 10:51:01 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 chl = PyMem_New(struct kevent, nchanges);
2043 if (chl == NULL) {
2044 PyErr_NoMemory();
2045 goto error;
2046 }
2047 i = 0;
2048 while ((ei = PyIter_Next(it)) != NULL) {
2049 if (!kqueue_event_Check(ei)) {
2050 Py_DECREF(ei);
2051 PyErr_SetString(PyExc_TypeError,
2052 "changelist must be an iterable of "
2053 "select.kevent objects");
2054 goto error;
2055 } else {
2056 chl[i++] = ((kqueue_event_Object *)ei)->e;
2057 }
2058 Py_DECREF(ei);
2059 }
2060 }
2061 Py_CLEAR(it);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 /* event list */
2064 if (nevents) {
2065 evl = PyMem_New(struct kevent, nevents);
2066 if (evl == NULL) {
2067 PyErr_NoMemory();
2068 goto error;
2069 }
2070 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 Py_BEGIN_ALLOW_THREADS
2073 gotevents = kevent(self->kqfd, chl, nchanges,
2074 evl, nevents, ptimeoutspec);
2075 Py_END_ALLOW_THREADS
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 if (gotevents == -1) {
2078 PyErr_SetFromErrno(PyExc_OSError);
2079 goto error;
2080 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 result = PyList_New(gotevents);
2083 if (result == NULL) {
2084 goto error;
2085 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 for (i = 0; i < gotevents; i++) {
2088 kqueue_event_Object *ch;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
2091 if (ch == NULL) {
2092 goto error;
2093 }
2094 ch->e = evl[i];
2095 PyList_SET_ITEM(result, i, (PyObject *)ch);
2096 }
2097 PyMem_Free(chl);
2098 PyMem_Free(evl);
2099 return result;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002100
2101 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 PyMem_Free(chl);
2103 PyMem_Free(evl);
2104 Py_XDECREF(result);
2105 Py_XDECREF(it);
2106 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002107}
2108
2109PyDoc_STRVAR(kqueue_queue_control_doc,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00002110"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002111\n\
2112Calls the kernel kevent function.\n\
2113- changelist must be a list of kevent objects describing the changes\n\
2114 to be made to the kernel's watch list or None.\n\
2115- max_events lets you specify the maximum number of events that the\n\
2116 kernel will return.\n\
2117- timeout is the maximum time to wait in seconds, or else None,\n\
2118 to wait forever. timeout accepts floats for smaller timeouts, too.");
2119
2120
2121static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
2123 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
2124 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
2125 kqueue_queue_close_doc},
2126 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
2127 kqueue_queue_fileno_doc},
2128 {"control", (PyCFunction)kqueue_queue_control,
2129 METH_VARARGS , kqueue_queue_control_doc},
2130 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002131};
2132
2133static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 {"closed", (getter)kqueue_queue_get_closed, NULL,
2135 "True if the kqueue handler is closed"},
2136 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002137};
2138
2139PyDoc_STRVAR(kqueue_queue_doc,
2140"Kqueue syscall wrapper.\n\
2141\n\
2142For example, to start watching a socket for input:\n\
2143>>> kq = kqueue()\n\
2144>>> sock = socket()\n\
2145>>> sock.connect((host, port))\n\
2146>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
2147\n\
2148To wait one second for it to become writeable:\n\
2149>>> kq.control(None, 1, 1000)\n\
2150\n\
2151To stop listening:\n\
2152>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
2153
2154static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 PyVarObject_HEAD_INIT(NULL, 0)
2156 "select.kqueue", /* tp_name */
2157 sizeof(kqueue_queue_Object), /* tp_basicsize */
2158 0, /* tp_itemsize */
2159 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
2160 0, /* tp_print */
2161 0, /* tp_getattr */
2162 0, /* tp_setattr */
2163 0, /* tp_reserved */
2164 0, /* tp_repr */
2165 0, /* tp_as_number */
2166 0, /* tp_as_sequence */
2167 0, /* tp_as_mapping */
2168 0, /* tp_hash */
2169 0, /* tp_call */
2170 0, /* tp_str */
2171 0, /* tp_getattro */
2172 0, /* tp_setattro */
2173 0, /* tp_as_buffer */
2174 Py_TPFLAGS_DEFAULT, /* tp_flags */
2175 kqueue_queue_doc, /* tp_doc */
2176 0, /* tp_traverse */
2177 0, /* tp_clear */
2178 0, /* tp_richcompare */
2179 0, /* tp_weaklistoffset */
2180 0, /* tp_iter */
2181 0, /* tp_iternext */
2182 kqueue_queue_methods, /* tp_methods */
2183 0, /* tp_members */
2184 kqueue_queue_getsetlist, /* tp_getset */
2185 0, /* tp_base */
2186 0, /* tp_dict */
2187 0, /* tp_descr_get */
2188 0, /* tp_descr_set */
2189 0, /* tp_dictoffset */
2190 0, /* tp_init */
2191 0, /* tp_alloc */
2192 kqueue_queue_new, /* tp_new */
2193 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002194};
2195
2196#endif /* HAVE_KQUEUE */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002197
2198
2199
2200
2201
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002202/* ************************************************************************ */
2203
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002204PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002205"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
2206\n\
2207Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00002208The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002209rlist -- wait until ready for reading\n\
2210wlist -- wait until ready for writing\n\
2211xlist -- wait for an ``exceptional condition''\n\
2212If only one kind of condition is required, pass [] for the other lists.\n\
2213A file descriptor is either a socket or file object, or a small integer\n\
2214gotten from a fileno() method call on one of those.\n\
2215\n\
2216The optional 4th argument specifies a timeout in seconds; it may be\n\
2217a floating point number to specify fractions of seconds. If it is absent\n\
2218or None, the call will never time out.\n\
2219\n\
2220The return value is a tuple of three lists corresponding to the first three\n\
2221arguments; each contains the subset of the corresponding file descriptors\n\
2222that are ready.\n\
2223\n\
2224*** IMPORTANT NOTICE ***\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002225On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00002226descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002227
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002228static PyMethodDef select_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natali986a56c2013-01-19 12:19:10 +01002230#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002232#endif /* HAVE_POLL */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002233#ifdef HAVE_SYS_DEVPOLL_H
2234 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2235#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00002237};
2238
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002239PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002240"This module supports asynchronous I/O on multiple file descriptors.\n\
2241\n\
2242*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002243On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00002244
Martin v. Löwis1a214512008-06-11 05:26:20 +00002245
2246static struct PyModuleDef selectmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 PyModuleDef_HEAD_INIT,
2248 "select",
2249 module_doc,
2250 -1,
2251 select_methods,
2252 NULL,
2253 NULL,
2254 NULL,
2255 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002256};
2257
Jesus Cead8b9ae62011-11-14 19:07:41 +01002258
2259
2260
Mark Hammond62b1ab12002-07-23 06:31:15 +00002261PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002262PyInit_select(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00002263{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 PyObject *m;
2265 m = PyModule_Create(&selectmodule);
2266 if (m == NULL)
2267 return NULL;
Fred Drake4baedc12002-04-01 14:53:37 +00002268
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002269 Py_INCREF(PyExc_OSError);
2270 PyModule_AddObject(m, "error", PyExc_OSError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002271
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002272#ifdef PIPE_BUF
R. David Murraye16cda92010-10-15 23:12:57 +00002273#ifdef HAVE_BROKEN_PIPE_BUF
2274#undef PIPE_BUF
2275#define PIPE_BUF 512
2276#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002277 PyModule_AddIntMacro(m, PIPE_BUF);
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002278#endif
Gregory P. Smithb970b862009-07-04 02:28:47 +00002279
Charles-François Natali986a56c2013-01-19 12:19:10 +01002280#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002281#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 if (select_have_broken_poll()) {
2283 if (PyObject_DelAttrString(m, "poll") == -1) {
2284 PyErr_Clear();
2285 }
2286 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002287#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002289#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 if (PyType_Ready(&poll_Type) < 0)
2291 return NULL;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002292 PyModule_AddIntMacro(m, POLLIN);
2293 PyModule_AddIntMacro(m, POLLPRI);
2294 PyModule_AddIntMacro(m, POLLOUT);
2295 PyModule_AddIntMacro(m, POLLERR);
2296 PyModule_AddIntMacro(m, POLLHUP);
2297 PyModule_AddIntMacro(m, POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00002298
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002299#ifdef POLLRDNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002300 PyModule_AddIntMacro(m, POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002301#endif
2302#ifdef POLLRDBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002303 PyModule_AddIntMacro(m, POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002304#endif
2305#ifdef POLLWRNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002306 PyModule_AddIntMacro(m, POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002307#endif
2308#ifdef POLLWRBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002309 PyModule_AddIntMacro(m, POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002310#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002311#ifdef POLLMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002312 PyModule_AddIntMacro(m, POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002313#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002315#endif /* HAVE_POLL */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002316
Jesus Cead8b9ae62011-11-14 19:07:41 +01002317#ifdef HAVE_SYS_DEVPOLL_H
2318 if (PyType_Ready(&devpoll_Type) < 0)
2319 return NULL;
2320#endif
2321
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002322#ifdef HAVE_EPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
2324 if (PyType_Ready(&pyEpoll_Type) < 0)
2325 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 Py_INCREF(&pyEpoll_Type);
2328 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002329
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002330 PyModule_AddIntMacro(m, EPOLLIN);
2331 PyModule_AddIntMacro(m, EPOLLOUT);
2332 PyModule_AddIntMacro(m, EPOLLPRI);
2333 PyModule_AddIntMacro(m, EPOLLERR);
2334 PyModule_AddIntMacro(m, EPOLLHUP);
2335 PyModule_AddIntMacro(m, EPOLLET);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002336#ifdef EPOLLONESHOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 /* Kernel 2.6.2+ */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002338 PyModule_AddIntMacro(m, EPOLLONESHOT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002339#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002341 PyModule_AddIntMacro(m, EPOLLRDNORM);
2342 PyModule_AddIntMacro(m, EPOLLRDBAND);
2343 PyModule_AddIntMacro(m, EPOLLWRNORM);
2344 PyModule_AddIntMacro(m, EPOLLWRBAND);
2345 PyModule_AddIntMacro(m, EPOLLMSG);
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002346
Benjamin Peterson95c16622011-12-27 15:36:32 -06002347#ifdef EPOLL_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002348 PyModule_AddIntMacro(m, EPOLL_CLOEXEC);
Benjamin Peterson95c16622011-12-27 15:36:32 -06002349#endif
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002350#endif /* HAVE_EPOLL */
2351
2352#ifdef HAVE_KQUEUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 kqueue_event_Type.tp_new = PyType_GenericNew;
2354 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
2355 if(PyType_Ready(&kqueue_event_Type) < 0)
2356 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 Py_INCREF(&kqueue_event_Type);
2359 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
2362 if(PyType_Ready(&kqueue_queue_Type) < 0)
2363 return NULL;
2364 Py_INCREF(&kqueue_queue_Type);
2365 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
2366
2367 /* event filters */
2368 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
2369 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
2370 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
2371 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
2372 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002373#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002375#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
2377 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 /* event flags */
2380 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
2381 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
2382 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
2383 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
2384 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
2385 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
2388 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
2391 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* READ WRITE filter flag */
2394 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 /* VNODE filter flags */
2397 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
2398 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
2399 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
2400 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
2401 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
2402 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
2403 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 /* PROC filter flags */
2406 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
2407 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
2408 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
2409 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
2410 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
2413 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
2414 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
2415
2416 /* NETDEV filter flags */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002417#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
2419 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
2420 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002421#endif
2422
2423#endif /* HAVE_KQUEUE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 return m;
Guido van Rossumed233a51992-06-23 09:07:03 +00002425}