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