root/src/modules/lua.c

Revision 0f81a29f005c4f87e08c22052c27101ab47f8444, 19.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

seems to address these. closes #36

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include "noit_conf.h"
9 #include "noit_module.h"
10 #include "noit_check.h"
11 #include "noit_check_tools.h"
12 #include "utils/noit_log.h"
13 #include "lua_noit.h"
14
15 #ifdef HAVE_ALLOCA_H
16 #include <alloca.h>
17 #endif
18 #include <assert.h>
19
20 static noit_log_stream_t nlerr = NULL;
21 static noit_log_stream_t nldeb = NULL;
22 static noit_hash_table noit_coros = NOIT_HASH_EMPTY;
23
24 struct loader_conf {
25   char *script_dir;
26 };
27 static struct loader_conf *__get_loader_conf(noit_module_loader_t *self) {
28   struct loader_conf *c;
29   c = noit_image_get_userdata(&self->hdr);
30   if(!c) {
31     c = calloc(1, sizeof(*c));
32     noit_image_set_userdata(&self->hdr, c);
33   }
34   return c;
35 }
36 static void
37 noit_lua_loader_set_directory(noit_module_loader_t *self, char *dir) {
38   struct loader_conf *c = __get_loader_conf(self);
39   if(c->script_dir) free(c->script_dir);
40   c->script_dir = strdup(dir);
41 }
42 static const char *
43 noit_lua_loader_get_directory(noit_module_loader_t *self) {
44   struct loader_conf *c = __get_loader_conf(self);
45   return c->script_dir;
46 }
47
48 noit_lua_check_info_t *
49 get_ci(lua_State *L) {
50   noit_lua_check_info_t *v = NULL;
51   if(noit_hash_retrieve(&noit_coros, (const char *)&L, sizeof(L), (void **)&v))
52     return v;
53   return NULL;
54 }
55 static void
56 int_cl_free(void *vcl) {
57   free(vcl);
58 }
59
60 static void
61 noit_event_dispose(void *ev) {
62   int mask;
63   eventer_t *value = ev;
64   eventer_t removed, e = *value;
65   noitL(nldeb, "lua check cleanup: dropping (%p)->fd (%d)\n", e, e->fd);
66   if(e->mask & (EVENTER_READ|EVENTER_WRITE|EVENTER_EXCEPTION)) {
67     noitL(nldeb, "    closing down fd %d\n", e->fd);
68     e->opset->close(e->fd, &mask, e);
69   }
70   removed = eventer_remove(e);
71   noitL(nldeb, "    remove from eventer system %s\n",
72         removed ? "succeeded" : "failed");
73   if(e->closure) {
74     struct nl_generic_cl *cl;
75     cl = e->closure;
76     if(cl->free) cl->free(cl);
77   }
78   eventer_free(e);
79   free(ev);
80 }
81 void
82 noit_lua_check_register_event(noit_lua_check_info_t *ci, eventer_t e) {
83   eventer_t *eptr;
84   eptr = calloc(1, sizeof(*eptr));
85   memcpy(eptr, &e, sizeof(*eptr));
86   if(!ci->events) ci->events = calloc(1, sizeof(*ci->events));
87   assert(noit_hash_store(ci->events, (const char *)eptr, sizeof(*eptr), eptr));
88 }
89 void
90 noit_lua_check_deregister_event(noit_lua_check_info_t *ci, eventer_t e,
91                                 int tofree) {
92   assert(ci->events);
93   assert(noit_hash_delete(ci->events, (const char *)&e, sizeof(e),
94                           NULL, tofree ? noit_event_dispose : free));
95 }
96 void
97 noit_lua_check_clean_events(noit_lua_check_info_t *ci) {
98   noit_hash_destroy(ci->events, NULL, noit_event_dispose);
99   free(ci->events);
100   ci->events = NULL;
101 }
102
103 static void
104 noit_lua_pushmodule(lua_State *L, const char *m) {
105   int stack_pos = LUA_GLOBALSINDEX;
106   char *copy, *part, *brkt;
107   copy = alloca(strlen(m)+1);
108   assert(copy);
109   memcpy(copy,m,strlen(m)+1);
110
111   for(part = strtok_r(copy, ".", &brkt);
112       part;
113       part = strtok_r(NULL, ".", &brkt)) {
114     lua_getfield(L, stack_pos, part);
115     if(stack_pos == -1) lua_remove(L, -2);
116     else stack_pos = -1;
117   }
118 }
119 static void
120 noit_lua_hash_to_table(lua_State *L,
121                        noit_hash_table *t) {
122   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
123   const char *key, *value;
124   int klen;
125   lua_createtable(L, 0, t ? t->size : 0);
126   if(t) {
127     while(noit_hash_next(t, &iter, &key, &klen, (void **)&value)) {
128       lua_pushlstring(L, value, strlen(value));
129       lua_setfield(L, -2, key);
130     }
131   }
132   return;
133 }
134 static int
135 noit_module_index_func(lua_State *L) {
136   int n;
137   n = lua_gettop(L);    /* number of arguments */
138   assert(n == 2);
139   if(!luaL_checkudata(L, 1, "noit_module_t")) {
140     luaL_error(L, "metatable error, arg1 not a noit_module_t!");
141   }
142   return 0;
143 }
144 static int
145 noit_lua_set_available(lua_State *L) {
146   noit_check_t *check;
147   noit_lua_check_info_t *ci;
148   if(lua_gettop(L)) luaL_error(L, "wrong number of arguments");
149   check = lua_touserdata(L, lua_upvalueindex(1));
150   ci = check->closure;
151   ci->current.available = lua_tointeger(L, lua_upvalueindex(2));
152   return 0;
153 }
154 static int
155 noit_lua_set_state(lua_State *L) {
156   noit_check_t *check;
157   noit_lua_check_info_t *ci;
158   if(lua_gettop(L)) luaL_error(L, "wrong number of arguments");
159   check = lua_touserdata(L, lua_upvalueindex(1));
160   ci = check->closure;
161   ci->current.state = lua_tointeger(L, lua_upvalueindex(2));
162   return 0;
163 }
164 static int
165 noit_lua_set_status(lua_State *L) {
166   noit_check_t *check;
167   noit_lua_check_info_t *ci;
168   if(lua_gettop(L) != 1) luaL_error(L, "wrong number of arguments");
169   check = lua_touserdata(L, lua_upvalueindex(1));
170   ci = check->closure;
171   /* strdup here... but free later */
172   ci->current.status = strdup(lua_tostring(L, 1));
173   return 0;
174 }
175 static int
176 noit_lua_set_metric(lua_State *L) {
177   noit_check_t *check;
178   noit_lua_check_info_t *ci;
179   const char *metric_name;
180   metric_type_t metric_type;
181
182   double __n;
183   int32_t __i;
184   u_int32_t __I;
185   int64_t __l;
186   u_int64_t __L;
187
188   if(lua_gettop(L) != 2) luaL_error(L, "wrong number of arguments");
189   check = lua_touserdata(L, lua_upvalueindex(1));
190   ci = check->closure;
191   if(!lua_isstring(L, 1)) luaL_error(L, "argument #1 must be a string");
192   metric_name = lua_tostring(L, 1);
193   metric_type = lua_tointeger(L, lua_upvalueindex(2));
194   if(lua_isnil(L, 2)) {
195     noit_stats_set_metric(&ci->current, metric_name, metric_type, NULL);
196     return 0;
197   }
198   switch(metric_type) {
199     case METRIC_GUESS:
200     case METRIC_STRING:
201       noit_stats_set_metric(&ci->current, metric_name, metric_type,
202                             (void *)lua_tostring(L, 2));
203       break;
204     case METRIC_INT32:
205       __i = luaL_optinteger(L, 2, 0);
206       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__i);
207       break;
208     case METRIC_UINT32:
209       __I = luaL_optinteger(L, 2, 0);
210       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__I);
211       break;
212     case METRIC_INT64:
213       __l = luaL_optinteger(L, 2, 0);
214       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__l);
215       break;
216     case METRIC_UINT64:
217       __L = luaL_optinteger(L, 2, 0);
218       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__L);
219       break;
220     case METRIC_DOUBLE:
221       __n = luaL_optnumber(L, 2, 0);
222       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__n);
223       break;
224   }
225   return 0;
226 }
227 static int
228 noit_check_index_func(lua_State *L) {
229   int n;
230   const char *k;
231   noit_check_t **udata, *check;
232   n = lua_gettop(L);    /* number of arguments */
233   assert(n == 2);
234   if(!luaL_checkudata(L, 1, "noit_check_t")) {
235     luaL_error(L, "metatable error, arg1 not a noit_check_t!");
236   }
237   udata = lua_touserdata(L, 1);
238   check = *udata;
239   if(!lua_isstring(L, 2)) {
240     luaL_error(L, "metatable error, arg2 not a string!");
241   }
242   k = lua_tostring(L, 2);
243   switch(*k) {
244     case 'a':
245       if(!strcmp(k, "available")) {
246         lua_pushlightuserdata(L, check);
247         lua_pushinteger(L, NP_AVAILABLE);
248         lua_pushcclosure(L, noit_lua_set_available, 2);
249       }
250       else break;
251       return 1;
252     case 'b':
253       if(!strcmp(k, "bad")) {
254         lua_pushlightuserdata(L, check);
255         lua_pushinteger(L, NP_BAD);
256         lua_pushcclosure(L, noit_lua_set_state, 2);
257       }
258       else break;
259       return 1;
260     case 'c':
261       if(!strcmp(k, "config")) noit_lua_hash_to_table(L, check->config);
262       else break;
263       return 1;
264     case 'g':
265       if(!strcmp(k, "good")) {
266         lua_pushlightuserdata(L, check);
267         lua_pushinteger(L, NP_GOOD);
268         lua_pushcclosure(L, noit_lua_set_state, 2);
269       }
270       else break;
271       return 1;
272     case 'm':
273       if(!strcmp(k, "module")) lua_pushstring(L, check->module);
274
275 #define IF_METRIC_BLOCK(name,type) \
276       if(!strcmp(k, name)) { \
277         lua_pushlightuserdata(L, check); \
278         lua_pushinteger(L, type); \
279         lua_pushcclosure(L, noit_lua_set_metric, 2); \
280       }
281
282       else IF_METRIC_BLOCK("metric", METRIC_GUESS)
283       else IF_METRIC_BLOCK("metric_string", METRIC_STRING)
284       else IF_METRIC_BLOCK("metric_int32", METRIC_INT32)
285       else IF_METRIC_BLOCK("metric_uint32", METRIC_UINT32)
286       else IF_METRIC_BLOCK("metric_int64", METRIC_INT64)
287       else IF_METRIC_BLOCK("metric_uint64", METRIC_UINT64)
288       else IF_METRIC_BLOCK("metric_double", METRIC_DOUBLE)
289       else break;
290       return 1;
291     case 'n':
292       if(!strcmp(k, "name")) lua_pushstring(L, check->name);
293       else break;
294       return 1;
295     case 'p':
296       if(!strcmp(k, "period")) lua_pushinteger(L, check->period);
297       else break;
298       return 1;
299     case 's':
300       if(!strcmp(k, "status")) {
301         lua_pushlightuserdata(L, check);
302         lua_pushcclosure(L, noit_lua_set_status, 1);
303       }
304       else break;
305       return 1;
306     case 't':
307       if(!strcmp(k, "target")) lua_pushstring(L, check->target);
308       else if(!strcmp(k, "timeout")) lua_pushinteger(L, check->timeout);
309       else break;
310       return 1;
311     case 'u':
312       if(!strcmp(k, "unavailable")) {
313         lua_pushlightuserdata(L, check);
314         lua_pushinteger(L, NP_UNAVAILABLE);
315         lua_pushcclosure(L, noit_lua_set_available, 2);
316       }
317       else break;
318       return 1;
319     default:
320       break;
321   }
322   luaL_error(L, "noit_check_t no such element: %s", k);
323   return 0;
324 }
325 static void
326 noit_lua_setup_module(lua_State *L,
327                       noit_module_t *mod) {
328   noit_module_t **addr;
329   addr = (noit_module_t **)lua_newuserdata(L, sizeof(mod));
330   *addr = mod;
331   if(luaL_newmetatable(L, "noit_module_t") == 1) {
332     lua_pushcclosure(L, noit_module_index_func, 0);
333     lua_setfield(L, -2, "__index");
334   }
335   lua_setmetatable(L, -2);
336 }
337 static void
338 noit_lua_setup_check(lua_State *L,
339                      noit_check_t *check) {
340   noit_check_t **addr;
341   addr = (noit_check_t **)lua_newuserdata(L, sizeof(check));
342   *addr = check;
343   if(luaL_newmetatable(L, "noit_check_t") == 1) {
344     lua_pushcclosure(L, noit_check_index_func, 0);
345     lua_setfield(L, -2, "__index");
346   }
347   lua_setmetatable(L, -2);
348 }
349 static int
350 noit_lua_module_onload(noit_image_t *i) {
351   int rv;
352   lua_State *L;
353   lua_module_closure_t *lmc;
354
355   lmc = noit_image_get_userdata(i);
356   L = lmc->lua_state;
357   lua_getglobal(L, "require");
358   lua_pushstring(L, lmc->object);
359   rv = lua_pcall(L, 1, 1, 0);
360   if(rv) {
361     int i;
362     noitL(nlerr, "lua: %s.onload failed\n", lmc->object);
363     i = lua_gettop(L);
364     if(i>0) {
365       if(lua_isstring(L, i)) {
366         const char *err;
367         size_t len;
368         err = lua_tolstring(L, i, &len);
369         noitL(nlerr, "lua: %s\n", err);
370       }
371     }
372     lua_pop(L,i);
373     return -1;
374   }
375   lua_pop(L, lua_gettop(L));
376
377   noit_lua_pushmodule(L, lmc->object);
378   if(lua_isnil(L, -1)) {
379     lua_pop(L, 1);
380     noitL(nlerr, "lua: no such object %s\n", lmc->object);
381     return -1;
382   }
383   lua_getfield(L, -1, "onload");
384   lua_remove(L, -2);
385   if(!lua_isfunction(L, -1)) {
386     lua_pop(L, 1);
387     /* No onload */
388     return 0;
389   }
390   noit_lua_setup_module(L, (noit_module_t *)i);
391   lua_pcall(L, 1, 1, 0);
392   if(lua_isnumber(L, -1)) {
393     int rv;
394     rv = lua_tointeger(L, -1);
395     lua_pop(L, 1);
396     return rv;
397   }
398   lua_pop(L,1);
399   noitL(nlerr, "%s.onload must return a integer\n", lmc->object);
400   return -1;
401 }
402 #define LMC_DECL(L, mod) \
403   lua_State *L; \
404   lua_module_closure_t *lmc; \
405   lmc = noit_module_get_userdata(mod); \
406   L = lmc->lua_state
407 #define SETUP_CALL(L, func, failure) do { \
408   noit_lua_pushmodule(L, lmc->object); \
409   lua_getfield(L, -1, func); \
410   lua_remove(L, -2); \
411   if(!lua_isfunction(L, -1)) { \
412     lua_pop(L, 1); \
413     failure; \
414   } \
415 } while(0)
416 #define RETURN_INT(L, func) do { \
417   int base = lua_gettop(L); \
418   assert(base == 1); \
419   if(lua_isnumber(L, -1)) { \
420     int rv; \
421     rv = lua_tointeger(L, -1); \
422     lua_pop(L, 1); \
423     return rv; \
424   } \
425   lua_pop(L,1); \
426   noitL(nlerr, "%s.%s must return a integer\n", lmc->object, func); \
427 } while(0)
428
429 static int
430 noit_lua_module_config(noit_module_t *mod,
431                        noit_hash_table *options) {
432   LMC_DECL(L, mod);
433   SETUP_CALL(L, "config", return 0);
434
435   noit_lua_setup_module(L, mod);
436   noit_lua_hash_to_table(L, options);
437   lua_pcall(L, 2, 1, 0);
438
439   RETURN_INT(L, "config");
440   return -1;
441 }
442 static int
443 noit_lua_module_init(noit_module_t *mod) {
444   LMC_DECL(L, mod);
445   SETUP_CALL(L, "init", return 0);
446
447   noit_lua_setup_module(L, mod);
448   lua_pcall(L, 1, 1, 0);
449
450   RETURN_INT(L, "init");
451   return -1;
452 }
453 static void
454 noit_lua_module_cleanup(noit_module_t *mod, noit_check_t *check) {
455   noit_lua_check_info_t *ci = check->closure;
456   LMC_DECL(L, mod);
457   SETUP_CALL(L, "cleanup", goto clean);
458
459   noit_lua_setup_module(L, mod);
460   noit_lua_setup_check(L, check);
461   lua_pcall(L, 2, 0, 0);
462
463  clean:
464   if(ci) {
465     noit_lua_check_clean_events(ci);
466     free(ci);
467     check->closure = NULL;
468   }
469 }
470
471 /* Here is where the magic starts */
472 static void
473 noit_lua_log_results(noit_module_t *self, noit_check_t *check) {
474   noit_lua_check_info_t *ci = check->closure;
475   struct timeval duration;
476   u_int32_t duration_ms;
477
478   gettimeofday(&ci->finish_time, NULL);
479   sub_timeval(ci->finish_time, check->last_fire_time, &duration);
480
481   memcpy(&ci->current.whence, &ci->finish_time, sizeof(ci->current.whence));
482   ci->current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
483   duration_ms = ci->current.duration;
484
485   noit_check_set_stats(self, check, &ci->current);
486   free(ci->current.status);
487 }
488 int
489 noit_lua_yield(noit_lua_check_info_t *ci, int nargs) {
490   noitL(nldeb, "lua: %p yielding\n", ci->coro_state);
491   return lua_yield(ci->coro_state, nargs);
492 }
493 int
494 noit_lua_resume(noit_lua_check_info_t *ci, int nargs) {
495   int result, base;
496   noit_check_t *check;
497   check = ci->check;
498
499   noitL(nldeb, "lua: %p resuming\n", ci->coro_state);
500   result = lua_resume(ci->coro_state, nargs);
501   switch(result) {
502     case 0: /* success */
503       break;
504     case LUA_YIELD: /* The complicated case */
505       /* The person yielding had better setup an event
506        * to wake up the coro...
507        */
508       lua_gc(ci->lmc->lua_state, LUA_GCCOLLECT, 0);
509       goto done;
510     default: /* Errors */
511       noitL(nlerr, "lua resume returned: %d\n", result);
512       ci->current.status = ci->timed_out ? "timeout" : "unknown error";
513       ci->current.available = NP_UNAVAILABLE;
514       ci->current.state = NP_BAD;
515       base = lua_gettop(ci->coro_state);
516       if(base>0) {
517         if(lua_isstring(ci->coro_state, base)) {
518           const char *err;
519           err = lua_tostring(ci->coro_state, base);
520           if(err) ci->current.status = (char *)err;
521         }
522       }
523       break;
524   }
525   lua_getglobal(ci->lmc->lua_state, "noit_coros");
526   luaL_unref(ci->lmc->lua_state, -1, ci->coro_state_ref);
527   lua_pop(ci->lmc->lua_state, 1);
528   lua_gc(ci->lmc->lua_state, LUA_GCCOLLECT, 0);
529
530   noit_lua_log_results(ci->self, ci->check);
531   noit_lua_module_cleanup(ci->self, ci->check);
532   ci = NULL; /* we freed it... explode if someone uses it before we return */
533   check->flags &= ~NP_RUNNING;
534
535  done:
536   return result;
537 }
538 static int
539 noit_lua_check_timeout(eventer_t e, int mask, void *closure,
540                        struct timeval *now) {
541   noit_check_t *check;
542   struct nl_intcl *int_cl = closure;
543   noit_lua_check_info_t *ci = int_cl->ci;
544   check = ci->check;
545   noitL(nldeb, "lua: %p ->check_timeout\n", ci->coro_state);
546   ci->timed_out = 1;
547   noit_lua_check_deregister_event(ci, e, 0);
548   if(ci->coro_state) {
549     /* Our coro is still "in-flight". To fix this we will unreference
550      * it, garbage collect it and then ensure that it failes a resume
551      */
552     lua_getglobal(ci->lmc->lua_state, "noit_coros");
553     luaL_unref(ci->lmc->lua_state, -1, ci->coro_state_ref);
554     lua_pop(ci->lmc->lua_state, 1);
555     lua_gc(ci->lmc->lua_state, LUA_GCCOLLECT, 0);
556     assert(2 == noit_lua_resume(ci, 0));
557   } else {
558     noit_lua_log_results(ci->self, ci->check);
559     noit_lua_module_cleanup(ci->self, ci->check);
560     check->flags &= ~NP_RUNNING;
561   }
562   if(int_cl->free) int_cl->free(int_cl);
563   return 0;
564 }
565 static int
566 noit_lua_initiate(noit_module_t *self, noit_check_t *check) {
567   LMC_DECL(L, self);
568   struct nl_intcl *int_cl;
569   noit_lua_check_info_t *ci;
570   struct timeval p_int, __now;
571   eventer_t e;
572
573   if(!check->closure) check->closure = calloc(1, sizeof(noit_lua_check_info_t));
574   ci = check->closure;
575
576   /* We cannot be running */
577   assert(!(check->flags & NP_RUNNING));
578   check->flags |= NP_RUNNING;
579
580   ci->self = self;
581   ci->check = check;
582   noit_check_stats_clear(&ci->current);
583
584   gettimeofday(&__now, NULL);
585   memcpy(&check->last_fire_time, &__now, sizeof(__now));
586
587   e = eventer_alloc();
588   e->mask = EVENTER_TIMER;
589   e->callback = noit_lua_check_timeout;
590   /* We wrap this in an alloc so we can blindly free it later */
591   int_cl = calloc(1, sizeof(*int_cl));
592   int_cl->ci = ci;
593   int_cl->free = int_cl_free;
594   e->closure = int_cl;
595   memcpy(&e->whence, &__now, sizeof(__now));
596   p_int.tv_sec = check->timeout / 1000;
597   p_int.tv_usec = (check->timeout % 1000) * 1000;
598   add_timeval(e->whence, p_int, &e->whence);
599   noit_lua_check_register_event(ci, e);
600   eventer_add(e);
601
602   noitL(nldeb, "initiate gettop => %d\n", lua_gettop(L));
603   ci->lmc = lmc;
604   lua_getglobal(L, "noit_coros");
605   ci->coro_state = lua_newthread(L);
606   ci->coro_state_ref = luaL_ref(L, -2);
607   lua_pop(L, 1); /* pops noit_coros */
608   noit_hash_store(&noit_coros,
609                   (const char *)&ci->coro_state, sizeof(ci->coro_state),
610                   ci);
611
612   SETUP_CALL(ci->coro_state, "initiate", goto fail);
613   noit_lua_setup_module(ci->coro_state, ci->self);
614   noit_lua_setup_check(ci->coro_state, ci->check);
615   noit_lua_resume(ci, 2);
616
617   return 0;
618
619  fail:
620   noit_lua_log_results(ci->self, ci->check);
621   noit_lua_module_cleanup(ci->self, ci->check);
622   check->flags &= ~NP_RUNNING;
623   return -1;
624 }
625 /* This is the standard wrapper */
626 static int
627 noit_lua_module_initiate_check(noit_module_t *self, noit_check_t *check,
628                                int once, noit_check_t *cause) {
629   if(!check->closure) check->closure = calloc(1, sizeof(noit_lua_check_info_t));
630   INITIATE_CHECK(noit_lua_initiate, self, check);
631   return 0;
632 }
633 static noit_module_t *
634 noit_lua_loader_load(noit_module_loader_t *loader,
635                      char *module_name,
636                      noit_conf_section_t section) {
637   noit_module_t *m;
638   lua_State *L;
639   lua_module_closure_t *lmc;
640   char *object;
641  
642   noitL(nldeb, "Loading lua module: %s\n", module_name);
643   if(noit_conf_get_string(section, "@object", &object) == 0) {
644     noitL(nlerr, "Lua module %s require object attribute.\n", module_name);
645     return NULL;
646   }
647
648   m = noit_blank_module();
649   m->hdr.magic = NOIT_MODULE_MAGIC;
650   m->hdr.version = NOIT_MODULE_ABI_VERSION;
651   m->hdr.name = strdup(module_name);
652   m->hdr.description = strdup("Lua module");
653   m->hdr.onload = noit_lua_module_onload;
654   lmc = calloc(1, sizeof(*lmc));
655   lmc->object = object;
656
657   L = lmc->lua_state = lua_open();
658
659
660   lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
661   luaL_openlibs(L);  /* open libraries */
662   luaopen_noit(L);
663   lua_newtable(L);
664   lua_setglobal(L, "noit_coros");
665
666   lua_getfield(L, LUA_GLOBALSINDEX, "package");
667   lua_pushfstring(L, "%s", noit_lua_loader_get_directory(loader));
668   lua_setfield(L, -2, "path");
669   lua_pop(L, 1);
670
671   lua_gc(L, LUA_GCRESTART, 0);
672
673   noit_image_set_userdata(&m->hdr, lmc);
674   if(m->hdr.onload(&m->hdr) == -1) {
675     free(m->hdr.name);
676     free(m->hdr.description);
677     free(lmc->object);
678     free(lmc);
679     /* FIXME: We leak the opaque_handler in the module here... */
680     free(m);
681     return NULL;
682   }
683
684   m->config = noit_lua_module_config;
685   m->init = noit_lua_module_init;
686   m->initiate_check = noit_lua_module_initiate_check;
687   m->cleanup = noit_lua_module_cleanup;
688
689   noit_register_module(m);
690   return m;
691 }
692
693 static int
694 noit_lua_loader_config(noit_module_loader_t *self, noit_hash_table *o) {
695   char *dir = ".";
696   noit_hash_retrieve(o, "directory", strlen("directory"), (void **)&dir);
697   noit_lua_loader_set_directory(self, dir);
698   return 0;
699 }
700 static int
701 noit_lua_loader_onload(noit_image_t *self) {
702   nlerr = noit_log_stream_find("error/serf");
703   nldeb = noit_log_stream_find("debug/serf");
704   if(!nlerr) nlerr = noit_stderr;
705   if(!nldeb) nldeb = noit_debug;
706   return 0;
707 }
708
709 noit_module_loader_t lua = {
710   {
711     NOIT_LOADER_MAGIC,
712     NOIT_LOADER_ABI_VERSION,
713     "lua",
714     "Lua check loader",
715     noit_lua_loader_onload,
716   },
717   noit_lua_loader_config,
718   NULL,
719   noit_lua_loader_load
720 };
Note: See TracBrowser for help on using the browser.