8fd585f22e7b082f22e8e1a939132f9b17880406
[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 <stdlib.h>
24 #include "nixio.h"
25
26
27 /**
28  * nanosleep()
29  */
30 static int nixio_nanosleep(lua_State *L) {
31         struct timespec req, rem;
32         req.tv_sec = luaL_optint(L, 1, 0);
33         req.tv_nsec = luaL_optint(L, 2, 0);
34
35         int status = nanosleep(&req, &rem);
36         if (!status) {
37                 lua_pushboolean(L, 1);
38                 return 1;
39         } else {
40                 if (errno == EINTR) {
41                         lua_pushboolean(L, 0);
42                         lua_pushinteger(L, rem.tv_sec);
43                         lua_pushinteger(L, rem.tv_nsec);
44                         return 3;
45                 } else {
46                         return nixio__perror(L);
47                 }
48         }
49 }
50
51 /**
52  * Checks whether a flag is set in the table and translates it into a bitmap
53  */
54 static void nixio_poll_flags__w(lua_State *L, int *map, int f, const char *t) {
55         lua_pushstring(L, t);
56         lua_rawget(L, -2);
57         if (lua_toboolean(L, -1)) {
58                 *map |= f;
59         }
60         lua_pop(L, 1);
61 }
62
63 /**
64  * Checks whether a flag is set in the bitmap and sets the matching table value
65  */
66 static void nixio_poll_flags__r(lua_State *L, int *map, int f, const char *t) {
67         lua_pushstring(L, t);
68         if (*map & f) {
69                 lua_pushboolean(L, 1);
70         } else {
71                 lua_pushnil(L);
72         }
73         lua_rawset(L, -3);
74 }
75
76 /**
77  * Translate integer to poll flags and vice versa
78  */
79 static int nixio_poll_flags(lua_State *L) {
80         int flags;
81         if (lua_istable(L, 1)) {
82                 lua_settop(L, 1);
83                 flags = 0;
84                 nixio_poll_flags__w(L, &flags, POLLIN, "in");
85                 nixio_poll_flags__w(L, &flags, POLLPRI, "pri");
86                 nixio_poll_flags__w(L, &flags, POLLOUT, "out");
87                 nixio_poll_flags__w(L, &flags, POLLERR, "err");
88                 nixio_poll_flags__w(L, &flags, POLLHUP, "hup");
89                 nixio_poll_flags__w(L, &flags, POLLNVAL, "nval");
90                 lua_pushinteger(L, flags);
91         } else {
92                 flags = luaL_checkinteger(L, 1);
93                 lua_newtable(L);
94                 nixio_poll_flags__r(L, &flags, POLLIN, "in");
95                 nixio_poll_flags__r(L, &flags, POLLPRI, "pri");
96                 nixio_poll_flags__r(L, &flags, POLLOUT, "out");
97                 nixio_poll_flags__r(L, &flags, POLLERR, "err");
98                 nixio_poll_flags__r(L, &flags, POLLHUP, "hup");
99                 nixio_poll_flags__r(L, &flags, POLLNVAL, "nval");
100         }
101         return 1;
102 }
103
104 /**
105  * poll({{fd = socket, events = FLAGS}, ...}, timeout)
106  */
107 static int nixio_poll(lua_State *L) {
108         int len = lua_objlen(L, 1);
109         int i, fd;
110         int timeout = luaL_optint(L, 2, 0);
111         int status = -1;
112
113         /* we are being abused as sleep() replacement... */
114         if (lua_isnoneornil(L, 1) || len < 1) {
115                 return nixio__pstatus(L, !poll(NULL, 0, timeout));
116         }
117
118         luaL_checktype(L, 1, LUA_TTABLE);
119         struct pollfd *fds = calloc(len, sizeof(struct pollfd));
120
121         for (i = 0; i < len; i++) {
122                 lua_rawgeti(L, 1, i+1);
123                 if (!lua_istable(L, -1)) {
124                         free(fds);
125                         return luaL_argerror(L, 1, "invalid datastructure");
126                 }
127
128                 lua_pushliteral(L, "fd");
129                 lua_rawget(L, -2);
130                 fd = nixio__tofd(L, -1);
131                 if (fd == -1) {
132                         free(fds);
133                         return luaL_argerror(L, 1, "invalid fd in datastructure");
134                 }
135                 fds[i].fd = fd;
136
137                 lua_pushliteral(L, "events");
138                 lua_rawget(L, -3);
139                 fds[i].events = (short)lua_tointeger(L, -1);
140
141                 lua_pop(L, 3);
142         }
143
144         status = poll(fds, (nfds_t)len, timeout);
145
146         if (status < 1) {
147                 free(fds);
148                 return nixio__perror(L);
149         }
150
151         for (i = 0; i < len; i++) {
152                 lua_rawgeti(L, 1, i+1);
153
154                 lua_pushliteral(L, "revents");
155                 lua_pushinteger(L, fds[i].revents);
156                 lua_rawset(L, -3);
157
158                 lua_pop(L, 1);
159         }
160
161         free(fds);
162
163         lua_pushinteger(L, status);
164         lua_pushvalue(L, 1);
165
166         return 2;
167 }
168
169 /* module table */
170 static const luaL_reg R[] = {
171         {"nanosleep",   nixio_nanosleep},
172         {"poll",                nixio_poll},
173         {"poll_flags",  nixio_poll_flags},
174         {NULL,                  NULL}
175 };
176
177 void nixio_open_poll(lua_State *L) {
178         luaL_register(L, NULL, R);
179 }