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