45c9bc7ad23ac5945801ff6ca3f08ffa39c6f729
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 struct lua_uloop_interval
{
45 struct uloop_interval i
;
49 static lua_State
*state
;
52 ul_create_userdata(lua_State
*L
, size_t size
, const luaL_Reg
*reg
, lua_CFunction gc
)
54 void *ret
= lua_newuserdata(L
, size
);
57 lua_createtable(L
, 0, 2);
59 lua_setfield(L
, -2, "__index");
60 lua_pushcfunction(L
, gc
);
61 lua_setfield(L
, -2, "__gc");
63 lua_setmetatable(L
, -3);
65 luaI_openlib(L
, NULL
, reg
, 1);
71 static void ul_timer_cb(struct uloop_timeout
*t
)
73 struct lua_uloop_timeout
*tout
= container_of(t
, struct lua_uloop_timeout
, t
);
75 lua_getglobal(state
, "__uloop_cb");
76 lua_rawgeti(state
, -1, tout
->r
);
77 lua_remove(state
, -2);
79 lua_call(state
, 0, 0);
83 static int ul_timer_set(lua_State
*L
)
85 struct lua_uloop_timeout
*tout
;
88 if (!lua_isnumber(L
, -1)) {
89 lua_pushstring(L
, "invalid arg list");
95 set
= lua_tointeger(L
, -1);
96 tout
= lua_touserdata(L
, 1);
97 uloop_timeout_set(&tout
->t
, set
);
102 static int ul_timer_remaining(lua_State
*L
)
104 struct lua_uloop_timeout
*tout
;
106 tout
= lua_touserdata(L
, 1);
107 lua_pushnumber(L
, uloop_timeout_remaining64(&tout
->t
));
111 static int ul_timer_free(lua_State
*L
)
113 struct lua_uloop_timeout
*tout
= lua_touserdata(L
, 1);
115 uloop_timeout_cancel(&tout
->t
);
117 /* obj.__index.__gc = nil , make sure executing only once*/
118 lua_getfield(L
, -1, "__index");
119 lua_pushstring(L
, "__gc");
123 lua_getglobal(state
, "__uloop_cb");
124 luaL_unref(state
, -1, tout
->r
);
129 static const luaL_Reg timer_m
[] = {
130 { "set", ul_timer_set
},
131 { "remaining", ul_timer_remaining
},
132 { "cancel", ul_timer_free
},
136 static int ul_timer(lua_State
*L
)
138 struct lua_uloop_timeout
*tout
;
142 if (lua_isnumber(L
, -1)) {
143 set
= lua_tointeger(L
, -1);
147 if (!lua_isfunction(L
, -1)) {
148 lua_pushstring(L
, "invalid arg list");
154 lua_getglobal(L
, "__uloop_cb");
155 lua_pushvalue(L
, -2);
156 ref
= luaL_ref(L
, -2);
158 tout
= ul_create_userdata(L
, sizeof(*tout
), timer_m
, ul_timer_free
);
160 tout
->t
.cb
= ul_timer_cb
;
163 uloop_timeout_set(&tout
->t
, set
);
168 static void ul_ufd_cb(struct uloop_fd
*fd
, unsigned int events
)
170 struct lua_uloop_fd
*ufd
= container_of(fd
, struct lua_uloop_fd
, fd
);
172 lua_getglobal(state
, "__uloop_cb");
173 lua_rawgeti(state
, -1, ufd
->r
);
174 lua_remove(state
, -2);
177 lua_getglobal(state
, "__uloop_fds");
178 lua_rawgeti(state
, -1, ufd
->fd_r
);
179 lua_remove(state
, -2);
182 lua_pushinteger(state
, events
);
183 lua_call(state
, 2, 0);
187 static int get_sock_fd(lua_State
* L
, int idx
) {
189 if(lua_isnumber(L
, idx
)) {
190 fd
= lua_tonumber(L
, idx
);
192 luaL_checktype(L
, idx
, LUA_TUSERDATA
);
193 lua_getfield(L
, idx
, "getfd");
195 return luaL_error(L
, "socket type missing 'getfd' method");
196 /* if we have absolute, no need to adjust for getfield() call */
197 lua_pushvalue(L
, idx
> 0 ? idx
: idx
- 1);
199 fd
= lua_tointeger(L
, -1);
205 static int ul_ufd_delete(lua_State
*L
)
207 struct lua_uloop_fd
*ufd
= lua_touserdata(L
, 1);
209 uloop_fd_delete(&ufd
->fd
);
211 /* obj.__index.__gc = nil , make sure executing only once*/
212 lua_getfield(L
, -1, "__index");
213 lua_pushstring(L
, "__gc");
217 lua_getglobal(state
, "__uloop_cb");
218 luaL_unref(state
, -1, ufd
->r
);
219 lua_remove(state
, -1);
221 lua_getglobal(state
, "__uloop_fds");
222 luaL_unref(state
, -1, ufd
->fd_r
);
223 lua_remove(state
, -1);
228 static const luaL_Reg ufd_m
[] = {
229 { "delete", ul_ufd_delete
},
233 static int ul_ufd_add(lua_State
*L
)
235 struct lua_uloop_fd
*ufd
;
237 unsigned int flags
= 0;
241 flags
= luaL_checkinteger(L
, 3);
243 lua_pushstring(L
, "flags cannot be zero");
247 luaL_checktype(L
, 2, LUA_TFUNCTION
);
249 fd
= get_sock_fd(L
, 1);
251 lua_getglobal(L
, "__uloop_cb");
253 ref
= luaL_ref(L
, -2);
256 lua_getglobal(L
, "__uloop_fds");
258 fd_ref
= luaL_ref(L
, -2);
261 ufd
= ul_create_userdata(L
, sizeof(*ufd
), ufd_m
, ul_ufd_delete
);
265 ufd
->fd
.cb
= ul_ufd_cb
;
266 uloop_fd_add(&ufd
->fd
, flags
);
271 static int ul_process_free(lua_State
*L
)
273 struct lua_uloop_process
*proc
= lua_touserdata(L
, 1);
275 /* obj.__index.__gc = nil , make sure executing only once*/
276 lua_getfield(L
, -1, "__index");
277 lua_pushstring(L
, "__gc");
281 if (proc
->r
!= LUA_NOREF
) {
282 uloop_process_delete(&proc
->p
);
284 lua_getglobal(state
, "__uloop_cb");
285 luaL_unref(state
, -1, proc
->r
);
286 lua_remove(state
, -1);
292 static int ul_process_pid(lua_State
*L
)
294 struct lua_uloop_process
*proc
= lua_touserdata(L
, 1);
297 lua_pushnumber(L
, proc
->p
.pid
);
304 static const luaL_Reg process_m
[] = {
305 { "delete", ul_process_free
},
306 { "pid", ul_process_pid
},
310 static void ul_process_cb(struct uloop_process
*p
, int ret
)
312 struct lua_uloop_process
*proc
= container_of(p
, struct lua_uloop_process
, p
);
314 lua_getglobal(state
, "__uloop_cb");
315 lua_rawgeti(state
, -1, proc
->r
);
317 luaL_unref(state
, -2, proc
->r
);
319 lua_remove(state
, -2);
320 lua_pushinteger(state
, ret
>> 8);
321 lua_call(state
, 1, 0);
324 static int ul_process(lua_State
*L
)
326 struct lua_uloop_process
*proc
;
330 if (!lua_isfunction(L
, -1) || !lua_istable(L
, -2) ||
331 !lua_istable(L
, -3) || !lua_isstring(L
, -4)) {
332 lua_pushstring(L
, "invalid arg list");
341 lua_pushstring(L
, "failed to fork");
349 int argn
= lua_objlen(L
, -3);
350 int envn
= lua_objlen(L
, -2);
351 char** argp
= malloc(sizeof(char*) * (argn
+ 2));
352 char** envp
= malloc(sizeof(char*) * (envn
+ 1));
358 argp
[0] = (char*) lua_tostring(L
, -4);
359 for (i
= 1; i
<= argn
; i
++) {
360 lua_rawgeti(L
, -3, i
);
361 argp
[i
] = (char*) lua_tostring(L
, -1);
366 for (i
= 1; i
<= envn
; i
++) {
367 lua_rawgeti(L
, -2, i
);
368 envp
[i
- 1] = (char*) lua_tostring(L
, -1);
373 execve(*argp
, argp
, envp
);
377 lua_getglobal(L
, "__uloop_cb");
378 lua_pushvalue(L
, -2);
379 ref
= luaL_ref(L
, -2);
381 proc
= ul_create_userdata(L
, sizeof(*proc
), process_m
, ul_process_free
);
384 proc
->p
.cb
= ul_process_cb
;
385 uloop_process_add(&proc
->p
);
390 static void ul_interval_cb(struct uloop_interval
*i
)
392 struct lua_uloop_interval
*intv
= container_of(i
, struct lua_uloop_interval
, i
);
394 lua_getglobal(state
, "__uloop_cb");
395 lua_rawgeti(state
, -1, intv
->r
);
396 lua_remove(state
, -2);
398 lua_call(state
, 0, 0);
401 static int ul_interval_set(lua_State
*L
)
403 struct lua_uloop_interval
*intv
;
406 if (!lua_isnumber(L
, -1)) {
407 lua_pushstring(L
, "invalid arg list");
413 set
= lua_tointeger(L
, -1);
414 intv
= lua_touserdata(L
, 1);
415 uloop_interval_set(&intv
->i
, set
);
420 static int ul_interval_expirations(lua_State
*L
)
422 struct lua_uloop_interval
*intv
;
424 intv
= lua_touserdata(L
, 1);
425 lua_pushinteger(L
, intv
->i
.expirations
);
429 static int ul_interval_remaining(lua_State
*L
)
431 struct lua_uloop_interval
*intv
;
433 intv
= lua_touserdata(L
, 1);
434 lua_pushnumber(L
, uloop_interval_remaining(&intv
->i
));
438 static int ul_interval_free(lua_State
*L
)
440 struct lua_uloop_interval
*intv
= lua_touserdata(L
, 1);
442 uloop_interval_cancel(&intv
->i
);
444 /* obj.__index.__gc = nil , make sure executing only once*/
445 lua_getfield(L
, -1, "__index");
446 lua_pushstring(L
, "__gc");
450 lua_getglobal(state
, "__uloop_cb");
451 luaL_unref(state
, -1, intv
->r
);
456 static const luaL_Reg interval_m
[] = {
457 { "set", ul_interval_set
},
458 { "expirations", ul_interval_expirations
},
459 { "remaining", ul_interval_remaining
},
460 { "cancel", ul_interval_free
},
464 static int ul_interval(lua_State
*L
)
466 struct lua_uloop_interval
*intv
;
467 unsigned int set
= 0;
470 if (lua_isnumber(L
, -1)) {
471 set
= lua_tointeger(L
, -1);
475 if (!lua_isfunction(L
, -1)) {
476 lua_pushstring(L
, "invalid arg list");
482 lua_getglobal(L
, "__uloop_cb");
483 lua_pushvalue(L
, -2);
484 ref
= luaL_ref(L
, -2);
486 intv
= ul_create_userdata(L
, sizeof(*intv
), interval_m
, ul_interval_free
);
488 intv
->i
.cb
= ul_interval_cb
;
491 uloop_interval_set(&intv
->i
, set
);
496 static int ul_init(lua_State
*L
)
499 lua_pushboolean(L
, 1);
504 static int ul_run(lua_State
*L
)
507 lua_pushboolean(L
, 1);
512 static int ul_end(lua_State
*L
)
518 static luaL_reg uloop_func
[] = {
522 {"process", ul_process
},
523 {"fd_add", ul_ufd_add
},
524 {"interval", ul_interval
},
529 /* avoid warnings about missing declarations */
530 int luaopen_uloop(lua_State
*L
);
531 int luaclose_uloop(lua_State
*L
);
533 int luaopen_uloop(lua_State
*L
)
537 lua_createtable(L
, 1, 0);
538 lua_setglobal(L
, "__uloop_cb");
540 lua_createtable(L
, 1, 0);
541 lua_setglobal(L
, "__uloop_fds");
543 luaL_openlib(L
, "uloop", uloop_func
, 0);
544 lua_pushstring(L
, "_VERSION");
545 lua_pushstring(L
, "1.0");
548 lua_pushstring(L
, "ULOOP_READ");
549 lua_pushinteger(L
, ULOOP_READ
);
552 lua_pushstring(L
, "ULOOP_WRITE");
553 lua_pushinteger(L
, ULOOP_WRITE
);
556 lua_pushstring(L
, "ULOOP_EDGE_TRIGGER");
557 lua_pushinteger(L
, ULOOP_EDGE_TRIGGER
);
560 lua_pushstring(L
, "ULOOP_BLOCKING");
561 lua_pushinteger(L
, ULOOP_BLOCKING
);
567 int luaclose_uloop(lua_State
*L
)
569 lua_pushstring(L
, "Called");