Move #nativeWaitForService into CommonConnectivityJni

AServiceManager_waitForService sytem APIs currently only callable
via dlsym(): b/376759605. When built with sdk_version current,
the NDK stubs for "sdk" variants only include NDK APIs,
excluding #systemapi APIs.

Flag: EXEMPT Mechanical refactors
Test: atest ServiceManagerWrapperIntegrationTest
Change-Id: I48f690d771c7905e79e463617ded772f753cc854
This commit is contained in:
Mark Chien 2025-08-04 10:56:38 +00:00 • committed by Patrick Rohr
commit f250ade6fa
9 changed files with 53 additions and 115 deletions

View file

@ -107,6 +107,7 @@ cc_library_shared {
],
min_sdk_version: "30",
shared_libs: [
"libbinder_ndk",
"liblog",
"libnativehelper",
],
@ -136,7 +137,6 @@ cc_library_shared {
srcs: [
":services.connectivity-netstats-jni-sources",
"jni/com_android_server_connectivity_ClatCoordinator.cpp",
"jni/com_android_server_ServiceManagerWrapper.cpp",
"jni/onload.cpp",
],
header_libs: [
@ -154,7 +154,6 @@ cc_library_shared {
],
shared_libs: [
"libbase",
"libbinder_ndk",
"libcutils",
"libnetdutils",
"liblog",

View file

@ -1,54 +0,0 @@
/*
* Copyright (C) 2023 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 <android/binder_ibinder_jni.h>
#include <android/binder_manager.h>
#include <jni.h>
#include "nativehelper/JNIHelp.h"
#include <nativehelper/ScopedUtfChars.h>
#include <private/android_filesystem_config.h>
namespace android {
static jobject com_android_server_ServiceManagerWrapper_waitForService(
JNIEnv* env, jobject clazz, jstring serviceName) {
ScopedUtfChars name(env, serviceName);
// AServiceManager_waitForService is available on only 31+, but it's still safe for Thread
// service because it's enabled on only 34+
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"
return AIBinder_toJavaBinder(env, AServiceManager_waitForService(name.c_str()));
#pragma clang diagnostic pop
}
/*
* JNI registration.
*/
static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{"nativeWaitForService",
"(Ljava/lang/String;)Landroid/os/IBinder;",
(void*)com_android_server_ServiceManagerWrapper_waitForService},
};
int register_com_android_server_ServiceManagerWrapper(JNIEnv* env) {
return jniRegisterNativeMethods(env,
"android/net/connectivity/com/android/server/ServiceManagerWrapper",
gMethods, NELEM(gMethods));
}
}; // namespace android

View file

@ -21,10 +21,9 @@
namespace android {
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_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_net_module_util_ServiceConnectivityJni(JNIEnv *env,
char const *class_name);
@ -35,10 +34,6 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, void*) {
return JNI_ERR;
}
if (register_com_android_server_ServiceManagerWrapper(env) < 0) {
return JNI_ERR;
}
if (android::modules::sdklevel::IsAtLeastT()) {
if (register_com_android_server_connectivity_ClatCoordinator(env) < 0) {
return JNI_ERR;

View file

@ -1,46 +0,0 @@
/*
* Copyright (C) 2023 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.server;
import android.annotation.RequiresApi;
import android.os.IBinder;
import android.os.Build;
import android.os.ServiceManager;
/** Provides a way to access {@link ServiceManager#waitForService} API. */
@RequiresApi(Build.VERSION_CODES.S)
public final class ServiceManagerWrapper {
static {
System.loadLibrary("service-connectivity");
}
private ServiceManagerWrapper() {}
/**
* Returns the specified service from the service manager.
*
* If the service is not running, service manager will attempt to start it, and this function
* will wait for it to be ready.
*
* @return {@code null} only if there are permission problems or fatal errors
*/
public static IBinder waitForService(String serviceName) {
return nativeWaitForService(serviceName);
}
private static native IBinder nativeWaitForService(String serviceName);
}

View file

@ -16,6 +16,9 @@
package com.android.net.module.util;
import android.annotation.RequiresApi;
import android.os.Build;
import android.os.IBinder;
import android.system.ErrnoException;
/**
@ -48,4 +51,15 @@ public class CommonConnectivityJni {
* @throws ErrnoException if setting expiration time is failed.
*/
public static native void setTimerFdTime(int fd, long timeMs) throws ErrnoException;
/**
* Returns the specified service from the service manager.
*
* If the service is not running, service manager will attempt to start it, and this function
* will wait for it to be ready.
*
* @return {@code null} only if there are permission problems or fatal errors
*/
@RequiresApi(Build.VERSION_CODES.S)
public static native IBinder waitForService(String serviceName);
}

View file

@ -26,6 +26,7 @@ cc_library_static {
"jni_headers",
],
shared_libs: [
"libbinder_ndk",
"liblog",
"libnativehelper_compat_libc++",
],

View file

@ -13,12 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "CommonConnectivityJni"
#include <dlfcn.h>
#include <sys/timerfd.h>
#include <android/binder_ibinder_jni.h>
#include <android/log.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h>
#define MSEC_PER_SEC 1000
#define NSEC_PER_MSEC 1000000
#define LIBBINDER_NDK_PATH "libbinder_ndk.so"
namespace android {
@ -51,6 +58,26 @@ static void setTimerFdTime(JNIEnv *env, jclass clazz, jint tfd,
}
}
using waitForServiceFunc = AIBinder *(*)(const char *instance);
static jobject ServiceManagerWrapper_waitForService(JNIEnv *env, jobject clazz,
jstring serviceName) {
// AServiceManager_waitForService system APIs currently only callable via
// dlsym(): b/376759605. When built with sdk_version current, the NDK stubs
// for "sdk" variants only include NDK APIs, excluding #systemapi APIs.
// TODO: AServiceManager_waitForService requires SDK version 31.
// Remove this after dropping mainline support for R.
auto waitForService = (waitForServiceFunc)dlsym(RTLD_DEFAULT, "AServiceManager_waitForService");
if (waitForService == nullptr) {
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
"Failed to look up AServiceManager_waitForService");
return nullptr;
}
ScopedUtfChars name(env, serviceName);
return AIBinder_toJavaBinder(env, waitForService(name.c_str()));
}
//------------------------------------------------------------------------------
/*
@ -60,6 +87,8 @@ static const JNINativeMethod gMethods[] = {
/* name, signature, funcPtr */
{"createTimerFd", "()I", (void *)createTimerFd},
{"setTimerFdTime", "(IJ)V", (void *)setTimerFdTime},
{"waitForService", "(Ljava/lang/String;)Landroid/os/IBinder;",
(void *)ServiceManagerWrapper_waitForService},
};
int register_CommonConnectivityJni(JNIEnv *env, char const *class_name) {

View file

@ -18,7 +18,7 @@ package com.android.server.net.integrationtests
import android.content.Context
import android.os.Build
import com.android.server.ServiceManagerWrapper
import com.android.net.module.util.CommonConnectivityJni
import com.android.testutils.ConnectivityModuleTest
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
import com.android.testutils.DevSdkIgnoreRunner
@ -26,13 +26,13 @@ import kotlin.test.assertNotNull
import org.junit.Test
import org.junit.runner.RunWith
/** Integration tests for {@link ServiceManagerWrapper}. */
/** Integration tests for {@link CommonConnectivityJni}. */
@RunWith(DevSdkIgnoreRunner::class)
@IgnoreUpTo(Build.VERSION_CODES.S)
@ConnectivityModuleTest
class ServiceManagerWrapperIntegrationTest {
@Test
fun testWaitForService_successFullyRetrievesConnectivityServiceBinder() {
assertNotNull(ServiceManagerWrapper.waitForService(Context.CONNECTIVITY_SERVICE))
assertNotNull(CommonConnectivityJni.waitForService(Context.CONNECTIVITY_SERVICE))
}
}

View file

@ -123,10 +123,10 @@ import android.util.SparseArray;
import com.android.connectivity.resources.R;
import com.android.internal.annotations.VisibleForTesting;
import com.android.net.module.util.CommonConnectivityJni;
import com.android.net.module.util.IIpv4PrefixRequest;
import com.android.net.module.util.RoutingCoordinatorManager;
import com.android.net.module.util.SharedLog;
import com.android.server.ServiceManagerWrapper;
import com.android.server.connectivity.ConnectivityResources;
import com.android.server.connectivity.MockableSystemProperties;
import com.android.server.thread.openthread.BackboneRouterState;
@ -292,7 +292,7 @@ final class ThreadNetworkControllerService extends IThreadNetworkController.Stub
handler,
new MockableSystemProperties(),
networkProvider,
() -> IOtDaemon.Stub.asInterface(ServiceManagerWrapper.waitForService("ot_daemon")),
() -> IOtDaemon.Stub.asInterface(CommonConnectivityJni.waitForService("ot_daemon")),
connectivityManager,
routingCoordinatorManager,
new TunInterfaceController(TUN_IF_NAME),