Use poll() instead of select()
[project/luci.git] / libs / nixio / src / poll.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
5  *
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <lua.h>
20 #include <lualib.h>
21 #include <lauxlib.h>
22 #include <poll.h>
23 #include <time.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include "nixio.h"
27
28
29 /**
30  * nanosleep()
31  */
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);
36
37         int status = nanosleep(&req, &rem);
38         if (!status) {
39                 lua_pushboolean(L, 1);
40                 return 1;
41         } else {
42                 if (errno == EINTR) {
43                         lua_pushboolean(L, 0);
44                         lua_pushinteger(L, rem.tv_sec);
45                         lua_pushinteger(L, rem.tv_nsec);
46                         return 3;
47                 } else {
48                         return nixio__perror(L);
49                 }
50         }
51 }
52
53 /**
54  * Checks whether a flag is set in the table and translates it into a bitmap
55  */
56 static void nixio_poll_flags__w(lua_State *L, int *map, int f, const char *t) {
57         lua_pushstring(L, t);
58         lua_rawget(L, -2);
59         if (lua_toboolean(L, -1)) {
60                 *map |= f;
61         }
62         lua_pop(L, 1);
63 }
64
65 /**
66  * Checks whether a flag is set in the bitmap and sets the matching table value
67  */
68 static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) {
69         lua_pushstring(L, t);
70         if (*map & f) {
71                 lua_pushboolean(L, 1);
72         } else {
73                 lua_pushnil(L);
74         }
75         lua_rawset(L, -3);
76 }
77
78 /**
79  * Translate integer to poll flags and vice versa
80  */
81 static int nixio_poll_flags(lua_State *L) {
82         int flags;
83         if (lua_istable(L, 1)) {
84                 lua_settop(L, 1);
85                 flags = 0;
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);
93         } else {
94                 flags = luaL_checkinteger(L, 1);
95                 lua_newtable(L);
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");
102         }
103         return 1;
104 }
105
106 /**
107  * poll({{fd = socket, events = FLAGS}, ...}, timeout)
108  */
109 static int nixio_poll(lua_State *L) {
110         int len = lua_objlen(L, 1);
111         int i, fd;
112         int timeout = luaL_optint(L, 2, 0);
113         int status = -1;
114
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));
118         }
119
120         luaL_checktype(L, 1, LUA_TTABLE);
121         struct pollfd *fds = calloc(len, sizeof(struct pollfd));
122
123         for (i = 0; i < len; i++) {
124                 lua_rawgeti(L, 1, i+1);
125                 if (!lua_istable(L, -1)) {
126                         free(fds);
127                         return luaL_argerror(L, 1, "invalid datastructure");
128                 }
129
130                 lua_pushliteral(L, "fd");
131                 lua_rawget(L, -2);
132                 fd = nixio__tofd(L, -1);
133                 if (fd == -1) {
134                         free(fds);
135                         return luaL_argerror(L, 1, "invalid fd in datastructure");
136                 }
137                 fds[i].fd = fd;
138
139                 lua_pushliteral(L, "events");
140                 lua_rawget(L, -3);
141                 fds[i].events = (short)lua_tointeger(L, -1);
142
143                 lua_pop(L, 3);
144         }
145
146         status = poll(fds, (nfds_t)len, timeout);
147
148         if (status < 1) {
149                 free(fds);
150                 return nixio__perror(L);
151         }
152
153         for (i = 0; i < len; i++) {
154                 lua_rawgeti(L, 1, i+1);
155
156                 lua_pushliteral(L, "revents");
157                 lua_pushinteger(L, fds[i].revents);
158                 lua_rawset(L, -3);
159
160                 lua_pop(L, 1);
161         }
162
163         free(fds);
164
165         lua_pushinteger(L, status);
166         lua_pushvalue(L, 1);
167
168         return 2;
169 }
170
171 /* module table */
172 static const luaL_reg R[] = {
173         {"nanosleep",   nixio_nanosleep},
174         {"poll",                nixio_poll},
175         {"poll_flags",  nixio_poll_flags},
176         {NULL,                  NULL}
177 };
178
179 void nixio_open_poll(lua_State *L) {
180         luaL_register(L, NULL, R);
181 }