JNIEXPORT jlong JNICALL Java_com_sun_management_UnixOperatingSystem_getCommittedVirtualMemorySize (JNIEnv *env, jobject mbean) { #ifdef __solaris__ psinfo_t psinfo; ssize_t result; size_t remaining; char* addr; int fd; fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0); if (fd < 0) { throw_internal_error(env, "Unable to open /proc/self/psinfo"); return -1; } addr = (char *)&psinfo; for (remaining = sizeof(psinfo_t); remaining > 0;) { result = JVM_Read(fd, addr, remaining); if (result < 0) { JVM_Close(fd); throw_internal_error(env, "Unable to read /proc/self/psinfo"); return -1; } remaining -= result; addr += result; } JVM_Close(fd); return (jlong) psinfo.pr_size * 1024; #else /* __linux__ */ FILE *fp; unsigned long vsize = 0; if ((fp = fopen("/proc/self/stat", "r")) == NULL) { throw_internal_error(env, "Unable to open /proc/self/stat"); return -1; } // Ignore everything except the vsize entry if (fscanf(fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %*u %*u %*d %lu %*[^\n]\n", &vsize) == EOF) { throw_internal_error(env, "Unable to get virtual memory usage"); fclose(fp); return -1; } fclose(fp); return (jlong)vsize; #endif }
JNIEXPORT jlong JNICALL Java_java_util_zip_ZipFile_open(JNIEnv *env, jclass cls, jstring name, jint mode, jlong lastModified, jboolean usemmap) { const char *path = JNU_GetStringPlatformChars(env, name, 0); char *msg = 0; jlong result = 0; int flag = 0; jzfile *zip = 0; if (mode & OPEN_READ) flag |= O_RDONLY; if (mode & OPEN_DELETE) flag |= JVM_O_DELETE; if (path != 0) { zip = ZIP_Get_From_Cache(path, &msg, lastModified); if (zip == 0 && msg == 0) { ZFILE zfd = 0; #ifdef WIN32 zfd = winFileHandleOpen(env, name, flag); if (zfd == -1) { /* Exception already pending. */ goto finally; } #else zfd = JVM_Open(path, flag, 0); if (zfd < 0) { throwFileNotFoundException(env, name); goto finally; } #endif zip = ZIP_Put_In_Cache0(path, zfd, &msg, lastModified, usemmap); } if (zip != 0) { result = ptr_to_jlong(zip); } else if (msg != 0) { ThrowZipException(env, msg); free(msg); } else if (errno == ENOMEM) { JNU_ThrowOutOfMemoryError(env, 0); } else { ThrowZipException(env, "error in opening zip file"); } finally: JNU_ReleaseStringPlatformChars(env, name, path); } return result; }
WITH_PLATFORM_STRING(env, path, ps) { FD fd; #ifdef __linux__ /* Remove trailing slashes, since the kernel won't */ char *p = (char *)ps + strlen(ps) - 1; while ((p > ps) && (*p == '/')) *p-- = '\0'; #endif fd = JVM_Open(ps, flags, 0666); if (fd >= 0) { SET_FD(this, fd, fid); } else { throwFileNotFoundException(env, path); } } END_PLATFORM_STRING(env, ps);
/* * Opens the named file for reading, returning a ZFILE. * * Compare this with winFileHandleOpen in windows/native/java/io/io_util_md.c. * This function does not take JNIEnv* and uses CreateFile (instead of * CreateFileW). The expectation is that this function will be called only * from ZIP_Open_Generic, which in turn is used by the JVM, where we do not * need to concern ourselves with wide chars. */ static ZFILE ZFILE_Open(const char *fname, int flags) { #ifdef WIN32 const DWORD access = (flags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) : (flags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ; const DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; const DWORD disposition = /* Note: O_TRUNC overrides O_CREAT */ (flags & O_TRUNC) ? CREATE_ALWAYS : (flags & O_CREAT) ? OPEN_ALWAYS : OPEN_EXISTING; const DWORD maybeWriteThrough = (flags & (O_SYNC | O_DSYNC)) ? FILE_FLAG_WRITE_THROUGH : FILE_ATTRIBUTE_NORMAL; const DWORD maybeDeleteOnClose = (flags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE : FILE_ATTRIBUTE_NORMAL; const DWORD flagsAndAttributes = maybeWriteThrough | maybeDeleteOnClose; return (jlong) CreateFile( fname, /* Wide char path name */ access, /* Read and/or write permission */ sharing, /* File sharing flags */ NULL, /* Security attributes */ disposition, /* creation disposition */ flagsAndAttributes, /* flags and attributes */ NULL); #else return JVM_Open(fname, flags, 0); #endif }
JNIEXPORT jlong JNICALL Java_com_sun_management_UnixOperatingSystem_getCommittedVirtualMemorySize (JNIEnv *env, jobject mbean) { #ifdef __solaris__ psinfo_t psinfo; ssize_t result; size_t remaining; char* addr; int fd; fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0); if (fd < 0) { throw_internal_error(env, "Unable to open /proc/self/psinfo"); return -1; } addr = (char *)&psinfo; for (remaining = sizeof(psinfo_t); remaining > 0;) { result = JVM_Read(fd, addr, remaining); if (result < 0) { JVM_Close(fd); throw_internal_error(env, "Unable to read /proc/self/psinfo"); return -1; } remaining -= result; addr += result; } JVM_Close(fd); return (jlong) psinfo.pr_size * 1024; #elif defined(__linux__) FILE *fp; unsigned long vsize = 0; if ((fp = fopen("/proc/self/stat", "r")) == NULL) { throw_internal_error(env, "Unable to open /proc/self/stat"); return -1; } // Ignore everything except the vsize entry if (fscanf(fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %*u %*u %*d %lu %*[^\n]\n", &vsize) == EOF) { throw_internal_error(env, "Unable to get virtual memory usage"); fclose(fp); return -1; } fclose(fp); return (jlong)vsize; #elif defined(__APPLE__) struct task_basic_info t_info; mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; kern_return_t res = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count); if (res != KERN_SUCCESS) { throw_internal_error(env, "task_info failed"); } return t_info.virtual_size; #else /* _ALLBSD_SOURCE */ /* * XXXBSD: there's no way available to do it in FreeBSD, AFAIK. */ // throw_internal_error(env, "Unimplemented in FreeBSD"); return (64 * MB); #endif }