utils: fix build error with g++
[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 static lua_State *state;
45
46 static void *
47 ul_create_userdata(lua_State *L, size_t size, const luaL_Reg *reg, lua_CFunction gc)
48 {
49         void *ret = lua_newuserdata(L, size);
50
51         memset(ret, 0, size);
52         lua_createtable(L, 0, 2);
53         lua_pushvalue(L, -1);
54         lua_setfield(L, -2, "__index");
55         lua_pushcfunction(L, gc);
56         lua_setfield(L, -2, "__gc");
57         lua_pushvalue(L, -1);
58         lua_setmetatable(L, -3);
59         lua_pushvalue(L, -2);
60         luaI_openlib(L, NULL, reg, 1);
61         lua_pushvalue(L, -2);
62
63         return ret;
64 }
65
66 static void ul_timer_cb(struct uloop_timeout *t)
67 {
68         struct lua_uloop_timeout *tout = container_of(t, struct lua_uloop_timeout, t);
69
70         lua_getglobal(state, "__uloop_cb");
71         lua_rawgeti(state, -1, tout->r);
72         lua_remove(state, -2);
73
74         lua_call(state, 0, 0);
75
76 }
77
78 static int ul_timer_set(lua_State *L)
79 {
80         struct lua_uloop_timeout *tout;
81         double set;
82
83         if (!lua_isnumber(L, -1)) {
84                 lua_pushstring(L, "invalid arg list");
85                 lua_error(L);
86
87                 return 0;
88         }
89
90         set = lua_tointeger(L, -1);
91         tout = lua_touserdata(L, 1);
92         uloop_timeout_set(&tout->t, set);
93
94         return 1;
95 }
96
97 static int ul_timer_remaining(lua_State *L)
98 {
99         struct lua_uloop_timeout *tout;
100
101         tout = lua_touserdata(L, 1);
102         lua_pushnumber(L, uloop_timeout_remaining(&tout->t));
103         return 1;
104 }
105
106 static int ul_timer_free(lua_State *L)
107 {
108         struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
109
110         uloop_timeout_cancel(&tout->t);
111
112         /* obj.__index.__gc = nil , make sure executing only once*/
113         lua_getfield(L, -1, "__index");
114         lua_pushstring(L, "__gc");
115         lua_pushnil(L);
116         lua_settable(L, -3);
117
118         lua_getglobal(state, "__uloop_cb");
119         luaL_unref(state, -1, tout->r);
120
121         return 1;
122 }
123
124 static const luaL_Reg timer_m[] = {
125         { "set", ul_timer_set },
126         { "remaining", ul_timer_remaining },
127         { "cancel", ul_timer_free },
128         { NULL, NULL }
129 };
130
131 static int ul_timer(lua_State *L)
132 {
133         struct lua_uloop_timeout *tout;
134         int set = 0;
135         int ref;
136
137         if (lua_isnumber(L, -1)) {
138                 set = lua_tointeger(L, -1);
139                 lua_pop(L, 1);
140         }
141
142         if (!lua_isfunction(L, -1)) {
143                 lua_pushstring(L, "invalid arg list");
144                 lua_error(L);
145
146                 return 0;
147         }
148
149         lua_getglobal(L, "__uloop_cb");
150         lua_pushvalue(L, -2);
151         ref = luaL_ref(L, -2);
152
153         tout = ul_create_userdata(L, sizeof(*tout), timer_m, ul_timer_free);
154         tout->r = ref;
155         tout->t.cb = ul_timer_cb;
156
157         if (set)
158                 uloop_timeout_set(&tout->t, set);
159
160         return 1;
161 }
162
163 static void ul_ufd_cb(struct uloop_fd *fd, unsigned int events)
164 {
165         struct lua_uloop_fd *ufd = container_of(fd, struct lua_uloop_fd, fd);
166
167         lua_getglobal(state, "__uloop_cb");
168         lua_rawgeti(state, -1, ufd->r);
169         lua_remove(state, -2);
170
171         /* push fd object */
172         lua_getglobal(state, "__uloop_fds");
173         lua_rawgeti(state, -1, ufd->fd_r);
174         lua_remove(state, -2);
175
176         /* push events */
177         lua_pushinteger(state, events);
178         lua_call(state, 2, 0);
179 }
180
181
182 static int get_sock_fd(lua_State* L, int idx) {
183         int fd;
184         if(lua_isnumber(L, idx)) {
185                 fd = lua_tonumber(L, idx);
186         } else {
187                 luaL_checktype(L, idx, LUA_TUSERDATA);
188                 lua_getfield(L, idx, "getfd");
189                 if(lua_isnil(L, -1))
190                         return luaL_error(L, "socket type missing 'getfd' method");
191                 lua_pushvalue(L, idx - 1);
192                 lua_call(L, 1, 1);
193                 fd = lua_tointeger(L, -1);
194                 lua_pop(L, 1);
195         }
196         return fd;
197 }
198
199 static int ul_ufd_delete(lua_State *L)
200 {
201         struct lua_uloop_fd *ufd = lua_touserdata(L, 1);
202
203         uloop_fd_delete(&ufd->fd);
204
205         /* obj.__index.__gc = nil , make sure executing only once*/
206         lua_getfield(L, -1, "__index");
207         lua_pushstring(L, "__gc");
208         lua_pushnil(L);
209         lua_settable(L, -3);
210
211         lua_getglobal(state, "__uloop_cb");
212         luaL_unref(state, -1, ufd->r);
213         lua_remove(state, -1);
214
215         lua_getglobal(state, "__uloop_fds");
216         luaL_unref(state, -1, ufd->fd_r);
217         lua_remove(state, -1);
218
219         return 1;
220 }
221
222 static const luaL_Reg ufd_m[] = {
223         { "delete", ul_ufd_delete },
224         { NULL, NULL }
225 };
226
227 static int ul_ufd_add(lua_State *L)
228 {
229         struct lua_uloop_fd *ufd;
230         int fd = 0;
231         unsigned int flags = 0;
232         int ref;
233         int fd_ref;
234
235         if (lua_isnumber(L, -1)) {
236                 flags = lua_tointeger(L, -1);
237                 lua_pop(L, 1);
238         }
239
240         if (!lua_isfunction(L, -1)) {
241                 lua_pushstring(L, "invalid arg list");
242                 lua_error(L);
243
244                 return 0;
245         }
246
247         fd = get_sock_fd(L, -2);
248
249         lua_getglobal(L, "__uloop_cb");
250         lua_pushvalue(L, -2);
251         ref = luaL_ref(L, -2);
252         lua_pop(L, 1);
253
254         lua_getglobal(L, "__uloop_fds");
255         lua_pushvalue(L, -3);
256         fd_ref = luaL_ref(L, -2);
257         lua_pop(L, 1);
258
259         ufd = ul_create_userdata(L, sizeof(*ufd), ufd_m, ul_ufd_delete);
260         ufd->r = ref;
261         ufd->fd.fd = fd;
262         ufd->fd_r = fd_ref;
263         ufd->fd.cb = ul_ufd_cb;
264         if (flags)
265                 uloop_fd_add(&ufd->fd, flags);
266
267         return 1;
268 }
269
270 static int ul_process_free(lua_State *L)
271 {
272         struct lua_uloop_process *proc = lua_touserdata(L, 1);
273
274         /* obj.__index.__gc = nil , make sure executing only once*/
275         lua_getfield(L, -1, "__index");
276         lua_pushstring(L, "__gc");
277         lua_pushnil(L);
278         lua_settable(L, -3);
279
280         if (proc->r != LUA_NOREF) {
281                 uloop_process_delete(&proc->p);
282
283                 lua_getglobal(state, "__uloop_cb");
284                 luaL_unref(state, -1, proc->r);
285                 lua_remove(state, -1);
286         }
287
288         return 1;
289 }
290
291 static int ul_process_pid(lua_State *L)
292 {
293         struct lua_uloop_process *proc = lua_touserdata(L, 1);
294
295         if (proc->p.pid) {
296                 lua_pushnumber(L, proc->p.pid);
297                 return 1;
298         }
299
300         return 0;
301 }
302
303 static const luaL_Reg process_m[] = {
304         { "delete", ul_process_free },
305         { "pid", ul_process_pid },
306         { NULL, NULL }
307 };
308
309 static void ul_process_cb(struct uloop_process *p, int ret)
310 {
311         struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
312
313         lua_getglobal(state, "__uloop_cb");
314         lua_rawgeti(state, -1, proc->r);
315
316         luaL_unref(state, -2, proc->r);
317         proc->r = LUA_NOREF;
318         lua_remove(state, -2);
319         lua_pushinteger(state, ret >> 8);
320         lua_call(state, 1, 0);
321 }
322
323 static int ul_process(lua_State *L)
324 {
325         struct lua_uloop_process *proc;
326         pid_t pid;
327         int ref;
328
329         if (!lua_isfunction(L, -1) || !lua_istable(L, -2) ||
330                         !lua_istable(L, -3) || !lua_isstring(L, -4)) {
331                 lua_pushstring(L, "invalid arg list");
332                 lua_error(L);
333
334                 return 0;
335         }
336
337         pid = fork();
338
339         if (pid == -1) {
340                 lua_pushstring(L, "failed to fork");
341                 lua_error(L);
342
343                 return 0;
344         }
345
346         if (pid == 0) {
347                 /* child */
348                 int argn = lua_objlen(L, -3);
349                 int envn = lua_objlen(L, -2);
350                 char** argp = malloc(sizeof(char*) * (argn + 2));
351                 char** envp = malloc(sizeof(char*) * (envn + 1));
352                 int i = 1;
353
354                 if (!argp || !envp)
355                         _exit(-1);
356
357                 argp[0] = (char*) lua_tostring(L, -4);
358                 for (i = 1; i <= argn; i++) {
359                         lua_rawgeti(L, -3, i);
360                         argp[i] = (char*) lua_tostring(L, -1);
361                         lua_pop(L, 1);
362                 }
363                 argp[i] = NULL;
364
365                 for (i = 1; i <= envn; i++) {
366                         lua_rawgeti(L, -2, i);
367                         envp[i - 1] = (char*) lua_tostring(L, -1);
368                         lua_pop(L, 1);
369                 }
370                 envp[i - 1] = NULL;
371
372                 execve(*argp, argp, envp);
373                 _exit(-1);
374         }
375
376         lua_getglobal(L, "__uloop_cb");
377         lua_pushvalue(L, -2);
378         ref = luaL_ref(L, -2);
379
380         proc = ul_create_userdata(L, sizeof(*proc), process_m, ul_process_free);
381         proc->r = ref;
382         proc->p.pid = pid;
383         proc->p.cb = ul_process_cb;
384         uloop_process_add(&proc->p);
385
386         return 1;
387 }
388
389 static int ul_init(lua_State *L)
390 {
391         uloop_init();
392         lua_pushboolean(L, 1);
393
394         return 1;
395 }
396
397 static int ul_run(lua_State *L)
398 {
399         uloop_run();
400         lua_pushboolean(L, 1);
401
402         return 1;
403 }
404
405 static int ul_end(lua_State *L)
406 {
407         uloop_end();
408         return 1;
409 }
410
411 static luaL_reg uloop_func[] = {
412         {"init", ul_init},
413         {"run", ul_run},
414         {"timer", ul_timer},
415         {"process", ul_process},
416         {"fd_add", ul_ufd_add},
417         {"cancel", ul_end},
418         {NULL, NULL},
419 };
420
421 /* avoid warnings about missing declarations */
422 int luaopen_uloop(lua_State *L);
423 int luaclose_uloop(lua_State *L);
424
425 int luaopen_uloop(lua_State *L)
426 {
427         state = L;
428
429         lua_createtable(L, 1, 0);
430         lua_setglobal(L, "__uloop_cb");
431
432         lua_createtable(L, 1, 0);
433         lua_setglobal(L, "__uloop_fds");
434
435         luaL_openlib(L, "uloop", uloop_func, 0);
436         lua_pushstring(L, "_VERSION");
437         lua_pushstring(L, "1.0");
438         lua_rawset(L, -3);
439
440         lua_pushstring(L, "ULOOP_READ");
441         lua_pushinteger(L, ULOOP_READ);
442         lua_rawset(L, -3);
443
444         lua_pushstring(L, "ULOOP_WRITE");
445         lua_pushinteger(L, ULOOP_WRITE);
446         lua_rawset(L, -3);
447
448         lua_pushstring(L, "ULOOP_EDGE_TRIGGER");
449         lua_pushinteger(L, ULOOP_EDGE_TRIGGER);
450         lua_rawset(L, -3);
451
452         lua_pushstring(L, "ULOOP_BLOCKING");
453         lua_pushinteger(L, ULOOP_BLOCKING);
454         lua_rawset(L, -3);
455
456         return 1;
457 }
458
459 int luaclose_uloop(lua_State *L)
460 {
461         lua_pushstring(L, "Called");
462
463         return 1;
464 }