Created
October 16, 2023 04:20
-
-
Save adrian154/b5293a0b1c15acb8f699927c457c9f6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "build/dev_bithole_bpf_BPF.h" | |
#include <sys/syscall.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
JNIEXPORT jint JNICALL Java_dev_bithole_bpf_BPF_syscall(JNIEnv *env, jobject this, jint command, jbyteArray attrIn) { | |
jbyte *attr = (*env)->GetByteArrayElements(env, attrIn, NULL); | |
int retval = syscall(SYS_bpf, command, (union bpf_attr *)attr, (*env)->GetArrayLength(env, attrIn)); | |
(*env)->ReleaseByteArrayElements(env, attrIn, attr, 0); | |
return retval; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.bithole.bpf; | |
import java.lang.UnsatisfiedLinkError; | |
import java.io.File; | |
import java.io.IOException; | |
import java.lang.ExceptionInInitializerError; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.FileOutputStream; | |
public class BPF { | |
static { | |
try { | |
System.loadLibrary("bpf"); | |
} catch(UnsatisfiedLinkError err) { | |
try { | |
File tempFile = File.createTempFile("bpf", ""); | |
tempFile.deleteOnExit(); | |
InputStream input = BPF.class.getResourceAsStream("/libbpf.so"); | |
try(OutputStream output = new FileOutputStream(tempFile)) { | |
input.transferTo(output); | |
} catch(IOException e) { | |
throw e; | |
} | |
System.load(tempFile.getPath()); | |
} catch(IOException e) { | |
throw new ExceptionInInitializerError(e); | |
} | |
} | |
} | |
public native int syscall(int command, byte[] attr); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javac -h build -d bin BPF.java | |
gcc -c bpf.c -o build/bpf.o -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -Wall -Wextra -Wpedantic | |
gcc -shared -o bin/libbpf.so build/bpf.o | |
jar cf bpf.jar -C bin . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment