Move TimerFdUtils JNI functionality into ServiceConnectivityJni

Test: build and boots
Change-Id: Idb9f8c9979aff43e6e5351d190329e823112e005
This commit is contained in:
Patrick Rohr 2025-01-16 11:17:19 -08:00
commit 4971556451
7 changed files with 67 additions and 43 deletions

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;

View file

@ -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",

View file

@ -0,0 +1,79 @@
/*
* Copyright (C) 2024 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.
*/
#include <errno.h>
#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/scoped_utf_chars.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#define MSEC_PER_SEC 1000
#define NSEC_PER_MSEC 1000000
namespace android {
static jint
com_android_net_module_util_ServiceConnectivityJni_createTimerFd(JNIEnv *env,
jclass clazz) {
int tfd;
tfd = timerfd_create(CLOCK_BOOTTIME, 0);
if (tfd == -1) {
jniThrowErrnoException(env, "createTimerFd", tfd);
}
return tfd;
}
static void
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;
new_value.it_value.tv_nsec = (milliseconds % MSEC_PER_SEC) * NSEC_PER_MSEC;
// Set the interval time to 0 because it's designed for repeated timer expirations after the
// initial expiration, which doesn't fit the current usage.
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_nsec = 0;
int ret = timerfd_settime(tfd, 0, &new_value, NULL);
if (ret == -1) {
jniThrowErrnoException(env, "setTime", ret);
}
}
/*
* JNI registration.
*/
static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{"createTimerFd", "()I",
(void *)com_android_net_module_util_ServiceConnectivityJni_createTimerFd},
{"setTime", "(IJ)V",
(void *)com_android_net_module_util_ServiceConnectivityJni_setTime},
};
int register_com_android_net_module_util_ServiceConnectivityJni(JNIEnv *env,
char const *class_name) {
return jniRegisterNativeMethods(env, class_name, gMethods, NELEM(gMethods));
}
}; // namespace android

View file

@ -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;

View file

@ -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;