blob: a65af2ff347439513abf4bc83bef831c52d63a86 [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
Guido van Rossumbcc20741998-08-04 22:53:56 +000055#endif
Guido van Rossumed233a51992-06-23 09:07:03 +000056
Barry Warsawc1cb3601996-12-12 22:16:21 +000057/* list of Python objects and their file descriptor */
58typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 PyObject *obj; /* owned reference */
60 SOCKET fd;
61 int sentinel; /* -1 == sentinel */
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000062} pylist;
63
Barry Warsawc1cb3601996-12-12 22:16:21 +000064static void
Tim Peters4b046c22001-08-16 21:59:46 +000065reap_obj(pylist fd2obj[FD_SETSIZE + 1])
Barry Warsawc1cb3601996-12-12 22:16:21 +000066{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 int i;
68 for (i = 0; i < FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +020069 Py_CLEAR(fd2obj[i].obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 }
71 fd2obj[0].sentinel = -1;
Barry Warsawc1cb3601996-12-12 22:16:21 +000072}
73
74
Barry Warsawe4ac0aa1996-12-12 00:04:35 +000075/* returns -1 and sets the Python exception if an error occurred, otherwise
76 returns a number >= 0
77*/
Guido van Rossum4f0fbf81996-06-12 04:22:53 +000078static int
Brett Cannon62dba4c2003-09-10 19:37:42 +000079seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +000080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 int max = -1;
82 int index = 0;
Antoine Pitroue4ad37e2012-11-01 20:13:54 +010083 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 PyObject* fast_seq = NULL;
85 PyObject* o = NULL;
Guido van Rossum07432c01995-03-29 16:47:45 +000086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 fd2obj[0].obj = (PyObject*)0; /* set list to zero size */
88 FD_ZERO(set);
Barry Warsawc1cb3601996-12-12 22:16:21 +000089
Benjamin Petersone0edb8b2010-06-27 23:49:45 +000090 fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 if (!fast_seq)
92 return -1;
93
Antoine Pitroue4ad37e2012-11-01 20:13:54 +010094 for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 SOCKET v;
96
97 /* any intervening fileno() calls could decr this refcnt */
98 if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i)))
Jesus Cea62a5c322012-07-19 21:31:26 +020099 goto finally;
Brett Cannon62dba4c2003-09-10 19:37:42 +0000100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 Py_INCREF(o);
102 v = PyObject_AsFileDescriptor( o );
103 if (v == -1) goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000104
Guido van Rossum947a0fa2000-01-14 16:33:09 +0000105#if defined(_MSC_VER)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 max = 0; /* not used for Win32 */
Barry Warsawc1cb3601996-12-12 22:16:21 +0000107#else /* !_MSC_VER */
Charles-François Nataliaa26b272011-08-28 17:51:43 +0200108 if (!_PyIsSelectable_fd(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 PyErr_SetString(PyExc_ValueError,
110 "filedescriptor out of range in select()");
111 goto finally;
112 }
113 if (v > max)
114 max = v;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000115#endif /* _MSC_VER */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 FD_SET(v, set);
Barry Warsawc1cb3601996-12-12 22:16:21 +0000117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /* add object and its file descriptor to the list */
119 if (index >= FD_SETSIZE) {
120 PyErr_SetString(PyExc_ValueError,
121 "too many file descriptors in select()");
122 goto finally;
123 }
124 fd2obj[index].obj = o;
125 fd2obj[index].fd = v;
126 fd2obj[index].sentinel = 0;
127 fd2obj[++index].sentinel = -1;
128 }
129 Py_DECREF(fast_seq);
130 return max+1;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000131
132 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 Py_XDECREF(o);
134 Py_DECREF(fast_seq);
135 return -1;
Guido van Rossumed233a51992-06-23 09:07:03 +0000136}
137
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000138/* returns NULL and sets the Python exception if an error occurred */
139static PyObject *
Tim Peters4b046c22001-08-16 21:59:46 +0000140set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1])
Guido van Rossumed233a51992-06-23 09:07:03 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 int i, j, count=0;
143 PyObject *list, *o;
144 SOCKET fd;
Guido van Rossumed233a51992-06-23 09:07:03 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
147 if (FD_ISSET(fd2obj[j].fd, set))
148 count++;
149 }
150 list = PyList_New(count);
151 if (!list)
152 return NULL;
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 i = 0;
155 for (j = 0; fd2obj[j].sentinel >= 0; j++) {
156 fd = fd2obj[j].fd;
157 if (FD_ISSET(fd, set)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 o = fd2obj[j].obj;
159 fd2obj[j].obj = NULL;
160 /* transfer ownership */
161 if (PyList_SetItem(list, i, o) < 0)
162 goto finally;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 i++;
165 }
166 }
167 return list;
Barry Warsawc1cb3601996-12-12 22:16:21 +0000168 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 Py_DECREF(list);
170 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000171}
Barry Warsawc1cb3601996-12-12 22:16:21 +0000172
Barry Warsawb44740f2001-08-16 16:52:59 +0000173#undef SELECT_USES_HEAP
174#if FD_SETSIZE > 1024
175#define SELECT_USES_HEAP
176#endif /* FD_SETSIZE > 1024 */
177
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000178static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000179select_select(PyObject *self, PyObject *args)
Guido van Rossumed233a51992-06-23 09:07:03 +0000180{
Barry Warsawb44740f2001-08-16 16:52:59 +0000181#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 pylist *rfd2obj, *wfd2obj, *efd2obj;
Barry Warsawb44740f2001-08-16 16:52:59 +0000183#else /* !SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 /* XXX: All this should probably be implemented as follows:
185 * - find the highest descriptor we're interested in
186 * - add one
187 * - that's the size
188 * See: Stevens, APitUE, $12.5.1
189 */
190 pylist rfd2obj[FD_SETSIZE + 1];
191 pylist wfd2obj[FD_SETSIZE + 1];
192 pylist efd2obj[FD_SETSIZE + 1];
Barry Warsawb44740f2001-08-16 16:52:59 +0000193#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 PyObject *ifdlist, *ofdlist, *efdlist;
195 PyObject *ret = NULL;
196 PyObject *tout = Py_None;
197 fd_set ifdset, ofdset, efdset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 struct timeval tv, *tvp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 int imax, omax, emax, max;
200 int n;
Guido van Rossumed233a51992-06-23 09:07:03 +0000201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 /* convert arguments */
203 if (!PyArg_UnpackTuple(args, "select", 3, 4,
204 &ifdlist, &ofdlist, &efdlist, &tout))
205 return NULL;
Guido van Rossumed233a51992-06-23 09:07:03 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 if (tout == Py_None)
208 tvp = (struct timeval *)0;
209 else if (!PyNumber_Check(tout)) {
210 PyErr_SetString(PyExc_TypeError,
211 "timeout must be a float or None");
212 return NULL;
213 }
214 else {
Victor Stinner5a8e5792014-02-18 01:35:40 +0100215 /* On OpenBSD 5.4, timeval.tv_sec is a long.
216 * Example: long is 64-bit, whereas time_t is 32-bit. */
Victor Stinnerb2a37732012-03-14 00:20:51 +0100217 time_t sec;
Victor Stinner5a8e5792014-02-18 01:35:40 +0100218 /* On OS X 64-bit, timeval.tv_usec is an int (and thus still 4
219 bytes as required), but no longer defined by a long. */
220 long usec;
221 if (_PyTime_ObjectToTimeval(tout, &sec, &usec,
Victor Stinner3c1b3792014-02-17 00:02:43 +0100222 _PyTime_ROUND_UP) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 return NULL;
Victor Stinner5a8e5792014-02-18 01:35:40 +0100224#ifdef MS_WINDOWS
225 /* On Windows, timeval.tv_sec is a long (32 bit),
226 * whereas time_t can be 64-bit. */
Victor Stinnerb2a37732012-03-14 00:20:51 +0100227 assert(sizeof(tv.tv_sec) == sizeof(long));
228#if SIZEOF_TIME_T > SIZEOF_LONG
229 if (sec > LONG_MAX) {
230 PyErr_SetString(PyExc_OverflowError,
231 "timeout is too large");
232 return NULL;
233 }
234#endif
Victor Stinner329e4922014-02-18 09:30:33 +0100235 tv.tv_sec = (long)sec;
Victor Stinnerb2a37732012-03-14 00:20:51 +0100236#else
Victor Stinner5a8e5792014-02-18 01:35:40 +0100237 assert(sizeof(tv.tv_sec) >= sizeof(sec));
Victor Stinner5a8e5792014-02-18 01:35:40 +0100238 tv.tv_sec = sec;
Victor Stinner329e4922014-02-18 09:30:33 +0100239#endif
Victor Stinner5a8e5792014-02-18 01:35:40 +0100240 tv.tv_usec = usec;
Victor Stinner5d272cc2012-03-13 13:35:55 +0100241 if (tv.tv_sec < 0) {
242 PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 return NULL;
244 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 tvp = &tv;
246 }
Guido van Rossumed233a51992-06-23 09:07:03 +0000247
Guido van Rossumed233a51992-06-23 09:07:03 +0000248
Barry Warsawb44740f2001-08-16 16:52:59 +0000249#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 /* Allocate memory for the lists */
251 rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
252 wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
253 efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1);
254 if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
255 if (rfd2obj) PyMem_DEL(rfd2obj);
256 if (wfd2obj) PyMem_DEL(wfd2obj);
257 if (efd2obj) PyMem_DEL(efd2obj);
258 return PyErr_NoMemory();
259 }
Barry Warsawb44740f2001-08-16 16:52:59 +0000260#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* Convert sequences to fd_sets, and get maximum fd number
262 * propagates the Python exception set in seq2set()
263 */
264 rfd2obj[0].sentinel = -1;
265 wfd2obj[0].sentinel = -1;
266 efd2obj[0].sentinel = -1;
267 if ((imax=seq2set(ifdlist, &ifdset, rfd2obj)) < 0)
268 goto finally;
269 if ((omax=seq2set(ofdlist, &ofdset, wfd2obj)) < 0)
270 goto finally;
271 if ((emax=seq2set(efdlist, &efdset, efd2obj)) < 0)
272 goto finally;
273 max = imax;
274 if (omax > max) max = omax;
275 if (emax > max) max = emax;
Guido van Rossumed233a51992-06-23 09:07:03 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_BEGIN_ALLOW_THREADS
278 n = select(max, &ifdset, &ofdset, &efdset, tvp);
279 Py_END_ALLOW_THREADS
Guido van Rossumed233a51992-06-23 09:07:03 +0000280
Thomas Heller106f4c72002-09-24 16:51:00 +0000281#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 if (n == SOCKET_ERROR) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200283 PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000285#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 if (n < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200287 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
Thomas Heller106f4c72002-09-24 16:51:00 +0000289#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 else {
291 /* any of these three calls can raise an exception. it's more
292 convenient to test for this after all three calls... but
293 is that acceptable?
294 */
295 ifdlist = set2list(&ifdset, rfd2obj);
296 ofdlist = set2list(&ofdset, wfd2obj);
297 efdlist = set2list(&efdset, efd2obj);
298 if (PyErr_Occurred())
299 ret = NULL;
300 else
301 ret = PyTuple_Pack(3, ifdlist, ofdlist, efdlist);
Barry Warsawe4ac0aa1996-12-12 00:04:35 +0000302
Victor Stinnerbbf8ce52013-07-09 00:49:03 +0200303 Py_XDECREF(ifdlist);
304 Py_XDECREF(ofdlist);
305 Py_XDECREF(efdlist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 }
307
Barry Warsawc1cb3601996-12-12 22:16:21 +0000308 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 reap_obj(rfd2obj);
310 reap_obj(wfd2obj);
311 reap_obj(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000312#ifdef SELECT_USES_HEAP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 PyMem_DEL(rfd2obj);
314 PyMem_DEL(wfd2obj);
315 PyMem_DEL(efd2obj);
Barry Warsawb44740f2001-08-16 16:52:59 +0000316#endif /* SELECT_USES_HEAP */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 return ret;
Guido van Rossumed233a51992-06-23 09:07:03 +0000318}
319
Nicholas Bastine62c5c82004-03-21 23:45:42 +0000320#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321/*
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000322 * poll() support
323 */
324
325typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 PyObject_HEAD
327 PyObject *dict;
328 int ufd_uptodate;
329 int ufd_len;
330 struct pollfd *ufds;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300331 int poll_running;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000332} pollObject;
333
Jeremy Hylton938ace62002-07-17 16:30:39 +0000334static PyTypeObject poll_Type;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336/* Update the malloc'ed array of pollfds to match the dictionary
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000337 contained within a pollObject. Return 1 on success, 0 on an error.
338*/
339
340static int
341update_ufd_array(pollObject *self)
342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_ssize_t i, pos;
344 PyObject *key, *value;
345 struct pollfd *old_ufds = self->ufds;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 self->ufd_len = PyDict_Size(self->dict);
348 PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len);
349 if (self->ufds == NULL) {
350 self->ufds = old_ufds;
351 PyErr_NoMemory();
352 return 0;
353 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 i = pos = 0;
356 while (PyDict_Next(self->dict, &pos, &key, &value)) {
Serhiy Storchaka78980432013-01-15 01:12:17 +0200357 assert(i < self->ufd_len);
358 /* Never overflow */
359 self->ufds[i].fd = (int)PyLong_AsLong(key);
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200360 self->ufds[i].events = (short)(unsigned short)PyLong_AsLong(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 i++;
362 }
Serhiy Storchaka78980432013-01-15 01:12:17 +0200363 assert(i == self->ufd_len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 self->ufd_uptodate = 1;
365 return 1;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000366}
367
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200368static int
369ushort_converter(PyObject *obj, void *ptr)
370{
371 unsigned long uval;
372
373 uval = PyLong_AsUnsignedLong(obj);
374 if (uval == (unsigned long)-1 && PyErr_Occurred())
375 return 0;
376 if (uval > USHRT_MAX) {
377 PyErr_SetString(PyExc_OverflowError,
378 "Python int too large for C unsigned short");
379 return 0;
380 }
381
382 *(unsigned short *)ptr = Py_SAFE_DOWNCAST(uval, unsigned long, unsigned short);
383 return 1;
384}
385
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000386PyDoc_STRVAR(poll_register_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000387"register(fd [, eventmask] ) -> None\n\n\
388Register a file descriptor with the polling object.\n\
Barry Warsaw2f704552001-08-16 16:55:10 +0000389fd -- either an integer, or an object with a fileno() method returning an\n\
390 int.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000391events -- an optional bitmask describing the type of events to check for");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000392
393static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394poll_register(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 PyObject *o, *key, *value;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200397 int fd;
398 unsigned short events = POLLIN | POLLPRI | POLLOUT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 int err;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000400
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200401 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 fd = PyObject_AsFileDescriptor(o);
405 if (fd == -1) return NULL;
Guido van Rossuma0dfc852001-10-25 20:18:35 +0000406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Add entry to the internal dictionary: the key is the
408 file descriptor, and the value is the event mask. */
409 key = PyLong_FromLong(fd);
410 if (key == NULL)
411 return NULL;
412 value = PyLong_FromLong(events);
413 if (value == NULL) {
414 Py_DECREF(key);
415 return NULL;
416 }
417 err = PyDict_SetItem(self->dict, key, value);
418 Py_DECREF(key);
419 Py_DECREF(value);
420 if (err < 0)
421 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 self->ufd_uptodate = 0;
424
425 Py_INCREF(Py_None);
426 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000427}
428
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000429PyDoc_STRVAR(poll_modify_doc,
430"modify(fd, eventmask) -> None\n\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +0000431Modify an already registered file descriptor.\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000432fd -- either an integer, or an object with a fileno() method returning an\n\
433 int.\n\
434events -- an optional bitmask describing the type of events to check for");
435
436static PyObject *
437poll_modify(pollObject *self, PyObject *args)
438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 PyObject *o, *key, *value;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200440 int fd;
441 unsigned short events;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 int err;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000443
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200444 if (!PyArg_ParseTuple(args, "OO&:modify", &o, ushort_converter, &events))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 fd = PyObject_AsFileDescriptor(o);
448 if (fd == -1) return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 /* Modify registered fd */
451 key = PyLong_FromLong(fd);
452 if (key == NULL)
453 return NULL;
454 if (PyDict_GetItem(self->dict, key) == NULL) {
455 errno = ENOENT;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200456 PyErr_SetFromErrno(PyExc_OSError);
Jesus Cea62a5c322012-07-19 21:31:26 +0200457 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 return NULL;
459 }
460 value = PyLong_FromLong(events);
461 if (value == NULL) {
462 Py_DECREF(key);
463 return NULL;
464 }
465 err = PyDict_SetItem(self->dict, key, value);
466 Py_DECREF(key);
467 Py_DECREF(value);
468 if (err < 0)
469 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 self->ufd_uptodate = 0;
472
473 Py_INCREF(Py_None);
474 return Py_None;
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000475}
476
477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(poll_unregister_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000479"unregister(fd) -> None\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000480Remove a file descriptor being tracked by the polling object.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000481
482static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483poll_unregister(pollObject *self, PyObject *o)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 PyObject *key;
486 int fd;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 fd = PyObject_AsFileDescriptor( o );
489 if (fd == -1)
490 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 /* Check whether the fd is already in the array */
493 key = PyLong_FromLong(fd);
494 if (key == NULL)
495 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (PyDict_DelItem(self->dict, key) == -1) {
498 Py_DECREF(key);
499 /* This will simply raise the KeyError set by PyDict_DelItem
500 if the file descriptor isn't registered. */
501 return NULL;
502 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 Py_DECREF(key);
505 self->ufd_uptodate = 0;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_INCREF(Py_None);
508 return Py_None;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000509}
510
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000511PyDoc_STRVAR(poll_poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000512"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
513Polls the set of registered file descriptors, returning a list containing \n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000514any descriptors that have events or errors to report.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000515
516static PyObject *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517poll_poll(pollObject *self, PyObject *args)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 PyObject *result_list = NULL, *tout = NULL;
520 int timeout = 0, poll_result, i, j;
521 PyObject *value = NULL, *num = NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
524 return NULL;
525 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 /* Check values for timeout */
528 if (tout == NULL || tout == Py_None)
529 timeout = -1;
530 else if (!PyNumber_Check(tout)) {
531 PyErr_SetString(PyExc_TypeError,
532 "timeout must be an integer or None");
533 return NULL;
534 }
535 else {
536 tout = PyNumber_Long(tout);
537 if (!tout)
538 return NULL;
Serhiy Storchaka78980432013-01-15 01:12:17 +0200539 timeout = _PyLong_AsInt(tout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 Py_DECREF(tout);
541 if (timeout == -1 && PyErr_Occurred())
542 return NULL;
543 }
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000544
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300545 /* Avoid concurrent poll() invocation, issue 8865 */
546 if (self->poll_running) {
547 PyErr_SetString(PyExc_RuntimeError,
548 "concurrent poll() invocation");
549 return NULL;
550 }
551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 /* Ensure the ufd array is up to date */
553 if (!self->ufd_uptodate)
554 if (update_ufd_array(self) == 0)
555 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000556
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300557 self->poll_running = 1;
558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* call poll() */
560 Py_BEGIN_ALLOW_THREADS
561 poll_result = poll(self->ufds, self->ufd_len, timeout);
562 Py_END_ALLOW_THREADS
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000563
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300564 self->poll_running = 0;
565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 if (poll_result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200567 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 return NULL;
569 }
570
571 /* build the result list */
572
573 result_list = PyList_New(poll_result);
574 if (!result_list)
575 return NULL;
576 else {
577 for (i = 0, j = 0; j < poll_result; j++) {
578 /* skip to the next fired descriptor */
579 while (!self->ufds[i].revents) {
580 i++;
581 }
582 /* if we hit a NULL return, set value to NULL
583 and break out of loop; code at end will
584 clean up result_list */
585 value = PyTuple_New(2);
586 if (value == NULL)
587 goto error;
588 num = PyLong_FromLong(self->ufds[i].fd);
589 if (num == NULL) {
590 Py_DECREF(value);
591 goto error;
592 }
593 PyTuple_SET_ITEM(value, 0, num);
594
595 /* The &0xffff is a workaround for AIX. 'revents'
596 is a 16-bit short, and IBM assigned POLLNVAL
597 to be 0x8000, so the conversion to int results
598 in a negative number. See SF bug #923315. */
599 num = PyLong_FromLong(self->ufds[i].revents & 0xffff);
600 if (num == NULL) {
601 Py_DECREF(value);
602 goto error;
603 }
604 PyTuple_SET_ITEM(value, 1, num);
605 if ((PyList_SetItem(result_list, j, value)) == -1) {
606 Py_DECREF(value);
607 goto error;
608 }
609 i++;
610 }
611 }
612 return result_list;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000613
614 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 Py_DECREF(result_list);
616 return NULL;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000617}
618
619static PyMethodDef poll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 {"register", (PyCFunction)poll_register,
621 METH_VARARGS, poll_register_doc},
622 {"modify", (PyCFunction)poll_modify,
623 METH_VARARGS, poll_modify_doc},
624 {"unregister", (PyCFunction)poll_unregister,
625 METH_O, poll_unregister_doc},
626 {"poll", (PyCFunction)poll_poll,
627 METH_VARARGS, poll_poll_doc},
628 {NULL, NULL} /* sentinel */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000629};
630
631static pollObject *
Fred Drake8ce159a2000-08-31 05:18:54 +0000632newPollObject(void)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 pollObject *self;
635 self = PyObject_New(pollObject, &poll_Type);
636 if (self == NULL)
637 return NULL;
638 /* ufd_uptodate is a Boolean, denoting whether the
639 array pointed to by ufds matches the contents of the dictionary. */
640 self->ufd_uptodate = 0;
641 self->ufds = NULL;
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300642 self->poll_running = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 self->dict = PyDict_New();
644 if (self->dict == NULL) {
645 Py_DECREF(self);
646 return NULL;
647 }
648 return self;
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000649}
650
651static void
652poll_dealloc(pollObject *self)
653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 if (self->ufds != NULL)
655 PyMem_DEL(self->ufds);
656 Py_XDECREF(self->dict);
657 PyObject_Del(self);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000658}
659
Tim Peters0c322792002-07-17 16:49:03 +0000660static PyTypeObject poll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 /* The ob_type field must be initialized in the module init function
662 * to be portable to Windows without using C++. */
663 PyVarObject_HEAD_INIT(NULL, 0)
664 "select.poll", /*tp_name*/
665 sizeof(pollObject), /*tp_basicsize*/
666 0, /*tp_itemsize*/
667 /* methods */
668 (destructor)poll_dealloc, /*tp_dealloc*/
669 0, /*tp_print*/
670 0, /*tp_getattr*/
671 0, /*tp_setattr*/
672 0, /*tp_reserved*/
673 0, /*tp_repr*/
674 0, /*tp_as_number*/
675 0, /*tp_as_sequence*/
676 0, /*tp_as_mapping*/
677 0, /*tp_hash*/
678 0, /*tp_call*/
679 0, /*tp_str*/
680 0, /*tp_getattro*/
681 0, /*tp_setattro*/
682 0, /*tp_as_buffer*/
683 Py_TPFLAGS_DEFAULT, /*tp_flags*/
684 0, /*tp_doc*/
685 0, /*tp_traverse*/
686 0, /*tp_clear*/
687 0, /*tp_richcompare*/
688 0, /*tp_weaklistoffset*/
689 0, /*tp_iter*/
690 0, /*tp_iternext*/
691 poll_methods, /*tp_methods*/
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +0000692};
693
Jesus Cead8b9ae62011-11-14 19:07:41 +0100694#ifdef HAVE_SYS_DEVPOLL_H
695typedef struct {
696 PyObject_HEAD
697 int fd_devpoll;
698 int max_n_fds;
699 int n_fds;
700 struct pollfd *fds;
701} devpollObject;
702
703static PyTypeObject devpoll_Type;
704
Victor Stinner13423c32013-08-22 00:19:50 +0200705static PyObject *
706devpoll_err_closed(void)
707{
708 PyErr_SetString(PyExc_ValueError, "I/O operation on closed devpoll object");
709 return NULL;
710}
711
Jesus Cead8b9ae62011-11-14 19:07:41 +0100712static int devpoll_flush(devpollObject *self)
713{
714 int size, n;
715
716 if (!self->n_fds) return 0;
717
718 size = sizeof(struct pollfd)*self->n_fds;
719 self->n_fds = 0;
720
Victor Stinner54799672015-03-19 23:33:09 +0100721 n = _Py_write(self->fd_devpoll, self->fds, size);
722 if (n == -1)
Jesus Cead8b9ae62011-11-14 19:07:41 +0100723 return -1;
Victor Stinner54799672015-03-19 23:33:09 +0100724
Jesus Cead8b9ae62011-11-14 19:07:41 +0100725 if (n < size) {
726 /*
727 ** Data writed to /dev/poll is a binary data structure. It is not
728 ** clear what to do if a partial write occurred. For now, raise
729 ** an exception and see if we actually found this problem in
730 ** the wild.
731 ** See http://bugs.python.org/issue6397.
732 */
733 PyErr_Format(PyExc_IOError, "failed to write all pollfds. "
734 "Please, report at http://bugs.python.org/. "
735 "Data to report: Size tried: %d, actual size written: %d.",
736 size, n);
737 return -1;
738 }
739 return 0;
740}
741
742static PyObject *
743internal_devpoll_register(devpollObject *self, PyObject *args, int remove)
744{
745 PyObject *o;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200746 int fd;
747 unsigned short events = POLLIN | POLLPRI | POLLOUT;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100748
Victor Stinner13423c32013-08-22 00:19:50 +0200749 if (self->fd_devpoll < 0)
750 return devpoll_err_closed();
751
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200752 if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events))
Jesus Cead8b9ae62011-11-14 19:07:41 +0100753 return NULL;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100754
755 fd = PyObject_AsFileDescriptor(o);
756 if (fd == -1) return NULL;
757
758 if (remove) {
759 self->fds[self->n_fds].fd = fd;
760 self->fds[self->n_fds].events = POLLREMOVE;
761
762 if (++self->n_fds == self->max_n_fds) {
763 if (devpoll_flush(self))
764 return NULL;
765 }
766 }
767
768 self->fds[self->n_fds].fd = fd;
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200769 self->fds[self->n_fds].events = (signed short)events;
Jesus Cead8b9ae62011-11-14 19:07:41 +0100770
771 if (++self->n_fds == self->max_n_fds) {
772 if (devpoll_flush(self))
773 return NULL;
774 }
775
776 Py_RETURN_NONE;
777}
778
779PyDoc_STRVAR(devpoll_register_doc,
780"register(fd [, eventmask] ) -> None\n\n\
781Register a file descriptor with the polling object.\n\
782fd -- either an integer, or an object with a fileno() method returning an\n\
783 int.\n\
784events -- an optional bitmask describing the type of events to check for");
785
786static PyObject *
787devpoll_register(devpollObject *self, PyObject *args)
788{
789 return internal_devpoll_register(self, args, 0);
790}
791
792PyDoc_STRVAR(devpoll_modify_doc,
793"modify(fd[, eventmask]) -> None\n\n\
794Modify a possible already registered file descriptor.\n\
795fd -- either an integer, or an object with a fileno() method returning an\n\
796 int.\n\
797events -- an optional bitmask describing the type of events to check for");
798
799static PyObject *
800devpoll_modify(devpollObject *self, PyObject *args)
801{
802 return internal_devpoll_register(self, args, 1);
803}
804
805
806PyDoc_STRVAR(devpoll_unregister_doc,
807"unregister(fd) -> None\n\n\
808Remove a file descriptor being tracked by the polling object.");
809
810static PyObject *
811devpoll_unregister(devpollObject *self, PyObject *o)
812{
813 int fd;
814
Victor Stinner13423c32013-08-22 00:19:50 +0200815 if (self->fd_devpoll < 0)
816 return devpoll_err_closed();
817
Jesus Cead8b9ae62011-11-14 19:07:41 +0100818 fd = PyObject_AsFileDescriptor( o );
819 if (fd == -1)
820 return NULL;
821
822 self->fds[self->n_fds].fd = fd;
823 self->fds[self->n_fds].events = POLLREMOVE;
824
825 if (++self->n_fds == self->max_n_fds) {
826 if (devpoll_flush(self))
827 return NULL;
828 }
829
830 Py_RETURN_NONE;
831}
832
833PyDoc_STRVAR(devpoll_poll_doc,
834"poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\
835Polls the set of registered file descriptors, returning a list containing \n\
836any descriptors that have events or errors to report.");
837
838static PyObject *
839devpoll_poll(devpollObject *self, PyObject *args)
840{
841 struct dvpoll dvp;
842 PyObject *result_list = NULL, *tout = NULL;
843 int poll_result, i;
844 long timeout;
845 PyObject *value, *num1, *num2;
846
Victor Stinner13423c32013-08-22 00:19:50 +0200847 if (self->fd_devpoll < 0)
848 return devpoll_err_closed();
849
Jesus Cead8b9ae62011-11-14 19:07:41 +0100850 if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) {
851 return NULL;
852 }
853
854 /* Check values for timeout */
855 if (tout == NULL || tout == Py_None)
856 timeout = -1;
857 else if (!PyNumber_Check(tout)) {
858 PyErr_SetString(PyExc_TypeError,
859 "timeout must be an integer or None");
860 return NULL;
861 }
862 else {
863 tout = PyNumber_Long(tout);
864 if (!tout)
865 return NULL;
866 timeout = PyLong_AsLong(tout);
867 Py_DECREF(tout);
868 if (timeout == -1 && PyErr_Occurred())
869 return NULL;
870 }
871
872 if ((timeout < -1) || (timeout > INT_MAX)) {
873 PyErr_SetString(PyExc_OverflowError,
874 "timeout is out of range");
875 return NULL;
876 }
877
878 if (devpoll_flush(self))
879 return NULL;
880
881 dvp.dp_fds = self->fds;
882 dvp.dp_nfds = self->max_n_fds;
883 dvp.dp_timeout = timeout;
884
885 /* call devpoll() */
886 Py_BEGIN_ALLOW_THREADS
887 poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp);
888 Py_END_ALLOW_THREADS
889
890 if (poll_result < 0) {
891 PyErr_SetFromErrno(PyExc_IOError);
892 return NULL;
893 }
894
895 /* build the result list */
896
897 result_list = PyList_New(poll_result);
898 if (!result_list)
899 return NULL;
900 else {
901 for (i = 0; i < poll_result; i++) {
902 num1 = PyLong_FromLong(self->fds[i].fd);
903 num2 = PyLong_FromLong(self->fds[i].revents);
904 if ((num1 == NULL) || (num2 == NULL)) {
905 Py_XDECREF(num1);
906 Py_XDECREF(num2);
907 goto error;
908 }
909 value = PyTuple_Pack(2, num1, num2);
910 Py_DECREF(num1);
911 Py_DECREF(num2);
912 if (value == NULL)
913 goto error;
914 if ((PyList_SetItem(result_list, i, value)) == -1) {
915 Py_DECREF(value);
916 goto error;
917 }
918 }
919 }
920
921 return result_list;
922
923 error:
924 Py_DECREF(result_list);
925 return NULL;
926}
927
Richard Oudkerk168d59b2013-08-22 13:31:15 +0100928static int
929devpoll_internal_close(devpollObject *self)
930{
931 int save_errno = 0;
932 if (self->fd_devpoll >= 0) {
933 int fd = self->fd_devpoll;
934 self->fd_devpoll = -1;
935 Py_BEGIN_ALLOW_THREADS
936 if (close(fd) < 0)
937 save_errno = errno;
938 Py_END_ALLOW_THREADS
939 }
940 return save_errno;
941}
942
Victor Stinner13423c32013-08-22 00:19:50 +0200943static PyObject*
944devpoll_close(devpollObject *self)
945{
946 errno = devpoll_internal_close(self);
947 if (errno < 0) {
948 PyErr_SetFromErrno(PyExc_OSError);
949 return NULL;
950 }
951 Py_RETURN_NONE;
952}
953
954PyDoc_STRVAR(devpoll_close_doc,
955"close() -> None\n\
956\n\
957Close the devpoll file descriptor. Further operations on the devpoll\n\
958object will raise an exception.");
959
960static PyObject*
961devpoll_get_closed(devpollObject *self)
962{
963 if (self->fd_devpoll < 0)
964 Py_RETURN_TRUE;
965 else
966 Py_RETURN_FALSE;
967}
968
969static PyObject*
970devpoll_fileno(devpollObject *self)
971{
972 if (self->fd_devpoll < 0)
973 return devpoll_err_closed();
974 return PyLong_FromLong(self->fd_devpoll);
975}
976
977PyDoc_STRVAR(devpoll_fileno_doc,
978"fileno() -> int\n\
979\n\
980Return the file descriptor.");
981
Jesus Cead8b9ae62011-11-14 19:07:41 +0100982static PyMethodDef devpoll_methods[] = {
983 {"register", (PyCFunction)devpoll_register,
984 METH_VARARGS, devpoll_register_doc},
985 {"modify", (PyCFunction)devpoll_modify,
986 METH_VARARGS, devpoll_modify_doc},
987 {"unregister", (PyCFunction)devpoll_unregister,
988 METH_O, devpoll_unregister_doc},
989 {"poll", (PyCFunction)devpoll_poll,
990 METH_VARARGS, devpoll_poll_doc},
Victor Stinner13423c32013-08-22 00:19:50 +0200991 {"close", (PyCFunction)devpoll_close, METH_NOARGS,
992 devpoll_close_doc},
993 {"fileno", (PyCFunction)devpoll_fileno, METH_NOARGS,
994 devpoll_fileno_doc},
Jesus Cead8b9ae62011-11-14 19:07:41 +0100995 {NULL, NULL} /* sentinel */
996};
997
Victor Stinner13423c32013-08-22 00:19:50 +0200998static PyGetSetDef devpoll_getsetlist[] = {
999 {"closed", (getter)devpoll_get_closed, NULL,
1000 "True if the devpoll object is closed"},
1001 {0},
1002};
1003
Jesus Cead8b9ae62011-11-14 19:07:41 +01001004static devpollObject *
1005newDevPollObject(void)
1006{
1007 devpollObject *self;
1008 int fd_devpoll, limit_result;
1009 struct pollfd *fds;
1010 struct rlimit limit;
1011
Jesus Cead8b9ae62011-11-14 19:07:41 +01001012 /*
1013 ** If we try to process more that getrlimit()
1014 ** fds, the kernel will give an error, so
1015 ** we set the limit here. It is a dynamic
1016 ** value, because we can change rlimit() anytime.
1017 */
1018 limit_result = getrlimit(RLIMIT_NOFILE, &limit);
Jesus Cead8b9ae62011-11-14 19:07:41 +01001019 if (limit_result == -1) {
1020 PyErr_SetFromErrno(PyExc_OSError);
1021 return NULL;
1022 }
Victor Stinnera555cfc2015-03-18 00:22:14 +01001023
1024 fd_devpoll = _Py_open("/dev/poll", O_RDWR);
1025 if (fd_devpoll == -1)
Jesus Cead8b9ae62011-11-14 19:07:41 +01001026 return NULL;
Jesus Cead8b9ae62011-11-14 19:07:41 +01001027
1028 fds = PyMem_NEW(struct pollfd, limit.rlim_cur);
1029 if (fds == NULL) {
1030 close(fd_devpoll);
1031 PyErr_NoMemory();
1032 return NULL;
1033 }
1034
1035 self = PyObject_New(devpollObject, &devpoll_Type);
1036 if (self == NULL) {
1037 close(fd_devpoll);
1038 PyMem_DEL(fds);
1039 return NULL;
1040 }
1041 self->fd_devpoll = fd_devpoll;
1042 self->max_n_fds = limit.rlim_cur;
1043 self->n_fds = 0;
1044 self->fds = fds;
1045
1046 return self;
1047}
1048
1049static void
1050devpoll_dealloc(devpollObject *self)
1051{
Richard Oudkerka93bf7b2013-08-22 14:03:44 +01001052 (void)devpoll_internal_close(self);
Jesus Cead8b9ae62011-11-14 19:07:41 +01001053 PyMem_DEL(self->fds);
Jesus Cead8b9ae62011-11-14 19:07:41 +01001054 PyObject_Del(self);
1055}
1056
1057static PyTypeObject devpoll_Type = {
1058 /* The ob_type field must be initialized in the module init function
1059 * to be portable to Windows without using C++. */
1060 PyVarObject_HEAD_INIT(NULL, 0)
1061 "select.devpoll", /*tp_name*/
1062 sizeof(devpollObject), /*tp_basicsize*/
1063 0, /*tp_itemsize*/
1064 /* methods */
1065 (destructor)devpoll_dealloc, /*tp_dealloc*/
1066 0, /*tp_print*/
1067 0, /*tp_getattr*/
1068 0, /*tp_setattr*/
1069 0, /*tp_reserved*/
1070 0, /*tp_repr*/
1071 0, /*tp_as_number*/
1072 0, /*tp_as_sequence*/
1073 0, /*tp_as_mapping*/
1074 0, /*tp_hash*/
1075 0, /*tp_call*/
1076 0, /*tp_str*/
1077 0, /*tp_getattro*/
1078 0, /*tp_setattro*/
1079 0, /*tp_as_buffer*/
1080 Py_TPFLAGS_DEFAULT, /*tp_flags*/
1081 0, /*tp_doc*/
1082 0, /*tp_traverse*/
1083 0, /*tp_clear*/
1084 0, /*tp_richcompare*/
1085 0, /*tp_weaklistoffset*/
1086 0, /*tp_iter*/
1087 0, /*tp_iternext*/
1088 devpoll_methods, /*tp_methods*/
Victor Stinner13423c32013-08-22 00:19:50 +02001089 0, /* tp_members */
1090 devpoll_getsetlist, /* tp_getset */
Jesus Cead8b9ae62011-11-14 19:07:41 +01001091};
1092#endif /* HAVE_SYS_DEVPOLL_H */
1093
1094
1095
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001096PyDoc_STRVAR(poll_doc,
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001097"Returns a polling object, which supports registering and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001098unregistering file descriptors, and then polling them for I/O events.");
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001099
1100static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001101select_poll(PyObject *self, PyObject *unused)
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 return (PyObject *)newPollObject();
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001104}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001105
Jesus Cead8b9ae62011-11-14 19:07:41 +01001106#ifdef HAVE_SYS_DEVPOLL_H
1107PyDoc_STRVAR(devpoll_doc,
1108"Returns a polling object, which supports registering and\n\
1109unregistering file descriptors, and then polling them for I/O events.");
1110
1111static PyObject *
1112select_devpoll(PyObject *self, PyObject *unused)
1113{
1114 return (PyObject *)newDevPollObject();
1115}
1116#endif
1117
1118
Thomas Wouters477c8d52006-05-27 19:21:47 +00001119#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120/*
Thomas Wouters477c8d52006-05-27 19:21:47 +00001121 * On some systems poll() sets errno on invalid file descriptors. We test
1122 * for this at runtime because this bug may be fixed or introduced between
1123 * OS releases.
1124 */
1125static int select_have_broken_poll(void)
1126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 int poll_test;
1128 int filedes[2];
Thomas Wouters477c8d52006-05-27 19:21:47 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 };
Thomas Wouters477c8d52006-05-27 19:21:47 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 /* Create a file descriptor to make invalid */
1133 if (pipe(filedes) < 0) {
1134 return 1;
1135 }
1136 poll_struct.fd = filedes[0];
1137 close(filedes[0]);
1138 close(filedes[1]);
1139 poll_test = poll(&poll_struct, 1, 0);
1140 if (poll_test < 0) {
1141 return 1;
1142 } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) {
1143 return 1;
1144 }
1145 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001146}
1147#endif /* __APPLE__ */
1148
1149#endif /* HAVE_POLL */
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00001150
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001151#ifdef HAVE_EPOLL
1152/* **************************************************************************
1153 * epoll interface for Linux 2.6
1154 *
1155 * Written by Christian Heimes
1156 * Inspired by Twisted's _epoll.pyx and select.poll()
1157 */
1158
1159#ifdef HAVE_SYS_EPOLL_H
1160#include <sys/epoll.h>
1161#endif
1162
1163typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 PyObject_HEAD
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001165 SOCKET epfd; /* epoll control file descriptor */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001166} pyEpoll_Object;
1167
1168static PyTypeObject pyEpoll_Type;
1169#define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type))
1170
1171static PyObject *
1172pyepoll_err_closed(void)
1173{
Victor Stinner13423c32013-08-22 00:19:50 +02001174 PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001176}
1177
1178static int
1179pyepoll_internal_close(pyEpoll_Object *self)
1180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 int save_errno = 0;
1182 if (self->epfd >= 0) {
1183 int epfd = self->epfd;
1184 self->epfd = -1;
1185 Py_BEGIN_ALLOW_THREADS
1186 if (close(epfd) < 0)
1187 save_errno = errno;
1188 Py_END_ALLOW_THREADS
1189 }
1190 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001191}
1192
1193static PyObject *
Benjamin Peterson95c16622011-12-27 15:36:32 -06001194newPyEpoll_Object(PyTypeObject *type, int sizehint, int flags, SOCKET fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 pyEpoll_Object *self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 assert(type != NULL && type->tp_alloc != NULL);
1199 self = (pyEpoll_Object *) type->tp_alloc(type, 0);
1200 if (self == NULL)
1201 return NULL;
1202
1203 if (fd == -1) {
1204 Py_BEGIN_ALLOW_THREADS
Benjamin Peterson95c16622011-12-27 15:36:32 -06001205#ifdef HAVE_EPOLL_CREATE1
Victor Stinnerdaf45552013-08-28 00:53:59 +02001206 flags |= EPOLL_CLOEXEC;
Benjamin Peterson83251c12011-12-27 16:01:21 -06001207 if (flags)
1208 self->epfd = epoll_create1(flags);
1209 else
Benjamin Peterson95c16622011-12-27 15:36:32 -06001210#endif
Benjamin Peterson83251c12011-12-27 16:01:21 -06001211 self->epfd = epoll_create(sizehint);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 Py_END_ALLOW_THREADS
1213 }
1214 else {
1215 self->epfd = fd;
1216 }
1217 if (self->epfd < 0) {
1218 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001219 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 return NULL;
1221 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001222
1223#ifndef HAVE_EPOLL_CREATE1
Victor Stinnerd72fe892013-08-28 12:22:39 +02001224 if (fd == -1 && _Py_set_inheritable(self->epfd, 0, NULL) < 0) {
Victor Stinnerdaf45552013-08-28 00:53:59 +02001225 Py_DECREF(self);
1226 return NULL;
1227 }
1228#endif
1229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001231}
1232
1233
1234static PyObject *
1235pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1236{
Benjamin Peterson83251c12011-12-27 16:01:21 -06001237 int flags = 0, sizehint = FD_SETSIZE - 1;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001238 static char *kwlist[] = {"sizehint", "flags", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001239
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001240 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
1241 &sizehint, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 return NULL;
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001243 if (sizehint < 0) {
1244 PyErr_SetString(PyExc_ValueError, "negative sizehint");
1245 return NULL;
1246 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001247
Benjamin Peterson95c16622011-12-27 15:36:32 -06001248 return newPyEpoll_Object(type, sizehint, flags, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001249}
1250
1251
1252static void
1253pyepoll_dealloc(pyEpoll_Object *self)
1254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 (void)pyepoll_internal_close(self);
1256 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001257}
1258
1259static PyObject*
1260pyepoll_close(pyEpoll_Object *self)
1261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 errno = pyepoll_internal_close(self);
1263 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001264 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 return NULL;
1266 }
1267 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001268}
1269
1270PyDoc_STRVAR(pyepoll_close_doc,
1271"close() -> None\n\
1272\n\
1273Close the epoll control file descriptor. Further operations on the epoll\n\
1274object will raise an exception.");
1275
1276static PyObject*
1277pyepoll_get_closed(pyEpoll_Object *self)
1278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (self->epfd < 0)
1280 Py_RETURN_TRUE;
1281 else
1282 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001283}
1284
1285static PyObject*
1286pyepoll_fileno(pyEpoll_Object *self)
1287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (self->epfd < 0)
1289 return pyepoll_err_closed();
1290 return PyLong_FromLong(self->epfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001291}
1292
1293PyDoc_STRVAR(pyepoll_fileno_doc,
1294"fileno() -> int\n\
1295\n\
1296Return the epoll control file descriptor.");
1297
1298static PyObject*
1299pyepoll_fromfd(PyObject *cls, PyObject *args)
1300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1304 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001305
Benjamin Peterson95c16622011-12-27 15:36:32 -06001306 return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001307}
1308
1309PyDoc_STRVAR(pyepoll_fromfd_doc,
1310"fromfd(fd) -> epoll\n\
1311\n\
1312Create an epoll object from a given control fd.");
1313
1314static PyObject *
1315pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events)
1316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 struct epoll_event ev;
1318 int result;
1319 int fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 if (epfd < 0)
1322 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 fd = PyObject_AsFileDescriptor(pfd);
1325 if (fd == -1) {
1326 return NULL;
1327 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001328
Guido van Rossumee07b942013-12-06 17:46:22 -08001329 switch (op) {
1330 case EPOLL_CTL_ADD:
1331 case EPOLL_CTL_MOD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 ev.events = events;
1333 ev.data.fd = fd;
1334 Py_BEGIN_ALLOW_THREADS
1335 result = epoll_ctl(epfd, op, fd, &ev);
1336 Py_END_ALLOW_THREADS
1337 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001338 case EPOLL_CTL_DEL:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL
1340 * operation required a non-NULL pointer in event, even
1341 * though this argument is ignored. */
1342 Py_BEGIN_ALLOW_THREADS
1343 result = epoll_ctl(epfd, op, fd, &ev);
1344 if (errno == EBADF) {
1345 /* fd already closed */
1346 result = 0;
1347 errno = 0;
1348 }
1349 Py_END_ALLOW_THREADS
1350 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001351 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 result = -1;
1353 errno = EINVAL;
1354 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 if (result < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001357 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 return NULL;
1359 }
1360 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001361}
1362
1363static PyObject *
1364pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 PyObject *pfd;
1367 unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI;
1368 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist,
1371 &pfd, &events)) {
1372 return NULL;
1373 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001376}
1377
1378PyDoc_STRVAR(pyepoll_register_doc,
Georg Brandl222569d2010-08-02 20:47:56 +00001379"register(fd[, eventmask]) -> None\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001380\n\
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001381Registers a new fd or raises an OSError if the fd is already registered.\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00001382fd is the target file descriptor of the operation.\n\
1383events is a bit set composed of the various EPOLL constants; the default\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001384is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\
1385\n\
1386The epoll interface supports all file descriptors that support poll.");
1387
1388static PyObject *
1389pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 PyObject *pfd;
1392 unsigned int events;
1393 static char *kwlist[] = {"fd", "eventmask", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist,
1396 &pfd, &events)) {
1397 return NULL;
1398 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001401}
1402
1403PyDoc_STRVAR(pyepoll_modify_doc,
1404"modify(fd, eventmask) -> None\n\
1405\n\
1406fd is the target file descriptor of the operation\n\
1407events is a bit set composed of the various EPOLL constants");
1408
1409static PyObject *
1410pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 PyObject *pfd;
1413 static char *kwlist[] = {"fd", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist,
1416 &pfd)) {
1417 return NULL;
1418 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001421}
1422
1423PyDoc_STRVAR(pyepoll_unregister_doc,
1424"unregister(fd) -> None\n\
1425\n\
1426fd is the target file descriptor of the operation.");
1427
1428static PyObject *
1429pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
1430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 double dtimeout = -1.;
1432 int timeout;
1433 int maxevents = -1;
1434 int nfds, i;
1435 PyObject *elist = NULL, *etuple = NULL;
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001436 struct epoll_event *evs = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 static char *kwlist[] = {"timeout", "maxevents", NULL};
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 if (self->epfd < 0)
1440 return pyepoll_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist,
1443 &dtimeout, &maxevents)) {
1444 return NULL;
1445 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (dtimeout < 0) {
1448 timeout = -1;
1449 }
1450 else if (dtimeout * 1000.0 > INT_MAX) {
1451 PyErr_SetString(PyExc_OverflowError,
1452 "timeout is too large");
1453 return NULL;
1454 }
1455 else {
Victor Stinnerdcd97402014-01-31 12:12:53 +01001456 /* epoll_wait() has a resolution of 1 millisecond, round away from zero
1457 to wait *at least* dtimeout seconds. */
1458 timeout = (int)ceil(dtimeout * 1000.0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 if (maxevents == -1) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001462 maxevents = FD_SETSIZE-1;
1463 }
1464 else if (maxevents < 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 PyErr_Format(PyExc_ValueError,
1466 "maxevents must be greater than 0, got %d",
1467 maxevents);
1468 return NULL;
1469 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001470
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001471 evs = PyMem_New(struct epoll_event, maxevents);
1472 if (evs == NULL) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001473 PyErr_NoMemory();
1474 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 Py_BEGIN_ALLOW_THREADS
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001478 nfds = epoll_wait(self->epfd, evs, maxevents, timeout);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 Py_END_ALLOW_THREADS
1480 if (nfds < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001481 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 goto error;
1483 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 elist = PyList_New(nfds);
1486 if (elist == NULL) {
1487 goto error;
1488 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 for (i = 0; i < nfds; i++) {
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001491 etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (etuple == NULL) {
1493 Py_CLEAR(elist);
1494 goto error;
1495 }
1496 PyList_SET_ITEM(elist, i, etuple);
1497 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001498
Christian Heimesf6cd9672008-03-26 13:45:42 +00001499 error:
Charles-François Natalia6ebb2d2013-01-12 12:31:00 +01001500 PyMem_Free(evs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 return elist;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001502}
1503
1504PyDoc_STRVAR(pyepoll_poll_doc,
1505"poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\
1506\n\
1507Wait for events on the epoll file descriptor for a maximum time of timeout\n\
1508in seconds (as float). -1 makes poll wait indefinitely.\n\
1509Up to maxevents are returned to the caller.");
1510
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001511static PyObject *
1512pyepoll_enter(pyEpoll_Object *self, PyObject *args)
1513{
1514 if (self->epfd < 0)
1515 return pyepoll_err_closed();
1516
1517 Py_INCREF(self);
1518 return (PyObject *)self;
1519}
1520
1521static PyObject *
1522pyepoll_exit(PyObject *self, PyObject *args)
1523{
1524 _Py_IDENTIFIER(close);
1525
1526 return _PyObject_CallMethodId(self, &PyId_close, NULL);
1527}
1528
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001529static PyMethodDef pyepoll_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 {"fromfd", (PyCFunction)pyepoll_fromfd,
1531 METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc},
1532 {"close", (PyCFunction)pyepoll_close, METH_NOARGS,
1533 pyepoll_close_doc},
1534 {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS,
1535 pyepoll_fileno_doc},
1536 {"modify", (PyCFunction)pyepoll_modify,
1537 METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc},
1538 {"register", (PyCFunction)pyepoll_register,
1539 METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc},
1540 {"unregister", (PyCFunction)pyepoll_unregister,
1541 METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc},
1542 {"poll", (PyCFunction)pyepoll_poll,
1543 METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc},
Antoine Pitrou09bb89b2012-12-15 21:14:21 +01001544 {"__enter__", (PyCFunction)pyepoll_enter, METH_NOARGS,
1545 NULL},
1546 {"__exit__", (PyCFunction)pyepoll_exit, METH_VARARGS,
1547 NULL},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001549};
1550
1551static PyGetSetDef pyepoll_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 {"closed", (getter)pyepoll_get_closed, NULL,
1553 "True if the epoll handler is closed"},
1554 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001555};
1556
1557PyDoc_STRVAR(pyepoll_doc,
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06001558"select.epoll(sizehint=-1, flags=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001559\n\
1560Returns an epolling object\n\
1561\n\
1562sizehint must be a positive integer or -1 for the default size. The\n\
1563sizehint is used to optimize internal data structures. It doesn't limit\n\
1564the maximum number of monitored events.");
1565
1566static PyTypeObject pyEpoll_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 PyVarObject_HEAD_INIT(NULL, 0)
1568 "select.epoll", /* tp_name */
1569 sizeof(pyEpoll_Object), /* tp_basicsize */
1570 0, /* tp_itemsize */
1571 (destructor)pyepoll_dealloc, /* tp_dealloc */
1572 0, /* tp_print */
1573 0, /* tp_getattr */
1574 0, /* tp_setattr */
1575 0, /* tp_reserved */
1576 0, /* tp_repr */
1577 0, /* tp_as_number */
1578 0, /* tp_as_sequence */
1579 0, /* tp_as_mapping */
1580 0, /* tp_hash */
1581 0, /* tp_call */
1582 0, /* tp_str */
1583 PyObject_GenericGetAttr, /* tp_getattro */
1584 0, /* tp_setattro */
1585 0, /* tp_as_buffer */
1586 Py_TPFLAGS_DEFAULT, /* tp_flags */
1587 pyepoll_doc, /* tp_doc */
1588 0, /* tp_traverse */
1589 0, /* tp_clear */
1590 0, /* tp_richcompare */
1591 0, /* tp_weaklistoffset */
1592 0, /* tp_iter */
1593 0, /* tp_iternext */
1594 pyepoll_methods, /* tp_methods */
1595 0, /* tp_members */
1596 pyepoll_getsetlist, /* tp_getset */
1597 0, /* tp_base */
1598 0, /* tp_dict */
1599 0, /* tp_descr_get */
1600 0, /* tp_descr_set */
1601 0, /* tp_dictoffset */
1602 0, /* tp_init */
1603 0, /* tp_alloc */
1604 pyepoll_new, /* tp_new */
1605 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001606};
1607
1608#endif /* HAVE_EPOLL */
1609
1610#ifdef HAVE_KQUEUE
1611/* **************************************************************************
1612 * kqueue interface for BSD
1613 *
1614 * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
1615 * All rights reserved.
1616 *
1617 * Redistribution and use in source and binary forms, with or without
1618 * modification, are permitted provided that the following conditions
1619 * are met:
1620 * 1. Redistributions of source code must retain the above copyright
1621 * notice, this list of conditions and the following disclaimer.
1622 * 2. Redistributions in binary form must reproduce the above copyright
1623 * notice, this list of conditions and the following disclaimer in the
1624 * documentation and/or other materials provided with the distribution.
1625 *
1626 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1627 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1628 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1629 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1630 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1631 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1632 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1633 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1634 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1635 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1636 * SUCH DAMAGE.
1637 */
1638
1639#ifdef HAVE_SYS_EVENT_H
1640#include <sys/event.h>
1641#endif
1642
1643PyDoc_STRVAR(kqueue_event_doc,
Benjamin Peterson1baf4652009-12-31 03:11:23 +00001644"kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001645\n\
1646This object is the equivalent of the struct kevent for the C API.\n\
1647\n\
1648See the kqueue manpage for more detailed information about the meaning\n\
1649of the arguments.\n\
1650\n\
1651One minor note: while you might hope that udata could store a\n\
1652reference to a python object, it cannot, because it is impossible to\n\
1653keep a proper reference count of the object once it's passed into the\n\
1654kernel. Therefore, I have restricted it to only storing an integer. I\n\
1655recommend ignoring it and simply using the 'ident' field to key off\n\
1656of. You could also set up a dictionary on the python side to store a\n\
1657udata->object mapping.");
1658
1659typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 PyObject_HEAD
1661 struct kevent e;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001662} kqueue_event_Object;
1663
1664static PyTypeObject kqueue_event_Type;
1665
1666#define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type))
1667
1668typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 PyObject_HEAD
1670 SOCKET kqfd; /* kqueue control fd */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001671} kqueue_queue_Object;
1672
1673static PyTypeObject kqueue_queue_Type;
1674
1675#define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type))
1676
Antoine Pitroud83f1e62009-11-04 21:10:38 +00001677#if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P)
1678# error uintptr_t does not match void *!
1679#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
1680# define T_UINTPTRT T_ULONGLONG
1681# define T_INTPTRT T_LONGLONG
1682# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
1683# define UINTPTRT_FMT_UNIT "K"
1684# define INTPTRT_FMT_UNIT "L"
1685#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
1686# define T_UINTPTRT T_ULONG
1687# define T_INTPTRT T_LONG
1688# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1689# define UINTPTRT_FMT_UNIT "k"
1690# define INTPTRT_FMT_UNIT "l"
1691#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
1692# define T_UINTPTRT T_UINT
1693# define T_INTPTRT T_INT
1694# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
1695# define UINTPTRT_FMT_UNIT "I"
1696# define INTPTRT_FMT_UNIT "i"
1697#else
1698# error uintptr_t does not match int, long, or long long!
1699#endif
1700
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001701/*
1702 * kevent is not standard and its members vary across BSDs.
1703 */
1704#if !defined(__OpenBSD__)
Christian Heimesaf01f662013-12-21 16:19:10 +01001705# define IDENT_TYPE T_UINTPTRT
1706# define IDENT_CAST Py_intptr_t
1707# define DATA_TYPE T_INTPTRT
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001708# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
Christian Heimesaf01f662013-12-21 16:19:10 +01001709# define IDENT_AsType PyLong_AsUintptr_t
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001710#else
Christian Heimesaf01f662013-12-21 16:19:10 +01001711# define IDENT_TYPE T_UINT
1712# define IDENT_CAST int
1713# define DATA_TYPE T_INT
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001714# define DATA_FMT_UNIT "i"
Christian Heimesaf01f662013-12-21 16:19:10 +01001715# define IDENT_AsType PyLong_AsUnsignedLong
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001716#endif
1717
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001718/* Unfortunately, we can't store python objects in udata, because
1719 * kevents in the kernel can be removed without warning, which would
1720 * forever lose the refcount on the object stored with it.
1721 */
1722
1723#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
1724static struct PyMemberDef kqueue_event_members[] = {
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001725 {"ident", IDENT_TYPE, KQ_OFF(e.ident)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 {"filter", T_SHORT, KQ_OFF(e.filter)},
1727 {"flags", T_USHORT, KQ_OFF(e.flags)},
1728 {"fflags", T_UINT, KQ_OFF(e.fflags)},
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001729 {"data", DATA_TYPE, KQ_OFF(e.data)},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 {"udata", T_UINTPTRT, KQ_OFF(e.udata)},
1731 {NULL} /* Sentinel */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001732};
1733#undef KQ_OFF
1734
1735static PyObject *
Georg Brandlc0e22b72010-03-14 10:51:01 +00001736
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001737kqueue_event_repr(kqueue_event_Object *s)
1738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 char buf[1024];
1740 PyOS_snprintf(
1741 buf, sizeof(buf),
1742 "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1743 "data=0x%zd udata=%p>",
1744 (size_t)(s->e.ident), s->e.filter, s->e.flags,
1745 s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1746 return PyUnicode_FromString(buf);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001747}
1748
1749static int
1750kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
1751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 PyObject *pfd;
1753 static char *kwlist[] = {"ident", "filter", "flags", "fflags",
1754 "data", "udata", NULL};
Christian Heimesf1fe1592013-08-25 14:57:00 +02001755 static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1760 &pfd, &(self->e.filter), &(self->e.flags),
1761 &(self->e.fflags), &(self->e.data), &(self->e.udata))) {
1762 return -1;
1763 }
1764
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001765 if (PyLong_Check(pfd)
1766#if IDENT_TYPE == T_UINT
Christian Heimesaf01f662013-12-21 16:19:10 +01001767 && PyLong_AsUnsignedLong(pfd) <= UINT_MAX
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001768#endif
1769 ) {
1770 self->e.ident = IDENT_AsType(pfd);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 }
1772 else {
1773 self->e.ident = PyObject_AsFileDescriptor(pfd);
1774 }
1775 if (PyErr_Occurred()) {
1776 return -1;
1777 }
1778 return 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001779}
1780
1781static PyObject *
1782kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 int op)
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 Py_intptr_t result = 0;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 if (!kqueue_event_Check(o)) {
1788 if (op == Py_EQ || op == Py_NE) {
1789 PyObject *res = op == Py_EQ ? Py_False : Py_True;
1790 Py_INCREF(res);
1791 return res;
1792 }
1793 PyErr_Format(PyExc_TypeError,
1794 "can't compare %.200s to %.200s",
1795 Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1796 return NULL;
1797 }
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001798 if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 ((result = s->e.filter - o->e.filter) == 0) &&
1800 ((result = s->e.flags - o->e.flags) == 0) &&
Charles-Francois Natali002a77d2013-05-06 21:24:31 +02001801 ((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 ((result = s->e.data - o->e.data) == 0) &&
1803 ((result = s->e.udata - o->e.udata) == 0)
1804 ) {
1805 result = 0;
1806 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 switch (op) {
Guido van Rossumee07b942013-12-06 17:46:22 -08001809 case Py_EQ:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 result = (result == 0);
1811 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001812 case Py_NE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 result = (result != 0);
1814 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001815 case Py_LE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 result = (result <= 0);
1817 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001818 case Py_GE:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 result = (result >= 0);
1820 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001821 case Py_LT:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 result = (result < 0);
1823 break;
Guido van Rossumee07b942013-12-06 17:46:22 -08001824 case Py_GT:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 result = (result > 0);
1826 break;
1827 }
1828 return PyBool_FromLong((long)result);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001829}
1830
1831static PyTypeObject kqueue_event_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 PyVarObject_HEAD_INIT(NULL, 0)
1833 "select.kevent", /* tp_name */
1834 sizeof(kqueue_event_Object), /* tp_basicsize */
1835 0, /* tp_itemsize */
1836 0, /* tp_dealloc */
1837 0, /* tp_print */
1838 0, /* tp_getattr */
1839 0, /* tp_setattr */
1840 0, /* tp_reserved */
1841 (reprfunc)kqueue_event_repr, /* tp_repr */
1842 0, /* tp_as_number */
1843 0, /* tp_as_sequence */
1844 0, /* tp_as_mapping */
1845 0, /* tp_hash */
1846 0, /* tp_call */
1847 0, /* tp_str */
1848 0, /* tp_getattro */
1849 0, /* tp_setattro */
1850 0, /* tp_as_buffer */
1851 Py_TPFLAGS_DEFAULT, /* tp_flags */
1852 kqueue_event_doc, /* tp_doc */
1853 0, /* tp_traverse */
1854 0, /* tp_clear */
1855 (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */
1856 0, /* tp_weaklistoffset */
1857 0, /* tp_iter */
1858 0, /* tp_iternext */
1859 0, /* tp_methods */
1860 kqueue_event_members, /* tp_members */
1861 0, /* tp_getset */
1862 0, /* tp_base */
1863 0, /* tp_dict */
1864 0, /* tp_descr_get */
1865 0, /* tp_descr_set */
1866 0, /* tp_dictoffset */
1867 (initproc)kqueue_event_init, /* tp_init */
1868 0, /* tp_alloc */
1869 0, /* tp_new */
1870 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001871};
1872
1873static PyObject *
1874kqueue_queue_err_closed(void)
1875{
Victor Stinner13423c32013-08-22 00:19:50 +02001876 PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue object");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001878}
1879
1880static int
1881kqueue_queue_internal_close(kqueue_queue_Object *self)
1882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 int save_errno = 0;
1884 if (self->kqfd >= 0) {
1885 int kqfd = self->kqfd;
1886 self->kqfd = -1;
1887 Py_BEGIN_ALLOW_THREADS
1888 if (close(kqfd) < 0)
1889 save_errno = errno;
1890 Py_END_ALLOW_THREADS
1891 }
1892 return save_errno;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001893}
1894
1895static PyObject *
1896newKqueue_Object(PyTypeObject *type, SOCKET fd)
1897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 kqueue_queue_Object *self;
1899 assert(type != NULL && type->tp_alloc != NULL);
1900 self = (kqueue_queue_Object *) type->tp_alloc(type, 0);
1901 if (self == NULL) {
1902 return NULL;
1903 }
1904
1905 if (fd == -1) {
1906 Py_BEGIN_ALLOW_THREADS
1907 self->kqfd = kqueue();
1908 Py_END_ALLOW_THREADS
1909 }
1910 else {
1911 self->kqfd = fd;
1912 }
1913 if (self->kqfd < 0) {
1914 Py_DECREF(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001915 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 return NULL;
1917 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001918
1919 if (fd == -1) {
1920 if (_Py_set_inheritable(self->kqfd, 0, NULL) < 0) {
1921 Py_DECREF(self);
1922 return NULL;
1923 }
1924 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 return (PyObject *)self;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001926}
1927
1928static PyObject *
1929kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931 if ((args != NULL && PyObject_Size(args)) ||
1932 (kwds != NULL && PyObject_Size(kwds))) {
1933 PyErr_SetString(PyExc_ValueError,
1934 "select.kqueue doesn't accept arguments");
1935 return NULL;
1936 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 return newKqueue_Object(type, -1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001939}
1940
1941static void
1942kqueue_queue_dealloc(kqueue_queue_Object *self)
1943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 kqueue_queue_internal_close(self);
1945 Py_TYPE(self)->tp_free(self);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001946}
1947
1948static PyObject*
1949kqueue_queue_close(kqueue_queue_Object *self)
1950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 errno = kqueue_queue_internal_close(self);
1952 if (errno < 0) {
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001953 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 return NULL;
1955 }
1956 Py_RETURN_NONE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001957}
1958
1959PyDoc_STRVAR(kqueue_queue_close_doc,
1960"close() -> None\n\
1961\n\
1962Close the kqueue control file descriptor. Further operations on the kqueue\n\
1963object will raise an exception.");
1964
1965static PyObject*
1966kqueue_queue_get_closed(kqueue_queue_Object *self)
1967{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 if (self->kqfd < 0)
1969 Py_RETURN_TRUE;
1970 else
1971 Py_RETURN_FALSE;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001972}
1973
1974static PyObject*
1975kqueue_queue_fileno(kqueue_queue_Object *self)
1976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 if (self->kqfd < 0)
1978 return kqueue_queue_err_closed();
1979 return PyLong_FromLong(self->kqfd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001980}
1981
1982PyDoc_STRVAR(kqueue_queue_fileno_doc,
1983"fileno() -> int\n\
1984\n\
1985Return the kqueue control file descriptor.");
1986
1987static PyObject*
1988kqueue_queue_fromfd(PyObject *cls, PyObject *args)
1989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 SOCKET fd;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 if (!PyArg_ParseTuple(args, "i:fromfd", &fd))
1993 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 return newKqueue_Object((PyTypeObject*)cls, fd);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001996}
1997
1998PyDoc_STRVAR(kqueue_queue_fromfd_doc,
1999"fromfd(fd) -> kqueue\n\
2000\n\
2001Create a kqueue object from a given control fd.");
2002
2003static PyObject *
2004kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
2005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 int nevents = 0;
2007 int gotevents = 0;
2008 int nchanges = 0;
2009 int i = 0;
2010 PyObject *otimeout = NULL;
2011 PyObject *ch = NULL;
2012 PyObject *it = NULL, *ei = NULL;
2013 PyObject *result = NULL;
2014 struct kevent *evl = NULL;
2015 struct kevent *chl = NULL;
Victor Stinnerd327f9d2012-03-13 15:29:08 +01002016 struct timespec timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 struct timespec *ptimeoutspec;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 if (self->kqfd < 0)
2020 return kqueue_queue_err_closed();
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout))
2023 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 if (nevents < 0) {
2026 PyErr_Format(PyExc_ValueError,
2027 "Length of eventlist must be 0 or positive, got %d",
2028 nevents);
2029 return NULL;
2030 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 if (otimeout == Py_None || otimeout == NULL) {
2033 ptimeoutspec = NULL;
2034 }
2035 else if (PyNumber_Check(otimeout)) {
Victor Stinner3c1b3792014-02-17 00:02:43 +01002036 if (_PyTime_ObjectToTimespec(otimeout, &timeout.tv_sec,
2037 &timeout.tv_nsec, _PyTime_ROUND_UP) == -1)
Victor Stinner5d272cc2012-03-13 13:35:55 +01002038 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002039
Victor Stinner5d272cc2012-03-13 13:35:55 +01002040 if (timeout.tv_sec < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 PyErr_SetString(PyExc_ValueError,
2042 "timeout must be positive or None");
2043 return NULL;
2044 }
Victor Stinnerd528b012012-03-13 16:25:35 +01002045 ptimeoutspec = &timeout;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 }
2047 else {
2048 PyErr_Format(PyExc_TypeError,
2049 "timeout argument must be an number "
2050 "or None, got %.200s",
2051 Py_TYPE(otimeout)->tp_name);
2052 return NULL;
2053 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 if (ch != NULL && ch != Py_None) {
2056 it = PyObject_GetIter(ch);
2057 if (it == NULL) {
2058 PyErr_SetString(PyExc_TypeError,
2059 "changelist is not iterable");
2060 return NULL;
2061 }
2062 nchanges = PyObject_Size(ch);
2063 if (nchanges < 0) {
2064 goto error;
2065 }
Georg Brandlc0e22b72010-03-14 10:51:01 +00002066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 chl = PyMem_New(struct kevent, nchanges);
2068 if (chl == NULL) {
2069 PyErr_NoMemory();
2070 goto error;
2071 }
2072 i = 0;
2073 while ((ei = PyIter_Next(it)) != NULL) {
2074 if (!kqueue_event_Check(ei)) {
2075 Py_DECREF(ei);
2076 PyErr_SetString(PyExc_TypeError,
2077 "changelist must be an iterable of "
2078 "select.kevent objects");
2079 goto error;
2080 } else {
2081 chl[i++] = ((kqueue_event_Object *)ei)->e;
2082 }
2083 Py_DECREF(ei);
2084 }
2085 }
2086 Py_CLEAR(it);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 /* event list */
2089 if (nevents) {
2090 evl = PyMem_New(struct kevent, nevents);
2091 if (evl == NULL) {
2092 PyErr_NoMemory();
2093 goto error;
2094 }
2095 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 Py_BEGIN_ALLOW_THREADS
2098 gotevents = kevent(self->kqfd, chl, nchanges,
2099 evl, nevents, ptimeoutspec);
2100 Py_END_ALLOW_THREADS
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 if (gotevents == -1) {
2103 PyErr_SetFromErrno(PyExc_OSError);
2104 goto error;
2105 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 result = PyList_New(gotevents);
2108 if (result == NULL) {
2109 goto error;
2110 }
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 for (i = 0; i < gotevents; i++) {
2113 kqueue_event_Object *ch;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type);
2116 if (ch == NULL) {
2117 goto error;
2118 }
2119 ch->e = evl[i];
2120 PyList_SET_ITEM(result, i, (PyObject *)ch);
2121 }
2122 PyMem_Free(chl);
2123 PyMem_Free(evl);
2124 return result;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002125
2126 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 PyMem_Free(chl);
2128 PyMem_Free(evl);
2129 Py_XDECREF(result);
2130 Py_XDECREF(it);
2131 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002132}
2133
2134PyDoc_STRVAR(kqueue_queue_control_doc,
Benjamin Peterson9bc93512008-09-22 22:10:59 +00002135"control(changelist, max_events[, timeout=None]) -> eventlist\n\
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002136\n\
2137Calls the kernel kevent function.\n\
2138- changelist must be a list of kevent objects describing the changes\n\
2139 to be made to the kernel's watch list or None.\n\
2140- max_events lets you specify the maximum number of events that the\n\
2141 kernel will return.\n\
2142- timeout is the maximum time to wait in seconds, or else None,\n\
2143 to wait forever. timeout accepts floats for smaller timeouts, too.");
2144
2145
2146static PyMethodDef kqueue_queue_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 {"fromfd", (PyCFunction)kqueue_queue_fromfd,
2148 METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc},
2149 {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS,
2150 kqueue_queue_close_doc},
2151 {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS,
2152 kqueue_queue_fileno_doc},
2153 {"control", (PyCFunction)kqueue_queue_control,
2154 METH_VARARGS , kqueue_queue_control_doc},
2155 {NULL, NULL},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002156};
2157
2158static PyGetSetDef kqueue_queue_getsetlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 {"closed", (getter)kqueue_queue_get_closed, NULL,
2160 "True if the kqueue handler is closed"},
2161 {0},
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002162};
2163
2164PyDoc_STRVAR(kqueue_queue_doc,
2165"Kqueue syscall wrapper.\n\
2166\n\
2167For example, to start watching a socket for input:\n\
2168>>> kq = kqueue()\n\
2169>>> sock = socket()\n\
2170>>> sock.connect((host, port))\n\
2171>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\
2172\n\
2173To wait one second for it to become writeable:\n\
2174>>> kq.control(None, 1, 1000)\n\
2175\n\
2176To stop listening:\n\
2177>>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)");
2178
2179static PyTypeObject kqueue_queue_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 PyVarObject_HEAD_INIT(NULL, 0)
2181 "select.kqueue", /* tp_name */
2182 sizeof(kqueue_queue_Object), /* tp_basicsize */
2183 0, /* tp_itemsize */
2184 (destructor)kqueue_queue_dealloc, /* tp_dealloc */
2185 0, /* tp_print */
2186 0, /* tp_getattr */
2187 0, /* tp_setattr */
2188 0, /* tp_reserved */
2189 0, /* tp_repr */
2190 0, /* tp_as_number */
2191 0, /* tp_as_sequence */
2192 0, /* tp_as_mapping */
2193 0, /* tp_hash */
2194 0, /* tp_call */
2195 0, /* tp_str */
2196 0, /* tp_getattro */
2197 0, /* tp_setattro */
2198 0, /* tp_as_buffer */
2199 Py_TPFLAGS_DEFAULT, /* tp_flags */
2200 kqueue_queue_doc, /* tp_doc */
2201 0, /* tp_traverse */
2202 0, /* tp_clear */
2203 0, /* tp_richcompare */
2204 0, /* tp_weaklistoffset */
2205 0, /* tp_iter */
2206 0, /* tp_iternext */
2207 kqueue_queue_methods, /* tp_methods */
2208 0, /* tp_members */
2209 kqueue_queue_getsetlist, /* tp_getset */
2210 0, /* tp_base */
2211 0, /* tp_dict */
2212 0, /* tp_descr_get */
2213 0, /* tp_descr_set */
2214 0, /* tp_dictoffset */
2215 0, /* tp_init */
2216 0, /* tp_alloc */
2217 kqueue_queue_new, /* tp_new */
2218 0, /* tp_free */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002219};
2220
2221#endif /* HAVE_KQUEUE */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002222
2223
2224
2225
2226
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002227/* ************************************************************************ */
2228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002229PyDoc_STRVAR(select_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002230"select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
2231\n\
2232Wait until one or more file descriptors are ready for some kind of I/O.\n\
Brett Cannon62dba4c2003-09-10 19:37:42 +00002233The first three arguments are sequences of file descriptors to be waited for:\n\
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002234rlist -- wait until ready for reading\n\
2235wlist -- wait until ready for writing\n\
2236xlist -- wait for an ``exceptional condition''\n\
2237If only one kind of condition is required, pass [] for the other lists.\n\
2238A file descriptor is either a socket or file object, or a small integer\n\
2239gotten from a fileno() method call on one of those.\n\
2240\n\
2241The optional 4th argument specifies a timeout in seconds; it may be\n\
2242a floating point number to specify fractions of seconds. If it is absent\n\
2243or None, the call will never time out.\n\
2244\n\
2245The return value is a tuple of three lists corresponding to the first three\n\
2246arguments; each contains the subset of the corresponding file descriptors\n\
2247that are ready.\n\
2248\n\
2249*** IMPORTANT NOTICE ***\n\
Christian Heimesaf01f662013-12-21 16:19:10 +01002250On Windows only sockets are supported; on Unix, all file\n\
Christian Heimesf6cd9672008-03-26 13:45:42 +00002251descriptors can be used.");
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002252
Barry Warsawe4ac0aa1996-12-12 00:04:35 +00002253static PyMethodDef select_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 {"select", select_select, METH_VARARGS, select_doc},
Charles-François Natali986a56c2013-01-19 12:19:10 +01002255#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 {"poll", select_poll, METH_NOARGS, poll_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +00002257#endif /* HAVE_POLL */
Jesus Cead8b9ae62011-11-14 19:07:41 +01002258#ifdef HAVE_SYS_DEVPOLL_H
2259 {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc},
2260#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 {0, 0}, /* sentinel */
Guido van Rossumed233a51992-06-23 09:07:03 +00002262};
2263
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002264PyDoc_STRVAR(module_doc,
Guido van Rossum1d8fb2d1998-06-28 16:54:49 +00002265"This module supports asynchronous I/O on multiple file descriptors.\n\
2266\n\
2267*** IMPORTANT NOTICE ***\n\
Christian Heimesaf01f662013-12-21 16:19:10 +01002268On Windows only sockets are supported; on Unix, all file descriptors.");
Guido van Rossumed233a51992-06-23 09:07:03 +00002269
Martin v. Löwis1a214512008-06-11 05:26:20 +00002270
2271static struct PyModuleDef selectmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 PyModuleDef_HEAD_INIT,
2273 "select",
2274 module_doc,
2275 -1,
2276 select_methods,
2277 NULL,
2278 NULL,
2279 NULL,
2280 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00002281};
2282
Jesus Cead8b9ae62011-11-14 19:07:41 +01002283
2284
2285
Mark Hammond62b1ab12002-07-23 06:31:15 +00002286PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00002287PyInit_select(void)
Guido van Rossumed233a51992-06-23 09:07:03 +00002288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 PyObject *m;
2290 m = PyModule_Create(&selectmodule);
2291 if (m == NULL)
2292 return NULL;
Fred Drake4baedc12002-04-01 14:53:37 +00002293
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002294 Py_INCREF(PyExc_OSError);
2295 PyModule_AddObject(m, "error", PyExc_OSError);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002296
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002297#ifdef PIPE_BUF
R. David Murraye16cda92010-10-15 23:12:57 +00002298#ifdef HAVE_BROKEN_PIPE_BUF
2299#undef PIPE_BUF
2300#define PIPE_BUF 512
2301#endif
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002302 PyModule_AddIntMacro(m, PIPE_BUF);
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +00002303#endif
Gregory P. Smithb970b862009-07-04 02:28:47 +00002304
Charles-François Natali986a56c2013-01-19 12:19:10 +01002305#if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002306#ifdef __APPLE__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 if (select_have_broken_poll()) {
2308 if (PyObject_DelAttrString(m, "poll") == -1) {
2309 PyErr_Clear();
2310 }
2311 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002312#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 {
Thomas Wouters477c8d52006-05-27 19:21:47 +00002314#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 if (PyType_Ready(&poll_Type) < 0)
2316 return NULL;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002317 PyModule_AddIntMacro(m, POLLIN);
2318 PyModule_AddIntMacro(m, POLLPRI);
2319 PyModule_AddIntMacro(m, POLLOUT);
2320 PyModule_AddIntMacro(m, POLLERR);
2321 PyModule_AddIntMacro(m, POLLHUP);
2322 PyModule_AddIntMacro(m, POLLNVAL);
Andrew M. Kuchlingcf96dc82000-08-25 01:15:33 +00002323
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002324#ifdef POLLRDNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002325 PyModule_AddIntMacro(m, POLLRDNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002326#endif
2327#ifdef POLLRDBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002328 PyModule_AddIntMacro(m, POLLRDBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002329#endif
2330#ifdef POLLWRNORM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002331 PyModule_AddIntMacro(m, POLLWRNORM);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002332#endif
2333#ifdef POLLWRBAND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002334 PyModule_AddIntMacro(m, POLLWRBAND);
Andrew M. Kuchling36d97eb2000-09-28 21:33:44 +00002335#endif
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002336#ifdef POLLMSG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002337 PyModule_AddIntMacro(m, POLLMSG);
Sjoerd Mullender239f8362000-08-25 13:59:18 +00002338#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00002340#endif /* HAVE_POLL */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002341
Jesus Cead8b9ae62011-11-14 19:07:41 +01002342#ifdef HAVE_SYS_DEVPOLL_H
2343 if (PyType_Ready(&devpoll_Type) < 0)
2344 return NULL;
2345#endif
2346
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002347#ifdef HAVE_EPOLL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 Py_TYPE(&pyEpoll_Type) = &PyType_Type;
2349 if (PyType_Ready(&pyEpoll_Type) < 0)
2350 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 Py_INCREF(&pyEpoll_Type);
2353 PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002354
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002355 PyModule_AddIntMacro(m, EPOLLIN);
2356 PyModule_AddIntMacro(m, EPOLLOUT);
2357 PyModule_AddIntMacro(m, EPOLLPRI);
2358 PyModule_AddIntMacro(m, EPOLLERR);
2359 PyModule_AddIntMacro(m, EPOLLHUP);
2360 PyModule_AddIntMacro(m, EPOLLET);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002361#ifdef EPOLLONESHOT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 /* Kernel 2.6.2+ */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002363 PyModule_AddIntMacro(m, EPOLLONESHOT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002364#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002366 PyModule_AddIntMacro(m, EPOLLRDNORM);
2367 PyModule_AddIntMacro(m, EPOLLRDBAND);
2368 PyModule_AddIntMacro(m, EPOLLWRNORM);
2369 PyModule_AddIntMacro(m, EPOLLWRBAND);
2370 PyModule_AddIntMacro(m, EPOLLMSG);
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -06002371
Benjamin Peterson95c16622011-12-27 15:36:32 -06002372#ifdef EPOLL_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +02002373 PyModule_AddIntMacro(m, EPOLL_CLOEXEC);
Benjamin Peterson95c16622011-12-27 15:36:32 -06002374#endif
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002375#endif /* HAVE_EPOLL */
2376
2377#ifdef HAVE_KQUEUE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 kqueue_event_Type.tp_new = PyType_GenericNew;
2379 Py_TYPE(&kqueue_event_Type) = &PyType_Type;
2380 if(PyType_Ready(&kqueue_event_Type) < 0)
2381 return NULL;
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 Py_INCREF(&kqueue_event_Type);
2384 PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 Py_TYPE(&kqueue_queue_Type) = &PyType_Type;
2387 if(PyType_Ready(&kqueue_queue_Type) < 0)
2388 return NULL;
2389 Py_INCREF(&kqueue_queue_Type);
2390 PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type);
2391
2392 /* event filters */
2393 PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ);
2394 PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE);
2395 PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO);
2396 PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE);
2397 PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002398#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002400#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL);
2402 PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 /* event flags */
2405 PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD);
2406 PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE);
2407 PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE);
2408 PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE);
2409 PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT);
2410 PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS);
2413 PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF);
2416 PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 /* READ WRITE filter flag */
2419 PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 /* VNODE filter flags */
2422 PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE);
2423 PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE);
2424 PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND);
2425 PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB);
2426 PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK);
2427 PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME);
2428 PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 /* PROC filter flags */
2431 PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT);
2432 PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK);
2433 PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC);
2434 PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK);
2435 PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK);
2438 PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD);
2439 PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR);
2440
2441 /* NETDEV filter flags */
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002442#ifdef EVFILT_NETDEV
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP);
2444 PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN);
2445 PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV);
Christian Heimes4fbc72b2008-03-22 00:47:35 +00002446#endif
2447
2448#endif /* HAVE_KQUEUE */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 return m;
Guido van Rossumed233a51992-06-23 09:07:03 +00002450}