/* * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/ #include"jni.h" #include"jni_util.h" #include"jvm.h" #include"io_util.h" #include"io_util_md.h" #include <string.h> #include <unistd.h>
// 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 * of other threads using the wrong fd (closed but recycled fd, * that gets re-opened with some other filename) is reduced. * Practically the chance of its occurrence is low, however, we are * taking extra precaution over here.
*/
(*env)->SetIntField(env, this, IO_fd_fdID, -1); if ((*env)->ExceptionOccurred(env)) { return;
} /* * Don't close file descriptors 0, 1, or 2. If we close these stream * then a subsequent file open or socket will use them. Instead we * just redirect these file descriptors to /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 ist noch experimentell.