root/src/modules/lua.c

Revision 95f54849093d7581fcc34d6d1012b021926f2d30, 24.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

that's just painful code copying and fixing. Hopefully, this will ultimately result in replacing the DNS C module with lua code so I only have to fix bugs in one of these nasties. refs #269

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