comparison src/os/linux/vm/os_linux.cpp @ 2023:54f5dd2aa1d9

Merge
author zgu
date Sat, 11 Dec 2010 13:46:36 -0500
parents 2d4762ec74af 828eafbd85cc
children 03e1b9fce89d
comparison
equal deleted inserted replaced
2022:2d4762ec74af 2023:54f5dd2aa1d9
42 #include "prims/jvm.h" 42 #include "prims/jvm.h"
43 #include "prims/jvm_misc.hpp" 43 #include "prims/jvm_misc.hpp"
44 #include "runtime/arguments.hpp" 44 #include "runtime/arguments.hpp"
45 #include "runtime/extendedPC.hpp" 45 #include "runtime/extendedPC.hpp"
46 #include "runtime/globals.hpp" 46 #include "runtime/globals.hpp"
47 #include "runtime/hpi.hpp"
48 #include "runtime/interfaceSupport.hpp" 47 #include "runtime/interfaceSupport.hpp"
49 #include "runtime/java.hpp" 48 #include "runtime/java.hpp"
50 #include "runtime/javaCalls.hpp" 49 #include "runtime/javaCalls.hpp"
51 #include "runtime/mutexLocker.hpp" 50 #include "runtime/mutexLocker.hpp"
52 #include "runtime/objectMonitor.hpp" 51 #include "runtime/objectMonitor.hpp"
1564 } 1563 }
1565 1564
1566 // unused on linux for now. 1565 // unused on linux for now.
1567 void os::set_error_file(const char *logfile) {} 1566 void os::set_error_file(const char *logfile) {}
1568 1567
1568
1569 // This method is a copy of JDK's sysGetLastErrorString
1570 // from src/solaris/hpi/src/system_md.c
1571
1572 size_t os::lasterror(char *buf, size_t len) {
1573
1574 if (errno == 0) return 0;
1575
1576 const char *s = ::strerror(errno);
1577 size_t n = ::strlen(s);
1578 if (n >= len) {
1579 n = len - 1;
1580 }
1581 ::strncpy(buf, s, n);
1582 buf[n] = '\0';
1583 return n;
1584 }
1585
1569 intx os::current_thread_id() { return (intx)pthread_self(); } 1586 intx os::current_thread_id() { return (intx)pthread_self(); }
1570 int os::current_process_id() { 1587 int os::current_process_id() {
1571 1588
1572 // Under the old linux thread library, linux gives each thread 1589 // Under the old linux thread library, linux gives each thread
1573 // its own process id. Because of this each thread will return 1590 // its own process id. Because of this each thread will return
1937 pthread_mutex_unlock(&dl_mutex); 1954 pthread_mutex_unlock(&dl_mutex);
1938 return res; 1955 return res;
1939 } 1956 }
1940 1957
1941 1958
1942 bool _print_ascii_file(const char* filename, outputStream* st) { 1959 static bool _print_ascii_file(const char* filename, outputStream* st) {
1943 int fd = open(filename, O_RDONLY); 1960 int fd = ::open(filename, O_RDONLY);
1944 if (fd == -1) { 1961 if (fd == -1) {
1945 return false; 1962 return false;
1946 } 1963 }
1947 1964
1948 char buf[32]; 1965 char buf[32];
1949 int bytes; 1966 int bytes;
1950 while ((bytes = read(fd, buf, sizeof(buf))) > 0) { 1967 while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) {
1951 st->print_raw(buf, bytes); 1968 st->print_raw(buf, bytes);
1952 } 1969 }
1953 1970
1954 close(fd); 1971 ::close(fd);
1955 1972
1956 return true; 1973 return true;
1957 } 1974 }
1958 1975
1959 void os::print_dll_info(outputStream *st) { 1976 void os::print_dll_info(outputStream *st) {
2227 2244
2228 if (0 == access(buf, F_OK)) { 2245 if (0 == access(buf, F_OK)) {
2229 // Use current module name "libjvm[_g].so" instead of 2246 // Use current module name "libjvm[_g].so" instead of
2230 // "libjvm"debug_only("_g")".so" since for fastdebug version 2247 // "libjvm"debug_only("_g")".so" since for fastdebug version
2231 // we should have "libjvm.so" but debug_only("_g") adds "_g"! 2248 // we should have "libjvm.so" but debug_only("_g") adds "_g"!
2232 // It is used when we are choosing the HPI library's name
2233 // "libhpi[_g].so" in hpi::initialize_get_interface().
2234 len = strlen(buf); 2249 len = strlen(buf);
2235 snprintf(buf + len, buflen-len, "/hotspot/libjvm%s.so", p); 2250 snprintf(buf + len, buflen-len, "/hotspot/libjvm%s.so", p);
2236 } else { 2251 } else {
2237 // Go back to path of .so 2252 // Go back to path of .so
2238 rp = realpath(dli_fname, buf); 2253 rp = realpath(dli_fname, buf);
2412 2427
2413 snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d", 2428 snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d",
2414 os::get_temp_directory(), os::current_process_id(), num); 2429 os::get_temp_directory(), os::current_process_id(), num);
2415 unlink(buf); 2430 unlink(buf);
2416 2431
2417 int fd = open(buf, O_CREAT | O_RDWR, S_IRWXU); 2432 int fd = ::open(buf, O_CREAT | O_RDWR, S_IRWXU);
2418 2433
2419 if (fd != -1) { 2434 if (fd != -1) {
2420 off_t rv = lseek(fd, size-2, SEEK_SET); 2435 off_t rv = ::lseek(fd, size-2, SEEK_SET);
2421 if (rv != (off_t)-1) { 2436 if (rv != (off_t)-1) {
2422 if (write(fd, "", 1) == 1) { 2437 if (::write(fd, "", 1) == 1) {
2423 mmap(base, size, 2438 mmap(base, size,
2424 PROT_READ|PROT_WRITE|PROT_EXEC, 2439 PROT_READ|PROT_WRITE|PROT_EXEC,
2425 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0); 2440 MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0);
2426 } 2441 }
2427 } 2442 }
2428 close(fd); 2443 ::close(fd);
2429 unlink(buf); 2444 unlink(buf);
2430 } 2445 }
2431 } 2446 }
2432 2447
2433 // NOTE: Linux kernel does not really reserve the pages for us. 2448 // NOTE: Linux kernel does not really reserve the pages for us.
4055 } 4070 }
4056 4071
4057 // Initialize lock used to serialize thread creation (see os::create_thread) 4072 // Initialize lock used to serialize thread creation (see os::create_thread)
4058 Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false)); 4073 Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false));
4059 4074
4060 // Initialize HPI.
4061 jint hpi_result = hpi::initialize();
4062 if (hpi_result != JNI_OK) {
4063 tty->print_cr("There was an error trying to initialize the HPI library.");
4064 return hpi_result;
4065 }
4066
4067 // at-exit methods are called in the reverse order of their registration. 4075 // at-exit methods are called in the reverse order of their registration.
4068 // atexit functions are called on return from main or as a result of a 4076 // atexit functions are called on return from main or as a result of a
4069 // call to exit(3C). There can be only 32 of these functions registered 4077 // call to exit(3C). There can be only 32 of these functions registered
4070 // and atexit() does not set errno. 4078 // and atexit() does not set errno.
4071 4079
4259 char pathbuf[MAX_PATH]; 4267 char pathbuf[MAX_PATH];
4260 if (strlen(path) > MAX_PATH - 1) { 4268 if (strlen(path) > MAX_PATH - 1) {
4261 errno = ENAMETOOLONG; 4269 errno = ENAMETOOLONG;
4262 return -1; 4270 return -1;
4263 } 4271 }
4264 hpi::native_path(strcpy(pathbuf, path)); 4272 os::native_path(strcpy(pathbuf, path));
4265 return ::stat(pathbuf, sbuf); 4273 return ::stat(pathbuf, sbuf);
4266 } 4274 }
4267 4275
4268 bool os::check_heap(bool force) { 4276 bool os::check_heap(bool force) {
4269 return true; 4277 return true;
4291 } 4299 }
4292 closedir(dir); 4300 closedir(dir);
4293 return result; 4301 return result;
4294 } 4302 }
4295 4303
4304 // This code originates from JDK's sysOpen and open64_w
4305 // from src/solaris/hpi/src/system_md.c
4306
4307 #ifndef O_DELETE
4308 #define O_DELETE 0x10000
4309 #endif
4310
4311 // Open a file. Unlink the file immediately after open returns
4312 // if the specified oflag has the O_DELETE flag set.
4313 // O_DELETE is used only in j2se/src/share/native/java/util/zip/ZipFile.c
4314
4315 int os::open(const char *path, int oflag, int mode) {
4316
4317 if (strlen(path) > MAX_PATH - 1) {
4318 errno = ENAMETOOLONG;
4319 return -1;
4320 }
4321 int fd;
4322 int o_delete = (oflag & O_DELETE);
4323 oflag = oflag & ~O_DELETE;
4324
4325 fd = ::open64(path, oflag, mode);
4326 if (fd == -1) return -1;
4327
4328 //If the open succeeded, the file might still be a directory
4329 {
4330 struct stat64 buf64;
4331 int ret = ::fstat64(fd, &buf64);
4332 int st_mode = buf64.st_mode;
4333
4334 if (ret != -1) {
4335 if ((st_mode & S_IFMT) == S_IFDIR) {
4336 errno = EISDIR;
4337 ::close(fd);
4338 return -1;
4339 }
4340 } else {
4341 ::close(fd);
4342 return -1;
4343 }
4344 }
4345
4346 /*
4347 * All file descriptors that are opened in the JVM and not
4348 * specifically destined for a subprocess should have the
4349 * close-on-exec flag set. If we don't set it, then careless 3rd
4350 * party native code might fork and exec without closing all
4351 * appropriate file descriptors (e.g. as we do in closeDescriptors in
4352 * UNIXProcess.c), and this in turn might:
4353 *
4354 * - cause end-of-file to fail to be detected on some file
4355 * descriptors, resulting in mysterious hangs, or
4356 *
4357 * - might cause an fopen in the subprocess to fail on a system
4358 * suffering from bug 1085341.
4359 *
4360 * (Yes, the default setting of the close-on-exec flag is a Unix
4361 * design flaw)
4362 *
4363 * See:
4364 * 1085341: 32-bit stdio routines should support file descriptors >255
4365 * 4843136: (process) pipe file descriptor from Runtime.exec not being closed
4366 * 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 9
4367 */
4368 #ifdef FD_CLOEXEC
4369 {
4370 int flags = ::fcntl(fd, F_GETFD);
4371 if (flags != -1)
4372 ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4373 }
4374 #endif
4375
4376 if (o_delete != 0) {
4377 ::unlink(path);
4378 }
4379 return fd;
4380 }
4381
4382
4296 // create binary file, rewriting existing file if required 4383 // create binary file, rewriting existing file if required
4297 int os::create_binary_file(const char* path, bool rewrite_existing) { 4384 int os::create_binary_file(const char* path, bool rewrite_existing) {
4298 int oflags = O_WRONLY | O_CREAT; 4385 int oflags = O_WRONLY | O_CREAT;
4299 if (!rewrite_existing) { 4386 if (!rewrite_existing) {
4300 oflags |= O_EXCL; 4387 oflags |= O_EXCL;
4308 } 4395 }
4309 4396
4310 // move file pointer to the specified offset 4397 // move file pointer to the specified offset
4311 jlong os::seek_to_file_offset(int fd, jlong offset) { 4398 jlong os::seek_to_file_offset(int fd, jlong offset) {
4312 return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET); 4399 return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET);
4400 }
4401
4402 // This code originates from JDK's sysAvailable
4403 // from src/solaris/hpi/src/native_threads/src/sys_api_td.c
4404
4405 int os::available(int fd, jlong *bytes) {
4406 jlong cur, end;
4407 int mode;
4408 struct stat64 buf64;
4409
4410 if (::fstat64(fd, &buf64) >= 0) {
4411 mode = buf64.st_mode;
4412 if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
4413 /*
4414 * XXX: is the following call interruptible? If so, this might
4415 * need to go through the INTERRUPT_IO() wrapper as for other
4416 * blocking, interruptible calls in this file.
4417 */
4418 int n;
4419 if (::ioctl(fd, FIONREAD, &n) >= 0) {
4420 *bytes = n;
4421 return 1;
4422 }
4423 }
4424 }
4425 if ((cur = ::lseek64(fd, 0L, SEEK_CUR)) == -1) {
4426 return 0;
4427 } else if ((end = ::lseek64(fd, 0L, SEEK_END)) == -1) {
4428 return 0;
4429 } else if (::lseek64(fd, cur, SEEK_SET) == -1) {
4430 return 0;
4431 }
4432 *bytes = end - cur;
4433 return 1;
4313 } 4434 }
4314 4435
4315 // Map a block of memory. 4436 // Map a block of memory.
4316 char* os::map_memory(int fd, const char* file_name, size_t file_offset, 4437 char* os::map_memory(int fd, const char* file_name, size_t file_offset,
4317 char *addr, size_t bytes, bool read_only, 4438 char *addr, size_t bytes, bool read_only,
4536 } 4657 }
4537 4658
4538 int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); 4659 int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
4539 if (fd != -1) { 4660 if (fd != -1) {
4540 struct stat buf; 4661 struct stat buf;
4541 close(fd); 4662 ::close(fd);
4542 while (::stat(filename, &buf) == 0) { 4663 while (::stat(filename, &buf) == 0) {
4543 (void)::poll(NULL, 0, 100); 4664 (void)::poll(NULL, 0, 100);
4544 } 4665 }
4545 } else { 4666 } else {
4546 jio_fprintf(stderr, 4667 jio_fprintf(stderr,