47 #include <sys/mman.h> 48 #include <sys/resource.h> 49 #include <sys/utsname.h> 50 #include <time.h> 51 #include <unistd.h> 52 #include <utmpx.h> 53 54 // Todo: provide a os::get_max_process_id() or similar. Number of processes 55 // may have been configured, can be read more accurately from proc fs etc. 56 #ifndef MAX_PID 57 #define MAX_PID INT_MAX 58 #endif 59 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID) 60 61 #define ROOT_UID 0 62 63 #ifndef MAP_ANONYMOUS 64 #define MAP_ANONYMOUS MAP_ANON 65 #endif 66 67 #define check_with_errno(check_type, cond, msg) \ 68 do { \ 69 int err = errno; \ 70 check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err), \ 71 os::errno_name(err)); \ 72 } while (false) 73 74 #define assert_with_errno(cond, msg) check_with_errno(assert, cond, msg) 75 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg) 76 77 // Check core dump limit and report possible place where core can be found 78 void os::check_dump_limit(char* buffer, size_t bufferSize) { 79 if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) { 80 jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line"); 81 VMError::record_coredump_status(buffer, false); 82 return; 83 } 84 85 int n; 86 struct rlimit rlim; 159 160 size_t os::lasterror(char *buf, size_t len) { 161 if (errno == 0) return 0; 162 163 const char *s = os::strerror(errno); 164 size_t n = ::strlen(s); 165 if (n >= len) { 166 n = len - 1; 167 } 168 ::strncpy(buf, s, n); 169 buf[n] = '\0'; 170 return n; 171 } 172 173 void os::wait_for_keypress_at_exit(void) { 174 // don't do anything on posix platforms 175 return; 176 } 177 178 int os::create_file_for_heap(const char* dir) { 179 180 const char name_template[] = "/jvmheap.XXXXXX"; 181 182 size_t fullname_len = strlen(dir) + strlen(name_template); 183 char *fullname = (char*)os::malloc(fullname_len + 1, mtInternal); 184 if (fullname == NULL) { 185 vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno))); 186 return -1; 187 } 188 int n = snprintf(fullname, fullname_len + 1, "%s%s", dir, name_template); 189 assert((size_t)n == fullname_len, "Unexpected number of characters in string"); 190 191 os::native_path(fullname); 192 193 // set the file creation mask. 194 mode_t file_mode = S_IRUSR | S_IWUSR; 195 196 // create a new file. 197 int fd = mkstemp(fullname); 198 199 if (fd < 0) { 200 warning("Could not create file for heap with template %s", fullname); 201 os::free(fullname); 202 return -1; 203 } 204 205 // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted. 206 int ret = unlink(fullname); 207 assert_with_errno(ret == 0, "unlink returned error"); 208 209 os::free(fullname); 210 return fd; 211 } 212 213 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) { 214 char * addr; 215 int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS; 216 if (requested_addr != NULL) { 217 assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size"); 218 flags |= MAP_FIXED; 219 } 220 221 // Map reserved/uncommitted pages PROT_NONE so we fail early if we 222 // touch an uncommitted page. Otherwise, the read/write might 223 // succeed if we have enough swap space to back the physical page. 224 addr = (char*)::mmap(requested_addr, bytes, PROT_NONE, 225 flags, -1, 0); 226 227 if (addr != MAP_FAILED) { 228 MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC); 229 return addr; | 47 #include <sys/mman.h> 48 #include <sys/resource.h> 49 #include <sys/utsname.h> 50 #include <time.h> 51 #include <unistd.h> 52 #include <utmpx.h> 53 54 // Todo: provide a os::get_max_process_id() or similar. Number of processes 55 // may have been configured, can be read more accurately from proc fs etc. 56 #ifndef MAX_PID 57 #define MAX_PID INT_MAX 58 #endif 59 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID) 60 61 #define ROOT_UID 0 62 63 #ifndef MAP_ANONYMOUS 64 #define MAP_ANONYMOUS MAP_ANON 65 #endif 66 67 #ifndef O_TMPFILE 68 #ifdef __O_TMPFILE 69 #define O_TMPFILE __O_TMPFILE 70 #endif 71 #endif 72 73 #define check_with_errno(check_type, cond, msg) \ 74 do { \ 75 int err = errno; \ 76 check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err), \ 77 os::errno_name(err)); \ 78 } while (false) 79 80 #define assert_with_errno(cond, msg) check_with_errno(assert, cond, msg) 81 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg) 82 83 // Check core dump limit and report possible place where core can be found 84 void os::check_dump_limit(char* buffer, size_t bufferSize) { 85 if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) { 86 jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line"); 87 VMError::record_coredump_status(buffer, false); 88 return; 89 } 90 91 int n; 92 struct rlimit rlim; 165 166 size_t os::lasterror(char *buf, size_t len) { 167 if (errno == 0) return 0; 168 169 const char *s = os::strerror(errno); 170 size_t n = ::strlen(s); 171 if (n >= len) { 172 n = len - 1; 173 } 174 ::strncpy(buf, s, n); 175 buf[n] = '\0'; 176 return n; 177 } 178 179 void os::wait_for_keypress_at_exit(void) { 180 // don't do anything on posix platforms 181 return; 182 } 183 184 int os::create_file_for_heap(const char* dir) { 185 char* native_dir = os::strdup(dir); 186 if (native_dir == NULL) { 187 vm_exit_during_initialization(err_msg("strdup failed during creation of backing file for heap (%s)", os::strerror(errno))); 188 return -1; 189 } 190 os::native_path(native_dir); 191 int fd = os::open(dir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR); 192 os::free(native_dir); 193 194 if (fd == -1) { 195 const char name_template[] = "/jvmheap.XXXXXX"; 196 197 size_t fullname_len = strlen(dir) + strlen(name_template); 198 char *fullname = (char*)os::malloc(fullname_len + 1, mtInternal); 199 if (fullname == NULL) { 200 vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno))); 201 return -1; 202 } 203 int n = snprintf(fullname, fullname_len + 1, "%s%s", dir, name_template); 204 assert((size_t)n == fullname_len, "Unexpected number of characters in string"); 205 206 os::native_path(fullname); 207 208 // create a new file. 209 fd = mkstemp(fullname); 210 211 if (fd < 0) { 212 warning("Could not create file for heap with template %s", fullname); 213 os::free(fullname); 214 return -1; 215 } else { 216 // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted. 217 int ret = unlink(fullname); 218 assert_with_errno(ret == 0, "unlink returned error"); 219 } 220 221 os::free(fullname); 222 } 223 224 return fd; 225 } 226 227 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) { 228 char * addr; 229 int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS; 230 if (requested_addr != NULL) { 231 assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size"); 232 flags |= MAP_FIXED; 233 } 234 235 // Map reserved/uncommitted pages PROT_NONE so we fail early if we 236 // touch an uncommitted page. Otherwise, the read/write might 237 // succeed if we have enough swap space to back the physical page. 238 addr = (char*)::mmap(requested_addr, bytes, PROT_NONE, 239 flags, -1, 0); 240 241 if (addr != MAP_FAILED) { 242 MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC); 243 return addr; |