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.
25 #include <libubox/uloop.h>
26 #include <libubox/list.h>
28 struct lua_uloop_timeout {
29 struct uloop_timeout t;
33 struct lua_uloop_process {
34 struct uloop_process p;
38 static lua_State *state;
40 static void ul_timer_cb(struct uloop_timeout *t)
42 struct lua_uloop_timeout *tout = container_of(t, struct lua_uloop_timeout, t);
44 lua_getglobal(state, "__uloop_cb");
45 lua_rawgeti(state, -1, tout->r);
46 lua_call(state, 0, 0);
49 static int ul_timer_set(lua_State *L)
51 struct lua_uloop_timeout *tout;
54 if (!lua_isnumber(L, -1)) {
55 lua_pushstring(L, "invalid arg list");
61 set = lua_tointeger(L, -1);
62 tout = lua_touserdata(L, 1);
63 uloop_timeout_set(&tout->t, set);
68 static int ul_timer_free(lua_State *L)
70 struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
72 uloop_timeout_cancel(&tout->t);
73 lua_getglobal(state, "__uloop_cb");
74 luaL_unref(L, -1, tout->r);
79 static const luaL_Reg timer_m[] = {
80 { "set", ul_timer_set },
81 { "cancel", ul_timer_free },
85 static int ul_timer(lua_State *L)
87 struct lua_uloop_timeout *tout;
91 if (lua_isnumber(L, -1)) {
92 set = lua_tointeger(L, -1);
96 if (!lua_isfunction(L, -1)) {
97 lua_pushstring(L, "invalid arg list");
103 lua_getglobal(L, "__uloop_cb");
104 lua_pushvalue(L, -2);
105 ref = luaL_ref(L, -2);
107 tout = lua_newuserdata(L, sizeof(*tout));
108 lua_createtable(L, 0, 2);
109 lua_pushvalue(L, -1);
110 lua_setfield(L, -2, "__index");
111 lua_pushcfunction(L, ul_timer_free);
112 lua_setfield(L, -2, "__gc");
113 lua_pushvalue(L, -1);
114 lua_setmetatable(L, -3);
115 lua_pushvalue(L, -2);
116 luaI_openlib(L, NULL, timer_m, 1);
117 lua_pushvalue(L, -2);
119 memset(tout, 0, sizeof(*tout));
122 tout->t.cb = ul_timer_cb;
124 uloop_timeout_set(&tout->t, set);
129 static void proc_cb(struct uloop_process *p, int ret)
131 struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
133 lua_getglobal(state, "__uloop_cb");
134 lua_rawgeti(state, -1, proc->r);
135 luaL_unref(state, -2, proc->r);
136 lua_pushinteger(state, ret >> 8);
137 lua_call(state, 1, 0);
140 static int ul_process(lua_State *L)
142 struct lua_uloop_process *proc;
146 if (!lua_isfunction(L, -1) || !lua_istable(L, -2) ||
147 !lua_istable(L, -3) || !lua_isstring(L, -4)) {
148 lua_pushstring(L, "invalid arg list");
157 lua_pushstring(L, "failed to fork");
165 int argn = lua_objlen(L, -3);
166 int envn = lua_objlen(L, -2);
167 char** argp = malloc(sizeof(char*) * (argn + 2));
168 char** envp = malloc(sizeof(char*) * envn + 1);
171 argp[0] = (char*) lua_tostring(L, -4);
172 for (i = 1; i <= argn; i++) {
173 lua_rawgeti(L, -3, i);
174 argp[i] = (char*) lua_tostring(L, -1);
179 for (i = 1; i <= envn; i++) {
180 lua_rawgeti(L, -2, i);
181 envp[i - 1] = (char*) lua_tostring(L, -1);
186 execve(*argp, argp, envp);
190 lua_getglobal(L, "__uloop_cb");
191 lua_pushvalue(L, -2);
192 ref = luaL_ref(L, -2);
194 proc = lua_newuserdata(L, sizeof(*proc));
195 memset(proc, 0, sizeof(*proc));
199 proc->p.cb = proc_cb;
200 uloop_process_add(&proc->p);
205 static int ul_init(lua_State *L)
208 lua_pushboolean(L, 1);
213 static int ul_run(lua_State *L)
216 lua_pushboolean(L, 1);
221 static luaL_reg uloop_func[] = {
225 {"process", ul_process},
229 int luaopen_uloop(lua_State *L)
233 lua_createtable(L, 1, 0);
234 lua_setglobal(L, "__uloop_cb");
236 luaL_openlib(L, "uloop", uloop_func, 0);
237 lua_pushstring(L, "_VERSION");
238 lua_pushstring(L, "1.0");
244 int luaclose_uloop(lua_State *L)
246 lua_pushstring(L, "Called");