Class LinuxLibcFunctions

java.lang.Object
oshi.ffm.ForeignFunctions
oshi.ffm.linux.LinuxLibcFunctions

public final class LinuxLibcFunctions extends ForeignFunctions
FFM bindings for Linux libc functions used by OSHI.

Covers: getpid, gettid, syscall, getloadavg, sysinfo, statvfs, gethostname, getaddrinfo/freeaddrinfo/gai_strerror, and getrlimit.

  • Field Details

    • RLIMIT_NOFILE

      public static final int RLIMIT_NOFILE
      See Also:
    • AI_CANONNAME

      public static final int AI_CANONNAME
      See Also:
    • SYSINFO_LAYOUT

      public static final StructLayout SYSINFO_LAYOUT
      struct sysinfo layout (64-bit Linux).
        long    uptime       (8)
        long[3] loads        (24)
        long    totalram     (8)
        long    freeram      (8)
        long    sharedram    (8)
        long    bufferram    (8)
        long    totalswap    (8)
        long    freeswap     (8)
        short   procs        (2)
        ... padding/high memory fields follow
      
    • STATVFS_LAYOUT

      public static final StructLayout STATVFS_LAYOUT
      struct statvfs layout (64-bit Linux, no _f_unused padding field).
    • RLIMIT_LAYOUT

      public static final StructLayout RLIMIT_LAYOUT
      struct rlimit layout: two unsigned long fields.
    • ADDRINFO_LAYOUT

      public static final StructLayout ADDRINFO_LAYOUT
      struct addrinfo layout (64-bit Linux).
        int     ai_flags     (4)
        int     ai_family    (4)
        int     ai_socktype  (4)
        int     ai_protocol  (4)
        long    ai_addrlen   (8)   -- socklen_t padded to 8
        ptr     ai_addr      (8)
        ptr     ai_canonname (8)   -- offset 32
        ptr     ai_next      (8)
      
    • LOGIN_PROCESS

      public static final short LOGIN_PROCESS
      utmpx entry type: session leader of a logged in user.
      See Also:
    • USER_PROCESS

      public static final short USER_PROCESS
      utmpx entry type: normal process.
      See Also:
    • UTMPX_LAYOUT

      public static final StructLayout UTMPX_LAYOUT
      struct utmpx layout (64-bit Linux).
        short   ut_type      (2)
        pad                  (2)
        int     ut_pid       (4)
        char    ut_line[32]  (32)
        char    ut_id[4]     (4)
        char    ut_user[32]  (32)
        char    ut_host[256] (256)
        short[2] ut_exit     (4)
        int     ut_session   (4)
        int     tv_sec       (4)
        int     tv_usec      (4)
        int[4]  ut_addr_v6   (16)
        char[20] reserved    (20)
        total = 384
      
  • Method Details

    • hasGettid

      public static boolean hasGettid()
      Returns whether gettid() is directly available in libc.
      Returns:
      true if gettid is available
    • setutxent

      public static void setutxent() throws Throwable
      Calls setutxent() to rewind the utmpx database.
      Throws:
      Throwable - if the native call fails
    • getutxent

      public static MemorySegment getutxent() throws Throwable
      Calls getutxent() to read the next entry from the utmpx database.
      Returns:
      a pointer to the utmpx structure, or null if no more entries
      Throws:
      Throwable - if the native call fails
    • endutxent

      public static void endutxent() throws Throwable
      Calls endutxent() to close the utmpx database.
      Throws:
      Throwable - if the native call fails
    • utmpxType

      public static short utmpxType(MemorySegment ut)
      Reads ut_type from a utmpx segment.
      Parameters:
      ut - segment populated by getutxent()
      Returns:
      the entry type
    • utmpxUser

      public static String utmpxUser(MemorySegment ut)
      Reads ut_user from a utmpx segment.
      Parameters:
      ut - segment populated by getutxent()
      Returns:
      the username string
    • utmpxLine

      public static String utmpxLine(MemorySegment ut)
      Reads ut_line from a utmpx segment.
      Parameters:
      ut - segment populated by getutxent()
      Returns:
      the device name string
    • utmpxAddrV6

      public static int[] utmpxAddrV6(MemorySegment ut)
      Reads ut_addr_v6 from a utmpx segment as a 4-element int array.
      Parameters:
      ut - segment populated by getutxent()
      Returns:
      the IPv6/IPv4 address as 4 ints
    • utmpxLoginTime

      public static long utmpxLoginTime(MemorySegment ut)
      Reads the login time from a utmpx segment as epoch milliseconds.
      Parameters:
      ut - segment populated by getutxent()
      Returns:
      login time in milliseconds since epoch
    • getpid

      public static int getpid() throws Throwable
      Calls getpid().
      Returns:
      the process ID of the calling process
      Throws:
      Throwable - if the native call fails
    • gettid

      public static int gettid() throws Throwable
      Calls gettid() directly. Only valid if hasGettid() is true.
      Returns:
      the thread ID of the calling thread
      Throws:
      Throwable - if the native call fails
    • syscallGettid

      public static long syscallGettid() throws Throwable
      Calls syscall(SYS_GETTID).
      Returns:
      the thread ID of the calling thread
      Throws:
      Throwable - if the native call fails
    • getloadavg

      public static int getloadavg(MemorySegment loadavg, int nelem) throws Throwable
      Calls getloadavg(double[] loadavg, int nelem).
      Parameters:
      loadavg - pre-allocated segment of nelem doubles
      nelem - number of load average values to retrieve (1–3)
      Returns:
      number of samples set, or -1 on error
      Throws:
      Throwable
    • sysinfo

      public static int sysinfo(MemorySegment info) throws Throwable
      Calls sysinfo(struct sysinfo *info).
      Parameters:
      info - segment allocated with SYSINFO_LAYOUT
      Returns:
      0 on success, -1 on error
      Throws:
      Throwable
    • sysinfoProcs

      public static int sysinfoProcs(MemorySegment info)
      Reads the procs field from a sysinfo segment.
      Parameters:
      info - segment populated by sysinfo(MemorySegment)
      Returns:
      number of current processes
    • statvfs

      public static int statvfs(MemorySegment path, MemorySegment buf) throws Throwable
      Calls statvfs(const char *path, struct statvfs *buf).
      Parameters:
      path - path segment (null-terminated UTF-8)
      buf - segment allocated with STATVFS_LAYOUT
      Returns:
      0 on success, -1 on error
      Throws:
      Throwable
    • statvfsFrsize

      public static long statvfsFrsize(MemorySegment buf)
      Reads f_frsize from a statvfs segment.
      Parameters:
      buf - segment populated by statvfs(MemorySegment, MemorySegment)
      Returns:
      fragment size in bytes
    • statvfsBlocks

      public static long statvfsBlocks(MemorySegment buf)
      Reads f_blocks from a statvfs segment.
      Parameters:
      buf - segment populated by statvfs(MemorySegment, MemorySegment)
      Returns:
      total data blocks in filesystem
    • statvfsBfree

      public static long statvfsBfree(MemorySegment buf)
      Reads f_bfree from a statvfs segment.
      Parameters:
      buf - segment populated by statvfs(MemorySegment, MemorySegment)
      Returns:
      free blocks in filesystem
    • statvfsBavail

      public static long statvfsBavail(MemorySegment buf)
      Reads f_bavail from a statvfs segment.
      Parameters:
      buf - segment populated by statvfs(MemorySegment, MemorySegment)
      Returns:
      free blocks available to unprivileged users
    • statvfsFiles

      public static long statvfsFiles(MemorySegment buf)
      Reads f_files from a statvfs segment.
      Parameters:
      buf - segment populated by statvfs(MemorySegment, MemorySegment)
      Returns:
      total file nodes in filesystem
    • statvfsFfree

      public static long statvfsFfree(MemorySegment buf)
      Reads f_ffree from a statvfs segment.
      Parameters:
      buf - segment populated by statvfs(MemorySegment, MemorySegment)
      Returns:
      free file nodes in filesystem
    • gethostname

      public static int gethostname(MemorySegment buf, long len) throws Throwable
      Calls gethostname(char *name, size_t len).
      Parameters:
      buf - segment of at least len bytes
      len - buffer length
      Returns:
      0 on success, -1 on error
      Throws:
      Throwable
    • getaddrinfo

      public static int getaddrinfo(MemorySegment node, MemorySegment service, MemorySegment hints, MemorySegment res) throws Throwable
      Calls getaddrinfo(node, service, hints, res).
      Parameters:
      node - hostname segment (null-terminated UTF-8)
      service - NULL segment or service name
      hints - segment allocated with ADDRINFO_LAYOUT, or NULL
      res - pointer-to-pointer output segment (ADDRESS-sized)
      Returns:
      0 on success, non-zero error code on failure
      Throws:
      Throwable
    • freeaddrinfo

      public static void freeaddrinfo(MemorySegment res) throws Throwable
      Calls freeaddrinfo(struct addrinfo *res).
      Parameters:
      res - the addrinfo pointer returned by getaddrinfo
      Throws:
      Throwable
    • gaiStrerror

      public static String gaiStrerror(int errcode, Arena arena) throws Throwable
      Calls gai_strerror(int errcode) and returns the error string.
      Parameters:
      errcode - error code from getaddrinfo
      arena - arena to scope the string reinterpret
      Returns:
      human-readable error string
      Throws:
      Throwable
    • addrinfoCanoname

      public static String addrinfoCanoname(MemorySegment resPtr, Arena arena)
      Reads ai_canonname from the first addrinfo result pointer.
      Parameters:
      resPtr - the raw pointer value stored in the output segment after getaddrinfo
      arena - arena to scope the reinterpret
      Returns:
      the canonical name string, or null if the pointer is NULL
    • getrlimit

      public static int getrlimit(int resource, MemorySegment rlim) throws Throwable
      Calls getrlimit(int resource, struct rlimit *rlim).
      Parameters:
      resource - resource constant (e.g. RLIMIT_NOFILE)
      rlim - segment allocated with RLIMIT_LAYOUT
      Returns:
      0 on success, -1 on error
      Throws:
      Throwable
    • rlimitCur

      public static long rlimitCur(MemorySegment rlim)
      Reads rlim_cur from a rlimit segment.
      Parameters:
      rlim - segment populated by getrlimit(int, MemorySegment)
      Returns:
      the soft resource limit
    • rlimitMax

      public static long rlimitMax(MemorySegment rlim)
      Reads rlim_max from a rlimit segment.
      Parameters:
      rlim - segment populated by getrlimit(int, MemorySegment)
      Returns:
      the hard resource limit