use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class PosixJavaLangSubstitutions method doForkAndExec.
@SuppressWarnings("try")
static int doForkAndExec(CCharPointer file, CCharPointer dir, CCharPointerPointer argv, CCharPointerPointer envp, int[] stdioFds, int failFd) {
final int buflen = SizeOf.get(dirent.class) + Limits.PATH_MAX() + 1;
final boolean haveProcFs = Platform.includedIn(Platform.LINUX.class);
try (// only this thread will exist in the child and garbage collection is not possible.
PinnedObject bufferPin = PinnedObject.create(new byte[buflen]);
CCharPointerHolder procFdsPath = haveProcFs ? CTypeConversion.toCString("/proc/self/fd/") : null;
CCharPointerHolder searchPaths = CTypeConversion.toCString(System.getenv("PATH"));
CCharPointerHolder searchPathSeparator = CTypeConversion.toCString(":");
NoAllocationVerifier v = NoAllocationVerifier.factory("fragile state after fork()")) {
CCharPointer procFdsPathPtr = (procFdsPath != null) ? procFdsPath.get() : WordFactory.nullPointer();
return uninterruptibleForkAndExec(file, dir, argv, envp, stdioFds, failFd, bufferPin.addressOfArrayElement(0), buflen, procFdsPathPtr, searchPaths.get(), searchPathSeparator.get());
}
}
use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class PolyglotNativeAPI method handleThrowable.
private static PolyglotStatus handleThrowable(Throwable t) {
PolyglotStatus errorCode = t instanceof PolyglotNativeAPIError ? ((PolyglotNativeAPIError) t).getCode() : polyglot_generic_failure;
String message = t.getMessage();
PolyglotExtendedErrorInfo unmanagedErrorInfo = UnmanagedMemory.malloc(SizeOf.get(PolyglotExtendedErrorInfo.class));
unmanagedErrorInfo.setErrorCode(errorCode.getCValue());
CCharPointerHolder holder = CTypeConversion.toCString(message);
CCharPointer value = holder.get();
unmanagedErrorInfo.setErrorMessage(value);
errorInfo.set(new ErrorInfoHolder(unmanagedErrorInfo, holder));
return errorCode;
}
use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class Util_jni method lookupAllHostAddr.
/* @formatter:on */
// Do not re-wrap long lines and comments: @formatter:off
@Substitute
@SuppressWarnings({ "static-method" })
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException, SocketException, InterruptedException {
CCharPointer hostname;
InetAddress[] ret = null;
int retLen = 0;
int error = 0;
Netdb.addrinfo hints = StackValue.get(SizeOf.get(Netdb.addrinfo.class));
Netdb.addrinfo res = WordFactory.nullPointer();
Netdb.addrinfoPointer resPtr = StackValue.get(SizeOf.get(Netdb.addrinfoPointer.class));
Netdb.addrinfo resNew = WordFactory.nullPointer();
if (host == null) {
throw new NullPointerException("host is null");
}
try (CCharPointerHolder hostPin = CTypeConversion.toCString(host)) {
// "hostname" is pinned through the end of the method.
hostname = hostPin.get();
// #ifdef MACOSX
if (IsDefined.MACOSX()) {
/*
* If we're looking up the local machine, attempt to get the address from
* getifaddrs. This ensures we get an IPv6 address for the local machine.
*/
ret = Util_java_net_Inet6AddressImpl.lookupIfLocalhost(hostname, true);
if (ret != null) {
return ret;
}
}
// #endif MACOSX
/* Try once, with our static buffer. */
LibC.memset(hints, WordFactory.signed(0), SizeOf.unsigned(Netdb.addrinfo.class));
hints.set_ai_flags(Netdb.AI_CANONNAME());
hints.set_ai_family(Socket.AF_UNSPEC());
// #endif
try {
// res needs cleanup at "cleanupAndReturn".
error = Netdb.getaddrinfo(hostname, WordFactory.nullPointer(), hints, resPtr);
if (error != 0) {
throw new UnknownHostException(host);
} else {
res = resPtr.read();
int i = 0;
int inetCount = 0;
int inet6Count = 0;
int inetIndex;
int inet6Index;
Netdb.addrinfo itr;
Netdb.addrinfo last = WordFactory.nullPointer();
Netdb.addrinfo iterator = res;
while (iterator.isNonNull()) {
boolean skip = false;
itr = resNew;
while (itr.isNonNull()) {
if ((iterator.ai_family() == itr.ai_family()) && (iterator.ai_addrlen() == itr.ai_addrlen())) {
if (itr.ai_family() == Socket.AF_INET()) {
NetinetIn.sockaddr_in addr1;
NetinetIn.sockaddr_in addr2;
addr1 = (NetinetIn.sockaddr_in) iterator.ai_addr();
addr2 = (NetinetIn.sockaddr_in) itr.ai_addr();
if (addr1.sin_addr().s_addr() == addr2.sin_addr().s_addr()) {
skip = true;
break;
}
} else {
NetinetIn.sockaddr_in6 addr1;
NetinetIn.sockaddr_in6 addr2;
addr1 = (NetinetIn.sockaddr_in6) iterator.ai_addr();
addr2 = (NetinetIn.sockaddr_in6) itr.ai_addr();
int t;
for (t = 0; t < 16; t++) {
if (addr1.sin6_addr().s6_addr().read(t) != addr2.sin6_addr().s6_addr().read(t)) {
break;
}
}
if (t < 16) {
itr = itr.ai_next();
continue;
} else {
skip = true;
break;
}
}
} else if ((iterator.ai_family() != Socket.AF_INET()) && (iterator.ai_family() != Socket.AF_INET6())) {
/* we can't handle other family types */
skip = true;
break;
}
itr = itr.ai_next();
}
if (!skip) {
Netdb.addrinfo next = LibC.malloc(SizeOf.unsigned(Netdb.addrinfo.class));
if (next.isNull()) {
throw new OutOfMemoryError("malloc failed");
}
LibC.memcpy(next, iterator, SizeOf.unsigned(Netdb.addrinfo.class));
next.set_ai_next(WordFactory.nullPointer());
if (resNew.isNull()) {
resNew = next;
} else {
last.set_ai_next(next);
}
last = next;
i++;
if (iterator.ai_family() == Socket.AF_INET()) {
inetCount++;
} else if (iterator.ai_family() == Socket.AF_INET6()) {
inet6Count++;
}
}
iterator = iterator.ai_next();
}
retLen = i;
iterator = resNew;
ret = new InetAddress[retLen];
if (Target_java_net_InetAddress.preferIPv6Address) {
/* AF_INET addresses will be offset by inet6Count */
inetIndex = inet6Count;
inet6Index = 0;
} else {
/* AF_INET6 addresses will be offset by inetCount */
inetIndex = 0;
inet6Index = inetCount;
}
while (iterator.isNonNull()) {
int ret1;
if (iterator.ai_family() == Socket.AF_INET()) {
Inet4Address iaObj = Util_java_net_Inet4Address.new_Inet4Address();
JavaNetNetUtil.setInetAddress_addr(iaObj, NetinetIn.ntohl(((NetinetIn.sockaddr_in) iterator.ai_addr()).sin_addr().s_addr()));
JavaNetNetUtil.setInetAddress_hostName(iaObj, host);
ret[inetIndex] = iaObj;
inetIndex++;
} else if (iterator.ai_family() == Socket.AF_INET6()) {
// 455 jint scope = 0;
int scope = 0;
// 457 jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
Inet6Address iaObj = Util_java_net_Inet6Address.new_Inet6Address();
// 458 if (IS_NULL(iaObj)) {
if (iaObj == null) {
// 459 ret = NULL;
ret = null;
// 460 goto cleanupAndReturn;
return ret;
}
// 462 ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
ret1 = JavaNetNetUtil.setInet6Address_ipaddress(iaObj, ((NetinetIn.sockaddr_in6) iterator.ai_addr()).sin6_addr().s6_addr());
// 463 if (!ret1) {
if (!CTypeConversion.toBoolean(ret1)) {
// 464 ret = NULL;
ret = null;
// 465 goto cleanupAndReturn;
return ret;
}
// 468 scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
scope = ((NetinetIn.sockaddr_in6) iterator.ai_addr()).sin6_scope_id();
// 469 if (scope != 0) { /* zero is default value, no need to set */
if (scope != 0) {
/* zero is default value, no need to set */
// 470 setInet6Address_scopeid(env, iaObj, scope);
JavaNetNetUtil.setInet6Address_scopeid(iaObj, scope);
}
// 472 setInetAddress_hostName(env, iaObj, host);
JavaNetNetUtil.setInetAddress_hostName(iaObj, host);
// 473 (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
ret[inet6Index] = iaObj;
// 474 inet6Index++;
inet6Index++;
}
iterator = iterator.ai_next();
}
}
} finally {
/* cleanupAndReturn: */
Netdb.addrinfo iterator;
Netdb.addrinfo tmp;
iterator = resNew;
while (iterator.isNonNull()) {
tmp = iterator;
iterator = iterator.ai_next();
LibC.free(tmp);
}
Netdb.freeaddrinfo(res);
}
// JNU_ReleaseStringPlatformChars(env, host, hostname)
// happens when I exit the CCharPointerHolder region.
}
return ret;
}
use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class PosixUtils method fileOpen.
static void fileOpen(String path, FileDescriptor fd, int flags) throws FileNotFoundException {
try (CCharPointerHolder pathPin = CTypeConversion.toCString(removeTrailingSlashes(path))) {
CCharPointer pathPtr = pathPin.get();
int handle = open(pathPtr, flags, 0666);
if (handle >= 0) {
Util_java_io_FileDescriptor.setFD(fd, handle);
} else {
throw new FileNotFoundException(path);
}
}
}
use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.
the class PosixUtils method setLocale.
public static String setLocale(int category, String locale) {
if (locale == null) {
CCharPointer cstrResult = Locale.setlocale(category, WordFactory.nullPointer());
return CTypeConversion.toJavaString(cstrResult);
}
try (CCharPointerHolder localePin = CTypeConversion.toCString(locale)) {
CCharPointer cstrLocale = localePin.get();
CCharPointer cstrResult = Locale.setlocale(category, cstrLocale);
return CTypeConversion.toJavaString(cstrResult);
}
}
Aggregations