0f0c878d4def7a8232f970ad51c5f7154f659377
[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 <libubox/uloop.h>
26 #include <libubox/list.h>
27
28 struct lua_uloop_timeout {
29 struct uloop_timeout t;
30 int r;
31 };
32
33 struct lua_uloop_process {
34 struct uloop_process p;
35 int r;
36 };
37
38 static lua_State *state;
39
40 static void ul_timer_cb(struct uloop_timeout *t)
41 {
42 struct lua_uloop_timeout *tout = container_of(t, struct lua_uloop_timeout, t);
43
44 lua_getglobal(state, "__uloop_cb");
45 lua_rawgeti(state, -1, tout->r);
46 lua_call(state, 0, 0);
47 }
48
49 static int ul_timer_set(lua_State *L)
50 {
51 struct lua_uloop_timeout *tout;
52 double set;
53
54 if (!lua_isnumber(L, -1)) {
55 lua_pushstring(L, "invalid arg list");
56 lua_error(L);
57
58 return 0;
59 }
60
61 set = lua_tointeger(L, -1);
62 tout = lua_touserdata(L, 1);
63 uloop_timeout_set(&tout->t, set);
64
65 return 1;
66 }
67
68 static int ul_timer_free(lua_State *L)
69 {
70 struct lua_uloop_timeout *tout = lua_touserdata(L, 1);
71
72 uloop_timeout_cancel(&tout->t);
73 lua_getglobal(state, "__uloop_cb");
74 luaL_unref(L, -1, tout->r);
75
76 return 1;
77 }
78
79 static const luaL_Reg timer_m[] = {
80 { "set", ul_timer_set },
81 { "cancel", ul_timer_free },
82 { NULL, NULL }
83 };
84
85 static int ul_timer(lua_State *L)
86 {
87 struct lua_uloop_timeout *tout;
88 int set = 0;
89 int ref;
90
91 if (lua_isnumber(L, -1)) {
92 set = lua_tointeger(L, -1);
93 lua_pop(L, 1);
94 }
95
96 if (!lua_isfunction(L, -1)) {
97 lua_pushstring(L, "invalid arg list");
98 lua_error(L);
99
100 return 0;
101 }
102
103 lua_getglobal(L, "__uloop_cb");
104 lua_pushvalue(L, -2);
105 ref = luaL_ref(L, -2);
106
107 tout = lua_newuserdata(L, sizeof(*tout));
108 lua_createtable(L, 0, 2);
109 lua_pushvalue(L, -1);
110 lua_setfield(L, -2, "__index");
111 lua_pushcfunction(L, ul_timer_free);
112 lua_setfield(L, -2, "__gc");
113 lua_pushvalue(L, -1);
114 lua_setmetatable(L, -3);
115 lua_pushvalue(L, -2);
116 luaI_openlib(L, NULL, timer_m, 1);
117 lua_pushvalue(L, -2);
118
119 memset(tout, 0, sizeof(*tout));
120
121 tout->r = ref;
122 tout->t.cb = ul_timer_cb;
123 if (set)
124 uloop_timeout_set(&tout->t, set);
125
126 return 1;
127 }
128
129 static void proc_cb(struct uloop_process *p, int ret)
130 {
131 struct lua_uloop_process *proc = container_of(p, struct lua_uloop_process, p);
132
133 lua_getglobal(state, "__uloop_cb");
134 lua_rawgeti(state, -1, proc->r);
135 luaL_unref(state, -2, proc->r);
136 lua_pushinteger(state, ret >> 8);
137 lua_call(state, 1, 0);
138 }
139
140 static int ul_process(lua_State *L)
141 {
142 struct lua_uloop_process *proc;
143 pid_t pid;
144 int ref;
145
146 if (!lua_isfunction(L, -1) || !lua_istable(L, -2) ||
147 !lua_istable(L, -3) || !lua_isstring(L, -4)) {
148 lua_pushstring(L, "invalid arg list");
149 lua_error(L);
150
151 return 0;
152 }
153
154 pid = fork();
155
156 if (pid == -1) {
157 lua_pushstring(L, "failed to fork");
158 lua_error(L);
159
160 return 0;
161 }
162
163 if (pid == 0) {
164 /* child */
165 int argn = lua_objlen(L, -3);
166 int envn = lua_objlen(L, -2);
167 char** argp = malloc(sizeof(char*) * (argn + 2));
168 char** envp = malloc(sizeof(char*) * envn + 1);
169 int i = 1;
170
171 argp[0] = (char*) lua_tostring(L, -4);
172 for (i = 1; i <= argn; i++) {
173 lua_rawgeti(L, -3, i);
174 argp[i] = (char*) lua_tostring(L, -1);
175 lua_pop(L, 1);
176 }
177 argp[i] = NULL;
178
179 for (i = 1; i <= envn; i++) {
180 lua_rawgeti(L, -2, i);
181 envp[i - 1] = (char*) lua_tostring(L, -1);
182 lua_pop(L, 1);
183 }
184 envp[i - 1] = NULL;
185
186 execve(*argp, argp, envp);
187 exit(-1);
188 }
189
190 lua_getglobal(L, "__uloop_cb");
191 lua_pushvalue(L, -2);
192 ref = luaL_ref(L, -2);
193
194 proc = lua_newuserdata(L, sizeof(*proc));
195 memset(proc, 0, sizeof(*proc));
196
197 proc->r = ref;
198 proc->p.pid = pid;
199 proc->p.cb = proc_cb;
200 uloop_process_add(&proc->p);
201
202 return 1;
203 }
204
205 static int ul_init(lua_State *L)
206 {
207 uloop_init();
208 lua_pushboolean(L, 1);
209
210 return 1;
211 }
212
213 static int ul_run(lua_State *L)
214 {
215 uloop_run();
216 lua_pushboolean(L, 1);
217
218 return 1;
219 }
220
221 static luaL_reg uloop_func[] = {
222 {"init", ul_init},
223 {"run", ul_run},
224 {"timer", ul_timer},
225 {"process", ul_process},
226 {NULL, NULL},
227 };
228
229 int luaopen_uloop(lua_State *L)
230 {
231 state = L;
232
233 lua_createtable(L, 1, 0);
234 lua_setglobal(L, "__uloop_cb");
235
236 luaL_openlib(L, "uloop", uloop_func, 0);
237 lua_pushstring(L, "_VERSION");
238 lua_pushstring(L, "1.0");
239 lua_rawset(L, -3);
240
241 return 1;
242 }
243
244 int luaclose_uloop(lua_State *L)
245 {
246 lua_pushstring(L, "Called");
247
248 return 1;
249 }