Annotate

util-src/poll.c @ 13553:850e4ade7a01

net.server_epoll: Make running out of buffer space a fatal error Prevent Bad Things from happening when the buffer gets full. This of course opens up the possibility of intentionally killing connections by sending much stuff, which need to be mitigated with rate limits elsewhere.
author Kim Alvefur <zash@zash.se>
date Sat, 09 Nov 2024 15:42:31 +0100
parent 13347:5fe8a8e16b27
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 * Lua polling library
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
4 * Copyright (C) 2017-2022 Kim Alvefur
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 *
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 * This project is MIT licensed. Please see the
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 * COPYING file in the source package for more information.
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 *
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 #include <string.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 #include <errno.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
14 #if defined(__linux__)
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 #define USE_EPOLL
12315
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
16 #define POLL_BACKEND "epoll"
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
17 #elif defined(__unix__)
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
18 #define USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
19 #define POLL_BACKEND "poll"
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
20 #else
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
21 #define USE_SELECT
12315
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
22 #define POLL_BACKEND "select"
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 #ifdef USE_EPOLL
12884
f5a75aaa8a25 util.poll: Include unistd.h only for epoll
Kim Alvefur <zash@zash.se>
parents: 12575
diff changeset
26 #include <unistd.h>
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 #include <sys/epoll.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 #ifndef MAX_EVENTS
13347
5fe8a8e16b27 util.poll: Rename things to clarify poll(2) limits
Kim Alvefur <zash@zash.se>
parents: 13335
diff changeset
29 /* Maximum number of returned events, retrieved into Lpoll_state */
13335
8b3bf0d2ffd4 util.poll: Quadruple number of events retrieved at once from epoll
Kim Alvefur <zash@zash.se>
parents: 13329
diff changeset
30 #define MAX_EVENTS 256
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
32 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
33 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
34 #include <poll.h>
13347
5fe8a8e16b27 util.poll: Rename things to clarify poll(2) limits
Kim Alvefur <zash@zash.se>
parents: 13335
diff changeset
35 #ifndef MAX_WATCHED
5fe8a8e16b27 util.poll: Rename things to clarify poll(2) limits
Kim Alvefur <zash@zash.se>
parents: 13335
diff changeset
36 /* Maximum number of watched sockets, kept in Lpoll_state */
5fe8a8e16b27 util.poll: Rename things to clarify poll(2) limits
Kim Alvefur <zash@zash.se>
parents: 13335
diff changeset
37 #define MAX_WATCHED 10000
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
38 #endif
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
39 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
40 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 #include <sys/select.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 #include <lualib.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 #include <lauxlib.h>
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46
12315
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
47 #define STATE_MT "util.poll<" POLL_BACKEND ">"
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
49 #if (LUA_VERSION_NUM < 504)
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
50 #define luaL_pushfail lua_pushnil
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
51 #endif
9318
3429006518bf util.poll: Lua 5.1 compat
Kim Alvefur <zash@zash.se>
parents: 9317
diff changeset
52
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 * Structure to keep state for each type of API
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 typedef struct Lpoll_state {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 int processed;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 int epoll_fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 struct epoll_event events[MAX_EVENTS];
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
61 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
62 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
63 nfds_t count;
13347
5fe8a8e16b27 util.poll: Rename things to clarify poll(2) limits
Kim Alvefur <zash@zash.se>
parents: 13335
diff changeset
64 struct pollfd events[MAX_WATCHED];
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
65 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
66 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 fd_set wantread;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 fd_set wantwrite;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 fd_set readable;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 fd_set writable;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 fd_set all;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 fd_set err;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 } Lpoll_state;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 * Add an FD to be watched
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
79 static int Ladd(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 int fd = luaL_checkinteger(L, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 int wantread = lua_toboolean(L, 3);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 int wantwrite = lua_toboolean(L, 4);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85
9447
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
86 if(fd < 0) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
87 luaL_pushfail(L);
9447
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
88 lua_pushstring(L, strerror(EBADF));
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
89 lua_pushinteger(L, EBADF);
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
90 return 3;
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
91 }
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
92
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, fd, &event);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 if(ret < 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 ret = errno;
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
104 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
113 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
114 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
115
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
116 for(nfds_t i = 0; i < state->count; i++) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
117 if(state->events[i].fd == fd) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
118 luaL_pushfail(L);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
119 lua_pushstring(L, strerror(EEXIST));
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
120 lua_pushinteger(L, EEXIST);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
121 return 3;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
122 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
123 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
124
13347
5fe8a8e16b27 util.poll: Rename things to clarify poll(2) limits
Kim Alvefur <zash@zash.se>
parents: 13335
diff changeset
125 if(state->count >= MAX_WATCHED) {
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
126 luaL_pushfail(L);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
127 lua_pushstring(L, strerror(EMFILE));
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
128 lua_pushinteger(L, EMFILE);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
129 return 3;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
130 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
131
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
132 state->events[state->count].fd = fd;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
133 state->events[state->count].events = (wantread ? POLLIN : 0) | (wantwrite ? POLLOUT : 0);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
134 state->events[state->count].revents = 0;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
135 state->count++;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
136
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
137 lua_pushboolean(L, 1);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
138 return 1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
139 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
140 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141
9447
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
142 if(fd > FD_SETSIZE) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
143 luaL_pushfail(L);
9447
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
144 lua_pushstring(L, strerror(EBADF));
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
145 lua_pushinteger(L, EBADF);
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
146 return 3;
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
147 }
6397e965a22d net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents: 9446
diff changeset
148
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 if(FD_ISSET(fd, &state->all)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
150 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 lua_pushstring(L, strerror(EEXIST));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 lua_pushinteger(L, EEXIST);
9446
6b4d28eb19cf util.poll: Fix missing return for adding duplicate FD
Kim Alvefur <zash@zash.se>
parents: 9440
diff changeset
153 return 3;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 FD_SET(fd, &state->all);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
161
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 if(wantread) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163 FD_SET(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 if(wantwrite) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170 FD_SET(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 * Set events to watch for, readable and/or writable
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
184 static int Lset(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 int fd = luaL_checkinteger(L, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 int wantread = lua_toboolean(L, 3);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 int wantwrite = lua_toboolean(L, 4);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_MOD, fd, &event);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 if(ret == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 ret = errno;
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
207 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
211 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
212
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
213 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
214 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
215 int wantread = lua_toboolean(L, 3);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
216 int wantwrite = lua_toboolean(L, 4);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
217
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
218 for(nfds_t i = 0; i < state->count; i++) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
219 struct pollfd *event = &state->events[i];
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
220
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
221 if(event->fd == fd) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
222 event->events = (wantread ? POLLIN : 0) | (wantwrite ? POLLOUT : 0);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
223 lua_pushboolean(L, 1);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
224 return 1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
225 } else if(event->fd == -1) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
226 break;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
227 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
228 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
229
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
230 luaL_pushfail(L);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
231 lua_pushstring(L, strerror(ENOENT));
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
232 lua_pushinteger(L, ENOENT);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
233 return 3;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
234 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
235 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
236
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
237 if(!FD_ISSET(fd, &state->all)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
238 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
239 lua_pushstring(L, strerror(ENOENT));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
240 lua_pushinteger(L, ENOENT);
10096
46a7792fdac5 util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents: 9954
diff changeset
241 return 3;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
242 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
243
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 if(!lua_isnoneornil(L, 3)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
245 if(lua_toboolean(L, 3)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 FD_SET(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
248 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
250 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
251 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 if(!lua_isnoneornil(L, 4)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 if(lua_toboolean(L, 4)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 FD_SET(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
259 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
267 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
268 * Remove FDs
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
270 static int Ldel(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 int fd = luaL_checkinteger(L, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
274 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
278
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
279 int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_DEL, fd, &event);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 if(ret == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 ret = errno;
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
287 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
291 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
292
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
293 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
294 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
295
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
296 if(state->count == 0) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
297 luaL_pushfail(L);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
298 lua_pushstring(L, strerror(ENOENT));
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
299 lua_pushinteger(L, ENOENT);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
300 return 3;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
301 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
302
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
303 /*
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
304 * Move the last item on top of the removed one
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
305 */
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
306 struct pollfd *last = &state->events[state->count - 1];
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
307
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
308 for(nfds_t i = 0; i < state->count; i++) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
309 struct pollfd *event = &state->events[i];
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
310
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
311 if(event->fd == fd) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
312 event->fd = last->fd;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
313 event->events = last->events;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
314 event->revents = last->revents;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
315 last->fd = -1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
316 state->count--;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
317
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
318 lua_pushboolean(L, 1);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
319 return 1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
320 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
321 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
322
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
323 luaL_pushfail(L);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
324 lua_pushstring(L, strerror(ENOENT));
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
325 lua_pushinteger(L, ENOENT);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
326 return 3;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
327 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
328 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
329
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
330 if(!FD_ISSET(fd, &state->all)) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
331 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
332 lua_pushstring(L, strerror(ENOENT));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
333 lua_pushinteger(L, ENOENT);
10096
46a7792fdac5 util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents: 9954
diff changeset
334 return 3;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
335 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
337 FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
338 FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
339 FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
340 FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
341 FD_CLR(fd, &state->all);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
342 FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
343
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
345 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
348
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
349
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
350 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
351 * Check previously manipulated event state for FDs ready for reading or writing
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
353 static int Lpushevent(lua_State *L, struct Lpoll_state *state) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 if(state->processed > 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357 state->processed--;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 struct epoll_event event = state->events[state->processed];
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 lua_pushinteger(L, event.data.fd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 lua_pushboolean(L, event.events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 lua_pushboolean(L, event.events & EPOLLOUT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
362 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
363 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
364
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
365 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
366 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
367
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
368 for(int i = state->processed - 1; i >= 0; i--) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
369 struct pollfd *event = &state->events[i];
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
370
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
371 if(event->fd != -1 && event->revents != 0) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
372 lua_pushinteger(L, event->fd);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
373 lua_pushboolean(L, event->revents & (POLLIN | POLLHUP | POLLERR));
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
374 lua_pushboolean(L, event->revents & POLLOUT);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
375 event->revents = 0;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
376 state->processed = i;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
377 return 3;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
378 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
379 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
380
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
381 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
382 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
383
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
384 for(int fd = state->processed + 1; fd < FD_SETSIZE; fd++) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
385 if(FD_ISSET(fd, &state->readable) || FD_ISSET(fd, &state->writable) || FD_ISSET(fd, &state->err)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
386 lua_pushinteger(L, fd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
387 lua_pushboolean(L, FD_ISSET(fd, &state->readable) | FD_ISSET(fd, &state->err));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
388 lua_pushboolean(L, FD_ISSET(fd, &state->writable));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
389 FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
390 FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
391 FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
392 state->processed = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
393 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
394 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
395 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
396
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 return 0;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
399 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
400
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
402 * Wait for event
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
403 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
404 static int Lwait(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 int ret = Lpushevent(L, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
408
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
409 if(ret != 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
410 return ret;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
411 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
412
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
413 lua_Number timeout = luaL_checknumber(L, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414 luaL_argcheck(L, timeout >= 0, 1, "positive number expected");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
415
13329
649876680bf0 util.poll: Return early if given zero timeout and no pending events
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
416 if(timeout == 0.0) {
649876680bf0 util.poll: Return early if given zero timeout and no pending events
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
417 lua_pushnil(L);
649876680bf0 util.poll: Return early if given zero timeout and no pending events
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
418 lua_pushstring(L, "timeout");
649876680bf0 util.poll: Return early if given zero timeout and no pending events
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
419 return 2;
649876680bf0 util.poll: Return early if given zero timeout and no pending events
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
420 }
649876680bf0 util.poll: Return early if given zero timeout and no pending events
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
421
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
423 ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000);
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
424 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
425 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
426 ret = poll(state->events, state->count, timeout * 1000);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
427 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
428 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
429 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
430 * select(2) mutates the fd_sets passed to it so in order to not
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
431 * have to recreate it manually every time a copy is made.
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
432 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
433 memcpy(&state->readable, &state->wantread, sizeof(fd_set));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
434 memcpy(&state->writable, &state->wantwrite, sizeof(fd_set));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
435 memcpy(&state->err, &state->all, sizeof(fd_set));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
437 struct timeval tv;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
438 tv.tv_sec = (time_t)timeout;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
439 tv.tv_usec = ((suseconds_t)(timeout * 1000000)) % 1000000;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
440
9437
b202aa1e2d7b util.poll: Fix monitoring of socket exceptions in select mode
Kim Alvefur <zash@zash.se>
parents: 9318
diff changeset
441 ret = select(FD_SETSIZE, &state->readable, &state->writable, &state->err, &tv);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
442 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
443
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
444 if(ret == 0) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
445 /* Is this an error? */
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
446 lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
447 lua_pushstring(L, "timeout");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
448 return 2;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
449 }
9508
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
450 else if(ret < 0 && errno == EINTR) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
451 /* Is this an error? */
9508
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
452 lua_pushnil(L);
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
453 lua_pushstring(L, "signal");
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
454 return 2;
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
455 }
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
456 else if(ret < 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
457 ret = errno;
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
458 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
459 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
460 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
461 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
462 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
463
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
464 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
465 * Search for the first ready FD and return it
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
466 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
467 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
468 state->processed = ret;
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
469 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
470 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
471 state->processed = state->count;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
472 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
473 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
474 state->processed = -1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
475 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
476 return Lpushevent(L, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
477 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
478
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
479 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
480 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
481 * Return Epoll FD
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
482 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
483 static int Lgetfd(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
484 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
485 lua_pushinteger(L, state->epoll_fd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
486 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
487 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
488
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
489 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
490 * Close epoll FD
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
491 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
492 static int Lgc(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
493 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
494
9478
bd178ed0459b util.poll: Fix inverted logic
Kim Alvefur <zash@zash.se>
parents: 9476
diff changeset
495 if(state->epoll_fd == -1) {
9475
9b26a50cdfe3 util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents: 9450
diff changeset
496 return 0;
9b26a50cdfe3 util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents: 9450
diff changeset
497 }
9b26a50cdfe3 util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents: 9450
diff changeset
498
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
499 if(close(state->epoll_fd) == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
500 state->epoll_fd = -1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
501 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
502 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
503 lua_pushstring(L, strerror(errno));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
504 lua_error(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
505 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
506
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
507 return 0;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
508 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
509 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
510
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
511 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
512 * String representation
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
513 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
514 static int Ltos(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
515 struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
516 lua_pushfstring(L, "%s: %p", STATE_MT, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
517 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
518 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
519
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
520 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
521 * Create a new context
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
522 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
523 static int Lnew(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
524 /* Allocate state */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
525 Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
526 luaL_setmetatable(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
527
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
528 /* Initialize state */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
529 #ifdef USE_EPOLL
9476
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
530 state->epoll_fd = -1;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
531 state->processed = 0;
9476
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
532
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
533 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
534
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
535 if(epoll_fd <= 0) {
10921
6eb5d2bb11af util-src: Use the luaL_pushfail API added in Lua 5.4 to highlight all failure conditions
Kim Alvefur <zash@zash.se>
parents: 10096
diff changeset
536 luaL_pushfail(L);
9476
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
537 lua_pushstring(L, strerror(errno));
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
538 lua_pushinteger(L, errno);
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
539 return 3;
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
540 }
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
541
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
542 state->epoll_fd = epoll_fd;
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
543 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
544 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
545 state->processed = -1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
546 state->count = 0;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
547
13347
5fe8a8e16b27 util.poll: Rename things to clarify poll(2) limits
Kim Alvefur <zash@zash.se>
parents: 13335
diff changeset
548 for(nfds_t i = 0; i < MAX_WATCHED; i++) {
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
549 state->events[i].fd = -1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
550 state->events[i].events = 0;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
551 state->events[i].revents = 0;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
552 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
553
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
554 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
555 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
556 FD_ZERO(&state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
557 FD_ZERO(&state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
558 FD_ZERO(&state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
559 FD_ZERO(&state->writable);
9448
352e8b75c1ae util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents: 9447
diff changeset
560 FD_ZERO(&state->all);
352e8b75c1ae util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents: 9447
diff changeset
561 FD_ZERO(&state->err);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
562 state->processed = FD_SETSIZE;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
563 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
564
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
565 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
566 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
567
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
568 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
569 * Open library
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
570 */
12976
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
571 int luaopen_prosody_util_poll(lua_State *L) {
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
572 luaL_checkversion(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
573
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
574 luaL_newmetatable(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
575 {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
576
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
577 lua_pushliteral(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
578 lua_setfield(L, -2, "__name");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
579
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
580 lua_pushcfunction(L, Ltos);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
581 lua_setfield(L, -2, "__tostring");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
582
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
583 lua_createtable(L, 0, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
584 {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
585 lua_pushcfunction(L, Ladd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
586 lua_setfield(L, -2, "add");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
587 lua_pushcfunction(L, Lset);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
588 lua_setfield(L, -2, "set");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
589 lua_pushcfunction(L, Ldel);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
590 lua_setfield(L, -2, "del");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
591 lua_pushcfunction(L, Lwait);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
592 lua_setfield(L, -2, "wait");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
593 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
594 lua_pushcfunction(L, Lgetfd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
595 lua_setfield(L, -2, "getfd");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
596 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
597 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
598 lua_setfield(L, -2, "__index");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
599
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
600 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
601 lua_pushcfunction(L, Lgc);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
602 lua_setfield(L, -2, "__gc");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
603 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
604 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
605
9507
33d21f020b66 net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents: 9506
diff changeset
606 lua_createtable(L, 0, 3);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
607 {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
608 lua_pushcfunction(L, Lnew);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
609 lua_setfield(L, -2, "new");
9506
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
610
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
611 #define push_errno(named_error) lua_pushinteger(L, named_error);\
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
612 lua_setfield(L, -2, #named_error);
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
613
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
614 push_errno(EEXIST);
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
615 push_errno(EMFILE);
9507
33d21f020b66 net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents: 9506
diff changeset
616 push_errno(ENOENT);
9506
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
617
12315
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
618 lua_pushliteral(L, POLL_BACKEND);
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
619 lua_setfield(L, -2, "api");
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
620
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
621 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
622 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
623 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
624
12976
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
625 /* COMPAT */
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
626 int luaopen_util_poll(lua_State *L) {
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
627 return luaopen_prosody_util_poll(L);
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
628 }
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
629