[PATCH] Prefer TMPDIR over hard coded /tmp

Robert Stupp snazy at snazy.de
Mon Apr 23 14:23:41 UTC 2018

For MacOS and Windows, Java prefers the user's temporary directory for 
java.io.tmpdir, but not for Linux, where it is always set to /tmp. The 
burden with this is that if you want to use a different temp directory, 
you have to explicitly pass -Djava.io.tmpdir=... on the command line, 
which can be difficult especially for unit tests or microbenchmarks in 
3rd party code ran via Maven for example.

java_props_t.tmp_dir is always initialized as P_tmpdir in 
GetJavaProperties (src/java.base/unix/native/libjava/java_props_md.c).

The attached patch changed the behavior to use the content of the 
environment variable TMPDIR, if present. Note that this does not change 
where the hsperf* folders are created.

I'm not sure why java.io.tmpdir is always /tmp in Linux as according to 
the SCM history, this was always as it is (references the initial 
OpenJDK commit).

I haven't found any tests that validates the content of java.io.tmpdir.


Robert Stupp

-------------- next part --------------
# HG changeset patch
# User Robert Stupp <snazy at snazy.de>
# Date 1524398509 -7200
#      Sun Apr 22 14:01:49 2018 +0200
# Branch use-TMPDIR-env-Linux
# Node ID 3f9f58a5d4049fcba8e5201e321bf71984430ce9
# Parent  fcd5df7aa235ca39852b04eb589e25f156870ce4
Use TMPDIR environment variable for java.io.tmpdir on Linux

diff --git a/src/java.base/unix/native/libjava/java_props_md.c b/src/java.base/unix/native/libjava/java_props_md.c
--- a/src/java.base/unix/native/libjava/java_props_md.c
+++ b/src/java.base/unix/native/libjava/java_props_md.c
@@ -358,7 +358,7 @@
         return &sprops;
-    /* tmp dir */
+    /* tmp dir, use the default from _PATH_VARTMP */
     sprops.tmp_dir = P_tmpdir;
 #ifdef MACOSX
     /* darwin has a per-user temp dir */
@@ -367,6 +367,14 @@
     if (pathSize > 0 && pathSize <= PATH_MAX) {
         sprops.tmp_dir = tmp_path;
+    /* Use the temporary directory injected via the environment variable TMPDIR
+     * instead of the "hard coded" _PATH_VARTMP
+     */
+    v = getenv("TMPDIR");
+    if (v) {
+        sprops.tmp_dir = strdup(v);
+    }
 #endif /* MACOSX */
     /* Printing properties */

More information about the core-libs-dev mailing list