1 | /* select - Module containing unix select(2) call. |
2 | 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. |
5 | */ |
6 | |
7 | #if defined(HAVE_POLL_H) && !defined(_GNU_SOURCE) |
8 | #define _GNU_SOURCE |
9 | #endif |
10 | |
11 | #include "Python.h" |
12 | #include "structmember.h" // PyMemberDef |
13 | |
14 | #ifdef HAVE_SYS_DEVPOLL_H |
15 | #include <sys/resource.h> |
16 | #include <sys/devpoll.h> |
17 | #include <sys/types.h> |
18 | #include <sys/stat.h> |
19 | #include <fcntl.h> |
20 | #endif |
21 | |
22 | #ifdef __APPLE__ |
23 | /* Perform runtime testing for a broken poll on OSX to make it easier |
24 | * to use the same binary on multiple releases of the OS. |
25 | */ |
26 | #undef HAVE_BROKEN_POLL |
27 | #endif |
28 | |
29 | /* Windows #defines FD_SETSIZE to 64 if FD_SETSIZE isn't already defined. |
30 | 64 is too small (too many people have bumped into that limit). |
31 | Here we boost it. |
32 | Users who want even more than the boosted limit should #define |
33 | FD_SETSIZE higher before this; e.g., via compiler /D switch. |
34 | */ |
35 | #if defined(MS_WINDOWS) && !defined(FD_SETSIZE) |
36 | #define FD_SETSIZE 512 |
37 | #endif |
38 | |
39 | #if defined(HAVE_POLL_H) |
40 | #include <poll.h> |
41 | #elif defined(HAVE_SYS_POLL_H) |
42 | #include <sys/poll.h> |
43 | #endif |
44 | |
45 | #ifdef __sgi |
46 | /* This is missing from unistd.h */ |
47 | extern void bzero(void *, int); |
48 | #endif |
49 | |
50 | #ifdef HAVE_SYS_TYPES_H |
51 | #include <sys/types.h> |
52 | #endif |
53 | |
54 | #ifdef MS_WINDOWS |
55 | # define WIN32_LEAN_AND_MEAN |
56 | # include <winsock.h> |
57 | #else |
58 | # define SOCKET int |
59 | #endif |
60 | |
61 | typedef struct { |
62 | PyObject *close; |
63 | PyTypeObject *poll_Type; |
64 | PyTypeObject *devpoll_Type; |
65 | PyTypeObject *pyEpoll_Type; |
66 | PyTypeObject *kqueue_event_Type; |
67 | PyTypeObject *kqueue_queue_Type; |
68 | } _selectstate; |
69 | |
70 | static struct PyModuleDef selectmodule; |
71 | |
72 | static inline _selectstate* |
73 | get_select_state(PyObject *module) |
74 | { |
75 | void *state = PyModule_GetState(module); |
76 | assert(state != NULL); |
77 | return (_selectstate *)state; |
78 | } |
79 | |
80 | #define _selectstate_by_type(type) get_select_state(PyType_GetModule(type)) |
81 | |
82 | /*[clinic input] |
83 | module select |
84 | class select.poll "pollObject *" "_selectstate_by_type(type)->poll_Type" |
85 | class select.devpoll "devpollObject *" "_selectstate_by_type(type)->devpoll_Type" |
86 | class select.epoll "pyEpoll_Object *" "_selectstate_by_type(type)->pyEpoll_Type" |
87 | class select.kqueue "kqueue_queue_Object *" "_selectstate_by_type(type)->kqueue_queue_Type" |
88 | [clinic start generated code]*/ |
89 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=8072de35824aa327]*/ |
90 | |
91 | /* list of Python objects and their file descriptor */ |
92 | typedef struct { |
93 | PyObject *obj; /* owned reference */ |
94 | SOCKET fd; |
95 | int sentinel; /* -1 == sentinel */ |
96 | } pylist; |
97 | |
98 | static void |
99 | reap_obj(pylist fd2obj[FD_SETSIZE + 1]) |
100 | { |
101 | unsigned int i; |
102 | for (i = 0; i < (unsigned int)FD_SETSIZE + 1 && fd2obj[i].sentinel >= 0; i++) { |
103 | Py_CLEAR(fd2obj[i].obj); |
104 | } |
105 | fd2obj[0].sentinel = -1; |
106 | } |
107 | |
108 | |
109 | /* returns -1 and sets the Python exception if an error occurred, otherwise |
110 | returns a number >= 0 |
111 | */ |
112 | static int |
113 | seq2set(PyObject *seq, fd_set *set, pylist fd2obj[FD_SETSIZE + 1]) |
114 | { |
115 | int max = -1; |
116 | unsigned int index = 0; |
117 | Py_ssize_t i; |
118 | PyObject* fast_seq = NULL; |
119 | PyObject* o = NULL; |
120 | |
121 | fd2obj[0].obj = (PyObject*)0; /* set list to zero size */ |
122 | FD_ZERO(set); |
123 | |
124 | fast_seq = PySequence_Fast(seq, "arguments 1-3 must be sequences" ); |
125 | if (!fast_seq) |
126 | return -1; |
127 | |
128 | for (i = 0; i < PySequence_Fast_GET_SIZE(fast_seq); i++) { |
129 | SOCKET v; |
130 | |
131 | /* any intervening fileno() calls could decr this refcnt */ |
132 | if (!(o = PySequence_Fast_GET_ITEM(fast_seq, i))) |
133 | goto finally; |
134 | |
135 | Py_INCREF(o); |
136 | v = PyObject_AsFileDescriptor( o ); |
137 | if (v == -1) goto finally; |
138 | |
139 | #if defined(_MSC_VER) |
140 | max = 0; /* not used for Win32 */ |
141 | #else /* !_MSC_VER */ |
142 | if (!_PyIsSelectable_fd(v)) { |
143 | PyErr_SetString(PyExc_ValueError, |
144 | "filedescriptor out of range in select()" ); |
145 | goto finally; |
146 | } |
147 | if (v > max) |
148 | max = v; |
149 | #endif /* _MSC_VER */ |
150 | FD_SET(v, set); |
151 | |
152 | /* add object and its file descriptor to the list */ |
153 | if (index >= (unsigned int)FD_SETSIZE) { |
154 | PyErr_SetString(PyExc_ValueError, |
155 | "too many file descriptors in select()" ); |
156 | goto finally; |
157 | } |
158 | fd2obj[index].obj = o; |
159 | fd2obj[index].fd = v; |
160 | fd2obj[index].sentinel = 0; |
161 | fd2obj[++index].sentinel = -1; |
162 | } |
163 | Py_DECREF(fast_seq); |
164 | return max+1; |
165 | |
166 | finally: |
167 | Py_XDECREF(o); |
168 | Py_DECREF(fast_seq); |
169 | return -1; |
170 | } |
171 | |
172 | /* returns NULL and sets the Python exception if an error occurred */ |
173 | static PyObject * |
174 | set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 1]) |
175 | { |
176 | int i, j, count=0; |
177 | PyObject *list, *o; |
178 | SOCKET fd; |
179 | |
180 | for (j = 0; fd2obj[j].sentinel >= 0; j++) { |
181 | if (FD_ISSET(fd2obj[j].fd, set)) |
182 | count++; |
183 | } |
184 | list = PyList_New(count); |
185 | if (!list) |
186 | return NULL; |
187 | |
188 | i = 0; |
189 | for (j = 0; fd2obj[j].sentinel >= 0; j++) { |
190 | fd = fd2obj[j].fd; |
191 | if (FD_ISSET(fd, set)) { |
192 | o = fd2obj[j].obj; |
193 | fd2obj[j].obj = NULL; |
194 | /* transfer ownership */ |
195 | if (PyList_SetItem(list, i, o) < 0) |
196 | goto finally; |
197 | |
198 | i++; |
199 | } |
200 | } |
201 | return list; |
202 | finally: |
203 | Py_DECREF(list); |
204 | return NULL; |
205 | } |
206 | |
207 | #undef SELECT_USES_HEAP |
208 | #if FD_SETSIZE > 1024 |
209 | #define SELECT_USES_HEAP |
210 | #endif /* FD_SETSIZE > 1024 */ |
211 | |
212 | /*[clinic input] |
213 | select.select |
214 | |
215 | rlist: object |
216 | wlist: object |
217 | xlist: object |
218 | timeout as timeout_obj: object = None |
219 | / |
220 | |
221 | Wait until one or more file descriptors are ready for some kind of I/O. |
222 | |
223 | The first three arguments are iterables of file descriptors to be waited for: |
224 | rlist -- wait until ready for reading |
225 | wlist -- wait until ready for writing |
226 | xlist -- wait for an "exceptional condition" |
227 | If only one kind of condition is required, pass [] for the other lists. |
228 | |
229 | A file descriptor is either a socket or file object, or a small integer |
230 | gotten from a fileno() method call on one of those. |
231 | |
232 | The optional 4th argument specifies a timeout in seconds; it may be |
233 | a floating point number to specify fractions of seconds. If it is absent |
234 | or None, the call will never time out. |
235 | |
236 | The return value is a tuple of three lists corresponding to the first three |
237 | arguments; each contains the subset of the corresponding file descriptors |
238 | that are ready. |
239 | |
240 | *** IMPORTANT NOTICE *** |
241 | On Windows, only sockets are supported; on Unix, all file |
242 | descriptors can be used. |
243 | [clinic start generated code]*/ |
244 | |
245 | static PyObject * |
246 | select_select_impl(PyObject *module, PyObject *rlist, PyObject *wlist, |
247 | PyObject *xlist, PyObject *timeout_obj) |
248 | /*[clinic end generated code: output=2b3cfa824f7ae4cf input=e467f5d68033de00]*/ |
249 | { |
250 | #ifdef SELECT_USES_HEAP |
251 | pylist *rfd2obj, *wfd2obj, *efd2obj; |
252 | #else /* !SELECT_USES_HEAP */ |
253 | /* XXX: All this should probably be implemented as follows: |
254 | * - find the highest descriptor we're interested in |
255 | * - add one |
256 | * - that's the size |
257 | * See: Stevens, APitUE, $12.5.1 |
258 | */ |
259 | pylist rfd2obj[FD_SETSIZE + 1]; |
260 | pylist wfd2obj[FD_SETSIZE + 1]; |
261 | pylist efd2obj[FD_SETSIZE + 1]; |
262 | #endif /* SELECT_USES_HEAP */ |
263 | PyObject *ret = NULL; |
264 | fd_set ifdset, ofdset, efdset; |
265 | struct timeval tv, *tvp; |
266 | int imax, omax, emax, max; |
267 | int n; |
268 | _PyTime_t timeout, deadline = 0; |
269 | |
270 | if (timeout_obj == Py_None) |
271 | tvp = (struct timeval *)NULL; |
272 | else { |
273 | if (_PyTime_FromSecondsObject(&timeout, timeout_obj, |
274 | _PyTime_ROUND_TIMEOUT) < 0) { |
275 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
276 | PyErr_SetString(PyExc_TypeError, |
277 | "timeout must be a float or None" ); |
278 | } |
279 | return NULL; |
280 | } |
281 | |
282 | if (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_TIMEOUT) == -1) |
283 | return NULL; |
284 | if (tv.tv_sec < 0) { |
285 | PyErr_SetString(PyExc_ValueError, "timeout must be non-negative" ); |
286 | return NULL; |
287 | } |
288 | tvp = &tv; |
289 | } |
290 | |
291 | #ifdef SELECT_USES_HEAP |
292 | /* Allocate memory for the lists */ |
293 | rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1); |
294 | wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1); |
295 | efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 1); |
296 | if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) { |
297 | if (rfd2obj) PyMem_Free(rfd2obj); |
298 | if (wfd2obj) PyMem_Free(wfd2obj); |
299 | if (efd2obj) PyMem_Free(efd2obj); |
300 | return PyErr_NoMemory(); |
301 | } |
302 | #endif /* SELECT_USES_HEAP */ |
303 | |
304 | /* Convert iterables to fd_sets, and get maximum fd number |
305 | * propagates the Python exception set in seq2set() |
306 | */ |
307 | rfd2obj[0].sentinel = -1; |
308 | wfd2obj[0].sentinel = -1; |
309 | efd2obj[0].sentinel = -1; |
310 | if ((imax = seq2set(rlist, &ifdset, rfd2obj)) < 0) |
311 | goto finally; |
312 | if ((omax = seq2set(wlist, &ofdset, wfd2obj)) < 0) |
313 | goto finally; |
314 | if ((emax = seq2set(xlist, &efdset, efd2obj)) < 0) |
315 | goto finally; |
316 | |
317 | max = imax; |
318 | if (omax > max) max = omax; |
319 | if (emax > max) max = emax; |
320 | |
321 | if (tvp) |
322 | deadline = _PyTime_GetMonotonicClock() + timeout; |
323 | |
324 | do { |
325 | Py_BEGIN_ALLOW_THREADS |
326 | errno = 0; |
327 | n = select(max, &ifdset, &ofdset, &efdset, tvp); |
328 | Py_END_ALLOW_THREADS |
329 | |
330 | if (errno != EINTR) |
331 | break; |
332 | |
333 | /* select() was interrupted by a signal */ |
334 | if (PyErr_CheckSignals()) |
335 | goto finally; |
336 | |
337 | if (tvp) { |
338 | timeout = deadline - _PyTime_GetMonotonicClock(); |
339 | if (timeout < 0) { |
340 | /* bpo-35310: lists were unmodified -- clear them explicitly */ |
341 | FD_ZERO(&ifdset); |
342 | FD_ZERO(&ofdset); |
343 | FD_ZERO(&efdset); |
344 | n = 0; |
345 | break; |
346 | } |
347 | _PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING); |
348 | /* retry select() with the recomputed timeout */ |
349 | } |
350 | } while (1); |
351 | |
352 | #ifdef MS_WINDOWS |
353 | if (n == SOCKET_ERROR) { |
354 | PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError()); |
355 | } |
356 | #else |
357 | if (n < 0) { |
358 | PyErr_SetFromErrno(PyExc_OSError); |
359 | } |
360 | #endif |
361 | else { |
362 | /* any of these three calls can raise an exception. it's more |
363 | convenient to test for this after all three calls... but |
364 | is that acceptable? |
365 | */ |
366 | rlist = set2list(&ifdset, rfd2obj); |
367 | wlist = set2list(&ofdset, wfd2obj); |
368 | xlist = set2list(&efdset, efd2obj); |
369 | if (PyErr_Occurred()) |
370 | ret = NULL; |
371 | else |
372 | ret = PyTuple_Pack(3, rlist, wlist, xlist); |
373 | |
374 | Py_XDECREF(rlist); |
375 | Py_XDECREF(wlist); |
376 | Py_XDECREF(xlist); |
377 | } |
378 | |
379 | finally: |
380 | reap_obj(rfd2obj); |
381 | reap_obj(wfd2obj); |
382 | reap_obj(efd2obj); |
383 | #ifdef SELECT_USES_HEAP |
384 | PyMem_Free(rfd2obj); |
385 | PyMem_Free(wfd2obj); |
386 | PyMem_Free(efd2obj); |
387 | #endif /* SELECT_USES_HEAP */ |
388 | return ret; |
389 | } |
390 | |
391 | #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL) |
392 | /* |
393 | * poll() support |
394 | */ |
395 | |
396 | typedef struct { |
397 | PyObject_HEAD |
398 | PyObject *dict; |
399 | int ufd_uptodate; |
400 | int ufd_len; |
401 | struct pollfd *ufds; |
402 | int poll_running; |
403 | } pollObject; |
404 | |
405 | /* Update the malloc'ed array of pollfds to match the dictionary |
406 | contained within a pollObject. Return 1 on success, 0 on an error. |
407 | */ |
408 | |
409 | static int |
410 | update_ufd_array(pollObject *self) |
411 | { |
412 | Py_ssize_t i, pos; |
413 | PyObject *key, *value; |
414 | struct pollfd *old_ufds = self->ufds; |
415 | |
416 | self->ufd_len = PyDict_GET_SIZE(self->dict); |
417 | PyMem_RESIZE(self->ufds, struct pollfd, self->ufd_len); |
418 | if (self->ufds == NULL) { |
419 | self->ufds = old_ufds; |
420 | PyErr_NoMemory(); |
421 | return 0; |
422 | } |
423 | |
424 | i = pos = 0; |
425 | while (PyDict_Next(self->dict, &pos, &key, &value)) { |
426 | assert(i < self->ufd_len); |
427 | /* Never overflow */ |
428 | self->ufds[i].fd = (int)PyLong_AsLong(key); |
429 | self->ufds[i].events = (short)(unsigned short)PyLong_AsLong(value); |
430 | i++; |
431 | } |
432 | assert(i == self->ufd_len); |
433 | self->ufd_uptodate = 1; |
434 | return 1; |
435 | } |
436 | |
437 | /*[clinic input] |
438 | select.poll.register |
439 | |
440 | fd: fildes |
441 | either an integer, or an object with a fileno() method returning an int |
442 | eventmask: unsigned_short(c_default="POLLIN | POLLPRI | POLLOUT") = select.POLLIN | select.POLLPRI | select.POLLOUT |
443 | an optional bitmask describing the type of events to check for |
444 | / |
445 | |
446 | Register a file descriptor with the polling object. |
447 | [clinic start generated code]*/ |
448 | |
449 | static PyObject * |
450 | select_poll_register_impl(pollObject *self, int fd, unsigned short eventmask) |
451 | /*[clinic end generated code: output=0dc7173c800a4a65 input=34e16cfb28d3c900]*/ |
452 | { |
453 | PyObject *key, *value; |
454 | int err; |
455 | |
456 | /* Add entry to the internal dictionary: the key is the |
457 | file descriptor, and the value is the event mask. */ |
458 | key = PyLong_FromLong(fd); |
459 | if (key == NULL) |
460 | return NULL; |
461 | value = PyLong_FromLong(eventmask); |
462 | if (value == NULL) { |
463 | Py_DECREF(key); |
464 | return NULL; |
465 | } |
466 | err = PyDict_SetItem(self->dict, key, value); |
467 | Py_DECREF(key); |
468 | Py_DECREF(value); |
469 | if (err < 0) |
470 | return NULL; |
471 | |
472 | self->ufd_uptodate = 0; |
473 | |
474 | Py_RETURN_NONE; |
475 | } |
476 | |
477 | |
478 | /*[clinic input] |
479 | select.poll.modify |
480 | |
481 | fd: fildes |
482 | either an integer, or an object with a fileno() method returning |
483 | an int |
484 | eventmask: unsigned_short |
485 | a bitmask describing the type of events to check for |
486 | / |
487 | |
488 | Modify an already registered file descriptor. |
489 | [clinic start generated code]*/ |
490 | |
491 | static PyObject * |
492 | select_poll_modify_impl(pollObject *self, int fd, unsigned short eventmask) |
493 | /*[clinic end generated code: output=1a7b88bf079eff17 input=a8e383df075c32cf]*/ |
494 | { |
495 | PyObject *key, *value; |
496 | int err; |
497 | |
498 | /* Modify registered fd */ |
499 | key = PyLong_FromLong(fd); |
500 | if (key == NULL) |
501 | return NULL; |
502 | err = PyDict_Contains(self->dict, key); |
503 | if (err < 0) { |
504 | Py_DECREF(key); |
505 | return NULL; |
506 | } |
507 | if (err == 0) { |
508 | errno = ENOENT; |
509 | PyErr_SetFromErrno(PyExc_OSError); |
510 | Py_DECREF(key); |
511 | return NULL; |
512 | } |
513 | value = PyLong_FromLong(eventmask); |
514 | if (value == NULL) { |
515 | Py_DECREF(key); |
516 | return NULL; |
517 | } |
518 | err = PyDict_SetItem(self->dict, key, value); |
519 | Py_DECREF(key); |
520 | Py_DECREF(value); |
521 | if (err < 0) |
522 | return NULL; |
523 | |
524 | self->ufd_uptodate = 0; |
525 | |
526 | Py_RETURN_NONE; |
527 | } |
528 | |
529 | |
530 | /*[clinic input] |
531 | select.poll.unregister |
532 | |
533 | fd: fildes |
534 | / |
535 | |
536 | Remove a file descriptor being tracked by the polling object. |
537 | [clinic start generated code]*/ |
538 | |
539 | static PyObject * |
540 | select_poll_unregister_impl(pollObject *self, int fd) |
541 | /*[clinic end generated code: output=8c9f42e75e7d291b input=4b4fccc1040e79cb]*/ |
542 | { |
543 | PyObject *key; |
544 | |
545 | /* Check whether the fd is already in the array */ |
546 | key = PyLong_FromLong(fd); |
547 | if (key == NULL) |
548 | return NULL; |
549 | |
550 | if (PyDict_DelItem(self->dict, key) == -1) { |
551 | Py_DECREF(key); |
552 | /* This will simply raise the KeyError set by PyDict_DelItem |
553 | if the file descriptor isn't registered. */ |
554 | return NULL; |
555 | } |
556 | |
557 | Py_DECREF(key); |
558 | self->ufd_uptodate = 0; |
559 | |
560 | Py_RETURN_NONE; |
561 | } |
562 | |
563 | /*[clinic input] |
564 | select.poll.poll |
565 | |
566 | timeout as timeout_obj: object = None |
567 | The maximum time to wait in milliseconds, or else None (or a negative |
568 | value) to wait indefinitely. |
569 | / |
570 | |
571 | Polls the set of registered file descriptors. |
572 | |
573 | Returns a list containing any descriptors that have events or errors to |
574 | report, as a list of (fd, event) 2-tuples. |
575 | [clinic start generated code]*/ |
576 | |
577 | static PyObject * |
578 | select_poll_poll_impl(pollObject *self, PyObject *timeout_obj) |
579 | /*[clinic end generated code: output=876e837d193ed7e4 input=c2f6953ec45e5622]*/ |
580 | { |
581 | PyObject *result_list = NULL; |
582 | int poll_result, i, j; |
583 | PyObject *value = NULL, *num = NULL; |
584 | _PyTime_t timeout = -1, ms = -1, deadline = 0; |
585 | int async_err = 0; |
586 | |
587 | if (timeout_obj != Py_None) { |
588 | if (_PyTime_FromMillisecondsObject(&timeout, timeout_obj, |
589 | _PyTime_ROUND_TIMEOUT) < 0) { |
590 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
591 | PyErr_SetString(PyExc_TypeError, |
592 | "timeout must be an integer or None" ); |
593 | } |
594 | return NULL; |
595 | } |
596 | |
597 | ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_TIMEOUT); |
598 | if (ms < INT_MIN || ms > INT_MAX) { |
599 | PyErr_SetString(PyExc_OverflowError, "timeout is too large" ); |
600 | return NULL; |
601 | } |
602 | |
603 | if (timeout >= 0) { |
604 | deadline = _PyTime_GetMonotonicClock() + timeout; |
605 | } |
606 | } |
607 | |
608 | /* On some OSes, typically BSD-based ones, the timeout parameter of the |
609 | poll() syscall, when negative, must be exactly INFTIM, where defined, |
610 | or -1. See issue 31334. */ |
611 | if (ms < 0) { |
612 | #ifdef INFTIM |
613 | ms = INFTIM; |
614 | #else |
615 | ms = -1; |
616 | #endif |
617 | } |
618 | |
619 | /* Avoid concurrent poll() invocation, issue 8865 */ |
620 | if (self->poll_running) { |
621 | PyErr_SetString(PyExc_RuntimeError, |
622 | "concurrent poll() invocation" ); |
623 | return NULL; |
624 | } |
625 | |
626 | /* Ensure the ufd array is up to date */ |
627 | if (!self->ufd_uptodate) |
628 | if (update_ufd_array(self) == 0) |
629 | return NULL; |
630 | |
631 | self->poll_running = 1; |
632 | |
633 | /* call poll() */ |
634 | async_err = 0; |
635 | do { |
636 | Py_BEGIN_ALLOW_THREADS |
637 | errno = 0; |
638 | poll_result = poll(self->ufds, self->ufd_len, (int)ms); |
639 | Py_END_ALLOW_THREADS |
640 | |
641 | if (errno != EINTR) |
642 | break; |
643 | |
644 | /* poll() was interrupted by a signal */ |
645 | if (PyErr_CheckSignals()) { |
646 | async_err = 1; |
647 | break; |
648 | } |
649 | |
650 | if (timeout >= 0) { |
651 | timeout = deadline - _PyTime_GetMonotonicClock(); |
652 | if (timeout < 0) { |
653 | poll_result = 0; |
654 | break; |
655 | } |
656 | ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
657 | /* retry poll() with the recomputed timeout */ |
658 | } |
659 | } while (1); |
660 | |
661 | self->poll_running = 0; |
662 | |
663 | if (poll_result < 0) { |
664 | if (!async_err) |
665 | PyErr_SetFromErrno(PyExc_OSError); |
666 | return NULL; |
667 | } |
668 | |
669 | /* build the result list */ |
670 | |
671 | result_list = PyList_New(poll_result); |
672 | if (!result_list) |
673 | return NULL; |
674 | |
675 | for (i = 0, j = 0; j < poll_result; j++) { |
676 | /* skip to the next fired descriptor */ |
677 | while (!self->ufds[i].revents) { |
678 | i++; |
679 | } |
680 | /* if we hit a NULL return, set value to NULL |
681 | and break out of loop; code at end will |
682 | clean up result_list */ |
683 | value = PyTuple_New(2); |
684 | if (value == NULL) |
685 | goto error; |
686 | num = PyLong_FromLong(self->ufds[i].fd); |
687 | if (num == NULL) { |
688 | Py_DECREF(value); |
689 | goto error; |
690 | } |
691 | PyTuple_SET_ITEM(value, 0, num); |
692 | |
693 | /* The &0xffff is a workaround for AIX. 'revents' |
694 | is a 16-bit short, and IBM assigned POLLNVAL |
695 | to be 0x8000, so the conversion to int results |
696 | in a negative number. See SF bug #923315. */ |
697 | num = PyLong_FromLong(self->ufds[i].revents & 0xffff); |
698 | if (num == NULL) { |
699 | Py_DECREF(value); |
700 | goto error; |
701 | } |
702 | PyTuple_SET_ITEM(value, 1, num); |
703 | PyList_SET_ITEM(result_list, j, value); |
704 | i++; |
705 | } |
706 | return result_list; |
707 | |
708 | error: |
709 | Py_DECREF(result_list); |
710 | return NULL; |
711 | } |
712 | |
713 | static pollObject * |
714 | newPollObject(PyObject *module) |
715 | { |
716 | pollObject *self; |
717 | self = PyObject_New(pollObject, get_select_state(module)->poll_Type); |
718 | if (self == NULL) |
719 | return NULL; |
720 | /* ufd_uptodate is a Boolean, denoting whether the |
721 | array pointed to by ufds matches the contents of the dictionary. */ |
722 | self->ufd_uptodate = 0; |
723 | self->ufds = NULL; |
724 | self->poll_running = 0; |
725 | self->dict = PyDict_New(); |
726 | if (self->dict == NULL) { |
727 | Py_DECREF(self); |
728 | return NULL; |
729 | } |
730 | return self; |
731 | } |
732 | |
733 | static void |
734 | poll_dealloc(pollObject *self) |
735 | { |
736 | PyObject* type = (PyObject *)Py_TYPE(self); |
737 | if (self->ufds != NULL) |
738 | PyMem_Free(self->ufds); |
739 | Py_XDECREF(self->dict); |
740 | PyObject_Free(self); |
741 | Py_DECREF(type); |
742 | } |
743 | |
744 | |
745 | #ifdef HAVE_SYS_DEVPOLL_H |
746 | static PyMethodDef devpoll_methods[]; |
747 | |
748 | typedef struct { |
749 | PyObject_HEAD |
750 | int fd_devpoll; |
751 | int max_n_fds; |
752 | int n_fds; |
753 | struct pollfd *fds; |
754 | } devpollObject; |
755 | |
756 | static PyObject * |
757 | devpoll_err_closed(void) |
758 | { |
759 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed devpoll object" ); |
760 | return NULL; |
761 | } |
762 | |
763 | static int devpoll_flush(devpollObject *self) |
764 | { |
765 | int size, n; |
766 | |
767 | if (!self->n_fds) return 0; |
768 | |
769 | size = sizeof(struct pollfd)*self->n_fds; |
770 | self->n_fds = 0; |
771 | |
772 | n = _Py_write(self->fd_devpoll, self->fds, size); |
773 | if (n == -1) |
774 | return -1; |
775 | |
776 | if (n < size) { |
777 | /* |
778 | ** Data writed to /dev/poll is a binary data structure. It is not |
779 | ** clear what to do if a partial write occurred. For now, raise |
780 | ** an exception and see if we actually found this problem in |
781 | ** the wild. |
782 | ** See http://bugs.python.org/issue6397. |
783 | */ |
784 | PyErr_Format(PyExc_OSError, "failed to write all pollfds. " |
785 | "Please, report at http://bugs.python.org/. " |
786 | "Data to report: Size tried: %d, actual size written: %d." , |
787 | size, n); |
788 | return -1; |
789 | } |
790 | return 0; |
791 | } |
792 | |
793 | static PyObject * |
794 | internal_devpoll_register(devpollObject *self, int fd, |
795 | unsigned short events, int remove) |
796 | { |
797 | if (self->fd_devpoll < 0) |
798 | return devpoll_err_closed(); |
799 | |
800 | if (remove) { |
801 | self->fds[self->n_fds].fd = fd; |
802 | self->fds[self->n_fds].events = POLLREMOVE; |
803 | |
804 | if (++self->n_fds == self->max_n_fds) { |
805 | if (devpoll_flush(self)) |
806 | return NULL; |
807 | } |
808 | } |
809 | |
810 | self->fds[self->n_fds].fd = fd; |
811 | self->fds[self->n_fds].events = (signed short)events; |
812 | |
813 | if (++self->n_fds == self->max_n_fds) { |
814 | if (devpoll_flush(self)) |
815 | return NULL; |
816 | } |
817 | |
818 | Py_RETURN_NONE; |
819 | } |
820 | |
821 | /*[clinic input] |
822 | select.devpoll.register |
823 | |
824 | fd: fildes |
825 | either an integer, or an object with a fileno() method returning |
826 | an int |
827 | eventmask: unsigned_short(c_default="POLLIN | POLLPRI | POLLOUT") = select.POLLIN | select.POLLPRI | select.POLLOUT |
828 | an optional bitmask describing the type of events to check for |
829 | / |
830 | |
831 | Register a file descriptor with the polling object. |
832 | [clinic start generated code]*/ |
833 | |
834 | static PyObject * |
835 | select_devpoll_register_impl(devpollObject *self, int fd, |
836 | unsigned short eventmask) |
837 | /*[clinic end generated code: output=6e07fe8b74abba0c input=22006fabe9567522]*/ |
838 | { |
839 | return internal_devpoll_register(self, fd, eventmask, 0); |
840 | } |
841 | |
842 | /*[clinic input] |
843 | select.devpoll.modify |
844 | |
845 | fd: fildes |
846 | either an integer, or an object with a fileno() method returning |
847 | an int |
848 | eventmask: unsigned_short(c_default="POLLIN | POLLPRI | POLLOUT") = select.POLLIN | select.POLLPRI | select.POLLOUT |
849 | an optional bitmask describing the type of events to check for |
850 | / |
851 | |
852 | Modify a possible already registered file descriptor. |
853 | [clinic start generated code]*/ |
854 | |
855 | static PyObject * |
856 | select_devpoll_modify_impl(devpollObject *self, int fd, |
857 | unsigned short eventmask) |
858 | /*[clinic end generated code: output=bc2e6d23aaff98b4 input=09fa335db7cdc09e]*/ |
859 | { |
860 | return internal_devpoll_register(self, fd, eventmask, 1); |
861 | } |
862 | |
863 | /*[clinic input] |
864 | select.devpoll.unregister |
865 | |
866 | fd: fildes |
867 | / |
868 | |
869 | Remove a file descriptor being tracked by the polling object. |
870 | [clinic start generated code]*/ |
871 | |
872 | static PyObject * |
873 | select_devpoll_unregister_impl(devpollObject *self, int fd) |
874 | /*[clinic end generated code: output=95519ffa0c7d43fe input=b4ea42a4442fd467]*/ |
875 | { |
876 | if (self->fd_devpoll < 0) |
877 | return devpoll_err_closed(); |
878 | |
879 | self->fds[self->n_fds].fd = fd; |
880 | self->fds[self->n_fds].events = POLLREMOVE; |
881 | |
882 | if (++self->n_fds == self->max_n_fds) { |
883 | if (devpoll_flush(self)) |
884 | return NULL; |
885 | } |
886 | |
887 | Py_RETURN_NONE; |
888 | } |
889 | |
890 | /*[clinic input] |
891 | select.devpoll.poll |
892 | timeout as timeout_obj: object = None |
893 | The maximum time to wait in milliseconds, or else None (or a negative |
894 | value) to wait indefinitely. |
895 | / |
896 | |
897 | Polls the set of registered file descriptors. |
898 | |
899 | Returns a list containing any descriptors that have events or errors to |
900 | report, as a list of (fd, event) 2-tuples. |
901 | [clinic start generated code]*/ |
902 | |
903 | static PyObject * |
904 | select_devpoll_poll_impl(devpollObject *self, PyObject *timeout_obj) |
905 | /*[clinic end generated code: output=2654e5457cca0b3c input=3c3f0a355ec2bedb]*/ |
906 | { |
907 | struct dvpoll dvp; |
908 | PyObject *result_list = NULL; |
909 | int poll_result, i; |
910 | PyObject *value, *num1, *num2; |
911 | _PyTime_t timeout, ms, deadline = 0; |
912 | |
913 | if (self->fd_devpoll < 0) |
914 | return devpoll_err_closed(); |
915 | |
916 | /* Check values for timeout */ |
917 | if (timeout_obj == Py_None) { |
918 | timeout = -1; |
919 | ms = -1; |
920 | } |
921 | else { |
922 | if (_PyTime_FromMillisecondsObject(&timeout, timeout_obj, |
923 | _PyTime_ROUND_TIMEOUT) < 0) { |
924 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
925 | PyErr_SetString(PyExc_TypeError, |
926 | "timeout must be an integer or None" ); |
927 | } |
928 | return NULL; |
929 | } |
930 | |
931 | ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_TIMEOUT); |
932 | if (ms < -1 || ms > INT_MAX) { |
933 | PyErr_SetString(PyExc_OverflowError, "timeout is too large" ); |
934 | return NULL; |
935 | } |
936 | } |
937 | |
938 | if (devpoll_flush(self)) |
939 | return NULL; |
940 | |
941 | dvp.dp_fds = self->fds; |
942 | dvp.dp_nfds = self->max_n_fds; |
943 | dvp.dp_timeout = (int)ms; |
944 | |
945 | if (timeout >= 0) |
946 | deadline = _PyTime_GetMonotonicClock() + timeout; |
947 | |
948 | do { |
949 | /* call devpoll() */ |
950 | Py_BEGIN_ALLOW_THREADS |
951 | errno = 0; |
952 | poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp); |
953 | Py_END_ALLOW_THREADS |
954 | |
955 | if (errno != EINTR) |
956 | break; |
957 | |
958 | /* devpoll() was interrupted by a signal */ |
959 | if (PyErr_CheckSignals()) |
960 | return NULL; |
961 | |
962 | if (timeout >= 0) { |
963 | timeout = deadline - _PyTime_GetMonotonicClock(); |
964 | if (timeout < 0) { |
965 | poll_result = 0; |
966 | break; |
967 | } |
968 | ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
969 | dvp.dp_timeout = (int)ms; |
970 | /* retry devpoll() with the recomputed timeout */ |
971 | } |
972 | } while (1); |
973 | |
974 | if (poll_result < 0) { |
975 | PyErr_SetFromErrno(PyExc_OSError); |
976 | return NULL; |
977 | } |
978 | |
979 | /* build the result list */ |
980 | result_list = PyList_New(poll_result); |
981 | if (!result_list) |
982 | return NULL; |
983 | |
984 | for (i = 0; i < poll_result; i++) { |
985 | num1 = PyLong_FromLong(self->fds[i].fd); |
986 | num2 = PyLong_FromLong(self->fds[i].revents); |
987 | if ((num1 == NULL) || (num2 == NULL)) { |
988 | Py_XDECREF(num1); |
989 | Py_XDECREF(num2); |
990 | goto error; |
991 | } |
992 | value = PyTuple_Pack(2, num1, num2); |
993 | Py_DECREF(num1); |
994 | Py_DECREF(num2); |
995 | if (value == NULL) |
996 | goto error; |
997 | PyList_SET_ITEM(result_list, i, value); |
998 | } |
999 | |
1000 | return result_list; |
1001 | |
1002 | error: |
1003 | Py_DECREF(result_list); |
1004 | return NULL; |
1005 | } |
1006 | |
1007 | static int |
1008 | devpoll_internal_close(devpollObject *self) |
1009 | { |
1010 | int save_errno = 0; |
1011 | if (self->fd_devpoll >= 0) { |
1012 | int fd = self->fd_devpoll; |
1013 | self->fd_devpoll = -1; |
1014 | Py_BEGIN_ALLOW_THREADS |
1015 | if (close(fd) < 0) |
1016 | save_errno = errno; |
1017 | Py_END_ALLOW_THREADS |
1018 | } |
1019 | return save_errno; |
1020 | } |
1021 | |
1022 | /*[clinic input] |
1023 | select.devpoll.close |
1024 | |
1025 | Close the devpoll file descriptor. |
1026 | |
1027 | Further operations on the devpoll object will raise an exception. |
1028 | [clinic start generated code]*/ |
1029 | |
1030 | static PyObject * |
1031 | select_devpoll_close_impl(devpollObject *self) |
1032 | /*[clinic end generated code: output=26b355bd6429f21b input=6273c30f5560a99b]*/ |
1033 | { |
1034 | errno = devpoll_internal_close(self); |
1035 | if (errno < 0) { |
1036 | PyErr_SetFromErrno(PyExc_OSError); |
1037 | return NULL; |
1038 | } |
1039 | Py_RETURN_NONE; |
1040 | } |
1041 | |
1042 | static PyObject* |
1043 | devpoll_get_closed(devpollObject *self, void *Py_UNUSED(ignored)) |
1044 | { |
1045 | if (self->fd_devpoll < 0) |
1046 | Py_RETURN_TRUE; |
1047 | else |
1048 | Py_RETURN_FALSE; |
1049 | } |
1050 | |
1051 | /*[clinic input] |
1052 | select.devpoll.fileno |
1053 | |
1054 | Return the file descriptor. |
1055 | [clinic start generated code]*/ |
1056 | |
1057 | static PyObject * |
1058 | select_devpoll_fileno_impl(devpollObject *self) |
1059 | /*[clinic end generated code: output=26920929f8d292f4 input=ef15331ebde6c368]*/ |
1060 | { |
1061 | if (self->fd_devpoll < 0) |
1062 | return devpoll_err_closed(); |
1063 | return PyLong_FromLong(self->fd_devpoll); |
1064 | } |
1065 | |
1066 | static PyGetSetDef devpoll_getsetlist[] = { |
1067 | {"closed" , (getter)devpoll_get_closed, NULL, |
1068 | "True if the devpoll object is closed" }, |
1069 | {0}, |
1070 | }; |
1071 | |
1072 | static devpollObject * |
1073 | newDevPollObject(PyObject *module) |
1074 | { |
1075 | devpollObject *self; |
1076 | int fd_devpoll, limit_result; |
1077 | struct pollfd *fds; |
1078 | struct rlimit limit; |
1079 | |
1080 | /* |
1081 | ** If we try to process more that getrlimit() |
1082 | ** fds, the kernel will give an error, so |
1083 | ** we set the limit here. It is a dynamic |
1084 | ** value, because we can change rlimit() anytime. |
1085 | */ |
1086 | limit_result = getrlimit(RLIMIT_NOFILE, &limit); |
1087 | if (limit_result == -1) { |
1088 | PyErr_SetFromErrno(PyExc_OSError); |
1089 | return NULL; |
1090 | } |
1091 | |
1092 | fd_devpoll = _Py_open("/dev/poll" , O_RDWR); |
1093 | if (fd_devpoll == -1) |
1094 | return NULL; |
1095 | |
1096 | fds = PyMem_NEW(struct pollfd, limit.rlim_cur); |
1097 | if (fds == NULL) { |
1098 | close(fd_devpoll); |
1099 | PyErr_NoMemory(); |
1100 | return NULL; |
1101 | } |
1102 | |
1103 | self = PyObject_New(devpollObject, get_select_state(module)->devpoll_Type); |
1104 | if (self == NULL) { |
1105 | close(fd_devpoll); |
1106 | PyMem_Free(fds); |
1107 | return NULL; |
1108 | } |
1109 | self->fd_devpoll = fd_devpoll; |
1110 | self->max_n_fds = limit.rlim_cur; |
1111 | self->n_fds = 0; |
1112 | self->fds = fds; |
1113 | |
1114 | return self; |
1115 | } |
1116 | |
1117 | static void |
1118 | devpoll_dealloc(devpollObject *self) |
1119 | { |
1120 | PyObject *type = (PyObject *)Py_TYPE(self); |
1121 | (void)devpoll_internal_close(self); |
1122 | PyMem_Free(self->fds); |
1123 | PyObject_Free(self); |
1124 | Py_DECREF(type); |
1125 | } |
1126 | |
1127 | static PyType_Slot devpoll_Type_slots[] = { |
1128 | {Py_tp_dealloc, devpoll_dealloc}, |
1129 | {Py_tp_getset, devpoll_getsetlist}, |
1130 | {Py_tp_methods, devpoll_methods}, |
1131 | {0, 0}, |
1132 | }; |
1133 | |
1134 | static PyType_Spec devpoll_Type_spec = { |
1135 | "select.devpoll" , |
1136 | sizeof(devpollObject), |
1137 | 0, |
1138 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, |
1139 | devpoll_Type_slots |
1140 | }; |
1141 | |
1142 | #endif /* HAVE_SYS_DEVPOLL_H */ |
1143 | |
1144 | |
1145 | /*[clinic input] |
1146 | select.poll |
1147 | |
1148 | Returns a polling object. |
1149 | |
1150 | This object supports registering and unregistering file descriptors, and then |
1151 | polling them for I/O events. |
1152 | [clinic start generated code]*/ |
1153 | |
1154 | static PyObject * |
1155 | select_poll_impl(PyObject *module) |
1156 | /*[clinic end generated code: output=16a665a4e1d228c5 input=3f877909d5696bbf]*/ |
1157 | { |
1158 | return (PyObject *)newPollObject(module); |
1159 | } |
1160 | |
1161 | #ifdef HAVE_SYS_DEVPOLL_H |
1162 | |
1163 | /*[clinic input] |
1164 | select.devpoll |
1165 | |
1166 | Returns a polling object. |
1167 | |
1168 | This object supports registering and unregistering file descriptors, and then |
1169 | polling them for I/O events. |
1170 | [clinic start generated code]*/ |
1171 | |
1172 | static PyObject * |
1173 | select_devpoll_impl(PyObject *module) |
1174 | /*[clinic end generated code: output=ea9213cc87fd9581 input=53a1af94564f00a3]*/ |
1175 | { |
1176 | return (PyObject *)newDevPollObject(module); |
1177 | } |
1178 | #endif |
1179 | |
1180 | |
1181 | #ifdef __APPLE__ |
1182 | /* |
1183 | * On some systems poll() sets errno on invalid file descriptors. We test |
1184 | * for this at runtime because this bug may be fixed or introduced between |
1185 | * OS releases. |
1186 | */ |
1187 | static int select_have_broken_poll(void) |
1188 | { |
1189 | int poll_test; |
1190 | int filedes[2]; |
1191 | |
1192 | struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 }; |
1193 | |
1194 | /* Create a file descriptor to make invalid */ |
1195 | if (pipe(filedes) < 0) { |
1196 | return 1; |
1197 | } |
1198 | poll_struct.fd = filedes[0]; |
1199 | close(filedes[0]); |
1200 | close(filedes[1]); |
1201 | poll_test = poll(&poll_struct, 1, 0); |
1202 | if (poll_test < 0) { |
1203 | return 1; |
1204 | } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) { |
1205 | return 1; |
1206 | } |
1207 | return 0; |
1208 | } |
1209 | #endif /* __APPLE__ */ |
1210 | |
1211 | #endif /* HAVE_POLL */ |
1212 | |
1213 | #ifdef HAVE_EPOLL |
1214 | /* ************************************************************************** |
1215 | * epoll interface for Linux 2.6 |
1216 | * |
1217 | * Written by Christian Heimes |
1218 | * Inspired by Twisted's _epoll.pyx and select.poll() |
1219 | */ |
1220 | |
1221 | #ifdef HAVE_SYS_EPOLL_H |
1222 | #include <sys/epoll.h> |
1223 | #endif |
1224 | |
1225 | typedef struct { |
1226 | PyObject_HEAD |
1227 | SOCKET epfd; /* epoll control file descriptor */ |
1228 | } pyEpoll_Object; |
1229 | |
1230 | static PyObject * |
1231 | pyepoll_err_closed(void) |
1232 | { |
1233 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll object" ); |
1234 | return NULL; |
1235 | } |
1236 | |
1237 | static int |
1238 | pyepoll_internal_close(pyEpoll_Object *self) |
1239 | { |
1240 | int save_errno = 0; |
1241 | if (self->epfd >= 0) { |
1242 | int epfd = self->epfd; |
1243 | self->epfd = -1; |
1244 | Py_BEGIN_ALLOW_THREADS |
1245 | if (close(epfd) < 0) |
1246 | save_errno = errno; |
1247 | Py_END_ALLOW_THREADS |
1248 | } |
1249 | return save_errno; |
1250 | } |
1251 | |
1252 | static PyObject * |
1253 | newPyEpoll_Object(PyTypeObject *type, int sizehint, SOCKET fd) |
1254 | { |
1255 | pyEpoll_Object *self; |
1256 | assert(type != NULL); |
1257 | allocfunc epoll_alloc = PyType_GetSlot(type, Py_tp_alloc); |
1258 | assert(epoll_alloc != NULL); |
1259 | self = (pyEpoll_Object *) epoll_alloc(type, 0); |
1260 | if (self == NULL) |
1261 | return NULL; |
1262 | |
1263 | if (fd == -1) { |
1264 | Py_BEGIN_ALLOW_THREADS |
1265 | #ifdef HAVE_EPOLL_CREATE1 |
1266 | self->epfd = epoll_create1(EPOLL_CLOEXEC); |
1267 | #else |
1268 | self->epfd = epoll_create(sizehint); |
1269 | #endif |
1270 | Py_END_ALLOW_THREADS |
1271 | } |
1272 | else { |
1273 | self->epfd = fd; |
1274 | } |
1275 | if (self->epfd < 0) { |
1276 | Py_DECREF(self); |
1277 | PyErr_SetFromErrno(PyExc_OSError); |
1278 | return NULL; |
1279 | } |
1280 | |
1281 | #ifndef HAVE_EPOLL_CREATE1 |
1282 | if (fd == -1 && _Py_set_inheritable(self->epfd, 0, NULL) < 0) { |
1283 | Py_DECREF(self); |
1284 | return NULL; |
1285 | } |
1286 | #endif |
1287 | |
1288 | return (PyObject *)self; |
1289 | } |
1290 | |
1291 | |
1292 | /*[clinic input] |
1293 | @classmethod |
1294 | select.epoll.__new__ |
1295 | |
1296 | sizehint: int = -1 |
1297 | The expected number of events to be registered. It must be positive, |
1298 | or -1 to use the default. It is only used on older systems where |
1299 | epoll_create1() is not available; otherwise it has no effect (though its |
1300 | value is still checked). |
1301 | flags: int = 0 |
1302 | Deprecated and completely ignored. However, when supplied, its value |
1303 | must be 0 or select.EPOLL_CLOEXEC, otherwise OSError is raised. |
1304 | |
1305 | Returns an epolling object. |
1306 | [clinic start generated code]*/ |
1307 | |
1308 | static PyObject * |
1309 | select_epoll_impl(PyTypeObject *type, int sizehint, int flags) |
1310 | /*[clinic end generated code: output=c87404e705013bb5 input=303e3295e7975e43]*/ |
1311 | { |
1312 | if (sizehint == -1) { |
1313 | sizehint = FD_SETSIZE - 1; |
1314 | } |
1315 | else if (sizehint <= 0) { |
1316 | PyErr_SetString(PyExc_ValueError, "negative sizehint" ); |
1317 | return NULL; |
1318 | } |
1319 | |
1320 | #ifdef HAVE_EPOLL_CREATE1 |
1321 | if (flags && flags != EPOLL_CLOEXEC) { |
1322 | PyErr_SetString(PyExc_OSError, "invalid flags" ); |
1323 | return NULL; |
1324 | } |
1325 | #endif |
1326 | |
1327 | return newPyEpoll_Object(type, sizehint, -1); |
1328 | } |
1329 | |
1330 | |
1331 | static void |
1332 | pyepoll_dealloc(pyEpoll_Object *self) |
1333 | { |
1334 | PyTypeObject* type = Py_TYPE(self); |
1335 | (void)pyepoll_internal_close(self); |
1336 | freefunc epoll_free = PyType_GetSlot(type, Py_tp_free); |
1337 | epoll_free((PyObject *)self); |
1338 | Py_DECREF((PyObject *)type); |
1339 | } |
1340 | |
1341 | /*[clinic input] |
1342 | select.epoll.close |
1343 | |
1344 | Close the epoll control file descriptor. |
1345 | |
1346 | Further operations on the epoll object will raise an exception. |
1347 | [clinic start generated code]*/ |
1348 | |
1349 | static PyObject * |
1350 | select_epoll_close_impl(pyEpoll_Object *self) |
1351 | /*[clinic end generated code: output=ee2144c446a1a435 input=ca6c66ba5a736bfd]*/ |
1352 | { |
1353 | errno = pyepoll_internal_close(self); |
1354 | if (errno < 0) { |
1355 | PyErr_SetFromErrno(PyExc_OSError); |
1356 | return NULL; |
1357 | } |
1358 | Py_RETURN_NONE; |
1359 | } |
1360 | |
1361 | |
1362 | static PyObject* |
1363 | pyepoll_get_closed(pyEpoll_Object *self, void *Py_UNUSED(ignored)) |
1364 | { |
1365 | if (self->epfd < 0) |
1366 | Py_RETURN_TRUE; |
1367 | else |
1368 | Py_RETURN_FALSE; |
1369 | } |
1370 | |
1371 | /*[clinic input] |
1372 | select.epoll.fileno |
1373 | |
1374 | Return the epoll control file descriptor. |
1375 | [clinic start generated code]*/ |
1376 | |
1377 | static PyObject * |
1378 | select_epoll_fileno_impl(pyEpoll_Object *self) |
1379 | /*[clinic end generated code: output=e171375fdc619ba3 input=c11091a6aee60b5c]*/ |
1380 | { |
1381 | if (self->epfd < 0) |
1382 | return pyepoll_err_closed(); |
1383 | return PyLong_FromLong(self->epfd); |
1384 | } |
1385 | |
1386 | |
1387 | /*[clinic input] |
1388 | @classmethod |
1389 | select.epoll.fromfd |
1390 | |
1391 | fd: int |
1392 | / |
1393 | |
1394 | Create an epoll object from a given control fd. |
1395 | [clinic start generated code]*/ |
1396 | |
1397 | static PyObject * |
1398 | select_epoll_fromfd_impl(PyTypeObject *type, int fd) |
1399 | /*[clinic end generated code: output=c15de2a083524e8e input=faecefdb55e3046e]*/ |
1400 | { |
1401 | SOCKET s_fd = (SOCKET)fd; |
1402 | return newPyEpoll_Object(type, FD_SETSIZE - 1, s_fd); |
1403 | } |
1404 | |
1405 | |
1406 | static PyObject * |
1407 | pyepoll_internal_ctl(int epfd, int op, int fd, unsigned int events) |
1408 | { |
1409 | struct epoll_event ev; |
1410 | int result; |
1411 | |
1412 | if (epfd < 0) |
1413 | return pyepoll_err_closed(); |
1414 | |
1415 | switch (op) { |
1416 | case EPOLL_CTL_ADD: |
1417 | case EPOLL_CTL_MOD: |
1418 | ev.events = events; |
1419 | ev.data.fd = fd; |
1420 | Py_BEGIN_ALLOW_THREADS |
1421 | result = epoll_ctl(epfd, op, fd, &ev); |
1422 | Py_END_ALLOW_THREADS |
1423 | break; |
1424 | case EPOLL_CTL_DEL: |
1425 | /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL |
1426 | * operation required a non-NULL pointer in event, even |
1427 | * though this argument is ignored. */ |
1428 | Py_BEGIN_ALLOW_THREADS |
1429 | result = epoll_ctl(epfd, op, fd, &ev); |
1430 | Py_END_ALLOW_THREADS |
1431 | break; |
1432 | default: |
1433 | result = -1; |
1434 | errno = EINVAL; |
1435 | } |
1436 | |
1437 | if (result < 0) { |
1438 | PyErr_SetFromErrno(PyExc_OSError); |
1439 | return NULL; |
1440 | } |
1441 | Py_RETURN_NONE; |
1442 | } |
1443 | |
1444 | /*[clinic input] |
1445 | select.epoll.register |
1446 | |
1447 | fd: fildes |
1448 | the target file descriptor of the operation |
1449 | eventmask: unsigned_int(c_default="EPOLLIN | EPOLLPRI | EPOLLOUT", bitwise=True) = select.EPOLLIN | select.EPOLLPRI | select.EPOLLOUT |
1450 | a bit set composed of the various EPOLL constants |
1451 | |
1452 | Registers a new fd or raises an OSError if the fd is already registered. |
1453 | |
1454 | The epoll interface supports all file descriptors that support poll. |
1455 | [clinic start generated code]*/ |
1456 | |
1457 | static PyObject * |
1458 | select_epoll_register_impl(pyEpoll_Object *self, int fd, |
1459 | unsigned int eventmask) |
1460 | /*[clinic end generated code: output=318e5e6386520599 input=a5071b71edfe3578]*/ |
1461 | { |
1462 | return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, fd, eventmask); |
1463 | } |
1464 | |
1465 | /*[clinic input] |
1466 | select.epoll.modify |
1467 | |
1468 | fd: fildes |
1469 | the target file descriptor of the operation |
1470 | eventmask: unsigned_int(bitwise=True) |
1471 | a bit set composed of the various EPOLL constants |
1472 | |
1473 | Modify event mask for a registered file descriptor. |
1474 | [clinic start generated code]*/ |
1475 | |
1476 | static PyObject * |
1477 | select_epoll_modify_impl(pyEpoll_Object *self, int fd, |
1478 | unsigned int eventmask) |
1479 | /*[clinic end generated code: output=7e3447307cff6f65 input=88a83dac53a8c3da]*/ |
1480 | { |
1481 | return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, fd, eventmask); |
1482 | } |
1483 | |
1484 | /*[clinic input] |
1485 | select.epoll.unregister |
1486 | |
1487 | fd: fildes |
1488 | the target file descriptor of the operation |
1489 | |
1490 | Remove a registered file descriptor from the epoll object. |
1491 | [clinic start generated code]*/ |
1492 | |
1493 | static PyObject * |
1494 | select_epoll_unregister_impl(pyEpoll_Object *self, int fd) |
1495 | /*[clinic end generated code: output=07c5dbd612a512d4 input=3093f68d3644743d]*/ |
1496 | { |
1497 | return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, fd, 0); |
1498 | } |
1499 | |
1500 | /*[clinic input] |
1501 | select.epoll.poll |
1502 | |
1503 | timeout as timeout_obj: object = None |
1504 | the maximum time to wait in seconds (as float); |
1505 | a timeout of None or -1 makes poll wait indefinitely |
1506 | maxevents: int = -1 |
1507 | the maximum number of events returned; -1 means no limit |
1508 | |
1509 | Wait for events on the epoll file descriptor. |
1510 | |
1511 | Returns a list containing any descriptors that have events to report, |
1512 | as a list of (fd, events) 2-tuples. |
1513 | [clinic start generated code]*/ |
1514 | |
1515 | static PyObject * |
1516 | select_epoll_poll_impl(pyEpoll_Object *self, PyObject *timeout_obj, |
1517 | int maxevents) |
1518 | /*[clinic end generated code: output=e02d121a20246c6c input=33d34a5ea430fd5b]*/ |
1519 | { |
1520 | int nfds, i; |
1521 | PyObject *elist = NULL, *etuple = NULL; |
1522 | struct epoll_event *evs = NULL; |
1523 | _PyTime_t timeout = -1, ms = -1, deadline = 0; |
1524 | |
1525 | if (self->epfd < 0) |
1526 | return pyepoll_err_closed(); |
1527 | |
1528 | if (timeout_obj != Py_None) { |
1529 | /* epoll_wait() has a resolution of 1 millisecond, round towards |
1530 | infinity to wait at least timeout seconds. */ |
1531 | if (_PyTime_FromSecondsObject(&timeout, timeout_obj, |
1532 | _PyTime_ROUND_TIMEOUT) < 0) { |
1533 | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
1534 | PyErr_SetString(PyExc_TypeError, |
1535 | "timeout must be an integer or None" ); |
1536 | } |
1537 | return NULL; |
1538 | } |
1539 | |
1540 | ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
1541 | if (ms < INT_MIN || ms > INT_MAX) { |
1542 | PyErr_SetString(PyExc_OverflowError, "timeout is too large" ); |
1543 | return NULL; |
1544 | } |
1545 | /* epoll_wait(2) treats all arbitrary negative numbers the same |
1546 | for the timeout argument, but -1 is the documented way to block |
1547 | indefinitely in the epoll_wait(2) documentation, so we set ms |
1548 | to -1 if the value of ms is a negative number. |
1549 | |
1550 | Note that we didn't use INFTIM here since it's non-standard and |
1551 | isn't available under Linux. */ |
1552 | if (ms < 0) { |
1553 | ms = -1; |
1554 | } |
1555 | |
1556 | if (timeout >= 0) { |
1557 | deadline = _PyTime_GetMonotonicClock() + timeout; |
1558 | } |
1559 | } |
1560 | |
1561 | if (maxevents == -1) { |
1562 | maxevents = FD_SETSIZE-1; |
1563 | } |
1564 | else if (maxevents < 1) { |
1565 | PyErr_Format(PyExc_ValueError, |
1566 | "maxevents must be greater than 0, got %d" , |
1567 | maxevents); |
1568 | return NULL; |
1569 | } |
1570 | |
1571 | evs = PyMem_New(struct epoll_event, maxevents); |
1572 | if (evs == NULL) { |
1573 | PyErr_NoMemory(); |
1574 | return NULL; |
1575 | } |
1576 | |
1577 | do { |
1578 | Py_BEGIN_ALLOW_THREADS |
1579 | errno = 0; |
1580 | nfds = epoll_wait(self->epfd, evs, maxevents, (int)ms); |
1581 | Py_END_ALLOW_THREADS |
1582 | |
1583 | if (errno != EINTR) |
1584 | break; |
1585 | |
1586 | /* poll() was interrupted by a signal */ |
1587 | if (PyErr_CheckSignals()) |
1588 | goto error; |
1589 | |
1590 | if (timeout >= 0) { |
1591 | timeout = deadline - _PyTime_GetMonotonicClock(); |
1592 | if (timeout < 0) { |
1593 | nfds = 0; |
1594 | break; |
1595 | } |
1596 | ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING); |
1597 | /* retry epoll_wait() with the recomputed timeout */ |
1598 | } |
1599 | } while(1); |
1600 | |
1601 | if (nfds < 0) { |
1602 | PyErr_SetFromErrno(PyExc_OSError); |
1603 | goto error; |
1604 | } |
1605 | |
1606 | elist = PyList_New(nfds); |
1607 | if (elist == NULL) { |
1608 | goto error; |
1609 | } |
1610 | |
1611 | for (i = 0; i < nfds; i++) { |
1612 | etuple = Py_BuildValue("iI" , evs[i].data.fd, evs[i].events); |
1613 | if (etuple == NULL) { |
1614 | Py_CLEAR(elist); |
1615 | goto error; |
1616 | } |
1617 | PyList_SET_ITEM(elist, i, etuple); |
1618 | } |
1619 | |
1620 | error: |
1621 | PyMem_Free(evs); |
1622 | return elist; |
1623 | } |
1624 | |
1625 | |
1626 | /*[clinic input] |
1627 | select.epoll.__enter__ |
1628 | |
1629 | [clinic start generated code]*/ |
1630 | |
1631 | static PyObject * |
1632 | select_epoll___enter___impl(pyEpoll_Object *self) |
1633 | /*[clinic end generated code: output=ab45d433504db2a0 input=3c22568587efeadb]*/ |
1634 | { |
1635 | if (self->epfd < 0) |
1636 | return pyepoll_err_closed(); |
1637 | |
1638 | Py_INCREF(self); |
1639 | return (PyObject *)self; |
1640 | } |
1641 | |
1642 | /*[clinic input] |
1643 | select.epoll.__exit__ |
1644 | |
1645 | exc_type: object = None |
1646 | exc_value: object = None |
1647 | exc_tb: object = None |
1648 | / |
1649 | |
1650 | [clinic start generated code]*/ |
1651 | |
1652 | static PyObject * |
1653 | select_epoll___exit___impl(pyEpoll_Object *self, PyObject *exc_type, |
1654 | PyObject *exc_value, PyObject *exc_tb) |
1655 | /*[clinic end generated code: output=c480f38ce361748e input=7ae81a5a4c1a98d8]*/ |
1656 | { |
1657 | _selectstate *state = _selectstate_by_type(Py_TYPE(self)); |
1658 | return PyObject_CallMethodObjArgs((PyObject *)self, state->close, NULL); |
1659 | } |
1660 | |
1661 | static PyGetSetDef pyepoll_getsetlist[] = { |
1662 | {"closed" , (getter)pyepoll_get_closed, NULL, |
1663 | "True if the epoll handler is closed" }, |
1664 | {0}, |
1665 | }; |
1666 | |
1667 | PyDoc_STRVAR(pyepoll_doc, |
1668 | "select.epoll(sizehint=-1, flags=0)\n\ |
1669 | \n\ |
1670 | Returns an epolling object\n\ |
1671 | \n\ |
1672 | sizehint must be a positive integer or -1 for the default size. The\n\ |
1673 | sizehint is used to optimize internal data structures. It doesn't limit\n\ |
1674 | the maximum number of monitored events." ); |
1675 | |
1676 | #endif /* HAVE_EPOLL */ |
1677 | |
1678 | #ifdef HAVE_KQUEUE |
1679 | /* ************************************************************************** |
1680 | * kqueue interface for BSD |
1681 | * |
1682 | * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes |
1683 | * All rights reserved. |
1684 | * |
1685 | * Redistribution and use in source and binary forms, with or without |
1686 | * modification, are permitted provided that the following conditions |
1687 | * are met: |
1688 | * 1. Redistributions of source code must retain the above copyright |
1689 | * notice, this list of conditions and the following disclaimer. |
1690 | * 2. Redistributions in binary form must reproduce the above copyright |
1691 | * notice, this list of conditions and the following disclaimer in the |
1692 | * documentation and/or other materials provided with the distribution. |
1693 | * |
1694 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
1695 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
1696 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
1697 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
1698 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
1699 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
1700 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
1701 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
1702 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
1703 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
1704 | * SUCH DAMAGE. |
1705 | */ |
1706 | |
1707 | #ifdef HAVE_SYS_EVENT_H |
1708 | #include <sys/event.h> |
1709 | #endif |
1710 | |
1711 | PyDoc_STRVAR(kqueue_event_doc, |
1712 | "kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)\n\ |
1713 | \n\ |
1714 | This object is the equivalent of the struct kevent for the C API.\n\ |
1715 | \n\ |
1716 | See the kqueue manpage for more detailed information about the meaning\n\ |
1717 | of the arguments.\n\ |
1718 | \n\ |
1719 | One minor note: while you might hope that udata could store a\n\ |
1720 | reference to a python object, it cannot, because it is impossible to\n\ |
1721 | keep a proper reference count of the object once it's passed into the\n\ |
1722 | kernel. Therefore, I have restricted it to only storing an integer. I\n\ |
1723 | recommend ignoring it and simply using the 'ident' field to key off\n\ |
1724 | of. You could also set up a dictionary on the python side to store a\n\ |
1725 | udata->object mapping." ); |
1726 | |
1727 | typedef struct { |
1728 | PyObject_HEAD |
1729 | struct kevent e; |
1730 | } kqueue_event_Object; |
1731 | |
1732 | #define kqueue_event_Check(op, state) (PyObject_TypeCheck((op), state->kqueue_event_Type)) |
1733 | |
1734 | typedef struct { |
1735 | PyObject_HEAD |
1736 | SOCKET kqfd; /* kqueue control fd */ |
1737 | } kqueue_queue_Object; |
1738 | |
1739 | #if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P) |
1740 | # error uintptr_t does not match void *! |
1741 | #elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG) |
1742 | # define T_UINTPTRT T_ULONGLONG |
1743 | # define T_INTPTRT T_LONGLONG |
1744 | # define UINTPTRT_FMT_UNIT "K" |
1745 | # define INTPTRT_FMT_UNIT "L" |
1746 | #elif (SIZEOF_UINTPTR_T == SIZEOF_LONG) |
1747 | # define T_UINTPTRT T_ULONG |
1748 | # define T_INTPTRT T_LONG |
1749 | # define UINTPTRT_FMT_UNIT "k" |
1750 | # define INTPTRT_FMT_UNIT "l" |
1751 | #elif (SIZEOF_UINTPTR_T == SIZEOF_INT) |
1752 | # define T_UINTPTRT T_UINT |
1753 | # define T_INTPTRT T_INT |
1754 | # define UINTPTRT_FMT_UNIT "I" |
1755 | # define INTPTRT_FMT_UNIT "i" |
1756 | #else |
1757 | # error uintptr_t does not match int, long, or long long! |
1758 | #endif |
1759 | |
1760 | #if SIZEOF_LONG_LONG == 8 |
1761 | # define T_INT64 T_LONGLONG |
1762 | # define INT64_FMT_UNIT "L" |
1763 | #elif SIZEOF_LONG == 8 |
1764 | # define T_INT64 T_LONG |
1765 | # define INT64_FMT_UNIT "l" |
1766 | #elif SIZEOF_INT == 8 |
1767 | # define T_INT64 T_INT |
1768 | # define INT64_FMT_UNIT "i" |
1769 | #else |
1770 | # define INT64_FMT_UNIT "_" |
1771 | #endif |
1772 | |
1773 | #if SIZEOF_LONG_LONG == 4 |
1774 | # define T_UINT32 T_ULONGLONG |
1775 | # define UINT32_FMT_UNIT "K" |
1776 | #elif SIZEOF_LONG == 4 |
1777 | # define T_UINT32 T_ULONG |
1778 | # define UINT32_FMT_UNIT "k" |
1779 | #elif SIZEOF_INT == 4 |
1780 | # define T_UINT32 T_UINT |
1781 | # define UINT32_FMT_UNIT "I" |
1782 | #else |
1783 | # define UINT32_FMT_UNIT "_" |
1784 | #endif |
1785 | |
1786 | /* |
1787 | * kevent is not standard and its members vary across BSDs. |
1788 | */ |
1789 | #ifdef __NetBSD__ |
1790 | # define FILTER_TYPE T_UINT32 |
1791 | # define FILTER_FMT_UNIT UINT32_FMT_UNIT |
1792 | # define FLAGS_TYPE T_UINT32 |
1793 | # define FLAGS_FMT_UNIT UINT32_FMT_UNIT |
1794 | # define FFLAGS_TYPE T_UINT32 |
1795 | # define FFLAGS_FMT_UNIT UINT32_FMT_UNIT |
1796 | #else |
1797 | # define FILTER_TYPE T_SHORT |
1798 | # define FILTER_FMT_UNIT "h" |
1799 | # define FLAGS_TYPE T_USHORT |
1800 | # define FLAGS_FMT_UNIT "H" |
1801 | # define FFLAGS_TYPE T_UINT |
1802 | # define FFLAGS_FMT_UNIT "I" |
1803 | #endif |
1804 | |
1805 | #if defined(__NetBSD__) || defined(__OpenBSD__) |
1806 | # define DATA_TYPE T_INT64 |
1807 | # define DATA_FMT_UNIT INT64_FMT_UNIT |
1808 | #else |
1809 | # define DATA_TYPE T_INTPTRT |
1810 | # define DATA_FMT_UNIT INTPTRT_FMT_UNIT |
1811 | #endif |
1812 | |
1813 | /* Unfortunately, we can't store python objects in udata, because |
1814 | * kevents in the kernel can be removed without warning, which would |
1815 | * forever lose the refcount on the object stored with it. |
1816 | */ |
1817 | |
1818 | #define KQ_OFF(x) offsetof(kqueue_event_Object, x) |
1819 | static struct PyMemberDef kqueue_event_members[] = { |
1820 | {"ident" , T_UINTPTRT, KQ_OFF(e.ident)}, |
1821 | {"filter" , FILTER_TYPE, KQ_OFF(e.filter)}, |
1822 | {"flags" , FLAGS_TYPE, KQ_OFF(e.flags)}, |
1823 | {"fflags" , T_UINT, KQ_OFF(e.fflags)}, |
1824 | {"data" , DATA_TYPE, KQ_OFF(e.data)}, |
1825 | {"udata" , T_UINTPTRT, KQ_OFF(e.udata)}, |
1826 | {NULL} /* Sentinel */ |
1827 | }; |
1828 | #undef KQ_OFF |
1829 | |
1830 | static PyObject * |
1831 | |
1832 | kqueue_event_repr(kqueue_event_Object *s) |
1833 | { |
1834 | char buf[1024]; |
1835 | PyOS_snprintf( |
1836 | buf, sizeof(buf), |
1837 | "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x " |
1838 | "data=0x%llx udata=%p>" , |
1839 | (size_t)(s->e.ident), (int)s->e.filter, (unsigned int)s->e.flags, |
1840 | (unsigned int)s->e.fflags, (long long)(s->e.data), (void *)s->e.udata); |
1841 | return PyUnicode_FromString(buf); |
1842 | } |
1843 | |
1844 | static int |
1845 | kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds) |
1846 | { |
1847 | PyObject *pfd; |
1848 | static char *kwlist[] = {"ident" , "filter" , "flags" , "fflags" , |
1849 | "data" , "udata" , NULL}; |
1850 | static const char fmt[] = "O|" |
1851 | FILTER_FMT_UNIT FLAGS_FMT_UNIT FFLAGS_FMT_UNIT DATA_FMT_UNIT |
1852 | UINTPTRT_FMT_UNIT ":kevent" ; |
1853 | |
1854 | EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */ |
1855 | |
1856 | if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, |
1857 | &pfd, &(self->e.filter), &(self->e.flags), |
1858 | &(self->e.fflags), &(self->e.data), &(self->e.udata))) { |
1859 | return -1; |
1860 | } |
1861 | |
1862 | if (PyLong_Check(pfd)) { |
1863 | self->e.ident = PyLong_AsSize_t(pfd); |
1864 | } |
1865 | else { |
1866 | self->e.ident = PyObject_AsFileDescriptor(pfd); |
1867 | } |
1868 | if (PyErr_Occurred()) { |
1869 | return -1; |
1870 | } |
1871 | return 0; |
1872 | } |
1873 | |
1874 | static PyObject * |
1875 | kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o, |
1876 | int op) |
1877 | { |
1878 | int result; |
1879 | _selectstate *state = _selectstate_by_type(Py_TYPE(s)); |
1880 | |
1881 | if (!kqueue_event_Check(o, state)) { |
1882 | Py_RETURN_NOTIMPLEMENTED; |
1883 | } |
1884 | |
1885 | #define CMP(a, b) ((a) != (b)) ? ((a) < (b) ? -1 : 1) |
1886 | result = CMP(s->e.ident, o->e.ident) |
1887 | : CMP(s->e.filter, o->e.filter) |
1888 | : CMP(s->e.flags, o->e.flags) |
1889 | : CMP(s->e.fflags, o->e.fflags) |
1890 | : CMP(s->e.data, o->e.data) |
1891 | : CMP((intptr_t)s->e.udata, (intptr_t)o->e.udata) |
1892 | : 0; |
1893 | #undef CMP |
1894 | |
1895 | Py_RETURN_RICHCOMPARE(result, 0, op); |
1896 | } |
1897 | |
1898 | static PyType_Slot kqueue_event_Type_slots[] = { |
1899 | {Py_tp_doc, (void*)kqueue_event_doc}, |
1900 | {Py_tp_init, kqueue_event_init}, |
1901 | {Py_tp_members, kqueue_event_members}, |
1902 | {Py_tp_new, PyType_GenericNew}, |
1903 | {Py_tp_repr, kqueue_event_repr}, |
1904 | {Py_tp_richcompare, kqueue_event_richcompare}, |
1905 | {0, 0}, |
1906 | }; |
1907 | |
1908 | static PyType_Spec kqueue_event_Type_spec = { |
1909 | "select.kevent" , |
1910 | sizeof(kqueue_event_Object), |
1911 | 0, |
1912 | Py_TPFLAGS_DEFAULT, |
1913 | kqueue_event_Type_slots |
1914 | }; |
1915 | |
1916 | static PyObject * |
1917 | kqueue_queue_err_closed(void) |
1918 | { |
1919 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue object" ); |
1920 | return NULL; |
1921 | } |
1922 | |
1923 | static int |
1924 | kqueue_queue_internal_close(kqueue_queue_Object *self) |
1925 | { |
1926 | int save_errno = 0; |
1927 | if (self->kqfd >= 0) { |
1928 | int kqfd = self->kqfd; |
1929 | self->kqfd = -1; |
1930 | Py_BEGIN_ALLOW_THREADS |
1931 | if (close(kqfd) < 0) |
1932 | save_errno = errno; |
1933 | Py_END_ALLOW_THREADS |
1934 | } |
1935 | return save_errno; |
1936 | } |
1937 | |
1938 | static PyObject * |
1939 | newKqueue_Object(PyTypeObject *type, SOCKET fd) |
1940 | { |
1941 | kqueue_queue_Object *self; |
1942 | assert(type != NULL); |
1943 | allocfunc queue_alloc = PyType_GetSlot(type, Py_tp_alloc); |
1944 | assert(queue_alloc != NULL); |
1945 | self = (kqueue_queue_Object *) queue_alloc(type, 0); |
1946 | if (self == NULL) { |
1947 | return NULL; |
1948 | } |
1949 | |
1950 | if (fd == -1) { |
1951 | Py_BEGIN_ALLOW_THREADS |
1952 | self->kqfd = kqueue(); |
1953 | Py_END_ALLOW_THREADS |
1954 | } |
1955 | else { |
1956 | self->kqfd = fd; |
1957 | } |
1958 | if (self->kqfd < 0) { |
1959 | Py_DECREF(self); |
1960 | PyErr_SetFromErrno(PyExc_OSError); |
1961 | return NULL; |
1962 | } |
1963 | |
1964 | if (fd == -1) { |
1965 | if (_Py_set_inheritable(self->kqfd, 0, NULL) < 0) { |
1966 | Py_DECREF(self); |
1967 | return NULL; |
1968 | } |
1969 | } |
1970 | return (PyObject *)self; |
1971 | } |
1972 | |
1973 | /*[clinic input] |
1974 | @classmethod |
1975 | select.kqueue.__new__ |
1976 | |
1977 | Kqueue syscall wrapper. |
1978 | |
1979 | For example, to start watching a socket for input: |
1980 | >>> kq = kqueue() |
1981 | >>> sock = socket() |
1982 | >>> sock.connect((host, port)) |
1983 | >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0) |
1984 | |
1985 | To wait one second for it to become writeable: |
1986 | >>> kq.control(None, 1, 1000) |
1987 | |
1988 | To stop listening: |
1989 | >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0) |
1990 | [clinic start generated code]*/ |
1991 | |
1992 | static PyObject * |
1993 | select_kqueue_impl(PyTypeObject *type) |
1994 | /*[clinic end generated code: output=e0ff89f154d56236 input=cf625e49218366e8]*/ |
1995 | { |
1996 | return newKqueue_Object(type, -1); |
1997 | } |
1998 | |
1999 | static void |
2000 | kqueue_queue_dealloc(kqueue_queue_Object *self) |
2001 | { |
2002 | PyTypeObject* type = Py_TYPE(self); |
2003 | kqueue_queue_internal_close(self); |
2004 | freefunc kqueue_free = PyType_GetSlot(type, Py_tp_free); |
2005 | kqueue_free((PyObject *)self); |
2006 | Py_DECREF((PyObject *)type); |
2007 | } |
2008 | |
2009 | /*[clinic input] |
2010 | select.kqueue.close |
2011 | |
2012 | Close the kqueue control file descriptor. |
2013 | |
2014 | Further operations on the kqueue object will raise an exception. |
2015 | [clinic start generated code]*/ |
2016 | |
2017 | static PyObject * |
2018 | select_kqueue_close_impl(kqueue_queue_Object *self) |
2019 | /*[clinic end generated code: output=d1c7df0b407a4bc1 input=0b12d95430e0634c]*/ |
2020 | { |
2021 | errno = kqueue_queue_internal_close(self); |
2022 | if (errno < 0) { |
2023 | PyErr_SetFromErrno(PyExc_OSError); |
2024 | return NULL; |
2025 | } |
2026 | Py_RETURN_NONE; |
2027 | } |
2028 | |
2029 | static PyObject* |
2030 | kqueue_queue_get_closed(kqueue_queue_Object *self, void *Py_UNUSED(ignored)) |
2031 | { |
2032 | if (self->kqfd < 0) |
2033 | Py_RETURN_TRUE; |
2034 | else |
2035 | Py_RETURN_FALSE; |
2036 | } |
2037 | |
2038 | /*[clinic input] |
2039 | select.kqueue.fileno |
2040 | |
2041 | Return the kqueue control file descriptor. |
2042 | [clinic start generated code]*/ |
2043 | |
2044 | static PyObject * |
2045 | select_kqueue_fileno_impl(kqueue_queue_Object *self) |
2046 | /*[clinic end generated code: output=716f46112a4f6e5c input=41911c539ca2b0ca]*/ |
2047 | { |
2048 | if (self->kqfd < 0) |
2049 | return kqueue_queue_err_closed(); |
2050 | return PyLong_FromLong(self->kqfd); |
2051 | } |
2052 | |
2053 | /*[clinic input] |
2054 | @classmethod |
2055 | select.kqueue.fromfd |
2056 | |
2057 | fd: int |
2058 | / |
2059 | |
2060 | Create a kqueue object from a given control fd. |
2061 | [clinic start generated code]*/ |
2062 | |
2063 | static PyObject * |
2064 | select_kqueue_fromfd_impl(PyTypeObject *type, int fd) |
2065 | /*[clinic end generated code: output=d02c3c7dc538a653 input=f6172a48ca4ecdd0]*/ |
2066 | { |
2067 | SOCKET s_fd = (SOCKET)fd; |
2068 | |
2069 | return newKqueue_Object(type, s_fd); |
2070 | } |
2071 | |
2072 | /*[clinic input] |
2073 | select.kqueue.control |
2074 | |
2075 | changelist: object |
2076 | Must be an iterable of kevent objects describing the changes to be made |
2077 | to the kernel's watch list or None. |
2078 | maxevents: int |
2079 | The maximum number of events that the kernel will return. |
2080 | timeout as otimeout: object = None |
2081 | The maximum time to wait in seconds, or else None to wait forever. |
2082 | This accepts floats for smaller timeouts, too. |
2083 | / |
2084 | |
2085 | Calls the kernel kevent function. |
2086 | [clinic start generated code]*/ |
2087 | |
2088 | static PyObject * |
2089 | select_kqueue_control_impl(kqueue_queue_Object *self, PyObject *changelist, |
2090 | int maxevents, PyObject *otimeout) |
2091 | /*[clinic end generated code: output=81324ff5130db7ae input=59c4e30811209c47]*/ |
2092 | { |
2093 | int gotevents = 0; |
2094 | int nchanges = 0; |
2095 | int i = 0; |
2096 | PyObject *seq = NULL, *ei = NULL; |
2097 | PyObject *result = NULL; |
2098 | struct kevent *evl = NULL; |
2099 | struct kevent *chl = NULL; |
2100 | struct timespec timeoutspec; |
2101 | struct timespec *ptimeoutspec; |
2102 | _PyTime_t timeout, deadline = 0; |
2103 | _selectstate *state = _selectstate_by_type(Py_TYPE(self)); |
2104 | |
2105 | if (self->kqfd < 0) |
2106 | return kqueue_queue_err_closed(); |
2107 | |
2108 | if (maxevents < 0) { |
2109 | PyErr_Format(PyExc_ValueError, |
2110 | "Length of eventlist must be 0 or positive, got %d" , |
2111 | maxevents); |
2112 | return NULL; |
2113 | } |
2114 | |
2115 | if (otimeout == Py_None) { |
2116 | ptimeoutspec = NULL; |
2117 | } |
2118 | else { |
2119 | if (_PyTime_FromSecondsObject(&timeout, |
2120 | otimeout, _PyTime_ROUND_TIMEOUT) < 0) { |
2121 | PyErr_Format(PyExc_TypeError, |
2122 | "timeout argument must be a number " |
2123 | "or None, got %.200s" , |
2124 | _PyType_Name(Py_TYPE(otimeout))); |
2125 | return NULL; |
2126 | } |
2127 | |
2128 | if (_PyTime_AsTimespec(timeout, &timeoutspec) == -1) |
2129 | return NULL; |
2130 | |
2131 | if (timeoutspec.tv_sec < 0) { |
2132 | PyErr_SetString(PyExc_ValueError, |
2133 | "timeout must be positive or None" ); |
2134 | return NULL; |
2135 | } |
2136 | ptimeoutspec = &timeoutspec; |
2137 | } |
2138 | |
2139 | if (changelist != Py_None) { |
2140 | seq = PySequence_Fast(changelist, "changelist is not iterable" ); |
2141 | if (seq == NULL) { |
2142 | return NULL; |
2143 | } |
2144 | if (PySequence_Fast_GET_SIZE(seq) > INT_MAX) { |
2145 | PyErr_SetString(PyExc_OverflowError, |
2146 | "changelist is too long" ); |
2147 | goto error; |
2148 | } |
2149 | nchanges = (int)PySequence_Fast_GET_SIZE(seq); |
2150 | |
2151 | chl = PyMem_New(struct kevent, nchanges); |
2152 | if (chl == NULL) { |
2153 | PyErr_NoMemory(); |
2154 | goto error; |
2155 | } |
2156 | _selectstate *state = _selectstate_by_type(Py_TYPE(self)); |
2157 | for (i = 0; i < nchanges; ++i) { |
2158 | ei = PySequence_Fast_GET_ITEM(seq, i); |
2159 | if (!kqueue_event_Check(ei, state)) { |
2160 | PyErr_SetString(PyExc_TypeError, |
2161 | "changelist must be an iterable of " |
2162 | "select.kevent objects" ); |
2163 | goto error; |
2164 | } |
2165 | chl[i] = ((kqueue_event_Object *)ei)->e; |
2166 | } |
2167 | Py_CLEAR(seq); |
2168 | } |
2169 | |
2170 | /* event list */ |
2171 | if (maxevents) { |
2172 | evl = PyMem_New(struct kevent, maxevents); |
2173 | if (evl == NULL) { |
2174 | PyErr_NoMemory(); |
2175 | goto error; |
2176 | } |
2177 | } |
2178 | |
2179 | if (ptimeoutspec) |
2180 | deadline = _PyTime_GetMonotonicClock() + timeout; |
2181 | |
2182 | do { |
2183 | Py_BEGIN_ALLOW_THREADS |
2184 | errno = 0; |
2185 | gotevents = kevent(self->kqfd, chl, nchanges, |
2186 | evl, maxevents, ptimeoutspec); |
2187 | Py_END_ALLOW_THREADS |
2188 | |
2189 | if (errno != EINTR) |
2190 | break; |
2191 | |
2192 | /* kevent() was interrupted by a signal */ |
2193 | if (PyErr_CheckSignals()) |
2194 | goto error; |
2195 | |
2196 | if (ptimeoutspec) { |
2197 | timeout = deadline - _PyTime_GetMonotonicClock(); |
2198 | if (timeout < 0) { |
2199 | gotevents = 0; |
2200 | break; |
2201 | } |
2202 | if (_PyTime_AsTimespec(timeout, &timeoutspec) == -1) |
2203 | goto error; |
2204 | /* retry kevent() with the recomputed timeout */ |
2205 | } |
2206 | } while (1); |
2207 | |
2208 | if (gotevents == -1) { |
2209 | PyErr_SetFromErrno(PyExc_OSError); |
2210 | goto error; |
2211 | } |
2212 | |
2213 | result = PyList_New(gotevents); |
2214 | if (result == NULL) { |
2215 | goto error; |
2216 | } |
2217 | |
2218 | for (i = 0; i < gotevents; i++) { |
2219 | kqueue_event_Object *ch; |
2220 | |
2221 | ch = PyObject_New(kqueue_event_Object, state->kqueue_event_Type); |
2222 | if (ch == NULL) { |
2223 | goto error; |
2224 | } |
2225 | ch->e = evl[i]; |
2226 | PyList_SET_ITEM(result, i, (PyObject *)ch); |
2227 | } |
2228 | PyMem_Free(chl); |
2229 | PyMem_Free(evl); |
2230 | return result; |
2231 | |
2232 | error: |
2233 | PyMem_Free(chl); |
2234 | PyMem_Free(evl); |
2235 | Py_XDECREF(result); |
2236 | Py_XDECREF(seq); |
2237 | return NULL; |
2238 | } |
2239 | |
2240 | static PyGetSetDef kqueue_queue_getsetlist[] = { |
2241 | {"closed" , (getter)kqueue_queue_get_closed, NULL, |
2242 | "True if the kqueue handler is closed" }, |
2243 | {0}, |
2244 | }; |
2245 | |
2246 | #endif /* HAVE_KQUEUE */ |
2247 | |
2248 | |
2249 | /* ************************************************************************ */ |
2250 | |
2251 | #include "clinic/selectmodule.c.h" |
2252 | |
2253 | #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL) |
2254 | |
2255 | static PyMethodDef poll_methods[] = { |
2256 | SELECT_POLL_REGISTER_METHODDEF |
2257 | SELECT_POLL_MODIFY_METHODDEF |
2258 | SELECT_POLL_UNREGISTER_METHODDEF |
2259 | SELECT_POLL_POLL_METHODDEF |
2260 | {NULL, NULL} /* sentinel */ |
2261 | }; |
2262 | |
2263 | |
2264 | static PyType_Slot poll_Type_slots[] = { |
2265 | {Py_tp_dealloc, poll_dealloc}, |
2266 | {Py_tp_methods, poll_methods}, |
2267 | {0, 0}, |
2268 | }; |
2269 | |
2270 | static PyType_Spec poll_Type_spec = { |
2271 | .name = "select.poll" , |
2272 | .basicsize = sizeof(pollObject), |
2273 | .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, |
2274 | .slots = poll_Type_slots, |
2275 | }; |
2276 | |
2277 | #ifdef HAVE_SYS_DEVPOLL_H |
2278 | |
2279 | static PyMethodDef devpoll_methods[] = { |
2280 | SELECT_DEVPOLL_REGISTER_METHODDEF |
2281 | SELECT_DEVPOLL_MODIFY_METHODDEF |
2282 | SELECT_DEVPOLL_UNREGISTER_METHODDEF |
2283 | SELECT_DEVPOLL_POLL_METHODDEF |
2284 | SELECT_DEVPOLL_CLOSE_METHODDEF |
2285 | SELECT_DEVPOLL_FILENO_METHODDEF |
2286 | {NULL, NULL} /* sentinel */ |
2287 | }; |
2288 | |
2289 | #endif /* HAVE_SYS_DEVPOLL_H */ |
2290 | |
2291 | #endif /* HAVE_POLL */ |
2292 | |
2293 | #ifdef HAVE_EPOLL |
2294 | |
2295 | static PyMethodDef pyepoll_methods[] = { |
2296 | SELECT_EPOLL_FROMFD_METHODDEF |
2297 | SELECT_EPOLL_CLOSE_METHODDEF |
2298 | SELECT_EPOLL_FILENO_METHODDEF |
2299 | SELECT_EPOLL_MODIFY_METHODDEF |
2300 | SELECT_EPOLL_REGISTER_METHODDEF |
2301 | SELECT_EPOLL_UNREGISTER_METHODDEF |
2302 | SELECT_EPOLL_POLL_METHODDEF |
2303 | SELECT_EPOLL___ENTER___METHODDEF |
2304 | SELECT_EPOLL___EXIT___METHODDEF |
2305 | {NULL, NULL}, |
2306 | }; |
2307 | |
2308 | static PyType_Slot pyEpoll_Type_slots[] = { |
2309 | {Py_tp_dealloc, pyepoll_dealloc}, |
2310 | {Py_tp_doc, (void*)pyepoll_doc}, |
2311 | {Py_tp_getattro, PyObject_GenericGetAttr}, |
2312 | {Py_tp_getset, pyepoll_getsetlist}, |
2313 | {Py_tp_methods, pyepoll_methods}, |
2314 | {Py_tp_new, select_epoll}, |
2315 | {0, 0}, |
2316 | }; |
2317 | |
2318 | static PyType_Spec pyEpoll_Type_spec = { |
2319 | "select.epoll" , |
2320 | sizeof(pyEpoll_Object), |
2321 | 0, |
2322 | Py_TPFLAGS_DEFAULT, |
2323 | pyEpoll_Type_slots |
2324 | }; |
2325 | |
2326 | #endif /* HAVE_EPOLL */ |
2327 | |
2328 | #ifdef HAVE_KQUEUE |
2329 | |
2330 | static PyMethodDef kqueue_queue_methods[] = { |
2331 | SELECT_KQUEUE_FROMFD_METHODDEF |
2332 | SELECT_KQUEUE_CLOSE_METHODDEF |
2333 | SELECT_KQUEUE_FILENO_METHODDEF |
2334 | SELECT_KQUEUE_CONTROL_METHODDEF |
2335 | {NULL, NULL}, |
2336 | }; |
2337 | |
2338 | static PyType_Slot kqueue_queue_Type_slots[] = { |
2339 | {Py_tp_dealloc, kqueue_queue_dealloc}, |
2340 | {Py_tp_doc, (void*)select_kqueue__doc__}, |
2341 | {Py_tp_getset, kqueue_queue_getsetlist}, |
2342 | {Py_tp_methods, kqueue_queue_methods}, |
2343 | {Py_tp_new, select_kqueue}, |
2344 | {0, 0}, |
2345 | }; |
2346 | |
2347 | static PyType_Spec kqueue_queue_Type_spec = { |
2348 | "select.kqueue" , |
2349 | sizeof(kqueue_queue_Object), |
2350 | 0, |
2351 | Py_TPFLAGS_DEFAULT, |
2352 | kqueue_queue_Type_slots |
2353 | }; |
2354 | |
2355 | #endif /* HAVE_KQUEUE */ |
2356 | |
2357 | |
2358 | |
2359 | |
2360 | |
2361 | /* ************************************************************************ */ |
2362 | |
2363 | |
2364 | static PyMethodDef select_methods[] = { |
2365 | SELECT_SELECT_METHODDEF |
2366 | SELECT_POLL_METHODDEF |
2367 | SELECT_DEVPOLL_METHODDEF |
2368 | {0, 0}, /* sentinel */ |
2369 | }; |
2370 | |
2371 | PyDoc_STRVAR(module_doc, |
2372 | "This module supports asynchronous I/O on multiple file descriptors.\n\ |
2373 | \n\ |
2374 | *** IMPORTANT NOTICE ***\n\ |
2375 | On Windows, only sockets are supported; on Unix, all file descriptors." ); |
2376 | |
2377 | |
2378 | |
2379 | static int |
2380 | _select_traverse(PyObject *module, visitproc visit, void *arg) |
2381 | { |
2382 | _selectstate *state = get_select_state(module); |
2383 | |
2384 | Py_VISIT(state->close); |
2385 | Py_VISIT(state->poll_Type); |
2386 | Py_VISIT(state->devpoll_Type); |
2387 | Py_VISIT(state->pyEpoll_Type); |
2388 | Py_VISIT(state->kqueue_event_Type); |
2389 | Py_VISIT(state->kqueue_queue_Type); |
2390 | return 0; |
2391 | } |
2392 | |
2393 | static int |
2394 | _select_clear(PyObject *module) |
2395 | { |
2396 | _selectstate *state = get_select_state(module); |
2397 | |
2398 | Py_CLEAR(state->close); |
2399 | Py_CLEAR(state->poll_Type); |
2400 | Py_CLEAR(state->devpoll_Type); |
2401 | Py_CLEAR(state->pyEpoll_Type); |
2402 | Py_CLEAR(state->kqueue_event_Type); |
2403 | Py_CLEAR(state->kqueue_queue_Type); |
2404 | return 0; |
2405 | } |
2406 | |
2407 | static void |
2408 | _select_free(void *module) |
2409 | { |
2410 | _select_clear((PyObject *)module); |
2411 | } |
2412 | |
2413 | int |
2414 | _select_exec(PyObject *m) |
2415 | { |
2416 | _selectstate *state = get_select_state(m); |
2417 | |
2418 | state->close = PyUnicode_InternFromString("close" ); |
2419 | if (state->close == NULL) { |
2420 | return -1; |
2421 | } |
2422 | if (PyModule_AddObjectRef(m, "error" , PyExc_OSError) < 0) { |
2423 | return -1; |
2424 | } |
2425 | |
2426 | #ifdef PIPE_BUF |
2427 | #ifdef HAVE_BROKEN_PIPE_BUF |
2428 | #undef PIPE_BUF |
2429 | #define PIPE_BUF 512 |
2430 | #endif |
2431 | PyModule_AddIntMacro(m, PIPE_BUF); |
2432 | #endif |
2433 | |
2434 | #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL) |
2435 | #ifdef __APPLE__ |
2436 | if (select_have_broken_poll()) { |
2437 | if (PyObject_DelAttrString(m, "poll" ) == -1) { |
2438 | PyErr_Clear(); |
2439 | } |
2440 | } else { |
2441 | #else |
2442 | { |
2443 | #endif |
2444 | state->poll_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
2445 | m, &poll_Type_spec, NULL); |
2446 | if (state->poll_Type == NULL) { |
2447 | return -1; |
2448 | } |
2449 | |
2450 | PyModule_AddIntMacro(m, POLLIN); |
2451 | PyModule_AddIntMacro(m, POLLPRI); |
2452 | PyModule_AddIntMacro(m, POLLOUT); |
2453 | PyModule_AddIntMacro(m, POLLERR); |
2454 | PyModule_AddIntMacro(m, POLLHUP); |
2455 | PyModule_AddIntMacro(m, POLLNVAL); |
2456 | |
2457 | #ifdef POLLRDNORM |
2458 | PyModule_AddIntMacro(m, POLLRDNORM); |
2459 | #endif |
2460 | #ifdef POLLRDBAND |
2461 | PyModule_AddIntMacro(m, POLLRDBAND); |
2462 | #endif |
2463 | #ifdef POLLWRNORM |
2464 | PyModule_AddIntMacro(m, POLLWRNORM); |
2465 | #endif |
2466 | #ifdef POLLWRBAND |
2467 | PyModule_AddIntMacro(m, POLLWRBAND); |
2468 | #endif |
2469 | #ifdef POLLMSG |
2470 | PyModule_AddIntMacro(m, POLLMSG); |
2471 | #endif |
2472 | #ifdef POLLRDHUP |
2473 | /* Kernel 2.6.17+ */ |
2474 | PyModule_AddIntMacro(m, POLLRDHUP); |
2475 | #endif |
2476 | } |
2477 | #endif /* HAVE_POLL */ |
2478 | |
2479 | #ifdef HAVE_SYS_DEVPOLL_H |
2480 | state->devpoll_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
2481 | m, &devpoll_Type_spec, NULL); |
2482 | if (state->devpoll_Type == NULL) { |
2483 | return -1; |
2484 | } |
2485 | #endif |
2486 | |
2487 | #ifdef HAVE_EPOLL |
2488 | state->pyEpoll_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
2489 | m, &pyEpoll_Type_spec, NULL); |
2490 | if (state->pyEpoll_Type == NULL) { |
2491 | return -1; |
2492 | } |
2493 | if (PyModule_AddType(m, state->pyEpoll_Type) < 0) { |
2494 | return -1; |
2495 | } |
2496 | |
2497 | PyModule_AddIntMacro(m, EPOLLIN); |
2498 | PyModule_AddIntMacro(m, EPOLLOUT); |
2499 | PyModule_AddIntMacro(m, EPOLLPRI); |
2500 | PyModule_AddIntMacro(m, EPOLLERR); |
2501 | PyModule_AddIntMacro(m, EPOLLHUP); |
2502 | #ifdef EPOLLRDHUP |
2503 | /* Kernel 2.6.17 */ |
2504 | PyModule_AddIntMacro(m, EPOLLRDHUP); |
2505 | #endif |
2506 | PyModule_AddIntMacro(m, EPOLLET); |
2507 | #ifdef EPOLLONESHOT |
2508 | /* Kernel 2.6.2+ */ |
2509 | PyModule_AddIntMacro(m, EPOLLONESHOT); |
2510 | #endif |
2511 | #ifdef EPOLLEXCLUSIVE |
2512 | PyModule_AddIntMacro(m, EPOLLEXCLUSIVE); |
2513 | #endif |
2514 | |
2515 | #ifdef EPOLLRDNORM |
2516 | PyModule_AddIntMacro(m, EPOLLRDNORM); |
2517 | #endif |
2518 | #ifdef EPOLLRDBAND |
2519 | PyModule_AddIntMacro(m, EPOLLRDBAND); |
2520 | #endif |
2521 | #ifdef EPOLLWRNORM |
2522 | PyModule_AddIntMacro(m, EPOLLWRNORM); |
2523 | #endif |
2524 | #ifdef EPOLLWRBAND |
2525 | PyModule_AddIntMacro(m, EPOLLWRBAND); |
2526 | #endif |
2527 | #ifdef EPOLLMSG |
2528 | PyModule_AddIntMacro(m, EPOLLMSG); |
2529 | #endif |
2530 | |
2531 | #ifdef EPOLL_CLOEXEC |
2532 | PyModule_AddIntMacro(m, EPOLL_CLOEXEC); |
2533 | #endif |
2534 | #endif /* HAVE_EPOLL */ |
2535 | |
2536 | #ifdef HAVE_KQUEUE |
2537 | state->kqueue_event_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
2538 | m, &kqueue_event_Type_spec, NULL); |
2539 | if (state->kqueue_event_Type == NULL) { |
2540 | return -1; |
2541 | } |
2542 | if (PyModule_AddType(m, state->kqueue_event_Type) < 0) { |
2543 | return -1; |
2544 | } |
2545 | |
2546 | state->kqueue_queue_Type = (PyTypeObject *)PyType_FromModuleAndSpec( |
2547 | m, &kqueue_queue_Type_spec, NULL); |
2548 | if (state->kqueue_queue_Type == NULL) { |
2549 | return -1; |
2550 | } |
2551 | if (PyModule_AddType(m, state->kqueue_queue_Type) < 0) { |
2552 | return -1; |
2553 | } |
2554 | |
2555 | /* event filters */ |
2556 | PyModule_AddIntConstant(m, "KQ_FILTER_READ" , EVFILT_READ); |
2557 | PyModule_AddIntConstant(m, "KQ_FILTER_WRITE" , EVFILT_WRITE); |
2558 | #ifdef EVFILT_AIO |
2559 | PyModule_AddIntConstant(m, "KQ_FILTER_AIO" , EVFILT_AIO); |
2560 | #endif |
2561 | #ifdef EVFILT_VNODE |
2562 | PyModule_AddIntConstant(m, "KQ_FILTER_VNODE" , EVFILT_VNODE); |
2563 | #endif |
2564 | #ifdef EVFILT_PROC |
2565 | PyModule_AddIntConstant(m, "KQ_FILTER_PROC" , EVFILT_PROC); |
2566 | #endif |
2567 | #ifdef EVFILT_NETDEV |
2568 | PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV" , EVFILT_NETDEV); |
2569 | #endif |
2570 | #ifdef EVFILT_SIGNAL |
2571 | PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL" , EVFILT_SIGNAL); |
2572 | #endif |
2573 | PyModule_AddIntConstant(m, "KQ_FILTER_TIMER" , EVFILT_TIMER); |
2574 | |
2575 | /* event flags */ |
2576 | PyModule_AddIntConstant(m, "KQ_EV_ADD" , EV_ADD); |
2577 | PyModule_AddIntConstant(m, "KQ_EV_DELETE" , EV_DELETE); |
2578 | PyModule_AddIntConstant(m, "KQ_EV_ENABLE" , EV_ENABLE); |
2579 | PyModule_AddIntConstant(m, "KQ_EV_DISABLE" , EV_DISABLE); |
2580 | PyModule_AddIntConstant(m, "KQ_EV_ONESHOT" , EV_ONESHOT); |
2581 | PyModule_AddIntConstant(m, "KQ_EV_CLEAR" , EV_CLEAR); |
2582 | |
2583 | #ifdef EV_SYSFLAGS |
2584 | PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS" , EV_SYSFLAGS); |
2585 | #endif |
2586 | #ifdef EV_FLAG1 |
2587 | PyModule_AddIntConstant(m, "KQ_EV_FLAG1" , EV_FLAG1); |
2588 | #endif |
2589 | |
2590 | PyModule_AddIntConstant(m, "KQ_EV_EOF" , EV_EOF); |
2591 | PyModule_AddIntConstant(m, "KQ_EV_ERROR" , EV_ERROR); |
2592 | |
2593 | /* READ WRITE filter flag */ |
2594 | #ifdef NOTE_LOWAT |
2595 | PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT" , NOTE_LOWAT); |
2596 | #endif |
2597 | |
2598 | /* VNODE filter flags */ |
2599 | #ifdef EVFILT_VNODE |
2600 | PyModule_AddIntConstant(m, "KQ_NOTE_DELETE" , NOTE_DELETE); |
2601 | PyModule_AddIntConstant(m, "KQ_NOTE_WRITE" , NOTE_WRITE); |
2602 | PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND" , NOTE_EXTEND); |
2603 | PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB" , NOTE_ATTRIB); |
2604 | PyModule_AddIntConstant(m, "KQ_NOTE_LINK" , NOTE_LINK); |
2605 | PyModule_AddIntConstant(m, "KQ_NOTE_RENAME" , NOTE_RENAME); |
2606 | PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE" , NOTE_REVOKE); |
2607 | #endif |
2608 | |
2609 | /* PROC filter flags */ |
2610 | #ifdef EVFILT_PROC |
2611 | PyModule_AddIntConstant(m, "KQ_NOTE_EXIT" , NOTE_EXIT); |
2612 | PyModule_AddIntConstant(m, "KQ_NOTE_FORK" , NOTE_FORK); |
2613 | PyModule_AddIntConstant(m, "KQ_NOTE_EXEC" , NOTE_EXEC); |
2614 | PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK" , NOTE_PCTRLMASK); |
2615 | PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK" , NOTE_PDATAMASK); |
2616 | |
2617 | PyModule_AddIntConstant(m, "KQ_NOTE_TRACK" , NOTE_TRACK); |
2618 | PyModule_AddIntConstant(m, "KQ_NOTE_CHILD" , NOTE_CHILD); |
2619 | PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR" , NOTE_TRACKERR); |
2620 | #endif |
2621 | |
2622 | /* NETDEV filter flags */ |
2623 | #ifdef EVFILT_NETDEV |
2624 | PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP" , NOTE_LINKUP); |
2625 | PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN" , NOTE_LINKDOWN); |
2626 | PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV" , NOTE_LINKINV); |
2627 | #endif |
2628 | |
2629 | #endif /* HAVE_KQUEUE */ |
2630 | return 0; |
2631 | } |
2632 | |
2633 | static PyModuleDef_Slot _select_slots[] = { |
2634 | {Py_mod_exec, _select_exec}, |
2635 | {0, NULL} |
2636 | }; |
2637 | |
2638 | static struct PyModuleDef selectmodule = { |
2639 | PyModuleDef_HEAD_INIT, |
2640 | .m_name = "select" , |
2641 | .m_doc = module_doc, |
2642 | .m_size = sizeof(_selectstate), |
2643 | .m_methods = select_methods, |
2644 | .m_slots = _select_slots, |
2645 | .m_traverse = _select_traverse, |
2646 | .m_clear = _select_clear, |
2647 | .m_free = _select_free, |
2648 | }; |
2649 | |
2650 | PyMODINIT_FUNC |
2651 | PyInit_select(void) |
2652 | { |
2653 | return PyModuleDef_Init(&selectmodule); |
2654 | } |
2655 | |