Search in sources :

Example 1 with CIntPointer

use of org.graalvm.nativeimage.c.type.CIntPointer in project graal by oracle.

the class PosixJavaIOSubstitutions method available.

@Substitute
public int available() throws IOException {
    int handle = PosixUtils.getFDHandle(fd);
    SignedWord ret = WordFactory.zero();
    boolean av = false;
    stat stat = StackValue.get(SizeOf.get(stat.class));
    if (fstat(handle, stat) >= 0) {
        int mode = stat.st_mode();
        if (Util_java_io_FileInputStream.isChr(mode) || Util_java_io_FileInputStream.isFifo(mode) || Util_java_io_FileInputStream.isSock(mode)) {
            CIntPointer np = StackValue.get(SizeOf.get(CIntPointer.class));
            if (ioctl(handle, FIONREAD(), np) >= 0) {
                ret = WordFactory.signed(np.read());
                av = true;
            }
        }
    }
    if (!av) {
        SignedWord cur;
        SignedWord end;
        if ((cur = lseek(handle, WordFactory.zero(), SEEK_CUR())).equal(WordFactory.signed(-1))) {
            av = false;
        } else if ((end = lseek(handle, WordFactory.zero(), SEEK_END())).equal(WordFactory.signed(-1))) {
            av = false;
        } else if (lseek(handle, cur, SEEK_SET()).equal(WordFactory.signed(-1))) {
            av = false;
        } else {
            ret = end.subtract(cur);
            av = true;
        }
    }
    if (av) {
        long r = ret.rawValue();
        if (r > Integer.MAX_VALUE) {
            r = Integer.MAX_VALUE;
        }
        return (int) r;
    }
    throw new IOException(lastErrorString(""));
}
Also used : Stat.stat(com.oracle.svm.core.posix.headers.Stat.stat) Stat.fstat(com.oracle.svm.core.posix.headers.Stat.fstat) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) IOException(java.io.IOException) SignedWord(org.graalvm.word.SignedWord) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 2 with CIntPointer

use of org.graalvm.nativeimage.c.type.CIntPointer in project graal by oracle.

the class PosixJavaLangSubstitutions method forkAndExec.

/*
     * NOTE: This implementation uses simple fork() and exec() calls. However, OpenJDK uses
     * posix_spawn() on some platforms, specifically on Solaris to avoid swap exhaustion when memory
     * is reserved conservatively for the fork'ed process. That implementation is more complex and
     * requires a helper tool to cleanly launch the actual target executable.
     */
