Skip to content

Commit 5d2c2f9

Browse files
move to syscall
1 parent 11b824d commit 5d2c2f9

File tree

1 file changed

+40
-2
lines changed
  • dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/servicediscovery

1 file changed

+40
-2
lines changed

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/servicediscovery/MemFDUnixWriter.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class MemFDUnixWriter implements ForeignMemoryWriter {
1313
private static final Logger log = LoggerFactory.getLogger(MemFDUnixWriter.class);
1414

1515
private interface LibC extends Library {
16-
int memfd_create(String name, int flags);
16+
int syscall(int number, Object... args);
1717

1818
NativeLong write(int fd, Pointer buf, NativeLong count);
1919

@@ -36,7 +36,13 @@ private interface LibC extends Library {
3636
public void write(String fileName, byte[] payload) {
3737
final LibC libc = Native.load("c", LibC.class);
3838

39-
int memFd = libc.memfd_create(fileName, MFD_CLOEXEC | MFD_ALLOW_SEALING);
39+
String arch = System.getProperty("os.arch");
40+
int memfdSyscall = getMemfdSyscall(arch);
41+
if (memfdSyscall <= 0) {
42+
log.debug("service discovery not supported for arch={}", arch);
43+
return;
44+
}
45+
int memFd = libc.syscall(memfdSyscall, fileName, MFD_CLOEXEC | MFD_ALLOW_SEALING);
4046
if (memFd < 0) {
4147
log.warn("{} memfd create failed, errno={}", fileName, Native.getLastError());
4248
return;
@@ -60,4 +66,36 @@ public void write(String fileName, byte[] payload) {
6066
}
6167
// memfd is not closed to keep it readable for the lifetime of the process.
6268
}
69+
70+
private static int getMemfdSyscall(String arch) {
71+
switch (arch.toLowerCase()) {
72+
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/x86_64/bits/syscall.h.in#L320
73+
case "x86_64":
74+
return 319;
75+
case "x64":
76+
return 319;
77+
case "amd64":
78+
return 319;
79+
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/i386/bits/syscall.h.in#L356
80+
case "x386":
81+
return 356;
82+
case "86":
83+
return 356;
84+
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/aarch64/bits/syscall.h.in#L264
85+
case "aarch64":
86+
return 279;
87+
case "arm64":
88+
return 279;
89+
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/arm/bits/syscall.h.in#L343
90+
case "arm":
91+
return 385;
92+
case "arm32":
93+
return 385;
94+
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/powerpc64/bits/syscall.h.in#L350
95+
case "ppc64":
96+
return 360;
97+
default:
98+
return -1;
99+
}
100+
}
63101
}

0 commit comments

Comments
 (0)