bpf jni: add native_getProgramIdFromCgroup

Test: TreeHugger
Bug: 292156770
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Change-Id: Ib7b194b9cbf6519f9fbcfd1fcefcbe5d825f5c3a
This commit is contained in:
Maciej Żenczykowski
2023-10-04 01:12:14 +00:00
parent 5b2611d044
commit 85ac050b35
2 changed files with 27 additions and 0 deletions

View File

@@ -66,4 +66,6 @@ public class BpfUtils {
throws IOException;
private static native boolean native_detachSingleProgramFromCgroup(int type,
String programPath, String cgroupPath) throws IOException;
private static native int native_getProgramIdFromCgroup(int type, String cgroupPath)
throws IOException;
}

View File

@@ -110,6 +110,29 @@ static jboolean com_android_net_module_util_BpfUtil_detachSingleProgramFromCgrou
return true;
}
static jint com_android_net_module_util_BpfUtil_getProgramIdFromCgroup(JNIEnv *env,
jclass clazz, jint type, jstring cgroupPath) {
ScopedUtfChars dirPath(env, cgroupPath);
unique_fd cg_fd(open(dirPath.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC));
if (cg_fd == -1) {
jniThrowExceptionFmt(env, "java/io/IOException",
"Failed to open the cgroup directory %s: %s",
dirPath.c_str(), strerror(errno));
return -1;
}
int id = bpf::queryProgram(cg_fd, (bpf_attach_type) type);
if (id < 0) {
jniThrowExceptionFmt(env, "java/io/IOException",
"Failed to query bpf program %d at %s: %s",
type, dirPath.c_str(), strerror(errno));
return -1;
}
return id; // may return 0 meaning none
}
/*
* JNI registration.
*/
@@ -121,6 +144,8 @@ static const JNINativeMethod gMethods[] = {
(void*) com_android_net_module_util_BpfUtil_detachProgramFromCgroup },
{ "native_detachSingleProgramFromCgroup", "(ILjava/lang/String;Ljava/lang/String;)Z",
(void*) com_android_net_module_util_BpfUtil_detachSingleProgramFromCgroup },
{ "native_getProgramIdFromCgroup", "(ILjava/lang/String;)I",
(void*) com_android_net_module_util_BpfUtil_getProgramIdFromCgroup },
};
int register_com_android_net_module_util_BpfUtils(JNIEnv* env, char const* class_name) {