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

chore: Use modern Lua functions in chisels #1616

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
steps:
- name: Install deps ⛓️
run: |
apt update && apt install -y --no-install-recommends ca-certificates cmake build-essential git clang llvm pkg-config autoconf automake libtool libelf-dev wget libc-ares-dev libcurl4-openssl-dev libssl-dev libtbb-dev libjq-dev libjsoncpp-dev libgrpc++-dev protobuf-compiler-grpc libgtest-dev libprotobuf-dev liblua5.1-dev linux-headers-${{ matrix.arch }}
apt update && apt install -y --no-install-recommends ca-certificates cmake build-essential git clang llvm pkg-config autoconf automake libtool libelf-dev wget libc-ares-dev libcurl4-openssl-dev libssl-dev libtbb-dev libjq-dev libjsoncpp-dev libgrpc++-dev protobuf-compiler-grpc libgtest-dev libprotobuf-dev liblua5.2-dev linux-headers-${{ matrix.arch }}

- name: Checkout Libs ⤵️
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
Expand Down Expand Up @@ -65,7 +65,7 @@ jobs:
steps:
- name: Install deps ⛓️
run: |
apt update && apt install -y --no-install-recommends ca-certificates cmake build-essential git clang llvm pkg-config autoconf automake libtool libelf-dev wget libc-ares-dev libcurl4-openssl-dev libssl-dev libtbb-dev libjq-dev libjsoncpp-dev libgrpc++-dev protobuf-compiler-grpc libgtest-dev libprotobuf-dev liblua5.1-dev linux-headers-amd64
apt update && apt install -y --no-install-recommends ca-certificates cmake build-essential git clang llvm pkg-config autoconf automake libtool libelf-dev wget libc-ares-dev libcurl4-openssl-dev libssl-dev libtbb-dev libjq-dev libjsoncpp-dev libgrpc++-dev protobuf-compiler-grpc libgtest-dev libprotobuf-dev liblua5.2-dev linux-headers-amd64

- name: Checkout Libs ⤵️
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
Expand Down Expand Up @@ -128,7 +128,7 @@ jobs:
- name: Install deps ⛓️
run: |
sudo apt update
sudo apt install -y --no-install-recommends ca-certificates cmake build-essential git clang llvm pkg-config autoconf automake libtool libelf-dev wget libc-ares-dev libcurl4-openssl-dev libssl-dev libre2-dev libtbb-dev libjq-dev libjsoncpp-dev libgrpc++-dev protobuf-compiler-grpc libgtest-dev libprotobuf-dev liblua5.1-dev linux-headers-$(uname -r)
sudo apt install -y --no-install-recommends ca-certificates cmake build-essential git clang llvm pkg-config autoconf automake libtool libelf-dev wget libc-ares-dev libcurl4-openssl-dev libssl-dev libre2-dev libtbb-dev libjq-dev libjsoncpp-dev libgrpc++-dev protobuf-compiler-grpc libgtest-dev libprotobuf-dev liblua5.2-dev linux-headers-$(uname -r)
sudo .github/install-deps.sh

- name: Build and test 🏗️🧪
Expand Down Expand Up @@ -294,7 +294,7 @@ jobs:
protobuf-compiler-grpc \
libgtest-dev \
libprotobuf-dev \
liblua5.1-dev \
liblua5.2-dev \
"linux-headers-$(uname -r)"
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-14 90
sudo update-alternatives --install /usr/bin/llvm-strip llvm-strip /usr/bin/llvm-strip-14 90
Expand Down
8 changes: 2 additions & 6 deletions cmake/modules/luajit.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ elseif(NOT USE_BUNDLED_LUAJIT)
message(STATUS "Found LuaJIT: include: ${LUAJIT_INCLUDE}, lib: ${LUAJIT_LIB}")
else()
# alternatively try stock Lua
find_package(Lua51)
find_package(Lua REQUIRED)
set(LUAJIT_LIB ${LUA_LIBRARY})
set(LUAJIT_INCLUDE ${LUA_INCLUDE_DIR})

if(NOT ${LUA51_FOUND})
message(FATAL_ERROR "Couldn't find system LuaJIT or Lua")
endif()
endif()
else()
set(LUAJIT_SRC "${PROJECT_BINARY_DIR}/luajit-prefix/src/luajit/src")
Expand All @@ -38,7 +34,7 @@ else()
if(NOT WIN32)
set(LUAJIT_LIB "${LUAJIT_SRC}/libluajit.a")
else()
set(LUAJIT_LIB "${LUAJIT_SRC}/lua51.lib")
set(LUAJIT_LIB "${LUAJIT_SRC}/lua52.lib")
endif()

if(NOT TARGET luajit)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/containers/sinsp.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN apt-get update && \
libjsoncpp1 \
openssl \
libre2-5 \
liblua5.1-0 \
liblua5.2-0 \
libtbb2

