Skip to content
Snippets Groups Projects
Commit 9aa2aba0 authored by Maciej Żenczykowski's avatar Maciej Żenczykowski Committed by Automerger Merge Worker
Browse files

bpf jni: add native_getProgramIdFromCgroup am: 85ac050b

parents d1e9b19c 85ac050b
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment