| 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, expr) 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 |
expr \ |
|---|
| 585 |
return rv; \ |
|---|
| 586 |
} \ |
|---|
| 587 |
lua_pop(L,1); \ |
|---|
| 588 |
noitL(nlerr, "%s.%s must return a integer\n", lmc->object, func); \ |
|---|
| 589 |
} while(0) |
|---|
| 590 |
|
|---|
| 591 |
static int |
|---|
| 592 |
noit_lua_module_config(noit_module_t *mod, |
|---|
| 593 |
noit_hash_table *options) { |
|---|
| 594 |
LMC_DECL(L, mod); |
|---|
| 595 |
SETUP_CALL(L, "config", return 0); |
|---|
| 596 |
|
|---|
| 597 |
noit_lua_setup_module(L, mod); |
|---|
| 598 |
noit_lua_hash_to_table(L, options); |
|---|
| 599 |
noit_hash_destroy(options, free, free); |
|---|
| 600 |
free(options); |
|---|
| 601 |
lua_pcall(L, 2, 1, 0); |
|---|
| 602 |
|
|---|
| 603 |
/* If rv == 0, the caller will free options. We've |
|---|
| 604 |
* already freed options, that would be bad. fudge -> 1 */ |
|---|
| 605 |
RETURN_INT(L, "config", { rv = (rv == 0) ? 1 : rv; }); |
|---|
| 606 |
return -1; |
|---|
| 607 |
} |
|---|
| 608 |
static int |
|---|
| 609 |
noit_lua_module_init(noit_module_t *mod) { |
|---|
| 610 |
LMC_DECL(L, mod); |
|---|
| 611 |
SETUP_CALL(L, "init", return 0); |
|---|
| 612 |
|
|---|
| 613 |
noit_lua_setup_module(L, mod); |
|---|
| 614 |
lua_pcall(L, 1, 1, 0); |
|---|
| 615 |
|
|---|
| 616 |
RETURN_INT(L, "init", ); |
|---|
| 617 |
return -1; |
|---|
| 618 |
} |
|---|
| 619 |
static void |
|---|
| 620 |
noit_lua_module_cleanup(noit_module_t *mod, noit_check_t *check) { |
|---|
| 621 |
noit_lua_check_info_t *ci = check->closure; |
|---|
| 622 |
LMC_DECL(L, mod); |
|---|
| 623 |
SETUP_CALL(L, "cleanup", goto clean); |
|---|
| 624 |
|
|---|
| 625 |
noit_lua_setup_module(L, mod); |
|---|
| 626 |
noit_lua_setup_check(L, check); |
|---|
| 627 |
lua_pcall(L, 2, 0, 0); |
|---|
| 628 |
|
|---|
| 629 |
clean: |
|---|
| 630 |
if(ci) { |
|---|
| 631 |
noit_lua_check_clean_events(ci); |
|---|
| 632 |
free(ci); |
|---|
| 633 |
check->closure = NULL; |
|---|
| 634 |
} |
|---|
| 635 |
} |
|---|
| 636 |
|
|---|
| 637 |
/* Here is where the magic starts */ |
|---|
| 638 |
static void |
|---|
| 639 |
noit_lua_log_results(noit_module_t *self, noit_check_t *check) { |
|---|
| 640 |
noit_lua_check_info_t *ci = check->closure; |
|---|
| 641 |
struct timeval duration; |
|---|
| 642 |
|
|---|
| 643 |
gettimeofday(&ci->finish_time, NULL); |
|---|
| 644 |
sub_timeval(ci->finish_time, check->last_fire_time, &duration); |
|---|
| 645 |
|
|---|
| 646 |
memcpy(&ci->current.whence, &ci->finish_time, sizeof(ci->current.whence)); |
|---|
| 647 |
ci->current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000; |
|---|
| 648 |
|
|---|
| 649 |
/* Only set out stats/log if someone has actually performed a check */ |
|---|
| 650 |
if(ci->current.state != NP_UNKNOWN || |
|---|
| 651 |
ci->current.available != NP_UNKNOWN) |
|---|
| 652 |
noit_check_set_stats(self, check, &ci->current); |
|---|
| 653 |
free(ci->current.status); |
|---|
| 654 |
} |
|---|
| 655 |
int |
|---|
| 656 |
noit_lua_yield(noit_lua_check_info_t *ci, int nargs) { |
|---|
| 657 |
noitL(nldeb, "lua: %p yielding\n", ci->coro_state); |
|---|
| 658 |
return lua_yield(ci->coro_state, nargs); |
|---|
| 659 |
} |
|---|
| 660 |
int |
|---|
| 661 |
noit_lua_resume(noit_lua_check_info_t *ci, int nargs) { |
|---|
| 662 |
int result = -1, base; |
|---|
| 663 |
noit_module_t *self; |
|---|
| 664 |
noit_check_t *check; |
|---|
| 665 |
|
|---|
| 666 |
noitL(nldeb, "lua: %p resuming(%d)\n", ci->coro_state, nargs); |
|---|
| 667 |
result = lua_resume(ci->coro_state, nargs); |
|---|
| 668 |
switch(result) { |
|---|
| 669 |
case 0: /* success */ |
|---|
| 670 |
break; |
|---|
| 671 |
case LUA_YIELD: /* The complicated case */ |
|---|
| 672 |
/* The person yielding had better setup an event |
|---|
| 673 |
* to wake up the coro... |
|---|
| 674 |
*/ |
|---|
| 675 |
lua_gc(ci->lmc->lua_state, LUA_GCCOLLECT, 0); |
|---|
| 676 |
goto done; |
|---|
| 677 |
default: /* Errors */ |
|---|
| 678 |
noitL(nldeb, "lua resume returned: %d\n", result); |
|---|
| 679 |
ci->current.status = strdup(ci->timed_out ? "timeout" : "unknown error"); |
|---|
| 680 |
ci->current.available = NP_UNAVAILABLE; |
|---|
| 681 |
ci->current.state = NP_BAD; |
|---|
| 682 |
base = lua_gettop(ci->coro_state); |
|---|
| 683 |
if(base>0) { |
|---|
| 684 |
if(lua_isstring(ci->coro_state, base)) { |
|---|
| 685 |
const char *err, *nerr; |
|---|
| 686 |
err = lua_tostring(ci->coro_state, base); |
|---|
| 687 |
nerr = lua_tostring(ci->coro_state, base - 2); |
|---|
| 688 |
if(err) noitL(nldeb, "err -> %s\n", err); |
|---|
| 689 |
if(nerr) noitL(nldeb, "nerr -> %s\n", nerr); |
|---|
| 690 |
if(nerr && *nerr == 31) nerr = NULL; // 31? WTF lua? |
|---|
| 691 |
if(!nerr && err) { |
|---|
| 692 |
nerr = strchr(err, ' '); /* advance past the file */ |
|---|
| 693 |
if(nerr) nerr += 1; |
|---|
| 694 |
} |
|---|
| 695 |
if(nerr) { |
|---|
| 696 |
free(ci->current.status); |
|---|
| 697 |
ci->current.status = strdup(nerr); |
|---|
| 698 |
} |
|---|
| 699 |
} |
|---|
| 700 |
} |
|---|
| 701 |
break; |
|---|
| 702 |
} |
|---|
| 703 |
|
|---|
| 704 |
self = ci->self; |
|---|
| 705 |
check = ci->check; |
|---|
| 706 |
cancel_coro(ci); |
|---|
| 707 |
noit_lua_log_results(self, check); |
|---|
| 708 |
noit_lua_module_cleanup(self, check); |
|---|
| 709 |
ci = NULL; /* we freed it... explode if someone uses it before we return */ |
|---|
| 710 |
check->flags &= ~NP_RUNNING; |
|---|
| 711 |
|
|---|
| 712 |
done: |
|---|
| 713 |
return result; |
|---|
| 714 |
} |
|---|
| 715 |
static int |
|---|
| 716 |
noit_lua_check_timeout(eventer_t e, int mask, void *closure, |
|---|
| 717 |
struct timeval *now) { |
|---|
| 718 |
noit_module_t *self; |
|---|
| 719 |
noit_check_t *check; |
|---|
| 720 |
struct nl_intcl *int_cl = closure; |
|---|
| 721 |
noit_lua_check_info_t *ci = int_cl->ci; |
|---|
| 722 |
noitL(nldeb, "lua: %p ->check_timeout\n", ci->coro_state); |
|---|
| 723 |
ci->timed_out = 1; |
|---|
| 724 |
noit_lua_check_deregister_event(ci, e, 0); |
|---|
| 725 |
|
|---|
| 726 |
self = ci->self; |
|---|
| 727 |
check = ci->check; |
|---|
| 728 |
|
|---|
| 729 |
if(ci->coro_state) { |
|---|
| 730 |
/* Our coro is still "in-flight". To fix this we will unreference |
|---|
| 731 |
* it, garbage collect it and then ensure that it failes a resume |
|---|
| 732 |
*/ |
|---|
| 733 |
cancel_coro(ci); |
|---|
| 734 |
} |
|---|
| 735 |
ci->current.status = strdup("timeout"); |
|---|
| 736 |
ci->current.available = NP_UNAVAILABLE; |
|---|
| 737 |
ci->current.state = NP_BAD; |
|---|
| 738 |
|
|---|
| 739 |
noit_lua_log_results(self, check); |
|---|
| 740 |
noit_lua_module_cleanup(self, check); |
|---|
| 741 |
check->flags &= ~NP_RUNNING; |
|---|
| 742 |
|
|---|
| 743 |
if(int_cl->free) int_cl->free(int_cl); |
|---|
| 744 |
return 0; |
|---|
| 745 |
} |
|---|
| 746 |
static int |
|---|
| 747 |
noit_lua_initiate(noit_module_t *self, noit_check_t *check, |
|---|
| 748 |
noit_check_t *cause) { |
|---|
| 749 |
LMC_DECL(L, self); |
|---|
| 750 |
struct nl_intcl *int_cl; |
|---|
| 751 |
noit_lua_check_info_t *ci; |
|---|
| 752 |
struct timeval p_int, __now; |
|---|
| 753 |
eventer_t e; |
|---|
| 754 |
|
|---|
| 755 |
if(!check->closure) check->closure = calloc(1, sizeof(noit_lua_check_info_t)); |
|---|
| 756 |
ci = check->closure; |
|---|
| 757 |
|
|---|
| 758 |
/* We cannot be running */ |
|---|
| 759 |
assert(!(check->flags & NP_RUNNING)); |
|---|
| 760 |
check->flags |= NP_RUNNING; |
|---|
| 761 |
|
|---|
| 762 |
ci->self = self; |
|---|
| 763 |
ci->check = check; |
|---|
| 764 |
ci->cause = cause; |
|---|
| 765 |
noit_check_stats_clear(&ci->current); |
|---|
| 766 |
|
|---|
| 767 |
gettimeofday(&__now, NULL); |
|---|
| 768 |
memcpy(&check->last_fire_time, &__now, sizeof(__now)); |
|---|
| 769 |
|
|---|
| 770 |
e = eventer_alloc(); |
|---|
| 771 |
e->mask = EVENTER_TIMER; |
|---|
| 772 |
e->callback = noit_lua_check_timeout; |
|---|
| 773 |
/* We wrap this in an alloc so we can blindly free it later */ |
|---|
| 774 |
int_cl = calloc(1, sizeof(*int_cl)); |
|---|
| 775 |
int_cl->ci = ci; |
|---|
| 776 |
int_cl->free = int_cl_free; |
|---|
| 777 |
e->closure = int_cl; |
|---|
| 778 |
memcpy(&e->whence, &__now, sizeof(__now)); |
|---|
| 779 |
p_int.tv_sec = check->timeout / 1000; |
|---|
| 780 |
p_int.tv_usec = (check->timeout % 1000) * 1000; |
|---|
| 781 |
add_timeval(e->whence, p_int, &e->whence); |
|---|
| 782 |
noit_lua_check_register_event(ci, e); |
|---|
| 783 |
eventer_add(e); |
|---|
| 784 |
|
|---|
| 785 |
ci->lmc = lmc; |
|---|
| 786 |
lua_getglobal(L, "noit_coros"); |
|---|
| 787 |
ci->coro_state = lua_newthread(L); |
|---|
| 788 |
ci->coro_state_ref = luaL_ref(L, -2); |
|---|
| 789 |
lua_pop(L, 1); /* pops noit_coros */ |
|---|
| 790 |
noit_hash_store(&noit_coros, |
|---|
| 791 |
(const char *)&ci->coro_state, sizeof(ci->coro_state), |
|---|
| 792 |
ci); |
|---|
| 793 |
|
|---|
| 794 |
SETUP_CALL(ci->coro_state, "initiate", goto fail); |
|---|
| 795 |
noit_lua_setup_module(ci->coro_state, ci->self); |
|---|
| 796 |
noit_lua_setup_check(ci->coro_state, ci->check); |
|---|
| 797 |
if(cause) |
|---|
| 798 |
noit_lua_setup_check(ci->coro_state, ci->cause); |
|---|
| 799 |
else |
|---|
| 800 |
lua_pushnil(ci->coro_state); |
|---|
| 801 |
noit_lua_resume(ci, 3); |
|---|
| 802 |
return 0; |
|---|
| 803 |
|
|---|
| 804 |
fail: |
|---|
| 805 |
noit_lua_log_results(ci->self, ci->check); |
|---|
| 806 |
noit_lua_module_cleanup(ci->self, ci->check); |
|---|
| 807 |
check->flags &= ~NP_RUNNING; |
|---|
| 808 |
return -1; |
|---|
| 809 |
} |
|---|
| 810 |
/* This is the standard wrapper */ |
|---|
| 811 |
static int |
|---|
| 812 |
noit_lua_module_initiate_check(noit_module_t *self, noit_check_t *check, |
|---|
| 813 |
int once, noit_check_t *cause) { |
|---|
| 814 |
if(!check->closure) check->closure = calloc(1, sizeof(noit_lua_check_info_t)); |
|---|
| 815 |
INITIATE_CHECK(noit_lua_initiate, self, check, cause); |
|---|
| 816 |
return 0; |
|---|
| 817 |
} |
|---|
| 818 |
static int noit_lua_panic(lua_State *L) { |
|---|
| 819 |
assert(L == NULL); |
|---|
| 820 |
return 0; |
|---|
| 821 |
} |
|---|
| 822 |
static noit_module_t * |
|---|
| 823 |
noit_lua_loader_load(noit_module_loader_t *loader, |
|---|
| 824 |
char *module_name, |
|---|
| 825 |
noit_conf_section_t section) { |
|---|
| 826 |
int rv; |
|---|
| 827 |
noit_module_t *m; |
|---|
| 828 |
lua_State *L; |
|---|
| 829 |
lua_module_closure_t *lmc; |
|---|
| 830 |
char *object; |
|---|
| 831 |
|
|---|
| 832 |
noitL(nldeb, "Loading lua module: %s\n", module_name); |
|---|
| 833 |
if(noit_conf_get_string(section, "@object", &object) == 0) { |
|---|
| 834 |
noitL(nlerr, "Lua module %s require object attribute.\n", module_name); |
|---|
| 835 |
return NULL; |
|---|
| 836 |
} |
|---|
| 837 |
|
|---|
| 838 |
m = noit_blank_module(); |
|---|
| 839 |
m->hdr.magic = NOIT_MODULE_MAGIC; |
|---|
| 840 |
m->hdr.version = NOIT_MODULE_ABI_VERSION; |
|---|
| 841 |
m->hdr.name = strdup(module_name); |
|---|
| 842 |
m->hdr.description = strdup("Lua module"); |
|---|
| 843 |
m->hdr.onload = noit_lua_module_onload; |
|---|
| 844 |
lmc = calloc(1, sizeof(*lmc)); |
|---|
| 845 |
lmc->object = object; |
|---|
| 846 |
|
|---|
| 847 |
L = lmc->lua_state = lua_open(); |
|---|
| 848 |
lua_atpanic(L, &noit_lua_panic); |
|---|
| 849 |
|
|---|
| 850 |
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ |
|---|
| 851 |
luaL_openlibs(L); /* open libraries */ |
|---|
| 852 |
luaopen_pack(L); |
|---|
| 853 |
luaopen_noit(L); |
|---|
| 854 |
|
|---|
| 855 |
lua_newtable(L); |
|---|
| 856 |
lua_setglobal(L, "noit_coros"); |
|---|
| 857 |
|
|---|
| 858 |
lua_getfield(L, LUA_GLOBALSINDEX, "package"); |
|---|
| 859 |
lua_pushfstring(L, "%s", noit_lua_loader_get_directory(loader)); |
|---|
| 860 |
lua_setfield(L, -2, "path"); |
|---|
| 861 |
lua_pop(L, 1); |
|---|
| 862 |
|
|---|
| 863 |
#define require(a) do { \ |
|---|
| 864 |
lua_getglobal(L, "require"); \ |
|---|
| 865 |
lua_pushstring(L, #a); \ |
|---|
| 866 |
rv = lua_pcall(L, 1, 1, 0); \ |
|---|
| 867 |
if(rv != 0) { \ |
|---|
| 868 |
noitL(noit_stderr, "Loading: %d\n", rv); \ |
|---|
| 869 |
goto load_failed; \ |
|---|
| 870 |
} \ |
|---|
| 871 |
lua_pop(L, 1); \ |
|---|
| 872 |
} while(0) |
|---|
| 873 |
|
|---|
| 874 |
require(noit.timeval); |
|---|
| 875 |
|
|---|
| 876 |
lua_gc(L, LUA_GCRESTART, 0); |
|---|
| 877 |
|
|---|
| 878 |
noit_image_set_userdata(&m->hdr, lmc); |
|---|
| 879 |
if(m->hdr.onload(&m->hdr) == -1) { |
|---|
| 880 |
load_failed: |
|---|
| 881 |
lua_close(L); |
|---|
| 882 |
free(m->hdr.name); |
|---|
| 883 |
free(m->hdr.description); |
|---|
| 884 |
free(lmc->object); |
|---|
| 885 |
free(lmc); |
|---|
| 886 |
/* FIXME: We leak the opaque_handler in the module here... */ |
|---|
| 887 |
free(m); |
|---|
| 888 |
return NULL; |
|---|
| 889 |
} |
|---|
| 890 |
|
|---|
| 891 |
m->config = noit_lua_module_config; |
|---|
| 892 |
m->init = noit_lua_module_init; |
|---|
| 893 |
m->initiate_check = noit_lua_module_initiate_check; |
|---|
| 894 |
m->cleanup = noit_lua_module_cleanup; |
|---|
| 895 |
|
|---|
| 896 |
noit_register_module(m); |
|---|
| 897 |
return m; |
|---|
| 898 |
} |
|---|
| 899 |
|
|---|
| 900 |
static int |
|---|
| 901 |
noit_lua_loader_config(noit_module_loader_t *self, noit_hash_table *o) { |
|---|
| 902 |
const char *dir = "."; |
|---|
| 903 |
noit_hash_retr_str(o, "directory", strlen("directory"), &dir); |
|---|
| 904 |
noit_lua_loader_set_directory(self, dir); |
|---|
| 905 |
return 0; |
|---|
| 906 |
} |
|---|
| 907 |
static int |
|---|
| 908 |
noit_lua_loader_onload(noit_image_t *self) { |
|---|
| 909 |
nlerr = noit_log_stream_find("error/lua"); |
|---|
| 910 |
nldeb = noit_log_stream_find("debug/lua"); |
|---|
| 911 |
if(!nlerr) nlerr = noit_stderr; |
|---|
| 912 |
if(!nldeb) nldeb = noit_debug; |
|---|
| 913 |
eventer_name_callback("lua/check_timeout", noit_lua_check_timeout); |
|---|
| 914 |
return 0; |
|---|
| 915 |
} |
|---|
| 916 |
|
|---|
| 917 |
#include "lua.xmlh" |
|---|
| 918 |
noit_module_loader_t lua = { |
|---|
| 919 |
{ |
|---|
| 920 |
NOIT_LOADER_MAGIC, |
|---|
| 921 |
NOIT_LOADER_ABI_VERSION, |
|---|
| 922 |
"lua", |
|---|
| 923 |
"Lua check loader", |
|---|
| 924 |
lua_xml_description, |
|---|
| 925 |
noit_lua_loader_onload, |
|---|
| 926 |
}, |
|---|
| 927 |
noit_lua_loader_config, |
|---|
| 928 |
NULL, |
|---|
| 929 |
noit_lua_loader_load |
|---|
| 930 |
}; |
|---|