Home > Software design >  JNA getrlimits() returns wrong values
JNA getrlimits() returns wrong values

Time:10-18

Is the JNA functionality correct for the following snippet? It seems to me that retrieving the values for Resource.RLIMIT_NOFILE returns the values of Resource.RLIMIT_NPROC and may be a bug?

Code:

public interface CLibrary extends LibCAPI, Library {
    CLibrary INSTANCE = Native.load("c", CLibrary.class);
}

public class Main {
    public static void main(String[] args) {
        final CLibrary INSTANCE = Native.load("c", CLibrary.class);
        final Resource.Rlimit rlimit = new Resource.Rlimit();
        INSTANCE.getrlimit(Resource.RLIMIT_NOFILE, rlimit);
        System.out.println(rlimit.rlim_max);
        System.out.println(rlimit.rlim_cur);
    }
}

Output:

8352
5568

The output corresponds to the maxprocess output of my macOs 12.6, but I would expect it to be the one of maxfiles.

❯ launchctl limit
    cpu         unlimited      unlimited
    filesize    unlimited      unlimited
    data        unlimited      unlimited
    stack       8388608        67104768
    core        0              unlimited
    rss         unlimited      unlimited
    memlock     unlimited      unlimited
    maxproc     5568           8352
    maxfiles    256            unlimited

CodePudding user response:

The JNA class you inherited is based on Linux LibC where RLIMIT_NOFILE is defined as 7:

# define RLIMIT_NOFILE      7   /* max number of open files */

While there is a lot of overlap between C headers on Linux and macOS, there's enough of a difference in XNU that you should always verify macOS headers separately.

On macOS these constants are apparently different; you've correctly noted it's returning RLIMIT_NPROC there instead, and you should use 8.

#define RLIMIT_NPROC    7               /* number of processes */
#define RLIMIT_NOFILE   8               /* number of open files */

On macOS you might consider using sysctl instead:

kern.maxfiles: 245760
kern.maxfilesperproc: 122880
kern.num_files: 8685
  • Related