[Tarantool-patches] [PATCH 2/2] Allow to set directory for copying DSO before load

Kirill Yukhin kyukhin at tarantool.org
Wed May 20 12:51:18 MSK 2020


Make it possible to set temporary directory where
module will be copied before load.

@TarantoolBot document
Title: Justify module (re-)loading semantics

It is now possible to set directory where temporary
copies of modules to be loaded will be created.
It is done by setting $(TMPDIR) variable. It
will be "/tmp" if variable was not set.

Follow up #4945
---
 src/box/call.c                | 12 ++++++++----
 src/box/func.c                | 23 +++++++++++++++++++----
 test/box/func_reload.result   | 30 +++++++++++++++++++++++-------
 test/box/func_reload.test.lua | 13 ++++++++++---
 4 files changed, 60 insertions(+), 18 deletions(-)

diff --git a/src/box/call.c b/src/box/call.c
index 7c70d81..c16ac70 100644
--- a/src/box/call.c
+++ b/src/box/call.c
@@ -129,10 +129,14 @@ box_module_reload(const char *name)
 		return -1;
 	}
 	struct module *module = NULL;
-	if (module_reload(name, name + strlen(name), &module) == 0 &&
-	    module != NULL)
-		return 0;
-	diag_set(ClientError, ER_NO_SUCH_MODULE, name);
+	if (module_reload(name, name + strlen(name), &module) == 0) {
+		if (module != NULL)
+			return 0;
+		else {
+			diag_set(ClientError, ER_NO_SUCH_MODULE, name);
+			return -1;
+		}
+	}
 	return -1;
 }
 
diff --git a/src/box/func.c b/src/box/func.c
index a42a269..0405eb9 100644
--- a/src/box/func.c
+++ b/src/box/func.c
@@ -261,14 +261,29 @@ module_load(const char *package, const char *package_end)
 	module->package[package_len] = 0;
 	rlist_create(&module->funcs);
 	module->calls = 0;
-	char dir_name[] = "/tmp/tntXXXXXX";
+
+	const char *tmpdir = getenv("TMPDIR");
+	if (tmpdir == NULL)
+		tmpdir = "/tmp";
+	char dir_name[PATH_MAX + 1];
+	int rc = snprintf(dir_name, sizeof(dir_name), "%s/tntXXXXXX", tmpdir);
+	if (rc < 0 || (size_t) rc >= sizeof(dir_name)) {
+		diag_set(SystemError, "failed to generate path to tmp dir");
+		goto error;
+	}
+
 	if (mkdtemp(dir_name) == NULL) {
-		diag_set(SystemError, "failed to create unique dir name");
+		diag_set(SystemError, "failed to create unique dir name: %s",
+			 dir_name);
 		goto error;
 	}
 	char load_name[PATH_MAX + 1];
-	snprintf(load_name, sizeof(load_name), "%s/%.*s." TARANTOOL_LIBEXT,
-		 dir_name, package_len, package);
+	rc = snprintf(load_name, sizeof(load_name), "%s/%.*s." TARANTOOL_LIBEXT,
+		      dir_name, package_len, package);
+	if (rc < 0 || (size_t) rc >= sizeof(dir_name)) {
+		diag_set(SystemError, "failed to generate path to DSO");
+		goto error;
+	}
 
 	FILE *source, *target;
 	source = fopen(path, "r");
diff --git a/test/box/func_reload.result b/test/box/func_reload.result
index 1313fdf..fbc2ad7 100644
--- a/test/box/func_reload.result
+++ b/test/box/func_reload.result
@@ -233,20 +233,36 @@ c:call("reload.test_reload_fail")
 ---
 - [[2]]
 ...
-box.schema.func.drop("reload.test_reload")
+box.schema.func.reload()
 ---
+- error: 'bad argument #1 to ''?'' (string expected, got no value)'
 ...
-box.schema.func.drop("reload.test_reload_fail")
+box.schema.func.reload("non-existing")
 ---
+- error: Module 'non-existing' does not exist
 ...
-_ = fio.unlink(reload_path)
+-- Make sure that $TMPDIR env variable is used to generate temporary
+-- path for DSO copy
+os.setenv("TMPDIR", "/dev/null")
 ---
 ...
-box.schema.func.reload()
+_, err = pcall(box.schema.func.reload, "reload")
 ---
-- error: 'bad argument #1 to ''?'' (string expected, got no value)'
 ...
-box.schema.func.reload("non-existing")
+tostring(err):gsub(': [/%w]+$', '')
+---
+- failed to create unique dir name
+- 1
+...
+os.setenv("TMPDIR", nil)
+---
+...
+box.schema.func.drop("reload.test_reload")
+---
+...
+box.schema.func.drop("reload.test_reload_fail")
+---
+...
+_ = fio.unlink(reload_path)
 ---
-- error: Module 'non-existing' does not exist
 ...
diff --git a/test/box/func_reload.test.lua b/test/box/func_reload.test.lua
index 4c062fd..5b1b4e9 100644
--- a/test/box/func_reload.test.lua
+++ b/test/box/func_reload.test.lua
@@ -79,9 +79,16 @@ fio.copyfile(reload1_path, reload_path)
 c:call("reload.test_reload")
 c:call("reload.test_reload_fail")
 
+box.schema.func.reload()
+box.schema.func.reload("non-existing")
+
+-- Make sure that $TMPDIR env variable is used to generate temporary
+-- path for DSO copy
+os.setenv("TMPDIR", "/dev/null")
+_, err = pcall(box.schema.func.reload, "reload")
+tostring(err):gsub(': [/%w]+$', '')
+os.setenv("TMPDIR", nil)
+
 box.schema.func.drop("reload.test_reload")
 box.schema.func.drop("reload.test_reload_fail")
 _ = fio.unlink(reload_path)
-
-box.schema.func.reload()
-box.schema.func.reload("non-existing")
-- 
1.8.3.1



More information about the Tarantool-patches mailing list