From e1a8a6f3738f1b085044b867014707ce91b60e3b Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Thu, 21 Jan 2016 19:08:23 +0200 Subject: [PATCH] Luafilter is not built by default The luafilter requires the lua development libraries so it isn't built by default. --- cmake/macros.cmake | 3 + server/modules/filter/CMakeLists.txt | 21 ++--- server/modules/filter/luafilter.c | 131 +++++++++++++++------------ 3 files changed, 86 insertions(+), 69 deletions(-) diff --git a/cmake/macros.cmake b/cmake/macros.cmake index 4b26d9beb..71ceb041b 100644 --- a/cmake/macros.cmake +++ b/cmake/macros.cmake @@ -65,6 +65,9 @@ macro(set_variables) # Build the multimaster monitor set(BUILD_MMMON TRUE CACHE BOOL "Build multimaster monitor") + # Build Luafilter + set(BUILD_LUAFILTER FALSE CACHE BOOL "Build Luafilter") + # Use gcov build flags set(GCOV FALSE CACHE BOOL "Use gcov build flags") diff --git a/server/modules/filter/CMakeLists.txt b/server/modules/filter/CMakeLists.txt index 497079993..fc22fca52 100644 --- a/server/modules/filter/CMakeLists.txt +++ b/server/modules/filter/CMakeLists.txt @@ -36,20 +36,17 @@ target_link_libraries(topfilter maxscale-common) set_target_properties(topfilter PROPERTIES VERSION "1.0.1") install(TARGETS topfilter DESTINATION ${MAXSCALE_LIBDIR}) -if(LUA_FOUND) - include_directories(${LUA_INCLUDE_DIR}) - add_library(luafilter SHARED luafilter.c) - target_link_libraries(luafilter log_manager utils ${LUA_LIBRARIES}) - install(TARGETS luafilter DESTINATION ${MAXSCALE_LIBDIR}) -else() - message(STATUS "Lua was not found, luafilter will not be built.") +if(BUILD_LUAFILTER) + if(LUA_FOUND) + include_directories(${LUA_INCLUDE_DIR}) + add_library(luafilter SHARED luafilter.c) + target_link_libraries(luafilter log_manager utils ${LUA_LIBRARIES}) + install(TARGETS luafilter DESTINATION ${MAXSCALE_LIBDIR}) + else() + message(STATUS "Lua was not found, luafilter will not be built.") + endif() endif() -add_library(dbfwfilter SHARED dbfwfilter.c) -target_link_libraries(dbfwfilter log_manager query_classifier) -set_target_properties(dbfwfilter PROPERTIES VERSION "1.0.0") -install(TARGETS dbfwfilter DESTINATION ${MAXSCALE_LIBDIR}) - add_library(namedserverfilter SHARED namedserverfilter.c) target_link_libraries(namedserverfilter maxscale-common) set_target_properties(namedserverfilter PROPERTIES VERSION "1.1.0") diff --git a/server/modules/filter/luafilter.c b/server/modules/filter/luafilter.c index f7a23a98a..6907c9fe8 100644 --- a/server/modules/filter/luafilter.c +++ b/server/modules/filter/luafilter.c @@ -173,23 +173,38 @@ static FILTER * createInstance(char **options, FILTER_PARAMETER **params) { LUA_INSTANCE *my_instance; + bool error = false; if ((my_instance = (LUA_INSTANCE*) calloc(1, sizeof(LUA_INSTANCE))) == NULL) { return NULL; } - spinlock_init(&my_instance->lock); - for (int i = 0; params[i]; i++) - { + spinlock_init(&my_instance->lock); + + for (int i = 0; params[i] && !error; i++) + { if (strcmp(params[i]->name, "global_script") == 0) { - my_instance->global_script = strdup(params[i]->value); + error = (my_instance->global_script = strdup(params[i]->value)) == NULL; } else if (strcmp(params[i]->name, "session_script") == 0) { - my_instance->session_script = strdup(params[i]->value); + error = (my_instance->session_script = strdup(params[i]->value)) == NULL; } + else if (!filter_standard_parameter(params[i]->name)) + { + MXS_ERROR("Unexpected parameter '%s'", params[i]->name); + error = true; + } + } + + if (error) + { + free(my_instance->global_script); + free(my_instance->session_script); + free(my_instance); + return NULL; } if (my_instance->global_script) @@ -205,16 +220,16 @@ createInstance(char **options, FILTER_PARAMETER **params) free(my_instance->global_script); free(my_instance->session_script); free(my_instance); - return NULL; + my_instance = NULL; } - - if (my_instance->global_lua_state) + else if (my_instance->global_lua_state) { lua_getglobal(my_instance->global_lua_state, "createInstance"); if (lua_pcall(my_instance->global_lua_state, 0, 0, 0)) { - MXS_ERROR("luafilter: Failed to get global variable 'createInstance': %s.", - lua_tostring(my_instance->global_lua_state, -1)); + MXS_WARNING("luafilter: Failed to get global variable 'createInstance': %s." + " The createInstance entry point will not be called for the global script.", + lua_tostring(my_instance->global_lua_state, -1)); } } } @@ -222,7 +237,7 @@ createInstance(char **options, FILTER_PARAMETER **params) { MXS_ERROR("Unable to initialize new Lua state."); free(my_instance); - return NULL; + my_instance = NULL; } } @@ -270,33 +285,36 @@ static void * newSession(FILTER *instance, SESSION *session) lua_tostring(my_session->lua_state, -1)); lua_close(my_session->lua_state); free(my_session); - return NULL; + my_session = NULL; } - - lua_pushcfunction(my_session->lua_state, id_gen); - lua_setglobal(my_session->lua_state, "id_gen"); - - lua_getglobal(my_session->lua_state, "newSession"); - if (lua_pcall(my_session->lua_state, 0, 0, 0)) + else { - MXS_ERROR("luafilter: Failed to get global variable 'newSession': '%s'.", - lua_tostring(my_session->lua_state, -1)); + lua_pushcfunction(my_session->lua_state, id_gen); + lua_setglobal(my_session->lua_state, "id_gen"); + + lua_getglobal(my_session->lua_state, "newSession"); + if (lua_pcall(my_session->lua_state, 0, 0, 0)) + { + MXS_WARNING("luafilter: Failed to get global variable 'newSession': '%s'." + " The newSession entry point will not be called.", + lua_tostring(my_session->lua_state, -1)); + } } } - if (my_instance->global_lua_state) + if (my_session && my_instance->global_lua_state) { spinlock_acquire(&my_instance->lock); lua_getglobal(my_instance->global_lua_state, "newSession"); if (lua_pcall(my_instance->global_lua_state, 0, 0, 0)) { - MXS_ERROR("luafilter: Failed to get global variable 'newSession': '%s'.", + MXS_WARNING("luafilter: Failed to get global variable 'newSession': '%s'." + " The newSession entry point will not be called for the global script.", lua_tostring(my_instance->global_lua_state, -1)); } spinlock_release(&my_instance->lock); } - return my_session; } @@ -320,11 +338,10 @@ static void closeSession(FILTER *instance, void *session) lua_getglobal(my_session->lua_state, "closeSession"); if (lua_pcall(my_session->lua_state, 0, 0, 0)) { - MXS_ERROR("luafilter: Failed to get global variable 'closeSession': '%s'.", + MXS_WARNING("luafilter: Failed to get global variable 'closeSession': '%s'." + " The closeSession entry point will not be called.", lua_tostring(my_session->lua_state, -1)); } - - lua_close(my_session->lua_state); spinlock_release(&my_session->lock); } @@ -334,8 +351,9 @@ static void closeSession(FILTER *instance, void *session) lua_getglobal(my_instance->global_lua_state, "closeSession"); if (lua_pcall(my_instance->global_lua_state, 0, 0, 0)) { - MXS_ERROR("luafilter: Failed to get global variable 'closeSession': '%s'.", - lua_tostring(my_instance->global_lua_state, -1)); + MXS_WARNING("luafilter: Failed to get global variable 'closeSession': '%s'." + " The closeSession entry point will not be called for the global script.", + lua_tostring(my_instance->global_lua_state, -1)); } spinlock_release(&my_instance->lock); } @@ -350,6 +368,7 @@ static void closeSession(FILTER *instance, void *session) static void freeSession(FILTER *instance, void *session) { LUA_SESSION *my_session = (LUA_SESSION *) session; + lua_close(my_session->lua_state); free(my_session); } @@ -367,6 +386,12 @@ static void setDownstream(FILTER *instance, void *session, DOWNSTREAM *downstrea my_session->down = *downstream; } +/** + * Set the filter upstream + * @param instance Filter instance + * @param session Filter session + * @param upstream Upstream filter + */ static void setUpstream(FILTER *instance, void *session, UPSTREAM *upstream) { LUA_SESSION *my_session = (LUA_SESSION *) session; @@ -386,37 +411,30 @@ static int clientReply(FILTER *instance, void *session, GWBUF *queue) { LUA_SESSION *my_session = (LUA_SESSION *) session; LUA_INSTANCE *my_instance = (LUA_INSTANCE *) instance; - char *fullquery; - if (modutil_is_SQL(queue) || modutil_is_SQL_prepare(queue)) + if (my_session->lua_state) { - fullquery = modutil_get_SQL(queue); - - if (my_session->lua_state) + spinlock_acquire(&my_session->lock); + lua_getglobal(my_session->lua_state, "clientReply"); + if (lua_pcall(my_session->lua_state, 0, 0, 0)) { - spinlock_acquire(&my_session->lock); - lua_getglobal(my_session->lua_state, "clientReply"); - lua_pushlstring(my_session->lua_state, fullquery, strlen(fullquery)); - if (lua_pcall(my_session->lua_state, 1, 0, 0)) - { - MXS_ERROR("luafilter: Session scope call to 'clientReply' failed: '%s'.", - lua_tostring(my_session->lua_state, -1)); - } - spinlock_release(&my_session->lock); - } - if (my_instance->global_lua_state) - { - spinlock_acquire(&my_instance->lock); - lua_getglobal(my_instance->global_lua_state, "clientReply"); - lua_pushlstring(my_instance->global_lua_state, fullquery, strlen(fullquery)); - if (lua_pcall(my_instance->global_lua_state, 1, 0, 0)) - { - MXS_ERROR("luafilter: Global scope call to 'clientReply' failed: '%s'.", - lua_tostring(my_session->lua_state, -1)); - } - spinlock_release(&my_instance->lock); + MXS_ERROR("luafilter: Session scope call to 'clientReply' failed: '%s'.", + lua_tostring(my_session->lua_state, -1)); } + spinlock_release(&my_session->lock); } + if (my_instance->global_lua_state) + { + spinlock_acquire(&my_instance->lock); + lua_getglobal(my_instance->global_lua_state, "clientReply"); + if (lua_pcall(my_instance->global_lua_state, 0, 0, 0)) + { + MXS_ERROR("luafilter: Global scope call to 'clientReply' failed: '%s'.", + lua_tostring(my_session->lua_state, -1)); + } + spinlock_release(&my_instance->lock); + } + return my_session->up.clientReply(my_session->up.instance, my_session->up.session, queue); } @@ -463,8 +481,7 @@ static int routeQuery(FILTER *instance, void *session, GWBUF *queue) MXS_ERROR("luafilter: Session scope call to 'routeQuery' failed: '%s'.", lua_tostring(my_session->lua_state, -1)); } - - if (lua_gettop(my_session->lua_state)) + else if (lua_gettop(my_session->lua_state)) { if (lua_isstring(my_session->lua_state, -1)) { @@ -492,7 +509,7 @@ static int routeQuery(FILTER *instance, void *session, GWBUF *queue) MXS_ERROR("luafilter: Global scope call to 'routeQuery' failed: '%s'.", lua_tostring(my_session->lua_state, -1)); } - if (lua_gettop(my_instance->global_lua_state)) + else if (lua_gettop(my_instance->global_lua_state)) { if (lua_isstring(my_instance->global_lua_state, -1)) {