summaryrefslogtreecommitdiff
path: root/luaif
diff options
context:
space:
mode:
Diffstat (limited to 'luaif')
-rw-r--r--luaif/argvenvp.c11
-rw-r--r--luaif/luaif.c54
-rw-r--r--luaif/paths.c18
3 files changed, 78 insertions, 5 deletions
diff --git a/luaif/argvenvp.c b/luaif/argvenvp.c
index 28b079b..c4eda50 100644
--- a/luaif/argvenvp.c
+++ b/luaif/argvenvp.c
@@ -65,7 +65,10 @@ void sb_push_string_to_lua_stack(char *str)
{
struct lua_instance *luaif = get_lua();
- if (luaif) lua_pushstring(luaif->lua, str);
+ if (luaif) {
+ lua_pushstring(luaif->lua, str);
+ release_lua(luaif);
+ }
}
/* Exec preprocessor:
@@ -81,11 +84,13 @@ int sb_execve_preprocess(char **file, char ***argv, char ***envp)
if (!argv || !envp) {
SB_LOG(SB_LOGLEVEL_ERROR,
"ERROR: sb_argvenvp: (argv || envp) == NULL");
+ release_lua(luaif);
return -1;
}
if (getenv("SBOX_DISABLE_ARGVENVP")) {
SB_LOG(SB_LOGLEVEL_DEBUG, "sb_argvenvp disabled(E):");
+ release_lua(luaif);
return 0;
}
@@ -119,6 +124,7 @@ int sb_execve_preprocess(char **file, char ***argv, char ***envp)
SB_LOG(SB_LOGLEVEL_NOISE,
"sb_execve_preprocess: at exit, gettop=%d", lua_gettop(luaif->lua));
+ release_lua(luaif);
return res;
}
@@ -141,6 +147,7 @@ int sb_execve_postprocess(char *exec_type,
if (!argv || !envp) {
SB_LOG(SB_LOGLEVEL_ERROR,
"ERROR: sb_argvenvp: (argv || envp) == NULL");
+ release_lua(luaif);
return -1;
}
@@ -208,6 +215,7 @@ int sb_execve_postprocess(char *exec_type,
SB_LOG(SB_LOGLEVEL_NOISE,
"sb_execve_postprocess: at exit, gettop=%d", lua_gettop(luaif->lua));
+ release_lua(luaif);
return res;
}
@@ -260,5 +268,6 @@ char *sb_query_exec_policy(const char *field_name, const char *binary_name,
/* normalize lua stack */
lua_pop(luaif->lua, 2);
+ release_lua(luaif);
return (result);
}
diff --git a/luaif/luaif.c b/luaif/luaif.c
index 8841dfa..bfb849e 100644
--- a/luaif/luaif.c
+++ b/luaif/luaif.c
@@ -272,6 +272,48 @@ static struct lua_instance *alloc_lua(void)
return(tmp);
}
+static void increment_luaif_usage_counter(volatile struct lua_instance *ptr)
+{
+ if (SB_LOG_IS_ACTIVE(SB_LOGLEVEL_DEBUG)) {
+ /* Well, to make this bullet-proof the luaif structure
+ * should be locked, but since this code is now used only for
+ * producing debugging information and the pointer is marked
+ * "volatile", the results are good enough. No need to slow
+ * down anything with additional locks - this function is
+ * called frequently. */
+ if (ptr->lua_instance_in_use > 0) SB_LOG(SB_LOGLEVEL_DEBUG,
+ "Lua instance already in use! (%d)",
+ ptr->lua_instance_in_use);
+
+ (ptr->lua_instance_in_use)++;
+ }
+}
+
+void release_lua(struct lua_instance *luaif)
+{
+ if (SB_LOG_IS_ACTIVE(SB_LOGLEVEL_DEBUG)) {
+ int i;
+ volatile struct lua_instance *ptr = luaif;
+
+ SB_LOG(SB_LOGLEVEL_NOISE, "release_lua()");
+
+ if (!ptr) {
+ SB_LOG(SB_LOGLEVEL_DEBUG,
+ "release_lua(): ptr is NULL ");
+ return;
+ }
+
+ i = ptr->lua_instance_in_use;
+ if (i > 1) SB_LOG(SB_LOGLEVEL_DEBUG,
+ "Lua instance usage counter was %d", i);
+
+ (ptr->lua_instance_in_use)--;
+ }
+}
+
+/* get access to lua context. Remember to call release_lua() after the
+ * pointer is not needed anymore.
+*/
struct lua_instance *get_lua(void)
{
struct lua_instance *ptr = NULL;
@@ -280,6 +322,8 @@ struct lua_instance *get_lua(void)
if (!SB_LOG_INITIALIZED()) sblog_init();
+ SB_LOG(SB_LOGLEVEL_NOISE, "get_lua()");
+
if (pthread_detection_done == 0) check_pthread_library();
if (pthread_library_is_available) {
@@ -312,6 +356,10 @@ struct lua_instance *get_lua(void)
exit(1);
}
}
+
+ if (SB_LOG_IS_ACTIVE(SB_LOGLEVEL_DEBUG)) {
+ increment_luaif_usage_counter(ptr);
+ }
return(ptr);
}
@@ -336,9 +384,12 @@ void sb2_preload_library_constructor(void)
char *sb2__read_string_variable_from_lua__(const char *name)
{
struct lua_instance *luaif;
+ char *cp;
luaif = get_lua();
- return(read_string_variable_from_lua(luaif, name));
+ cp = read_string_variable_from_lua(luaif, name);
+ release_lua(luaif);
+ return(cp);
}
/* Read and execute an lua file. Used from sb2-show, useful
@@ -352,6 +403,7 @@ void sb2__load_and_execute_lua_file__(const char *filename)
luaif = get_lua();
load_and_execute_lua_file(luaif, filename);
+ release_lua(luaif);
}
#if 0 /* DISABLED 2008-10-23/LTA: sb_decolonize_path() is not currently available*/
diff --git a/luaif/paths.c b/luaif/paths.c
index 08e8cf8..dfaf5bb 100644
--- a/luaif/paths.c
+++ b/luaif/paths.c
@@ -935,7 +935,7 @@ static char *scratchbox_path_internal(
int dont_resolve_final_symlink,
int process_path_for_exec)
{
- struct lua_instance *luaif;
+ struct lua_instance *luaif = NULL;
char *mapping_result;
SB_LOG(SB_LOGLEVEL_DEBUG, "scratchbox_path_internal: %s(%s)", func_name, path);
@@ -966,6 +966,7 @@ static char *scratchbox_path_internal(
SB_LOG(SB_LOGLEVEL_ERROR,
"ERROR: scratchbox_path_internal: path==NULL [%s]",
func_name);
+ release_lua(luaif);
return NULL;
}
@@ -976,6 +977,7 @@ static char *scratchbox_path_internal(
*/
SB_LOG(SB_LOGLEVEL_INFO, "disabled(E): %s '%s'",
func_name, path);
+ release_lua(luaif);
return strdup(path);
}
if (luaif->mapping_disabled) {
@@ -985,6 +987,7 @@ static char *scratchbox_path_internal(
*/
SB_LOG(SB_LOGLEVEL_INFO, "disabled(%d): %s '%s'",
luaif->mapping_disabled, func_name, path);
+ release_lua(luaif);
return strdup(path);
}
@@ -994,6 +997,11 @@ static char *scratchbox_path_internal(
char *decolon_path = NULL;
char *full_path_for_rule_selection = NULL;
+
+ /* FIXME: following call to sb_decolonize_path() will lead to
+ * another call to get_lua()...it would be more efficient to just
+ * pass luaif pointer from here.
+ */
full_path_for_rule_selection = sb_decolonize_path(func_name, path);
if (*full_path_for_rule_selection != '/') {
@@ -1107,6 +1115,7 @@ static char *scratchbox_path_internal(
SB_LOG(SB_LOGLEVEL_NOISE, "scratchbox_path_internal: mapping_result='%s'",
mapping_result ? mapping_result : "<No result>");
+ release_lua(luaif);
return(mapping_result);
}
@@ -1207,9 +1216,12 @@ char *scratchbox_reverse_path(
const char *full_path)
{
struct lua_instance *luaif = get_lua();
+ char *result;
- return (call_lua_function_sbox_reverse_path(luaif,
+ result = call_lua_function_sbox_reverse_path(luaif,
(sbox_binary_name ? sbox_binary_name : "UNKNOWN"),
- func_name, full_path));
+ func_name, full_path);
+ release_lua(luaif);
+ return(result);
}