nixio: Fixes, use POSIX calls for file i/o
[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 "nixio.h"
20 #include <poll.h>
21 #include <time.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "nixio.h"
26
27
28 /**
29  * nanosleep()
30  */
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);
35
36         int status = nanosleep(&req, &rem);
37         if (!status) {
38                 lua_pushboolean(L, 1);
39                 return 1;
40         } else {
41                 if (errno == EINTR) {
42                         lua_pushboolean(L, 0);
43                         lua_pushinteger(L, rem.tv_sec);
44                         lua_pushinteger(L, rem.tv_nsec);
45                         return 3;
46                 } else {
47                         return nixio__perror(L);
48                 }
49         }
50 }
51
52 /**
53  * Checks whether a flag is set in the bitmap and sets the matching table value
54  */
55 static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) {
56         lua_pushstring(L, t);
57         if (*map & f) {
58                 lua_pushboolean(L, 1);
59         } else {
60                 lua_pushnil(L);
61         }
62         lua_rawset(L, -3);
63 }
64
65 /**
66  * Translate integer to poll flags and vice versa
67  */
68 static int nixio_poll_flags(lua_State *L) {
69         int flags;
70         if (lua_isnumber(L, 1)) {
71                 flags = luaL_checkinteger(L, 1);
72                 lua_newtable(L);
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");
79          } else {
80                 flags = 0;
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")) {
85                                 flags |= POLLIN;
86                         } else if (!strcmp(flag, "pri")) {
87                                 flags |= POLLPRI;
88                         } else if (!strcmp(flag, "out")) {
89                                 flags |= POLLOUT;
90                         } else if (!strcmp(flag, "err")) {
91                                 flags |= POLLERR;
92                         } else if (!strcmp(flag, "hup")) {
93                                 flags |= POLLHUP;
94                         } else if (!strcmp(flag, "nval")) {
95                                 flags |= POLLNVAL;
96                         } else {
97                                 return luaL_argerror(L, i,
98                                  "supported values: in, pri, out, err, hup, nval");
99                         }
100                 }
101                 lua_pushinteger(L, flags);
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 }