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);
82 lua_getglobal(state, "__uloop_cb");
83 luaL_unref(L, -1, tout->r);
88 static const luaL_Reg timer_m[] = {
89 { "set", ul_timer_set },
90 { "cancel", ul_timer_free },
94 static int ul_timer(lua_State *L)
96 struct lua_uloop_timeout *tout;
100 if (lua_isnumber(L, -1)) {
101 set = lua_tointeger(L, -1);
105 if (!lua_isfunction(L, -1)) {
106 lua_pushstring(L, "invalid arg list");
112 lua_getglobal(L, "__uloop_cb");
113 lua_pushvalue(L, -2);
114 ref = luaL_ref(L, -2);
116 tout = lua_newuserdata(L, sizeof(*tout));
117 lua_createtable(L, 0, 2);
118 lua_pushvalue(L, -1);
119 lua_setfield(L, -2, "__index");
120 lua_pushcfunction(L, ul_timer_free);
121 lua_setfield(L, -2, "__gc");
122 lua_pushvalue(L, -1);
123 lua_setmetatable(L, -3);
124 lua_pushvalue(L, -2);
125 luaI_openlib(L, NULL, timer_m, 1);
126 lua_pushvalue(L, -2);
128 memset(tout, 0, sizeof(*tout));
131 tout->t.cb = ul_timer_cb;
133 uloop_timeout_set(&tout->t, set);
138 static void ul_ufd_cb(struct uloop_fd *fd, unsigned int events)
140 struct lua_uloop_fd *ufd = container_of(fd, struct lua_uloop_fd, fd);
142 lua_getglobal(state, "__uloop_cb");
143 lua_rawgeti(state, -1, ufd->r);
144 lua_remove(state, -2);
147 lua_getglobal(state, "__uloop_fds");
148 lua_rawgeti(state, -1, ufd->fd_r);
149 lua_remove(state, -2);
152 lua_pushinteger(state, events);
154 lua_call(state, 2, 0);
158 static int get_sock_fd(lua_State* L, int idx) {
160 if(lua_isnumber(L, idx)) {
161 fd = lua_tonumber(L, idx);
163 luaL_checktype(L, idx, LUA_TUSERDATA);
164 lua_getfield(L, idx, "getfd");
166 return luaL_error(L, "socket type missing 'getfd' method");
167 lua_pushvalue(L, idx - 1);
169 fd = lua_tointeger(L, -1);
175 static int ul_ufd_delete(lua_State *L)
177 struct lua_uloop_fd *ufd = lua_touserdata(L, 1);
179 uloop_fd_delete(&ufd->fd);
180 lua_getglobal(state, "__uloop_cb");
181 luaL_unref(L, -1, ufd->r);
182 lua_getglobal(state, "__uloop_fds");
183 luaL_unref(L, -1, ufd->fd_r);
188 static const luaL_Reg ufd_m[] = {
189 { "delete", ul_ufd_delete },
193 static int ul_ufd_add(lua_State *L)
195 struct lua_uloop_fd *ufd;
197 unsigned int flags = 0;
201 if (lua_isnumber(L, -1)) {
202 flags = lua_tointeger(L, -1);
206 if (!lua_isfunction(L, -1)) {
207 lua_pushstring(L, "invalid arg list");
213 fd = get_sock_fd(L, -2);
215 lua_getglobal(L, "__uloop_cb");
216 lua_pushvalue(L, -2);
217 ref = luaL_ref(L, -2);
220 lua_getglobal(L, "__uloop_fds");
221 lua_pushvalue(L, -3);
222 fd_ref = luaL_ref(L, -2);
225 ufd = lua_newuserdata(L, sizeof(*ufd));
227 lua_createtable(L, 0, 2);
228 lua_pushvalue(L, -1);
229 lua_setfield(L, -2, "__index");
230 lua_pushcfunction(L, ul_ufd_delete);
231 lua_setfield(L, -2, "__gc");
232 lua_pushvalue(L, -1);
233 lua_setmetatable(L, -3);
234 lua_pushvalue(L, -2);
235 luaI_openlib(L, NULL, ufd_m, 1);
236 lua_pushvalue(L, -2);
238 memset(ufd, 0, sizeof(*ufd));
243 ufd->fd.cb = ul_ufd_cb;
245 uloop_fd_add(&ufd->fd, flags);
250 static void ul_process_cb(struct uloop_process *p, int ret)
252 struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
254 lua_getglobal(state, "__uloop_cb");
255 lua_rawgeti(state, -1, proc->r);
257 luaL_unref(state, -2, proc->r);
258 lua_remove(state, -2);
259 lua_pushinteger(state, ret >> 8);
260 lua_call(state, 1, 0);
263 static int ul_process(lua_State *L)
265 struct lua_uloop_process *proc;
269 if (!lua_isfunction(L, -1) || !lua_istable(L, -2) ||
270 !lua_istable(L, -3) || !lua_isstring(L, -4)) {
271 lua_pushstring(L, "invalid arg list");
280 lua_pushstring(L, "failed to fork");
288 int argn = lua_objlen(L, -3);
289 int envn = lua_objlen(L, -2);
290 char** argp = malloc(sizeof(char*) * (argn + 2));
291 char** envp = malloc(sizeof(char*) * envn + 1);
294 argp[0] = (char*) lua_tostring(L, -4);
295 for (i = 1; i <= argn; i++) {
296 lua_rawgeti(L, -3, i);
297 argp[i] = (char*) lua_tostring(L, -1);
302 for (i = 1; i <= envn; i++) {
303 lua_rawgeti(L, -2, i);
304 envp[i - 1] = (char*) lua_tostring(L, -1);
309 execve(*argp, argp, envp);
313 lua_getglobal(L, "__uloop_cb");
314 lua_pushvalue(L, -2);
315 ref = luaL_ref(L, -2);
317 proc = lua_newuserdata(L, sizeof(*proc));
318 memset(proc, 0, sizeof(*proc));
322 proc->p.cb = ul_process_cb;
323 uloop_process_add(&proc->p);
328 static int ul_init(lua_State *L)
331 lua_pushboolean(L, 1);
336 static int ul_run(lua_State *L)
339 lua_pushboolean(L, 1);
344 static luaL_reg uloop_func[] = {
348 {"process", ul_process},
349 {"fd_add", ul_ufd_add},
353 /* avoid warnings about missing declarations */
354 int luaopen_uloop(lua_State *L);
355 int luaclose_uloop(lua_State *L);
357 int luaopen_uloop(lua_State *L)
361 lua_createtable(L, 1, 0);
362 lua_setglobal(L, "__uloop_cb");
364 lua_createtable(L, 1, 0);
365 lua_setglobal(L, "__uloop_fds");
367 luaL_openlib(L, "uloop", uloop_func, 0);
368 lua_pushstring(L, "_VERSION");
369 lua_pushstring(L, "1.0");
372 lua_pushstring(L, "ULOOP_READ");
373 lua_pushinteger(L, ULOOP_READ);
376 lua_pushstring(L, "ULOOP_WRITE");
377 lua_pushinteger(L, ULOOP_WRITE);
380 lua_pushstring(L, "ULOOP_EDGE_TRIGGER");
381 lua_pushinteger(L, ULOOP_EDGE_TRIGGER);
384 lua_pushstring(L, "ULOOP_BLOCKING");
385 lua_pushinteger(L, ULOOP_BLOCKING);
391 int luaclose_uloop(lua_State *L)
393 lua_pushstring(L, "Called");