@Substitute
@SuppressWarnings({ "unused", "static-method" })
int forkAndExec(int mode, byte[] helperpath, byte[] file, byte[] argBlock, int argCount, byte[] envBlock, int envCount, byte[] dir, int[] fds, boolean redirectErrorStream) throws IOException {
    int[] pipes = new int[8];
    Arrays.fill(pipes, -1);
    try (// 
    PinnedObject filePin = PinnedObject.create(file);
        PinnedObject dirPin = PinnedObject.create(dir);
        PinnedObject argBlockPin = PinnedObject.create(argBlock);
        PinnedObject argvPin = PinnedObject.create(new CCharPointerPointer[argCount + 2]);
        PinnedObject envBlockPin = PinnedObject.create(envBlock);
        PinnedObject envpPin = PinnedObject.create(new CCharPointerPointer[envCount + 1]);
        // 
        PinnedObject pipesPin = PinnedObject.create(pipes)) {
        CCharPointerPointer argv = argvPin.addressOfArrayElement(0);
        argv.write(0, filePin.addressOfArrayElement(0));
        Java_lang_UNIXProcess_Supplement.gatherCStringPointers(argBlockPin.addressOfArrayElement(0), argBlock.length, argv.addressOf(1), argCount + 1);
        CCharPointerPointer envp = WordFactory.nullPointer();
        if (envBlock != null) {
            envp = envpPin.addressOfArrayElement(0);
            Java_lang_UNIXProcess_Supplement.gatherCStringPointers(envBlockPin.addressOfArrayElement(0), envBlock.length, envp.addressOf(0), envCount + 1);
        }
        CIntPointer[] stdioPipes = new CIntPointer[3];
        for (int i = 0; i <= 2; i++) {
            if (fds[i] == -1) {
                stdioPipes[i] = pipesPin.addressOfArrayElement(2 * i);
                if (Unistd.pipe(stdioPipes[i]) < 0) {
                    throw new IOException("pipe() failed");
                }
            }
        }
        CIntPointer failPipe = pipesPin.addressOfArrayElement(2 * stdioPipes.length);
        if (Unistd.pipe(failPipe) < 0) {
            throw new IOException("pipe() failed");
        }
        int[] childStdioFds = new int[3];
        childStdioFds[0] = (fds[0] != -1) ? fds[0] : stdioPipes[0].read(0);
        childStdioFds[1] = (fds[1] != -1) ? fds[1] : stdioPipes[1].read(1);
        childStdioFds[2] = (fds[2] != -1) ? fds[2] : stdioPipes[2].read(1);
        int childFailFd = failPipe.read(1);
        CCharPointer filep = filePin.addressOfArrayElement(0);
        CCharPointer dirp = (dir != null) ? dirPin.addressOfArrayElement(0) : WordFactory.nullPointer();
        int childPid = Java_lang_UNIXProcess_Supplement.doForkAndExec(filep, dirp, argv, envp, childStdioFds, childFailFd);
        if (childPid < 0) {
            throw new IOException("fork() failed");
        }
        // If we are here, we are the parent.
        // store fds of our pipe ends for caller, close unused fds (those of the child)
        fds[0] = fds[1] = fds[2] = -1;
        if (stdioPipes[0].isNonNull()) {
            fds[0] = stdioPipes[0].read(1);
            Unistd.close(stdioPipes[0].read(0));
        }
        if (stdioPipes[1].isNonNull()) {
            fds[1] = stdioPipes[1].read(0);
            Unistd.close(stdioPipes[1].read(1));
        }
        if (stdioPipes[2].isNonNull()) {
            fds[2] = stdioPipes[2].read(0);
            Unistd.close(stdioPipes[2].read(1));
        }
        Unistd.close(failPipe.read(1));
        // read status from child
        final int intSize = SizeOf.get(CIntPointer.class);
        CIntPointer pErrno = StackValue.get(intSize);
        SignedWord failBytes = Java_lang_UNIXProcess_Supplement.readEntirely(failPipe.read(0), pErrno, WordFactory.unsigned(intSize));
        Unistd.close(failPipe.read(0));
        if (failBytes.equal(0)) {
            // success: pipe closed during exec()
            return childPid;
        } else if (failBytes.equal(SizeOf.get(CIntPointer.class))) {
            int errbuflen = 256;
            try (PinnedObject errbuf = PinnedObject.create(new byte[errbuflen])) {
                CCharPointer detailCstr = Errno.strerror_r(pErrno.read(), errbuf.addressOfArrayElement(0), WordFactory.unsigned(errbuflen));
                String detail = CTypeConversion.toJavaString(detailCstr);
                throw new IOException("error=" + pErrno.read() + ", " + detail);
            }
        } else {
            throw new IOException("unexpected data from child");
        }
    } catch (IOException e) {
        // NOTE: not a finally statement because when successful, some pipes need to stay open
        for (int fd : pipes) {
            if (fd != -1) {
                Unistd.close(fd);
            }
        }
        throw e;
    }
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) PinnedObject(org.graalvm.nativeimage.PinnedObject) IOException(java.io.IOException) SignedWord(org.graalvm.word.SignedWord) CCharPointerPointer(org.graalvm.nativeimage.c.type.CCharPointerPointer) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 3 with CIntPointer

use of org.graalvm.nativeimage.c.type.CIntPointer 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)

Example 4 with CIntPointer

use of org.graalvm.nativeimage.c.type.CIntPointer in project graal by oracle.

the class Util_jni method socketAccept.

