Merge pull request #429 from chris5560/master
[project/luci.git] / libs / luci-lib-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 <time.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <sys/time.h>
25
26
27 static int nixio_gettimeofday(lua_State *L) {
28         struct timeval tv;
29         gettimeofday(&tv, NULL);
30         nixio__pushnumber(L, tv.tv_sec);
31         nixio__pushnumber(L, tv.tv_usec);
32         return 2;
33 }
34
35
36 /**
37  * nanosleep()
38  */
39 static int nixio_nanosleep(lua_State *L) {
40         struct timespec req, rem;
41         req.tv_sec = luaL_optint(L, 1, 0);
42         req.tv_nsec = luaL_optint(L, 2, 0);
43
44         int status = nanosleep(&req, &rem);
45         if (!status) {
46                 lua_pushboolean(L, 1);
47                 return 1;
48         } else {
49                 if (errno == EINTR) {
50                         lua_pushboolean(L, 0);
51                         lua_pushinteger(L, rem.tv_sec);
52                         lua_pushinteger(L, rem.tv_nsec);
53                         return 3;
54                 } else {
55                         return nixio__perror(L);
56                 }
57         }
58 }
59
60 /**
61  * Checks whether a flag is set in the bitmap and sets the matching table value
62  */
63 static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) {
64         lua_pushstring(L, t);
65         if (*map & f) {
66                 lua_pushboolean(L, 1);
67         } else {
68                 lua_pushnil(L);
69         }
70         lua_rawset(L, -3);
71 }
72
73 /**
74  * Translate integer to poll flags and vice versa
75  */
76 static int nixio_poll_flags(lua_State *L) {
77         int flags;
78         if (lua_isnumber(L, 1)) {
79                 flags = luaL_checkinteger(L, 1);
80                 lua_newtable(L);
81                 nixio_poll_flags__r(L, &flags, POLLIN, "in");
82                 nixio_poll_flags__r(L, &flags, POLLOUT, "out");
83                 nixio_poll_flags__r(L, &flags, POLLERR, "err");
84 #ifndef __WINNT__
85                 nixio_poll_flags__r(L, &flags, POLLPRI, "pri");
86                 nixio_poll_flags__r(L, &flags, POLLHUP, "hup");
87                 nixio_poll_flags__r(L, &flags, POLLNVAL, "nval");
88 #endif
89          } else {
90                 flags = 0;
91                 const int j = lua_gettop(L);
92                 for (int i=1; i<=j; i++) {
93                         const char *flag = luaL_checkstring(L, i);
94                         if (!strcmp(flag, "in")) {
95                                 flags |= POLLIN;
96                         } else if (!strcmp(flag, "out")) {
97                                 flags |= POLLOUT;
98                         } else if (!strcmp(flag, "err")) {
99                                 flags |= POLLERR;
100                         } else if (!strcmp(flag, "pri")) {
101 #ifndef __WINNT__
102                                 flags |= POLLPRI;
103 #endif
104                         } else if (!strcmp(flag, "hup")) {
105 #ifndef __WINNT__
106                                 flags |= POLLHUP;
107 #endif
108                         } else if (!strcmp(flag, "nval")) {
109 #ifndef __WINNT__
110                                 flags |= POLLNVAL;
111 #endif
112                         } else {
113                                 return luaL_argerror(L, i,
114                                  "supported values: in, pri, out, err, hup, nval");
115                         }
116                 }
117                 lua_pushinteger(L, flags);
118         }
119         return 1;
120 }
121
122 /**
123  * poll({{fd = socket, events = FLAGS}, ...}, timeout)
124  */
125 static int nixio_poll(lua_State *L) {
126         int len = lua_objlen(L, 1);
127         int i, fd;
128         int timeout = luaL_optint(L, 2, 0);
129         int status = -1;
130
131         /* we are being abused as sleep() replacement... */
132         if (lua_isnoneornil(L, 1) || len < 1) {
133                 if (!poll(NULL, 0, timeout)) {
134                         lua_pushinteger(L, 0);
135                         return 1;
136                 } else {
137                         return nixio__perror(L);
138                 }
139         }
140
141         luaL_checktype(L, 1, LUA_TTABLE);
142         struct pollfd *fds = calloc(len, sizeof(struct pollfd));
143         if (!fds) {
144                 return luaL_error(L, NIXIO_OOM);
145         }
146
147         for (i = 0; i < len; i++) {
148                 lua_rawgeti(L, 1, i+1);
149                 if (!lua_istable(L, -1)) {
150                         free(fds);
151                         return luaL_argerror(L, 1, "invalid datastructure");
152                 }
153
154                 lua_pushliteral(L, "fd");
155                 lua_rawget(L, -2);
156                 fd = nixio__tofd(L, -1);
157                 if (fd == -1) {
158                         free(fds);
159                         return luaL_argerror(L, 1, "invalid fd in datastructure");
160                 }
161                 fds[i].fd = fd;
162
163                 lua_pushliteral(L, "events");
164                 lua_rawget(L, -3);
165                 fds[i].events = (short)lua_tointeger(L, -1);
166
167                 lua_pop(L, 3);
168         }
169
170         status = poll(fds, (nfds_t)len, timeout);
171
172         if (status == 0) {
173                 free(fds);
174                 lua_pushboolean(L, 0);
175                 return 1;
176         } else if (status < 0) {
177                 free(fds);
178                 return nixio__perror(L);
179         }
180
181         for (i = 0; i < len; i++) {
182                 lua_rawgeti(L, 1, i+1);
183
184                 lua_pushliteral(L, "revents");
185                 lua_pushinteger(L, fds[i].revents);
186                 lua_rawset(L, -3);
187
188                 lua_pop(L, 1);
189         }
190
191         free(fds);
192
193         lua_pushinteger(L, status);
194         lua_pushvalue(L, 1);
195
196         return 2;
197 }
198
199 /* module table */
200 static const luaL_reg R[] = {
201         {"gettimeofday", nixio_gettimeofday},
202         {"nanosleep",   nixio_nanosleep},
203         {"poll",                nixio_poll},
204         {"poll_flags",  nixio_poll_flags},
205         {NULL,                  NULL}
206 };
207
208 void nixio_open_poll(lua_State *L) {
209         luaL_register(L, NULL, R);
210 }