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