2 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 struct lua_uloop_timeout {
35 struct uloop_timeout t;
39 struct lua_uloop_process {
40 struct uloop_process p;
44 static lua_State *state;
46 static void ul_timer_cb(struct uloop_timeout *t)
48 struct lua_uloop_timeout *tout = container_of(t, struct lua_uloop_timeout, t);
50 lua_getglobal(state, "__uloop_cb");
51 lua_rawgeti(state, -1, tout->r);
52 lua_remove(state, -2);
54 lua_call(state, 0, 0);
58 static int ul_timer_set(lua_State *L)
60 struct lua_uloop_timeout *tout;
63 if (!lua_isnumber(L, -1)) {
64 lua_pushstring(L, "invalid arg list");
70 set = lua_tointeger(L, -1);
71 tout = lua_touserdata(L, 1);
72 uloop_timeout_set(&tout->t, set);
77 static int ul_timer_free(lua_State *L)
79 struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
81 uloop_timeout_cancel(&tout->t);
83 /* obj.__index.__gc = nil , make sure executing only once*/
84 lua_getfield(L, -1, "__index");
85 lua_pushstring(L, "__gc");
89 lua_getglobal(state, "__uloop_cb");
90 luaL_unref(state, -1, tout->r);
95 static const luaL_Reg timer_m[] = {
96 { "set", ul_timer_set },
97 { "cancel", ul_timer_free },
101 static int ul_timer(lua_State *L)
103 struct lua_uloop_timeout *tout;
107 if (lua_isnumber(L, -1)) {
108 set = lua_tointeger(L, -1);
112 if (!lua_isfunction(L, -1)) {
113 lua_pushstring(L, "invalid arg list");
119 lua_getglobal(L, "__uloop_cb");
120 lua_pushvalue(L, -2);
121 ref = luaL_ref(L, -2);
123 tout = lua_newuserdata(L, sizeof(*tout));
124 lua_createtable(L, 0, 2);
125 lua_pushvalue(L, -1);
126 lua_setfield(L, -2, "__index");
127 lua_pushcfunction(L, ul_timer_free);
128 lua_setfield(L, -2, "__gc");
129 lua_pushvalue(L, -1);
130 lua_setmetatable(L, -3);
131 lua_pushvalue(L, -2);
132 luaI_openlib(L, NULL, timer_m, 1);
133 lua_pushvalue(L, -2);
135 memset(tout, 0, sizeof(*tout));
138 tout->t.cb = ul_timer_cb;
140 uloop_timeout_set(&tout->t, set);
145 static void ul_ufd_cb(struct uloop_fd *fd, unsigned int events)
147 struct lua_uloop_fd *ufd = container_of(fd, struct lua_uloop_fd, fd);
149 lua_getglobal(state, "__uloop_cb");
150 lua_rawgeti(state, -1, ufd->r);
151 lua_remove(state, -2);
154 lua_getglobal(state, "__uloop_fds");
155 lua_rawgeti(state, -1, ufd->fd_r);
156 lua_remove(state, -2);
159 lua_pushinteger(state, events);
160 lua_call(state, 2, 0);
164 static int get_sock_fd(lua_State* L, int idx) {
166 if(lua_isnumber(L, idx)) {
167 fd = lua_tonumber(L, idx);
169 luaL_checktype(L, idx, LUA_TUSERDATA);
170 lua_getfield(L, idx, "getfd");
172 return luaL_error(L, "socket type missing 'getfd' method");
173 lua_pushvalue(L, idx - 1);
175 fd = lua_tointeger(L, -1);
181 static int ul_ufd_delete(lua_State *L)
183 struct lua_uloop_fd *ufd = lua_touserdata(L, 1);
185 uloop_fd_delete(&ufd->fd);
187 /* obj.__index.__gc = nil , make sure executing only once*/
188 lua_getfield(L, -1, "__index");
189 lua_pushstring(L, "__gc");
193 lua_getglobal(state, "__uloop_cb");
194 luaL_unref(state, -1, ufd->r);
195 lua_remove(state, -1);
197 lua_getglobal(state, "__uloop_fds");
198 luaL_unref(state, -1, ufd->fd_r);
199 lua_remove(state, -1);
204 static const luaL_Reg ufd_m[] = {
205 { "delete", ul_ufd_delete },
209 static int ul_ufd_add(lua_State *L)
211 struct lua_uloop_fd *ufd;
213 unsigned int flags = 0;
217 if (lua_isnumber(L, -1)) {
218 flags = lua_tointeger(L, -1);
222 if (!lua_isfunction(L, -1)) {
223 lua_pushstring(L, "invalid arg list");
229 fd = get_sock_fd(L, -2);
231 lua_getglobal(L, "__uloop_cb");
232 lua_pushvalue(L, -2);
233 ref = luaL_ref(L, -2);
236 lua_getglobal(L, "__uloop_fds");
237 lua_pushvalue(L, -3);
238 fd_ref = luaL_ref(L, -2);
241 ufd = lua_newuserdata(L, sizeof(*ufd));
243 lua_createtable(L, 0, 2);
244 lua_pushvalue(L, -1);
245 lua_setfield(L, -2, "__index");
246 lua_pushcfunction(L, ul_ufd_delete);
247 lua_setfield(L, -2, "__gc");
248 lua_pushvalue(L, -1);
249 lua_setmetatable(L, -3);
250 lua_pushvalue(L, -2);
251 luaI_openlib(L, NULL, ufd_m, 1);
252 lua_pushvalue(L, -2);
254 memset(ufd, 0, sizeof(*ufd));
259 ufd->fd.cb = ul_ufd_cb;
261 uloop_fd_add(&ufd->fd, flags);
266 static void ul_process_cb(struct uloop_process *p, int ret)
268 struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
270 lua_getglobal(state, "__uloop_cb");
271 lua_rawgeti(state, -1, proc->r);
273 luaL_unref(state, -2, proc->r);
274 lua_remove(state, -2);
275 lua_pushinteger(state, ret >> 8);
276 lua_call(state, 1, 0);
279 static int ul_process(lua_State *L)
281 struct lua_uloop_process *proc;
285 if (!lua_isfunction(L, -1) || !lua_istable(L, -2) ||
286 !lua_istable(L, -3) || !lua_isstring(L, -4)) {
287 lua_pushstring(L, "invalid arg list");
296 lua_pushstring(L, "failed to fork");
304 int argn = lua_objlen(L, -3);
305 int envn = lua_objlen(L, -2);
306 char** argp = malloc(sizeof(char*) * (argn + 2));
307 char** envp = malloc(sizeof(char*) * envn + 1);
310 argp[0] = (char*) lua_tostring(L, -4);
311 for (i = 1; i <= argn; i++) {
312 lua_rawgeti(L, -3, i);
313 argp[i] = (char*) lua_tostring(L, -1);
318 for (i = 1; i <= envn; i++) {
319 lua_rawgeti(L, -2, i);
320 envp[i - 1] = (char*) lua_tostring(L, -1);
325 execve(*argp, argp, envp);
329 lua_getglobal(L, "__uloop_cb");
330 lua_pushvalue(L, -2);
331 ref = luaL_ref(L, -2);
333 proc = lua_newuserdata(L, sizeof(*proc));
334 memset(proc, 0, sizeof(*proc));
338 proc->p.cb = ul_process_cb;
339 uloop_process_add(&proc->p);
344 static int ul_init(lua_State *L)
347 lua_pushboolean(L, 1);
352 static int ul_run(lua_State *L)
355 lua_pushboolean(L, 1);
360 static int ul_end(lua_State *L)
366 static luaL_reg uloop_func[] = {
370 {"process", ul_process},
371 {"fd_add", ul_ufd_add},
376 /* avoid warnings about missing declarations */
377 int luaopen_uloop(lua_State *L);
378 int luaclose_uloop(lua_State *L);
380 int luaopen_uloop(lua_State *L)
384 lua_createtable(L, 1, 0);
385 lua_setglobal(L, "__uloop_cb");
387 lua_createtable(L, 1, 0);
388 lua_setglobal(L, "__uloop_fds");
390 luaL_openlib(L, "uloop", uloop_func, 0);
391 lua_pushstring(L, "_VERSION");
392 lua_pushstring(L, "1.0");
395 lua_pushstring(L, "ULOOP_READ");
396 lua_pushinteger(L, ULOOP_READ);
399 lua_pushstring(L, "ULOOP_WRITE");
400 lua_pushinteger(L, ULOOP_WRITE);
403 lua_pushstring(L, "ULOOP_EDGE_TRIGGER");
404 lua_pushinteger(L, ULOOP_EDGE_TRIGGER);
407 lua_pushstring(L, "ULOOP_BLOCKING");
408 lua_pushinteger(L, ULOOP_BLOCKING);
414 int luaclose_uloop(lua_State *L)
416 lua_pushstring(L, "Called");