/* @formatter:on */
/* Do not re-format commented-out code: @formatter:off */
// 644 /*
// 645  * Class:     java_net_PlainSocketImpl
// 646  * Method:    socketAccept
// 647  * Signature: (Ljava/net/SocketImpl;)V
// 648  */
// 649 JNIEXPORT void JNICALL
// 650 Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
// 651                                            jobject socket) {
@Substitute
void socketAccept(SocketImpl socket) throws IOException {
    // 653     /* fields on this */
    // 654     int port;
    CIntPointer port_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
    // 655     jint timeout = (*env)->GetIntField(env, this, psi_timeoutID);
    int timeout = Util_java_net_PlainSocketImpl.as_Target_java_net_AbstractPlainSocketImpl(this).timeout;
    // 656     jlong prevTime = 0;
    long prevTime = 0;
    // 657     jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
    FileDescriptor fdObj = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).fd;
    // 658
    // 659     /* the FileDescriptor field on socket */
    // 660     jobject socketFdObj;
    FileDescriptor socketFdObj;
    // 661     /* the InetAddress field on socket */
    // 662     jobject socketAddressObj;
    InetAddress socketAddressObj;
    // 663
    // 664     /* the ServerSocket fd int field on fdObj */
    // 665     jint fd;
    int fd;
    // 666
    // 667     /* accepted fd */
    // 668     jint newfd;
    /* Initialized for the use in the finally-clause. */
    int newfd = -1;
    // 669
    // 670     SOCKADDR him;
    Socket.sockaddr him = StackValue.get(JavaNetNetUtilMD.SOCKADDR_LEN());
    // 671     int len;
    CIntPointer len_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
    // 672
    // 673     len = SOCKADDR_LEN;
    len_Pointer.write(JavaNetNetUtilMD.SOCKADDR_LEN());
    // 675     if (IS_NULL(fdObj)) {
    if (fdObj == null) {
        // 678         return;
        throw new SocketException("Socket closed");
    } else {
        // 680         fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
        fd = Util_java_io_FileDescriptor.getFD(fdObj);
    }
    // 682     if (IS_NULL(socket)) {
    if (socket == null) {
        // 684         return;
        throw new NullPointerException("socket is null");
    }
    try {
        // 696     for (;;) {
        for (; ; ) {
            // 697         int ret;
            int ret;
            // 700         if (prevTime == 0 && timeout > 0) {
            if (prevTime == 0 && timeout > 0) {
                // 701             prevTime = JVM_CurrentTimeMillis(env, 0);
                prevTime = System.currentTimeMillis();
            }
            // 706         if (timeout <= 0) {
            if (timeout <= 0) {
                // 707             ret = NET_Timeout(fd, -1);
                ret = JavaNetNetUtilMD.NET_Timeout(fd, -1);
            } else {
                // 709             ret = NET_Timeout(fd, timeout);
                ret = JavaNetNetUtilMD.NET_Timeout(fd, timeout);
            }
            // 711         if (ret == 0) {
            if (ret == 0) {
                // 714             return;
                throw new SocketTimeoutException("Accept timed out");
            // 715         } else if (ret == JVM_IO_ERR) {
            } else if (ret == Target_jvm.JVM_IO_ERR()) {
                // 716             if (errno == EBADF) {
                if (Errno.errno() == Errno.EBADF()) {
                    // 717                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
                    throw new SocketException("Socket closed");
                // 718             } else if (errno == ENOMEM) {
                } else if (Errno.errno() == Errno.ENOMEM()) {
                    // 719                JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
                    throw new OutOfMemoryError("NET_Timeout native heap allocation failed");
                } else {
                    // 721                NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
                    throw new SocketException("Accept failed");
                }
            // 723             return;
            // 724         } else if (ret == JVM_IO_INTR) {
            } else if (ret == Target_jvm.JVM_IO_INTR()) {
                // 727             return;
                throw new InterruptedIOException("operation interrupted");
            }
            // 729
            // 730         newfd = NET_Accept(fd, (struct sockaddr *)&him, (jint*)&len);
            newfd = JavaNetNetUtilMD.NET_Accept(fd, him, len_Pointer);
            // 733         if (newfd >= 0) {
            if (newfd >= 0) {
                // 734             SET_BLOCKING(newfd);
                Util_java_net_PlainSocketImpl.SET_BLOCKING(newfd);
                // 735             break;
                break;
            }
            // 739         if (!(errno == ECONNABORTED || errno == EWOULDBLOCK)) {
            if (!(Errno.errno() == Errno.ECONNABORTED() || Errno.errno() == Errno.EWOULDBLOCK())) {
                // 740             break;
                break;
            }
            // 744         if (timeout) {
            if (CTypeConversion.toBoolean(timeout)) {
                // 745             jlong currTime = JVM_CurrentTimeMillis(env, 0);
                long currTime = System.currentTimeMillis();
                // 746             timeout -= (currTime - prevTime);
                timeout -= (currTime - prevTime);
                // 748             if (timeout <= 0) {
                if (timeout <= 0) {
                    // 751                 return;
                    throw new SocketTimeoutException("Accept timed out");
                }
                // 753             prevTime = currTime;
                prevTime = currTime;
            }
        }
        // 757     if (newfd < 0) {
        if (newfd < 0) {
            // 758         if (newfd == -2) {
            if (newfd == -2) {
                // 760                             "operation interrupted");
                throw new InterruptedIOException("operation interrupted");
            } else {
                // 762             if (errno == EINVAL) {
                if (Errno.errno() == Errno.EINVAL()) {
                    // 763                 errno = EBADF;
                    Errno.set_errno(Errno.EBADF());
                }
                // 765             if (errno == EBADF) {
                if (Errno.errno() == Errno.EBADF()) {
                    // 766                 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
                    throw new SocketException("Socket closed");
                } else {
                    // 768                 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
                    throw new SocketException("Accept failed");
                }
            }
        // 771         return;
        }
    } finally {
        // 773
        // 774     /*
        // 775      * fill up the remote peer port and address in the new socket structure.
        // 776      */
        // 777     socketAddressObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
        socketAddressObj = JavaNetNetUtil.NET_SockaddrToInetAddress(him, port_Pointer);
        // 778     if (socketAddressObj == NULL) {
        if (socketAddressObj == null) {
            // 779         /* should be pending exception */
            // 780         close(newfd);
            Unistd.close(newfd);
        // 781         return;
        }
    }
    // 783
    // 784     /*
    // 785      * Populate SocketImpl.fd.fd
    // 786      */
    // 787     socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID);
    socketFdObj = Util_java_net_SocketImpl.from_SocketImpl(socket).fd;
    // 788     (*env)->SetIntField(env, socketFdObj, IO_fd_fdID, newfd);
    Util_java_io_FileDescriptor.setFD(socketFdObj, newfd);
    // 789
    // 790     (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
    Util_java_net_SocketImpl.from_SocketImpl(socket).address = socketAddressObj;
    // 791     (*env)->SetIntField(env, socket, psi_portID, port);
    Util_java_net_SocketImpl.from_SocketImpl(socket).port = port_Pointer.read();
    /* Not re-using the frame-local "port". */
    // 792     /* also fill up the local port information */
    // 793      port = (*env)->GetIntField(env, this, psi_localportID);
    int thisLocalPort = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).localport;
    // 794     (*env)->SetIntField(env, socket, psi_localportID, port);
    Util_java_net_SocketImpl.from_SocketImpl(socket).localport = thisLocalPort;
}
Also used : SocketException(java.net.SocketException) InterruptedIOException(java.io.InterruptedIOException) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) SocketTimeoutException(java.net.SocketTimeoutException) InetAddress(java.net.InetAddress) Util_java_io_FileDescriptor(com.oracle.svm.core.posix.PosixOSInterface.Util_java_io_FileDescriptor) FileDescriptor(java.io.FileDescriptor) ServerSocket(java.net.ServerSocket) Socket(com.oracle.svm.core.posix.headers.Socket) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 5 with CIntPointer

