blob: 6291a2daa929eb116a8f7536e8c9b18f98d229df [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
Richard Oudkerk168d59b2013-08-22 13:31:15 +0100914static int
915devpoll_internal_close(devpollObject *self)
916{
917 int save_errno = 0;
918 if (self->fd_devpoll >= 0) {
919 int fd = self->fd_devpoll;
920 self->fd_devpoll = -1;
921 Py_BEGIN_ALLOW_THREADS
922 if (close(fd) < 0)
923 save_errno = errno;
924 Py_END_ALLOW_THREADS
925 }
926 return save_errno;
927}
928
Victor Stinner13423c32013-08-22 00:19:50 +0200929static PyObject*
930devpoll_close(devpollObject *self)
931{
932 errno = devpoll_internal_close(self);
933 if (errno < 0) {
934 PyErr_SetFromErrno(PyExc_OSError);
935 return NULL;
936 }
937 Py_RETURN_NONE;
938}
939
940PyDoc_STRVAR(devpoll_close_doc,
941"close() -> None\n\
942\n\
943Close the devpoll file descriptor. Further operations on the devpoll\n\
944object will raise an exception.");
945
946static PyObject*
947devpoll_get_closed(devpollObject *self)
948{
949 if (self->fd_devpoll < 0)
950 Py_RETURN_TRUE;
951 else
952 Py_RETURN_FALSE;
953}
954
955static PyObject*
956devpoll_fileno(devpollObject *self)
957{
958 if (self->fd_devpoll < 0)
959 return devpoll_err_closed();
960 return PyLong_FromLong(self->fd_devpoll);
961}
962
963PyDoc_STRVAR(devpoll_fileno_doc,
964"fileno() -> int\n\
965\n\
966Return the file descriptor.");
967
Jesus Cead8b9ae62011-11-14 19:07:41 +0100968static PyMethodDef devpoll_methods[] = {
969 {"register", (PyCFunction)devpoll_register,
970 METH_VARARGS, devpoll_register_doc},
971 {"modify", (PyCFunction)devpoll_modify,
972 METH_VARARGS, devpoll_modify_doc},
973 {"unregister", (PyCFunction)devpoll_unregister,
974 METH_O, devpoll_unregister_doc},
975 {"poll", (PyCFunction)devpoll_poll,
976 METH_VARARGS, devpoll_poll_doc},
Victor Stinner13423c32013-08-22 00:19:50 +0200977 {"close", (PyCFunction)devpoll_close, METH_NOARGS,
978 devpoll_close_doc},
979 {"fileno", (PyCFunction)devpoll_fileno, METH_NOARGS,
980 devpoll_fileno_doc},
Jesus Cead8b9ae62011-11-14 19:07:41 +0100981 {NULL, NULL} /* sentinel */
982};
983
Victor Stinner13423c32013-08-22 00:19:50 +0200984static PyGetSetDef devpoll_getsetlist[] = {
985 {"closed", (getter)devpoll_get_closed, NULL,
986 "True if the devpoll object is closed"},
987 {0},
988};
989
Jesus Cead8b9ae62011-11-14 19:07:41 +0100990static devpollObject *
991newDevPollObject(void)
992{
993 devpollObject *self;
994 int fd_devpoll, limit_result;
995 struct pollfd *fds;
996 struct rlimit limit;
997
998 Py_BEGIN_ALLOW_THREADS
999 /*
1000 ** If we try to process more that getrlimit()
1001 ** fds, the kernel will give an error, so
1002 ** we set the limit here. It is a dynamic
1003 ** value, because we can change rlimit() anytime.
1004 */
1005 limit_result = getrlimit(RLIMIT_NOFILE, &limit);
1006 if (limit_result != -1)
Victor Stinnerdaf45552013-08-28 00:53:59 +02001007 fd_devpoll = _Py_open("/dev/poll", O_RDWR);
Jesus Cead8b9ae62011-11-14 19:07:41 +01001008 Py_END_ALLOW_THREADS
1009
1010 if (limit_result == -1) {
1011 PyErr_SetFromErrno(PyExc_OSError);
1012 return NULL;
1013 }
1014 if (fd_devpoll == -1) {
1015 PyErr_SetFromErrnoWithFilename(PyExc_IOError, "/dev/poll");
1016 return NULL;
1017 }
1018
1019 fds = PyMem_NEW(struct pollfd, limit.rlim_cur);
1020 if (fds == NULL) {
1021 close(fd_devpoll);
1022 PyErr_NoMemory();
1023 return NULL;
1024 }
1025
1026 self = PyObject_New(devpollObject, &devpoll_Type);
1027 if (self == NULL) {
1028 close(fd_devpoll);
1029 PyMem_DEL(fds);
1030 return NULL;
1031 }
1032 self->fd_devpoll = fd_devpoll;
1033 self->max_n_fds = limit.rlim_cur;
1034 self->n_fds = 0;
1035 self->fds = fds;
1036
1037 return self;
1038}
1039
1040static void
1041devpoll_dealloc(devpollObject *self)
1042{
Richard Oudkerka93bf7b2013-08-22 14:03:44 +01001043 (void)devpoll_internal_close(self);
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
Victor Stinnerdaf45552013-08-28 00:53:59 +02001197 flags |= EPOLL_CLOEXEC;
Benjamin Peterson83251c12011-12-27 16:01:21 -06001198 if (flags)
1199 self->epfd = epoll_create1(flags);
1200 else
Benjamin Peterson95c16622011-12-27 15:36:32 -06001201#endif
Benjamin Peterson83251c12011-12-27 16:01:21 -06001202 self->epfd = epoll_create(sizehint);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 Py_END_ALLOW_THREADS
1204 }
1205 else {
1206 self->epfd = fd;
1207 }
1208 if (self->epfd < 0) {
1209 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001210 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 return NULL;
1212 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001213
1214#ifndef HAVE_EPOLL_CREATE1
Victor Stinnerd72fe892013-08-28 12:22:39 +02001215 if (fd == -1 && _Py_set_inheritable(self->epfd, 0, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001216 Py_DECREF(self);
1217 return NULL;
1218 }
1219#endif
1220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001222}
1223
1224
1225static PyObject *
1226pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1227{
Benjamin Peterson83251c12011-12-27 16:01:21 -06001228 int flags = 0, sizehint = FD_SETSIZE - 1;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001229 static char *kwlist[] = {"sizehint", "flags", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001230
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001231 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
1232 &sizehint, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 return NULL;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001234 if (sizehint < 0) {
1235 PyErr_SetString(PyExc_ValueError, "negative sizehint");
1236 return NULL;
1237 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001238
Benjamin Peterson95c16622011-12-27 15:36:32 -06001239 return newPyEpoll_Object(type, sizehint, flags, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001240}
1241
1242
1243static void
1244pyepoll_dealloc(pyEpoll_Object *self)
1245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 (void)pyepoll_internal_close(self);
1247 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001248}
1249
1250static PyObject*
1251pyepoll_close(pyEpoll_Object *self)
1252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 errno = pyepoll_internal_close(self);
1254 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001255 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 return NULL;
1257 }
1258 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001259}
1260
1261PyDoc_STRVAR(pyepoll_close_doc,
1262"close() -> None\n\
1263\n\
1264Close the epoll control file descriptor. Further operations on the epoll\n\
1265object will raise an exception.");
1266
1267static PyObject*
1268pyepoll_get_closed(pyEpoll_Object *self)
1269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (self->epfd < 0)
1271 Py_RETURN_TRUE;
1272 else
1273 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001274}
1275
1276static PyObject*
1277pyepoll_fileno(pyEpoll_Object *self)
1278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (self->epfd < 0)
1280 return pyepoll_err_closed();
1281 return PyLong_FromLong(self->epfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001282}
1283
1284PyDoc_STRVAR(pyepoll_fileno_doc,
1285"fileno() -> int\n\
1286\n\
1287Return the epoll control file descriptor.");
1288
1289static PyObject*
1290pyepoll_fromfd(PyObject *cls, PyObject *args)
1291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1295 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001296
Benjamin Peterson95c16622011-12-27 15:36:32 -06001297 return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001298}
1299
1300PyDoc_STRVAR(pyepoll_fromfd_doc,
1301"fromfd(fd) -> epoll\n\
1302\n\
1303Create an epoll object from a given control fd.");
1304
1305static PyObject *
1306pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
1307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 struct epoll_event ev;
1309 int result;
1310 int fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if (epfd < 0)
1313 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 fd = PyObject_AsFileDescriptor(pfd);
1316 if (fd == -1) {
1317 return NULL;
1318 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 switch(op) {
1321 case EPOLL_CTL_ADD:
1322 case EPOLL_CTL_MOD:
1323 ev.events = events;
1324 ev.data.fd = fd;
1325 Py_BEGIN_ALLOW_THREADS
1326 result = epoll_ctl(epfd, op, fd, &ev);
1327 Py_END_ALLOW_THREADS
1328 break;
1329 case EPOLL_CTL_DEL:
1330 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
1331 * operation required a non-NULL pointer in event, even
1332 * though this argument is ignored. */
1333 Py_BEGIN_ALLOW_THREADS
1334 result = epoll_ctl(epfd, op, fd, &ev);
1335 if (errno == EBADF) {
1336 /* fd already closed */
1337 result = 0;
1338 errno = 0;
1339 }
1340 Py_END_ALLOW_THREADS
1341 break;
1342 default:
1343 result = -1;
1344 errno = EINVAL;
1345 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 if (result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001348 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 return NULL;
1350 }
1351 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001352}
1353
1354static PyObject *
1355pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 PyObject *pfd;
1358 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
1359 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
1362 &pfd, &events)) {
1363 return NULL;
1364 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001367}
1368
1369PyDoc_STRVAR(pyepoll_register_doc,
Georg Brandl222569d2010-08-02 20:47:56 +00001370"register(fd[, eventmask]) -> None\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001371\n\
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001372Registers a new fd or raises an OSError if the fd is already registered.\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00001373fd is the target file descriptor of the operation.\n\
1374events is a bit set composed of the various EPOLL constants; the default\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001375is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
1376\n\
1377The epoll interface supports all file descriptors that support poll.");
1378
1379static PyObject *
1380pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 PyObject *pfd;
1383 unsigned int events;
1384 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
1387 &pfd, &events)) {
1388 return NULL;
1389 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001392}
1393
1394PyDoc_STRVAR(pyepoll_modify_doc,
1395"modify(fd, eventmask) -> None\n\
1396\n\
1397fd is the target file descriptor of the operation\n\
1398events is a bit set composed of the various EPOLL constants");
1399
1400static PyObject *
1401pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 PyObject *pfd;
1404 static char *kwlist[] = {"fd", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
1407 &pfd)) {
1408 return NULL;
1409 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001412}
1413
1414PyDoc_STRVAR(pyepoll_unregister_doc,
1415"unregister(fd) -> None\n\
1416\n\
1417fd is the target file descriptor of the operation.");
1418
1419static PyObject *
1420pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 double dtimeout = -1.;
1423 int timeout;
1424 int maxevents = -1;
1425 int nfds, i;
1426 PyObject *elist = NULL, *etuple = NULL;
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001427 struct epoll_event *evs = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 static char *kwlist[] = {"timeout", "maxevents", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 if (self->epfd < 0)
1431 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
1434 &dtimeout, &maxevents)) {
1435 return NULL;
1436 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (dtimeout < 0) {
1439 timeout = -1;
1440 }
1441 else if (dtimeout * 1000.0 > INT_MAX) {
1442 PyErr_SetString(PyExc_OverflowError,
1443 "timeout is too large");
1444 return NULL;
1445 }
1446 else {
1447 timeout = (int)(dtimeout * 1000.0);
1448 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 if (maxevents == -1) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001451 maxevents = FD_SETSIZE-1;
1452 }
1453 else if (maxevents < 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 PyErr_Format(PyExc_ValueError,
1455 "maxevents must be greater than 0, got %d",
1456 maxevents);
1457 return NULL;
1458 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001459
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001460 evs = PyMem_New(struct epoll_event, maxevents);
1461 if (evs == NULL) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001462 PyErr_NoMemory();
1463 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 Py_BEGIN_ALLOW_THREADS
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001467 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 Py_END_ALLOW_THREADS
1469 if (nfds < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001470 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 goto error;
1472 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 elist = PyList_New(nfds);
1475 if (elist == NULL) {
1476 goto error;
1477 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 for (i = 0; i < nfds; i++) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001480 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 if (etuple == NULL) {
1482 Py_CLEAR(elist);
1483 goto error;
1484 }
1485 PyList_SET_ITEM(elist, i, etuple);
1486 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001487
Christian Heimesf6cd9672008-03-26 13:45:42 +00001488 error:
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001489 PyMem_Free(evs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 return elist;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001491}
1492
1493PyDoc_STRVAR(pyepoll_poll_doc,
1494"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1495\n\
1496Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1497in seconds (as float). -1 makes poll wait indefinitely.\n\
1498Up to maxevents are returned to the caller.");
1499
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001500static PyObject *
1501pyepoll_enter(pyEpoll_Object *self, PyObject *args)
1502{
1503 if (self->epfd < 0)
1504 return pyepoll_err_closed();
1505
1506 Py_INCREF(self);
1507 return (PyObject *)self;
1508}
1509
1510static PyObject *
1511pyepoll_exit(PyObject *self, PyObject *args)
1512{
1513 _Py_IDENTIFIER(close);
1514
1515 return _PyObject_CallMethodId(self, &PyId_close, NULL);
1516}
1517
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001518static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 {"fromfd", (PyCFunction)pyepoll_fromfd,
1520 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1521 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1522 pyepoll_close_doc},
1523 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1524 pyepoll_fileno_doc},
1525 {"modify", (PyCFunction)pyepoll_modify,
1526 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1527 {"register", (PyCFunction)pyepoll_register,
1528 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1529 {"unregister", (PyCFunction)pyepoll_unregister,
1530 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1531 {"poll", (PyCFunction)pyepoll_poll,
1532 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001533 {"__enter__", (PyCFunction)pyepoll_enter, METH_NOARGS,
1534 NULL},
1535 {"__exit__", (PyCFunction)pyepoll_exit, METH_VARARGS,
1536 NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001538};
1539
1540static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 {"closed", (getter)pyepoll_get_closed, NULL,
1542 "True if the epoll handler is closed"},
1543 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001544};
1545
1546PyDoc_STRVAR(pyepoll_doc,
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001547"select.epoll(sizehint=-1, flags=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001548\n\
1549Returns an epolling object\n\
1550\n\
1551sizehint must be a positive integer or -1 for the default size. The\n\
1552sizehint is used to optimize internal data structures. It doesn't limit\n\
1553the maximum number of monitored events.");
1554
1555static PyTypeObject pyEpoll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 PyVarObject_HEAD_INIT(NULL, 0)
1557 "select.epoll", /* tp_name */
1558 sizeof(pyEpoll_Object), /* tp_basicsize */
1559 0, /* tp_itemsize */
1560 (destructor)pyepoll_dealloc, /* tp_dealloc */
1561 0, /* tp_print */
1562 0, /* tp_getattr */
1563 0, /* tp_setattr */
1564 0, /* tp_reserved */
1565 0, /* tp_repr */
1566 0, /* tp_as_number */
1567 0, /* tp_as_sequence */
1568 0, /* tp_as_mapping */
1569 0, /* tp_hash */
1570 0, /* tp_call */
1571 0, /* tp_str */
1572 PyObject_GenericGetAttr, /* tp_getattro */
1573 0, /* tp_setattro */
1574 0, /* tp_as_buffer */
1575 Py_TPFLAGS_DEFAULT, /* tp_flags */
1576 pyepoll_doc, /* tp_doc */
1577 0, /* tp_traverse */
1578 0, /* tp_clear */
1579 0, /* tp_richcompare */
1580 0, /* tp_weaklistoffset */
1581 0, /* tp_iter */
1582 0, /* tp_iternext */
1583 pyepoll_methods, /* tp_methods */
1584 0, /* tp_members */
1585 pyepoll_getsetlist, /* tp_getset */
1586 0, /* tp_base */
1587 0, /* tp_dict */
1588 0, /* tp_descr_get */
1589 0, /* tp_descr_set */
1590 0, /* tp_dictoffset */
1591 0, /* tp_init */
1592 0, /* tp_alloc */
1593 pyepoll_new, /* tp_new */
1594 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001595};
1596
1597#endif /* HAVE_EPOLL */
1598
1599#ifdef HAVE_KQUEUE
1600/* **************************************************************************
1601 * kqueue interface for BSD
1602 *
1603 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1604 * All rights reserved.
1605 *
1606 * Redistribution and use in source and binary forms, with or without
1607 * modification, are permitted provided that the following conditions
1608 * are met:
1609 * 1. Redistributions of source code must retain the above copyright
1610 * notice, this list of conditions and the following disclaimer.
1611 * 2. Redistributions in binary form must reproduce the above copyright
1612 * notice, this list of conditions and the following disclaimer in the
1613 * documentation and/or other materials provided with the distribution.
1614 *
1615 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1616 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1617 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1618 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1619 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1620 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1621 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1622 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1623 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1624 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1625 * SUCH DAMAGE.
1626 */
1627
1628#ifdef HAVE_SYS_EVENT_H
1629#include <sys/event.h>
1630#endif
1631
1632PyDoc_STRVAR(kqueue_event_doc,
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001633"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001634\n\
1635This object is the equivalent of the struct kevent for the C API.\n\
1636\n\
1637See the kqueue manpage for more detailed information about the meaning\n\
1638of the arguments.\n\
1639\n\
1640One minor note: while you might hope that udata could store a\n\
1641reference to a python object, it cannot, because it is impossible to\n\
1642keep a proper reference count of the object once it's passed into the\n\
1643kernel. Therefore, I have restricted it to only storing an integer. I\n\
1644recommend ignoring it and simply using the 'ident' field to key off\n\
1645of. You could also set up a dictionary on the python side to store a\n\
1646udata->object mapping.");
1647
1648typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 PyObject_HEAD
1650 struct kevent e;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001651} kqueue_event_Object;
1652
1653static PyTypeObject kqueue_event_Type;
1654
1655#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1656
1657typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 PyObject_HEAD
1659 SOCKET kqfd; /* kqueue control fd */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001660} kqueue_queue_Object;
1661
1662static PyTypeObject kqueue_queue_Type;
1663
1664#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1665
Antoine Pitroud83f1e62009-11-04 21:10:38 +00001666#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1667# error uintptr_t does not match void *!
1668#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1669# define T_UINTPTRT T_ULONGLONG
1670# define T_INTPTRT T_LONGLONG
1671# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1672# define UINTPTRT_FMT_UNIT "K"
1673# define INTPTRT_FMT_UNIT "L"
1674#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1675# define T_UINTPTRT T_ULONG
1676# define T_INTPTRT T_LONG
1677# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1678# define UINTPTRT_FMT_UNIT "k"
1679# define INTPTRT_FMT_UNIT "l"
1680#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1681# define T_UINTPTRT T_UINT
1682# define T_INTPTRT T_INT
1683# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1684# define UINTPTRT_FMT_UNIT "I"
1685# define INTPTRT_FMT_UNIT "i"
1686#else
1687# error uintptr_t does not match int, long, or long long!
1688#endif
1689
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001690/*
1691 * kevent is not standard and its members vary across BSDs.
1692 */
1693#if !defined(__OpenBSD__)
1694# define IDENT_TYPE T_UINTPTRT
1695# define IDENT_CAST Py_intptr_t
1696# define DATA_TYPE T_INTPTRT
1697# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
1698# define IDENT_AsType PyLong_AsUintptr_t
1699#else
1700# define IDENT_TYPE T_UINT
1701# define IDENT_CAST int
1702# define DATA_TYPE T_INT
1703# define DATA_FMT_UNIT "i"
1704# define IDENT_AsType PyLong_AsUnsignedLong
1705#endif
1706
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001707/* Unfortunately, we can't store python objects in udata, because
1708 * kevents in the kernel can be removed without warning, which would
1709 * forever lose the refcount on the object stored with it.
1710 */
1711
1712#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1713static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001714 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 {"filter", T_SHORT, KQ_OFF(e.filter)},
1716 {"flags", T_USHORT, KQ_OFF(e.flags)},
1717 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001718 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1720 {NULL} /* Sentinel */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001721};
1722#undef KQ_OFF
1723
1724static PyObject *
Georg Brandlc0e22b72010-03-14 10:51:01 +00001725
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001726kqueue_event_repr(kqueue_event_Object *s)
1727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 char buf[1024];
1729 PyOS_snprintf(
1730 buf, sizeof(buf),
1731 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1732 "data=0x%zd udata=%p>",
1733 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1734 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1735 return PyUnicode_FromString(buf);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001736}
1737
1738static int
1739kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 PyObject *pfd;
1742 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1743 "data", "udata", NULL};
Christian Heimesf1fe1592013-08-25 14:57:00 +02001744 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1749 &pfd, &(self->e.filter), &(self->e.flags),
1750 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1751 return -1;
1752 }
1753
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001754 if (PyLong_Check(pfd)
1755#if IDENT_TYPE == T_UINT
1756 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
1757#endif
1758 ) {
1759 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 }
1761 else {
1762 self->e.ident = PyObject_AsFileDescriptor(pfd);
1763 }
1764 if (PyErr_Occurred()) {
1765 return -1;
1766 }
1767 return 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001768}
1769
1770static PyObject *
1771kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 int op)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 Py_intptr_t result = 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 if (!kqueue_event_Check(o)) {
1777 if (op == Py_EQ || op == Py_NE) {
1778 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1779 Py_INCREF(res);
1780 return res;
1781 }
1782 PyErr_Format(PyExc_TypeError,
1783 "can't compare %.200s to %.200s",
1784 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1785 return NULL;
1786 }
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001787 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 ((result = s->e.filter - o->e.filter) == 0) &&
1789 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001790 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 ((result = s->e.data - o->e.data) == 0) &&
1792 ((result = s->e.udata - o->e.udata) == 0)
1793 ) {
1794 result = 0;
1795 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 switch (op) {
1798 case Py_EQ:
1799 result = (result == 0);
1800 break;
1801 case Py_NE:
1802 result = (result != 0);
1803 break;
1804 case Py_LE:
1805 result = (result <= 0);
1806 break;
1807 case Py_GE:
1808 result = (result >= 0);
1809 break;
1810 case Py_LT:
1811 result = (result < 0);
1812 break;
1813 case Py_GT:
1814 result = (result > 0);
1815 break;
1816 }
1817 return PyBool_FromLong((long)result);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001818}
1819
1820static PyTypeObject kqueue_event_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 PyVarObject_HEAD_INIT(NULL, 0)
1822 "select.kevent", /* tp_name */
1823 sizeof(kqueue_event_Object), /* tp_basicsize */
1824 0, /* tp_itemsize */
1825 0, /* tp_dealloc */
1826 0, /* tp_print */
1827 0, /* tp_getattr */
1828 0, /* tp_setattr */
1829 0, /* tp_reserved */
1830 (reprfunc)kqueue_event_repr, /* tp_repr */
1831 0, /* tp_as_number */
1832 0, /* tp_as_sequence */
1833 0, /* tp_as_mapping */
1834 0, /* tp_hash */
1835 0, /* tp_call */
1836 0, /* tp_str */
1837 0, /* tp_getattro */
1838 0, /* tp_setattro */
1839 0, /* tp_as_buffer */
1840 Py_TPFLAGS_DEFAULT, /* tp_flags */
1841 kqueue_event_doc, /* tp_doc */
1842 0, /* tp_traverse */
1843 0, /* tp_clear */
1844 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1845 0, /* tp_weaklistoffset */
1846 0, /* tp_iter */
1847 0, /* tp_iternext */
1848 0, /* tp_methods */
1849 kqueue_event_members, /* tp_members */
1850 0, /* tp_getset */
1851 0, /* tp_base */
1852 0, /* tp_dict */
1853 0, /* tp_descr_get */
1854 0, /* tp_descr_set */
1855 0, /* tp_dictoffset */
1856 (initproc)kqueue_event_init, /* tp_init */
1857 0, /* tp_alloc */
1858 0, /* tp_new */
1859 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001860};
1861
1862static PyObject *
1863kqueue_queue_err_closed(void)
1864{
Victor Stinner13423c32013-08-22 00:19:50 +02001865 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001867}
1868
1869static int
1870kqueue_queue_internal_close(kqueue_queue_Object *self)
1871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 int save_errno = 0;
1873 if (self->kqfd >= 0) {
1874 int kqfd = self->kqfd;
1875 self->kqfd = -1;
1876 Py_BEGIN_ALLOW_THREADS
1877 if (close(kqfd) < 0)
1878 save_errno = errno;
1879 Py_END_ALLOW_THREADS
1880 }
1881 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001882}
1883
1884static PyObject *
1885newKqueue_Object(PyTypeObject *type, SOCKET fd)
1886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 kqueue_queue_Object *self;
1888 assert(type != NULL && type->tp_alloc != NULL);
1889 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1890 if (self == NULL) {
1891 return NULL;
1892 }
1893
1894 if (fd == -1) {
1895 Py_BEGIN_ALLOW_THREADS
1896 self->kqfd = kqueue();
1897 Py_END_ALLOW_THREADS
1898 }
1899 else {
1900 self->kqfd = fd;
1901 }
1902 if (self->kqfd < 0) {
1903 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001904 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 return NULL;
1906 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001907
1908 if (fd == -1) {
1909 if (_Py_set_inheritable(self->kqfd, 0, NULL) < 0) {
1910 Py_DECREF(self);
1911 return NULL;
1912 }
1913 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001915}
1916
1917static PyObject *
1918kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 if ((args != NULL && PyObject_Size(args)) ||
1921 (kwds != NULL && PyObject_Size(kwds))) {
1922 PyErr_SetString(PyExc_ValueError,
1923 "select.kqueue doesn't accept arguments");
1924 return NULL;
1925 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 return newKqueue_Object(type, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001928}
1929
1930static void
1931kqueue_queue_dealloc(kqueue_queue_Object *self)
1932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 kqueue_queue_internal_close(self);
1934 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001935}
1936
1937static PyObject*
1938kqueue_queue_close(kqueue_queue_Object *self)
1939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 errno = kqueue_queue_internal_close(self);
1941 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001942 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 return NULL;
1944 }
1945 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001946}
1947
1948PyDoc_STRVAR(kqueue_queue_close_doc,
1949"close() -> None\n\
1950\n\
1951Close the kqueue control file descriptor. Further operations on the kqueue\n\
1952object will raise an exception.");
1953
1954static PyObject*
1955kqueue_queue_get_closed(kqueue_queue_Object *self)
1956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 if (self->kqfd < 0)
1958 Py_RETURN_TRUE;
1959 else
1960 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001961}
1962
1963static PyObject*
1964kqueue_queue_fileno(kqueue_queue_Object *self)
1965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 if (self->kqfd < 0)
1967 return kqueue_queue_err_closed();
1968 return PyLong_FromLong(self->kqfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001969}
1970
1971PyDoc_STRVAR(kqueue_queue_fileno_doc,
1972"fileno() -> int\n\
1973\n\
1974Return the kqueue control file descriptor.");
1975
1976static PyObject*
1977kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1982 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001985}
1986
1987PyDoc_STRVAR(kqueue_queue_fromfd_doc,
1988"fromfd(fd) -> kqueue\n\
1989\n\
1990Create a kqueue object from a given control fd.");
1991
1992static PyObject *
1993kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
1994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 int nevents = 0;
1996 int gotevents = 0;
1997 int nchanges = 0;
1998 int i = 0;
1999 PyObject *otimeout = NULL;
2000 PyObject *ch = NULL;
2001 PyObject *it = NULL, *ei = NULL;
2002 PyObject *result = NULL;
2003 struct kevent *evl = NULL;
2004 struct kevent *chl = NULL;
Victor Stinnerd327f9d2012-03-13 15:29:08 +01002005 struct timespec timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 struct timespec *ptimeoutspec;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (self->kqfd < 0)
2009 return kqueue_queue_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
2012 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 if (nevents < 0) {
2015 PyErr_Format(PyExc_ValueError,
2016 "Length of eventlist must be 0 or positive, got %d",
2017 nevents);
2018 return NULL;
2019 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 if (otimeout == Py_None || otimeout == NULL) {
2022 ptimeoutspec = NULL;
2023 }
2024 else if (PyNumber_Check(otimeout)) {
Victor Stinner5d272cc2012-03-13 13:35:55 +01002025 if (_PyTime_ObjectToTimespec(otimeout,
2026 &timeout.tv_sec, &timeout.tv_nsec) == -1)
2027 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002028
Victor Stinner5d272cc2012-03-13 13:35:55 +01002029 if (timeout.tv_sec < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 PyErr_SetString(PyExc_ValueError,
2031 "timeout must be positive or None");
2032 return NULL;
2033 }
Victor Stinnerd528b012012-03-13 16:25:35 +01002034 ptimeoutspec = &timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 }
2036 else {
2037 PyErr_Format(PyExc_TypeError,
2038 "timeout argument must be an number "
2039 "or None, got %.200s",
2040 Py_TYPE(otimeout)->tp_name);
2041 return NULL;
2042 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 if (ch != NULL && ch != Py_None) {
2045 it = PyObject_GetIter(ch);
2046 if (it == NULL) {
2047 PyErr_SetString(PyExc_TypeError,
2048 "changelist is not iterable");
2049 return NULL;
2050 }
2051 nchanges = PyObject_Size(ch);
2052 if (nchanges < 0) {
2053 goto error;
2054 }
Georg Brandlc0e22b72010-03-14 10:51:01 +00002055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 chl = PyMem_New(struct kevent, nchanges);
2057 if (chl == NULL) {
2058 PyErr_NoMemory();
2059 goto error;
2060 }
2061 i = 0;
2062 while ((ei = PyIter_Next(it)) != NULL) {
2063 if (!kqueue_event_Check(ei)) {
2064 Py_DECREF(ei);
2065 PyErr_SetString(PyExc_TypeError,
2066 "changelist must be an iterable of "
2067 "select.kevent objects");
2068 goto error;
2069 } else {
2070 chl[i++] = ((kqueue_event_Object *)ei)->e;
2071 }
2072 Py_DECREF(ei);
2073 }
2074 }
2075 Py_CLEAR(it);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 /* event list */
2078 if (nevents) {
2079 evl = PyMem_New(struct kevent, nevents);
2080 if (evl == NULL) {
2081 PyErr_NoMemory();
2082 goto error;
2083 }
2084 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 Py_BEGIN_ALLOW_THREADS
2087 gotevents = kevent(self->kqfd, chl, nchanges,
2088 evl, nevents, ptimeoutspec);
2089 Py_END_ALLOW_THREADS
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 if (gotevents == -1) {
2092 PyErr_SetFromErrno(PyExc_OSError);
2093 goto error;
2094 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 result = PyList_New(gotevents);
2097 if (result == NULL) {
2098 goto error;
2099 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 for (i = 0; i < gotevents; i++) {
2102 kqueue_event_Object *ch;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
2105 if (ch == NULL) {
2106 goto error;
2107 }
2108 ch->e = evl[i];
2109 PyList_SET_ITEM(result, i, (PyObject *)ch);
2110 }
2111 PyMem_Free(chl);
2112 PyMem_Free(evl);
2113 return result;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002114
2115 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002116 PyMem_Free(chl);
2117 PyMem_Free(evl);
2118 Py_XDECREF(result);
2119 Py_XDECREF(it);
2120 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002121}
2122
2123PyDoc_STRVAR(kqueue_queue_control_doc,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00002124"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002125\n\
2126Calls the kernel kevent function.\n\
2127- changelist must be a list of kevent objects describing the changes\n\
2128 to be made to the kernel's watch list or None.\n\
2129- max_events lets you specify the maximum number of events that the\n\
2130 kernel will return.\n\
2131- timeout is the maximum time to wait in seconds, or else None,\n\
2132 to wait forever. timeout accepts floats for smaller timeouts, too.");
2133
2134
2135static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
2137 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
2138 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
2139 kqueue_queue_close_doc},
2140 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
2141 kqueue_queue_fileno_doc},
2142 {"control", (PyCFunction)kqueue_queue_control,
2143 METH_VARARGS , kqueue_queue_control_doc},
2144 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002145};
2146
2147static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 {"closed", (getter)kqueue_queue_get_closed, NULL,
2149 "True if the kqueue handler is closed"},
2150 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002151};
2152
2153PyDoc_STRVAR(kqueue_queue_doc,
2154"Kqueue syscall wrapper.\n\
2155\n\
2156For example, to start watching a socket for input:\n\
2157>>> kq = kqueue()\n\
2158>>> sock = socket()\n\
2159>>> sock.connect((host, port))\n\
2160>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
2161\n\
2162To wait one second for it to become writeable:\n\
2163>>> kq.control(None, 1, 1000)\n\
2164\n\
2165To stop listening:\n\
2166>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
2167
2168static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 PyVarObject_HEAD_INIT(NULL, 0)
2170 "select.kqueue", /* tp_name */
2171 sizeof(kqueue_queue_Object), /* tp_basicsize */
2172 0, /* tp_itemsize */
2173 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
2174 0, /* tp_print */
2175 0, /* tp_getattr */
2176 0, /* tp_setattr */
2177 0, /* tp_reserved */
2178 0, /* tp_repr */
2179 0, /* tp_as_number */
2180 0, /* tp_as_sequence */
2181 0, /* tp_as_mapping */
2182 0, /* tp_hash */
2183 0, /* tp_call */
2184 0, /* tp_str */
2185 0, /* tp_getattro */
2186 0, /* tp_setattro */
2187 0, /* tp_as_buffer */
2188 Py_TPFLAGS_DEFAULT, /* tp_flags */
2189 kqueue_queue_doc, /* tp_doc */
2190 0, /* tp_traverse */
2191 0, /* tp_clear */
2192 0, /* tp_richcompare */
2193 0, /* tp_weaklistoffset */
2194 0, /* tp_iter */
2195 0, /* tp_iternext */
2196 kqueue_queue_methods, /* tp_methods */
2197 0, /* tp_members */
2198 kqueue_queue_getsetlist, /* tp_getset */
2199 0, /* tp_base */
2200 0, /* tp_dict */
2201 0, /* tp_descr_get */
2202 0, /* tp_descr_set */
2203 0, /* tp_dictoffset */
2204 0, /* tp_init */
2205 0, /* tp_alloc */
2206 kqueue_queue_new, /* tp_new */
2207 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002208};
2209
2210#endif /* HAVE_KQUEUE */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002211
2212
2213
2214
2215
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002216/* ************************************************************************ */
2217
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002218PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002219"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
2220\n\
2221Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00002222The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002223rlist -- wait until ready for reading\n\
2224wlist -- wait until ready for writing\n\
2225xlist -- wait for an ``exceptional condition''\n\
2226If only one kind of condition is required, pass [] for the other lists.\n\
2227A file descriptor is either a socket or file object, or a small integer\n\
2228gotten from a fileno() method call on one of those.\n\
2229\n\
2230The optional 4th argument specifies a timeout in seconds; it may be\n\
2231a floating point number to specify fractions of seconds. If it is absent\n\
2232or None, the call will never time out.\n\
2233\n\
2234The return value is a tuple of three lists corresponding to the first three\n\
2235arguments; each contains the subset of the corresponding file descriptors\n\
2236that are ready.\n\
2237\n\
2238*** IMPORTANT NOTICE ***\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002239On Windows and OpenVMS, only sockets are supported; on Unix, all file\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00002240descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002241
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002242static PyMethodDef select_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natali986a56c2013-01-19 12:19:10 +01002244#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002246#endif /* HAVE_POLL */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002247#ifdef HAVE_SYS_DEVPOLL_H
2248 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2249#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00002251};
2252
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002253PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002254"This module supports asynchronous I/O on multiple file descriptors.\n\
2255\n\
2256*** IMPORTANT NOTICE ***\n\
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002257On Windows and OpenVMS, only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00002258
Martin v. Löwis1a214512008-06-11 05:26:20 +00002259
2260static struct PyModuleDef selectmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 PyModuleDef_HEAD_INIT,
2262 "select",
2263 module_doc,
2264 -1,
2265 select_methods,
2266 NULL,
2267 NULL,
2268 NULL,
2269 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002270};
2271
Jesus Cead8b9ae62011-11-14 19:07:41 +01002272
2273
2274
Mark Hammond62b1ab12002-07-23 06:31:15 +00002275PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002276PyInit_select(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00002277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 PyObject *m;
2279 m = PyModule_Create(&selectmodule);
2280 if (m == NULL)
2281 return NULL;
Fred Drake4baedc12002-04-01 14:53:37 +00002282
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002283 Py_INCREF(PyExc_OSError);
2284 PyModule_AddObject(m, "error", PyExc_OSError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002285
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002286#ifdef PIPE_BUF
R. David Murraye16cda92010-10-15 23:12:57 +00002287#ifdef HAVE_BROKEN_PIPE_BUF
2288#undef PIPE_BUF
2289#define PIPE_BUF 512
2290#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002291 PyModule_AddIntMacro(m, PIPE_BUF);
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002292#endif
Gregory P. Smithb970b862009-07-04 02:28:47 +00002293
Charles-François Natali986a56c2013-01-19 12:19:10 +01002294#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002295#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 if (select_have_broken_poll()) {
2297 if (PyObject_DelAttrString(m, "poll") == -1) {
2298 PyErr_Clear();
2299 }
2300 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002301#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002303#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 if (PyType_Ready(&poll_Type) < 0)
2305 return NULL;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002306 PyModule_AddIntMacro(m, POLLIN);
2307 PyModule_AddIntMacro(m, POLLPRI);
2308 PyModule_AddIntMacro(m, POLLOUT);
2309 PyModule_AddIntMacro(m, POLLERR);
2310 PyModule_AddIntMacro(m, POLLHUP);
2311 PyModule_AddIntMacro(m, POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00002312
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002313#ifdef POLLRDNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002314 PyModule_AddIntMacro(m, POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002315#endif
2316#ifdef POLLRDBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002317 PyModule_AddIntMacro(m, POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002318#endif
2319#ifdef POLLWRNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002320 PyModule_AddIntMacro(m, POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002321#endif
2322#ifdef POLLWRBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002323 PyModule_AddIntMacro(m, POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002324#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002325#ifdef POLLMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002326 PyModule_AddIntMacro(m, POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002327#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002329#endif /* HAVE_POLL */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002330
Jesus Cead8b9ae62011-11-14 19:07:41 +01002331#ifdef HAVE_SYS_DEVPOLL_H
2332 if (PyType_Ready(&devpoll_Type) < 0)
2333 return NULL;
2334#endif
2335
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002336#ifdef HAVE_EPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
2338 if (PyType_Ready(&pyEpoll_Type) < 0)
2339 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 Py_INCREF(&pyEpoll_Type);
2342 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002343
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002344 PyModule_AddIntMacro(m, EPOLLIN);
2345 PyModule_AddIntMacro(m, EPOLLOUT);
2346 PyModule_AddIntMacro(m, EPOLLPRI);
2347 PyModule_AddIntMacro(m, EPOLLERR);
2348 PyModule_AddIntMacro(m, EPOLLHUP);
2349 PyModule_AddIntMacro(m, EPOLLET);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002350#ifdef EPOLLONESHOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 /* Kernel 2.6.2+ */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002352 PyModule_AddIntMacro(m, EPOLLONESHOT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002353#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002355 PyModule_AddIntMacro(m, EPOLLRDNORM);
2356 PyModule_AddIntMacro(m, EPOLLRDBAND);
2357 PyModule_AddIntMacro(m, EPOLLWRNORM);
2358 PyModule_AddIntMacro(m, EPOLLWRBAND);
2359 PyModule_AddIntMacro(m, EPOLLMSG);
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002360
Benjamin Peterson95c16622011-12-27 15:36:32 -06002361#ifdef EPOLL_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002362 PyModule_AddIntMacro(m, EPOLL_CLOEXEC);
Benjamin Peterson95c16622011-12-27 15:36:32 -06002363#endif
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002364#endif /* HAVE_EPOLL */
2365
2366#ifdef HAVE_KQUEUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 kqueue_event_Type.tp_new = PyType_GenericNew;
2368 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
2369 if(PyType_Ready(&kqueue_event_Type) < 0)
2370 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 Py_INCREF(&kqueue_event_Type);
2373 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
2376 if(PyType_Ready(&kqueue_queue_Type) < 0)
2377 return NULL;
2378 Py_INCREF(&kqueue_queue_Type);
2379 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
2380
2381 /* event filters */
2382 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
2383 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
2384 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
2385 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
2386 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002387#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002389#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
2391 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 /* event flags */
2394 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
2395 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
2396 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
2397 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
2398 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
2399 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
2402 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
2405 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 /* READ WRITE filter flag */
2408 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 /* VNODE filter flags */
2411 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
2412 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
2413 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
2414 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
2415 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
2416 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
2417 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* PROC filter flags */
2420 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
2421 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
2422 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
2423 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
2424 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002426 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
2427 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
2428 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
2429
2430 /* NETDEV filter flags */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002431#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
2433 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
2434 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002435#endif
2436
2437#endif /* HAVE_KQUEUE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 return m;
Guido van Rossumed233a51992-06-23 09:07:03 +00002439}