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.
32 static int nixio_nanosleep(lua_State *L) {
33 struct timespec req, rem;
34 req.tv_sec = luaL_optint(L, 1, 0);
35 req.tv_nsec = luaL_optint(L, 2, 0);
37 int status = nanosleep(&req, &rem);
39 lua_pushboolean(L, 1);
43 lua_pushboolean(L, 0);
44 lua_pushinteger(L, rem.tv_sec);
45 lua_pushinteger(L, rem.tv_nsec);
48 return nixio__perror(L);
54 * Checks whether a flag is set in the table and translates it into a bitmap
56 static void nixio_poll_flags__w(lua_State *L, int *map, int f, const char *t) {
59 if (lua_toboolean(L, -1)) {
66 * Checks whether a flag is set in the bitmap and sets the matching table value
68 static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) {
71 lua_pushboolean(L, 1);
79 * Translate integer to poll flags and vice versa
81 static int nixio_poll_flags(lua_State *L) {
83 if (lua_istable(L, 1)) {
86 nixio_poll_flags__w(L, &flags, POLLIN, "in");
87 nixio_poll_flags__w(L, &flags, POLLPRI, "pri");
88 nixio_poll_flags__w(L, &flags, POLLOUT, "out");
89 nixio_poll_flags__w(L, &flags, POLLERR, "err");
90 nixio_poll_flags__w(L, &flags, POLLHUP, "hup");
91 nixio_poll_flags__w(L, &flags, POLLNVAL, "nval");
92 lua_pushinteger(L, flags);
94 flags = luaL_checkinteger(L, 1);
96 nixio_poll_flags__r(L, &flags, POLLIN, "in");
97 nixio_poll_flags__r(L, &flags, POLLPRI, "pri");
98 nixio_poll_flags__r(L, &flags, POLLOUT, "out");
99 nixio_poll_flags__r(L, &flags, POLLERR, "err");
100 nixio_poll_flags__r(L, &flags, POLLHUP, "hup");
101 nixio_poll_flags__r(L, &flags, POLLNVAL, "nval");
107 * poll({{fd = socket, events = FLAGS}, ...}, timeout)
109 static int nixio_poll(lua_State *L) {
110 int len = lua_objlen(L, 1);
112 int timeout = luaL_optint(L, 2, 0);
115 /* we are being abused as sleep() replacement... */
116 if (lua_isnoneornil(L, 1) || len < 1) {
117 return nixio__pstatus(L, !poll(NULL, 0, timeout));
120 luaL_checktype(L, 1, LUA_TTABLE);
121 struct pollfd *fds = calloc(len, sizeof(struct pollfd));
123 for (i = 0; i < len; i++) {
124 lua_rawgeti(L, 1, i+1);
125 if (!lua_istable(L, -1)) {
127 return luaL_argerror(L, 1, "invalid datastructure");
130 lua_pushliteral(L, "fd");
132 fd = nixio__tofd(L, -1);
135 return luaL_argerror(L, 1, "invalid fd in datastructure");
139 lua_pushliteral(L, "events");
141 fds[i].events = (short)lua_tointeger(L, -1);
146 status = poll(fds, (nfds_t)len, timeout);
150 return nixio__perror(L);
153 for (i = 0; i < len; i++) {
154 lua_rawgeti(L, 1, i+1);
156 lua_pushliteral(L, "revents");
157 lua_pushinteger(L, fds[i].revents);
165 lua_pushinteger(L, status);
172 static const luaL_reg R[] = {
173 {"nanosleep", nixio_nanosleep},
174 {"poll", nixio_poll},
175 {"poll_flags", nixio_poll_flags},
179 void nixio_open_poll(lua_State *L) {
180 luaL_register(L, NULL, R);