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