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