blob: dd037845fc446af31c83136563dbabb5679f75ed [file] [log] [blame]
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001#include <lauxlib.h>
2#include <lua.h>
3#include <lualib.h>
4
Thierry FOURNIER380d0932015-01-23 14:27:52 +01005#include <ebpttree.h>
6
7#include <common/cfgparse.h>
8
9#include <types/hlua.h>
10#include <types/proxy.h>
11
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010012#include <proto/task.h>
13
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010014/* Lua uses longjmp to perform yield or throwing errors. This
15 * macro is used only for identifying the function that can
16 * not return because a longjmp is executed.
17 * __LJMP marks a prototype of hlua file that can use longjmp.
18 * WILL_LJMP() marks an lua function that will use longjmp.
19 * MAY_LJMP() marks an lua function that may use longjmp.
20 */
21#define __LJMP
22#define WILL_LJMP(func) func
23#define MAY_LJMP(func) func
24
Thierry FOURNIER380d0932015-01-23 14:27:52 +010025/* The main Lua execution context. */
26struct hlua gL;
27
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010028/* This is the memory pool containing all the signal structs. These
29 * struct are used to store each requiered signal between two tasks.
30 */
31struct pool_head *pool2_hlua_com;
32
Thierry FOURNIER380d0932015-01-23 14:27:52 +010033/* Store the fast lua context for coroutines. This tree uses the
34 * Lua stack pointer value as indexed entry, and store the associated
35 * hlua context.
36 */
37struct eb_root hlua_ctx = EB_ROOT_UNIQUE;
38
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +010039/* Used to check an Lua function type in the stack. It creates and
40 * returns a reference of the function. This function throws an
41 * error if the rgument is not a "function".
42 */
43__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
44{
45 if (!lua_isfunction(L, argno)) {
46 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1));
47 WILL_LJMP(luaL_argerror(L, argno, msg));
48 }
49 lua_pushvalue(L, argno);
50 return luaL_ref(L, LUA_REGISTRYINDEX);
51}
52
53/* The three following functions are useful for adding entries
54 * in a table. These functions takes a string and respectively an
55 * integer, a string or a function and add it to the table in the
56 * top of the stack.
57 *
58 * These functions throws an error if no more stack size is
59 * available.
60 */
61__LJMP static inline void hlua_class_const_int(lua_State *L, const char *name,
62 unsigned int value)
63{
64 if (!lua_checkstack(L, 2))
65 WILL_LJMP(luaL_error(L, "full stack"));
66 lua_pushstring(L, name);
67 lua_pushunsigned(L, value);
68 lua_settable(L, -3);
69}
70__LJMP static inline void hlua_class_const_str(lua_State *L, const char *name,
71 const char *value)
72{
73 if (!lua_checkstack(L, 2))
74 WILL_LJMP(luaL_error(L, "full stack"));
75 lua_pushstring(L, name);
76 lua_pushstring(L, value);
77 lua_settable(L, -3);
78}
79__LJMP static inline void hlua_class_function(lua_State *L, const char *name,
80 int (*function)(lua_State *L))
81{
82 if (!lua_checkstack(L, 2))
83 WILL_LJMP(luaL_error(L, "full stack"));
84 lua_pushstring(L, name);
85 lua_pushcclosure(L, function, 0);
86 lua_settable(L, -3);
87}
88
89/* This function check the number of arguments available in the
90 * stack. If the number of arguments available is not the same
91 * then <nb> an error is throwed.
92 */
93__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
94{
95 if (lua_gettop(L) == nb)
96 return;
97 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
98}
99
100/* Return true if the data in stack[<ud>] is an object of
101 * type <class_ref>.
102 */
103static int hlua_udataistype(lua_State *L, int ud, int class_ref)
104{
105 void *p = lua_touserdata(L, ud);
106 if (!p)
107 return 0;
108
109 if (!lua_getmetatable(L, ud))
110 return 0;
111
112 lua_rawgeti(L, LUA_REGISTRYINDEX, class_ref);
113 if (!lua_rawequal(L, -1, -2)) {
114 lua_pop(L, 2);
115 return 0;
116 }
117
118 lua_pop(L, 2);
119 return 1;
120}
121
122/* Return an object of the expected type, or throws an error. */
123__LJMP static void *hlua_checkudata(lua_State *L, int ud, int class_ref)
124{
125 if (!hlua_udataistype(L, ud, class_ref))
126 WILL_LJMP(luaL_argerror(L, 1, NULL));
127 return lua_touserdata(L, ud);
128}
129
130/* This fucntion push an error string prefixed by the file name
131 * and the line number where the error is encountered.
132 */
133static int hlua_pusherror(lua_State *L, const char *fmt, ...)
134{
135 va_list argp;
136 va_start(argp, fmt);
137 luaL_where(L, 1);
138 lua_pushvfstring(L, fmt, argp);
139 va_end(argp);
140 lua_concat(L, 2);
141 return 1;
142}
143
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100144/* This function register a new signal. "lua" is the current lua
145 * execution context. It contains a pointer to the associated task.
146 * "link" is a list head attached to an other task that must be wake
147 * the lua task if an event occurs. This is useful with external
148 * events like TCP I/O or sleep functions. This funcion allocate
149 * memory for the signal.
150 */
151static int hlua_com_new(struct hlua *lua, struct list *link)
152{
153 struct hlua_com *com = pool_alloc2(pool2_hlua_com);
154 if (!com)
155 return 0;
156 LIST_ADDQ(&lua->com, &com->purge_me);
157 LIST_ADDQ(link, &com->wake_me);
158 com->task = lua->task;
159 return 1;
160}
161
162/* This function purge all the pending signals when the LUA execution
163 * is finished. This prevent than a coprocess try to wake a deleted
164 * task. This function remove the memory associated to the signal.
165 */
166static void hlua_com_purge(struct hlua *lua)
167{
168 struct hlua_com *com, *back;
169
170 /* Delete all pending communication signals. */
171 list_for_each_entry_safe(com, back, &lua->com, purge_me) {
172 LIST_DEL(&com->purge_me);
173 LIST_DEL(&com->wake_me);
174 pool_free2(pool2_hlua_com, com);
175 }
176}
177
178/* This function sends signals. It wakes all the tasks attached
179 * to a list head, and remove the signal, and free the used
180 * memory.
181 */
182static void hlua_com_wake(struct list *wake)
183{
184 struct hlua_com *com, *back;
185
186 /* Wake task and delete all pending communication signals. */
187 list_for_each_entry_safe(com, back, wake, wake_me) {
188 LIST_DEL(&com->purge_me);
189 LIST_DEL(&com->wake_me);
190 task_wakeup(com->task, TASK_WOKEN_MSG);
191 pool_free2(pool2_hlua_com, com);
192 }
193}
194
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100195/*
196 * The following functions are used to make correspondance between the the
197 * executed lua pointer and the "struct hlua *" that contain the context.
198 * They run with the tree head "hlua_ctx", they just perform lookup in the
199 * tree.
200 *
201 * - hlua_gethlua : return the hlua context associated with an lua_State.
202 * - hlua_delhlua : remove the association between hlua context and lua_state.
203 * - hlua_sethlua : create the association between hlua context and lua_state.
204 */
205static inline struct hlua *hlua_gethlua(lua_State *L)
206{
207 struct ebpt_node *node;
208
209 node = ebpt_lookup(&hlua_ctx, L);
210 if (!node)
211 return NULL;
212 return ebpt_entry(node, struct hlua, node);
213}
214static inline void hlua_delhlua(struct hlua *hlua)
215{
216 if (hlua->node.key)
217 ebpt_delete(&hlua->node);
218}
219static inline void hlua_sethlua(struct hlua *hlua)
220{
221 hlua->node.key = hlua->T;
222 ebpt_insert(&hlua_ctx, &hlua->node);
223}
224
225/* This function initialises the Lua environment stored in the session.
226 * It must be called at the start of the session. This function creates
227 * an LUA coroutine. It can not be use to crete the main LUA context.
228 */
229int hlua_ctx_init(struct hlua *lua, struct task *task)
230{
231 lua->Mref = LUA_REFNIL;
232 lua->state = HLUA_STOP;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100233 LIST_INIT(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100234 lua->T = lua_newthread(gL.T);
235 if (!lua->T) {
236 lua->Tref = LUA_REFNIL;
237 return 0;
238 }
239 hlua_sethlua(lua);
240 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
241 lua->task = task;
242 return 1;
243}
244
245/* Used to destroy the Lua coroutine when the attached session or task
246 * is destroyed. The destroy also the memory context. The struct "lua"
247 * is not freed.
248 */
249void hlua_ctx_destroy(struct hlua *lua)
250{
251 /* Remove context. */
252 hlua_delhlua(lua);
253
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100254 /* Purge all the pending signals. */
255 hlua_com_purge(lua);
256
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100257 /* The thread is garbage collected by Lua. */
258 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
259 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
260}
261
262/* This function is used to restore the Lua context when a coroutine
263 * fails. This function copy the common memory between old coroutine
264 * and the new coroutine. The old coroutine is destroyed, and its
265 * replaced by the new coroutine.
266 * If the flag "keep_msg" is set, the last entry of the old is assumed
267 * as string error message and it is copied in the new stack.
268 */
269static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
270{
271 lua_State *T;
272 int new_ref;
273
274 /* Renew the main LUA stack doesn't have sense. */
275 if (lua == &gL)
276 return 0;
277
278 /* Remove context. */
279 hlua_delhlua(lua);
280
281 /* New Lua coroutine. */
282 T = lua_newthread(gL.T);
283 if (!T)
284 return 0;
285
286 /* Copy last error message. */
287 if (keep_msg)
288 lua_xmove(lua->T, T, 1);
289
290 /* Copy data between the coroutines. */
291 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
292 lua_xmove(lua->T, T, 1);
293 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */
294
295 /* Destroy old data. */
296 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
297
298 /* The thread is garbage collected by Lua. */
299 luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
300
301 /* Fill the struct with the new coroutine values. */
302 lua->Mref = new_ref;
303 lua->T = T;
304 lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX);
305
306 /* Set context. */
307 hlua_sethlua(lua);
308
309 return 1;
310}
311
312/* This function start or resumes the Lua stack execution. If the flag
313 * "yield_allowed" if no set and the LUA stack execution returns a yield
314 * The function return an error.
315 *
316 * The function can returns 4 values:
317 * - HLUA_E_OK : The execution is terminated without any errors.
318 * - HLUA_E_AGAIN : The execution must continue at the next associated
319 * task wakeup.
320 * - HLUA_E_ERRMSG : An error has occured, an error message is set in
321 * the top of the stack.
322 * - HLUA_E_ERR : An error has occured without error message.
323 *
324 * If an error occured, the stack is renewed and it is ready to run new
325 * LUA code.
326 */
327static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
328{
329 int ret;
330 const char *msg;
331
332 lua->state = HLUA_RUN;
333
334 /* Call the function. */
335 ret = lua_resume(lua->T, gL.T, lua->nargs);
336 switch (ret) {
337
338 case LUA_OK:
339 ret = HLUA_E_OK;
340 break;
341
342 case LUA_YIELD:
343 if (!yield_allowed) {
344 lua_settop(lua->T, 0); /* Empty the stack. */
345 if (!lua_checkstack(lua->T, 1)) {
346 ret = HLUA_E_ERR;
347 break;
348 }
349 lua_pushfstring(lua->T, "yield not allowed");
350 ret = HLUA_E_ERRMSG;
351 break;
352 }
353 ret = HLUA_E_AGAIN;
354 break;
355
356 case LUA_ERRRUN:
357 if (!lua_checkstack(lua->T, 1)) {
358 ret = HLUA_E_ERR;
359 break;
360 }
361 msg = lua_tostring(lua->T, -1);
362 lua_settop(lua->T, 0); /* Empty the stack. */
363 lua_pop(lua->T, 1);
364 if (msg)
365 lua_pushfstring(lua->T, "runtime error: %s", msg);
366 else
367 lua_pushfstring(lua->T, "unknown runtime error");
368 ret = HLUA_E_ERRMSG;
369 break;
370
371 case LUA_ERRMEM:
372 lua_settop(lua->T, 0); /* Empty the stack. */
373 if (!lua_checkstack(lua->T, 1)) {
374 ret = HLUA_E_ERR;
375 break;
376 }
377 lua_pushfstring(lua->T, "out of memory error");
378 ret = HLUA_E_ERRMSG;
379 break;
380
381 case LUA_ERRERR:
382 if (!lua_checkstack(lua->T, 1)) {
383 ret = HLUA_E_ERR;
384 break;
385 }
386 msg = lua_tostring(lua->T, -1);
387 lua_settop(lua->T, 0); /* Empty the stack. */
388 lua_pop(lua->T, 1);
389 if (msg)
390 lua_pushfstring(lua->T, "message handler error: %s", msg);
391 else
392 lua_pushfstring(lua->T, "message handler error");
393 ret = HLUA_E_ERRMSG;
394 break;
395
396 default:
397 lua_settop(lua->T, 0); /* Empty the stack. */
398 if (!lua_checkstack(lua->T, 1)) {
399 ret = HLUA_E_ERR;
400 break;
401 }
402 lua_pushfstring(lua->T, "unknonwn error");
403 ret = HLUA_E_ERRMSG;
404 break;
405 }
406
407 switch (ret) {
408 case HLUA_E_AGAIN:
409 break;
410
411 case HLUA_E_ERRMSG:
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100412 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100413 hlua_ctx_renew(lua, 1);
414 lua->state = HLUA_STOP;
415 break;
416
417 case HLUA_E_ERR:
418 lua->state = HLUA_STOP;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100419 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100420 hlua_ctx_renew(lua, 0);
421 break;
422
423 case HLUA_E_OK:
424 lua->state = HLUA_STOP;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100425 hlua_com_purge(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100426 break;
427 }
428
429 return ret;
430}
431
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +0100432/* This function is called by the main configuration key "lua-load". It loads and
433 * execute an lua file during the parsing of the HAProxy configuration file. It is
434 * the main lua entry point.
435 *
436 * This funtion runs with the HAProxy keywords API. It returns -1 if an error is
437 * occured, otherwise it returns 0.
438 *
439 * In some error case, LUA set an error message in top of the stack. This function
440 * returns this error message in the HAProxy logs and pop it from the stack.
441 */
442static int hlua_load(char **args, int section_type, struct proxy *curpx,
443 struct proxy *defpx, const char *file, int line,
444 char **err)
445{
446 int error;
447
448 /* Just load and compile the file. */
449 error = luaL_loadfile(gL.T, args[1]);
450 if (error) {
451 memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1));
452 lua_pop(gL.T, 1);
453 return -1;
454 }
455
456 /* If no syntax error where detected, execute the code. */
457 error = lua_pcall(gL.T, 0, LUA_MULTRET, 0);
458 switch (error) {
459 case LUA_OK:
460 break;
461 case LUA_ERRRUN:
462 memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1));
463 lua_pop(gL.T, 1);
464 return -1;
465 case LUA_ERRMEM:
466 memprintf(err, "lua out of memory error\n");
467 return -1;
468 case LUA_ERRERR:
469 memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1));
470 lua_pop(gL.T, 1);
471 return -1;
472 case LUA_ERRGCMM:
473 memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1));
474 lua_pop(gL.T, 1);
475 return -1;
476 default:
477 memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1));
478 lua_pop(gL.T, 1);
479 return -1;
480 }
481
482 return 0;
483}
484
485/* configuration keywords declaration */
486static struct cfg_kw_list cfg_kws = {{ },{
487 { CFG_GLOBAL, "lua-load", hlua_load },
488 { 0, NULL, NULL },
489}};
490
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +0100491void hlua_init(void)
492{
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100493 /* Initialise com signals pool session. */
494 pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED);
495
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +0100496 /* Register configuration keywords. */
497 cfg_register_keywords(&cfg_kws);
498
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100499 /* Init main lua stack. */
500 gL.Mref = LUA_REFNIL;
501 gL.state = HLUA_STOP;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +0100502 LIST_INIT(&gL.com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100503 gL.T = luaL_newstate();
504 hlua_sethlua(&gL);
505 gL.Tref = LUA_REFNIL;
506 gL.task = NULL;
507
508 /* Initialise lua. */
509 luaL_openlibs(gL.T);
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +0100510}