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.
31 static int nixio_nanosleep(lua_State *L) {
32 struct timespec req, rem;
33 req.tv_sec = luaL_optint(L, 1, 0);
34 req.tv_nsec = luaL_optint(L, 2, 0);
36 int status = nanosleep(&req, &rem);
38 lua_pushboolean(L, 1);
42 lua_pushboolean(L, 0);
43 lua_pushinteger(L, rem.tv_sec);
44 lua_pushinteger(L, rem.tv_nsec);
47 return nixio__perror(L);
53 * Checks whether a flag is set in the bitmap and sets the matching table value
55 static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) {
58 lua_pushboolean(L, 1);
66 * Translate integer to poll flags and vice versa
68 static int nixio_poll_flags(lua_State *L) {
70 if (lua_isnumber(L, 1)) {
71 flags = luaL_checkinteger(L, 1);
73 nixio_poll_flags__r(L, &flags, POLLIN, "in");
74 nixio_poll_flags__r(L, &flags, POLLPRI, "pri");
75 nixio_poll_flags__r(L, &flags, POLLOUT, "out");
76 nixio_poll_flags__r(L, &flags, POLLERR, "err");
77 nixio_poll_flags__r(L, &flags, POLLHUP, "hup");
78 nixio_poll_flags__r(L, &flags, POLLNVAL, "nval");
81 const int j = lua_gettop(L);
82 for (int i=1; i<=j; i++) {
83 const char *flag = luaL_checkstring(L, i);
84 if (!strcmp(flag, "in")) {
86 } else if (!strcmp(flag, "pri")) {
88 } else if (!strcmp(flag, "out")) {
90 } else if (!strcmp(flag, "err")) {
92 } else if (!strcmp(flag, "hup")) {
94 } else if (!strcmp(flag, "nval")) {
97 return luaL_argerror(L, i,
98 "supported values: in, pri, out, err, hup, nval");
101 lua_pushinteger(L, flags);
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);