Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make lua state access thread-safe #867

Merged
merged 2 commits into from Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions userspace/engine/falco_common.h
Expand Up @@ -21,6 +21,7 @@ limitations under the License.

#include <string>
#include <exception>
#include <mutex>

extern "C" {
#include "lua.h"
Expand Down Expand Up @@ -94,11 +95,10 @@ class falco_common
protected:
lua_State *m_ls;

std::mutex m_ls_semaphore;

sinsp *m_inspector;

private:
void add_lua_path(std::string &path);
};



};
4 changes: 2 additions & 2 deletions userspace/engine/falco_engine.cpp
Expand Up @@ -287,8 +287,8 @@ unique_ptr<falco_engine::rule_result> falco_engine::process_sinsp_event(sinsp_ev

unique_ptr<struct rule_result> res(new rule_result());

std::lock_guard<std::mutex> guard(m_ls_semaphore);
lua_getglobal(m_ls, lua_on_event.c_str());

if(lua_isfunction(m_ls, -1))
{
lua_pushnumber(m_ls, ev->get_check_id());
Expand Down Expand Up @@ -335,8 +335,8 @@ unique_ptr<falco_engine::rule_result> falco_engine::process_k8s_audit_event(json

unique_ptr<struct rule_result> res(new rule_result());

std::lock_guard<std::mutex> guard(m_ls_semaphore);
lua_getglobal(m_ls, lua_on_event.c_str());

if(lua_isfunction(m_ls, -1))
{
lua_pushnumber(m_ls, ev->get_check_id());
Expand Down
13 changes: 5 additions & 8 deletions userspace/falco/falco_outputs.cpp
Expand Up @@ -57,7 +57,6 @@ falco_outputs::~falco_outputs()
if(m_initialized)
{
lua_getglobal(m_ls, m_lua_output_cleanup.c_str());

if(!lua_isfunction(m_ls, -1))
{
falco_logger::log(LOG_ERR, std::string("No function ") + m_lua_output_cleanup + " found. ");
Expand Down Expand Up @@ -148,8 +147,8 @@ void falco_outputs::handle_event(gen_event *ev, string &rule, string &source,
return;
}

std::lock_guard<std::mutex> guard(m_ls_semaphore);
lua_getglobal(m_ls, m_lua_output_event.c_str());

if(lua_isfunction(m_ls, -1))
{
lua_pushlightuserdata(m_ls, ev);
Expand All @@ -170,7 +169,6 @@ void falco_outputs::handle_event(gen_event *ev, string &rule, string &source,
{
throw falco_exception("No function " + m_lua_output_event + " found in lua compiler module");
}

}

void falco_outputs::handle_msg(uint64_t now,
Expand Down Expand Up @@ -226,8 +224,8 @@ void falco_outputs::handle_msg(uint64_t now,
full_msg += ")";
}

std::lock_guard<std::mutex> guard(m_ls_semaphore);
lua_getglobal(m_ls, m_lua_output_msg.c_str());

if(lua_isfunction(m_ls, -1))
{
lua_pushstring(m_ls, full_msg.c_str());
Expand All @@ -251,7 +249,6 @@ void falco_outputs::handle_msg(uint64_t now,
void falco_outputs::reopen_outputs()
{
lua_getglobal(m_ls, m_lua_output_reopen.c_str());

if(!lua_isfunction(m_ls, -1))
{
throw falco_exception("No function " + m_lua_output_reopen + " found. ");
Expand All @@ -271,8 +268,8 @@ int falco_outputs::handle_http(lua_State *ls)
struct curl_slist *slist1;
slist1 = NULL;

if (!lua_isstring(ls, -1) ||
!lua_isstring(ls, -2))
if(!lua_isstring(ls, -1) ||
!lua_isstring(ls, -2))
{
lua_pushstring(ls, "Invalid arguments passed to handle_http()");
lua_error(ls);
Expand All @@ -298,7 +295,7 @@ int falco_outputs::handle_http(lua_State *ls)
curl_easy_cleanup(curl);
curl = NULL;
curl_slist_free_all(slist1);
slist1 = NULL;
slist1 = NULL;
}
return 1;
}
Expand Down