1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/*
* POSIX library for Lua 5.1, 5.2, 5.3 & 5.4.
* Copyright (C) 2013-2025 Gary V. Vaughan
* Copyright (C) 2010-2013 Reuben Thomas <rrt@sc3d.org>
* Copyright (C) 2008-2010 Natanael Copa <natanael.copa@gmail.com>
* Clean up and bug fixes by Leo Razoumov <slonik.az@gmail.com> 2006-10-11
* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> 07 Apr 2006 23:17:49
* Based on original by Claudio Terra for Lua 3.x.
* With contributions by Roberto Ierusalimschy.
* With documentation from Steve Donovan 2012
*/
/***
Synchronous I/O Multiplexing.
Examine file descriptors for events, such as readyness for I/O.
@module posix.poll
*/
#include <poll.h>
#include "_helpers.c"
static struct {
short bit;
const char *name;
} poll_event_map[] = {
#define MAP(_NAME) \
{POLL##_NAME, #_NAME}
MAP(IN),
MAP(PRI),
MAP(OUT),
MAP(ERR),
MAP(HUP),
MAP(NVAL),
#undef MAP
};
#define PPOLL_EVENT_NUM (sizeof(poll_event_map) / sizeof(*poll_event_map))
static void
poll_events_createtable(lua_State *L)
{
lua_createtable(L, 0, PPOLL_EVENT_NUM);
}
static short
poll_events_from_table(lua_State *L, int table)
{
short events = 0;
size_t i;
/* Convert to absolute index */
if (table < 0)
table = lua_gettop(L) + table + 1;
for (i = 0; i < PPOLL_EVENT_NUM; i++)
{
lua_getfield(L, table, poll_event_map[i].name);
if (lua_toboolean(L, -1))
events |= poll_event_map[i].bit;
lua_pop(L, 1);
}
return events;
}
static void
poll_events_to_table(lua_State *L, int table, short events)
{
size_t i;
/* Convert to absolute index */
if (table < 0)
table = lua_gettop(L) + table + 1;
for (i = 0; i < PPOLL_EVENT_NUM; i++)
{
lua_pushboolean(L, events & poll_event_map[i].bit);
lua_setfield(L, table, poll_event_map[i].name);
}
}
static nfds_t
poll_fd_list_check_table(lua_State *L, int table)
{
nfds_t fd_num = 0;
/*
* Assume table is an argument number.
* Should be an assert(table > 0).
*/
luaL_checktype(L, table, LUA_TTABLE);
/* Nil key - the one before first */
lua_pushnil(L);
/* Push each key/value pair, popping previous key */
while (lua_next(L, 1) != 0)
{
/* Verify the fd key */
luaL_argcheck(L, lua_isinteger(L, -2), table,
"contains non-integer key(s)");
/* Verify the table value */
luaL_argcheck(L, lua_istable(L, -1), table,
"contains non-table value(s)");
lua_getfield(L, -1, "events");
luaL_argcheck(L, lua_istable(L, -1), table,
"contains invalid value table(s)");
lua_pop(L, 1);
lua_getfield(L, -1, "revents");
luaL_argcheck(L, lua_isnil(L, -1) || lua_istable(L, -1), table,
"contains invalid value table(s)");
lua_pop(L, 1);
/* Remove value (but leave the key) */
lua_pop(L, 1);
/* Count the fds */
fd_num++;
}
return fd_num;
}
static void
poll_fd_list_from_table(lua_State *L, int table, struct pollfd *fd_list)
{
struct pollfd *pollfd = fd_list;
/*
* Assume the table didn't change since
* the call to poll_fd_list_check_table
*/
/* Convert to absolute index */
if (table < 0)
table = lua_gettop(L) + table + 1;
/* Nil key - the one before first */
lua_pushnil(L);
/* Push each key/value pair, popping previous key */
while (lua_next(L, table) != 0)
{
/* Transfer the fd key */
pollfd->fd = lua_tointeger(L, -2);
/* Transfer "events" field from the value */
lua_getfield(L, -1, "events");
pollfd->events = poll_events_from_table(L, -1);
lua_pop(L, 1);
/* Remove value (but leave the key) */
lua_pop(L, 1);
/* Proceed to next fd */
pollfd++;
}
}
static void
poll_fd_list_to_table(lua_State *L, int table, const struct pollfd *fd_list)
{
const struct pollfd *pollfd = fd_list;
/*
* Assume the table didn't change since
* the call to poll_fd_list_check_table.
*/
/* Convert to absolute index */
if (table < 0)
table = lua_gettop(L) + table + 1;
/* Nil key - the one before first */
lua_pushnil(L);
/* Push each key/value pair, popping previous key */
while (lua_next(L, 1) != 0)
{
/* Transfer "revents" field to the value */
lua_getfield(L, -1, "revents");
if (lua_isnil(L, -1))
{
lua_pop(L, 1);
poll_events_createtable(L);
lua_pushvalue(L, -1);
lua_setfield(L, -3, "revents");
}
poll_events_to_table(L, -1, pollfd->revents);
lua_pop(L, 1);
/* Remove value (but leave the key) */
lua_pop(L, 1);
/* Proceed to next fd */
pollfd++;
}
}
/***
Wait for events on multiple file descriptors.
@function poll
@tparam table fds list of file descriptors
@int[opt=-1] timeout maximum timeout in milliseconds, or -1 to block indefinitely
@treturn[1] int `0` if timed out, number of *fd*'s ready if successful
@return[2] nil
@treturn[2] string error message
@treturn[2] int errnum
@see poll(2)
@see rpoll
@see poll.lua
*/
static int
Ppoll(lua_State *L)
{
struct pollfd *fd_list, static_fd_list[16];
nfds_t fd_num = poll_fd_list_check_table(L, 1);
int r, timeout = optint(L, 2, -1);
checknargs(L, 2);
fd_list = (fd_num <= sizeof(static_fd_list) / sizeof(*static_fd_list))
? static_fd_list
: lua_newuserdata(L, sizeof(*fd_list) * fd_num);
poll_fd_list_from_table(L, 1, fd_list);
r = poll(fd_list, fd_num, timeout);
/* If any of the descriptors changed state */
if (r > 0)
poll_fd_list_to_table(L, 1, fd_list);
return pushresult(L, r, NULL);
}
/***
Wait for some event on a file descriptor.
Based on [a lua-l list post](http://lua-users.org/lists/lua-l/2007-11/msg00346.html).
@function rpoll
@int fd file descriptor
@int[opt=-1] timeout maximum timeout in milliseconds, or `-1` to block indefinitely
@treturn[1] int `0` if timed out, number of *fd*'s ready if successful
@return[2] nil
@return[2] error message
@treturn[2] int errnum
@see poll
@usage
local stdio = require "posix.stdio"
local fh = io.open "one"
while true do
local r = rpoll (stdio.fileno(fh), 500)
if r == 0 then
print 'timeout'
elseif r == 1 then
print (fh:read ())
else
print "finish!"
break
end
end
*/
static int
Prpoll(lua_State *L)
{
struct pollfd fds;
int file = checkint(L, 1);
int timeout = checkint(L, 2);
checknargs(L, 2);
fds.fd = file;
fds.events = POLLIN;
return pushresult(L, poll(&fds, 1, timeout), NULL);
}
static const luaL_Reg posix_poll_fns[] =
{
LPOSIX_FUNC( Ppoll ),
LPOSIX_FUNC( Prpoll ),
{NULL, NULL}
};
LUALIB_API int
luaopen_posix_poll(lua_State *L)
{
luaL_newlib(L, posix_poll_fns);
lua_pushstring(L, LPOSIX_VERSION_STRING("poll"));
lua_setfield(L, -2, "version");
return 1;
}
|