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