Search in sources :

Example 1 with DIR

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()]);
}
Also used : ArrayList(java.util.ArrayList) PosixOSInterface.lastErrorString(com.oracle.svm.core.posix.PosixOSInterface.lastErrorString) Dirent.direntPointer(com.oracle.svm.core.posix.headers.Dirent.direntPointer) DIR(com.oracle.svm.core.posix.headers.Dirent.DIR) ENOTDIR(com.oracle.svm.core.posix.headers.Errno.ENOTDIR) S_IFDIR(com.oracle.svm.core.posix.headers.Stat.S_IFDIR) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Dirent.dirent(com.oracle.svm.core.posix.headers.Dirent.dirent) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 2 with DIR

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();
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) Dirent.direntPointer(com.oracle.svm.core.posix.headers.Dirent.direntPointer) DIR(com.oracle.svm.core.posix.headers.Dirent.DIR) CCharPointerPointer(org.graalvm.nativeimage.c.type.CCharPointerPointer) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Dirent.dirent(com.oracle.svm.core.posix.headers.Dirent.dirent) Uninterruptible(com.oracle.svm.core.annotate.Uninterruptible)

Aggregations

DIR (com.oracle.svm.core.posix.headers.Dirent.DIR)2 Dirent.dirent (com.oracle.svm.core.posix.headers.Dirent.dirent)2 Dirent.direntPointer (com.oracle.svm.core.posix.headers.Dirent.direntPointer)2 CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)2 Substitute (com.oracle.svm.core.annotate.Substitute)1 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)1 PosixOSInterface.lastErrorString (com.oracle.svm.core.posix.PosixOSInterface.lastErrorString)1 ENOTDIR (com.oracle.svm.core.posix.headers.Errno.ENOTDIR)1 S_IFDIR (com.oracle.svm.core.posix.headers.Stat.S_IFDIR)1 ArrayList (java.util.ArrayList)1 CCharPointerPointer (org.graalvm.nativeimage.c.type.CCharPointerPointer)1 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)1 CCharPointerHolder (org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)1