use of org.graalvm.nativeimage.c.type.CIntPointer in project graal by oracle.

the class Util_jni method socketCreate.

/* Substitutions for native methods. */
/* Do not re-format commented-out code: @formatter:off */
@SuppressWarnings("finally")
@Substitute
// 182                                            jboolean stream) {
void socketCreate(boolean stream) throws IOException, InterruptedException {
    // 183 jobject fdObj, ssObj;
    FileDescriptor fdObj;
    ServerSocket ssObj;
    // 184 int fd;
    int fd;
    // 185 int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
    int type = (stream ? Socket.SOCK_STREAM() : Socket.SOCK_DGRAM());
    // 186 #ifdef AF_INET6
    int domain;
    if (IsDefined.socket_AF_INET6()) {
        // 187 int domain = ipv6_available() ? AF_INET6 : AF_INET;
        domain = JavaNetNetUtil.ipv6_available() ? Socket.AF_INET6() : Socket.AF_INET();
    } else {
        // 189 int domain = AF_INET;
        domain = Socket.AF_INET();
    }
    // 190 #endif
    // 191
    // 192 if (socketExceptionCls == NULL) {
    // 193 jclass c = (*env)->FindClass(env, "java/net/SocketException");
    // 194 CHECK_NULL(c);
    // 195 socketExceptionCls = (jclass)(*env)->NewGlobalRef(env, c);
    // 196 CHECK_NULL(socketExceptionCls);
    // 197 }
    // 198 fdObj = (*env)->GetObjectField(env, this, psi_fdID);
    fdObj = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).fd;
    // 203 }
    if (fdObj == null) {
        throw new SocketException("null fd object");
    }
    // 205 if ((fd = JVM_Socket(domain, type, 0)) == JVM_IO_ERR) {
    if ((fd = Socket.socket(domain, type, 0)) == Target_jvm.JVM_IO_ERR()) {
        // 206 /* note: if you run out of fds, you may not be able to load
        // 207 * the exception class, and get a NoClassDefFoundError
        // 208 * instead.
        // 209 */
        // 210 NET_ThrowNew(env, errno, "can't create socket");
        JavaNetNetUtilMD.NET_ThrowNew(Errno.errno(), "can't create socket");
        // 211 return;
        return;
    }
    // 214 #ifdef AF_INET6
    if (IsDefined.socket_AF_INET6()) {
        // 216 if (domain == AF_INET6) {
        if (domain == Socket.AF_INET6()) {
            // 217 int arg = 0;
            CIntPointer argPointer = StackValue.get(SizeOf.get(CIntPointer.class));
            argPointer.write(0);
            // 219 sizeof(int)) < 0) {
            if (Socket.setsockopt(fd, NetinetIn.IPPROTO_IPV6(), NetinetIn.IPV6_V6ONLY(), argPointer, SizeOf.get(CIntPointer.class)) < 0) {
                try {
                    // 220 NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
                    JavaNetNetUtilMD.NET_ThrowNew(Errno.errno(), "cannot set IPPROTO_IPV6");
                } finally {
                    // 221 close(fd);
                    Unistd.close(fd);
                    // 222 return;
                    return;
                }
            }
        }
    }
    // 225 #endif /* AF_INET6 */
    // 226
    // 227 /*
    // 228 * If this is a server socket then enable SO_REUSEADDR
    // 229 * automatically and set to non blocking.
    // 230 */
    // 231 ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID);
    ssObj = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).serverSocket;
    // 232 if (ssObj != NULL) {
    if (ssObj != null) {
        // 233 int arg = 1;
        CIntPointer argPointer = StackValue.get(SizeOf.get(CIntPointer.class));
        argPointer.write(1);
        // 234 SET_NONBLOCKING(fd);
        Util_java_net_PlainSocketImpl.SET_NONBLOCKING(fd);
        // 236 sizeof(arg)) < 0) {
        if (Socket.setsockopt(fd, Socket.SOL_SOCKET(), Socket.SO_REUSEADDR(), argPointer, SizeOf.get(CIntPointer.class)) < 0) {
            try {
                // 237 NET_ThrowNew(env, errno, "cannot set SO_REUSEADDR");
                JavaNetNetUtilMD.NET_ThrowNew(Errno.errno(), "cannot set SO_REUSEADDR");
            } finally {
                // 238 close(fd);
                Unistd.close(fd);
            // 239 return;
            }
        }
    }
    // 243 (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
    PosixOSInterface.Util_java_io_FileDescriptor.setFD(fdObj, fd);
