// Function to close the fd held by this FileDescriptor and set fd to -1. void
fileDescriptorClose(JNIEnv *env, jobject this)
{
FD fd = (*env)->GetIntField(env, this, IO_fd_fdID); if ((*env)->ExceptionOccurred(env)) { return;
}
if (fd == -1) { return; // already closed and set to -1
}
/* Set the fd to -1 before closing it so that the timing window *ofotherthreadsusingthewrongfd(closedbutrecycledfd, *thatgetsre-openedwithsomeotherfilename)isreduced. *Practicallythechanceofitsoccurrenceislow,however,weare *takingextraprecautionoverhere.
*/
(*env)->SetIntField(env, this, IO_fd_fdID, -1); if ((*env)->ExceptionOccurred(env)) { return;
} /* *Don'tclosefiledescriptors0,1,or2.Ifweclosethesestream *thenasubsequentfileopenorsocketwillusethem.Insteadwe *justredirectthesefiledescriptorsto/dev/null.
*/ if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) { int devnull = open("/dev/null", O_WRONLY); if (devnull < 0) {
(*env)->SetIntField(env, this, IO_fd_fdID, fd);
JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
} else {
dup2(devnull, fd);
close(devnull);
}
} else { int result; #ifdefined(_AIX) /* AIX allows close to be restarted after EINTR */
RESTARTABLE(close(fd), result); #else
result = close(fd); #endif if (result == -1 && errno != EINTR) {
JNU_ThrowIOExceptionWithLastError(env, "close failed");
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.