Пример #1
0
int process(lua_sandbox* lsb, double ts)
{
  static const char* func_name = "process";
  lua_State* lua = lsb_get_lua(lsb);
  if (!lua) return 1;

  if (lsb_pcall_setup(lsb, func_name)) return 1;

  lua_pushnumber(lua, ts);
  if (lua_pcall(lua, 1, 1, 0) != 0) {
    char err[LSB_ERROR_SIZE];
    int len = snprintf(err, LSB_ERROR_SIZE, "%s() %s", func_name,
                       lua_tostring(lua, -1));
    if (len >= LSB_ERROR_SIZE || len < 0) {
      err[LSB_ERROR_SIZE - 1] = 0;
    }
    lsb_terminate(lsb, err);
    return 1;
  }

  if (!lua_isnumber(lua, 1)) {
    char err[LSB_ERROR_SIZE];
    int len = snprintf(err, LSB_ERROR_SIZE,
                       "%s() must return a single numeric value", func_name);
    if (len >= LSB_ERROR_SIZE || len < 0) {
      err[LSB_ERROR_SIZE - 1] = 0;
    }
    lsb_terminate(lsb, err);
    return 1;
  }

  int status = (int)lua_tointeger(lua, 1);
  lua_pop(lua, 1);

  lsb_pcall_teardown(lsb);

  return status;
}
Пример #2
0
int lsb_heka_pm_output(lsb_heka_sandbox *hsb,
                       lsb_heka_message *msg,
                       void *sequence_id,
                       bool profile)
{
  if (!hsb || !msg || hsb->type != 'o') return 1;

  if (lsb_pcall_setup(hsb->lsb, pm_func_name)) {
    char err[LSB_ERROR_SIZE];
    snprintf(err, LSB_ERROR_SIZE, "%s() function was not found", pm_func_name);
    lsb_terminate(hsb->lsb, err);
    return 1;
  }

  lua_State *lua = lsb_get_lua(hsb->lsb);
  if (!lua) return 1;

  int nargs = 0;
  if (sequence_id) {
    nargs = 1;
    lua_pushlightuserdata(lua, sequence_id);
  }
  return process_message(hsb, msg, lua, nargs, profile);
}
Пример #3
0
lsb_heka_sandbox* lsb_heka_create_output(void *parent,
                                         const char *lua_file,
                                         const char *state_file,
                                         const char *lsb_cfg,
                                         lsb_logger logger,
                                         lsb_heka_update_checkpoint ucp)
{
  if (!lua_file) {
    if (logger) logger(__FUNCTION__, 3, "lua_file must be specified");
    return NULL;
  }

  if (!ucp) {
    if (logger) logger(__FUNCTION__, 3, "update_checkpoint callback must be "
                       "specified");
    return NULL;
  }


  lsb_heka_sandbox *hsb = calloc(1, sizeof(lsb_heka_sandbox));
  if (!hsb) {
    if (logger) logger(__FUNCTION__, 3, "memory allocation failed");
    return NULL;
  }

  hsb->type = 'o';
  hsb->parent = parent;
  hsb->msg = NULL;
  hsb->cb.ucp = ucp;
  hsb->name = NULL;
  hsb->hostname = NULL;

  hsb->lsb = lsb_create(hsb, lua_file, lsb_cfg, logger);
  if (!hsb->lsb) {
    free(hsb);
    return NULL;
  }

  lua_State *lua = lsb_get_lua(hsb->lsb);
  set_restrictions(lua, hsb);

// todo link print to the logger or a no-op
  lsb_add_function(hsb->lsb, heka_decode_message, "decode_message");
  lsb_add_function(hsb->lsb, heka_encode_message, "encode_message");
  lsb_add_function(hsb->lsb, update_checkpoint, "update_checkpoint");

  if (lsb_init(hsb->lsb, state_file)) {
    if (logger) logger(hsb->name, 3, lsb_get_error(hsb->lsb));
    lsb_destroy(hsb->lsb);
    free(hsb->hostname);
    free(hsb->name);
    free(hsb);
    return NULL;
  }

// remove output function
  lua_pushnil(lua);
  lua_setglobal(lua, "output");

  return hsb;
}
Пример #4
0
lsb_heka_sandbox* lsb_heka_create_analysis(void *parent,
                                           const char *lua_file,
                                           const char *state_file,
                                           const char *lsb_cfg,
                                           lsb_logger logger,
                                           lsb_heka_im_analysis im)
{
  if (!lua_file) {
    if (logger) logger(__FUNCTION__, 3, "lua_file must be specified");
    return NULL;
  }

  if (!im) {
    if (logger) logger(__FUNCTION__, 3, "inject_message callback must be "
                       "specified");
    return NULL;
  }


  lsb_heka_sandbox *hsb = calloc(1, sizeof(lsb_heka_sandbox));
  if (!hsb) {
    if (logger) logger(__FUNCTION__, 3, "memory allocation failed");
    return NULL;
  }

  hsb->type = 'a';
  hsb->parent = parent;
  hsb->msg = NULL;
  hsb->cb.aim = im;
  hsb->name = NULL;
  hsb->hostname = NULL;

  hsb->lsb = lsb_create(hsb, lua_file, lsb_cfg, logger);
  if (!hsb->lsb) {
    free(hsb);
    return NULL;
  }

  lua_State *lua = lsb_get_lua(hsb->lsb);
  set_restrictions(lua, hsb);

// todo link print to the logger or a no-op
  lsb_add_function(hsb->lsb, heka_decode_message, "decode_message");
  lsb_add_function(hsb->lsb, read_message, "read_message");
  lsb_add_function(hsb->lsb, inject_message_analysis, "inject_message");
  lsb_add_function(hsb->lsb, inject_payload, "inject_payload");
// rename output to add_to_payload
  lua_getglobal(lua, "output");
  lua_setglobal(lua, "add_to_payload");
  lua_pushnil(lua);
  lua_setglobal(lua, "output");

  if (lsb_init(hsb->lsb, state_file)) {
    if (logger) logger(hsb->name, 3, lsb_get_error(hsb->lsb));
    lsb_destroy(hsb->lsb);
    free(hsb->hostname);
    free(hsb->name);
    free(hsb);
    return NULL;
  }
  return hsb;
}
Пример #5
0
lsb_heka_sandbox* lsb_heka_create_input(void *parent,
                                        const char *lua_file,
                                        const char *state_file,
                                        const char *lsb_cfg,
                                        lsb_logger logger,
                                        lsb_heka_im_input im)
{
  if (!lua_file) {
    if (logger) logger(__FUNCTION__, 3, "lua_file must be specified");
    return NULL;
  }

  if (!im) {
    if (logger) logger(__FUNCTION__, 3, "inject_message callback must be "
                       "specified");
    return NULL;
  }

  lsb_heka_sandbox *hsb = calloc(1, sizeof(lsb_heka_sandbox));
  if (!hsb) {
    if (logger) logger(__FUNCTION__, 3, "memory allocation failed");
    return NULL;
  }

  hsb->type = 'i';
  hsb->parent = parent;
  hsb->msg = NULL;
  hsb->cb.iim = im;
  hsb->name = NULL;
  hsb->hostname = NULL;

  hsb->lsb = lsb_create(hsb, lua_file, lsb_cfg, logger);
  if (!hsb->lsb) {
    free(hsb);
    return NULL;
  }

  lua_State *lua = lsb_get_lua(hsb->lsb);
  set_restrictions(lua, hsb);

// todo link print to the logger or a no-op
  lsb_add_function(hsb->lsb, heka_decode_message, "decode_message");
  lsb_add_function(hsb->lsb, read_message, "read_message");
  lsb_add_function(hsb->lsb, inject_message_input, "inject_message");
// inject_payload is intentionally excluded from input plugins
// you can construct whatever you need with inject_message

// preload the Heka stream reader module
  luaL_findtable(lua, LUA_REGISTRYINDEX, "_PRELOADED", 1);
  lua_pushstring(lua, mozsvc_heka_stream_reader_table);
  lua_pushcfunction(lua, luaopen_heka_stream_reader);
  lua_rawset(lua, -3);
  lua_pop(lua, 1); // remove the preloaded table

  if (lsb_init(hsb->lsb, state_file)) {
    if (logger) logger(hsb->name, 3, lsb_get_error(hsb->lsb));
    lsb_destroy(hsb->lsb);
    free(hsb->hostname);
    free(hsb->name);
    free(hsb);
    return NULL;
  }

// remove output function
  lua_pushnil(lua);
  lua_setglobal(lua, "output");

  return hsb;
}