Skip to contentSkip to conversation

openposix: Use TMPDIR instead of hardcoded /tmp

RFC In review Joerg Vehlow 2 participants 2 messages Dec 15, 2020

lore
Share & tools
lore
#0 lore raw

RFC 1/1 openposix: Use TMPDIR instead of hardcoded /tmp

From: Joerg Vehlow <[email protected]>

Hi everyone,

There are lots of code snippets like

snprintf(fname, sizeof(fname), "/tmp/<some_name>_%d", getpid()); unlink(fname);
open(fname, ...);

throughout the open posix tests.
I'd like to get rid of the hardcoded /tmp paths and replace them with TMPDIR, if it is set. Because the pattern is mostly the same, I'd like to generalize the implementation. Currently there is no library linked to the open_posix tests, that's why I choose a static inline function implemented in posixtests.h.
This could also be enhanced to use mkstemp and return the filehandle directly, because most of the time the file is opend anyway.

What is your opionion? Does a change like this make sense or would you do it differently?

Thanks,
J?rg

---
 .../conformance/interfaces/mmap/5-1.c               |  3 ++-
 testcases/open_posix_testsuite/include/posixtest.h  | 13 +++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
testcases/open_posix_testsuite/conformance/interfaces/mmap/5-1.c +2 -1
index d9d476bd5..fb98cb877 100644
@@ -93,7 +93,8 @@ int main(void)
 	int fd, fail = 0;
 	unsigned int i;
 
-	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_5_1_%d", getpid());
+	GET_TMP_FILENAME(tmpfname, "pts_mmap_5_1");
+
 	unlink(tmpfname);
 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
 	if (fd == -1) {
testcases/open_posix_testsuite/include/posixtest.h +13 -0
index cf0952cbf..e0f1f0214 100644
@@ -9,6 +9,9 @@
 /*
  * return codes
  */
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
 
 #define PTS_PASS        0
 #define PTS_FAIL        1
@@ -23,3 +26,13 @@
 #define LTP_ATTRIBUTE_NORETURN		__attribute__((noreturn))
 #define LTP_ATTRIBUTE_UNUSED		__attribute__((unused))
 #define LTP_ATTRIBUTE_UNUSED_RESULT	__attribute__((warn_unused_result))
+
+#define GET_TMP_FILENAME(target, prefix) \
+    snprintf(target, sizeof(target), "%s/" prefix "%d",  tmpdir(), getpid());
+
+static inline const char *tmpdir(void)
+{
+    const char *tmpdir_env;
+    tmpdir_env = getenv("TMPDIR");
+    return tmpdir_env ? tmpdir_env : "/tmp";
+}
Signature · 1 line
2.25.1
#1 lore raw

Hi!

Quoted 2 lines

What is your opionion? Does a change like this make sense or would you do it differently?

Sounds reasonable.

I guess that we should also bump the buffer sizes a bit since tests seems to use 256 bytes, which is not that hard to overflow. I would go for at least 4096.

Or we may as well give up on passing a stack allocated buffer and use malloc() for the path instead.

As for mkdtemp() if we added that we would have to add a function to remove the directory at the end, so I wouldn't do that unless we want to actually write a test library for the openposix tests.

And lastly but not least we do have testcases there that open files in current directory, these should be ideally converted to create the files in TMPDIR as well.

Signature · 2 lines
Cyril Hrubis
[email protected]