annotate util-src/poll.c @ 14229:ce31fdde0ad1 default tip

net.unbound: Simplify conditional
author Kim Alvefur <zash@zash.se>
date Sat, 13 Jun 2026 11:35:18 +0200
parents 75d09be04f13
children
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
14182
a331cde41c85 util.poll: Reject fd == FD_SETSIZE
Matthew Wild <mwild1@gmail.com>
parents: 13347
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
14213
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
237 if(fd >= FD_SETSIZE) {
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
238 luaL_pushfail(L);
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
239 lua_pushstring(L, strerror(EBADF));
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
240 lua_pushinteger(L, EBADF);
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
241 return 3;
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
242 }
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
243
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
244 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
245 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
246 lua_pushstring(L, strerror(ENOENT));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
247 lua_pushinteger(L, ENOENT);
10096
46a7792fdac5 util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents: 9954
diff changeset
248 return 3;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
249 }
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 if(!lua_isnoneornil(L, 3)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
252 if(lua_toboolean(L, 3)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
253 FD_SET(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
254 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
255 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
256 FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
257 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
258 }
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 if(!lua_isnoneornil(L, 4)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
261 if(lua_toboolean(L, 4)) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
262 FD_SET(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
263 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
264 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
265 FD_CLR(fd, &state->wantwrite);
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
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
269 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
270 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
272 }
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 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
275 * Remove FDs
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
276 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
277 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
278 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
279 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
280
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
282
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
283 struct epoll_event event;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
284 event.data.fd = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
285
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
286 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
287
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
288 if(ret == 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
289 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
290 return 1;
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 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
293 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
294 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
295 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
296 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
297 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
298 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
299
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
300 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
301 #ifdef USE_POLL
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 if(state->count == 0) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
304 luaL_pushfail(L);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
305 lua_pushstring(L, strerror(ENOENT));
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
306 lua_pushinteger(L, ENOENT);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
307 return 3;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
308 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
309
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 * 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
312 */
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
313 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
314
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
315 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
316 struct pollfd *event = &state->events[i];
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 if(event->fd == fd) {
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
319 event->fd = last->fd;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
320 event->events = last->events;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
321 event->revents = last->revents;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
322 last->fd = -1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
323 state->count--;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
324
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
325 lua_pushboolean(L, 1);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
326 return 1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
327 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
328 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
329
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
330 luaL_pushfail(L);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
331 lua_pushstring(L, strerror(ENOENT));
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
332 lua_pushinteger(L, ENOENT);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
333 return 3;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
334 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
335 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
336
14213
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
337 if(fd >= FD_SETSIZE) {
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
338 luaL_pushfail(L);
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
339 lua_pushstring(L, strerror(EBADF));
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
340 lua_pushinteger(L, EBADF);
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
341 return 3;
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
342 }
75d09be04f13 util.poll: Reject file descriptors outside of FD_SETSIZE in all methods
Kim Alvefur <zash@zash.se>
parents: 14182
diff changeset
343
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
344 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
345 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
346 lua_pushstring(L, strerror(ENOENT));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
347 lua_pushinteger(L, ENOENT);
10096
46a7792fdac5 util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents: 9954
diff changeset
348 return 3;
9313
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 FD_CLR(fd, &state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
352 FD_CLR(fd, &state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
353 FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
354 FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
355 FD_CLR(fd, &state->all);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
356 FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
357
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
358 lua_pushboolean(L, 1);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
359 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
360 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
361 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
362
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 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
365 * 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
366 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
367 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
368 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
369
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
370 if(state->processed > 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
371 state->processed--;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
372 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
373 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
374 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
375 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
376 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
377 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
378
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
379 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
380 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
381
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
382 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
383 struct pollfd *event = &state->events[i];
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
384
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
385 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
386 lua_pushinteger(L, event->fd);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
387 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
388 lua_pushboolean(L, event->revents & POLLOUT);
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
389 event->revents = 0;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
390 state->processed = i;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
391 return 3;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
392 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
393 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
394
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
395 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
396 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
397
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
398 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
399 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
400 lua_pushinteger(L, fd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
401 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
402 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
403 FD_CLR(fd, &state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
404 FD_CLR(fd, &state->writable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
405 FD_CLR(fd, &state->err);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
406 state->processed = fd;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
407 return 3;
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 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
410
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
411 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
412 return 0;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
413 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
414
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
415 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
416 * Wait for event
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
417 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
418 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
419 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
420
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
421 int ret = Lpushevent(L, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
422
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
423 if(ret != 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
424 return ret;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
425 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
426
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
427 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
428 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
429
13329
649876680bf0 util.poll: Return early if given zero timeout and no pending events
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
430 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
431 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
432 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
433 return 2;
649876680bf0 util.poll: Return early if given zero timeout and no pending events
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
434 }
649876680bf0 util.poll: Return early if given zero timeout and no pending events
Kim Alvefur <zash@zash.se>
parents: 12976
diff changeset
435
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
436 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
437 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
438 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
439 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
440 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
441 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
442 #ifdef USE_SELECT
9313
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 * 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
445 * 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
446 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
447 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
448 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
449 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
450
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
451 struct timeval tv;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
452 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
453 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
454
9437
b202aa1e2d7b util.poll: Fix monitoring of socket exceptions in select mode
Kim Alvefur <zash@zash.se>
parents: 9318
diff changeset
455 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
456 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
457
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
458 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
459 /* Is this an error? */
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
460 lua_pushnil(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
461 lua_pushstring(L, "timeout");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
462 return 2;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
463 }
9508
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
464 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
465 /* Is this an error? */
9508
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
466 lua_pushnil(L);
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
467 lua_pushstring(L, "signal");
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
468 return 2;
2055b497b515 net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents: 9507
diff changeset
469 }
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
470 else if(ret < 0) {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
471 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
472 luaL_pushfail(L);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
473 lua_pushstring(L, strerror(ret));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
474 lua_pushinteger(L, ret);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
475 return 3;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
476 }
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 * 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
480 */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
481 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
482 state->processed = ret;
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
483 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
484 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
485 state->processed = state->count;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
486 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
487 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
488 state->processed = -1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
489 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
490 return Lpushevent(L, state);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
491 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
492
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
493 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
494 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
495 * Return Epoll FD
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
496 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
497 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
498 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
499 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
500 return 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
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
503 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
504 * Close epoll FD
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
505 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
506 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
507 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
508
9478
bd178ed0459b util.poll: Fix inverted logic
Kim Alvefur <zash@zash.se>
parents: 9476
diff changeset
509 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
510 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
511 }
9b26a50cdfe3 util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents: 9450
diff changeset
512
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
513 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
514 state->epoll_fd = -1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
515 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
516 else {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
517 lua_pushstring(L, strerror(errno));
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
518 lua_error(L);
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 return 0;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
522 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
523 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
524
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
525 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
526 * String representation
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
527 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
528 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
529 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
530 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
531 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
532 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
533
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
534 /*
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
535 * Create a new context
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
536 */
9954
36280801ddf1 util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents: 9508
diff changeset
537 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
538 /* Allocate state */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
539 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
540 luaL_setmetatable(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
541
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
542 /* Initialize state */
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
543 #ifdef USE_EPOLL
9476
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
544 state->epoll_fd = -1;
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
545 state->processed = 0;
9476
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
546
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
547 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
548
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
549 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
550 luaL_pushfail(L);
9476
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
551 lua_pushstring(L, strerror(errno));
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
552 lua_pushinteger(L, errno);
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
553 return 3;
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
554 }
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
555
f3935aa4cc7e util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents: 9475
diff changeset
556 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
557 #endif
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
558 #ifdef USE_POLL
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
559 state->processed = -1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
560 state->count = 0;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
561
13347
5fe8a8e16b27 util.poll: Rename things to clarify poll(2) limits
Kim Alvefur <zash@zash.se>
parents: 13335
diff changeset
562 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
563 state->events[i].fd = -1;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
564 state->events[i].events = 0;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
565 state->events[i].revents = 0;
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
566 }
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
567
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
568 #endif
12314
898554323338 util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents: 10921
diff changeset
569 #ifdef USE_SELECT
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
570 FD_ZERO(&state->wantread);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
571 FD_ZERO(&state->wantwrite);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
572 FD_ZERO(&state->readable);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
573 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
574 FD_ZERO(&state->all);
352e8b75c1ae util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents: 9447
diff changeset
575 FD_ZERO(&state->err);
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
576 state->processed = FD_SETSIZE;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
577 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
578
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
579 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
580 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
581
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 * Open library
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
584 */
12976
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
585 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
586 luaL_checkversion(L);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
587
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
588 luaL_newmetatable(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
589 {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
590
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
591 lua_pushliteral(L, STATE_MT);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
592 lua_setfield(L, -2, "__name");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
593
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
594 lua_pushcfunction(L, Ltos);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
595 lua_setfield(L, -2, "__tostring");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
596
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
597 lua_createtable(L, 0, 2);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
598 {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
599 lua_pushcfunction(L, Ladd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
600 lua_setfield(L, -2, "add");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
601 lua_pushcfunction(L, Lset);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
602 lua_setfield(L, -2, "set");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
603 lua_pushcfunction(L, Ldel);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
604 lua_setfield(L, -2, "del");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
605 lua_pushcfunction(L, Lwait);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
606 lua_setfield(L, -2, "wait");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
607 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
608 lua_pushcfunction(L, Lgetfd);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
609 lua_setfield(L, -2, "getfd");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
610 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
611 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
612 lua_setfield(L, -2, "__index");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
613
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
614 #ifdef USE_EPOLL
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
615 lua_pushcfunction(L, Lgc);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
616 lua_setfield(L, -2, "__gc");
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
617 #endif
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
618 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
619
9507
33d21f020b66 net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents: 9506
diff changeset
620 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
621 {
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
622 lua_pushcfunction(L, Lnew);
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
623 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
624
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
625 #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
626 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
627
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
628 push_errno(EEXIST);
12316
6bb2f660f689 util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents: 12315
diff changeset
629 push_errno(EMFILE);
9507
33d21f020b66 net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents: 9506
diff changeset
630 push_errno(ENOENT);
9506
ae6636052be9 net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents: 9478
diff changeset
631
12315
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
632 lua_pushliteral(L, POLL_BACKEND);
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
633 lua_setfield(L, -2, "api");
cf2086a1bd45 util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents: 12314
diff changeset
634
9313
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
635 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
636 return 1;
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
637 }
b95ef295c66d util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff changeset
638
12976
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
639 /* COMPAT */
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
640 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
641 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
642 }
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 12884
diff changeset
643