COPY /sinsp-example /usr/local/bin/sinsp-example
Expand Down
43 changes: 27 additions & 16 deletions userspace/chisel/chisel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ void parse_lua_chisel_args(lua_State *ls, OUT chisel_desc* cd)
}
}

void sinsp_chisel::add_lua_package_path(lua_State* ls, const char* path)
void sinsp_chisel::add_lua_package_path(lua_State* ls, const std::string& path)
{
lua_getglobal(ls, "package");
lua_getfield(ls, -1, "path");
Expand Down Expand Up @@ -976,19 +976,24 @@ bool sinsp_chisel::init_lua_chisel(chisel_desc &cd, string const &fpath)
//
// Load our own lua libs
//
luaL_openlib(ls, CHISEL_TOOL_LIBRARY_NAME, ll_tool, 0);
luaL_openlib(ls, "chisel", ll_chisel, 0);
luaL_openlib(ls, "evt", ll_evt, 0);
lua_newtable(ls);
luaL_setfuncs(ls, ll_tool, 0);
lua_setglobal(ls, CHISEL_TOOL_LIBRARY_NAME);

lua_newtable(ls);
luaL_setfuncs(ls, ll_chisel, 0);
lua_setglobal(ls, "chisel");

lua_newtable(ls);
luaL_setfuncs(ls, ll_evt, 0);
lua_setglobal(ls, "evt");

//
// Add our chisel paths to package.path
//
for(vector<chiseldir_info>::const_iterator it = g_chisel_dirs->begin();
it != g_chisel_dirs->end(); ++it)
for(auto& el : *g_chisel_dirs)
{
string path(it->m_dir);
path += "?.lua";
add_lua_package_path(ls, path.c_str());
add_lua_package_path(ls, el.m_dir + "?.lua");
}

//
Expand Down Expand Up @@ -1181,18 +1186,24 @@ void sinsp_chisel::load(string cmdstr, bool is_file)
//
// Load our own lua libs
//
luaL_openlib(m_ls, CHISEL_TOOL_LIBRARY_NAME, ll_tool, 0);
luaL_openlib(m_ls, "chisel", ll_chisel, 0);
luaL_openlib(m_ls, "evt", ll_evt, 0);
lua_newtable(m_ls);
luaL_setfuncs(m_ls, ll_tool, 0);
lua_setglobal(m_ls, CHISEL_TOOL_LIBRARY_NAME);

lua_newtable(m_ls);
luaL_setfuncs(m_ls, ll_chisel, 0);
lua_setglobal(m_ls, "chisel");

lua_newtable(m_ls);
luaL_setfuncs(m_ls, ll_evt, 0);
lua_setglobal(m_ls, "evt");

//
// Add our chisel paths to package.path
//
for(uint32_t j = 0; j < g_chisel_dirs->size(); j++)
for(auto& el : *g_chisel_dirs)
{
string path(g_chisel_dirs->at(j).m_dir);
path += "?.lua";
add_lua_package_path(m_ls, path.c_str());
add_lua_package_path(m_ls, el.m_dir + "?.lua");
}

//
Expand Down
7 changes: 4 additions & 3 deletions userspace/chisel/chisel.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class chiselinfo
{
public:
chiselinfo(sinsp* inspector);
void init(std::string filterstr, std::string formatterstr);
void init(std::string filterstr, std::string formatterstr);
void set_filter(std::string filterstr);
void set_formatter(std::string formatterstr);
void set_callback_interval(uint64_t interval);
Expand All @@ -114,8 +114,10 @@ class SINSP_PUBLIC sinsp_chisel
public:
sinsp_chisel(sinsp* inspector, std::string filename, bool is_file = true);
~sinsp_chisel();
static void add_lua_package_path(lua_State* ls, const char* path);

static void add_lua_package_path(lua_State* ls, const std::string& path);
static void get_chisel_list(std::vector<chisel_desc>* chisel_descs);

void load(std::string cmdstr, bool is_file = true);
std::string get_name()
{
Expand Down Expand Up @@ -171,4 +173,3 @@ class SINSP_PUBLIC sinsp_chisel
};

/*@}*/

27 changes: 13 additions & 14 deletions userspace/chisel/chisel_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ limitations under the License.

*/

#include <iostream>
#include <fstream>
#include <cctype>
#include <locale>
#ifdef _WIN32
#include <io.h>
#else
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#endif
#include <json/json.h>

#include "strl.h"
#include "sinsp.h"
#include "sinsp_int.h"
Expand All @@ -52,6 +38,19 @@ extern "C" {
}
#endif

#include <cctype>
#include <fstream>
#include <iostream>
#include <locale>
#ifdef _WIN32
#include <io.h>
#else
#include <climits>
#include <cstdlib>
#include <unistd.h>
#include <sys/ioctl.h>
#endif

using namespace std;

extern vector<chiseldir_info>* g_chisel_dirs;
Expand Down