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