22
33import static datadog .trace .api .telemetry .LogCollector .SEND_TELEMETRY ;
44
5- import com .sun .jna .Library ;
6- import com .sun .jna .Memory ;
75import com .sun .jna .Native ;
8- import com .sun .jna .NativeLong ;
9- import com .sun .jna .Pointer ;
106import datadog .environment .OperatingSystem ;
7+ import datadog .environment .SystemProperties ;
118import datadog .trace .core .servicediscovery .ForeignMemoryWriter ;
129import org .slf4j .Logger ;
1310import org .slf4j .LoggerFactory ;
1411
15- public class MemFDUnixWriter implements ForeignMemoryWriter {
12+ abstract class MemFDUnixWriter implements ForeignMemoryWriter {
1613 private static final Logger log = LoggerFactory .getLogger (MemFDUnixWriter .class );
1714
18- private interface LibC extends Library {
19- int syscall (int number , Object ... args );
15+ protected abstract long syscall (long number , String name , int flags );
2016
21- NativeLong write (int fd , Pointer buf , NativeLong count );
17+ protected abstract long write (int fd , byte [] payload );
2218
23- int fcntl (int fd , int cmd , int arg );
24- }
19+ protected abstract int fcntl (int fd , int cmd , int arg );
20+
21+ protected abstract int getLastError ();
2522
2623 // https://elixir.bootlin.com/linux/v6.17.1/source/include/uapi/linux/memfd.h#L8-L9
2724 private static final int MFD_CLOEXEC = 0x0001 ;
@@ -36,36 +33,33 @@ private interface LibC extends Library {
3633 private static final int F_SEAL_GROW = 0x0004 ;
3734
3835 @ Override
39- public void write (String fileName , byte [] payload ) {
40- final LibC libc = Native .load ("c" , LibC .class );
41-
36+ public final void write (String fileName , byte [] payload ) {
4237 OperatingSystem .Architecture arch = OperatingSystem .architecture ();
4338 int memfdSyscall = getMemfdSyscall (arch );
4439 if (memfdSyscall <= 0 ) {
45- log .debug (SEND_TELEMETRY , "service discovery not supported for arch={}" , arch );
40+ log .debug (
41+ SEND_TELEMETRY ,
42+ "service discovery not supported for arch={}" ,
43+ SystemProperties .get ("os.arch" ));
4644 return ;
4745 }
48- int memFd = libc . syscall (memfdSyscall , fileName , MFD_CLOEXEC | MFD_ALLOW_SEALING );
46+ int memFd = ( int ) syscall (memfdSyscall , fileName , MFD_CLOEXEC | MFD_ALLOW_SEALING );
4947 if (memFd < 0 ) {
50- log .warn ("{} memfd create failed, errno={}" , fileName , Native . getLastError ());
48+ log .warn ("{} memfd create failed, errno={}" , fileName , getLastError ());
5149 return ;
5250 }
5351
5452 log .debug ("{} memfd created (fd={})" , fileName , memFd );
5553
56- Memory buf = new Memory (payload .length );
57- buf .write (0 , payload , 0 , payload .length );
58-
59- NativeLong written = libc .write (memFd , buf , new NativeLong (payload .length ));
60- if (written .longValue () != payload .length ) {
54+ long written = write (memFd , payload );
55+ if (written != payload .length ) {
6156 log .warn ("write to {} memfd failed errno={}" , fileName , Native .getLastError ());
6257 return ;
6358 }
64- log .debug ("wrote {} bytes to memfd {}" , written . longValue () , memFd );
65- int returnCode = libc . fcntl (memFd , F_ADD_SEALS , F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL );
59+ log .debug ("wrote {} bytes to memfd {}" , written , memFd );
60+ int returnCode = fcntl (memFd , F_ADD_SEALS , F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL );
6661 if (returnCode == -1 ) {
6762 log .warn ("failed to add seal to {} memfd errno={}" , fileName , Native .getLastError ());
68- return ;
6963 }
7064 // memfd is not closed to keep it readable for the lifetime of the process.
7165 }
0 commit comments