Search in sources :

Example 11 with CCharPointerHolder

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

the class PosixUtils method fileClose.

static void fileClose(FileDescriptor fd) throws IOException {
    int handle = Util_java_io_FileDescriptor.getFD(fd);
    if (handle == -1) {
        return;
    }
    Util_java_io_FileDescriptor.setFD(fd, -1);
    // Do not close file descriptors 0, 1, 2. Instead, redirect to /dev/null.
    if (handle >= 0 && handle <= 2) {
        int devnull;
        try (CCharPointerHolder pathPin = CTypeConversion.toCString("/dev/null")) {
            CCharPointer pathPtr = pathPin.get();
            devnull = open(pathPtr, O_WRONLY(), 0);
        }
        if (devnull < 0) {
            Util_java_io_FileDescriptor.setFD(fd, handle);
            throw new IOException(lastErrorString("open /dev/null failed"));
        } else {
            dup2(devnull, handle);
            close(devnull);
        }
    } else if (close(handle) == -1) {
        throw new IOException(lastErrorString("close failed"));
    }
}
Also used : IOException(java.io.IOException) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 12 with CCharPointerHolder

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

the class JavaMainWrapper method setCRuntimeArgument0.

public static boolean setCRuntimeArgument0(String arg0) {
    boolean arg0truncation = false;
    try (CCharPointerHolder arg0Pin = CTypeConversion.toCString(arg0)) {
        CCharPointer arg0Pointer = arg0Pin.get();
        UnsignedWord arg0Length = SubstrateUtil.strlen(arg0Pointer);
        UnsignedWord origLength = WordFactory.unsigned(getCRuntimeArgumentBlockLength());
        UnsignedWord newArgLength = origLength;
        if (arg0Length.add(1).belowThan(origLength)) {
            newArgLength = arg0Length.add(1);
        }
        arg0truncation = arg0Length.aboveThan(origLength);
        CCharPointer firstArgPos = argv.read(0);
        // Copy the new arg0 to the original argv[0] position
        MemoryUtil.copyConjointMemoryAtomic(WordFactory.pointer(arg0Pointer.rawValue()), WordFactory.pointer(firstArgPos.rawValue()), newArgLength);
        // Zero-out the remaining space
        MemoryUtil.fillToMemoryAtomic((Pointer) WordFactory.unsigned(firstArgPos.rawValue()).add(newArgLength), origLength.subtract(newArgLength), (byte) 0);
    }
    // Let caller know if truncation happened
    return arg0truncation;
}
Also used : UnsignedWord(org.graalvm.word.UnsignedWord) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 13 with CCharPointerHolder

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

the class Util_jni method getLocalHostName.

@Substitute
// 354 Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
@SuppressWarnings({ "static-method" })
public String getLocalHostName() {
    // 355     char hostname[NI_MAXHOST+1];
    CCharPointer hostname = StackValue.get(Netdb.NI_MAXHOST() + 1, SizeOf.get(CCharPointer.class));
    // 357     hostname[0] = '\0';
    hostname.write(0, (byte) '\0');
    // 358     if (JVM_GetHostName(hostname, sizeof(hostname))) {
    if (CTypeConversion.toBoolean(VmPrimsJVM.JVM_GetHostName(hostname, Netdb.NI_MAXHOST() + 1))) {
        // 360         strcpy(hostname, "localhost");
        try (CCharPointerHolder pin = CTypeConversion.toCString("localhost")) {
            LibC.strcpy(hostname, pin.get());
        }
    } else {
        // 362         struct addrinfo hints, *res;
        Netdb.addrinfo hints = StackValue.get(SizeOf.get(Netdb.addrinfo.class));
        Netdb.addrinfoPointer res = StackValue.get(SizeOf.get(Netdb.addrinfoPointer.class));
        // 363         int error;
        int error;
        // 365         hostname[NI_MAXHOST] = '\0';
        hostname.write(Netdb.NI_MAXHOST(), (byte) '\0');
        // 366         memset(&hints, 0, sizeof(hints));
        LibC.memset(hints, WordFactory.zero(), SizeOf.unsigned(Netdb.addrinfo.class));
        // 367         hints.ai_flags = AI_CANONNAME;
        hints.set_ai_flags(Netdb.AI_CANONNAME());
        // 368         hints.ai_family = AF_INET;
        hints.set_ai_family(Socket.AF_INET());
        // 370         error = getaddrinfo(hostname, NULL, &hints, &res);
        error = Netdb.getaddrinfo(hostname, WordFactory.nullPointer(), hints, res);
        // 372         if (error == 0) {/* host is known to name service */
        if (error == 0) {
            // 373             getnameinfo(res->ai_addr,
            // 374                         res->ai_addrlen,
            // 375                         hostname,
            // 376                         NI_MAXHOST,
            // 377                         NULL,
            // 378                         0,
            // 379                         NI_NAMEREQD);
            Netdb.getnameinfo(res.read().ai_addr(), res.read().ai_addrlen(), hostname, Netdb.NI_MAXHOST(), WordFactory.nullPointer(), 0, Netdb.NI_NAMEREQD());
            // 381             /* if getnameinfo fails hostname is still the value
            // 382                from gethostname */
            // 384             freeaddrinfo(res);
            Netdb.freeaddrinfo(res.read());
        }
    }
    // 387     return (*env)->NewStringUTF(env, hostname);
    return CTypeConversion.toJavaString(hostname);
}
Also used : Netdb(com.oracle.svm.core.posix.headers.Netdb) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 14 with CCharPointerHolder

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

