22
33import dev .aikido .agent_api .helpers .logging .LogManager ;
44import dev .aikido .agent_api .helpers .logging .Logger ;
5+ import jnr .a64asm .INST_CODE ;
6+ import jnr .ffi .Library ;
7+ import jnr .ffi .LibraryLoader ;
58
69import java .io .BufferedReader ;
710import java .io .IOException ;
811import java .io .InputStreamReader ;
912import java .io .InterruptedIOException ;
13+ import java .lang .annotation .Native ;
1014
1115public final class GetBinaryPath {
1216 private GetBinaryPath () {}
1317 private static final Logger logger = LogManager .getLogger (GetBinaryPath .class );
1418
1519 public static String getPathForBinary () {
16- String fileName = getFileName ();
17- String aikidoDirectory = System .getProperty ("AIK_agent_dir" );
18- if (aikidoDirectory == null ) {
19- return null ;
20- }
21- return aikidoDirectory + "/binaries/" + fileName ;
20+ String fileName = getFileName ();
21+ String aikidoDirectory = System .getProperty ("AIK_agent_dir" );
22+ if (aikidoDirectory == null ) {
23+ return null ;
24+ }
25+ return aikidoDirectory + "/binaries/" + fileName ;
2226 }
2327 private static String getFileName () {
2428 String os = System .getProperty ("os.name" ).toLowerCase ();
@@ -35,34 +39,29 @@ private static String getFileName() {
3539 fileName .append ("x86_64-" ); // Default to x86-64
3640 }
3741
38- boolean isMusl = os .contains ("musl" ) || architecture .contains ("musl" );
39-
4042 if (os .contains ("win" )) {
4143 fileName .append ("pc-windows-gnu.dll" ); // Windows
4244 } else if (os .contains ("mac" )) {
4345 fileName .append ("apple-darwin.dylib" ); // macOS
44- } else { // os.contains("nix") || os.contains("nux")
46+ } else {
4547 // Default to linux
4648 fileName .append (String .format ("unknown-linux-%s.so" , getLibCVariant ()));
4749 }
4850 return fileName .toString ();
4951 }
5052
53+ public interface Libc {
54+ Libc INSTANCE = LibraryLoader .create (Libc .class ).load ("c" );
55+ String gnu_get_libc_version ();
56+ }
57+
5158 private static String getLibCVariant () {
59+ // gnu_get_libc_version only works for systems with gnu installed.
5260 try {
53- Process process = new ProcessBuilder ("uname" , "-o" ).start ();
54- BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
55- String line = reader .readLine ();
56- if (line != null ) {
57- if (line .toLowerCase ().contains ("gnu" )) {
58- return "gnu" ;
59- } else {
60- return "musl" ;
61- }
62- }
63- } catch (IOException e ) {
64- logger .trace (e );
61+ Libc .INSTANCE .gnu_get_libc_version ();
62+ } catch (UnsatisfiedLinkError e ) {
63+ return "musl" ;
6564 }
66- return "gnu" ; // Default to gnu
65+ return "gnu" ;
6766 }
6867}
0 commit comments