b4aec2c896f19382ab0c4800d10c7f9bf1005689
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
;
47 ul_create_userdata(lua_State
*L
, size_t size
, const luaL_Reg
*reg
, lua_CFunction gc
)
49 void *ret
= lua_newuserdata(L
, size
);
52 lua_createtable(L
, 0, 2);
54 lua_setfield(L
, -2, "__index");
55 lua_pushcfunction(L
, gc
);
56 lua_setfield(L
, -2, "__gc");
58 lua_setmetatable(L
, -3);
60 luaI_openlib(L
, NULL
, reg
, 1);
66 static void ul_timer_cb(struct uloop_timeout
*t
)
68 struct lua_uloop_timeout
*tout
= container_of(t
, struct lua_uloop_timeout
, t
);
70 lua_getglobal(state
, "__uloop_cb");
71 lua_rawgeti(state
, -1, tout
->r
);
72 lua_remove(state
, -2);
74 lua_call(state
, 0, 0);
78 static int ul_timer_set(lua_State
*L
)
80 struct lua_uloop_timeout
*tout
;
83 if (!lua_isnumber(L
, -1)) {
84 lua_pushstring(L
, "invalid arg list");
90 set
= lua_tointeger(L
, -1);
91 tout
= lua_touserdata(L
, 1);
92 uloop_timeout_set(&tout
->t
, set
);
97 static int ul_timer_free(lua_State
*L
)
99 struct lua_uloop_timeout
*tout
= lua_touserdata(L
, 1);
101 uloop_timeout_cancel(&tout
->t
);
103 /* obj.__index.__gc = nil , make sure executing only once*/
104 lua_getfield(L
, -1, "__index");
105 lua_pushstring(L
, "__gc");
109 lua_getglobal(state
, "__uloop_cb");
110 luaL_unref(state
, -1, tout
->r
);
115 static const luaL_Reg timer_m
[] = {
116 { "set", ul_timer_set
},
117 { "cancel", ul_timer_free
},
121 static int ul_timer(lua_State
*L
)
123 struct lua_uloop_timeout
*tout
;
127 if (lua_isnumber(L
, -1)) {
128 set
= lua_tointeger(L
, -1);
132 if (!lua_isfunction(L
, -1)) {
133 lua_pushstring(L
, "invalid arg list");
139 lua_getglobal(L
, "__uloop_cb");
140 lua_pushvalue(L
, -2);
141 ref
= luaL_ref(L
, -2);
143 tout
= ul_create_userdata(L
, sizeof(*tout
), timer_m
, ul_timer_free
);
145 tout
->t
.cb
= ul_timer_cb
;
148 uloop_timeout_set(&tout
->t
, set
);
153 static void ul_ufd_cb(struct uloop_fd
*fd
, unsigned int events
)
155 struct lua_uloop_fd
*ufd
= container_of(fd
, struct lua_uloop_fd
, fd
);
157 lua_getglobal(state
, "__uloop_cb");
158 lua_rawgeti(state
, -1, ufd
->r
);
159 lua_remove(state
, -2);
162 lua_getglobal(state
, "__uloop_fds");
163 lua_rawgeti(state
, -1, ufd
->fd_r
);
164 lua_remove(state
, -2);
167 lua_pushinteger(state
, events
);
168 lua_call(state
, 2, 0);
172 static int get_sock_fd(lua_State
* L
, int idx
) {
174 if(lua_isnumber(L
, idx
)) {
175 fd
= lua_tonumber(L
, idx
);
177 luaL_checktype(L
, idx
, LUA_TUSERDATA
);
178 lua_getfield(L
, idx
, "getfd");
180 return luaL_error(L
, "socket type missing 'getfd' method");
181 lua_pushvalue(L
, idx
- 1);
183 fd
= lua_tointeger(L
, -1);
189 static int ul_ufd_delete(lua_State
*L
)
191 struct lua_uloop_fd
*ufd
= lua_touserdata(L
, 1);
193 uloop_fd_delete(&ufd
->fd
);
195 /* obj.__index.__gc = nil , make sure executing only once*/
196 lua_getfield(L
, -1, "__index");
197 lua_pushstring(L
, "__gc");
201 lua_getglobal(state
, "__uloop_cb");
202 luaL_unref(state
, -1, ufd
->r
);
203 lua_remove(state
, -1);
205 lua_getglobal(state
, "__uloop_fds");
206 luaL_unref(state
, -1, ufd
->fd_r
);
207 lua_remove(state
, -1);
212 static const luaL_Reg ufd_m
[] = {
213 { "delete", ul_ufd_delete
},
217 static int ul_ufd_add(lua_State
*L
)
219 struct lua_uloop_fd
*ufd
;
221 unsigned int flags
= 0;
225 if (lua_isnumber(L
, -1)) {
226 flags
= lua_tointeger(L
, -1);
230 if (!lua_isfunction(L
, -1)) {
231 lua_pushstring(L
, "invalid arg list");
237 fd
= get_sock_fd(L
, -2);
239 lua_getglobal(L
, "__uloop_cb");
240 lua_pushvalue(L
, -2);
241 ref
= luaL_ref(L
, -2);
244 lua_getglobal(L
, "__uloop_fds");
245 lua_pushvalue(L
, -3);
246 fd_ref
= luaL_ref(L
, -2);
249 ufd
= ul_create_userdata(L
, sizeof(*ufd
), ufd_m
, ul_ufd_delete
);
253 ufd
->fd
.cb
= ul_ufd_cb
;
255 uloop_fd_add(&ufd
->fd
, flags
);
260 static void ul_process_cb(struct uloop_process
*p
, int ret
)
262 struct lua_uloop_process
*proc
= container_of(p
, struct lua_uloop_process
, p
);
264 lua_getglobal(state
, "__uloop_cb");
265 lua_rawgeti(state
, -1, proc
->r
);
267 luaL_unref(state
, -2, proc
->r
);
268 lua_remove(state
, -2);
269 lua_pushinteger(state
, ret
>> 8);
270 lua_call(state
, 1, 0);
273 static int ul_process(lua_State
*L
)
275 struct lua_uloop_process
*proc
;
279 if (!lua_isfunction(L
, -1) || !lua_istable(L
, -2) ||
280 !lua_istable(L
, -3) || !lua_isstring(L
, -4)) {
281 lua_pushstring(L
, "invalid arg list");
290 lua_pushstring(L
, "failed to fork");
298 int argn
= lua_objlen(L
, -3);
299 int envn
= lua_objlen(L
, -2);
300 char** argp
= malloc(sizeof(char*) * (argn
+ 2));
301 char** envp
= malloc(sizeof(char*) * envn
+ 1);
304 argp
[0] = (char*) lua_tostring(L
, -4);
305 for (i
= 1; i
<= argn
; i
++) {
306 lua_rawgeti(L
, -3, i
);
307 argp
[i
] = (char*) lua_tostring(L
, -1);
312 for (i
= 1; i
<= envn
; i
++) {
313 lua_rawgeti(L
, -2, i
);
314 envp
[i
- 1] = (char*) lua_tostring(L
, -1);
319 execve(*argp
, argp
, envp
);
323 lua_getglobal(L
, "__uloop_cb");
324 lua_pushvalue(L
, -2);
325 ref
= luaL_ref(L
, -2);
327 proc
= lua_newuserdata(L
, sizeof(*proc
));
328 memset(proc
, 0, sizeof(*proc
));
332 proc
->p
.cb
= ul_process_cb
;
333 uloop_process_add(&proc
->p
);
338 static int ul_init(lua_State
*L
)
341 lua_pushboolean(L
, 1);
346 static int ul_run(lua_State
*L
)
349 lua_pushboolean(L
, 1);
354 static int ul_end(lua_State
*L
)
360 static luaL_reg uloop_func
[] = {
364 {"process", ul_process
},
365 {"fd_add", ul_ufd_add
},
370 /* avoid warnings about missing declarations */
371 int luaopen_uloop(lua_State
*L
);
372 int luaclose_uloop(lua_State
*L
);
374 int luaopen_uloop(lua_State
*L
)
378 lua_createtable(L
, 1, 0);
379 lua_setglobal(L
, "__uloop_cb");
381 lua_createtable(L
, 1, 0);
382 lua_setglobal(L
, "__uloop_fds");
384 luaL_openlib(L
, "uloop", uloop_func
, 0);
385 lua_pushstring(L
, "_VERSION");
386 lua_pushstring(L
, "1.0");
389 lua_pushstring(L
, "ULOOP_READ");
390 lua_pushinteger(L
, ULOOP_READ
);
393 lua_pushstring(L
, "ULOOP_WRITE");
394 lua_pushinteger(L
, ULOOP_WRITE
);
397 lua_pushstring(L
, "ULOOP_EDGE_TRIGGER");
398 lua_pushinteger(L
, ULOOP_EDGE_TRIGGER
);
401 lua_pushstring(L
, "ULOOP_BLOCKING");
402 lua_pushinteger(L
, ULOOP_BLOCKING
);
408 int luaclose_uloop(lua_State
*L
)
410 lua_pushstring(L
, "Called");