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