Software /
code /
prosody
Annotate
util-src/poll.c @ 12754:a92ca737d05f
util.roles: Fix tests to use autogenerated role id
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 08 Oct 2022 20:39:14 +0100 |
parent | 12575:1f6f05a98fcd |
child | 12884:f5a75aaa8a25 |
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 <unistd.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 #include <string.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 #include <errno.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
15 #if defined(__linux__) |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 #define USE_EPOLL |
12315
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12314
diff
changeset
|
17 #define POLL_BACKEND "epoll" |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
18 #elif defined(__unix__) |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
19 #define USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
20 #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
|
21 #else |
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
22 #define USE_SELECT |
12315
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12314
diff
changeset
|
23 #define POLL_BACKEND "select" |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 #ifdef USE_EPOLL |
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 |
9450
b890ceb1c24f
util.poll: Increase max epoll events per call
Kim Alvefur <zash@zash.se>
parents:
9448
diff
changeset
|
29 #define MAX_EVENTS 64 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
31 #endif |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
32 #ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
33 #include <poll.h> |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
34 #ifndef MAX_EVENTS |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
35 #define MAX_EVENTS 10000 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
36 #endif |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
37 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
38 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 #include <sys/select.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 #include <lualib.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 #include <lauxlib.h> |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 |
12315
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12314
diff
changeset
|
45 #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
|
46 |
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
|
47 #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
|
48 #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
|
49 #endif |
9318 | 50 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 * 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
|
53 */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 typedef struct Lpoll_state { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 int processed; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 int epoll_fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 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
|
59 #endif |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
60 #ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
61 nfds_t count; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
62 struct pollfd events[MAX_EVENTS]; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
63 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
64 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 fd_set wantread; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 fd_set wantwrite; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 fd_set readable; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 fd_set writable; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 fd_set all; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 fd_set err; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 } Lpoll_state; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 * 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
|
76 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
77 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
|
78 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
|
79 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
|
80 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 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
|
82 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
|
83 |
9447
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
84 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
|
85 luaL_pushfail(L); |
9447
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
86 lua_pushstring(L, strerror(EBADF)); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
87 lua_pushinteger(L, EBADF); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
88 return 3; |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
89 } |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
90 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 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
|
95 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP; |
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 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
|
99 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 if(ret < 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 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
|
102 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
111 #endif |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
112 #ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
113 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
114 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
|
115 if(state->events[i].fd == fd) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
116 luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
117 lua_pushstring(L, strerror(EEXIST)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
118 lua_pushinteger(L, EEXIST); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
119 return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
120 } |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
121 } |
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 if(state->count >= MAX_EVENTS) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
124 luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
125 lua_pushstring(L, strerror(EMFILE)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
126 lua_pushinteger(L, EMFILE); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
127 return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
128 } |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
129 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
130 state->events[state->count].fd = fd; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
131 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
|
132 state->events[state->count].revents = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
133 state->count++; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
134 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
135 lua_pushboolean(L, 1); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
136 return 1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
137 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
138 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 |
9447
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
140 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
|
141 luaL_pushfail(L); |
9447
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
142 lua_pushstring(L, strerror(EBADF)); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
143 lua_pushinteger(L, EBADF); |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
144 return 3; |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
145 } |
6397e965a22d
net.poll: Guard against negative or too large FDs
Kim Alvefur <zash@zash.se>
parents:
9446
diff
changeset
|
146 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 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
|
148 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 lua_pushstring(L, strerror(EEXIST)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 lua_pushinteger(L, EEXIST); |
9446
6b4d28eb19cf
util.poll: Fix missing return for adding duplicate FD
Kim Alvefur <zash@zash.se>
parents:
9440
diff
changeset
|
151 return 3; |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 FD_SET(fd, &state->all); |
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 if(wantread) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 FD_SET(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 if(wantwrite) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
168 FD_SET(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
169 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
170 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
173 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
174 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
175 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 |
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 * 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
|
181 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
182 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
|
183 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
|
184 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
|
185 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 #ifdef USE_EPOLL |
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 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
|
189 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
|
190 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
191 struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 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
|
194 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
195 event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP; |
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 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
|
198 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
199 if(ret == 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
200 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
201 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
202 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 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
|
205 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
206 lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
207 lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
208 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
209 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
211 #endif |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
212 #ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
213 int wantread = lua_toboolean(L, 3); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
214 int wantwrite = lua_toboolean(L, 4); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
215 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
216 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
|
217 struct pollfd *event = &state->events[i]; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
218 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
219 if(event->fd == fd) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
220 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
|
221 lua_pushboolean(L, 1); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
222 return 1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
223 } else if(event->fd == -1) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
224 break; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
225 } |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
226 } |
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 luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
229 lua_pushstring(L, strerror(ENOENT)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
230 lua_pushinteger(L, ENOENT); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
231 return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
232 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
233 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 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
|
236 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 lua_pushstring(L, strerror(ENOENT)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
238 lua_pushinteger(L, ENOENT); |
10096
46a7792fdac5
util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents:
9954
diff
changeset
|
239 return 3; |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
240 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
241 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 if(!lua_isnoneornil(L, 3)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 if(lua_toboolean(L, 3)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 FD_SET(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 } |
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, 4)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 if(lua_toboolean(L, 4)) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 FD_SET(fd, &state->wantwrite); |
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->wantwrite); |
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 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 #endif |
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 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 * Remove FDs |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
267 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
268 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
|
269 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
|
270 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
|
271 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 #ifdef USE_EPOLL |
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 struct epoll_event event; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
275 event.data.fd = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 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
|
278 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 if(ret == 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 return 1; |
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 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
284 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
|
285 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
286 lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
289 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
290 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
291 #endif |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
292 #ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
293 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
294 if(state->count == 0) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
295 luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
296 lua_pushstring(L, strerror(ENOENT)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
297 lua_pushinteger(L, ENOENT); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
298 return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
299 } |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
300 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
301 /* |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
302 * 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
|
303 */ |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
304 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
|
305 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
306 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
|
307 struct pollfd *event = &state->events[i]; |
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 if(event->fd == fd) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
310 event->fd = last->fd; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
311 event->events = last->events; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
312 event->revents = last->revents; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
313 last->fd = -1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
314 state->count--; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
315 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
316 lua_pushboolean(L, 1); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
317 return 1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
318 } |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
319 } |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
320 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
321 luaL_pushfail(L); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
322 lua_pushstring(L, strerror(ENOENT)); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
323 lua_pushinteger(L, ENOENT); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
324 return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
325 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
326 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
327 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
328 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
|
329 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
330 lua_pushstring(L, strerror(ENOENT)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
331 lua_pushinteger(L, ENOENT); |
10096
46a7792fdac5
util.poll: Add missing return statements in fallback mode
Kim Alvefur <zash@zash.se>
parents:
9954
diff
changeset
|
332 return 3; |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
333 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
334 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
335 FD_CLR(fd, &state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
336 FD_CLR(fd, &state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
337 FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
338 FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
339 FD_CLR(fd, &state->all); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
340 FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
341 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
342 lua_pushboolean(L, 1); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
343 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
344 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
345 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
346 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
347 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
348 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
349 * 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
|
350 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
351 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
|
352 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
353 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
354 if(state->processed > 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
355 state->processed--; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
356 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
|
357 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
|
358 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
|
359 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
|
360 return 3; |
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 |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
363 #endif |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
364 #ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
365 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
366 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
|
367 struct pollfd *event = &state->events[i]; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
368 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
369 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
|
370 lua_pushinteger(L, event->fd); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
371 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
|
372 lua_pushboolean(L, event->revents & POLLOUT); |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
373 event->revents = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
374 state->processed = i; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
375 return 3; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
376 } |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
377 } |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
378 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
379 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
380 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
381 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
382 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
|
383 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
|
384 lua_pushinteger(L, fd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
385 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
|
386 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
|
387 FD_CLR(fd, &state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
388 FD_CLR(fd, &state->writable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
389 FD_CLR(fd, &state->err); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
390 state->processed = fd; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
391 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
392 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
393 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
394 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
395 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
396 return 0; |
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 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
399 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
400 * Wait for event |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
401 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
402 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
|
403 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
|
404 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
405 int ret = Lpushevent(L, state); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
406 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
407 if(ret != 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
408 return ret; |
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 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
|
412 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
|
413 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
414 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
415 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
|
416 #endif |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
417 #ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
418 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
|
419 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
420 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
421 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
422 * 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
|
423 * 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
|
424 */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
425 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
|
426 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
|
427 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
|
428 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
429 struct timeval tv; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
430 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
|
431 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
|
432 |
9437
b202aa1e2d7b
util.poll: Fix monitoring of socket exceptions in select mode
Kim Alvefur <zash@zash.se>
parents:
9318
diff
changeset
|
433 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
|
434 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
435 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
436 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
|
437 /* Is this an error? */ |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
438 lua_pushnil(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
439 lua_pushstring(L, "timeout"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
440 return 2; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
441 } |
9508
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
442 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
|
443 /* Is this an error? */ |
9508
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
444 lua_pushnil(L); |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
445 lua_pushstring(L, "signal"); |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
446 return 2; |
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
447 } |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
448 else if(ret < 0) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
449 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
|
450 luaL_pushfail(L); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
451 lua_pushstring(L, strerror(ret)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
452 lua_pushinteger(L, ret); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
453 return 3; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
454 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
455 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
456 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
457 * 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
|
458 */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
459 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
460 state->processed = ret; |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
461 #endif |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
462 #ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
463 state->processed = state->count; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
464 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
465 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
466 state->processed = -1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
467 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
468 return Lpushevent(L, state); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
469 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
470 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
471 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
472 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
473 * Return Epoll FD |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
474 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
475 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
|
476 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
|
477 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
|
478 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
479 } |
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 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
482 * Close epoll FD |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
483 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
484 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
|
485 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
|
486 |
9478 | 487 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
|
488 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
|
489 } |
9b26a50cdfe3
util.poll: Early return from __gc in case of no valid epoll FD
Kim Alvefur <zash@zash.se>
parents:
9450
diff
changeset
|
490 |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
491 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
|
492 state->epoll_fd = -1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
493 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
494 else { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
495 lua_pushstring(L, strerror(errno)); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
496 lua_error(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
497 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
498 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
499 return 0; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
500 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
501 #endif |
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 * String representation |
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 Ltos(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 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
|
509 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
510 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
511 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
512 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
513 * Create a new context |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
514 */ |
9954
36280801ddf1
util.poll: Minimize scope of methods
Kim Alvefur <zash@zash.se>
parents:
9508
diff
changeset
|
515 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
|
516 /* Allocate state */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
517 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
|
518 luaL_setmetatable(L, STATE_MT); |
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 /* Initialize state */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
521 #ifdef USE_EPOLL |
9476
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
522 state->epoll_fd = -1; |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
523 state->processed = 0; |
9476
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
524 |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
525 int epoll_fd = epoll_create1(EPOLL_CLOEXEC); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
526 |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
527 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
|
528 luaL_pushfail(L); |
9476
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
529 lua_pushstring(L, strerror(errno)); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
530 lua_pushinteger(L, errno); |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
531 return 3; |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
532 } |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
533 |
f3935aa4cc7e
util.poll: Handle failed epoll FD creation
Kim Alvefur <zash@zash.se>
parents:
9475
diff
changeset
|
534 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
|
535 #endif |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
536 #ifdef USE_POLL |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
537 state->processed = -1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
538 state->count = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
539 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
540 for(nfds_t i = 0; i < MAX_EVENTS; i++) { |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
541 state->events[i].fd = -1; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
542 state->events[i].events = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
543 state->events[i].revents = 0; |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
544 } |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
545 |
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
546 #endif |
12314
898554323338
util.poll: Restructure to make adding additional system APIs easier
Kim Alvefur <zash@zash.se>
parents:
10921
diff
changeset
|
547 #ifdef USE_SELECT |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
548 FD_ZERO(&state->wantread); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
549 FD_ZERO(&state->wantwrite); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
550 FD_ZERO(&state->readable); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
551 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
|
552 FD_ZERO(&state->all); |
352e8b75c1ae
util.poll: Zero FD sets watched for errors on creation
Kim Alvefur <zash@zash.se>
parents:
9447
diff
changeset
|
553 FD_ZERO(&state->err); |
9313
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
554 state->processed = FD_SETSIZE; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
555 #endif |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
556 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
557 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
558 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
559 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
560 /* |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
561 * Open library |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
562 */ |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
563 int luaopen_util_poll(lua_State *L) { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
564 luaL_checkversion(L); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
565 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
566 luaL_newmetatable(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
567 { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
568 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
569 lua_pushliteral(L, STATE_MT); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
570 lua_setfield(L, -2, "__name"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
571 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
572 lua_pushcfunction(L, Ltos); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
573 lua_setfield(L, -2, "__tostring"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
574 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
575 lua_createtable(L, 0, 2); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
576 { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
577 lua_pushcfunction(L, Ladd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
578 lua_setfield(L, -2, "add"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
579 lua_pushcfunction(L, Lset); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
580 lua_setfield(L, -2, "set"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
581 lua_pushcfunction(L, Ldel); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
582 lua_setfield(L, -2, "del"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
583 lua_pushcfunction(L, Lwait); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
584 lua_setfield(L, -2, "wait"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
585 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
586 lua_pushcfunction(L, Lgetfd); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
587 lua_setfield(L, -2, "getfd"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
588 #endif |
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 lua_setfield(L, -2, "__index"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
591 |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
592 #ifdef USE_EPOLL |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
593 lua_pushcfunction(L, Lgc); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
594 lua_setfield(L, -2, "__gc"); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
595 #endif |
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 |
9507
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9506
diff
changeset
|
598 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
|
599 { |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
600 lua_pushcfunction(L, Lnew); |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
601 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
|
602 |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
603 #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
|
604 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
|
605 |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
606 push_errno(EEXIST); |
12316
6bb2f660f689
util.poll: Add support for the poll() API
Kim Alvefur <zash@zash.se>
parents:
12315
diff
changeset
|
607 push_errno(EMFILE); |
9507
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9506
diff
changeset
|
608 push_errno(ENOENT); |
9506
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9478
diff
changeset
|
609 |
12315
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12314
diff
changeset
|
610 lua_pushliteral(L, POLL_BACKEND); |
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12314
diff
changeset
|
611 lua_setfield(L, -2, "api"); |
cf2086a1bd45
util.poll: Expose API (epoll or select) used
Kim Alvefur <zash@zash.se>
parents:
12314
diff
changeset
|
612 |
9313
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 return 1; |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
615 } |
b95ef295c66d
util.poll: Import poll library with epoll and select support
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
616 |