Merge "Move #nativeWaitForService into CommonConnectivityJni" into main

This commit is contained in:
Patrick Rohr 2025-08-06 11:12:41 -07:00 • committed by Android (Google) Code Review
commit 2e342c6d9b
3 changed files with 53 additions and 115 deletions

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) {