JNIEXPORT jint JNICALL Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz, jobject fdo, jlong address, jint len) { DWORD read = 0; BOOL result = 0; HANDLE h = (HANDLE)(handleval(env, fdo)); if (h == INVALID_HANDLE_VALUE) { JNU_ThrowIOExceptionWithLastError(env, "Invalid handle"); return IOS_THROWN; } result = ReadFile(h, /* File handle to read */ (LPVOID)address, /* address to put data */ len, /* number of bytes to read */ &read, /* number of bytes read */ NULL); /* no overlapped struct */ if (result == 0) { int error = GetLastError(); if (error == ERROR_BROKEN_PIPE) { return IOS_EOF; } if (error == ERROR_NO_DATA) { return IOS_UNAVAILABLE; } JNU_ThrowIOExceptionWithLastError(env, "Read failed"); return IOS_THROWN; } return convertReturnVal(env, (jint)read, JNI_TRUE); }
JNIEXPORT void JNICALL Java_sun_net_spi_SdpProvider_convert(JNIEnv *env, jclass cls, jint fd) { #ifdef PROTO_SDP int domain = ipv6_available() ? AF_INET6 : AF_INET; int s = socket(domain, SOCK_STREAM, PROTO_SDP); if (s < 0) { JNU_ThrowIOExceptionWithLastError(env, "socket"); } else { int arg, len, res; struct linger linger; /* copy socket options that are relevant to SDP */ len = sizeof(arg); if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, &len) == 0) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, len); len = sizeof(arg); if (getsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, &len) == 0) setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, len); len = sizeof(linger); if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (void*)&linger, &len) == 0) setsockopt(s, SOL_SOCKET, SO_LINGER, (char*)&linger, len); RESTARTABLE(dup2(s, fd), res); if (res < 0) JNU_ThrowIOExceptionWithLastError(env, "dup2"); RESTARTABLE(close(s), res); } #else JNU_ThrowInternalError(env, "should not reach here"); #endif }
JNIEXPORT jint JNICALL Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fdo, jlong address, jint len, jlong offset) { BOOL result = 0; DWORD written = 0; HANDLE h = (HANDLE)(handleval(env, fdo)); DWORD lowPos = 0; long highPos = 0; DWORD lowOffset = 0; long highOffset = 0; lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT); if (lowPos == ((DWORD)-1)) { if (GetLastError() != ERROR_SUCCESS) { JNU_ThrowIOExceptionWithLastError(env, "Seek failed"); return IOS_THROWN; } } lowOffset = (DWORD)offset; highOffset = (DWORD)(offset >> 32); lowOffset = SetFilePointer(h, lowOffset, &highOffset, FILE_BEGIN); if (lowOffset == ((DWORD)-1)) { if (GetLastError() != ERROR_SUCCESS) { JNU_ThrowIOExceptionWithLastError(env, "Seek failed"); return IOS_THROWN; } } result = WriteFile(h, /* File handle to write */ (LPCVOID)address, /* pointers to the buffers */ len, /* number of bytes to write */ &written, /* receives number of bytes written */ NULL); /* no overlapped struct */ if ((h == INVALID_HANDLE_VALUE) || (result == 0)) { JNU_ThrowIOExceptionWithLastError(env, "Write failed"); return IOS_THROWN; } lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN); if (lowPos == ((DWORD)-1)) { if (GetLastError() != ERROR_SUCCESS) { JNU_ThrowIOExceptionWithLastError(env, "Seek failed"); return IOS_THROWN; } } return convertReturnVal(env, (jint)written, JNI_FALSE); }
/* * Returns JNI_TRUE if the specified owner is the only SID will access * to the file. */ static jboolean isAccessUserOnly(JNIEnv* env, SID* owner, ACL* acl) { ACL_SIZE_INFORMATION acl_size_info; DWORD i; /* * If there's no DACL then there's no access to the file */ if (acl == NULL) { return JNI_TRUE; } /* * Get the ACE count */ if (!GetAclInformation(acl, (void *) &acl_size_info, sizeof(acl_size_info), AclSizeInformation)) { JNU_ThrowIOExceptionWithLastError(env, "GetAclInformation failed"); return JNI_FALSE; } /* * Iterate over the ACEs. For each "allow" type check that the SID * matches the owner, and check that the access is read only. */ for (i = 0; i < acl_size_info.AceCount; i++) { void* ace; ACCESS_ALLOWED_ACE *access; SID* sid; if (!GetAce(acl, i, &ace)) { JNU_ThrowIOExceptionWithLastError(env, "GetAce failed"); return -1; } if (((ACCESS_ALLOWED_ACE *)ace)->Header.AceType != ACCESS_ALLOWED_ACE_TYPE) { continue; } access = (ACCESS_ALLOWED_ACE *)ace; sid = (SID *) &access->SidStart; if (!EqualSid(owner, sid)) { /* * If the ACE allows any access then the file is not secure. */ if (access->Mask & ANY_ACCESS) { return JNI_FALSE; } } } return JNI_TRUE; }
/* * Class: sun_management_FileSystemImpl * Method: init0 * Signature: ()V */ JNIEXPORT void JNICALL Java_sun_management_FileSystemImpl_init0 (JNIEnv *env, jclass ignored) { OSVERSIONINFO ver; HINSTANCE hInst; /* * Get the OS version. If dwPlatformId is VER_PLATFORM_WIN32_NT * it means we're running on a Windows NT, 2000, or XP machine. */ ver.dwOSVersionInfoSize = sizeof(ver); GetVersionEx(&ver); isNT = (ver.dwPlatformId == VER_PLATFORM_WIN32_NT); if (!isNT) { return; } /* * On NT/2000/XP we need the addresses of the security functions */ hInst = LoadLibrary("ADVAPI32.DLL"); if (hInst == NULL) { JNU_ThrowIOExceptionWithLastError(env, "Unable to load ADVAPI32.DLL"); return; } GetFileSecurity_func = (GetFileSecurityFunc)GetProcAddress(hInst, "GetFileSecurityA"); GetSecurityDescriptorOwner_func = (GetSecurityDescriptorOwnerFunc)GetProcAddress(hInst, "GetSecurityDescriptorOwner"); GetSecurityDescriptorDacl_func = (GetSecurityDescriptorDaclFunc)GetProcAddress(hInst, "GetSecurityDescriptorDacl"); GetAclInformation_func = (GetAclInformationFunc)GetProcAddress(hInst, "GetAclInformation"); GetAce_func = (GetAceFunc)GetProcAddress(hInst, "GetAce"); EqualSid_func = (EqualSidFunc)GetProcAddress(hInst, "EqualSid"); if (GetFileSecurity_func == NULL || GetSecurityDescriptorDacl_func == NULL || GetSecurityDescriptorDacl_func == NULL || GetAclInformation_func == NULL || GetAce_func == NULL || EqualSid_func == NULL) { JNU_ThrowIOExceptionWithLastError(env, "Unable to get address of security functions"); return; } }
JNIEXPORT jlong JNICALL Java_sun_nio_ch_FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz, jobject fdo, jlong address, jint len) { DWORD read = 0; BOOL result = 0; jlong totalRead = 0; LPVOID loc; int i = 0; DWORD num = 0; struct iovec *iovecp = (struct iovec *)jlong_to_ptr(address); HANDLE h = (HANDLE)(handleval(env, fdo)); if (h == INVALID_HANDLE_VALUE) { JNU_ThrowIOExceptionWithLastError(env, "Invalid handle"); return IOS_THROWN; } for(i=0; i<len; i++) { loc = (LPVOID)jlong_to_ptr(iovecp[i].iov_base); num = iovecp[i].iov_len; result = ReadFile(h, /* File handle to read */ loc, /* address to put data */ num, /* number of bytes to read */ &read, /* number of bytes read */ NULL); /* no overlapped struct */ if (read > 0) { totalRead += read; } if (read < num) { break; } } if (result == 0) { int error = GetLastError(); if (error == ERROR_BROKEN_PIPE) { return IOS_EOF; } if (error == ERROR_NO_DATA) { return IOS_UNAVAILABLE; } JNU_ThrowIOExceptionWithLastError(env, "Read failed"); return IOS_THROWN; } return convertLongReturnVal(env, totalRead, JNI_TRUE); }
/* * Class: sun_tools_attach_VirtualMachineImpl * Method: sigquit * Signature: (I)V */ JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_sigquit (JNIEnv *env, jclass cls, jint pid) { if (kill((pid_t)pid, SIGQUIT) == -1) { JNU_ThrowIOExceptionWithLastError(env, "kill"); } }
JNIEXPORT jint JNICALL Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv *env, jclass clazz, jobject fdo, jlong address, jint len, jboolean append) { BOOL result = 0; DWORD written = 0; HANDLE h = (HANDLE)(handleval(env, fdo)); if (h != INVALID_HANDLE_VALUE) { OVERLAPPED ov; LPOVERLAPPED lpOv; if (append == JNI_TRUE) { ov.Offset = (DWORD)0xFFFFFFFF; ov.OffsetHigh = (DWORD)0xFFFFFFFF; ov.hEvent = NULL; lpOv = &ov; } else { lpOv = NULL; } result = WriteFile(h, /* File handle to write */ (LPCVOID)address, /* pointers to the buffers */ len, /* number of bytes to write */ &written, /* receives number of bytes written */ lpOv); /* overlapped struct */ } if ((h == INVALID_HANDLE_VALUE) || (result == 0)) { JNU_ThrowIOExceptionWithLastError(env, "Write failed"); } return convertReturnVal(env, (jint)written, JNI_FALSE); }
/* * Class: sun_tools_attach_BSDVirtualMachine * Method: createAttachFile * Signature: (Ljava.lang.String;)V */ JNIEXPORT void JNICALL Java_sun_tools_attach_BsdVirtualMachine_createAttachFile(JNIEnv *env, jclass cls, jstring path) { const char* _path; jboolean isCopy; int fd, rc; _path = GetStringPlatformChars(env, path, &isCopy); if (_path == NULL) { JNU_ThrowIOException(env, "Must specify a path"); return; } RESTARTABLE(open(_path, O_CREAT | O_EXCL, S_IWUSR | S_IRUSR), fd); if (fd == -1) { /* release p here before we throw an I/O exception */ if (isCopy) { JNU_ReleaseStringPlatformChars(env, path, _path); } JNU_ThrowIOExceptionWithLastError(env, "open"); return; } RESTARTABLE(chown(_path, geteuid(), getegid()), rc); RESTARTABLE(close(fd), rc); /* release p here */ if (isCopy) { JNU_ReleaseStringPlatformChars(env, path, _path); } }
JNIEXPORT void JNICALL Java_sun_nio_ch_InheritedChannel_close0(JNIEnv *env, jclass cla, jint fd) { if (close(fd) < 0) { JNU_ThrowIOExceptionWithLastError(env, "close failed"); } }
JNIEXPORT jint JNICALL Java_sun_nio_ch_InheritedChannel_open0(JNIEnv *env, jclass cla, jstring path, jint oflag) { const char* str; int oflag_actual; /* convert to OS specific value */ switch (oflag) { case sun_nio_ch_InheritedChannel_O_RDWR : oflag_actual = O_RDWR; break; case sun_nio_ch_InheritedChannel_O_RDONLY : oflag_actual = O_RDONLY; break; case sun_nio_ch_InheritedChannel_O_WRONLY : oflag_actual = O_WRONLY; break; default : JNU_ThrowInternalError(env, "Unrecognized file mode"); return -1; } str = JNU_GetStringPlatformChars(env, path, NULL); if (str == NULL) { return (jint)-1; } else { int fd = open(str, oflag_actual); if (fd < 0) { JNU_ThrowIOExceptionWithLastError(env, str); } JNU_ReleaseStringPlatformChars(env, path, str); return (jint)fd; } }
JNIEXPORT void JNICALL Java_sun_nio_ch_InheritedChannel_dup2(JNIEnv *env, jclass cla, jint fd, jint fd2) { if (dup2(fd, fd2) < 0) { JNU_ThrowIOExceptionWithLastError(env, "dup2 failed"); } }
JNIEXPORT void JNICALL Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz, jobject fdo, jboolean blocking) { if (configureBlocking(fdval(env, fdo), blocking) < 0) JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); }
JNIEXPORT jboolean JNICALL Java_java_nio_MappedByteBuffer_isLoaded0(JNIEnv *env, jobject obj, jlong address, jlong len) { jboolean loaded = JNI_TRUE; jint pageSize = sysconf(_SC_PAGESIZE); jint numPages = (len + pageSize - 1) / pageSize; int result = 0; int i = 0; void *a = (void *) jlong_to_ptr(address); char * vec = (char *)malloc(numPages * sizeof(char)); if (vec == NULL) { JNU_ThrowOutOfMemoryError(env, NULL); return JNI_FALSE; } result = mincore(a, (size_t)len, vec); if (result != 0) { free(vec); JNU_ThrowIOExceptionWithLastError(env, "mincore failed"); return JNI_FALSE; } for (i=0; i<numPages; i++) { if (vec[i] == 0) { loaded = JNI_FALSE; break; } } free(vec); return loaded; }
/** * Converts an existing file descriptor, that references an unbound TCP socket, * to SDP. */ JNIEXPORT void JNICALL Java_sun_net_sdp_SdpSupport_convert0(JNIEnv *env, jclass cls, int fd) { int s = create(env); if (s >= 0) { socklen_t len; int arg, res; struct linger linger; /* copy socket options that are relevant to SDP */ len = sizeof(arg); if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, &len) == 0) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&arg, len); len = sizeof(arg); if (getsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, &len) == 0) setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char*)&arg, len); len = sizeof(linger); if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (void*)&linger, &len) == 0) setsockopt(s, SOL_SOCKET, SO_LINGER, (char*)&linger, len); RESTARTABLE(dup2(s, fd), res); if (res < 0) JNU_ThrowIOExceptionWithLastError(env, "dup2"); RESTARTABLE(close(s), res); } }
static void closeFileDescriptor(JNIEnv *env, int fd) { if (fd != -1) { int result = close(fd); if (result < 0) JNU_ThrowIOExceptionWithLastError(env, "Close failed"); } }
/** * Creates a SDP socket. */ static int create(JNIEnv* env) { int s; #if defined(__solaris__) #ifdef AF_INET6 int domain = ipv6_available() ? AF_INET6 : AF_INET; #else int domain = AF_INET; #endif s = socket(domain, SOCK_STREAM, PROTO_SDP); #elif defined(__linux__) /** * IPv6 not supported by SDP on Linux */ if (ipv6_available()) { JNU_ThrowIOException(env, "IPv6 not supported"); return -1; } s = socket(AF_INET_SDP, SOCK_STREAM, 0); #else /* not supported on other platforms at this time */ s = -1; errno = EPROTONOSUPPORT; #endif if (s < 0) JNU_ThrowIOExceptionWithLastError(env, "socket"); return s; }
/* * Class: sun_tools_attach_LinuxVirtualMachine * Method: sendQuitTo * Signature: (I)V */ JNIEXPORT void JNICALL Java_sun_tools_attach_LinuxVirtualMachine_sendQuitTo (JNIEnv *env, jclass cls, jint pid) { if (kill((pid_t)pid, SIGQUIT)) { JNU_ThrowIOExceptionWithLastError(env, "kill"); } }
JNIEXPORT jlong JNICALL Java_java_lang_ProcessImpl_openForAtomicAppend(JNIEnv *env, jclass ignored, jstring path) { const DWORD access = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); const DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; const DWORD disposition = OPEN_ALWAYS; const DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL; HANDLE h; WCHAR *pathbuf = getPath(env, path); if (pathbuf == NULL) { /* Exception already pending */ return -1; } h = CreateFileW( pathbuf, /* 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); free(pathbuf); if (h == INVALID_HANDLE_VALUE) { JNU_ThrowIOExceptionWithLastError(env, "CreateFileW"); } return ptr_to_jlong(h); }
/* * Class: sun_tools_attach_LinuxVirtualMachine * Method: write * Signature: (I[B)V */ JNIEXPORT void JNICALL Java_sun_tools_attach_LinuxVirtualMachine_write (JNIEnv *env, jclass cls, jint fd, jbyteArray ba, jint off, jint bufLen) { size_t remaining = bufLen; do { unsigned char buf[128]; size_t len = sizeof(buf); int n; if (len > remaining) { len = remaining; } (*env)->GetByteArrayRegion(env, ba, off, len, (jbyte *)buf); RESTARTABLE(write(fd, buf, len), n); if (n > 0) { off += n; remaining -= n; } else { JNU_ThrowIOExceptionWithLastError(env, "write"); return; } } while (remaining > 0); }
/* * Class: sun_tools_attach_WindowsVirtualMachine * Method: createPipe * Signature: (Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_sun_tools_attach_WindowsVirtualMachine_createPipe (JNIEnv *env, jclass cls, jstring pipename) { HANDLE hPipe; char name[MAX_PIPE_NAME_LENGTH]; jstring_to_cstring(env, pipename, name, MAX_PIPE_NAME_LENGTH); hPipe = CreateNamedPipe( name, // pipe name PIPE_ACCESS_INBOUND, // read access PIPE_TYPE_BYTE | // byte mode PIPE_READMODE_BYTE | PIPE_WAIT, // blocking mode 1, // max. instances 128, // output buffer size 8192, // input buffer size NMPWAIT_USE_DEFAULT_WAIT, // client time-out NULL); // default security attribute if (hPipe == INVALID_HANDLE_VALUE) { JNU_ThrowIOExceptionWithLastError(env, "CreateNamedPipe failed"); } return (jlong)hPipe; }
/* * Class: sun_nio_ch_sctp_SctpNet * Method: preClose0 * Signature: (I)V */ JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_preClose0 (JNIEnv *env, jclass clazz, jint fd) { if (preCloseFD >= 0) { if (dup2(preCloseFD, fd) < 0) JNU_ThrowIOExceptionWithLastError(env, "dup2 failed"); } }
JNIEXPORT void JNICALL Java_sun_nio_ch_NativeThread_signal(JNIEnv *env, jclass cl, jlong thread) { #ifdef __linux__ if (pthread_kill((pthread_t)thread, INTERRUPT_SIGNAL)) JNU_ThrowIOExceptionWithLastError(env, "Thread signal failed"); #endif }
JNIEXPORT void JNICALL Java_sun_nio_ch_FileDispatcher_init(JNIEnv *env, jclass cl) { int sp[2]; if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) JNU_ThrowIOExceptionWithLastError(env, "socketpair failed"); preCloseFD = sp[0]; close(sp[1]); }
JNIEXPORT void JNICALL Java_sun_nio_ch_FileDispatcher_preClose0(JNIEnv *env, jclass clazz, jobject fdo) { jint fd = fdval(env, fdo); if (preCloseFD >= 0) { if (dup2(preCloseFD, fd) < 0) JNU_ThrowIOExceptionWithLastError(env, "dup2 failed"); } }
JNIEXPORT jint JNICALL Java_sun_nio_ch_InheritedChannel_dup(JNIEnv *env, jclass cla, jint fd) { int newfd = dup(fd); if (newfd < 0) { JNU_ThrowIOExceptionWithLastError(env, "dup failed"); } return (jint)newfd; }
/* * Class: sun_tools_attach_LinuxVirtualMachine * Method: socket * Signature: ()I */ JNIEXPORT jint JNICALL Java_sun_tools_attach_LinuxVirtualMachine_socket (JNIEnv *env, jclass cls) { int fd = socket(PF_UNIX, SOCK_STREAM, 0); if (fd == -1) { JNU_ThrowIOExceptionWithLastError(env, "socket"); } return (jint)fd; }
/* * Class: sun_nio_ch_sctp_SctpNet * Method: close0 * Signature: (I)V */ JNIEXPORT void JNICALL Java_sun_nio_ch_sctp_SctpNet_close0 (JNIEnv *env, jclass clazz, jint fd) { if (fd != -1) { int rv = close(fd); if (rv < 0) JNU_ThrowIOExceptionWithLastError(env, "Close failed"); } }
WITH_PLATFORM_STRING(env, pathname, path) { char canonicalPath[JVM_MAXPATHLEN]; if (canonicalize(JVM_NativePath((char *)path), canonicalPath, JVM_MAXPATHLEN) < 0) { JNU_ThrowIOExceptionWithLastError(env, "Bad pathname"); } else { rv = JNU_NewStringPlatform(env, canonicalPath); } } END_PLATFORM_STRING(env, path);
JNIEXPORT void JNICALL Java_sun_nio_ch_SocketDispatcher_close0(JNIEnv *env, jclass clazz, jobject fdo) { jint fd = fdval(env, fdo); if (closesocket(fd) == SOCKET_ERROR) { JNU_ThrowIOExceptionWithLastError(env, "Socket close failed"); } }