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