Skip to content

Commit

Permalink
Enable customization of the system temp path on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
swesonga committed Oct 25, 2024
1 parent eed263c commit 9c32adb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/hotspot/os/linux/globals_linux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@
develop(bool, DelayThreadStartALot, false, \
"Artificially delay thread starts randomly for testing.") \
\
diagnostic(ccstr, SystemTempPath, NULL, \
"An alternative path to use for the system's temporary " \
"directory instead of hardcoded paths like /tmp ") \
\


// end of RUNTIME_OS_FLAGS
Expand Down
49 changes: 48 additions & 1 deletion src/hotspot/os/linux/os_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,54 @@ const char* os::dll_file_extension() { return ".so"; }

// This must be hard coded because it's the system's temporary
// directory not the java application's temp directory, ala java.io.tmpdir.
const char* os::get_temp_directory() { return "/tmp"; }
// Use half of PATH_MAX characters for the custom temporary path to allow
// paths of length (PATH_MAX / 2) to be appended to the custom temp path.
#define MAX_CACHED_TEMP_PATH_LENGTH (PATH_MAX / 2)

// This method allows for overriding the system's temporary directory via the -XX:SystemTempPath option
const char* os::get_temp_directory() {
static const char* cached_temp_path = NULL;
static const char* default_tmp_path = "/tmp";

if (cached_temp_path == NULL) {
bool use_default_tmp_path = true;

if (SystemTempPath != NULL) {
size_t new_tmp_path_length = strlen(SystemTempPath);

assert(new_tmp_path_length > 0, "SystemTempPath must have positive length");
assert(new_tmp_path_length < MAX_CACHED_TEMP_PATH_LENGTH, "SystemTempPath exceeds allowed length");

if (new_tmp_path_length > 0 && new_tmp_path_length < MAX_CACHED_TEMP_PATH_LENGTH) {
struct stat stat_buf;
bool path_exists = os::stat(SystemTempPath, &stat_buf) == 0;

if (path_exists) {
if (S_ISDIR(stat_buf.st_mode)) {
if (0 == access(SystemTempPath, R_OK | W_OK)) {
use_default_tmp_path = false;
} else {
warning("Cannot access the specified SystemTempPath");
}
} else {
warning("SystemTempPath must be a directory");
}
} else {
warning("Cannot find the specified SystemTempPath");
}
} else {
warning("Invalid SystemTempPath length");
}

if (use_default_tmp_path) {
warning("Ignoring SystemTempPath and using the default temp path");
}
}

cached_temp_path = use_default_tmp_path ? default_tmp_path : SystemTempPath;
}
return cached_temp_path;
}

// check if addr is inside libjvm.so
bool os::address_is_in_vm(address addr) {
Expand Down
6 changes: 5 additions & 1 deletion src/hotspot/os/posix/perfMemory_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,19 @@ static void save_memory_to_file(char* addr, size_t size) {
// return the user specific temporary directory name.
// the caller is expected to free the allocated memory.
//
#if defined(LINUX)
#define TMP_BUFFER_LEN (PATH_MAX / 2)
#else
#define TMP_BUFFER_LEN (4+22)
#endif
static char* get_user_tmp_dir(const char* user, int vmid, int nspid) {
char* tmpdir = (char *)os::get_temp_directory();
#if defined(LINUX)
// On linux, if containerized process, get dirname of
// /proc/{vmid}/root/tmp/{PERFDATA_NAME_user}
// otherwise /tmp/{PERFDATA_NAME_user}
char buffer[TMP_BUFFER_LEN];
assert(strlen(tmpdir) == 4, "No longer using /tmp - update buffer size");
assert((strlen(tmpdir) + 22) < TMP_BUFFER_LEN, "Insufficient buffer size for temp dir");

if (nspid != -1) {
jio_snprintf(buffer, TMP_BUFFER_LEN, "/proc/%d/root%s", vmid, tmpdir);
Expand Down

0 comments on commit 9c32adb

Please sign in to comment.