the class PosixJavaIOSubstitutions method canonicalize0.

@Substitute
private String canonicalize0(String path) throws IOException {
    final int maxPathLen = MAXPATHLEN();
    if (path.length() > maxPathLen) {
        throw new IOException("Bad pathname");
    }
    CCharPointer resolved = StackValue.get(maxPathLen);
    try (CCharPointerHolder pathPin = CTypeConversion.toCString(path)) {
        CCharPointer pathPtr = pathPin.get();
        if (realpath(pathPtr, resolved).notEqual(WordFactory.zero())) {
            // that worked, so return it
            return PosixUtils.collapse(CTypeConversion.toJavaString(resolved));
        }
    }
    // something's bogus in the original path, so remove names from the end
    // until either some subpath works or we run out of names
    String resolvedPart = path;
    String unresolvedPart = "";
    CCharPointer r = WordFactory.nullPointer();
    int lastSep = resolvedPart.lastIndexOf('/');
    while (lastSep != -1) {
        // prepend the last part of the resolved part to the unresolved part
        unresolvedPart = resolvedPart.substring(lastSep) + unresolvedPart;
        // remove the last part from the resolved part
        resolvedPart = lastSep == 0 ? "" : resolvedPart.substring(0, lastSep);
        lastSep = resolvedPart.lastIndexOf('/');
        if (lastSep == -1) {
            break;
        }
        try (CCharPointerHolder pathPin = CTypeConversion.toCString(resolvedPart)) {
            CCharPointer resolvedPartPtr = pathPin.get();
            r = realpath(resolvedPartPtr, resolved);
        }
        int errno = errno();
        if (r.notEqual(WordFactory.zero())) {
            // the subpath has a canonical path
            break;
        } else if (errno == ENOENT() || errno == ENOTDIR() || errno == EACCES()) {
            // Other I/O problems cause an error return.
            continue;
        } else {
            throw new IOException(lastErrorString("Bad pathname"));
        }
    }
    if (r.notEqual(WordFactory.zero())) {
        // append unresolved subpath to resolved subpath
        String rs = CTypeConversion.toJavaString(r);
        if (rs.length() + 1 + unresolvedPart.length() > maxPathLen) {
            throw new IOException(lastErrorString("Bad pathname"));
        }
        return PosixUtils.collapse(rs + "/" + unresolvedPart);
    } else {
        // nothing resolved, so just return the original path
        return PosixUtils.collapse(path);
    }
}
Also used : IOException(java.io.IOException) PosixOSInterface.lastErrorString(com.oracle.svm.core.posix.PosixOSInterface.lastErrorString) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 15 with CCharPointerHolder

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

the class PosixJavaIOSubstitutions method setLastModifiedTime.

@Substitute
public boolean setLastModifiedTime(File f, long time) {
    stat stat = StackValue.get(SizeOf.get(stat.class));
    try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
        CCharPointer pathPtr = pathPin.get();
        if (stat(pathPtr, stat) == 0) {
            timeval timeval = StackValue.get(2, SizeOf.get(timeval.class));
            // preserve access time
            timeval access = timeval.addressOf(0);
            access.set_tv_sec(stat.st_atime());
            access.set_tv_usec(0L);
            // change last-modified time
            timeval last = timeval.addressOf(1);
            last.set_tv_sec(time / 1000);
            last.set_tv_usec((time % 1000) * 1000);
            if (utimes(pathPtr, timeval) == 0) {
                return true;
            }
        }
    }
    return false;
}
Also used : Stat.stat(com.oracle.svm.core.posix.headers.Stat.stat) Stat.fstat(com.oracle.svm.core.posix.headers.Stat.fstat) Time.timeval(com.oracle.svm.core.posix.headers.Time.timeval) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Aggregations

CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)15 CCharPointerHolder (org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)15 Substitute (com.oracle.svm.core.annotate.Substitute)8 IOException (java.io.IOException)3 PosixOSInterface.lastErrorString (com.oracle.svm.core.posix.PosixOSInterface.lastErrorString)2 Netdb (com.oracle.svm.core.posix.headers.Netdb)2 Stat.fstat (com.oracle.svm.core.posix.headers.Stat.fstat)2 Stat.stat (com.oracle.svm.core.posix.headers.Stat.stat)2 NoAllocationVerifier (com.oracle.svm.core.heap.NoAllocationVerifier)1 DIR (com.oracle.svm.core.posix.headers.Dirent.DIR)1 Dirent.dirent (com.oracle.svm.core.posix.headers.Dirent.dirent)1 Dirent.direntPointer (com.oracle.svm.core.posix.headers.Dirent.direntPointer)1 ENOTDIR (com.oracle.svm.core.posix.headers.Errno.ENOTDIR)1 NetinetIn (com.oracle.svm.core.posix.headers.NetinetIn)1 S_IFDIR (com.oracle.svm.core.posix.headers.Stat.S_IFDIR)1 Statvfs.statvfs (com.oracle.svm.core.posix.headers.Statvfs.statvfs)1 Time.timeval (com.oracle.svm.core.posix.headers.Time.timeval)1 FileNotFoundException (java.io.FileNotFoundException)1 Inet4Address (java.net.Inet4Address)1 Inet6Address (java.net.Inet6Address)1