root/src/modules/lua.c

Revision 3821135574760be709319f24bb718263569486b4, 24.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

stackless lua errors should be put in the status while sanitizing the fileinfo

  • 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, "timeout")) lua_pushinteger(L, check->timeout);
434       else break;
435       return 1;
436     case 'u':
437       if(!strcmp(k, "unavailable")) {
438         lua_pushlightuserdata(L, check);
439         lua_pushinteger(L, NP_UNAVAILABLE);
440         lua_pushcclosure(L, noit_lua_set_available, 2);
441       }
442       else break;
443       return 1;
444     default:
445       break;
446   }
447   luaL_error(L, "noit_check_t no such element: %s", k);
448   return 0;
449 }
450 static void
451 noit_lua_setup_module(lua_State *L,
452                       noit_module_t *mod) {
453   noit_module_t **addr;
454   addr = (noit_module_t **)lua_newuserdata(L, sizeof(mod));
455   *addr = mod;
456   if(luaL_newmetatable(L, "noit_module_t") == 1) {
457     lua_pushcclosure(L, noit_module_index_func, 0);
458     lua_setfield(L, -2, "__index");
459   }
460   lua_setmetatable(L, -2);
461 }
462 static void
463 noit_lua_setup_check(lua_State *L,
464                      noit_check_t *check) {
465   noit_check_t **addr;
466   addr = (noit_check_t **)lua_newuserdata(L, sizeof(check));
467   *addr = check;
468   if(luaL_newmetatable(L, "noit_check_t") == 1) {
469     lua_pushcclosure(L, noit_check_index_func, 0);
470     lua_setfield(L, -2, "__index");
471   }
472   lua_setmetatable(L, -2);
473 }
474 static int
475 noit_lua_module_onload(noit_image_t *i) {
476   int rv;
477   lua_State *L;
478   lua_module_closure_t *lmc;
479
480   noit_lua_init();
481
482   lmc = noit_image_get_userdata(i);
483   L = lmc->lua_state;
484   lua_getglobal(L, "require");
485   lua_pushstring(L, lmc->object);
486   rv = lua_pcall(L, 1, 1, 0);
487   if(rv) {
488     int i;
489     noitL(nlerr, "lua: %s.onload failed\n", lmc->object);
490     i = lua_gettop(L);
491     if(i>0) {
492       if(lua_isstring(L, i)) {
493         const char *err;
494         size_t len;
495         err = lua_tolstring(L, i, &len);
496         noitL(nlerr, "lua: %s\n", err);
497       }
498     }
499     lua_pop(L,i);
500     return -1;
501   }
502   lua_pop(L, lua_gettop(L));
503
504   noit_lua_pushmodule(L, lmc->object);
505   if(lua_isnil(L, -1)) {
506     lua_pop(L, 1);
507     noitL(nlerr, "lua: no such object %s\n", lmc->object);
508     return -1;
509   }
510   lua_getfield(L, -1, "onload");
511   lua_remove(L, -2);
512   if(!lua_isfunction(L, -1)) {
513     lua_pop(L, 1);
514     /* No onload */
515     return 0;
516   }
517   noit_lua_setup_module(L, (noit_module_t *)i);
518   lua_pcall(L, 1, 1, 0);
519   if(lua_isnumber(L, -1)) {
520     int rv;
521     rv = lua_tointeger(L, -1);
522     lua_pop(L, 1);
523     return rv;
524   }
525   lua_pop(L,1);
526   noitL(nlerr, "%s.onload must return a integer\n", lmc->object);
527   return -1;
528 }
529 #define LMC_DECL(L, mod) \
530   lua_State *L; \
531   lua_module_closure_t *lmc; \
532   lmc = noit_module_get_userdata(mod); \
533   L = lmc->lua_state
534 #define SETUP_CALL(L, func, failure) do { \
535   noit_lua_pushmodule(L, lmc->object); \
536   lua_getfield(L, -1, func); \
537   lua_remove(L, -2); \
538   if(!lua_isfunction(L, -1)) { \
539     lua_pop(L, 1); \
540     failure; \
541   } \
542 } while(0)
543 #define RETURN_INT(L, func) do { \
544   int base = lua_gettop(L); \
545   assert(base == 1); \
546   if(lua_isnumber(L, -1)) { \
547     int rv; \
548     rv = lua_tointeger(L, -1); \
549     lua_pop(L, 1); \
550     return rv; \
551   } \
552   lua_pop(L,1); \
553   noitL(nlerr, "%s.%s must return a integer\n", lmc->object, func); \
554 } while(0)
555
556 static int
557 noit_lua_module_config(noit_module_t *mod,
558                        noit_hash_table *options) {
559   LMC_DECL(L, mod);
560   SETUP_CALL(L, "config", return 0);
561
562   noit_lua_setup_module(L, mod);
563   noit_lua_hash_to_table(L, options);
564   lua_pcall(L, 2, 1, 0);
565
566   RETURN_INT(L, "config");
567   return -1;
568 }
569 static int
570 noit_lua_module_init(noit_module_t *mod) {
571   LMC_DECL(L, mod);
572   SETUP_CALL(L, "init", return 0);
573
574   noit_lua_setup_module(L, mod);
575   lua_pcall(L, 1, 1, 0);
576
577   RETURN_INT(L, "init");
578   return -1;
579 }
580 static void
581 noit_lua_module_cleanup(noit_module_t *mod, noit_check_t *check) {
582   noit_lua_check_info_t *ci = check->closure;
583   LMC_DECL(L, mod);
584   SETUP_CALL(L, "cleanup", goto clean);
585
586   noit_lua_setup_module(L, mod);
587   noit_lua_setup_check(L, check);
588   lua_pcall(L, 2, 0, 0);
589
590  clean:
591   if(ci) {
592     noit_lua_check_clean_events(ci);
593     free(ci);
594     check->closure = NULL;
595   }
596 }
597
598 /* Here is where the magic starts */
599 static void
600 noit_lua_log_results(noit_module_t *self, noit_check_t *check) {
601   noit_lua_check_info_t *ci = check->closure;
602   struct timeval duration;
603
604   gettimeofday(&ci->finish_time, NULL);
605   sub_timeval(ci->finish_time, check->last_fire_time, &duration);
606
607   memcpy(&ci->current.whence, &ci->finish_time, sizeof(ci->current.whence));
608   ci->current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
609
610   noit_check_set_stats(self, check, &ci->current);
611   free(ci->current.status);
612 }
613 int
614 noit_lua_yield(noit_lua_check_info_t *ci, int nargs) {
615   noitL(nldeb, "lua: %p yielding\n", ci->coro_state);
616   return lua_yield(ci->coro_state, nargs);
617 }
618 int
619 noit_lua_resume(noit_lua_check_info_t *ci, int nargs) {
620   int result = -1, base;
621   noit_module_t *self;
622   noit_check_t *check;
623
624   noitL(nldeb, "lua: %p resuming\n", ci->coro_state);
625   result = lua_resume(ci->coro_state, nargs);
626   switch(result) {
627     case 0: /* success */
628       break;
629     case LUA_YIELD: /* The complicated case */
630       /* The person yielding had better setup an event
631        * to wake up the coro...
632        */
633       lua_gc(ci->lmc->lua_state, LUA_GCCOLLECT, 0);
634       goto done;
635     default: /* Errors */
636       noitL(nldeb, "lua resume returned: %d\n", result);
637       ci->current.status = strdup(ci->timed_out ? "timeout" : "unknown error");
638       ci->current.available = NP_UNAVAILABLE;
639       ci->current.state = NP_BAD;
640       base = lua_gettop(ci->coro_state);
641       if(base>0) {
642         if(lua_isstring(ci->coro_state, base)) {
643           const char *err, *nerr;
644           err = lua_tostring(ci->coro_state, base);
645           nerr = lua_tostring(ci->coro_state, base - 2);
646           if(nerr && *nerr == 31) nerr = NULL; // 31? WTF lua?
647           if(!nerr && err) {
648             nerr = strchr(err, ' '); /* advance past the file */
649             if(nerr) nerr += 1;
650           }
651           if(nerr) {
652             free(ci->current.status);
653             ci->current.status = strdup(nerr);
654           }
655         }
656       }
657       break;
658   }
659
660   self = ci->self;
661   check = ci->check;
662   cancel_coro(ci);
663   noit_lua_log_results(self, check);
664   noit_lua_module_cleanup(self, check);
665   ci = NULL; /* we freed it... explode if someone uses it before we return */
666   check->flags &= ~NP_RUNNING;
667
668  done:
669   return result;
670 }
671 static int
672 noit_lua_check_timeout(eventer_t e, int mask, void *closure,
673                        struct timeval *now) {
674   noit_module_t *self;
675   noit_check_t *check;
676   struct nl_intcl *int_cl = closure;
677   noit_lua_check_info_t *ci = int_cl->ci;
678   noitL(nldeb, "lua: %p ->check_timeout\n", ci->coro_state);
679   ci->timed_out = 1;
680   noit_lua_check_deregister_event(ci, e, 0);
681
682   self = ci->self;
683   check = ci->check;
684
685   if(ci->coro_state) {
686     /* Our coro is still "in-flight". To fix this we will unreference
687      * it, garbage collect it and then ensure that it failes a resume
688      */
689     cancel_coro(ci);
690   }
691   ci->current.status = strdup("timeout");
692   ci->current.available = NP_UNAVAILABLE;
693   ci->current.state = NP_BAD;
694
695   noit_lua_log_results(self, check);
696   noit_lua_module_cleanup(self, check);
697   check->flags &= ~NP_RUNNING;
698
699   if(int_cl->free) int_cl->free(int_cl);
700   return 0;
701 }
702 static int
703 noit_lua_initiate(noit_module_t *self, noit_check_t *check) {
704   LMC_DECL(L, self);
705   struct nl_intcl *int_cl;
706   noit_lua_check_info_t *ci;
707   struct timeval p_int, __now;
708   eventer_t e;
709
710   if(!check->closure) check->closure = calloc(1, sizeof(noit_lua_check_info_t));
711   ci = check->closure;
712
713   /* We cannot be running */
714   assert(!(check->flags & NP_RUNNING));
715   check->flags |= NP_RUNNING;
716
717   ci->self = self;
718   ci->check = check;
719   noit_check_stats_clear(&ci->current);
720
721   gettimeofday(&__now, NULL);
722   memcpy(&check->last_fire_time, &__now, sizeof(__now));
723
724   e = eventer_alloc();
725   e->mask = EVENTER_TIMER;
726   e->callback = noit_lua_check_timeout;
727   /* We wrap this in an alloc so we can blindly free it later */
728   int_cl = calloc(1, sizeof(*int_cl));
729   int_cl->ci = ci;
730   int_cl->free = int_cl_free;
731   e->closure = int_cl;
732   memcpy(&e->whence, &__now, sizeof(__now));
733   p_int.tv_sec = check->timeout / 1000;
734   p_int.tv_usec = (check->timeout % 1000) * 1000;
735   add_timeval(e->whence, p_int, &e->whence);
736   noit_lua_check_register_event(ci, e);
737   eventer_add(e);
738
739   ci->lmc = lmc;
740   lua_getglobal(L, "noit_coros");
741   ci->coro_state = lua_newthread(L);
742   ci->coro_state_ref = luaL_ref(L, -2);
743   lua_pop(L, 1); /* pops noit_coros */
744   noit_hash_store(&noit_coros,
745                   (const char *)&ci->coro_state, sizeof(ci->coro_state),
746                   ci);
747
748   SETUP_CALL(ci->coro_state, "initiate", goto fail);
749   noit_lua_setup_module(ci->coro_state, ci->self);
750   noit_lua_setup_check(ci->coro_state, ci->check);
751   noit_lua_resume(ci, 2);
752
753   return 0;
754
755  fail:
756   noit_lua_log_results(ci->self, ci->check);
757   noit_lua_module_cleanup(ci->self, ci->check);
758   check->flags &= ~NP_RUNNING;
759   return -1;
760 }
761 /* This is the standard wrapper */
762 static int
763 noit_lua_module_initiate_check(noit_module_t *self, noit_check_t *check,
764                                int once, noit_check_t *cause) {
765   if(!check->closure) check->closure = calloc(1, sizeof(noit_lua_check_info_t));
766   INITIATE_CHECK(noit_lua_initiate, self, check);
767   return 0;
768 }
769 static int noit_lua_panic(lua_State *L) {
770   assert(L == NULL);
771   return 0;
772 }
773 static noit_module_t *
774 noit_lua_loader_load(noit_module_loader_t *loader,
775                      char *module_name,
776                      noit_conf_section_t section) {
777   int rv;
778   noit_module_t *m;
779   lua_State *L;
780   lua_module_closure_t *lmc;
781   char *object;
782  
783   noitL(nldeb, "Loading lua module: %s\n", module_name);
784   if(noit_conf_get_string(section, "@object", &object) == 0) {
785     noitL(nlerr, "Lua module %s require object attribute.\n", module_name);
786     return NULL;
787   }
788
789   m = noit_blank_module();
790   m->hdr.magic = NOIT_MODULE_MAGIC;
791   m->hdr.version = NOIT_MODULE_ABI_VERSION;
792   m->hdr.name = strdup(module_name);
793   m->hdr.description = strdup("Lua module");
794   m->hdr.onload = noit_lua_module_onload;
795   lmc = calloc(1, sizeof(*lmc));
796   lmc->object = object;
797
798   L = lmc->lua_state = lua_open();
799   lua_atpanic(L, &noit_lua_panic);
800
801   lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
802   luaL_openlibs(L);  /* open libraries */
803   luaopen_pack(L);
804   luaopen_noit(L);
805
806   lua_newtable(L);
807   lua_setglobal(L, "noit_coros");
808
809   lua_getfield(L, LUA_GLOBALSINDEX, "package");
810   lua_pushfstring(L, "%s", noit_lua_loader_get_directory(loader));
811   lua_setfield(L, -2, "path");
812   lua_pop(L, 1);
813
814 #define require(a) do { \
815   lua_getglobal(L, "require"); \
816   lua_pushstring(L, #a); \
817   rv = lua_pcall(L, 1, 1, 0); \
818   if(rv != 0) { \
819     noitL(noit_stderr, "Loading: %d\n", rv); \
820     goto load_failed; \
821   } \
822   lua_pop(L, 1); \
823 } while(0)
824
825   require(noit.timeval);
826
827   lua_gc(L, LUA_GCRESTART, 0);
828
829   noit_image_set_userdata(&m->hdr, lmc);
830   if(m->hdr.onload(&m->hdr) == -1) {
831    load_failed:
832     lua_close(L);
833     free(m->hdr.name);
834     free(m->hdr.description);
835     free(lmc->object);
836     free(lmc);
837     /* FIXME: We leak the opaque_handler in the module here... */
838     free(m);
839     return NULL;
840   }
841
842   m->config = noit_lua_module_config;
843   m->init = noit_lua_module_init;
844   m->initiate_check = noit_lua_module_initiate_check;
845   m->cleanup = noit_lua_module_cleanup;
846
847   noit_register_module(m);
848   return m;
849 }
850
851 static int
852 noit_lua_loader_config(noit_module_loader_t *self, noit_hash_table *o) {
853   const char *dir = ".";
854   noit_hash_retr_str(o, "directory", strlen("directory"), &dir);
855   noit_lua_loader_set_directory(self, dir);
856   return 0;
857 }
858 static int
859 noit_lua_loader_onload(noit_image_t *self) {
860   nlerr = noit_log_stream_find("error/lua");
861   nldeb = noit_log_stream_find("debug/lua");
862   if(!nlerr) nlerr = noit_stderr;
863   if(!nldeb) nldeb = noit_debug;
864   eventer_name_callback("lua/check_timeout", noit_lua_check_timeout);
865   return 0;
866 }
867
868 #include "lua.xmlh"
869 noit_module_loader_t lua = {
870   {
871     NOIT_LOADER_MAGIC,
872     NOIT_LOADER_ABI_VERSION,
873     "lua",
874     "Lua check loader",
875     lua_xml_description,
876     noit_lua_loader_onload,
877   },
878   noit_lua_loader_config,
879   NULL,
880   noit_lua_loader_load
881 };
Note: See TracBrowser for help on using the browser.