Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 73ca3ed1 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN Committed by Automerger Merge Worker
Browse files

Merge "Use jniThrowErrnoException from JNIHelp" am: eff6bb9c am: 51a05fdd

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1703087

Change-Id: I52864a1dff894d827e97332ce9ddd506ae297270
parents 1d78211f 51a05fdd
Loading
Loading
Loading
Loading
+8 −36
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@


#include <DnsProxydProtocol.h> // NETID_USE_LOCAL_NAMESERVERS
#include <DnsProxydProtocol.h> // NETID_USE_LOCAL_NAMESERVERS
#include <cutils/properties.h>
#include <cutils/properties.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/JNIPlatformHelp.h>
#include <nativehelper/JNIPlatformHelp.h>
#include <nativehelper/ScopedLocalRef.h>
#include <nativehelper/ScopedLocalRef.h>
#include <utils/Log.h>
#include <utils/Log.h>
@@ -57,14 +58,6 @@ static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
    return clazz;
    return clazz;
}
}


static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
                                         const char* method_signature) {
    jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
    LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s with signature %s", method_name,
                        method_signature);
    return res;
}

template <typename T>
template <typename T>
static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
    jobject res = env->NewGlobalRef(in);
    jobject res = env->NewGlobalRef(in);
@@ -72,27 +65,6 @@ static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
    return static_cast<T>(res);
    return static_cast<T>(res);
}
}


static void throwErrnoException(JNIEnv* env, const char* functionName, int error) {
    ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
    if (detailMessage.get() == NULL) {
        // Not really much we can do here. We're probably dead in the water,
        // but let's try to stumble on...
        env->ExceptionClear();
    }
    static jclass errnoExceptionClass =
            MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/system/ErrnoException"));

    static jmethodID errnoExceptionCtor =
            GetMethodIDOrDie(env, errnoExceptionClass,
            "<init>", "(Ljava/lang/String;I)V");

    jobject exception = env->NewObject(errnoExceptionClass,
                                       errnoExceptionCtor,
                                       detailMessage.get(),
                                       error);
    env->Throw(reinterpret_cast<jthrowable>(exception));
}

static void android_net_utils_attachDropAllBPFFilter(JNIEnv *env, jobject clazz, jobject javaFd)
static void android_net_utils_attachDropAllBPFFilter(JNIEnv *env, jobject clazz, jobject javaFd)
{
{
    struct sock_filter filter_code[] = {
    struct sock_filter filter_code[] = {
@@ -165,7 +137,7 @@ static jobject android_net_utils_resNetworkQuery(JNIEnv *env, jobject thiz, jint
    int fd = resNetworkQuery(netId, queryname.data(), ns_class, ns_type, flags);
    int fd = resNetworkQuery(netId, queryname.data(), ns_class, ns_type, flags);


    if (fd < 0) {
    if (fd < 0) {
        throwErrnoException(env, "resNetworkQuery", -fd);
        jniThrowErrnoException(env, "resNetworkQuery", -fd);
        return nullptr;
        return nullptr;
    }
    }


@@ -180,7 +152,7 @@ static jobject android_net_utils_resNetworkSend(JNIEnv *env, jobject thiz, jint
    int fd = resNetworkSend(netId, data, msgLen, flags);
    int fd = resNetworkSend(netId, data, msgLen, flags);


    if (fd < 0) {
    if (fd < 0) {
        throwErrnoException(env, "resNetworkSend", -fd);
        jniThrowErrnoException(env, "resNetworkSend", -fd);
        return nullptr;
        return nullptr;
    }
    }


@@ -195,13 +167,13 @@ static jobject android_net_utils_resNetworkResult(JNIEnv *env, jobject thiz, job
    int res = resNetworkResult(fd, &rcode, buf.data(), MAXPACKETSIZE);
    int res = resNetworkResult(fd, &rcode, buf.data(), MAXPACKETSIZE);
    jniSetFileDescriptorOfFD(env, javaFd, -1);
    jniSetFileDescriptorOfFD(env, javaFd, -1);
    if (res < 0) {
    if (res < 0) {
        throwErrnoException(env, "resNetworkResult", -res);
        jniThrowErrnoException(env, "resNetworkResult", -res);
        return nullptr;
        return nullptr;
    }
    }


    jbyteArray answer = env->NewByteArray(res);
    jbyteArray answer = env->NewByteArray(res);
    if (answer == nullptr) {
    if (answer == nullptr) {
        throwErrnoException(env, "resNetworkResult", ENOMEM);
        jniThrowErrnoException(env, "resNetworkResult", ENOMEM);
        return nullptr;
        return nullptr;
    } else {
    } else {
        env->SetByteArrayRegion(answer, 0, res,
        env->SetByteArrayRegion(answer, 0, res,
@@ -223,7 +195,7 @@ static void android_net_utils_resNetworkCancel(JNIEnv *env, jobject thiz, jobjec
static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) {
static jobject android_net_utils_getDnsNetwork(JNIEnv *env, jobject thiz) {
    unsigned dnsNetId = 0;
    unsigned dnsNetId = 0;
    if (int res = getNetworkForDns(&dnsNetId) < 0) {
    if (int res = getNetworkForDns(&dnsNetId) < 0) {
        throwErrnoException(env, "getDnsNetId", -res);
        jniThrowErrnoException(env, "getDnsNetId", -res);
        return nullptr;
        return nullptr;
    }
    }
    bool privateDnsBypass = dnsNetId & NETID_USE_LOCAL_NAMESERVERS;
    bool privateDnsBypass = dnsNetId & NETID_USE_LOCAL_NAMESERVERS;
@@ -248,7 +220,7 @@ static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, j
    // Obtain the parameters of the TCP repair window.
    // Obtain the parameters of the TCP repair window.
    int rc = getsockopt(fd, IPPROTO_TCP, TCP_REPAIR_WINDOW, &trw, &size);
    int rc = getsockopt(fd, IPPROTO_TCP, TCP_REPAIR_WINDOW, &trw, &size);
    if (rc == -1) {
    if (rc == -1) {
        throwErrnoException(env, "getsockopt : TCP_REPAIR_WINDOW", errno);
        jniThrowErrnoException(env, "getsockopt : TCP_REPAIR_WINDOW", errno);
        return NULL;
        return NULL;
    }
    }


@@ -259,7 +231,7 @@ static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, j
    // should be applied to the window size.
    // should be applied to the window size.
    rc = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpinfo, &tcpinfo_size);
    rc = getsockopt(fd, IPPROTO_TCP, TCP_INFO, &tcpinfo, &tcpinfo_size);
    if (rc == -1) {
    if (rc == -1) {
        throwErrnoException(env, "getsockopt : TCP_INFO", errno);
        jniThrowErrnoException(env, "getsockopt : TCP_INFO", errno);
        return NULL;
        return NULL;
    }
    }