static int inject_message(lua_State* L) { static unsigned char header[14]; void* luserdata = lua_touserdata(L, lua_upvalueindex(1)); if (NULL == luserdata) { return luaL_error(L, "inject_message() invalid lightuserdata"); } lua_sandbox* lsb = (lua_sandbox*)luserdata; hs_analysis_plugin* p = (hs_analysis_plugin*)lsb_get_parent(lsb); if (lua_type(L, 1) == LUA_TTABLE) { lua_pushstring(L, p->sb->name); lua_setfield(L, 1, "Logger"); lua_pushstring(L, p->at->plugins->cfg->hostname); lua_setfield(L, 1, "Hostname"); lua_pushinteger(L, p->at->plugins->cfg->pid); lua_setfield(L, 1, "Pid"); } if (lsb_output_protobuf(lsb, 1, 0) != 0) { return luaL_error(L, "inject_message() could not encode protobuf - %s", lsb_get_error(lsb)); } size_t output_len = 0; const char* output = lsb_get_output(lsb, &output_len); pthread_mutex_lock(&p->at->plugins->output.lock); int len = hs_write_varint(header + 3, output_len); int tlen = 4 + len + output_len; ++p->sb->stats.im_cnt; p->sb->stats.im_bytes += tlen; header[0] = 0x1e; header[1] = (char)(len + 1); header[2] = 0x08; header[3 + len] = 0x1f; fwrite(header, 4 + len, 1, p->at->plugins->output.fh); fwrite(output, output_len, 1, p->at->plugins->output.fh); p->at->plugins->output.offset += tlen; if (p->at->plugins->output.offset >= (size_t)p->at->plugins->cfg->output_size) { ++p->at->plugins->output.id; hs_open_output_file(&p->at->plugins->output); } pthread_mutex_unlock(&p->at->plugins->output.lock); return 0; }
int write_output(lua_State* lua) { static const char* default_type = "txt"; // static const char* default_name = ""; void* luserdata = lua_touserdata(lua, lua_upvalueindex(1)); if (NULL == luserdata) { luaL_error(lua, "write() invalid lightuserdata"); } lua_sandbox* lsb = (lua_sandbox*)luserdata; // void* ud = NULL; const char* type = default_type; // const char* name = default_name; switch (lua_gettop(lua)) { case 0: break; case 2: // name = luaL_checkstring(lua, 2); // fallthru case 1: switch (lua_type(lua, 1)) { case LUA_TSTRING: type = lua_tostring(lua, 1); if (strlen(type) == 0) type = default_type; break; case LUA_TTABLE: type = ""; if (lsb_output_protobuf(lsb, 1, 0) != 0) { luaL_error(lua, "write() cound not encode protobuf - %s", lsb_get_error(lsb)); } break; case LUA_TUSERDATA: type = lsb_output_userdata(lsb, 1, 0); break; default: luaL_typerror(lua, 1, "string, table, or circular_buffer"); break; } break; default: luaL_error(lua, "write() takes a maximum of 2 arguments"); break; } written_data = lsb_get_output(lsb, &written_data_len); return 0; }