11package datadog .trace .agent .tooling .servicediscovery ;
22
3+ import static datadog .trace .api .telemetry .LogCollector .SEND_TELEMETRY ;
4+
35import com .sun .jna .Library ;
46import com .sun .jna .Memory ;
57import com .sun .jna .Native ;
68import com .sun .jna .NativeLong ;
79import com .sun .jna .Pointer ;
10+ import datadog .environment .OperatingSystem ;
811import datadog .trace .core .servicediscovery .ForeignMemoryWriter ;
912import org .slf4j .Logger ;
1013import org .slf4j .LoggerFactory ;
@@ -13,7 +16,7 @@ public class MemFDUnixWriter implements ForeignMemoryWriter {
1316 private static final Logger log = LoggerFactory .getLogger (MemFDUnixWriter .class );
1417
1518 private interface LibC extends Library {
16- int memfd_create ( String name , int flags );
19+ int syscall ( int number , Object ... args );
1720
1821 NativeLong write (int fd , Pointer buf , NativeLong count );
1922
@@ -36,7 +39,13 @@ private interface LibC extends Library {
3639 public void write (String fileName , byte [] payload ) {
3740 final LibC libc = Native .load ("c" , LibC .class );
3841
39- int memFd = libc .memfd_create (fileName , MFD_CLOEXEC | MFD_ALLOW_SEALING );
42+ OperatingSystem .Architecture arch = OperatingSystem .architecture ();
43+ int memfdSyscall = getMemfdSyscall (arch );
44+ if (memfdSyscall <= 0 ) {
45+ log .debug (SEND_TELEMETRY , "service discovery not supported for arch={}" , arch );
46+ return ;
47+ }
48+ int memFd = libc .syscall (memfdSyscall , fileName , MFD_CLOEXEC | MFD_ALLOW_SEALING );
4049 if (memFd < 0 ) {
4150 log .warn ("{} memfd create failed, errno={}" , fileName , Native .getLastError ());
4251 return ;
@@ -60,4 +69,23 @@ public void write(String fileName, byte[] payload) {
6069 }
6170 // memfd is not closed to keep it readable for the lifetime of the process.
6271 }
72+
73+ private static int getMemfdSyscall (OperatingSystem .Architecture arch ) {
74+ switch (arch ) {
75+ case X64 :
76+ // https://github.com/torvalds/linux/blob/v6.17/arch/x86/entry/syscalls/syscall_64.tbl#L331
77+ return 319 ;
78+ case X86 :
79+ // https://github.com/torvalds/linux/blob/v6.17/arch/x86/entry/syscalls/syscall_32.tbl#L371
80+ return 356 ;
81+ case ARM64 :
82+ // https://github.com/torvalds/linux/blob/v6.17/scripts/syscall.tbl#L329
83+ return 279 ;
84+ case ARM :
85+ // https://github.com/torvalds/linux/blob/v6.17/arch/arm64/tools/syscall_32.tbl#L400
86+ return 385 ;
87+ default :
88+ return -1 ;
89+ }
90+ }
6391}
0 commit comments