diff --git a/service/jni/onload.cpp b/service/jni/onload.cpp index 8e01260a22..f45dd3915a 100644 --- a/service/jni/onload.cpp +++ b/service/jni/onload.cpp @@ -26,7 +26,7 @@ int register_com_android_server_connectivity_ClatCoordinator(JNIEnv* env); int register_android_server_net_NetworkStatsFactory(JNIEnv* env); int register_android_server_net_NetworkStatsService(JNIEnv* env); int register_com_android_server_ServiceManagerWrapper(JNIEnv* env); -int register_com_android_net_module_util_TimerFdUtils(JNIEnv *env, +int register_com_android_net_module_util_ServiceConnectivityJni(JNIEnv *env, char const *class_name); extern "C" jint JNI_OnLoad(JavaVM* vm, void*) { @@ -58,9 +58,9 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void*) { } } - if (register_com_android_net_module_util_TimerFdUtils( + if (register_com_android_net_module_util_ServiceConnectivityJni( env, "android/net/connectivity/com/android/net/module/util/" - "TimerFdUtils") < 0) { + "ServiceConnectivityJni") < 0) { return JNI_ERR; } diff --git a/staticlibs/device/com/android/net/module/util/ServiceConnectivityJni.java b/staticlibs/device/com/android/net/module/util/ServiceConnectivityJni.java new file mode 100644 index 0000000000..f4529bd46e --- /dev/null +++ b/staticlibs/device/com/android/net/module/util/ServiceConnectivityJni.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.net.module.util; + +import java.io.IOException; + +/** + * Contains JNI functions for use in service-connectivity + */ +public class ServiceConnectivityJni { + static { + final String libName = JniUtil.getJniLibraryName(ServiceConnectivityJni.class.getPackage()); + if (libName.equals("android_net_connectivity_com_android_net_module_util_jni")) { + // This library is part of service-connectivity.jar when in the system server, + // so libservice-connectivity.so is the library to load. + System.loadLibrary("service-connectivity"); + } else { + System.loadLibrary(libName); + } + } + + /** + * Create a timerfd. + * + * @throws IOException if the timerfd creation is failed. + */ + public static native int createTimerFd() throws IOException; + + /** + * Set given time to the timerfd. + * + * @param timeMs target time + * @throws IOException if setting expiration time is failed. + */ + public static native void setTime(int fd, long timeMs) throws IOException; +} diff --git a/staticlibs/device/com/android/net/module/util/TimerFdUtils.java b/staticlibs/device/com/android/net/module/util/TimerFdUtils.java index f0de14255a..aff75f447e 100644 --- a/staticlibs/device/com/android/net/module/util/TimerFdUtils.java +++ b/staticlibs/device/com/android/net/module/util/TimerFdUtils.java @@ -25,40 +25,14 @@ import java.io.IOException; * Contains mostly timerfd functionality. */ public class TimerFdUtils { - static { - final String jniLibName = JniUtil.getJniLibraryName(TimerFdUtils.class.getPackage()); - if (jniLibName.equals("android_net_connectivity_com_android_net_module_util_jni")) { - // This library is part of service-connectivity.jar when in the system server, - // so libservice-connectivity.so is the library to load. - System.loadLibrary("service-connectivity"); - } else { - System.loadLibrary(jniLibName); - } - } - private static final String TAG = TimerFdUtils.class.getSimpleName(); - /** - * Create a timerfd. - * - * @throws IOException if the timerfd creation is failed. - */ - private static native int createTimerFd() throws IOException; - - /** - * Set given time to the timerfd. - * - * @param timeMs target time - * @throws IOException if setting expiration time is failed. - */ - private static native void setTime(int fd, long timeMs) throws IOException; - /** * Create a timerfd */ static int createTimerFileDescriptor() { try { - return createTimerFd(); + return ServiceConnectivityJni.createTimerFd(); } catch (IOException e) { Log.e(TAG, "createTimerFd failed", e); return -1; @@ -70,7 +44,7 @@ public class TimerFdUtils { */ static boolean setExpirationTime(int fd, long expirationTimeMs) { try { - setTime(fd, expirationTimeMs); + ServiceConnectivityJni.setTime(fd, expirationTimeMs); } catch (IOException e) { Log.e(TAG, "setExpirationTime failed", e); return false; diff --git a/staticlibs/native/timerfdutils/Android.bp b/staticlibs/native/timerfdutils/Android.bp index 939a2d2c2b..1f5542c12e 100644 --- a/staticlibs/native/timerfdutils/Android.bp +++ b/staticlibs/native/timerfdutils/Android.bp @@ -20,7 +20,7 @@ package { cc_library_static { name: "libnet_utils_device_common_timerfdjni", srcs: [ - "com_android_net_module_util_TimerFdUtils.cpp", + "com_android_net_module_util_ServiceConnectivityJni.cpp", ], header_libs: [ "jni_headers", diff --git a/staticlibs/native/timerfdutils/com_android_net_module_util_TimerFdUtils.cpp b/staticlibs/native/timerfdutils/com_android_net_module_util_ServiceConnectivityJni.cpp similarity index 84% rename from staticlibs/native/timerfdutils/com_android_net_module_util_TimerFdUtils.cpp rename to staticlibs/native/timerfdutils/com_android_net_module_util_ServiceConnectivityJni.cpp index c4c960d8a5..fdefd0e501 100644 --- a/staticlibs/native/timerfdutils/com_android_net_module_util_TimerFdUtils.cpp +++ b/staticlibs/native/timerfdutils/com_android_net_module_util_ServiceConnectivityJni.cpp @@ -33,7 +33,7 @@ namespace android { static jint -com_android_net_module_util_TimerFdUtils_createTimerFd(JNIEnv *env, +com_android_net_module_util_ServiceConnectivityJni_createTimerFd(JNIEnv *env, jclass clazz) { int tfd; tfd = timerfd_create(CLOCK_BOOTTIME, 0); @@ -44,7 +44,7 @@ com_android_net_module_util_TimerFdUtils_createTimerFd(JNIEnv *env, } static void -com_android_net_module_util_TimerFdUtils_setTime(JNIEnv *env, jclass clazz, +com_android_net_module_util_ServiceConnectivityJni_setTime(JNIEnv *env, jclass clazz, jint tfd, jlong milliseconds) { struct itimerspec new_value; new_value.it_value.tv_sec = milliseconds / MSEC_PER_SEC; @@ -66,12 +66,12 @@ com_android_net_module_util_TimerFdUtils_setTime(JNIEnv *env, jclass clazz, static const JNINativeMethod gMethods[] = { /* name, signature, funcPtr */ {"createTimerFd", "()I", - (void *)com_android_net_module_util_TimerFdUtils_createTimerFd}, + (void *)com_android_net_module_util_ServiceConnectivityJni_createTimerFd}, {"setTime", "(IJ)V", - (void *)com_android_net_module_util_TimerFdUtils_setTime}, + (void *)com_android_net_module_util_ServiceConnectivityJni_setTime}, }; -int register_com_android_net_module_util_TimerFdUtils(JNIEnv *env, +int register_com_android_net_module_util_ServiceConnectivityJni(JNIEnv *env, char const *class_name) { return jniRegisterNativeMethods(env, class_name, gMethods, NELEM(gMethods)); } diff --git a/staticlibs/tests/unit/jni/com_android_net_moduletests_util/onload.cpp b/staticlibs/tests/unit/jni/com_android_net_moduletests_util/onload.cpp index a0355401dc..af4810fb95 100644 --- a/staticlibs/tests/unit/jni/com_android_net_moduletests_util/onload.cpp +++ b/staticlibs/tests/unit/jni/com_android_net_moduletests_util/onload.cpp @@ -22,7 +22,7 @@ namespace android { -int register_com_android_net_module_util_TimerFdUtils(JNIEnv *env, +int register_com_android_net_module_util_ServiceConnectivityJni(JNIEnv *env, char const *class_name); extern "C" jint JNI_OnLoad(JavaVM *vm, void *) { @@ -32,8 +32,8 @@ extern "C" jint JNI_OnLoad(JavaVM *vm, void *) { return JNI_ERR; } - if (register_com_android_net_module_util_TimerFdUtils( - env, "com/android/net/moduletests/util/TimerFdUtils") < 0) + if (register_com_android_net_module_util_ServiceConnectivityJni( + env, "com/android/net/moduletests/util/ServiceConnectivityJni") < 0) return JNI_ERR; return JNI_VERSION_1_6; diff --git a/tests/unit/jni/android_net_frameworktests_util/onload.cpp b/tests/unit/jni/android_net_frameworktests_util/onload.cpp index a0ce4f8a8d..f70b04bf42 100644 --- a/tests/unit/jni/android_net_frameworktests_util/onload.cpp +++ b/tests/unit/jni/android_net_frameworktests_util/onload.cpp @@ -24,7 +24,7 @@ namespace android { int register_com_android_net_module_util_BpfMap(JNIEnv* env, char const* class_name); int register_com_android_net_module_util_TcUtils(JNIEnv* env, char const* class_name); -int register_com_android_net_module_util_TimerFdUtils(JNIEnv *env, +int register_com_android_net_module_util_ServiceConnectivityJni(JNIEnv *env, char const *class_name); extern "C" jint JNI_OnLoad(JavaVM* vm, void*) { @@ -40,8 +40,8 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void*) { if (register_com_android_net_module_util_TcUtils(env, "android/net/frameworktests/util/TcUtils") < 0) return JNI_ERR; - if (register_com_android_net_module_util_TimerFdUtils( - env, "android/net/frameworktests/util/TimerFdUtils") < 0) + if (register_com_android_net_module_util_ServiceConnectivityJni( + env, "android/net/frameworktests/util/ServiceConnectivityJni") < 0) return JNI_ERR; return JNI_VERSION_1_6;