Merge changes from topics "move-kerneltotag", "move-socket-tagging-to-mainline" am: ec7461850f am: 101bbfd2a2
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1964912 Change-Id: Iad7271aae7a5a4bafd3ba924546cb8ba6df8cd33
This commit is contained in:
@@ -195,7 +195,6 @@ cc_library_shared {
|
|||||||
"android_util_FileObserver.cpp",
|
"android_util_FileObserver.cpp",
|
||||||
"android/opengl/poly_clip.cpp", // TODO: .arm
|
"android/opengl/poly_clip.cpp", // TODO: .arm
|
||||||
"android/opengl/util.cpp",
|
"android/opengl/util.cpp",
|
||||||
"android_server_NetworkManagementSocketTagger.cpp",
|
|
||||||
"android_ddm_DdmHandleNativeHeap.cpp",
|
"android_ddm_DdmHandleNativeHeap.cpp",
|
||||||
"android_backup_BackupDataInput.cpp",
|
"android_backup_BackupDataInput.cpp",
|
||||||
"android_backup_BackupDataOutput.cpp",
|
"android_backup_BackupDataOutput.cpp",
|
||||||
@@ -299,6 +298,8 @@ cc_library_shared {
|
|||||||
"libdl_android",
|
"libdl_android",
|
||||||
"libtimeinstate",
|
"libtimeinstate",
|
||||||
"server_configurable_flags",
|
"server_configurable_flags",
|
||||||
|
// TODO: delete when ConnectivityT moves to APEX.
|
||||||
|
"libframework-connectivity-tiramisu-jni",
|
||||||
],
|
],
|
||||||
export_shared_lib_headers: [
|
export_shared_lib_headers: [
|
||||||
// our headers include libnativewindow's public headers
|
// our headers include libnativewindow's public headers
|
||||||
|
|||||||
@@ -164,7 +164,6 @@ extern int register_android_text_AndroidCharacter(JNIEnv *env);
|
|||||||
extern int register_android_text_Hyphenator(JNIEnv *env);
|
extern int register_android_text_Hyphenator(JNIEnv *env);
|
||||||
extern int register_android_opengl_classes(JNIEnv *env);
|
extern int register_android_opengl_classes(JNIEnv *env);
|
||||||
extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
|
extern int register_android_ddm_DdmHandleNativeHeap(JNIEnv *env);
|
||||||
extern int register_android_server_NetworkManagementSocketTagger(JNIEnv* env);
|
|
||||||
extern int register_android_backup_BackupDataInput(JNIEnv *env);
|
extern int register_android_backup_BackupDataInput(JNIEnv *env);
|
||||||
extern int register_android_backup_BackupDataOutput(JNIEnv *env);
|
extern int register_android_backup_BackupDataOutput(JNIEnv *env);
|
||||||
extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
|
extern int register_android_backup_FileBackupHelperBase(JNIEnv *env);
|
||||||
@@ -1618,7 +1617,6 @@ static const RegJNIRec gRegJNI[] = {
|
|||||||
REG_JNI(register_android_media_midi),
|
REG_JNI(register_android_media_midi),
|
||||||
|
|
||||||
REG_JNI(register_android_opengl_classes),
|
REG_JNI(register_android_opengl_classes),
|
||||||
REG_JNI(register_android_server_NetworkManagementSocketTagger),
|
|
||||||
REG_JNI(register_android_ddm_DdmHandleNativeHeap),
|
REG_JNI(register_android_ddm_DdmHandleNativeHeap),
|
||||||
REG_JNI(register_android_backup_BackupDataInput),
|
REG_JNI(register_android_backup_BackupDataInput),
|
||||||
REG_JNI(register_android_backup_BackupDataOutput),
|
REG_JNI(register_android_backup_BackupDataOutput),
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2011, 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#define LOG_TAG "NMST_QTagUidNative"
|
|
||||||
|
|
||||||
#include <android/multinetwork.h>
|
|
||||||
#include <cutils/qtaguid.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <nativehelper/JNIPlatformHelp.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <utils/Log.h>
|
|
||||||
#include <utils/misc.h>
|
|
||||||
|
|
||||||
#include "jni.h"
|
|
||||||
|
|
||||||
namespace android {
|
|
||||||
|
|
||||||
static jint tagSocketFd(JNIEnv* env, jclass, jobject fileDescriptor,
|
|
||||||
jint tagNum, jint uid) {
|
|
||||||
int userFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
|
|
||||||
|
|
||||||
if (env->ExceptionCheck()) {
|
|
||||||
ALOGE("Can't get FileDescriptor num");
|
|
||||||
return (jint)-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int res = android_tag_socket_with_uid(userFd, tagNum, uid);
|
|
||||||
if (res < 0) {
|
|
||||||
return (jint)-errno;
|
|
||||||
}
|
|
||||||
return (jint)res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static jint untagSocketFd(JNIEnv* env, jclass, jobject fileDescriptor) {
|
|
||||||
int userFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
|
|
||||||
|
|
||||||
if (env->ExceptionCheck()) {
|
|
||||||
ALOGE("Can't get FileDescriptor num");
|
|
||||||
return (jint)-1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int res = android_untag_socket(userFd);
|
|
||||||
if (res < 0) {
|
|
||||||
return (jint)-errno;
|
|
||||||
}
|
|
||||||
return (jint)res;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const JNINativeMethod gQTagUidMethods[] = {
|
|
||||||
{ "native_tagSocketFd", "(Ljava/io/FileDescriptor;II)I", (void*)tagSocketFd},
|
|
||||||
{ "native_untagSocketFd", "(Ljava/io/FileDescriptor;)I", (void*)untagSocketFd},
|
|
||||||
};
|
|
||||||
|
|
||||||
int register_android_server_NetworkManagementSocketTagger(JNIEnv* env) {
|
|
||||||
return jniRegisterNativeMethods(env, "com/android/server/NetworkManagementSocketTagger", gQTagUidMethods, NELEM(gQTagUidMethods));
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
@@ -39,7 +39,6 @@ filegroup {
|
|||||||
"src/android/net/TrafficStats.java",
|
"src/android/net/TrafficStats.java",
|
||||||
"src/android/net/UnderlyingNetworkInfo.*",
|
"src/android/net/UnderlyingNetworkInfo.*",
|
||||||
"src/android/net/netstats/**/*.*",
|
"src/android/net/netstats/**/*.*",
|
||||||
"src/com/android/server/NetworkManagementSocketTagger.java",
|
|
||||||
],
|
],
|
||||||
path: "src",
|
path: "src",
|
||||||
visibility: [
|
visibility: [
|
||||||
@@ -176,3 +175,34 @@ filegroup {
|
|||||||
"//packages/modules/Connectivity:__subpackages__",
|
"//packages/modules/Connectivity:__subpackages__",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cc_library_shared {
|
||||||
|
name: "libframework-connectivity-tiramisu-jni",
|
||||||
|
min_sdk_version: "30",
|
||||||
|
cflags: [
|
||||||
|
"-Wall",
|
||||||
|
"-Werror",
|
||||||
|
"-Wno-unused-parameter",
|
||||||
|
// Don't warn about S API usage even with
|
||||||
|
// min_sdk 30: the library is only loaded
|
||||||
|
// on S+ devices
|
||||||
|
"-Wno-unguarded-availability",
|
||||||
|
"-Wthread-safety",
|
||||||
|
],
|
||||||
|
srcs: [
|
||||||
|
"jni/android_net_TrafficStats.cpp",
|
||||||
|
"jni/onload.cpp",
|
||||||
|
],
|
||||||
|
shared_libs: [
|
||||||
|
"liblog",
|
||||||
|
],
|
||||||
|
static_libs: [
|
||||||
|
"libnativehelper_compat_libc++",
|
||||||
|
],
|
||||||
|
stl: "none",
|
||||||
|
apex_available: [
|
||||||
|
"com.android.tethering",
|
||||||
|
// TODO: remove when ConnectivityT moves to APEX.
|
||||||
|
"//apex_available:platform",
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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/file_descriptor_jni.h>
|
||||||
|
#include <android/multinetwork.h>
|
||||||
|
#include <nativehelper/JNIHelp.h>
|
||||||
|
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
static jint tagSocketFd(JNIEnv* env, jclass, jobject fileDescriptor, jint tag, jint uid) {
|
||||||
|
int fd = AFileDescriptor_getFd(env, fileDescriptor);
|
||||||
|
if (fd == -1) return -EBADF;
|
||||||
|
return android_tag_socket_with_uid(fd, tag, uid);
|
||||||
|
}
|
||||||
|
|
||||||
|
static jint untagSocketFd(JNIEnv* env, jclass, jobject fileDescriptor) {
|
||||||
|
int fd = AFileDescriptor_getFd(env, fileDescriptor);
|
||||||
|
if (fd == -1) return -EBADF;
|
||||||
|
return android_untag_socket(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const JNINativeMethod gMethods[] = {
|
||||||
|
/* name, signature, funcPtr */
|
||||||
|
{ "native_tagSocketFd", "(Ljava/io/FileDescriptor;II)I", (void*) tagSocketFd },
|
||||||
|
{ "native_untagSocketFd", "(Ljava/io/FileDescriptor;)I", (void*) untagSocketFd },
|
||||||
|
};
|
||||||
|
|
||||||
|
int register_android_net_TrafficStats(JNIEnv* env) {
|
||||||
|
return jniRegisterNativeMethods(env, "android/net/TrafficStats", gMethods, NELEM(gMethods));
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // namespace android
|
||||||
|
|
||||||
39
packages/ConnectivityT/framework-t/jni/onload.cpp
Normal file
39
packages/ConnectivityT/framework-t/jni/onload.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define LOG_TAG "FrameworkConnectivityJNI"
|
||||||
|
|
||||||
|
#include <log/log.h>
|
||||||
|
#include <nativehelper/JNIHelp.h>
|
||||||
|
|
||||||
|
namespace android {
|
||||||
|
|
||||||
|
int register_android_net_TrafficStats(JNIEnv* env);
|
||||||
|
|
||||||
|
extern "C" jint JNI_OnLoad(JavaVM* vm, void*) {
|
||||||
|
JNIEnv *env;
|
||||||
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
||||||
|
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "ERROR: GetEnv failed");
|
||||||
|
return JNI_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (register_android_net_TrafficStats(env) < 0) return JNI_ERR;
|
||||||
|
|
||||||
|
return JNI_VERSION_1_6;
|
||||||
|
}
|
||||||
|
|
||||||
|
}; // namespace android
|
||||||
|
|
||||||
@@ -31,12 +31,9 @@ import android.media.MediaPlayer;
|
|||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
|
import android.os.StrictMode;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.android.server.NetworkManagementSocketTagger;
|
|
||||||
|
|
||||||
import dalvik.system.SocketTagger;
|
|
||||||
|
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
@@ -56,6 +53,10 @@ import java.net.SocketException;
|
|||||||
* use {@link NetworkStatsManager} instead.
|
* use {@link NetworkStatsManager} instead.
|
||||||
*/
|
*/
|
||||||
public class TrafficStats {
|
public class TrafficStats {
|
||||||
|
static {
|
||||||
|
System.loadLibrary("framework-connectivity-tiramisu-jni");
|
||||||
|
}
|
||||||
|
|
||||||
private static final String TAG = TrafficStats.class.getSimpleName();
|
private static final String TAG = TrafficStats.class.getSimpleName();
|
||||||
/**
|
/**
|
||||||
* The return value to indicate that the device does not support the statistic.
|
* The return value to indicate that the device does not support the statistic.
|
||||||
@@ -232,9 +233,68 @@ public class TrafficStats {
|
|||||||
*/
|
*/
|
||||||
@SystemApi(client = MODULE_LIBRARIES)
|
@SystemApi(client = MODULE_LIBRARIES)
|
||||||
public static void attachSocketTagger() {
|
public static void attachSocketTagger() {
|
||||||
NetworkManagementSocketTagger.install();
|
dalvik.system.SocketTagger.set(new SocketTagger());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class SocketTagger extends dalvik.system.SocketTagger {
|
||||||
|
|
||||||
|
// TODO: set to false
|
||||||
|
private static final boolean LOGD = true;
|
||||||
|
|
||||||
|
SocketTagger() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tag(FileDescriptor fd) throws SocketException {
|
||||||
|
final UidTag tagInfo = sThreadUidTag.get();
|
||||||
|
if (LOGD) {
|
||||||
|
Log.d(TAG, "tagSocket(" + fd.getInt$() + ") with statsTag=0x"
|
||||||
|
+ Integer.toHexString(tagInfo.tag) + ", statsUid=" + tagInfo.uid);
|
||||||
|
}
|
||||||
|
if (tagInfo.tag == -1) {
|
||||||
|
StrictMode.noteUntaggedSocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tagInfo.tag == -1 && tagInfo.uid == -1) return;
|
||||||
|
final int errno = native_tagSocketFd(fd, tagInfo.tag, tagInfo.uid);
|
||||||
|
if (errno < 0) {
|
||||||
|
Log.i(TAG, "tagSocketFd(" + fd.getInt$() + ", "
|
||||||
|
+ tagInfo.tag + ", "
|
||||||
|
+ tagInfo.uid + ") failed with errno" + errno);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void untag(FileDescriptor fd) throws SocketException {
|
||||||
|
if (LOGD) {
|
||||||
|
Log.i(TAG, "untagSocket(" + fd.getInt$() + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
final UidTag tagInfo = sThreadUidTag.get();
|
||||||
|
if (tagInfo.tag == -1 && tagInfo.uid == -1) return;
|
||||||
|
|
||||||
|
final int errno = native_untagSocketFd(fd);
|
||||||
|
if (errno < 0) {
|
||||||
|
Log.w(TAG, "untagSocket(" + fd.getInt$() + ") failed with errno " + errno);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static native int native_tagSocketFd(FileDescriptor fd, int tag, int uid);
|
||||||
|
private static native int native_untagSocketFd(FileDescriptor fd);
|
||||||
|
|
||||||
|
private static class UidTag {
|
||||||
|
public int tag = -1;
|
||||||
|
public int uid = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ThreadLocal<UidTag> sThreadUidTag = new ThreadLocal<UidTag>() {
|
||||||
|
@Override
|
||||||
|
protected UidTag initialValue() {
|
||||||
|
return new UidTag();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set active tag to use when accounting {@link Socket} traffic originating
|
* Set active tag to use when accounting {@link Socket} traffic originating
|
||||||
* from the current thread. Only one active tag per thread is supported.
|
* from the current thread. Only one active tag per thread is supported.
|
||||||
@@ -249,7 +309,7 @@ public class TrafficStats {
|
|||||||
* @see #clearThreadStatsTag()
|
* @see #clearThreadStatsTag()
|
||||||
*/
|
*/
|
||||||
public static void setThreadStatsTag(int tag) {
|
public static void setThreadStatsTag(int tag) {
|
||||||
NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
|
getAndSetThreadStatsTag(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -267,7 +327,9 @@ public class TrafficStats {
|
|||||||
* restore any existing values after a nested operation is finished
|
* restore any existing values after a nested operation is finished
|
||||||
*/
|
*/
|
||||||
public static int getAndSetThreadStatsTag(int tag) {
|
public static int getAndSetThreadStatsTag(int tag) {
|
||||||
return NetworkManagementSocketTagger.setThreadSocketStatsTag(tag);
|
final int old = sThreadUidTag.get().tag;
|
||||||
|
sThreadUidTag.get().tag = tag;
|
||||||
|
return old;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -327,7 +389,7 @@ public class TrafficStats {
|
|||||||
* @see #setThreadStatsTag(int)
|
* @see #setThreadStatsTag(int)
|
||||||
*/
|
*/
|
||||||
public static int getThreadStatsTag() {
|
public static int getThreadStatsTag() {
|
||||||
return NetworkManagementSocketTagger.getThreadSocketStatsTag();
|
return sThreadUidTag.get().tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -337,7 +399,7 @@ public class TrafficStats {
|
|||||||
* @see #setThreadStatsTag(int)
|
* @see #setThreadStatsTag(int)
|
||||||
*/
|
*/
|
||||||
public static void clearThreadStatsTag() {
|
public static void clearThreadStatsTag() {
|
||||||
NetworkManagementSocketTagger.setThreadSocketStatsTag(-1);
|
sThreadUidTag.get().tag = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -357,7 +419,7 @@ public class TrafficStats {
|
|||||||
*/
|
*/
|
||||||
@SuppressLint("RequiresPermission")
|
@SuppressLint("RequiresPermission")
|
||||||
public static void setThreadStatsUid(int uid) {
|
public static void setThreadStatsUid(int uid) {
|
||||||
NetworkManagementSocketTagger.setThreadSocketStatsUid(uid);
|
sThreadUidTag.get().uid = uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -368,7 +430,7 @@ public class TrafficStats {
|
|||||||
* @see #setThreadStatsUid(int)
|
* @see #setThreadStatsUid(int)
|
||||||
*/
|
*/
|
||||||
public static int getThreadStatsUid() {
|
public static int getThreadStatsUid() {
|
||||||
return NetworkManagementSocketTagger.getThreadSocketStatsUid();
|
return sThreadUidTag.get().uid;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -395,7 +457,7 @@ public class TrafficStats {
|
|||||||
*/
|
*/
|
||||||
@SuppressLint("RequiresPermission")
|
@SuppressLint("RequiresPermission")
|
||||||
public static void clearThreadStatsUid() {
|
public static void clearThreadStatsUid() {
|
||||||
NetworkManagementSocketTagger.setThreadSocketStatsUid(-1);
|
setThreadStatsUid(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user