Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package datadog.trace.agent.tooling.servicediscovery;

import static datadog.trace.api.telemetry.LogCollector.SEND_TELEMETRY;

import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
Expand All @@ -13,7 +15,7 @@ public class MemFDUnixWriter implements ForeignMemoryWriter {
private static final Logger log = LoggerFactory.getLogger(MemFDUnixWriter.class);

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

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

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

int memFd = libc.memfd_create(fileName, MFD_CLOEXEC | MFD_ALLOW_SEALING);
String arch = System.getProperty("os.arch");
int memfdSyscall = getMemfdSyscall(arch);
if (memfdSyscall <= 0) {
log.debug(SEND_TELEMETRY, "service discovery not supported for arch={}", arch);
return;
}
int memFd = libc.syscall(memfdSyscall, fileName, MFD_CLOEXEC | MFD_ALLOW_SEALING);
if (memFd < 0) {
log.warn("{} memfd create failed, errno={}", fileName, Native.getLastError());
return;
Expand All @@ -60,4 +68,30 @@ public void write(String fileName, byte[] payload) {
}
// memfd is not closed to keep it readable for the lifetime of the process.
}

private static int getMemfdSyscall(String arch) {
switch (arch.toLowerCase()) {
case "x86_64":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could consolidate this by allowing the case statements to fallthrough.
case "x86_64":
case "x64":
case "amd64":
return 319;

Just a suggestion, I don't feel strongly about it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at these "magic numbers" makes every bone in my body shake, but because I can't think of a better idea that doesnt require a C shim 😅 or preprocessing of the source code 🫣 I'll stay quiet and say this lgtm 😜

case "x64":
case "amd64":
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/x86_64/bits/syscall.h.in#L320
return 319;
case "x386":
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/i386/bits/syscall.h.in#L356
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: same as above.

Also, does makes sense to have a x86 case ?

return 356;
case "aarch64":
case "arm64":
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/aarch64/bits/syscall.h.in#L264
return 279;
case "arm":
case "arm32":
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/arm/bits/syscall.h.in#L343
return 385;
case "ppc64":
// https://elixir.bootlin.com/musl/v1.2.5/source/arch/powerpc64/bits/syscall.h.in#L350
return 360;
default:
return -1;
}
}
}