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_remaining(lua_State
*L
)
99 struct lua_uloop_timeout
*tout
;
101 tout
= lua_touserdata(L
, 1);
102 lua_pushnumber(L
, uloop_timeout_remaining(&tout
->t
));
106 static int ul_timer_free(lua_State
*L
)
108 struct lua_uloop_timeout
*tout
= lua_touserdata(L
, 1);
110 uloop_timeout_cancel(&tout
->t
);
112 /* obj.__index.__gc = nil , make sure executing only once*/
113 lua_getfield(L
, -1, "__index");
114 lua_pushstring(L
, "__gc");
118 lua_getglobal(state
, "__uloop_cb");
119 luaL_unref(state
, -1, tout
->r
);
124 static const luaL_Reg timer_m
[] = {
125 { "set", ul_timer_set
},
126 { "remaining", ul_timer_remaining
},
127 { "cancel", ul_timer_free
},
131 static int ul_timer(lua_State
*L
)
133 struct lua_uloop_timeout
*tout
;
137 if (lua_isnumber(L
, -1)) {
138 set
= lua_tointeger(L
, -1);
142 if (!lua_isfunction(L
, -1)) {
143 lua_pushstring(L
, "invalid arg list");
149 lua_getglobal(L
, "__uloop_cb");
150 lua_pushvalue(L
, -2);
151 ref
= luaL_ref(L
, -2);
153 tout
= ul_create_userdata(L
, sizeof(*tout
), timer_m
, ul_timer_free
);
155 tout
->t
.cb
= ul_timer_cb
;
158 uloop_timeout_set(&tout
->t
, set
);
163 static void ul_ufd_cb(struct uloop_fd
*fd
, unsigned int events
)
165 struct lua_uloop_fd
*ufd
= container_of(fd
, struct lua_uloop_fd
, fd
);
167 lua_getglobal(state
, "__uloop_cb");
168 lua_rawgeti(state
, -1, ufd
->r
);
169 lua_remove(state
, -2);
172 lua_getglobal(state
, "__uloop_fds");
173 lua_rawgeti(state
, -1, ufd
->fd_r
);
174 lua_remove(state
, -2);
177 lua_pushinteger(state
, events
);
178 lua_call(state
, 2, 0);
182 static int get_sock_fd(lua_State
* L
, int idx
) {
184 if(lua_isnumber(L
, idx
)) {
185 fd
= lua_tonumber(L
, idx
);
187 luaL_checktype(L
, idx
, LUA_TUSERDATA
);
188 lua_getfield(L
, idx
, "getfd");
190 return luaL_error(L
, "socket type missing 'getfd' method");
191 /* if we have absolute, no need to adjust for getfield() call */
192 lua_pushvalue(L
, idx
> 0 ? idx
: idx
- 1);
194 fd
= lua_tointeger(L
, -1);
200 static int ul_ufd_delete(lua_State
*L
)
202 struct lua_uloop_fd
*ufd
= lua_touserdata(L
, 1);
204 uloop_fd_delete(&ufd
->fd
);
206 /* obj.__index.__gc = nil , make sure executing only once*/
207 lua_getfield(L
, -1, "__index");
208 lua_pushstring(L
, "__gc");
212 lua_getglobal(state
, "__uloop_cb");
213 luaL_unref(state
, -1, ufd
->r
);
214 lua_remove(state
, -1);
216 lua_getglobal(state
, "__uloop_fds");
217 luaL_unref(state
, -1, ufd
->fd_r
);
218 lua_remove(state
, -1);
223 static const luaL_Reg ufd_m
[] = {
224 { "delete", ul_ufd_delete
},
228 static int ul_ufd_add(lua_State
*L
)
230 struct lua_uloop_fd
*ufd
;
232 unsigned int flags
= 0;
236 flags
= luaL_checkinteger(L
, -1);
238 lua_pushstring(L
, "flags cannot be zero");
243 luaL_checktype(L
, -1, LUA_TFUNCTION
);
245 fd
= get_sock_fd(L
, -2);
247 lua_getglobal(L
, "__uloop_cb");
248 lua_pushvalue(L
, -2);
249 ref
= luaL_ref(L
, -2);
252 lua_getglobal(L
, "__uloop_fds");
253 lua_pushvalue(L
, -3);
254 fd_ref
= luaL_ref(L
, -2);
257 ufd
= ul_create_userdata(L
, sizeof(*ufd
), ufd_m
, ul_ufd_delete
);
261 ufd
->fd
.cb
= ul_ufd_cb
;
262 uloop_fd_add(&ufd
->fd
, flags
);
267 static int ul_process_free(lua_State
*L
)
269 struct lua_uloop_process
*proc
= lua_touserdata(L
, 1);
271 /* obj.__index.__gc = nil , make sure executing only once*/
272 lua_getfield(L
, -1, "__index");
273 lua_pushstring(L
, "__gc");
277 if (proc
->r
!= LUA_NOREF
) {
278 uloop_process_delete(&proc
->p
);
280 lua_getglobal(state
, "__uloop_cb");
281 luaL_unref(state
, -1, proc
->r
);
282 lua_remove(state
, -1);
288 static int ul_process_pid(lua_State
*L
)
290 struct lua_uloop_process
*proc
= lua_touserdata(L
, 1);
293 lua_pushnumber(L
, proc
->p
.pid
);
300 static const luaL_Reg process_m
[] = {
301 { "delete", ul_process_free
},
302 { "pid", ul_process_pid
},
306 static void ul_process_cb(struct uloop_process
*p
, int ret
)
308 struct lua_uloop_process
*proc
= container_of(p
, struct lua_uloop_process
, p
);
310 lua_getglobal(state
, "__uloop_cb");
311 lua_rawgeti(state
, -1, proc
->r
);
313 luaL_unref(state
, -2, proc
->r
);
315 lua_remove(state
, -2);
316 lua_pushinteger(state
, ret
>> 8);
317 lua_call(state
, 1, 0);
320 static int ul_process(lua_State
*L
)
322 struct lua_uloop_process
*proc
;
326 if (!lua_isfunction(L
, -1) || !lua_istable(L
, -2) ||
327 !lua_istable(L
, -3) || !lua_isstring(L
, -4)) {
328 lua_pushstring(L
, "invalid arg list");
337 lua_pushstring(L
, "failed to fork");
345 int argn
= lua_objlen(L
, -3);
346 int envn
= lua_objlen(L
, -2);
347 char** argp
= malloc(sizeof(char*) * (argn
+ 2));
348 char** envp
= malloc(sizeof(char*) * (envn
+ 1));
354 argp
[0] = (char*) lua_tostring(L
, -4);
355 for (i
= 1; i
<= argn
; i
++) {
356 lua_rawgeti(L
, -3, i
);
357 argp
[i
] = (char*) lua_tostring(L
, -1);
362 for (i
= 1; i
<= envn
; i
++) {
363 lua_rawgeti(L
, -2, i
);
364 envp
[i
- 1] = (char*) lua_tostring(L
, -1);
369 execve(*argp
, argp
, envp
);
373 lua_getglobal(L
, "__uloop_cb");
374 lua_pushvalue(L
, -2);
375 ref
= luaL_ref(L
, -2);
377 proc
= ul_create_userdata(L
, sizeof(*proc
), process_m
, ul_process_free
);
380 proc
->p
.cb
= ul_process_cb
;
381 uloop_process_add(&proc
->p
);
386 static int ul_init(lua_State
*L
)
389 lua_pushboolean(L
, 1);
394 static int ul_run(lua_State
*L
)
397 lua_pushboolean(L
, 1);
402 static int ul_end(lua_State
*L
)
408 static luaL_reg uloop_func
[] = {
412 {"process", ul_process
},
413 {"fd_add", ul_ufd_add
},
418 /* avoid warnings about missing declarations */
419 int luaopen_uloop(lua_State
*L
);
420 int luaclose_uloop(lua_State
*L
);
422 int luaopen_uloop(lua_State
*L
)
426 lua_createtable(L
, 1, 0);
427 lua_setglobal(L
, "__uloop_cb");
429 lua_createtable(L
, 1, 0);
430 lua_setglobal(L
, "__uloop_fds");
432 luaL_openlib(L
, "uloop", uloop_func
, 0);
433 lua_pushstring(L
, "_VERSION");
434 lua_pushstring(L
, "1.0");
437 lua_pushstring(L
, "ULOOP_READ");
438 lua_pushinteger(L
, ULOOP_READ
);
441 lua_pushstring(L
, "ULOOP_WRITE");
442 lua_pushinteger(L
, ULOOP_WRITE
);
445 lua_pushstring(L
, "ULOOP_EDGE_TRIGGER");
446 lua_pushinteger(L
, ULOOP_EDGE_TRIGGER
);
449 lua_pushstring(L
, "ULOOP_BLOCKING");
450 lua_pushinteger(L
, ULOOP_BLOCKING
);
456 int luaclose_uloop(lua_State
*L
)
458 lua_pushstring(L
, "Called");