45c9bc7ad23ac5945801ff6ca3f08ffa39c6f729
[project/libubox.git] / lua / uloop.c
1 /*
2 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
3 *
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.
7 *
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.
15 */
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20
21 #include <lua.h>
22 #include <lualib.h>
23 #include <lauxlib.h>
24
25 #include "../uloop.h"
26 #include "../list.h"
27
28 struct lua_uloop_fd {
29 struct uloop_fd fd;
30 int r;
31 int fd_r;
32 };
33
34 struct lua_uloop_timeout {
35 struct uloop_timeout t;
36 int r;
37 };
38
39 struct lua_uloop_process {
40 struct uloop_process p;
41 int r;
42 };
43
44 struct lua_uloop_interval {
45 struct uloop_interval i;
46 int r;
47 };
48
49 static lua_State *state;
50
51 static void *
52 ul_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg, lua_CFunction gc)
53 {
54 void *ret = lua_newuserdata(L, size);
55
56 memset(ret, 0, size);
57 lua_createtable(L, 0, 2);
58 lua_pushvalue(L, -1);
59 lua_setfield(L, -2, "__index");
60 lua_pushcfunction(L, gc);
61 lua_setfield(L, -2, "__gc");
62 lua_pushvalue(L, -1);
63 lua_setmetatable(L, -3);
64 lua_pushvalue(L, -2);
65 luaI_openlib(L, NULL, reg, 1);
66 lua_pushvalue(L, -2);
67
68 return ret;
69 }
70
71 static void ul_timer_cb(struct uloop_timeout *t)
72 {
73 struct lua_uloop_timeout *tout = container_of(t, struct lua_uloop_timeout, t);
74
75 lua_getglobal(state, "__uloop_cb");
76 lua_rawgeti(state, -1, tout->r);
77 lua_remove(state, -2);
78
79 lua_call(state, 0, 0);
80
81 }
82
83 static int ul_timer_set(lua_State *L)
84 {
85 struct lua_uloop_timeout *tout;
86 double set;
87
88 if (!lua_isnumber(L, -1)) {
89 lua_pushstring(L, "invalid arg list");
90 lua_error(L);
91
92 return 0;
93 }
94
95 set = lua_tointeger(L, -1);
96 tout = lua_touserdata(L, 1);
97 uloop_timeout_set(&tout->t, set);
98
99 return 1;
100 }
101
102 static int ul_timer_remaining(lua_State *L)
103 {
104 struct lua_uloop_timeout *tout;
105
106 tout = lua_touserdata(L, 1);
107 lua_pushnumber(L, uloop_timeout_remaining64(&tout->t));
108 return 1;
109 }
110
111 static int ul_timer_free(lua_State *L)
112 {
113 struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
114
115 uloop_timeout_cancel(&tout->t);
116
117 /* obj.__index.__gc = nil , make sure executing only once*/
118 lua_getfield(L, -1, "__index");
119 lua_pushstring(L, "__gc");
120 lua_pushnil(L);
121 lua_settable(L, -3);
122
123 lua_getglobal(state, "__uloop_cb");
124 luaL_unref(state, -1, tout->r);
125
126 return 1;
127 }
128
129 static const luaL_Reg timer_m[] = {
130 { "set", ul_timer_set },
131 { "remaining", ul_timer_remaining },
132 { "cancel", ul_timer_free },
133 { NULL, NULL }
134 };
135
136 static int ul_timer(lua_State *L)
137 {
138 struct lua_uloop_timeout *tout;
139 int set = 0;
140 int ref;
141
142 if (lua_isnumber(L, -1)) {
143 set = lua_tointeger(L, -1);
144 lua_pop(L, 1);
145 }
146
147 if (!lua_isfunction(L, -1)) {
148 lua_pushstring(L, "invalid arg list");
149 lua_error(L);
150
151 return 0;
152 }
153
154 lua_getglobal(L, "__uloop_cb");
155 lua_pushvalue(L, -2);
156 ref = luaL_ref(L, -2);
157
158 tout = ul_create_userdata(L, sizeof(*tout), timer_m, ul_timer_free);
159 tout->r = ref;
160 tout->t.cb = ul_timer_cb;
161
162 if (set)
163 uloop_timeout_set(&tout->t, set);
164
165 return 1;
166 }
167
168 static void ul_ufd_cb(struct uloop_fd *fd, unsigned int events)
169 {
170 struct lua_uloop_fd *ufd = container_of(fd, struct lua_uloop_fd, fd);
171
172 lua_getglobal(state, "__uloop_cb");
173 lua_rawgeti(state, -1, ufd->r);
174 lua_remove(state, -2);
175
176 /* push fd object */
177 lua_getglobal(state, "__uloop_fds");
178 lua_rawgeti(state, -1, ufd->fd_r);
179 lua_remove(state, -2);
180
181 /* push events */
182 lua_pushinteger(state, events);
183 lua_call(state, 2, 0);
184 }
185
186
187 static int get_sock_fd(lua_State* L, int idx) {
188 int fd;
189 if(lua_isnumber(L, idx)) {
190 fd = lua_tonumber(L, idx);
191 } else {
192 luaL_checktype(L, idx, LUA_TUSERDATA);
193 lua_getfield(L, idx, "getfd");
194 if(lua_isnil(L, -1))
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);
198 lua_call(L, 1, 1);
199 fd = lua_tointeger(L, -1);
200 lua_pop(L, 1);
201 }
202 return fd;
203 }
204
205 static int ul_ufd_delete(lua_State *L)
206 {
207 struct lua_uloop_fd *ufd = lua_touserdata(L, 1);
208
209 uloop_fd_delete(&ufd->fd);
210
211 /* obj.__index.__gc = nil , make sure executing only once*/
212 lua_getfield(L, -1, "__index");
213 lua_pushstring(L, "__gc");
214 lua_pushnil(L);
215 lua_settable(L, -3);
216
217 lua_getglobal(state, "__uloop_cb");
218 luaL_unref(state, -1, ufd->r);
219 lua_remove(state, -1);
220
221 lua_getglobal(state, "__uloop_fds");
222 luaL_unref(state, -1, ufd->fd_r);
223 lua_remove(state, -1);
224
225 return 1;
226 }
227
228 static const luaL_Reg ufd_m[] = {
229 { "delete", ul_ufd_delete },
230 { NULL, NULL }
231 };
232
233 static int ul_ufd_add(lua_State *L)
234 {
235 struct lua_uloop_fd *ufd;
236 int fd = 0;
237 unsigned int flags = 0;
238 int ref;
239 int fd_ref;
240
241 flags = luaL_checkinteger(L, 3);
242 if (!flags) {
243 lua_pushstring(L, "flags cannot be zero");
244 lua_error(L);
245 }
246
247 luaL_checktype(L, 2, LUA_TFUNCTION);
248
249 fd = get_sock_fd(L, 1);
250
251 lua_getglobal(L, "__uloop_cb");
252 lua_pushvalue(L, 2);
253 ref = luaL_ref(L, -2);
254 lua_pop(L, 1);
255
256 lua_getglobal(L, "__uloop_fds");
257 lua_pushvalue(L, 1);
258 fd_ref = luaL_ref(L, -2);
259 lua_pop(L, 1);
260
261 ufd = ul_create_userdata(L, sizeof(*ufd), ufd_m, ul_ufd_delete);
262 ufd->r = ref;
263 ufd->fd.fd = fd;
264 ufd->fd_r = fd_ref;
265 ufd->fd.cb = ul_ufd_cb;
266 uloop_fd_add(&ufd->fd, flags);
267
268 return 1;
269 }
270
271 static int ul_process_free(lua_State *L)
272 {
273 struct lua_uloop_process *proc = lua_touserdata(L, 1);
274
275 /* obj.__index.__gc = nil , make sure executing only once*/
276 lua_getfield(L, -1, "__index");
277 lua_pushstring(L, "__gc");
278 lua_pushnil(L);
279 lua_settable(L, -3);
280
281 if (proc->r != LUA_NOREF) {
282 uloop_process_delete(&proc->p);
283
284 lua_getglobal(state, "__uloop_cb");
285 luaL_unref(state, -1, proc->r);
286 lua_remove(state, -1);
287 }
288
289 return 1;
290 }
291
292 static int ul_process_pid(lua_State *L)
293 {
294 struct lua_uloop_process *proc = lua_touserdata(L, 1);
295
296 if (proc->p.pid) {
297 lua_pushnumber(L, proc->p.pid);
298 return 1;
299 }
300
301 return 0;
302 }
303
304 static const luaL_Reg process_m[] = {
305 { "delete", ul_process_free },
306 { "pid", ul_process_pid },
307 { NULL, NULL }
308 };
309
310 static void ul_process_cb(struct uloop_process *p, int ret)
311 {
312 struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
313
314 lua_getglobal(state, "__uloop_cb");
315 lua_rawgeti(state, -1, proc->r);
316
317 luaL_unref(state, -2, proc->r);
318 proc->r = LUA_NOREF;
319 lua_remove(state, -2);
320 lua_pushinteger(state, ret >> 8);
321 lua_call(state, 1, 0);
322 }
323
324 static int ul_process(lua_State *L)
325 {
326 struct lua_uloop_process *proc;
327 pid_t pid;
328 int ref;
329
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");
333 lua_error(L);
334
335 return 0;
336 }
337
338 pid = fork();
339
340 if (pid == -1) {
341 lua_pushstring(L, "failed to fork");
342 lua_error(L);
343
344 return 0;
345 }
346
347 if (pid == 0) {
348 /* child */
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));
353 int i = 1;
354
355 if (!argp || !envp)
356 _exit(-1);
357
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);
362 lua_pop(L, 1);
363 }
364 argp[i] = NULL;
365
366 for (i = 1; i <= envn; i++) {
367 lua_rawgeti(L, -2, i);
368 envp[i - 1] = (char*) lua_tostring(L, -1);
369 lua_pop(L, 1);
370 }
371 envp[i - 1] = NULL;
372
373 execve(*argp, argp, envp);
374 _exit(-1);
375 }
376
377 lua_getglobal(L, "__uloop_cb");
378 lua_pushvalue(L, -2);
379 ref = luaL_ref(L, -2);
380
381 proc = ul_create_userdata(L, sizeof(*proc), process_m, ul_process_free);
382 proc->r = ref;
383 proc->p.pid = pid;
384 proc->p.cb = ul_process_cb;
385 uloop_process_add(&proc->p);
386
387 return 1;
388 }
389
390 static void ul_interval_cb(struct uloop_interval *i)
391 {
392 struct lua_uloop_interval *intv = container_of(i, struct lua_uloop_interval, i);
393
394 lua_getglobal(state, "__uloop_cb");
395 lua_rawgeti(state, -1, intv->r);
396 lua_remove(state, -2);
397
398 lua_call(state, 0, 0);
399 }
400
401 static int ul_interval_set(lua_State *L)
402 {
403 struct lua_uloop_interval *intv;
404 double set;
405
406 if (!lua_isnumber(L, -1)) {
407 lua_pushstring(L, "invalid arg list");
408 lua_error(L);
409
410 return 0;
411 }
412
413 set = lua_tointeger(L, -1);
414 intv = lua_touserdata(L, 1);
415 uloop_interval_set(&intv->i, set);
416
417 return 1;
418 }
419
420 static int ul_interval_expirations(lua_State *L)
421 {
422 struct lua_uloop_interval *intv;
423
424 intv = lua_touserdata(L, 1);
425 lua_pushinteger(L, intv->i.expirations);
426 return 1;
427 }
428
429 static int ul_interval_remaining(lua_State *L)
430 {
431 struct lua_uloop_interval *intv;
432
433 intv = lua_touserdata(L, 1);
434 lua_pushnumber(L, uloop_interval_remaining(&intv->i));
435 return 1;
436 }
437
438 static int ul_interval_free(lua_State *L)
439 {
440 struct lua_uloop_interval *intv = lua_touserdata(L, 1);
441
442 uloop_interval_cancel(&intv->i);
443
444 /* obj.__index.__gc = nil , make sure executing only once*/
445 lua_getfield(L, -1, "__index");
446 lua_pushstring(L, "__gc");
447 lua_pushnil(L);
448 lua_settable(L, -3);
449
450 lua_getglobal(state, "__uloop_cb");
451 luaL_unref(state, -1, intv->r);
452
453 return 1;
454 }
455
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 },
461 { NULL, NULL }
462 };
463
464 static int ul_interval(lua_State *L)
465 {
466 struct lua_uloop_interval *intv;
467 unsigned int set = 0;
468 int ref;
469
470 if (lua_isnumber(L, -1)) {
471 set = lua_tointeger(L, -1);
472 lua_pop(L, 1);
473 }
474
475 if (!lua_isfunction(L, -1)) {
476 lua_pushstring(L, "invalid arg list");
477 lua_error(L);
478
479 return 0;
480 }
481
482 lua_getglobal(L, "__uloop_cb");
483 lua_pushvalue(L, -2);
484 ref = luaL_ref(L, -2);
485
486 intv = ul_create_userdata(L, sizeof(*intv), interval_m, ul_interval_free);
487 intv->r = ref;
488 intv->i.cb = ul_interval_cb;
489
490 if (set)
491 uloop_interval_set(&intv->i, set);
492
493 return 1;
494 }
495
496 static int ul_init(lua_State *L)
497 {
498 uloop_init();
499 lua_pushboolean(L, 1);
500
501 return 1;
502 }
503
504 static int ul_run(lua_State *L)
505 {
506 uloop_run();
507 lua_pushboolean(L, 1);
508
509 return 1;
510 }
511
512 static int ul_end(lua_State *L)
513 {
514 uloop_end();
515 return 1;
516 }
517
518 static luaL_reg uloop_func[] = {
519 {"init", ul_init},
520 {"run", ul_run},
521 {"timer", ul_timer},
522 {"process", ul_process},
523 {"fd_add", ul_ufd_add},
524 {"interval", ul_interval},
525 {"cancel", ul_end},
526 {NULL, NULL},
527 };
528
529 /* avoid warnings about missing declarations */
530 int luaopen_uloop(lua_State *L);
531 int luaclose_uloop(lua_State *L);
532
533 int luaopen_uloop(lua_State *L)
534 {
535 state = L;
536
537 lua_createtable(L, 1, 0);
538 lua_setglobal(L, "__uloop_cb");
539
540 lua_createtable(L, 1, 0);
541 lua_setglobal(L, "__uloop_fds");
542
543 luaL_openlib(L, "uloop", uloop_func, 0);
544 lua_pushstring(L, "_VERSION");
545 lua_pushstring(L, "1.0");
546 lua_rawset(L, -3);
547
548 lua_pushstring(L, "ULOOP_READ");
549 lua_pushinteger(L, ULOOP_READ);
550 lua_rawset(L, -3);
551
552 lua_pushstring(L, "ULOOP_WRITE");
553 lua_pushinteger(L, ULOOP_WRITE);
554 lua_rawset(L, -3);
555
556 lua_pushstring(L, "ULOOP_EDGE_TRIGGER");
557 lua_pushinteger(L, ULOOP_EDGE_TRIGGER);
558 lua_rawset(L, -3);
559
560 lua_pushstring(L, "ULOOP_BLOCKING");
561 lua_pushinteger(L, ULOOP_BLOCKING);
562 lua_rawset(L, -3);
563
564 return 1;
565 }
566
567 int luaclose_uloop(lua_State *L)
568 {
569 lua_pushstring(L, "Called");
570
571 return 1;
572 }