/* @formatter:on */
}
Also used : SocketException(java.net.SocketException) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) ServerSocket(java.net.ServerSocket) Util_java_io_FileDescriptor(com.oracle.svm.core.posix.PosixOSInterface.Util_java_io_FileDescriptor) FileDescriptor(java.io.FileDescriptor) Substitute(com.oracle.svm.core.annotate.Substitute)

Aggregations

CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)24 Substitute (com.oracle.svm.core.annotate.Substitute)10 Socket (com.oracle.svm.core.posix.headers.Socket)7 SocketException (java.net.SocketException)7 ServerSocket (java.net.ServerSocket)6 PinnedObject (org.graalvm.nativeimage.PinnedObject)6 CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)6 Util_java_io_FileDescriptor (com.oracle.svm.core.posix.PosixOSInterface.Util_java_io_FileDescriptor)5 FileDescriptor (java.io.FileDescriptor)5 CEntryPointOptions (com.oracle.svm.core.c.function.CEntryPointOptions)4 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)4 IOException (java.io.IOException)3 InterruptedIOException (java.io.InterruptedIOException)3 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)2 InetAddress (java.net.InetAddress)2 SocketTimeoutException (java.net.SocketTimeoutException)2 CCharPointerPointer (org.graalvm.nativeimage.c.type.CCharPointerPointer)2 WordPointer (org.graalvm.nativeimage.c.type.WordPointer)2 SignedWord (org.graalvm.word.SignedWord)2 DIR (com.oracle.svm.core.posix.headers.Dirent.DIR)1