All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [RFC] openposix: Use TMPDIR instead of hardcoded /tmp
@ 2020-12-15  6:07 Joerg Vehlow
  2020-12-15 10:18 ` Cyril Hrubis
  0 siblings, 1 reply; 2+ messages in thread
From: Joerg Vehlow @ 2020-12-15  6:07 UTC (permalink / raw)
  To: ltp

From: Joerg Vehlow <joerg.vehlow@aox-tech.de>

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(-)

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/mmap/5-1.c b/testcases/open_posix_testsuite/conformance/interfaces/mmap/5-1.c
index d9d476bd5..fb98cb877 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/mmap/5-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/mmap/5-1.c
@@ -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) {
diff --git a/testcases/open_posix_testsuite/include/posixtest.h b/testcases/open_posix_testsuite/include/posixtest.h
index cf0952cbf..e0f1f0214 100644
--- a/testcases/open_posix_testsuite/include/posixtest.h
+++ b/testcases/open_posix_testsuite/include/posixtest.h
@@ -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";
+}
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [LTP] [RFC] openposix: Use TMPDIR instead of hardcoded /tmp
  2020-12-15  6:07 [LTP] [RFC] openposix: Use TMPDIR instead of hardcoded /tmp Joerg Vehlow
@ 2020-12-15 10:18 ` Cyril Hrubis
  0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2020-12-15 10:18 UTC (permalink / raw)
  To: ltp

Hi!
> 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.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-12-15 10:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-15  6:07 [LTP] [RFC] openposix: Use TMPDIR instead of hardcoded /tmp Joerg Vehlow
2020-12-15 10:18 ` Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.