2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
30 static int nixio_nanosleep(lua_State *L) {
31 struct timespec req, rem;
32 req.tv_sec = luaL_optint(L, 1, 0);
33 req.tv_nsec = luaL_optint(L, 2, 0);
35 int status = nanosleep(&req, &rem);
37 lua_pushboolean(L, 1);
41 lua_pushboolean(L, 0);
42 lua_pushinteger(L, rem.tv_sec);
43 lua_pushinteger(L, rem.tv_nsec);
46 return nixio__perror(L);
52 * Checks whether a flag is set in the table and translates it into a bitmap
54 static void nixio_poll_flags__w(lua_State *L, int *map, int f, const char *t) {
57 if (lua_toboolean(L, -1)) {
64 * Checks whether a flag is set in the bitmap and sets the matching table value
66 static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) {
69 lua_pushboolean(L, 1);
77 * Translate integer to poll flags and vice versa
79 static int nixio_poll_flags(lua_State *L) {
81 if (lua_istable(L, 1)) {
84 nixio_poll_flags__w(L, &flags, POLLIN, "in");
85 nixio_poll_flags__w(L, &flags, POLLPRI, "pri");
86 nixio_poll_flags__w(L, &flags, POLLOUT, "out");
87 nixio_poll_flags__w(L, &flags, POLLERR, "err");
88 nixio_poll_flags__w(L, &flags, POLLHUP, "hup");
89 nixio_poll_flags__w(L, &flags, POLLNVAL, "nval");
90 lua_pushinteger(L, flags);
92 flags = luaL_checkinteger(L, 1);
94 nixio_poll_flags__r(L, &flags, POLLIN, "in");
95 nixio_poll_flags__r(L, &flags, POLLPRI, "pri");
96 nixio_poll_flags__r(L, &flags, POLLOUT, "out");
97 nixio_poll_flags__r(L, &flags, POLLERR, "err");
98 nixio_poll_flags__r(L, &flags, POLLHUP, "hup");
99 nixio_poll_flags__r(L, &flags, POLLNVAL, "nval");
105 * poll({{fd = socket, events = FLAGS}, ...}, timeout)
107 static int nixio_poll(lua_State *L) {
108 int len = lua_objlen(L, 1);
110 int timeout = luaL_optint(L, 2, 0);
113 /* we are being abused as sleep() replacement... */
114 if (lua_isnoneornil(L, 1) || len < 1) {
115 return nixio__pstatus(L, !poll(NULL, 0, timeout));
118 luaL_checktype(L, 1, LUA_TTABLE);
119 struct pollfd *fds = calloc(len, sizeof(struct pollfd));
121 for (i = 0; i < len; i++) {
122 lua_rawgeti(L, 1, i+1);
123 if (!lua_istable(L, -1)) {
125 return luaL_argerror(L, 1, "invalid datastructure");
128 lua_pushliteral(L, "fd");
130 fd = nixio__tofd(L, -1);
133 return luaL_argerror(L, 1, "invalid fd in datastructure");
137 lua_pushliteral(L, "events");
139 fds[i].events = (short)lua_tointeger(L, -1);
144 status = poll(fds, (nfds_t)len, timeout);
148 return nixio__perror(L);
151 for (i = 0; i < len; i++) {
152 lua_rawgeti(L, 1, i+1);
154 lua_pushliteral(L, "revents");
155 lua_pushinteger(L, fds[i].revents);
163 lua_pushinteger(L, status);
170 static const luaL_reg R[] = {
171 {"nanosleep", nixio_nanosleep},
172 {"poll", nixio_poll},
173 {"poll_flags", nixio_poll_flags},
177 void nixio_open_poll(lua_State *L) {
178 luaL_register(L, NULL, R);