Search code examples
shared-memoryjnaglibc

What is the correct Java mapping of the shm_open function?


I'm trying to write some JNA code that accesses glibc, specifically functions defined in mmap.h.

I have tried to define it exactly as shown in man shm_open. The getuid() function call just before it works, but shm_open doesn't return.

I'm constrained to using JNA 4.4.0 and JNA Platform 3.4.0.

interface LibC extends Library {
    LibC INSTANCE = Native.loadLibrary("c", LibC.class);

    int shm_open(String name, int oFlag, int mode);
}
// ...
int fileDescriptor = LibC.INSTANCE.shm_open("/some_memory.123", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
// ...

I expect a file descriptor to be returned, but I get these exceptions:

  • When I load "libc.so.6": java.lang.UnsatisfiedLinkError: Error looking up function 'shm_open': /lib/x86_64-linux-gnu/libc.so.6: undefined symbol: shm_open
  • When I load "c": java.lang.UnsatisfiedLinkError: Error looking up function 'shm_open': /usr/lib/jvm/java-8-openjdk-amd64/bin/java: undefined symbol: shm_open

Solution

  • Well, I figured out the issue while writing the question.

    While getuid() and friends are defined in libc, shm_open and friends are defined in librt. I really should have realized that because the manpage for shm_open explicitly states "Link with -lrt", indicating it residing in the "rt" lib.

    In short: I need a new interface for LibRT alongside LibC, loaded with name rt.