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 | |
| 721 | Py_BEGIN_ALLOW_THREADS |
| 722 | n = write(self->fd_devpoll, self->fds, size); |
| 723 | Py_END_ALLOW_THREADS |
| 724 | |
| 725 | if (n == -1 ) { |
| 726 | PyErr_SetFromErrno(PyExc_IOError); |
| 727 | return -1; |
| 728 | } |
| 729 | if (n < size) { |
| 730 | /* |
| 731 | ** Data writed to /dev/poll is a binary data structure. It is not |
| 732 | ** clear what to do if a partial write occurred. For now, raise |
| 733 | ** an exception and see if we actually found this problem in |
| 734 | ** the wild. |
| 735 | ** See http://bugs.python.org/issue6397. |
| 736 | */ |
| 737 | PyErr_Format(PyExc_IOError, "failed to write all pollfds. " |
| 738 | "Please, report at http://bugs.python.org/. " |
| 739 | "Data to report: Size tried: %d, actual size written: %d.", |
| 740 | size, n); |
| 741 | return -1; |
| 742 | } |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static PyObject * |
| 747 | internal_devpoll_register(devpollObject *self, PyObject *args, int remove) |
| 748 | { |
| 749 | PyObject *o; |
Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 750 | int fd; |
| 751 | unsigned short events = POLLIN | POLLPRI | POLLOUT; |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 752 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 753 | if (self->fd_devpoll < 0) |
| 754 | return devpoll_err_closed(); |
| 755 | |
Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 756 | if (!PyArg_ParseTuple(args, "O|O&:register", &o, ushort_converter, &events)) |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 757 | return NULL; |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 758 | |
| 759 | fd = PyObject_AsFileDescriptor(o); |
| 760 | if (fd == -1) return NULL; |
| 761 | |
| 762 | if (remove) { |
| 763 | self->fds[self->n_fds].fd = fd; |
| 764 | self->fds[self->n_fds].events = POLLREMOVE; |
| 765 | |
| 766 | if (++self->n_fds == self->max_n_fds) { |
| 767 | if (devpoll_flush(self)) |
| 768 | return NULL; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | self->fds[self->n_fds].fd = fd; |
Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 773 | self->fds[self->n_fds].events = (signed short)events; |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 774 | |
| 775 | if (++self->n_fds == self->max_n_fds) { |
| 776 | if (devpoll_flush(self)) |
| 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | Py_RETURN_NONE; |
| 781 | } |
| 782 | |
| 783 | PyDoc_STRVAR(devpoll_register_doc, |
| 784 | "register(fd [, eventmask] ) -> None\n\n\ |
| 785 | Register a file descriptor with the polling object.\n\ |
| 786 | fd -- either an integer, or an object with a fileno() method returning an\n\ |
| 787 | int.\n\ |
| 788 | events -- an optional bitmask describing the type of events to check for"); |
| 789 | |
| 790 | static PyObject * |
| 791 | devpoll_register(devpollObject *self, PyObject *args) |
| 792 | { |
| 793 | return internal_devpoll_register(self, args, 0); |
| 794 | } |
| 795 | |
| 796 | PyDoc_STRVAR(devpoll_modify_doc, |
| 797 | "modify(fd[, eventmask]) -> None\n\n\ |
| 798 | Modify a possible already registered file descriptor.\n\ |
| 799 | fd -- either an integer, or an object with a fileno() method returning an\n\ |
| 800 | int.\n\ |
| 801 | events -- an optional bitmask describing the type of events to check for"); |
| 802 | |
| 803 | static PyObject * |
| 804 | devpoll_modify(devpollObject *self, PyObject *args) |
| 805 | { |
| 806 | return internal_devpoll_register(self, args, 1); |
| 807 | } |
| 808 | |
| 809 | |
| 810 | PyDoc_STRVAR(devpoll_unregister_doc, |
| 811 | "unregister(fd) -> None\n\n\ |
| 812 | Remove a file descriptor being tracked by the polling object."); |
| 813 | |
| 814 | static PyObject * |
| 815 | devpoll_unregister(devpollObject *self, PyObject *o) |
| 816 | { |
| 817 | int fd; |
| 818 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 819 | if (self->fd_devpoll < 0) |
| 820 | return devpoll_err_closed(); |
| 821 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 822 | fd = PyObject_AsFileDescriptor( o ); |
| 823 | if (fd == -1) |
| 824 | return NULL; |
| 825 | |
| 826 | self->fds[self->n_fds].fd = fd; |
| 827 | self->fds[self->n_fds].events = POLLREMOVE; |
| 828 | |
| 829 | if (++self->n_fds == self->max_n_fds) { |
| 830 | if (devpoll_flush(self)) |
| 831 | return NULL; |
| 832 | } |
| 833 | |
| 834 | Py_RETURN_NONE; |
| 835 | } |
| 836 | |
| 837 | PyDoc_STRVAR(devpoll_poll_doc, |
| 838 | "poll( [timeout] ) -> list of (fd, event) 2-tuples\n\n\ |
| 839 | Polls the set of registered file descriptors, returning a list containing \n\ |
| 840 | any descriptors that have events or errors to report."); |
| 841 | |
| 842 | static PyObject * |
| 843 | devpoll_poll(devpollObject *self, PyObject *args) |
| 844 | { |
| 845 | struct dvpoll dvp; |
| 846 | PyObject *result_list = NULL, *tout = NULL; |
| 847 | int poll_result, i; |
| 848 | long timeout; |
| 849 | PyObject *value, *num1, *num2; |
| 850 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 851 | if (self->fd_devpoll < 0) |
| 852 | return devpoll_err_closed(); |
| 853 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 854 | if (!PyArg_UnpackTuple(args, "poll", 0, 1, &tout)) { |
| 855 | return NULL; |
| 856 | } |
| 857 | |
| 858 | /* Check values for timeout */ |
| 859 | if (tout == NULL || tout == Py_None) |
| 860 | timeout = -1; |
| 861 | else if (!PyNumber_Check(tout)) { |
| 862 | PyErr_SetString(PyExc_TypeError, |
| 863 | "timeout must be an integer or None"); |
| 864 | return NULL; |
| 865 | } |
| 866 | else { |
| 867 | tout = PyNumber_Long(tout); |
| 868 | if (!tout) |
| 869 | return NULL; |
| 870 | timeout = PyLong_AsLong(tout); |
| 871 | Py_DECREF(tout); |
| 872 | if (timeout == -1 && PyErr_Occurred()) |
| 873 | return NULL; |
| 874 | } |
| 875 | |
| 876 | if ((timeout < -1) || (timeout > INT_MAX)) { |
| 877 | PyErr_SetString(PyExc_OverflowError, |
| 878 | "timeout is out of range"); |
| 879 | return NULL; |
| 880 | } |
| 881 | |
| 882 | if (devpoll_flush(self)) |
| 883 | return NULL; |
| 884 | |
| 885 | dvp.dp_fds = self->fds; |
| 886 | dvp.dp_nfds = self->max_n_fds; |
| 887 | dvp.dp_timeout = timeout; |
| 888 | |
| 889 | /* call devpoll() */ |
| 890 | Py_BEGIN_ALLOW_THREADS |
| 891 | poll_result = ioctl(self->fd_devpoll, DP_POLL, &dvp); |
| 892 | Py_END_ALLOW_THREADS |
| 893 | |
| 894 | if (poll_result < 0) { |
| 895 | PyErr_SetFromErrno(PyExc_IOError); |
| 896 | return NULL; |
| 897 | } |
| 898 | |
| 899 | /* build the result list */ |
| 900 | |
| 901 | result_list = PyList_New(poll_result); |
| 902 | if (!result_list) |
| 903 | return NULL; |
| 904 | else { |
| 905 | for (i = 0; i < poll_result; i++) { |
| 906 | num1 = PyLong_FromLong(self->fds[i].fd); |
| 907 | num2 = PyLong_FromLong(self->fds[i].revents); |
| 908 | if ((num1 == NULL) || (num2 == NULL)) { |
| 909 | Py_XDECREF(num1); |
| 910 | Py_XDECREF(num2); |
| 911 | goto error; |
| 912 | } |
| 913 | value = PyTuple_Pack(2, num1, num2); |
| 914 | Py_DECREF(num1); |
| 915 | Py_DECREF(num2); |
| 916 | if (value == NULL) |
| 917 | goto error; |
| 918 | if ((PyList_SetItem(result_list, i, value)) == -1) { |
| 919 | Py_DECREF(value); |
| 920 | goto error; |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | return result_list; |
| 926 | |
| 927 | error: |
| 928 | Py_DECREF(result_list); |
| 929 | return NULL; |
| 930 | } |
| 931 | |
Richard Oudkerk | 168d59b | 2013-08-22 13:31:15 +0100 | [diff] [blame] | 932 | static int |
| 933 | devpoll_internal_close(devpollObject *self) |
| 934 | { |
| 935 | int save_errno = 0; |
| 936 | if (self->fd_devpoll >= 0) { |
| 937 | int fd = self->fd_devpoll; |
| 938 | self->fd_devpoll = -1; |
| 939 | Py_BEGIN_ALLOW_THREADS |
| 940 | if (close(fd) < 0) |
| 941 | save_errno = errno; |
| 942 | Py_END_ALLOW_THREADS |
| 943 | } |
| 944 | return save_errno; |
| 945 | } |
| 946 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 947 | static PyObject* |
| 948 | devpoll_close(devpollObject *self) |
| 949 | { |
| 950 | errno = devpoll_internal_close(self); |
| 951 | if (errno < 0) { |
| 952 | PyErr_SetFromErrno(PyExc_OSError); |
| 953 | return NULL; |
| 954 | } |
| 955 | Py_RETURN_NONE; |
| 956 | } |
| 957 | |
| 958 | PyDoc_STRVAR(devpoll_close_doc, |
| 959 | "close() -> None\n\ |
| 960 | \n\ |
| 961 | Close the devpoll file descriptor. Further operations on the devpoll\n\ |
| 962 | object will raise an exception."); |
| 963 | |
| 964 | static PyObject* |
| 965 | devpoll_get_closed(devpollObject *self) |
| 966 | { |
| 967 | if (self->fd_devpoll < 0) |
| 968 | Py_RETURN_TRUE; |
| 969 | else |
| 970 | Py_RETURN_FALSE; |
| 971 | } |
| 972 | |
| 973 | static PyObject* |
| 974 | devpoll_fileno(devpollObject *self) |
| 975 | { |
| 976 | if (self->fd_devpoll < 0) |
| 977 | return devpoll_err_closed(); |
| 978 | return PyLong_FromLong(self->fd_devpoll); |
| 979 | } |
| 980 | |
| 981 | PyDoc_STRVAR(devpoll_fileno_doc, |
| 982 | "fileno() -> int\n\ |
| 983 | \n\ |
| 984 | Return the file descriptor."); |
| 985 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 986 | static PyMethodDef devpoll_methods[] = { |
| 987 | {"register", (PyCFunction)devpoll_register, |
| 988 | METH_VARARGS, devpoll_register_doc}, |
| 989 | {"modify", (PyCFunction)devpoll_modify, |
| 990 | METH_VARARGS, devpoll_modify_doc}, |
| 991 | {"unregister", (PyCFunction)devpoll_unregister, |
| 992 | METH_O, devpoll_unregister_doc}, |
| 993 | {"poll", (PyCFunction)devpoll_poll, |
| 994 | METH_VARARGS, devpoll_poll_doc}, |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 995 | {"close", (PyCFunction)devpoll_close, METH_NOARGS, |
| 996 | devpoll_close_doc}, |
| 997 | {"fileno", (PyCFunction)devpoll_fileno, METH_NOARGS, |
| 998 | devpoll_fileno_doc}, |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 999 | {NULL, NULL} /* sentinel */ |
| 1000 | }; |
| 1001 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1002 | static PyGetSetDef devpoll_getsetlist[] = { |
| 1003 | {"closed", (getter)devpoll_get_closed, NULL, |
| 1004 | "True if the devpoll object is closed"}, |
| 1005 | {0}, |
| 1006 | }; |
| 1007 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1008 | static devpollObject * |
| 1009 | newDevPollObject(void) |
| 1010 | { |
| 1011 | devpollObject *self; |
| 1012 | int fd_devpoll, limit_result; |
| 1013 | struct pollfd *fds; |
| 1014 | struct rlimit limit; |
| 1015 | |
| 1016 | Py_BEGIN_ALLOW_THREADS |
| 1017 | /* |
| 1018 | ** If we try to process more that getrlimit() |
| 1019 | ** fds, the kernel will give an error, so |
| 1020 | ** we set the limit here. It is a dynamic |
| 1021 | ** value, because we can change rlimit() anytime. |
| 1022 | */ |
| 1023 | limit_result = getrlimit(RLIMIT_NOFILE, &limit); |
| 1024 | if (limit_result != -1) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1025 | fd_devpoll = _Py_open("/dev/poll", O_RDWR); |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1026 | Py_END_ALLOW_THREADS |
| 1027 | |
| 1028 | if (limit_result == -1) { |
| 1029 | PyErr_SetFromErrno(PyExc_OSError); |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | if (fd_devpoll == -1) { |
| 1033 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, "/dev/poll"); |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | |
| 1037 | fds = PyMem_NEW(struct pollfd, limit.rlim_cur); |
| 1038 | if (fds == NULL) { |
| 1039 | close(fd_devpoll); |
| 1040 | PyErr_NoMemory(); |
| 1041 | return NULL; |
| 1042 | } |
| 1043 | |
| 1044 | self = PyObject_New(devpollObject, &devpoll_Type); |
| 1045 | if (self == NULL) { |
| 1046 | close(fd_devpoll); |
| 1047 | PyMem_DEL(fds); |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | self->fd_devpoll = fd_devpoll; |
| 1051 | self->max_n_fds = limit.rlim_cur; |
| 1052 | self->n_fds = 0; |
| 1053 | self->fds = fds; |
| 1054 | |
| 1055 | return self; |
| 1056 | } |
| 1057 | |
| 1058 | static void |
| 1059 | devpoll_dealloc(devpollObject *self) |
| 1060 | { |
Richard Oudkerk | a93bf7b | 2013-08-22 14:03:44 +0100 | [diff] [blame] | 1061 | (void)devpoll_internal_close(self); |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1062 | PyMem_DEL(self->fds); |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1063 | PyObject_Del(self); |
| 1064 | } |
| 1065 | |
| 1066 | static PyTypeObject devpoll_Type = { |
| 1067 | /* The ob_type field must be initialized in the module init function |
| 1068 | * to be portable to Windows without using C++. */ |
| 1069 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1070 | "select.devpoll", /*tp_name*/ |
| 1071 | sizeof(devpollObject), /*tp_basicsize*/ |
| 1072 | 0, /*tp_itemsize*/ |
| 1073 | /* methods */ |
| 1074 | (destructor)devpoll_dealloc, /*tp_dealloc*/ |
| 1075 | 0, /*tp_print*/ |
| 1076 | 0, /*tp_getattr*/ |
| 1077 | 0, /*tp_setattr*/ |
| 1078 | 0, /*tp_reserved*/ |
| 1079 | 0, /*tp_repr*/ |
| 1080 | 0, /*tp_as_number*/ |
| 1081 | 0, /*tp_as_sequence*/ |
| 1082 | 0, /*tp_as_mapping*/ |
| 1083 | 0, /*tp_hash*/ |
| 1084 | 0, /*tp_call*/ |
| 1085 | 0, /*tp_str*/ |
| 1086 | 0, /*tp_getattro*/ |
| 1087 | 0, /*tp_setattro*/ |
| 1088 | 0, /*tp_as_buffer*/ |
| 1089 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 1090 | 0, /*tp_doc*/ |
| 1091 | 0, /*tp_traverse*/ |
| 1092 | 0, /*tp_clear*/ |
| 1093 | 0, /*tp_richcompare*/ |
| 1094 | 0, /*tp_weaklistoffset*/ |
| 1095 | 0, /*tp_iter*/ |
| 1096 | 0, /*tp_iternext*/ |
| 1097 | devpoll_methods, /*tp_methods*/ |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1098 | 0, /* tp_members */ |
| 1099 | devpoll_getsetlist, /* tp_getset */ |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1100 | }; |
| 1101 | #endif /* HAVE_SYS_DEVPOLL_H */ |
| 1102 | |
| 1103 | |
| 1104 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1105 | PyDoc_STRVAR(poll_doc, |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1106 | "Returns a polling object, which supports registering and\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1107 | unregistering file descriptors, and then polling them for I/O events."); |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1108 | |
| 1109 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1110 | select_poll(PyObject *self, PyObject *unused) |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | return (PyObject *)newPollObject(); |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1113 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1114 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1115 | #ifdef HAVE_SYS_DEVPOLL_H |
| 1116 | PyDoc_STRVAR(devpoll_doc, |
| 1117 | "Returns a polling object, which supports registering and\n\ |
| 1118 | unregistering file descriptors, and then polling them for I/O events."); |
| 1119 | |
| 1120 | static PyObject * |
| 1121 | select_devpoll(PyObject *self, PyObject *unused) |
| 1122 | { |
| 1123 | return (PyObject *)newDevPollObject(); |
| 1124 | } |
| 1125 | #endif |
| 1126 | |
| 1127 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1128 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | /* |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1130 | * On some systems poll() sets errno on invalid file descriptors. We test |
| 1131 | * for this at runtime because this bug may be fixed or introduced between |
| 1132 | * OS releases. |
| 1133 | */ |
| 1134 | static int select_have_broken_poll(void) |
| 1135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | int poll_test; |
| 1137 | int filedes[2]; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1139 | struct pollfd poll_struct = { 0, POLLIN|POLLPRI|POLLOUT, 0 }; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | /* Create a file descriptor to make invalid */ |
| 1142 | if (pipe(filedes) < 0) { |
| 1143 | return 1; |
| 1144 | } |
| 1145 | poll_struct.fd = filedes[0]; |
| 1146 | close(filedes[0]); |
| 1147 | close(filedes[1]); |
| 1148 | poll_test = poll(&poll_struct, 1, 0); |
| 1149 | if (poll_test < 0) { |
| 1150 | return 1; |
| 1151 | } else if (poll_test == 0 && poll_struct.revents != POLLNVAL) { |
| 1152 | return 1; |
| 1153 | } |
| 1154 | return 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1155 | } |
| 1156 | #endif /* __APPLE__ */ |
| 1157 | |
| 1158 | #endif /* HAVE_POLL */ |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 1159 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1160 | #ifdef HAVE_EPOLL |
| 1161 | /* ************************************************************************** |
| 1162 | * epoll interface for Linux 2.6 |
| 1163 | * |
| 1164 | * Written by Christian Heimes |
| 1165 | * Inspired by Twisted's _epoll.pyx and select.poll() |
| 1166 | */ |
| 1167 | |
| 1168 | #ifdef HAVE_SYS_EPOLL_H |
| 1169 | #include <sys/epoll.h> |
| 1170 | #endif |
| 1171 | |
| 1172 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | PyObject_HEAD |
Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1174 | SOCKET epfd; /* epoll control file descriptor */ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1175 | } pyEpoll_Object; |
| 1176 | |
| 1177 | static PyTypeObject pyEpoll_Type; |
| 1178 | #define pyepoll_CHECK(op) (PyObject_TypeCheck((op), &pyEpoll_Type)) |
| 1179 | |
| 1180 | static PyObject * |
| 1181 | pyepoll_err_closed(void) |
| 1182 | { |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1183 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed epoll object"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | return NULL; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | static int |
| 1188 | pyepoll_internal_close(pyEpoll_Object *self) |
| 1189 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | int save_errno = 0; |
| 1191 | if (self->epfd >= 0) { |
| 1192 | int epfd = self->epfd; |
| 1193 | self->epfd = -1; |
| 1194 | Py_BEGIN_ALLOW_THREADS |
| 1195 | if (close(epfd) < 0) |
| 1196 | save_errno = errno; |
| 1197 | Py_END_ALLOW_THREADS |
| 1198 | } |
| 1199 | return save_errno; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static PyObject * |
Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1203 | newPyEpoll_Object(PyTypeObject *type, int sizehint, int flags, SOCKET fd) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | pyEpoll_Object *self; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | assert(type != NULL && type->tp_alloc != NULL); |
| 1208 | self = (pyEpoll_Object *) type->tp_alloc(type, 0); |
| 1209 | if (self == NULL) |
| 1210 | return NULL; |
| 1211 | |
| 1212 | if (fd == -1) { |
| 1213 | Py_BEGIN_ALLOW_THREADS |
Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1214 | #ifdef HAVE_EPOLL_CREATE1 |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1215 | flags |= EPOLL_CLOEXEC; |
Benjamin Peterson | 83251c1 | 2011-12-27 16:01:21 -0600 | [diff] [blame] | 1216 | if (flags) |
| 1217 | self->epfd = epoll_create1(flags); |
| 1218 | else |
Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1219 | #endif |
Benjamin Peterson | 83251c1 | 2011-12-27 16:01:21 -0600 | [diff] [blame] | 1220 | self->epfd = epoll_create(sizehint); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | Py_END_ALLOW_THREADS |
| 1222 | } |
| 1223 | else { |
| 1224 | self->epfd = fd; |
| 1225 | } |
| 1226 | if (self->epfd < 0) { |
| 1227 | Py_DECREF(self); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1228 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | return NULL; |
| 1230 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1231 | |
| 1232 | #ifndef HAVE_EPOLL_CREATE1 |
Victor Stinner | d72fe89 | 2013-08-28 12:22:39 +0200 | [diff] [blame] | 1233 | if (fd == -1 && _Py_set_inheritable(self->epfd, 0, NULL) < 0) { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1234 | Py_DECREF(self); |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | #endif |
| 1238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | return (PyObject *)self; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | |
| 1243 | static PyObject * |
| 1244 | pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1245 | { |
Benjamin Peterson | 83251c1 | 2011-12-27 16:01:21 -0600 | [diff] [blame] | 1246 | int flags = 0, sizehint = FD_SETSIZE - 1; |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 1247 | static char *kwlist[] = {"sizehint", "flags", NULL}; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1248 | |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 1249 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist, |
| 1250 | &sizehint, &flags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | return NULL; |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 1252 | if (sizehint < 0) { |
| 1253 | PyErr_SetString(PyExc_ValueError, "negative sizehint"); |
| 1254 | return NULL; |
| 1255 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1256 | |
Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1257 | return newPyEpoll_Object(type, sizehint, flags, -1); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | static void |
| 1262 | pyepoll_dealloc(pyEpoll_Object *self) |
| 1263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | (void)pyepoll_internal_close(self); |
| 1265 | Py_TYPE(self)->tp_free(self); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | static PyObject* |
| 1269 | pyepoll_close(pyEpoll_Object *self) |
| 1270 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | errno = pyepoll_internal_close(self); |
| 1272 | if (errno < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1273 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | return NULL; |
| 1275 | } |
| 1276 | Py_RETURN_NONE; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | PyDoc_STRVAR(pyepoll_close_doc, |
| 1280 | "close() -> None\n\ |
| 1281 | \n\ |
| 1282 | Close the epoll control file descriptor. Further operations on the epoll\n\ |
| 1283 | object will raise an exception."); |
| 1284 | |
| 1285 | static PyObject* |
| 1286 | pyepoll_get_closed(pyEpoll_Object *self) |
| 1287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | if (self->epfd < 0) |
| 1289 | Py_RETURN_TRUE; |
| 1290 | else |
| 1291 | Py_RETURN_FALSE; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | static PyObject* |
| 1295 | pyepoll_fileno(pyEpoll_Object *self) |
| 1296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | if (self->epfd < 0) |
| 1298 | return pyepoll_err_closed(); |
| 1299 | return PyLong_FromLong(self->epfd); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | PyDoc_STRVAR(pyepoll_fileno_doc, |
| 1303 | "fileno() -> int\n\ |
| 1304 | \n\ |
| 1305 | Return the epoll control file descriptor."); |
| 1306 | |
| 1307 | static PyObject* |
| 1308 | pyepoll_fromfd(PyObject *cls, PyObject *args) |
| 1309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | SOCKET fd; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | if (!PyArg_ParseTuple(args, "i:fromfd", &fd)) |
| 1313 | return NULL; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1314 | |
Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 1315 | return newPyEpoll_Object((PyTypeObject*)cls, FD_SETSIZE - 1, 0, fd); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | PyDoc_STRVAR(pyepoll_fromfd_doc, |
| 1319 | "fromfd(fd) -> epoll\n\ |
| 1320 | \n\ |
| 1321 | Create an epoll object from a given control fd."); |
| 1322 | |
| 1323 | static PyObject * |
| 1324 | pyepoll_internal_ctl(int epfd, int op, PyObject *pfd, unsigned int events) |
| 1325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | struct epoll_event ev; |
| 1327 | int result; |
| 1328 | int fd; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | if (epfd < 0) |
| 1331 | return pyepoll_err_closed(); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | fd = PyObject_AsFileDescriptor(pfd); |
| 1334 | if (fd == -1) { |
| 1335 | return NULL; |
| 1336 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1337 | |
Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1338 | switch (op) { |
| 1339 | case EPOLL_CTL_ADD: |
| 1340 | case EPOLL_CTL_MOD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | ev.events = events; |
| 1342 | ev.data.fd = fd; |
| 1343 | Py_BEGIN_ALLOW_THREADS |
| 1344 | result = epoll_ctl(epfd, op, fd, &ev); |
| 1345 | Py_END_ALLOW_THREADS |
| 1346 | break; |
Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1347 | case EPOLL_CTL_DEL: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | /* In kernel versions before 2.6.9, the EPOLL_CTL_DEL |
| 1349 | * operation required a non-NULL pointer in event, even |
| 1350 | * though this argument is ignored. */ |
| 1351 | Py_BEGIN_ALLOW_THREADS |
| 1352 | result = epoll_ctl(epfd, op, fd, &ev); |
| 1353 | if (errno == EBADF) { |
| 1354 | /* fd already closed */ |
| 1355 | result = 0; |
| 1356 | errno = 0; |
| 1357 | } |
| 1358 | Py_END_ALLOW_THREADS |
| 1359 | break; |
Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1360 | default: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | result = -1; |
| 1362 | errno = EINVAL; |
| 1363 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | if (result < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1366 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | return NULL; |
| 1368 | } |
| 1369 | Py_RETURN_NONE; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | static PyObject * |
| 1373 | pyepoll_register(pyEpoll_Object *self, PyObject *args, PyObject *kwds) |
| 1374 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | PyObject *pfd; |
| 1376 | unsigned int events = EPOLLIN | EPOLLOUT | EPOLLPRI; |
| 1377 | static char *kwlist[] = {"fd", "eventmask", NULL}; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|I:register", kwlist, |
| 1380 | &pfd, &events)) { |
| 1381 | return NULL; |
| 1382 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_ADD, pfd, events); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | PyDoc_STRVAR(pyepoll_register_doc, |
Georg Brandl | 222569d | 2010-08-02 20:47:56 +0000 | [diff] [blame] | 1388 | "register(fd[, eventmask]) -> None\n\ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1389 | \n\ |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1390 | 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] | 1391 | fd is the target file descriptor of the operation.\n\ |
| 1392 | 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] | 1393 | is EPOLL_IN | EPOLL_OUT | EPOLL_PRI.\n\ |
| 1394 | \n\ |
| 1395 | The epoll interface supports all file descriptors that support poll."); |
| 1396 | |
| 1397 | static PyObject * |
| 1398 | pyepoll_modify(pyEpoll_Object *self, PyObject *args, PyObject *kwds) |
| 1399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | PyObject *pfd; |
| 1401 | unsigned int events; |
| 1402 | static char *kwlist[] = {"fd", "eventmask", NULL}; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "OI:modify", kwlist, |
| 1405 | &pfd, &events)) { |
| 1406 | return NULL; |
| 1407 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1409 | return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_MOD, pfd, events); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | PyDoc_STRVAR(pyepoll_modify_doc, |
| 1413 | "modify(fd, eventmask) -> None\n\ |
| 1414 | \n\ |
| 1415 | fd is the target file descriptor of the operation\n\ |
| 1416 | events is a bit set composed of the various EPOLL constants"); |
| 1417 | |
| 1418 | static PyObject * |
| 1419 | pyepoll_unregister(pyEpoll_Object *self, PyObject *args, PyObject *kwds) |
| 1420 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | PyObject *pfd; |
| 1422 | static char *kwlist[] = {"fd", NULL}; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:unregister", kwlist, |
| 1425 | &pfd)) { |
| 1426 | return NULL; |
| 1427 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | return pyepoll_internal_ctl(self->epfd, EPOLL_CTL_DEL, pfd, 0); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | PyDoc_STRVAR(pyepoll_unregister_doc, |
| 1433 | "unregister(fd) -> None\n\ |
| 1434 | \n\ |
| 1435 | fd is the target file descriptor of the operation."); |
| 1436 | |
| 1437 | static PyObject * |
| 1438 | pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds) |
| 1439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1440 | double dtimeout = -1.; |
| 1441 | int timeout; |
| 1442 | int maxevents = -1; |
| 1443 | int nfds, i; |
| 1444 | PyObject *elist = NULL, *etuple = NULL; |
Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1445 | struct epoll_event *evs = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | static char *kwlist[] = {"timeout", "maxevents", NULL}; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | if (self->epfd < 0) |
| 1449 | return pyepoll_err_closed(); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|di:poll", kwlist, |
| 1452 | &dtimeout, &maxevents)) { |
| 1453 | return NULL; |
| 1454 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | if (dtimeout < 0) { |
| 1457 | timeout = -1; |
| 1458 | } |
| 1459 | else if (dtimeout * 1000.0 > INT_MAX) { |
| 1460 | PyErr_SetString(PyExc_OverflowError, |
| 1461 | "timeout is too large"); |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | else { |
Victor Stinner | dcd9740 | 2014-01-31 12:12:53 +0100 | [diff] [blame] | 1465 | /* epoll_wait() has a resolution of 1 millisecond, round away from zero |
| 1466 | to wait *at least* dtimeout seconds. */ |
| 1467 | timeout = (int)ceil(dtimeout * 1000.0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | if (maxevents == -1) { |
Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1471 | maxevents = FD_SETSIZE-1; |
| 1472 | } |
| 1473 | else if (maxevents < 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | PyErr_Format(PyExc_ValueError, |
| 1475 | "maxevents must be greater than 0, got %d", |
| 1476 | maxevents); |
| 1477 | return NULL; |
| 1478 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1479 | |
Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1480 | evs = PyMem_New(struct epoll_event, maxevents); |
| 1481 | if (evs == NULL) { |
Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1482 | PyErr_NoMemory(); |
| 1483 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1485 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | Py_BEGIN_ALLOW_THREADS |
Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1487 | nfds = epoll_wait(self->epfd, evs, maxevents, timeout); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1488 | Py_END_ALLOW_THREADS |
| 1489 | if (nfds < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1490 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | goto error; |
| 1492 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | elist = PyList_New(nfds); |
| 1495 | if (elist == NULL) { |
| 1496 | goto error; |
| 1497 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | for (i = 0; i < nfds; i++) { |
Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1500 | etuple = Py_BuildValue("iI", evs[i].data.fd, evs[i].events); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | if (etuple == NULL) { |
| 1502 | Py_CLEAR(elist); |
| 1503 | goto error; |
| 1504 | } |
| 1505 | PyList_SET_ITEM(elist, i, etuple); |
| 1506 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1507 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 1508 | error: |
Charles-François Natali | a6ebb2d | 2013-01-12 12:31:00 +0100 | [diff] [blame] | 1509 | PyMem_Free(evs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | return elist; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | PyDoc_STRVAR(pyepoll_poll_doc, |
| 1514 | "poll([timeout=-1[, maxevents=-1]]) -> [(fd, events), (...)]\n\ |
| 1515 | \n\ |
| 1516 | Wait for events on the epoll file descriptor for a maximum time of timeout\n\ |
| 1517 | in seconds (as float). -1 makes poll wait indefinitely.\n\ |
| 1518 | Up to maxevents are returned to the caller."); |
| 1519 | |
Antoine Pitrou | 09bb89b | 2012-12-15 21:14:21 +0100 | [diff] [blame] | 1520 | static PyObject * |
| 1521 | pyepoll_enter(pyEpoll_Object *self, PyObject *args) |
| 1522 | { |
| 1523 | if (self->epfd < 0) |
| 1524 | return pyepoll_err_closed(); |
| 1525 | |
| 1526 | Py_INCREF(self); |
| 1527 | return (PyObject *)self; |
| 1528 | } |
| 1529 | |
| 1530 | static PyObject * |
| 1531 | pyepoll_exit(PyObject *self, PyObject *args) |
| 1532 | { |
| 1533 | _Py_IDENTIFIER(close); |
| 1534 | |
| 1535 | return _PyObject_CallMethodId(self, &PyId_close, NULL); |
| 1536 | } |
| 1537 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1538 | static PyMethodDef pyepoll_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1539 | {"fromfd", (PyCFunction)pyepoll_fromfd, |
| 1540 | METH_VARARGS | METH_CLASS, pyepoll_fromfd_doc}, |
| 1541 | {"close", (PyCFunction)pyepoll_close, METH_NOARGS, |
| 1542 | pyepoll_close_doc}, |
| 1543 | {"fileno", (PyCFunction)pyepoll_fileno, METH_NOARGS, |
| 1544 | pyepoll_fileno_doc}, |
| 1545 | {"modify", (PyCFunction)pyepoll_modify, |
| 1546 | METH_VARARGS | METH_KEYWORDS, pyepoll_modify_doc}, |
| 1547 | {"register", (PyCFunction)pyepoll_register, |
| 1548 | METH_VARARGS | METH_KEYWORDS, pyepoll_register_doc}, |
| 1549 | {"unregister", (PyCFunction)pyepoll_unregister, |
| 1550 | METH_VARARGS | METH_KEYWORDS, pyepoll_unregister_doc}, |
| 1551 | {"poll", (PyCFunction)pyepoll_poll, |
| 1552 | METH_VARARGS | METH_KEYWORDS, pyepoll_poll_doc}, |
Antoine Pitrou | 09bb89b | 2012-12-15 21:14:21 +0100 | [diff] [blame] | 1553 | {"__enter__", (PyCFunction)pyepoll_enter, METH_NOARGS, |
| 1554 | NULL}, |
| 1555 | {"__exit__", (PyCFunction)pyepoll_exit, METH_VARARGS, |
| 1556 | NULL}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | {NULL, NULL}, |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1558 | }; |
| 1559 | |
| 1560 | static PyGetSetDef pyepoll_getsetlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | {"closed", (getter)pyepoll_get_closed, NULL, |
| 1562 | "True if the epoll handler is closed"}, |
| 1563 | {0}, |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1564 | }; |
| 1565 | |
| 1566 | PyDoc_STRVAR(pyepoll_doc, |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 1567 | "select.epoll(sizehint=-1, flags=0)\n\ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1568 | \n\ |
| 1569 | Returns an epolling object\n\ |
| 1570 | \n\ |
| 1571 | sizehint must be a positive integer or -1 for the default size. The\n\ |
| 1572 | sizehint is used to optimize internal data structures. It doesn't limit\n\ |
| 1573 | the maximum number of monitored events."); |
| 1574 | |
| 1575 | static PyTypeObject pyEpoll_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1577 | "select.epoll", /* tp_name */ |
| 1578 | sizeof(pyEpoll_Object), /* tp_basicsize */ |
| 1579 | 0, /* tp_itemsize */ |
| 1580 | (destructor)pyepoll_dealloc, /* tp_dealloc */ |
| 1581 | 0, /* tp_print */ |
| 1582 | 0, /* tp_getattr */ |
| 1583 | 0, /* tp_setattr */ |
| 1584 | 0, /* tp_reserved */ |
| 1585 | 0, /* tp_repr */ |
| 1586 | 0, /* tp_as_number */ |
| 1587 | 0, /* tp_as_sequence */ |
| 1588 | 0, /* tp_as_mapping */ |
| 1589 | 0, /* tp_hash */ |
| 1590 | 0, /* tp_call */ |
| 1591 | 0, /* tp_str */ |
| 1592 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1593 | 0, /* tp_setattro */ |
| 1594 | 0, /* tp_as_buffer */ |
| 1595 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1596 | pyepoll_doc, /* tp_doc */ |
| 1597 | 0, /* tp_traverse */ |
| 1598 | 0, /* tp_clear */ |
| 1599 | 0, /* tp_richcompare */ |
| 1600 | 0, /* tp_weaklistoffset */ |
| 1601 | 0, /* tp_iter */ |
| 1602 | 0, /* tp_iternext */ |
| 1603 | pyepoll_methods, /* tp_methods */ |
| 1604 | 0, /* tp_members */ |
| 1605 | pyepoll_getsetlist, /* tp_getset */ |
| 1606 | 0, /* tp_base */ |
| 1607 | 0, /* tp_dict */ |
| 1608 | 0, /* tp_descr_get */ |
| 1609 | 0, /* tp_descr_set */ |
| 1610 | 0, /* tp_dictoffset */ |
| 1611 | 0, /* tp_init */ |
| 1612 | 0, /* tp_alloc */ |
| 1613 | pyepoll_new, /* tp_new */ |
| 1614 | 0, /* tp_free */ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1615 | }; |
| 1616 | |
| 1617 | #endif /* HAVE_EPOLL */ |
| 1618 | |
| 1619 | #ifdef HAVE_KQUEUE |
| 1620 | /* ************************************************************************** |
| 1621 | * kqueue interface for BSD |
| 1622 | * |
| 1623 | * Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes |
| 1624 | * All rights reserved. |
| 1625 | * |
| 1626 | * Redistribution and use in source and binary forms, with or without |
| 1627 | * modification, are permitted provided that the following conditions |
| 1628 | * are met: |
| 1629 | * 1. Redistributions of source code must retain the above copyright |
| 1630 | * notice, this list of conditions and the following disclaimer. |
| 1631 | * 2. Redistributions in binary form must reproduce the above copyright |
| 1632 | * notice, this list of conditions and the following disclaimer in the |
| 1633 | * documentation and/or other materials provided with the distribution. |
| 1634 | * |
| 1635 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 1636 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 1637 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 1638 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 1639 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 1640 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 1641 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 1642 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 1643 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 1644 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 1645 | * SUCH DAMAGE. |
| 1646 | */ |
| 1647 | |
| 1648 | #ifdef HAVE_SYS_EVENT_H |
| 1649 | #include <sys/event.h> |
| 1650 | #endif |
| 1651 | |
| 1652 | PyDoc_STRVAR(kqueue_event_doc, |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 1653 | "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] | 1654 | \n\ |
| 1655 | This object is the equivalent of the struct kevent for the C API.\n\ |
| 1656 | \n\ |
| 1657 | See the kqueue manpage for more detailed information about the meaning\n\ |
| 1658 | of the arguments.\n\ |
| 1659 | \n\ |
| 1660 | One minor note: while you might hope that udata could store a\n\ |
| 1661 | reference to a python object, it cannot, because it is impossible to\n\ |
| 1662 | keep a proper reference count of the object once it's passed into the\n\ |
| 1663 | kernel. Therefore, I have restricted it to only storing an integer. I\n\ |
| 1664 | recommend ignoring it and simply using the 'ident' field to key off\n\ |
| 1665 | of. You could also set up a dictionary on the python side to store a\n\ |
| 1666 | udata->object mapping."); |
| 1667 | |
| 1668 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | PyObject_HEAD |
| 1670 | struct kevent e; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1671 | } kqueue_event_Object; |
| 1672 | |
| 1673 | static PyTypeObject kqueue_event_Type; |
| 1674 | |
| 1675 | #define kqueue_event_Check(op) (PyObject_TypeCheck((op), &kqueue_event_Type)) |
| 1676 | |
| 1677 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | PyObject_HEAD |
| 1679 | SOCKET kqfd; /* kqueue control fd */ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1680 | } kqueue_queue_Object; |
| 1681 | |
| 1682 | static PyTypeObject kqueue_queue_Type; |
| 1683 | |
| 1684 | #define kqueue_queue_Check(op) (PyObject_TypeCheck((op), &kqueue_queue_Type)) |
| 1685 | |
Antoine Pitrou | d83f1e6 | 2009-11-04 21:10:38 +0000 | [diff] [blame] | 1686 | #if (SIZEOF_UINTPTR_T != SIZEOF_VOID_P) |
| 1687 | # error uintptr_t does not match void *! |
| 1688 | #elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG) |
| 1689 | # define T_UINTPTRT T_ULONGLONG |
| 1690 | # define T_INTPTRT T_LONGLONG |
| 1691 | # define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong |
| 1692 | # define UINTPTRT_FMT_UNIT "K" |
| 1693 | # define INTPTRT_FMT_UNIT "L" |
| 1694 | #elif (SIZEOF_UINTPTR_T == SIZEOF_LONG) |
| 1695 | # define T_UINTPTRT T_ULONG |
| 1696 | # define T_INTPTRT T_LONG |
| 1697 | # define PyLong_AsUintptr_t PyLong_AsUnsignedLong |
| 1698 | # define UINTPTRT_FMT_UNIT "k" |
| 1699 | # define INTPTRT_FMT_UNIT "l" |
| 1700 | #elif (SIZEOF_UINTPTR_T == SIZEOF_INT) |
| 1701 | # define T_UINTPTRT T_UINT |
| 1702 | # define T_INTPTRT T_INT |
| 1703 | # define PyLong_AsUintptr_t PyLong_AsUnsignedLong |
| 1704 | # define UINTPTRT_FMT_UNIT "I" |
| 1705 | # define INTPTRT_FMT_UNIT "i" |
| 1706 | #else |
| 1707 | # error uintptr_t does not match int, long, or long long! |
| 1708 | #endif |
| 1709 | |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1710 | /* |
| 1711 | * kevent is not standard and its members vary across BSDs. |
| 1712 | */ |
| 1713 | #if !defined(__OpenBSD__) |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1714 | # define IDENT_TYPE T_UINTPTRT |
| 1715 | # define IDENT_CAST Py_intptr_t |
| 1716 | # define DATA_TYPE T_INTPTRT |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1717 | # define DATA_FMT_UNIT INTPTRT_FMT_UNIT |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1718 | # define IDENT_AsType PyLong_AsUintptr_t |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1719 | #else |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1720 | # define IDENT_TYPE T_UINT |
| 1721 | # define IDENT_CAST int |
| 1722 | # define DATA_TYPE T_INT |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1723 | # define DATA_FMT_UNIT "i" |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1724 | # define IDENT_AsType PyLong_AsUnsignedLong |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1725 | #endif |
| 1726 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1727 | /* Unfortunately, we can't store python objects in udata, because |
| 1728 | * kevents in the kernel can be removed without warning, which would |
| 1729 | * forever lose the refcount on the object stored with it. |
| 1730 | */ |
| 1731 | |
| 1732 | #define KQ_OFF(x) offsetof(kqueue_event_Object, x) |
| 1733 | static struct PyMemberDef kqueue_event_members[] = { |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1734 | {"ident", IDENT_TYPE, KQ_OFF(e.ident)}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | {"filter", T_SHORT, KQ_OFF(e.filter)}, |
| 1736 | {"flags", T_USHORT, KQ_OFF(e.flags)}, |
| 1737 | {"fflags", T_UINT, KQ_OFF(e.fflags)}, |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1738 | {"data", DATA_TYPE, KQ_OFF(e.data)}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | {"udata", T_UINTPTRT, KQ_OFF(e.udata)}, |
| 1740 | {NULL} /* Sentinel */ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1741 | }; |
| 1742 | #undef KQ_OFF |
| 1743 | |
| 1744 | static PyObject * |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 1745 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1746 | kqueue_event_repr(kqueue_event_Object *s) |
| 1747 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1748 | char buf[1024]; |
| 1749 | PyOS_snprintf( |
| 1750 | buf, sizeof(buf), |
| 1751 | "<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x " |
| 1752 | "data=0x%zd udata=%p>", |
| 1753 | (size_t)(s->e.ident), s->e.filter, s->e.flags, |
| 1754 | s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata); |
| 1755 | return PyUnicode_FromString(buf); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | static int |
| 1759 | kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds) |
| 1760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1761 | PyObject *pfd; |
| 1762 | static char *kwlist[] = {"ident", "filter", "flags", "fflags", |
| 1763 | "data", "udata", NULL}; |
Christian Heimes | f1fe159 | 2013-08-25 14:57:00 +0200 | [diff] [blame] | 1764 | static char *fmt = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent"; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | 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] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, |
| 1769 | &pfd, &(self->e.filter), &(self->e.flags), |
| 1770 | &(self->e.fflags), &(self->e.data), &(self->e.udata))) { |
| 1771 | return -1; |
| 1772 | } |
| 1773 | |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1774 | if (PyLong_Check(pfd) |
| 1775 | #if IDENT_TYPE == T_UINT |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 1776 | && PyLong_AsUnsignedLong(pfd) <= UINT_MAX |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1777 | #endif |
| 1778 | ) { |
| 1779 | self->e.ident = IDENT_AsType(pfd); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | } |
| 1781 | else { |
| 1782 | self->e.ident = PyObject_AsFileDescriptor(pfd); |
| 1783 | } |
| 1784 | if (PyErr_Occurred()) { |
| 1785 | return -1; |
| 1786 | } |
| 1787 | return 0; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | static PyObject * |
| 1791 | kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | int op) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | Py_intptr_t result = 0; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1796 | if (!kqueue_event_Check(o)) { |
| 1797 | if (op == Py_EQ || op == Py_NE) { |
| 1798 | PyObject *res = op == Py_EQ ? Py_False : Py_True; |
| 1799 | Py_INCREF(res); |
| 1800 | return res; |
| 1801 | } |
| 1802 | PyErr_Format(PyExc_TypeError, |
| 1803 | "can't compare %.200s to %.200s", |
| 1804 | Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name); |
| 1805 | return NULL; |
| 1806 | } |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1807 | if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | ((result = s->e.filter - o->e.filter) == 0) && |
| 1809 | ((result = s->e.flags - o->e.flags) == 0) && |
Charles-Francois Natali | 002a77d | 2013-05-06 21:24:31 +0200 | [diff] [blame] | 1810 | ((result = (int)(s->e.fflags - o->e.fflags)) == 0) && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | ((result = s->e.data - o->e.data) == 0) && |
| 1812 | ((result = s->e.udata - o->e.udata) == 0) |
| 1813 | ) { |
| 1814 | result = 0; |
| 1815 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1817 | switch (op) { |
Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1818 | case Py_EQ: |
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_NE: |
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_LE: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | result = (result <= 0); |
| 1826 | break; |
Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1827 | case Py_GE: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | result = (result >= 0); |
| 1829 | break; |
Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1830 | case Py_LT: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1831 | result = (result < 0); |
| 1832 | break; |
Guido van Rossum | ee07b94 | 2013-12-06 17:46:22 -0800 | [diff] [blame] | 1833 | case Py_GT: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | result = (result > 0); |
| 1835 | break; |
| 1836 | } |
| 1837 | return PyBool_FromLong((long)result); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | static PyTypeObject kqueue_event_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1841 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1842 | "select.kevent", /* tp_name */ |
| 1843 | sizeof(kqueue_event_Object), /* tp_basicsize */ |
| 1844 | 0, /* tp_itemsize */ |
| 1845 | 0, /* tp_dealloc */ |
| 1846 | 0, /* tp_print */ |
| 1847 | 0, /* tp_getattr */ |
| 1848 | 0, /* tp_setattr */ |
| 1849 | 0, /* tp_reserved */ |
| 1850 | (reprfunc)kqueue_event_repr, /* tp_repr */ |
| 1851 | 0, /* tp_as_number */ |
| 1852 | 0, /* tp_as_sequence */ |
| 1853 | 0, /* tp_as_mapping */ |
| 1854 | 0, /* tp_hash */ |
| 1855 | 0, /* tp_call */ |
| 1856 | 0, /* tp_str */ |
| 1857 | 0, /* tp_getattro */ |
| 1858 | 0, /* tp_setattro */ |
| 1859 | 0, /* tp_as_buffer */ |
| 1860 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1861 | kqueue_event_doc, /* tp_doc */ |
| 1862 | 0, /* tp_traverse */ |
| 1863 | 0, /* tp_clear */ |
| 1864 | (richcmpfunc)kqueue_event_richcompare, /* tp_richcompare */ |
| 1865 | 0, /* tp_weaklistoffset */ |
| 1866 | 0, /* tp_iter */ |
| 1867 | 0, /* tp_iternext */ |
| 1868 | 0, /* tp_methods */ |
| 1869 | kqueue_event_members, /* tp_members */ |
| 1870 | 0, /* tp_getset */ |
| 1871 | 0, /* tp_base */ |
| 1872 | 0, /* tp_dict */ |
| 1873 | 0, /* tp_descr_get */ |
| 1874 | 0, /* tp_descr_set */ |
| 1875 | 0, /* tp_dictoffset */ |
| 1876 | (initproc)kqueue_event_init, /* tp_init */ |
| 1877 | 0, /* tp_alloc */ |
| 1878 | 0, /* tp_new */ |
| 1879 | 0, /* tp_free */ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1880 | }; |
| 1881 | |
| 1882 | static PyObject * |
| 1883 | kqueue_queue_err_closed(void) |
| 1884 | { |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 1885 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed kqueue object"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | return NULL; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | static int |
| 1890 | kqueue_queue_internal_close(kqueue_queue_Object *self) |
| 1891 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | int save_errno = 0; |
| 1893 | if (self->kqfd >= 0) { |
| 1894 | int kqfd = self->kqfd; |
| 1895 | self->kqfd = -1; |
| 1896 | Py_BEGIN_ALLOW_THREADS |
| 1897 | if (close(kqfd) < 0) |
| 1898 | save_errno = errno; |
| 1899 | Py_END_ALLOW_THREADS |
| 1900 | } |
| 1901 | return save_errno; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | static PyObject * |
| 1905 | newKqueue_Object(PyTypeObject *type, SOCKET fd) |
| 1906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | kqueue_queue_Object *self; |
| 1908 | assert(type != NULL && type->tp_alloc != NULL); |
| 1909 | self = (kqueue_queue_Object *) type->tp_alloc(type, 0); |
| 1910 | if (self == NULL) { |
| 1911 | return NULL; |
| 1912 | } |
| 1913 | |
| 1914 | if (fd == -1) { |
| 1915 | Py_BEGIN_ALLOW_THREADS |
| 1916 | self->kqfd = kqueue(); |
| 1917 | Py_END_ALLOW_THREADS |
| 1918 | } |
| 1919 | else { |
| 1920 | self->kqfd = fd; |
| 1921 | } |
| 1922 | if (self->kqfd < 0) { |
| 1923 | Py_DECREF(self); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1924 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | return NULL; |
| 1926 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1927 | |
| 1928 | if (fd == -1) { |
| 1929 | if (_Py_set_inheritable(self->kqfd, 0, NULL) < 0) { |
| 1930 | Py_DECREF(self); |
| 1931 | return NULL; |
| 1932 | } |
| 1933 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | return (PyObject *)self; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | static PyObject * |
| 1938 | kqueue_queue_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1940 | if ((args != NULL && PyObject_Size(args)) || |
| 1941 | (kwds != NULL && PyObject_Size(kwds))) { |
| 1942 | PyErr_SetString(PyExc_ValueError, |
| 1943 | "select.kqueue doesn't accept arguments"); |
| 1944 | return NULL; |
| 1945 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1947 | return newKqueue_Object(type, -1); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | static void |
| 1951 | kqueue_queue_dealloc(kqueue_queue_Object *self) |
| 1952 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | kqueue_queue_internal_close(self); |
| 1954 | Py_TYPE(self)->tp_free(self); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | static PyObject* |
| 1958 | kqueue_queue_close(kqueue_queue_Object *self) |
| 1959 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | errno = kqueue_queue_internal_close(self); |
| 1961 | if (errno < 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1962 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1963 | return NULL; |
| 1964 | } |
| 1965 | Py_RETURN_NONE; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | PyDoc_STRVAR(kqueue_queue_close_doc, |
| 1969 | "close() -> None\n\ |
| 1970 | \n\ |
| 1971 | Close the kqueue control file descriptor. Further operations on the kqueue\n\ |
| 1972 | object will raise an exception."); |
| 1973 | |
| 1974 | static PyObject* |
| 1975 | kqueue_queue_get_closed(kqueue_queue_Object *self) |
| 1976 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1977 | if (self->kqfd < 0) |
| 1978 | Py_RETURN_TRUE; |
| 1979 | else |
| 1980 | Py_RETURN_FALSE; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | static PyObject* |
| 1984 | kqueue_queue_fileno(kqueue_queue_Object *self) |
| 1985 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | if (self->kqfd < 0) |
| 1987 | return kqueue_queue_err_closed(); |
| 1988 | return PyLong_FromLong(self->kqfd); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | PyDoc_STRVAR(kqueue_queue_fileno_doc, |
| 1992 | "fileno() -> int\n\ |
| 1993 | \n\ |
| 1994 | Return the kqueue control file descriptor."); |
| 1995 | |
| 1996 | static PyObject* |
| 1997 | kqueue_queue_fromfd(PyObject *cls, PyObject *args) |
| 1998 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | SOCKET fd; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | if (!PyArg_ParseTuple(args, "i:fromfd", &fd)) |
| 2002 | return NULL; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | return newKqueue_Object((PyTypeObject*)cls, fd); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | PyDoc_STRVAR(kqueue_queue_fromfd_doc, |
| 2008 | "fromfd(fd) -> kqueue\n\ |
| 2009 | \n\ |
| 2010 | Create a kqueue object from a given control fd."); |
| 2011 | |
| 2012 | static PyObject * |
| 2013 | kqueue_queue_control(kqueue_queue_Object *self, PyObject *args) |
| 2014 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | int nevents = 0; |
| 2016 | int gotevents = 0; |
| 2017 | int nchanges = 0; |
| 2018 | int i = 0; |
| 2019 | PyObject *otimeout = NULL; |
| 2020 | PyObject *ch = NULL; |
| 2021 | PyObject *it = NULL, *ei = NULL; |
| 2022 | PyObject *result = NULL; |
| 2023 | struct kevent *evl = NULL; |
| 2024 | struct kevent *chl = NULL; |
Victor Stinner | d327f9d | 2012-03-13 15:29:08 +0100 | [diff] [blame] | 2025 | struct timespec timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | struct timespec *ptimeoutspec; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | if (self->kqfd < 0) |
| 2029 | return kqueue_queue_err_closed(); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 | if (!PyArg_ParseTuple(args, "Oi|O:control", &ch, &nevents, &otimeout)) |
| 2032 | return NULL; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2034 | if (nevents < 0) { |
| 2035 | PyErr_Format(PyExc_ValueError, |
| 2036 | "Length of eventlist must be 0 or positive, got %d", |
| 2037 | nevents); |
| 2038 | return NULL; |
| 2039 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | if (otimeout == Py_None || otimeout == NULL) { |
| 2042 | ptimeoutspec = NULL; |
| 2043 | } |
| 2044 | else if (PyNumber_Check(otimeout)) { |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 2045 | if (_PyTime_ObjectToTimespec(otimeout, &timeout.tv_sec, |
| 2046 | &timeout.tv_nsec, _PyTime_ROUND_UP) == -1) |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2047 | return NULL; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2048 | |
Victor Stinner | 5d272cc | 2012-03-13 13:35:55 +0100 | [diff] [blame] | 2049 | if (timeout.tv_sec < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | PyErr_SetString(PyExc_ValueError, |
| 2051 | "timeout must be positive or None"); |
| 2052 | return NULL; |
| 2053 | } |
Victor Stinner | d528b01 | 2012-03-13 16:25:35 +0100 | [diff] [blame] | 2054 | ptimeoutspec = &timeout; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | } |
| 2056 | else { |
| 2057 | PyErr_Format(PyExc_TypeError, |
| 2058 | "timeout argument must be an number " |
| 2059 | "or None, got %.200s", |
| 2060 | Py_TYPE(otimeout)->tp_name); |
| 2061 | return NULL; |
| 2062 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | if (ch != NULL && ch != Py_None) { |
| 2065 | it = PyObject_GetIter(ch); |
| 2066 | if (it == NULL) { |
| 2067 | PyErr_SetString(PyExc_TypeError, |
| 2068 | "changelist is not iterable"); |
| 2069 | return NULL; |
| 2070 | } |
| 2071 | nchanges = PyObject_Size(ch); |
| 2072 | if (nchanges < 0) { |
| 2073 | goto error; |
| 2074 | } |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 2075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | chl = PyMem_New(struct kevent, nchanges); |
| 2077 | if (chl == NULL) { |
| 2078 | PyErr_NoMemory(); |
| 2079 | goto error; |
| 2080 | } |
| 2081 | i = 0; |
| 2082 | while ((ei = PyIter_Next(it)) != NULL) { |
| 2083 | if (!kqueue_event_Check(ei)) { |
| 2084 | Py_DECREF(ei); |
| 2085 | PyErr_SetString(PyExc_TypeError, |
| 2086 | "changelist must be an iterable of " |
| 2087 | "select.kevent objects"); |
| 2088 | goto error; |
| 2089 | } else { |
| 2090 | chl[i++] = ((kqueue_event_Object *)ei)->e; |
| 2091 | } |
| 2092 | Py_DECREF(ei); |
| 2093 | } |
| 2094 | } |
| 2095 | Py_CLEAR(it); |
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 | /* event list */ |
| 2098 | if (nevents) { |
| 2099 | evl = PyMem_New(struct kevent, nevents); |
| 2100 | if (evl == NULL) { |
| 2101 | PyErr_NoMemory(); |
| 2102 | goto error; |
| 2103 | } |
| 2104 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 | Py_BEGIN_ALLOW_THREADS |
| 2107 | gotevents = kevent(self->kqfd, chl, nchanges, |
| 2108 | evl, nevents, ptimeoutspec); |
| 2109 | Py_END_ALLOW_THREADS |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | if (gotevents == -1) { |
| 2112 | PyErr_SetFromErrno(PyExc_OSError); |
| 2113 | goto error; |
| 2114 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | result = PyList_New(gotevents); |
| 2117 | if (result == NULL) { |
| 2118 | goto error; |
| 2119 | } |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | for (i = 0; i < gotevents; i++) { |
| 2122 | kqueue_event_Object *ch; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | ch = PyObject_New(kqueue_event_Object, &kqueue_event_Type); |
| 2125 | if (ch == NULL) { |
| 2126 | goto error; |
| 2127 | } |
| 2128 | ch->e = evl[i]; |
| 2129 | PyList_SET_ITEM(result, i, (PyObject *)ch); |
| 2130 | } |
| 2131 | PyMem_Free(chl); |
| 2132 | PyMem_Free(evl); |
| 2133 | return result; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2134 | |
| 2135 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 | PyMem_Free(chl); |
| 2137 | PyMem_Free(evl); |
| 2138 | Py_XDECREF(result); |
| 2139 | Py_XDECREF(it); |
| 2140 | return NULL; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | PyDoc_STRVAR(kqueue_queue_control_doc, |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 2144 | "control(changelist, max_events[, timeout=None]) -> eventlist\n\ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2145 | \n\ |
| 2146 | Calls the kernel kevent function.\n\ |
| 2147 | - changelist must be a list of kevent objects describing the changes\n\ |
| 2148 | to be made to the kernel's watch list or None.\n\ |
| 2149 | - max_events lets you specify the maximum number of events that the\n\ |
| 2150 | kernel will return.\n\ |
| 2151 | - timeout is the maximum time to wait in seconds, or else None,\n\ |
| 2152 | to wait forever. timeout accepts floats for smaller timeouts, too."); |
| 2153 | |
| 2154 | |
| 2155 | static PyMethodDef kqueue_queue_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2156 | {"fromfd", (PyCFunction)kqueue_queue_fromfd, |
| 2157 | METH_VARARGS | METH_CLASS, kqueue_queue_fromfd_doc}, |
| 2158 | {"close", (PyCFunction)kqueue_queue_close, METH_NOARGS, |
| 2159 | kqueue_queue_close_doc}, |
| 2160 | {"fileno", (PyCFunction)kqueue_queue_fileno, METH_NOARGS, |
| 2161 | kqueue_queue_fileno_doc}, |
| 2162 | {"control", (PyCFunction)kqueue_queue_control, |
| 2163 | METH_VARARGS , kqueue_queue_control_doc}, |
| 2164 | {NULL, NULL}, |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2165 | }; |
| 2166 | |
| 2167 | static PyGetSetDef kqueue_queue_getsetlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | {"closed", (getter)kqueue_queue_get_closed, NULL, |
| 2169 | "True if the kqueue handler is closed"}, |
| 2170 | {0}, |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2171 | }; |
| 2172 | |
| 2173 | PyDoc_STRVAR(kqueue_queue_doc, |
| 2174 | "Kqueue syscall wrapper.\n\ |
| 2175 | \n\ |
| 2176 | For example, to start watching a socket for input:\n\ |
| 2177 | >>> kq = kqueue()\n\ |
| 2178 | >>> sock = socket()\n\ |
| 2179 | >>> sock.connect((host, port))\n\ |
| 2180 | >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_ADD)], 0)\n\ |
| 2181 | \n\ |
| 2182 | To wait one second for it to become writeable:\n\ |
| 2183 | >>> kq.control(None, 1, 1000)\n\ |
| 2184 | \n\ |
| 2185 | To stop listening:\n\ |
| 2186 | >>> kq.control([kevent(sock, KQ_FILTER_WRITE, KQ_EV_DELETE)], 0)"); |
| 2187 | |
| 2188 | static PyTypeObject kqueue_queue_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2190 | "select.kqueue", /* tp_name */ |
| 2191 | sizeof(kqueue_queue_Object), /* tp_basicsize */ |
| 2192 | 0, /* tp_itemsize */ |
| 2193 | (destructor)kqueue_queue_dealloc, /* tp_dealloc */ |
| 2194 | 0, /* tp_print */ |
| 2195 | 0, /* tp_getattr */ |
| 2196 | 0, /* tp_setattr */ |
| 2197 | 0, /* tp_reserved */ |
| 2198 | 0, /* tp_repr */ |
| 2199 | 0, /* tp_as_number */ |
| 2200 | 0, /* tp_as_sequence */ |
| 2201 | 0, /* tp_as_mapping */ |
| 2202 | 0, /* tp_hash */ |
| 2203 | 0, /* tp_call */ |
| 2204 | 0, /* tp_str */ |
| 2205 | 0, /* tp_getattro */ |
| 2206 | 0, /* tp_setattro */ |
| 2207 | 0, /* tp_as_buffer */ |
| 2208 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2209 | kqueue_queue_doc, /* tp_doc */ |
| 2210 | 0, /* tp_traverse */ |
| 2211 | 0, /* tp_clear */ |
| 2212 | 0, /* tp_richcompare */ |
| 2213 | 0, /* tp_weaklistoffset */ |
| 2214 | 0, /* tp_iter */ |
| 2215 | 0, /* tp_iternext */ |
| 2216 | kqueue_queue_methods, /* tp_methods */ |
| 2217 | 0, /* tp_members */ |
| 2218 | kqueue_queue_getsetlist, /* tp_getset */ |
| 2219 | 0, /* tp_base */ |
| 2220 | 0, /* tp_dict */ |
| 2221 | 0, /* tp_descr_get */ |
| 2222 | 0, /* tp_descr_set */ |
| 2223 | 0, /* tp_dictoffset */ |
| 2224 | 0, /* tp_init */ |
| 2225 | 0, /* tp_alloc */ |
| 2226 | kqueue_queue_new, /* tp_new */ |
| 2227 | 0, /* tp_free */ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2228 | }; |
| 2229 | |
| 2230 | #endif /* HAVE_KQUEUE */ |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 2231 | |
| 2232 | |
| 2233 | |
| 2234 | |
| 2235 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2236 | /* ************************************************************************ */ |
| 2237 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2238 | PyDoc_STRVAR(select_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 2239 | "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\ |
| 2240 | \n\ |
| 2241 | 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] | 2242 | 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] | 2243 | rlist -- wait until ready for reading\n\ |
| 2244 | wlist -- wait until ready for writing\n\ |
| 2245 | xlist -- wait for an ``exceptional condition''\n\ |
| 2246 | If only one kind of condition is required, pass [] for the other lists.\n\ |
| 2247 | A file descriptor is either a socket or file object, or a small integer\n\ |
| 2248 | gotten from a fileno() method call on one of those.\n\ |
| 2249 | \n\ |
| 2250 | The optional 4th argument specifies a timeout in seconds; it may be\n\ |
| 2251 | a floating point number to specify fractions of seconds. If it is absent\n\ |
| 2252 | or None, the call will never time out.\n\ |
| 2253 | \n\ |
| 2254 | The return value is a tuple of three lists corresponding to the first three\n\ |
| 2255 | arguments; each contains the subset of the corresponding file descriptors\n\ |
| 2256 | that are ready.\n\ |
| 2257 | \n\ |
| 2258 | *** IMPORTANT NOTICE ***\n\ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 2259 | On Windows only sockets are supported; on Unix, all file\n\ |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 2260 | descriptors can be used."); |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 2261 | |
Barry Warsaw | e4ac0aa | 1996-12-12 00:04:35 +0000 | [diff] [blame] | 2262 | static PyMethodDef select_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | {"select", select_select, METH_VARARGS, select_doc}, |
Charles-François Natali | 986a56c | 2013-01-19 12:19:10 +0100 | [diff] [blame] | 2264 | #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | {"poll", select_poll, METH_NOARGS, poll_doc}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2266 | #endif /* HAVE_POLL */ |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 2267 | #ifdef HAVE_SYS_DEVPOLL_H |
| 2268 | {"devpoll", select_devpoll, METH_NOARGS, devpoll_doc}, |
| 2269 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | {0, 0}, /* sentinel */ |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2271 | }; |
| 2272 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2273 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | 1d8fb2d | 1998-06-28 16:54:49 +0000 | [diff] [blame] | 2274 | "This module supports asynchronous I/O on multiple file descriptors.\n\ |
| 2275 | \n\ |
| 2276 | *** IMPORTANT NOTICE ***\n\ |
Christian Heimes | af01f66 | 2013-12-21 16:19:10 +0100 | [diff] [blame] | 2277 | On Windows only sockets are supported; on Unix, all file descriptors."); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2278 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2279 | |
| 2280 | static struct PyModuleDef selectmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | PyModuleDef_HEAD_INIT, |
| 2282 | "select", |
| 2283 | module_doc, |
| 2284 | -1, |
| 2285 | select_methods, |
| 2286 | NULL, |
| 2287 | NULL, |
| 2288 | NULL, |
| 2289 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2290 | }; |
| 2291 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 2292 | |
| 2293 | |
| 2294 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 2295 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2296 | PyInit_select(void) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2297 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | PyObject *m; |
| 2299 | m = PyModule_Create(&selectmodule); |
| 2300 | if (m == NULL) |
| 2301 | return NULL; |
Fred Drake | 4baedc1 | 2002-04-01 14:53:37 +0000 | [diff] [blame] | 2302 | |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 2303 | Py_INCREF(PyExc_OSError); |
| 2304 | PyModule_AddObject(m, "error", PyExc_OSError); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2305 | |
Amaury Forgeot d'Arc | ace3102 | 2009-07-09 22:44:11 +0000 | [diff] [blame] | 2306 | #ifdef PIPE_BUF |
R. David Murray | e16cda9 | 2010-10-15 23:12:57 +0000 | [diff] [blame] | 2307 | #ifdef HAVE_BROKEN_PIPE_BUF |
| 2308 | #undef PIPE_BUF |
| 2309 | #define PIPE_BUF 512 |
| 2310 | #endif |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2311 | PyModule_AddIntMacro(m, PIPE_BUF); |
Amaury Forgeot d'Arc | ace3102 | 2009-07-09 22:44:11 +0000 | [diff] [blame] | 2312 | #endif |
Gregory P. Smith | b970b86 | 2009-07-04 02:28:47 +0000 | [diff] [blame] | 2313 | |
Charles-François Natali | 986a56c | 2013-01-19 12:19:10 +0100 | [diff] [blame] | 2314 | #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2315 | #ifdef __APPLE__ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2316 | if (select_have_broken_poll()) { |
| 2317 | if (PyObject_DelAttrString(m, "poll") == -1) { |
| 2318 | PyErr_Clear(); |
| 2319 | } |
| 2320 | } else { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2321 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2322 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2323 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | if (PyType_Ready(&poll_Type) < 0) |
| 2325 | return NULL; |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2326 | PyModule_AddIntMacro(m, POLLIN); |
| 2327 | PyModule_AddIntMacro(m, POLLPRI); |
| 2328 | PyModule_AddIntMacro(m, POLLOUT); |
| 2329 | PyModule_AddIntMacro(m, POLLERR); |
| 2330 | PyModule_AddIntMacro(m, POLLHUP); |
| 2331 | PyModule_AddIntMacro(m, POLLNVAL); |
Andrew M. Kuchling | cf96dc8 | 2000-08-25 01:15:33 +0000 | [diff] [blame] | 2332 | |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2333 | #ifdef POLLRDNORM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2334 | PyModule_AddIntMacro(m, POLLRDNORM); |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2335 | #endif |
| 2336 | #ifdef POLLRDBAND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2337 | PyModule_AddIntMacro(m, POLLRDBAND); |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2338 | #endif |
| 2339 | #ifdef POLLWRNORM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2340 | PyModule_AddIntMacro(m, POLLWRNORM); |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2341 | #endif |
| 2342 | #ifdef POLLWRBAND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2343 | PyModule_AddIntMacro(m, POLLWRBAND); |
Andrew M. Kuchling | 36d97eb | 2000-09-28 21:33:44 +0000 | [diff] [blame] | 2344 | #endif |
Sjoerd Mullender | 239f836 | 2000-08-25 13:59:18 +0000 | [diff] [blame] | 2345 | #ifdef POLLMSG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2346 | PyModule_AddIntMacro(m, POLLMSG); |
Sjoerd Mullender | 239f836 | 2000-08-25 13:59:18 +0000 | [diff] [blame] | 2347 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2349 | #endif /* HAVE_POLL */ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2350 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 2351 | #ifdef HAVE_SYS_DEVPOLL_H |
| 2352 | if (PyType_Ready(&devpoll_Type) < 0) |
| 2353 | return NULL; |
| 2354 | #endif |
| 2355 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2356 | #ifdef HAVE_EPOLL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | Py_TYPE(&pyEpoll_Type) = &PyType_Type; |
| 2358 | if (PyType_Ready(&pyEpoll_Type) < 0) |
| 2359 | return NULL; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | Py_INCREF(&pyEpoll_Type); |
| 2362 | PyModule_AddObject(m, "epoll", (PyObject *) &pyEpoll_Type); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2363 | |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2364 | PyModule_AddIntMacro(m, EPOLLIN); |
| 2365 | PyModule_AddIntMacro(m, EPOLLOUT); |
| 2366 | PyModule_AddIntMacro(m, EPOLLPRI); |
| 2367 | PyModule_AddIntMacro(m, EPOLLERR); |
| 2368 | PyModule_AddIntMacro(m, EPOLLHUP); |
| 2369 | PyModule_AddIntMacro(m, EPOLLET); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2370 | #ifdef EPOLLONESHOT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | /* Kernel 2.6.2+ */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2372 | PyModule_AddIntMacro(m, EPOLLONESHOT); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2373 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | /* PyModule_AddIntConstant(m, "EPOLL_RDHUP", EPOLLRDHUP); */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2375 | PyModule_AddIntMacro(m, EPOLLRDNORM); |
| 2376 | PyModule_AddIntMacro(m, EPOLLRDBAND); |
| 2377 | PyModule_AddIntMacro(m, EPOLLWRNORM); |
| 2378 | PyModule_AddIntMacro(m, EPOLLWRBAND); |
| 2379 | PyModule_AddIntMacro(m, EPOLLMSG); |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 2380 | |
Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 2381 | #ifdef EPOLL_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 2382 | PyModule_AddIntMacro(m, EPOLL_CLOEXEC); |
Benjamin Peterson | 95c1662 | 2011-12-27 15:36:32 -0600 | [diff] [blame] | 2383 | #endif |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2384 | #endif /* HAVE_EPOLL */ |
| 2385 | |
| 2386 | #ifdef HAVE_KQUEUE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | kqueue_event_Type.tp_new = PyType_GenericNew; |
| 2388 | Py_TYPE(&kqueue_event_Type) = &PyType_Type; |
| 2389 | if(PyType_Ready(&kqueue_event_Type) < 0) |
| 2390 | return NULL; |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | Py_INCREF(&kqueue_event_Type); |
| 2393 | PyModule_AddObject(m, "kevent", (PyObject *)&kqueue_event_Type); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | Py_TYPE(&kqueue_queue_Type) = &PyType_Type; |
| 2396 | if(PyType_Ready(&kqueue_queue_Type) < 0) |
| 2397 | return NULL; |
| 2398 | Py_INCREF(&kqueue_queue_Type); |
| 2399 | PyModule_AddObject(m, "kqueue", (PyObject *)&kqueue_queue_Type); |
| 2400 | |
| 2401 | /* event filters */ |
| 2402 | PyModule_AddIntConstant(m, "KQ_FILTER_READ", EVFILT_READ); |
| 2403 | PyModule_AddIntConstant(m, "KQ_FILTER_WRITE", EVFILT_WRITE); |
| 2404 | PyModule_AddIntConstant(m, "KQ_FILTER_AIO", EVFILT_AIO); |
| 2405 | PyModule_AddIntConstant(m, "KQ_FILTER_VNODE", EVFILT_VNODE); |
| 2406 | PyModule_AddIntConstant(m, "KQ_FILTER_PROC", EVFILT_PROC); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2407 | #ifdef EVFILT_NETDEV |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | PyModule_AddIntConstant(m, "KQ_FILTER_NETDEV", EVFILT_NETDEV); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2409 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | PyModule_AddIntConstant(m, "KQ_FILTER_SIGNAL", EVFILT_SIGNAL); |
| 2411 | PyModule_AddIntConstant(m, "KQ_FILTER_TIMER", EVFILT_TIMER); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2413 | /* event flags */ |
| 2414 | PyModule_AddIntConstant(m, "KQ_EV_ADD", EV_ADD); |
| 2415 | PyModule_AddIntConstant(m, "KQ_EV_DELETE", EV_DELETE); |
| 2416 | PyModule_AddIntConstant(m, "KQ_EV_ENABLE", EV_ENABLE); |
| 2417 | PyModule_AddIntConstant(m, "KQ_EV_DISABLE", EV_DISABLE); |
| 2418 | PyModule_AddIntConstant(m, "KQ_EV_ONESHOT", EV_ONESHOT); |
| 2419 | PyModule_AddIntConstant(m, "KQ_EV_CLEAR", EV_CLEAR); |
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 | PyModule_AddIntConstant(m, "KQ_EV_SYSFLAGS", EV_SYSFLAGS); |
| 2422 | PyModule_AddIntConstant(m, "KQ_EV_FLAG1", EV_FLAG1); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | PyModule_AddIntConstant(m, "KQ_EV_EOF", EV_EOF); |
| 2425 | PyModule_AddIntConstant(m, "KQ_EV_ERROR", EV_ERROR); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2427 | /* READ WRITE filter flag */ |
| 2428 | PyModule_AddIntConstant(m, "KQ_NOTE_LOWAT", NOTE_LOWAT); |
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 | /* VNODE filter flags */ |
| 2431 | PyModule_AddIntConstant(m, "KQ_NOTE_DELETE", NOTE_DELETE); |
| 2432 | PyModule_AddIntConstant(m, "KQ_NOTE_WRITE", NOTE_WRITE); |
| 2433 | PyModule_AddIntConstant(m, "KQ_NOTE_EXTEND", NOTE_EXTEND); |
| 2434 | PyModule_AddIntConstant(m, "KQ_NOTE_ATTRIB", NOTE_ATTRIB); |
| 2435 | PyModule_AddIntConstant(m, "KQ_NOTE_LINK", NOTE_LINK); |
| 2436 | PyModule_AddIntConstant(m, "KQ_NOTE_RENAME", NOTE_RENAME); |
| 2437 | PyModule_AddIntConstant(m, "KQ_NOTE_REVOKE", NOTE_REVOKE); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | /* PROC filter flags */ |
| 2440 | PyModule_AddIntConstant(m, "KQ_NOTE_EXIT", NOTE_EXIT); |
| 2441 | PyModule_AddIntConstant(m, "KQ_NOTE_FORK", NOTE_FORK); |
| 2442 | PyModule_AddIntConstant(m, "KQ_NOTE_EXEC", NOTE_EXEC); |
| 2443 | PyModule_AddIntConstant(m, "KQ_NOTE_PCTRLMASK", NOTE_PCTRLMASK); |
| 2444 | PyModule_AddIntConstant(m, "KQ_NOTE_PDATAMASK", NOTE_PDATAMASK); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2446 | PyModule_AddIntConstant(m, "KQ_NOTE_TRACK", NOTE_TRACK); |
| 2447 | PyModule_AddIntConstant(m, "KQ_NOTE_CHILD", NOTE_CHILD); |
| 2448 | PyModule_AddIntConstant(m, "KQ_NOTE_TRACKERR", NOTE_TRACKERR); |
| 2449 | |
| 2450 | /* NETDEV filter flags */ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2451 | #ifdef EVFILT_NETDEV |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2452 | PyModule_AddIntConstant(m, "KQ_NOTE_LINKUP", NOTE_LINKUP); |
| 2453 | PyModule_AddIntConstant(m, "KQ_NOTE_LINKDOWN", NOTE_LINKDOWN); |
| 2454 | PyModule_AddIntConstant(m, "KQ_NOTE_LINKINV", NOTE_LINKINV); |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 2455 | #endif |
| 2456 | |
| 2457 | #endif /* HAVE_KQUEUE */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | return m; |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 2459 | } |