From 6d21d423a4d38e257fb623aeb9e830e5752c9b6b Mon Sep 17 00:00:00 2001 From: coleenp Date: Wed, 31 Mar 2010 16:51:18 -0700 Subject: [PATCH] 6938627: Make temporary directory use property java.io.tmpdir when specified Summary: Get java.io.tmpdir property in os::get_temp_directory() and call this instead of harcoding "/tmp". Don't assume trailing file_separator either. Reviewed-by: dholmes, kamg --- src/os/linux/vm/attachListener_linux.cpp | 8 ++-- src/os/linux/vm/os_linux.cpp | 8 +++- src/os/linux/vm/perfMemory_linux.cpp | 7 ++-- src/os/solaris/vm/attachListener_solaris.cpp | 8 ++-- src/os/solaris/vm/os_solaris.cpp | 14 ++----- src/os/solaris/vm/perfMemory_solaris.cpp | 7 ++-- src/os/windows/vm/os_windows.cpp | 19 ++++----- src/os/windows/vm/perfMemory_windows.cpp | 7 ++-- src/share/vm/compiler/compileBroker.cpp | 11 ++++-- src/share/vm/utilities/ostream.cpp | 41 +++++++++++++++----- src/share/vm/utilities/vmError.cpp | 4 +- 11 files changed, 83 insertions(+), 51 deletions(-) diff --git a/src/os/linux/vm/attachListener_linux.cpp b/src/os/linux/vm/attachListener_linux.cpp index f40ac8e84..93a856786 100644 --- a/src/os/linux/vm/attachListener_linux.cpp +++ b/src/os/linux/vm/attachListener_linux.cpp @@ -192,7 +192,8 @@ int LinuxAttachListener::init() { res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr)); } if (res == -1) { - sprintf(path, "%s/.java_pid%d", os::get_temp_directory(), os::current_process_id()); + snprintf(path, PATH_MAX+1, "%s/.java_pid%d", + os::get_temp_directory(), os::current_process_id()); strcpy(addr.sun_path, path); ::unlink(path); res = ::bind(listener, (struct sockaddr*)&addr, sizeof(addr)); @@ -460,13 +461,14 @@ bool AttachListener::is_init_trigger() { if (init_at_startup() || is_initialized()) { return false; // initialized at startup or already initialized } - char fn[32]; + char fn[128]; sprintf(fn, ".attach_pid%d", os::current_process_id()); int ret; struct stat64 st; RESTARTABLE(::stat64(fn, &st), ret); if (ret == -1) { - sprintf(fn, "/tmp/.attach_pid%d", os::current_process_id()); + snprintf(fn, sizeof(fn), "%s/.attach_pid%d", + os::get_temp_directory(), os::current_process_id()); RESTARTABLE(::stat64(fn, &st), ret); } if (ret == 0) { diff --git a/src/os/linux/vm/os_linux.cpp b/src/os/linux/vm/os_linux.cpp index dec664574..d38f3866c 100644 --- a/src/os/linux/vm/os_linux.cpp +++ b/src/os/linux/vm/os_linux.cpp @@ -1522,7 +1522,10 @@ int os::current_process_id() { const char* os::dll_file_extension() { return ".so"; } -const char* os::get_temp_directory() { return "/tmp/"; } +const char* os::get_temp_directory() { + const char *prop = Arguments::get_property("java.io.tmpdir"); + return prop == NULL ? "/tmp" : prop; +} static bool file_exists(const char* filename) { struct stat statbuf; @@ -2305,7 +2308,8 @@ void linux_wrap_code(char* base, size_t size) { char buf[40]; int num = Atomic::add(1, &cnt); - sprintf(buf, "/tmp/hs-vm-%d-%d", os::current_process_id(), num); + snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d", + os::get_temp_directory(), os::current_process_id(), num); unlink(buf); int fd = open(buf, O_CREAT | O_RDWR, S_IRWXU); diff --git a/src/os/linux/vm/perfMemory_linux.cpp b/src/os/linux/vm/perfMemory_linux.cpp index 381650562..31d275c26 100644 --- a/src/os/linux/vm/perfMemory_linux.cpp +++ b/src/os/linux/vm/perfMemory_linux.cpp @@ -145,11 +145,11 @@ static char* get_user_tmp_dir(const char* user) { const char* tmpdir = os::get_temp_directory(); const char* perfdir = PERFDATA_NAME; - size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 2; + size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3; char* dirname = NEW_C_HEAP_ARRAY(char, nbytes); // construct the path name to user specific tmp directory - snprintf(dirname, nbytes, "%s%s_%s", tmpdir, perfdir, user); + snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user); return dirname; } @@ -331,8 +331,9 @@ static char* get_user_name_slow(int vmid, TRAPS) { } char* usrdir_name = NEW_C_HEAP_ARRAY(char, - strlen(tmpdirname) + strlen(dentry->d_name) + 1); + strlen(tmpdirname) + strlen(dentry->d_name) + 2); strcpy(usrdir_name, tmpdirname); + strcat(usrdir_name, "/"); strcat(usrdir_name, dentry->d_name); DIR* subdirp = os::opendir(usrdir_name); diff --git a/src/os/solaris/vm/attachListener_solaris.cpp b/src/os/solaris/vm/attachListener_solaris.cpp index 61c31e27f..c3f73da35 100644 --- a/src/os/solaris/vm/attachListener_solaris.cpp +++ b/src/os/solaris/vm/attachListener_solaris.cpp @@ -375,7 +375,8 @@ int SolarisAttachListener::create_door() { return -1; } - sprintf(door_path, "%s/.java_pid%d", os::get_temp_directory(), os::current_process_id()); + snprintf(door_path, sizeof(door_path), "%s/.java_pid%d", + os::get_temp_directory(), os::current_process_id()); RESTARTABLE(::creat(door_path, S_IRUSR | S_IWUSR), fd); if (fd == -1) { @@ -591,13 +592,14 @@ bool AttachListener::is_init_trigger() { if (init_at_startup() || is_initialized()) { return false; // initialized at startup or already initialized } - char fn[32]; + char fn[128]; sprintf(fn, ".attach_pid%d", os::current_process_id()); int ret; struct stat64 st; RESTARTABLE(::stat64(fn, &st), ret); if (ret == -1) { - sprintf(fn, "/tmp/.attach_pid%d", os::current_process_id()); + snprintf(fn, sizeof(fn), "%s/.attach_pid%d", + os::get_temp_directory(), os::current_process_id()); RESTARTABLE(::stat64(fn, &st), ret); } if (ret == 0) { diff --git a/src/os/solaris/vm/os_solaris.cpp b/src/os/solaris/vm/os_solaris.cpp index db56befcb..a13723485 100644 --- a/src/os/solaris/vm/os_solaris.cpp +++ b/src/os/solaris/vm/os_solaris.cpp @@ -676,15 +676,6 @@ bool os::have_special_privileges() { } -static char* get_property(char* name, char* buffer, int buffer_size) { - if (os::getenv(name, buffer, buffer_size)) { - return buffer; - } - static char empty[] = ""; - return empty; -} - - void os::init_system_properties_values() { char arch[12]; sysinfo(SI_ARCHITECTURE, arch, sizeof(arch)); @@ -1826,7 +1817,10 @@ void os::set_error_file(const char *logfile) {} const char* os::dll_file_extension() { return ".so"; } -const char* os::get_temp_directory() { return "/tmp/"; } +const char* os::get_temp_directory() { + const char *prop = Arguments::get_property("java.io.tmpdir"); + return prop == NULL ? "/tmp" : prop; +} static bool file_exists(const char* filename) { struct stat statbuf; diff --git a/src/os/solaris/vm/perfMemory_solaris.cpp b/src/os/solaris/vm/perfMemory_solaris.cpp index 459ef573a..ee565d03a 100644 --- a/src/os/solaris/vm/perfMemory_solaris.cpp +++ b/src/os/solaris/vm/perfMemory_solaris.cpp @@ -147,11 +147,11 @@ static char* get_user_tmp_dir(const char* user) { const char* tmpdir = os::get_temp_directory(); const char* perfdir = PERFDATA_NAME; - size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 2; + size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3; char* dirname = NEW_C_HEAP_ARRAY(char, nbytes); // construct the path name to user specific tmp directory - snprintf(dirname, nbytes, "%s%s_%s", tmpdir, perfdir, user); + snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user); return dirname; } @@ -322,8 +322,9 @@ static char* get_user_name_slow(int vmid, TRAPS) { } char* usrdir_name = NEW_C_HEAP_ARRAY(char, - strlen(tmpdirname) + strlen(dentry->d_name) + 1); + strlen(tmpdirname) + strlen(dentry->d_name) + 2); strcpy(usrdir_name, tmpdirname); + strcat(usrdir_name, "/"); strcat(usrdir_name, dentry->d_name); DIR* subdirp = os::opendir(usrdir_name); diff --git a/src/os/windows/vm/os_windows.cpp b/src/os/windows/vm/os_windows.cpp index 217d2c38f..3763e504e 100644 --- a/src/os/windows/vm/os_windows.cpp +++ b/src/os/windows/vm/os_windows.cpp @@ -998,15 +998,16 @@ os::closedir(DIR *dirp) const char* os::dll_file_extension() { return ".dll"; } -const char * os::get_temp_directory() -{ - static char path_buf[MAX_PATH]; - if (GetTempPath(MAX_PATH, path_buf)>0) - return path_buf; - else{ - path_buf[0]='\0'; - return path_buf; - } +const char* os::get_temp_directory() { + const char *prop = Arguments::get_property("java.io.tmpdir"); + if (prop != 0) return prop; + static char path_buf[MAX_PATH]; + if (GetTempPath(MAX_PATH, path_buf)>0) + return path_buf; + else{ + path_buf[0]='\0'; + return path_buf; + } } static bool file_exists(const char* filename) { diff --git a/src/os/windows/vm/perfMemory_windows.cpp b/src/os/windows/vm/perfMemory_windows.cpp index 95063df2e..f4b41d7e7 100644 --- a/src/os/windows/vm/perfMemory_windows.cpp +++ b/src/os/windows/vm/perfMemory_windows.cpp @@ -149,11 +149,11 @@ static char* get_user_tmp_dir(const char* user) { const char* tmpdir = os::get_temp_directory(); const char* perfdir = PERFDATA_NAME; - size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 2; + size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3; char* dirname = NEW_C_HEAP_ARRAY(char, nbytes); // construct the path name to user specific tmp directory - _snprintf(dirname, nbytes, "%s%s_%s", tmpdir, perfdir, user); + _snprintf(dirname, nbytes, "%s\\%s_%s", tmpdir, perfdir, user); return dirname; } @@ -318,8 +318,9 @@ static char* get_user_name_slow(int vmid) { } char* usrdir_name = NEW_C_HEAP_ARRAY(char, - strlen(tmpdirname) + strlen(dentry->d_name) + 1); + strlen(tmpdirname) + strlen(dentry->d_name) + 2); strcpy(usrdir_name, tmpdirname); + strcat(usrdir_name, "\\"); strcat(usrdir_name, dentry->d_name); DIR* subdirp = os::opendir(usrdir_name); diff --git a/src/share/vm/compiler/compileBroker.cpp b/src/share/vm/compiler/compileBroker.cpp index 6e519ec14..f3a0514d1 100644 --- a/src/share/vm/compiler/compileBroker.cpp +++ b/src/share/vm/compiler/compileBroker.cpp @@ -1414,9 +1414,14 @@ void CompileBroker::init_compiler_thread_log() { intx thread_id = os::current_thread_id(); for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) { const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL); - if (dir == NULL) dir = ""; - sprintf(fileBuf, "%shs_c" UINTX_FORMAT "_pid%u.log", - dir, thread_id, os::current_process_id()); + if (dir == NULL) { + jio_snprintf(fileBuf, sizeof(fileBuf), "hs_c" UINTX_FORMAT "_pid%u.log", + thread_id, os::current_process_id()); + } else { + jio_snprintf(fileBuf, sizeof(fileBuf), + "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir, + os::file_separator(), thread_id, os::current_process_id()); + } fp = fopen(fileBuf, "at"); if (fp != NULL) { file = NEW_C_HEAP_ARRAY(char, strlen(fileBuf)+1); diff --git a/src/share/vm/utilities/ostream.cpp b/src/share/vm/utilities/ostream.cpp index 60bc65e4e..16f37d59a 100644 --- a/src/share/vm/utilities/ostream.cpp +++ b/src/share/vm/utilities/ostream.cpp @@ -363,7 +363,7 @@ bool defaultStream::has_log_file() { return _log_file != NULL; } -static const char* make_log_name(const char* log_name, const char* force_directory, char* buf) { +static const char* make_log_name(const char* log_name, const char* force_directory) { const char* basename = log_name; char file_sep = os::file_separator()[0]; const char* cp; @@ -374,6 +374,27 @@ static const char* make_log_name(const char* log_name, const char* force_directo } const char* nametail = log_name; + // Compute buffer length + size_t buffer_length; + if (force_directory != NULL) { + buffer_length = strlen(force_directory) + strlen(os::file_separator()) + + strlen(basename) + 1; + } else { + buffer_length = strlen(log_name) + 1; + } + + const char* star = strchr(basename, '*'); + int star_pos = (star == NULL) ? -1 : (star - nametail); + + char pid[32]; + if (star_pos >= 0) { + jio_snprintf(pid, sizeof(pid), "%u", os::current_process_id()); + buffer_length += strlen(pid); + } + + // Create big enough buffer. + char *buf = NEW_C_HEAP_ARRAY(char, buffer_length); + strcpy(buf, ""); if (force_directory != NULL) { strcat(buf, force_directory); @@ -381,14 +402,11 @@ static const char* make_log_name(const char* log_name, const char* force_directo nametail = basename; // completely skip directory prefix } - const char* star = strchr(basename, '*'); - int star_pos = (star == NULL) ? -1 : (star - nametail); - if (star_pos >= 0) { // convert foo*bar.log to foo123bar.log int buf_pos = (int) strlen(buf); strncpy(&buf[buf_pos], nametail, star_pos); - sprintf(&buf[buf_pos + star_pos], "%u", os::current_process_id()); + strcpy(&buf[buf_pos + star_pos], pid); nametail += star_pos + 1; // skip prefix and star } @@ -399,20 +417,23 @@ static const char* make_log_name(const char* log_name, const char* force_directo void defaultStream::init_log() { // %%% Need a MutexLocker? const char* log_name = LogFile != NULL ? LogFile : "hotspot.log"; - char buf[O_BUFLEN*2]; - const char* try_name = make_log_name(log_name, NULL, buf); + const char* try_name = make_log_name(log_name, NULL); fileStream* file = new(ResourceObj::C_HEAP) fileStream(try_name); if (!file->is_open()) { // Try again to open the file. char warnbuf[O_BUFLEN*2]; - sprintf(warnbuf, "Warning: Cannot open log file: %s\n", try_name); + jio_snprintf(warnbuf, sizeof(warnbuf), + "Warning: Cannot open log file: %s\n", try_name); // Note: This feature is for maintainer use only. No need for L10N. jio_print(warnbuf); - try_name = make_log_name("hs_pid*.log", os::get_temp_directory(), buf); - sprintf(warnbuf, "Warning: Forcing option -XX:LogFile=%s\n", try_name); + FREE_C_HEAP_ARRAY(char, try_name); + try_name = make_log_name("hs_pid*.log", os::get_temp_directory()); + jio_snprintf(warnbuf, sizeof(warnbuf), + "Warning: Forcing option -XX:LogFile=%s\n", try_name); jio_print(warnbuf); delete file; file = new(ResourceObj::C_HEAP) fileStream(try_name); + FREE_C_HEAP_ARRAY(char, try_name); } if (file->is_open()) { _log_file = file; diff --git a/src/share/vm/utilities/vmError.cpp b/src/share/vm/utilities/vmError.cpp index b758d8e9f..81ab68302 100644 --- a/src/share/vm/utilities/vmError.cpp +++ b/src/share/vm/utilities/vmError.cpp @@ -807,8 +807,8 @@ void VMError::report_and_die() { if (fd == -1) { // try temp directory const char * tmpdir = os::get_temp_directory(); - jio_snprintf(buffer, sizeof(buffer), "%shs_err_pid%u.log", - (tmpdir ? tmpdir : ""), os::current_process_id()); + jio_snprintf(buffer, sizeof(buffer), "%s%shs_err_pid%u.log", + tmpdir, os::file_separator(), os::current_process_id()); fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, 0666); } -- GitLab