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