root/src/modules/lua.c

Revision 87e8a7c5242cb8108f494d787f6548c873e82743, 25.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

bug, broke target

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007-2010, 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 = 0.0;
292   int32_t __i = 0;
293   u_int32_t __I = 0;
294   int64_t __l = 0;
295   u_int64_t __L = 0;
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     lua_pushboolean(L, 1);
306     return 1;
307   }
308   switch(metric_type) {
309     case METRIC_INT32:
310     case METRIC_UINT32:
311     case METRIC_INT64:
312     case METRIC_UINT64:
313     case METRIC_DOUBLE:
314       if(!lua_isnumber(L, 2)) {
315         noit_stats_set_metric(&ci->current, metric_name, metric_type, NULL);
316         lua_pushboolean(L, 0);
317         return 1;
318       }
319     default:
320     break;
321   }
322   switch(metric_type) {
323     case METRIC_GUESS:
324     case METRIC_STRING:
325       noit_stats_set_metric(&ci->current, metric_name, metric_type,
326                             (void *)lua_tostring(L, 2));
327       break;
328     case METRIC_INT32:
329       __i = strtol(lua_tostring(L, 2), NULL, 10);
330       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__i);
331       break;
332     case METRIC_UINT32:
333       __I = strtoul(lua_tostring(L, 2), NULL, 10);
334       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__I);
335       break;
336     case METRIC_INT64:
337       __l = strtoll(lua_tostring(L, 2), NULL, 10);
338       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__l);
339       break;
340     case METRIC_UINT64:
341       __L = strtoull(lua_tostring(L, 2), NULL, 10);
342       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__L);
343       break;
344     case METRIC_DOUBLE:
345       __n = luaL_optnumber(L, 2, 0);
346       noit_stats_set_metric(&ci->current, metric_name, metric_type, &__n);
347       break;
348   }
349   lua_pushboolean(L, 1);
350   return 1;
351 }
352 static int
353 noit_check_index_func(lua_State *L) {
354   int n;
355   const char *k;
356   noit_check_t **udata, *check;
357   n = lua_gettop(L);    /* number of arguments */
358   assert(n == 2);
359   if(!luaL_checkudata(L, 1, "noit_check_t")) {
360     luaL_error(L, "metatable error, arg1 not a noit_check_t!");
361   }
362   udata = lua_touserdata(L, 1);
363   check = *udata;
364   if(!lua_isstring(L, 2)) {
365     luaL_error(L, "metatable error, arg2 not a string!");
366   }
367   k = lua_tostring(L, 2);
368   switch(*k) {
369     case 'a':
370       if(!strcmp(k, "available")) {
371         lua_pushlightuserdata(L, check);
372         lua_pushinteger(L, NP_AVAILABLE);
373         lua_pushcclosure(L, noit_lua_set_available, 2);
374       }
375       else break;
376       return 1;
377     case 'b':
378       if(!strcmp(k, "bad")) {
379         lua_pushlightuserdata(L, check);
380         lua_pushinteger(L, NP_BAD);
381         lua_pushcclosure(L, noit_lua_set_state, 2);
382       }
383       else break;
384       return 1;
385     case 'c':
386       if(!strcmp(k, "config")) noit_lua_hash_to_table(L, check->config);
387       else break;
388       return 1;
389     case 'g':
390       if(!strcmp(k, "good")) {
391         lua_pushlightuserdata(L, check);
392         lua_pushinteger(L, NP_GOOD);
393         lua_pushcclosure(L, noit_lua_set_state, 2);
394       }
395       else break;
396       return 1;
397     case 'm':
398       if(!strcmp(k, "module")) lua_pushstring(L, check->module);
399
400 #define IF_METRIC_BLOCK(name,type) \
401       if(!strcmp(k, name)) { \
402         lua_pushlightuserdata(L, check); \
403         lua_pushinteger(L, type); \
404         lua_pushcclosure(L, noit_lua_set_metric, 2); \
405       }
406
407       else IF_METRIC_BLOCK("metric", METRIC_GUESS)
408       else IF_METRIC_BLOCK("metric_string", METRIC_STRING)
409       else IF_METRIC_BLOCK("metric_int32", METRIC_INT32)
410       else IF_METRIC_BLOCK("metric_uint32", METRIC_UINT32)
411       else IF_METRIC_BLOCK("metric_int64", METRIC_INT64)
412       else IF_METRIC_BLOCK("metric_uint64", METRIC_UINT64)
413       else IF_METRIC_BLOCK("metric_double", METRIC_DOUBLE)
414       else break;
415       return 1;
416     case 'n':
417       if(!strcmp(k, "name")) lua_pushstring(L, check->name);
418       else break;
419       return 1;
420     case 'p':
421       if(!strcmp(k, "period")) lua_pushinteger(L, check->period);
422       else break;
423       return 1;
424     case 's':
425       if(!strcmp(k, "status")) {
426         lua_pushlightuserdata(L, check);
427         lua_pushcclosure(L, noit_lua_set_status, 1);
428       }
429       else break;
430       return 1;
431     case 't':
432       if(!strcmp(k, "target")) lua_pushstring(L, check->target);
433       else if(!strcmp(k, "target_ip")) {
434         if(check->target_ip[0] == '\0') lua_pushnil(L);
435         else lua_pushstring(L, check->target_ip);
436       }
437       else if(!strcmp(k, "timeout")) lua_pushinteger(L, check->timeout);
438       else break;
439       return 1;
440     case 'u':
441       if(!strcmp(k, "unavailable")) {
442         lua_pushlightuserdata(L, check);
443         lua_pushinteger(L, NP_UNAVAILABLE);
444         lua_pushcclosure(L, noit_lua_set_available, 2);
445       }
446       else break;
447       return 1;
448     default:
449       break;
450   }
451   luaL_error(L, "noit_check_t no such element: %s", k);
452   return 0;
453 }
454 static void
455 noit_lua_setup_module(lua_State *L,
456                       noit_module_t *mod) {
457   noit_module_t **addr;
458   addr = (noit_module_t **)lua_newuserdata(L, sizeof(mod));
459   *addr = mod;
460   if(luaL_newmetatable(L, "noit_module_t") == 1) {
461     lua_pushcclosure(L, noit_module_index_func, 0);
462     lua_setfield(L, -2, "__index");
463   }
464   lua_setmetatable(L, -2);
465 }
466 static void
467 noit_lua_setup_check(lua_State *L,
468                      noit_check_t *check) {
469   noit_check_t **addr;
470   addr = (noit_check_t **)lua_newuserdata(L, sizeof(check));
471   *addr = check;
472   if(luaL_newmetatable(L, "noit_check_t") == 1) {
473     lua_pushcclosure(L, noit_check_index_func, 0);
474     lua_setfield(L, -2, "__index");
475   }
476   lua_setmetatable(L, -2);
477 }
478 static int
479 noit_lua_module_onload(noit_image_t *i) {
480   int rv;
481   lua_State *L;
482   lua_module_closure_t *lmc;
483
484   noit_lua_init();
485
486   lmc = noit_image_get_userdata(i);
487   L = lmc->lua_state;
488   lua_getglobal(L, "require");
489   lua_pushstring(L, lmc->object);
490   rv = lua_pcall(L, 1, 1, 0);
491   if(rv) {
492     int i;
493     noitL(nlerr, "lua: %s.onload failed\n", lmc->object);
494     i = lua_gettop(L);
495     if(i>0) {
496       if(lua_isstring(L, i)) {
497         const char *err;
498         size_t len;
499         err = lua_tolstring(L, i, &len);
500         noitL(nlerr, "lua: %s\n", err);
501       }
502     }
503     lua_pop(L,i);
504     return -1;
505   }
506   lua_pop(L, lua_gettop(L));
507
508   noit_lua_pushmodule(L, lmc->object);
509   if(lua_isnil(L, -1)) {
510     lua_pop(L, 1);
511     noitL(nlerr, "lua: no such object %s\n", lmc->object);
512     return -1;
513   }
514   lua_getfield(L, -1, "onload");
515   lua_remove(L, -2);
516   if(!lua_isfunction(L, -1)) {
517     lua_pop(L, 1);
518     /* No onload */
519     return 0;
520   }
521   noit_lua_setup_module(L, (noit_module_t *)i);
522   lua_pcall(L, 1, 1, 0);
523   if(lua_isnumber(L, -1)) {
524     int rv;
525     rv = lua_tointeger(L, -1);
526     lua_pop(L, 1);
527     return rv;
528   }
529   lua_pop(L,1);
530   noitL(nlerr, "%s.onload must return a integer\n", lmc->object);
531   return -1;
532 }
533 #define LMC_DECL(L, mod) \
534   lua_State *L; \
535   lua_module_closure_t *lmc; \
536   lmc = noit_module_get_userdata(mod); \
537   L = lmc->lua_state
538 #define SETUP_CALL(L, func, failure) do { \
539   noit_lua_pushmodule(L, lmc->object); \
540   lua_getfield(L, -1, func); \
541   lua_remove(L, -2); \
542   if(!lua_isfunction(L, -1)) { \
543     lua_pop(L, 1); \
544     failure; \
545   } \
546 } while(0)
547 #define RETURN_INT(L, func) do { \
548   int base = lua_gettop(L); \
549   assert(base == 1); \
550   if(lua_isnumber(L, -1)) { \
551     int rv; \
552     rv = lua_tointeger(L, -1); \
553     lua_pop(L, 1); \
554     return rv; \
555   } \
556   lua_pop(L,1); \
557   noitL(nlerr, "%s.%s must return a integer\n", lmc->object, func); \
558 } while(0)
559
560 static int
561 noit_lua_module_config(noit_module_t *mod,
562                        noit_hash_table *options) {
563   LMC_DECL(L, mod);
564   SETUP_CALL(L, "config", return 0);
565
566   noit_lua_setup_module(L, mod);
567   noit_lua_hash_to_table(L, options);
568   lua_pcall(L, 2, 1, 0);
569
570   RETURN_INT(L, "config");
571   return -1;
572 }
573 static int
574 noit_lua_module_init(noit_module_t *mod) {
575   LMC_DECL(L, mod);
576   SETUP_CALL(L, "init", return 0);
577
578   noit_lua_setup_module(L, mod);
579   lua_pcall(L, 1, 1, 0);
580
581   RETURN_INT(L, "init");
582   return -1;
583 }
584 static void
585 noit_lua_module_cleanup(noit_module_t *mod, noit_check_t *check) {
586   noit_lua_check_info_t *ci = check->closure;
587   LMC_DECL(L, mod);
588   SETUP_CALL(L, "cleanup", goto clean);
589
590   noit_lua_setup_module(L, mod);
591   noit_lua_setup_check(L, check);
592   lua_pcall(L, 2, 0, 0);
593
594  clean:
595   if(ci) {
596     noit_lua_check_clean_events(ci);
597     free(ci);
598     check->closure = NULL;
599   }
600 }
601
602 /* Here is where the magic starts */
603 static void
604 noit_lua_log_results(noit_module_t *self, noit_check_t *check) {
605   noit_lua_check_info_t *ci = check->closure;
606   struct timeval duration;
607
608   gettimeofday(&ci->finish_time, NULL);
609   sub_timeval(ci->finish_time, check->last_fire_time, &duration);
610
611   memcpy(&ci->current.whence, &ci->finish_time, sizeof(ci->current.whence));
612   ci->current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
613
614   noit_check_set_stats(self, check, &ci->current);
615   free(ci->current.status);
616 }
617 int
618 noit_lua_yield(noit_lua_check_info_t *ci, int nargs) {
619   noitL(nldeb, "lua: %p yielding\n", ci->coro_state);
620   return lua_yield(ci->coro_state, nargs);
621 }
622 int
623 noit_lua_resume(noit_lua_check_info_t *ci, int nargs) {
624   int result = -1, base;
625   noit_module_t *self;
626   noit_check_t *check;
627
628   noitL(nldeb, "lua: %p resuming\n", ci->coro_state);
629   result = lua_resume(ci->coro_state, nargs);
630   switch(result) {
631     case 0: /* success */
632       break;
633     case LUA_YIELD: /* The complicated case */
634       /* The person yielding had better setup an event
635        * to wake up the coro...
636        */
637       lua_gc(ci->lmc->lua_state, LUA_GCCOLLECT, 0);
638       goto done;
639     default: /* Errors */
640       noitL(nldeb, "lua resume returned: %d\n", result);
641       ci->current.status = strdup(ci->timed_out ? "timeout" : "unknown error");
642       ci->current.available = NP_UNAVAILABLE;
643       ci->current.state = NP_BAD;
644       base = lua_gettop(ci->coro_state);
645       if(base>0) {
646         if(lua_isstring(ci->coro_state, base)) {
647           const char *err, *nerr;
648           err = lua_tostring(ci->coro_state, base);
649           nerr = lua_tostring(ci->coro_state, base - 2);
650           if(nerr && *nerr == 31) nerr = NULL; // 31? WTF lua?
651           if(!nerr && err) {
652             nerr = strchr(err, ' '); /* advance past the file */
653             if(nerr) nerr += 1;
654           }
655           if(nerr) {
656             free(ci->current.status);
657             ci->current.status = strdup(nerr);
658           }
659         }
660       }
661       break;
662   }
663
664   self = ci->self;
665   check = ci->check;
666   cancel_coro(ci);
667   noit_lua_log_results(self, check);
668   noit_lua_module_cleanup(self, check);
669   ci = NULL; /* we freed it... explode if someone uses it before we return */
670   check->flags &= ~NP_RUNNING;
671
672  done:
673   return result;
674 }
675 static int
676 noit_lua_check_timeout(eventer_t e, int mask, void *closure,
677                        struct timeval *now) {
678   noit_module_t *self;
679   noit_check_t *check;
680   struct nl_intcl *int_cl = closure;
681   noit_lua_check_info_t *ci = int_cl->ci;
682   noitL(nldeb, "lua: %p ->check_timeout\n", ci->coro_state);
683   ci->timed_out = 1;
684   noit_lua_check_deregister_event(ci, e, 0);
685
686   self = ci->self;
687   check = ci->check;
688
689   if(ci->coro_state) {
690     /* Our coro is still "in-flight". To fix this we will unreference
691      * it, garbage collect it and then ensure that it failes a resume
692      */
693     cancel_coro(ci);
694   }
695   ci->current.status = strdup("timeout");
696   ci->current.available = NP_UNAVAILABLE;
697   ci->current.state = NP_BAD;
698
699   noit_lua_log_results(self, check);
700   noit_lua_module_cleanup(self, check);
701   check->flags &= ~NP_RUNNING;
702
703   if(int_cl->free) int_cl->free(int_cl);
704   return 0;
705 }
706 static int
707 noit_lua_initiate(noit_module_t *self, noit_check_t *check) {
708   LMC_DECL(L, self);
709   struct nl_intcl *int_cl;
710   noit_lua_check_info_t *ci;
711   struct timeval p_int, __now;
712   eventer_t e;
713
714   if(!check->closure) check->closure = calloc(1, sizeof(noit_lua_check_info_t));
715   ci = check->closure;
716
717   /* We cannot be running */
718   assert(!(check->flags & NP_RUNNING));
719   check->flags |= NP_RUNNING;
720
721   ci->self = self;
722   ci->check = check;
723   noit_check_stats_clear(&ci->current);
724
725   gettimeofday(&__now, NULL);
726   memcpy(&check->last_fire_time, &__now, sizeof(__now));
727
728   e = eventer_alloc();
729   e->mask = EVENTER_TIMER;
730   e->callback = noit_lua_check_timeout;
731   /* We wrap this in an alloc so we can blindly free it later */
732   int_cl = calloc(1, sizeof(*int_cl));
733   int_cl->ci = ci;
734   int_cl->free = int_cl_free;
735   e->closure = int_cl;
736   memcpy(&e->whence, &__now, sizeof(__now));
737   p_int.tv_sec = check->timeout / 1000;
738   p_int.tv_usec = (check->timeout % 1000) * 1000;
739   add_timeval(e->whence, p_int, &e->whence);
740   noit_lua_check_register_event(ci, e);
741   eventer_add(e);
742
743   ci->lmc = lmc;
744   lua_getglobal(L, "noit_coros");
745   ci->coro_state = lua_newthread(L);
746   ci->coro_state_ref = luaL_ref(L, -2);
747   lua_pop(L, 1); /* pops noit_coros */
748   noit_hash_store(&noit_coros,
749                   (const char *)&ci->coro_state, sizeof(ci->coro_state),
750                   ci);
751
752   SETUP_CALL(ci->coro_state, "initiate", goto fail);
753   noit_lua_setup_module(ci->coro_state, ci->self);
754   noit_lua_setup_check(ci->coro_state, ci->check);
755   noit_lua_resume(ci, 2);
756
757   return 0;
758
759  fail:
760   noit_lua_log_results(ci->self, ci->check);
761   noit_lua_module_cleanup(ci->self, ci->check);
762   check->flags &= ~NP_RUNNING;
763   return -1;
764 }
765 /* This is the standard wrapper */
766 static int
767 noit_lua_module_initiate_check(noit_module_t *self, noit_check_t *check,
768                                int once, noit_check_t *cause) {
769   if(!check->closure) check->closure = calloc(1, sizeof(noit_lua_check_info_t));
770   INITIATE_CHECK(noit_lua_initiate, self, check);
771   return 0;
772 }
773 static int noit_lua_panic(lua_State *L) {
774   assert(L == NULL);
775   return 0;
776 }
777 static noit_module_t *
778 noit_lua_loader_load(noit_module_loader_t *loader,
779                      char *module_name,
780                      noit_conf_section_t section) {
781   int rv;
782   noit_module_t *m;
783   lua_State *L;
784   lua_module_closure_t *lmc;
785   char *object;
786  
787   noitL(nldeb, "Loading lua module: %s\n", module_name);
788   if(noit_conf_get_string(section, "@object", &object) == 0) {
789     noitL(nlerr, "Lua module %s require object attribute.\n", module_name);
790     return NULL;
791   }
792
793   m = noit_blank_module();
794   m->hdr.magic = NOIT_MODULE_MAGIC;
795   m->hdr.version = NOIT_MODULE_ABI_VERSION;
796   m->hdr.name = strdup(module_name);
797   m->hdr.description = strdup("Lua module");
798   m->hdr.onload = noit_lua_module_onload;
799   lmc = calloc(1, sizeof(*lmc));
800   lmc->object = object;
801
802   L = lmc->lua_state = lua_open();
803   lua_atpanic(L, &noit_lua_panic);
804
805   lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
806   luaL_openlibs(L);  /* open libraries */
807   luaopen_pack(L);
808   luaopen_noit(L);
809
810   lua_newtable(L);
811   lua_setglobal(L, "noit_coros");
812
813   lua_getfield(L, LUA_GLOBALSINDEX, "package");
814   lua_pushfstring(L, "%s", noit_lua_loader_get_directory(loader));
815   lua_setfield(L, -2, "path");
816   lua_pop(L, 1);
817
818 #define require(a) do { \
819   lua_getglobal(L, "require"); \
820   lua_pushstring(L, #a); \
821   rv = lua_pcall(L, 1, 1, 0); \
822   if(rv != 0) { \
823     noitL(noit_stderr, "Loading: %d\n", rv); \
824     goto load_failed; \
825   } \
826   lua_pop(L, 1); \
827 } while(0)
828
829   require(noit.timeval);
830
831   lua_gc(L, LUA_GCRESTART, 0);
832
833   noit_image_set_userdata(&m->hdr, lmc);
834   if(m->hdr.onload(&m->hdr) == -1) {
835    load_failed:
836     lua_close(L);
837     free(m->hdr.name);
838     free(m->hdr.description);
839     free(lmc->object);
840     free(lmc);
841     /* FIXME: We leak the opaque_handler in the module here... */
842     free(m);
843     return NULL;
844   }
845
846   m->config = noit_lua_module_config;
847   m->init = noit_lua_module_init;
848   m->initiate_check = noit_lua_module_initiate_check;
849   m->cleanup = noit_lua_module_cleanup;
850
851   noit_register_module(m);
852   return m;
853 }
854
855 static int
856 noit_lua_loader_config(noit_module_loader_t *self, noit_hash_table *o) {
857   const char *dir = ".";
858   noit_hash_retr_str(o, "directory", strlen("directory"), &dir);
859   noit_lua_loader_set_directory(self, dir);
860   return 0;
861 }
862 static int
863 noit_lua_loader_onload(noit_image_t *self) {
864   nlerr = noit_log_stream_find("error/lua");
865   nldeb = noit_log_stream_find("debug/lua");
866   if(!nlerr) nlerr = noit_stderr;
867   if(!nldeb) nldeb = noit_debug;
868   eventer_name_callback("lua/check_timeout", noit_lua_check_timeout);
869   return 0;
870 }
871
872 #include "lua.xmlh"
873 noit_module_loader_t lua = {
874   {
875     NOIT_LOADER_MAGIC,
876     NOIT_LOADER_ABI_VERSION,
877     "lua",
878     "Lua check loader",
879     lua_xml_description,
880     noit_lua_loader_onload,
881   },
882   noit_lua_loader_config,
883   NULL,
884   noit_lua_loader_load
885 };
Note: See TracBrowser for help on using the browser.