use of com.oracle.svm.core.posix.headers.Dirent.DIR in project graal by oracle.
the class PosixJavaIOSubstitutions method list.
@Substitute
public String[] list(File f) {
DIR dir;
try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
CCharPointer pathPtr = pathPin.get();
dir = opendir(pathPtr);
}
if (dir.isNull()) {
return null;
}
List<String> entries = new ArrayList<>();
dirent dirent = StackValue.get(SizeOf.get(dirent.class) + PATH_MAX() + 1);
direntPointer resultDirent = StackValue.get(SizeOf.get(direntPointer.class));
while (readdir_r(dir, dirent, resultDirent) == 0 && !resultDirent.read().isNull()) {
String name = CTypeConversion.toJavaString(dirent.d_name());
if (name.equals(".") || name.equals("..")) {
continue;
}
entries.add(name);
}
closedir(dir);
return entries.toArray(new String[entries.size()]);
}
use of com.oracle.svm.core.posix.headers.Dirent.DIR in project graal by oracle.
the class PosixJavaLangSubstitutions method uninterruptibleForkAndExec.
@Uninterruptible(reason = "fragile state after fork()")
private static int uninterruptibleForkAndExec(CCharPointer file, CCharPointer dir, CCharPointerPointer argv, CCharPointerPointer envp, int[] stdioFds, int initialFailFd, PointerBase buffer, int buflen, CCharPointer procFdsPath, CCharPointer searchPaths, CCharPointer searchPathSeparator) {
int childPid;
childPid = UnistdNoTransitions.fork();
if (childPid != 0) {
return childPid;
}
// If we are here, we are the child process.
int failFd = initialFailFd;
try {
// In case of an error, we "return" to end up in the finally block below and notify the
// parent process of the failure.
final int gotoFinally = -1;
if (Java_lang_UNIXProcess_Supplement.dup2(stdioFds[0], 0) < 0) {
return gotoFinally;
}
if (Java_lang_UNIXProcess_Supplement.dup2(stdioFds[1], 1) < 0) {
return gotoFinally;
}
if (Java_lang_UNIXProcess_Supplement.dup2(stdioFds[2], 2) < 0) {
return gotoFinally;
}
if (Java_lang_UNIXProcess_Supplement.dup2(failFd, 3) < 0) {
return gotoFinally;
}
failFd = 3;
// FD_CLOEXEC: close fail pipe on exec() to indicate success to parent
if (Fcntl.fcntl_no_transition(failFd, Fcntl.F_SETFD(), Fcntl.FD_CLOEXEC()) < 0) {
return gotoFinally;
}
/*
* opendir() below allocates a file descriptor. We close failFd+1 so it should become
* the descriptor allocated to opendir() and we can avoid closing it together with the
* other descriptors.
*/
final int maxFd = failFd + 1;
if (UnistdNoTransitions.close(maxFd) < 0) {
return gotoFinally;
}
if (procFdsPath.isNull()) {
// We have no procfs, resort to close file descriptors by trial and error
int maxOpenFds = (int) UnistdNoTransitions.sysconf(Unistd._SC_OPEN_MAX());
for (int fd = maxFd + 1; fd < maxOpenFds; fd++) {
if (UnistdNoTransitions.close(fd) != 0 && Errno.errno() != Errno.EBADF()) {
return gotoFinally;
}
}
} else {
DIR fddir = Dirent.opendir_no_transition(procFdsPath);
if (fddir.isNull()) {
return gotoFinally;
}
dirent dirent = WordFactory.pointer(buffer.rawValue());
direntPointer direntptr = StackValue.get(SizeOf.get(direntPointer.class));
int status;
while ((status = Dirent.readdir_r_no_transition(fddir, dirent, direntptr)) == 0 && direntptr.read().isNonNull()) {
CCharPointerPointer endptr = StackValue.get(SizeOf.get(CCharPointerPointer.class));
long fd = LibC.strtol(dirent.d_name(), endptr, 10);
if (fd > maxFd && endptr.read().isNonNull() && endptr.read().read() == '\0') {
UnistdNoTransitions.close((int) fd);
}
}
if (status != 0) {
// readdir_r() does not set errno
Errno.set_errno(status);
return gotoFinally;
}
Dirent.closedir_no_transition(fddir);
}
if (dir.isNonNull()) {
if (UnistdNoTransitions.chdir(dir) < 0) {
return gotoFinally;
}
}
CCharPointerPointer actualEnvp = envp;
if (actualEnvp.isNull()) {
actualEnvp = LibCHelper.getEnviron();
}
if (LibC.strchr(file, '/').isNonNull()) {
UnistdNoTransitions.execve(argv.read(0), argv, actualEnvp);
} else {
// Scan PATH for the file to execute. We cannot use execvpe()
// because it is a GNU extension that is not universally available.
final int fileStrlen = (int) LibC.strlen(file).rawValue();
int stickyErrno = 0;
final CCharPointerPointer saveptr = StackValue.get(SizeOf.get(CCharPointerPointer.class));
saveptr.write(WordFactory.nullPointer());
CCharPointer searchDir = LibC.strtok_r(searchPaths, searchPathSeparator, saveptr);
while (searchDir.isNonNull()) {
CCharPointer bufptr = WordFactory.pointer(buffer.rawValue());
int len0 = (int) LibC.strlen(searchDir).rawValue();
if (len0 + fileStrlen + 2 > buflen) {
Errno.set_errno(Errno.ENAMETOOLONG());
continue;
}
if (len0 > 0) {
LibC.strcpy(bufptr, searchDir);
if (bufptr.read(len0 - 1) != '/') {
bufptr.write(len0, (byte) '/');
len0++;
}
}
LibC.strcpy(bufptr.addressOf(len0), file);
UnistdNoTransitions.execve(bufptr, argv, actualEnvp);
int e = Errno.errno();
if (e == Errno.EACCES()) {
// as exec(): report EACCES unless we succeed later
stickyErrno = e;
} else if (e == Errno.ENOENT() || e == Errno.ENOTDIR() || e == Errno.ELOOP() || e == Errno.ESTALE() || e == Errno.ENODEV() || e == Errno.ETIMEDOUT()) {
// ignore
} else {
stickyErrno = e;
// bad
break;
}
searchDir = LibC.strtok_r(WordFactory.nullPointer(), searchPathSeparator, saveptr);
}
if (stickyErrno != 0) {
Errno.set_errno(stickyErrno);
}
}
// If we are here, exec certainly failed.
} catch (Throwable t) {
Errno.set_errno(Integer.MIN_VALUE);
} finally {
try {
// notify parent of failure
final int intSize = SizeOf.get(CIntPointer.class);
CIntPointer pErrno = StackValue.get(intSize);
pErrno.write(Errno.errno());
Java_lang_UNIXProcess_Supplement.writeEntirely(failFd, pErrno, WordFactory.unsigned(intSize));
UnistdNoTransitions.close(failFd);
} finally {
UnistdNoTransitions._exit(-1);
}
}
throw VMError.shouldNotReachHere();
}
Aggregations