View Javadoc
1   /*
2    * Copyright 2019-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.jna.platform.linux;
6   
7   import com.sun.jna.Native;
8   import com.sun.jna.NativeLong;
9   import com.sun.jna.Platform;
10  import com.sun.jna.Structure;
11  import com.sun.jna.Structure.FieldOrder;
12  import com.sun.jna.platform.linux.LibC;
13  
14  import oshi.jna.platform.unix.CLibrary;
15  
16  /**
17   * Linux C Library. This class should be considered non-API as it may be removed if/when its code is incorporated into
18   * the JNA project.
19   */
20  public interface LinuxLibc extends LibC, CLibrary {
21  
22      LinuxLibc INSTANCE = Native.load("c", LinuxLibc.class);
23  
24      /**
25       * SYS_gettid Defined in one of: arch/arm64/include/asm/unistd32.h, 224 arch/x86/include/uapi/asm/unistd_32.h, 224
26       * arch/x86/include/uapi/asm/unistd_64.h, 186 include/uapi/asm-generic/unistd.h, 178
27       */
28      NativeLong SYS_GETTID = new NativeLong(Platform.isIntel() ? (Platform.is64Bit() ? 186 : 224)
29              : ((Platform.isARM() && Platform.is64Bit()) ? 224 : 178));
30  
31      /**
32       * Return type for getutxent()
33       */
34      @FieldOrder({ "ut_type", "ut_pid", "ut_line", "ut_id", "ut_user", "ut_host", "ut_exit", "ut_session", "ut_tv",
35              "ut_addr_v6", "reserved" })
36      class LinuxUtmpx extends Structure {
37          public short ut_type; // Type of login.
38          public int ut_pid; // Process ID of login process.
39          public byte[] ut_line = new byte[UT_LINESIZE]; // Devicename.
40          public byte[] ut_id = new byte[4]; // Inittab ID.
41          public byte[] ut_user = new byte[UT_NAMESIZE]; // Username.
42          public byte[] ut_host = new byte[UT_HOSTSIZE]; // Hostname for remote login.
43          public Exit_status ut_exit; // Exit status of a process marked as DEAD_PROCESS.
44          public int ut_session; // Session ID, used for windowing.
45          public Ut_Tv ut_tv; // Time entry was made.
46          public int[] ut_addr_v6 = new int[4]; // Internet address of remote host; IPv4 address uses just ut_addr_v6[0]
47          public byte[] reserved = new byte[20]; // Reserved for future use.
48      }
49  
50      /**
51       * Part of utmpx structure
52       */
53      @FieldOrder({ "e_termination", "e_exit" })
54      class Exit_status extends Structure {
55          public short e_termination; // Process termination status
56          public short e_exit; // Process exit status
57      }
58  
59      /**
60       * 32-bit timeval required for utmpx structure
61       */
62      @FieldOrder({ "tv_sec", "tv_usec" })
63      class Ut_Tv extends Structure {
64          public int tv_sec; // seconds
65          public int tv_usec; // microseconds
66      }
67  
68      /**
69       * Reads a line from the current file position in the utmp file. It returns a pointer to a structure containing the
70       * fields of the line.
71       * <p>
72       * Not thread safe
73       *
74       * @return a {@link LinuxUtmpx} on success, and NULL on failure (which includes the "record not found" case)
75       */
76      LinuxUtmpx getutxent();
77  
78      /**
79       * Returns the caller's thread ID (TID). In a single-threaded process, the thread ID is equal to the process ID. In
80       * a multithreaded process, all threads have the same PID, but each one has a unique TID.
81       *
82       * @return the thread ID of the calling thread.
83       */
84      int gettid();
85  
86      /**
87       * syscall() performs the system call whose assembly language interface has the specified number with the specified
88       * arguments.
89       *
90       * @param number sys call number
91       * @param args   sys call arguments
92       * @return The return value is defined by the system call being invoked. In general, a 0 return value indicates
93       *         success. A -1 return value indicates an error, and an error code is stored in errno.
94       */
95      NativeLong syscall(NativeLong number, Object... args);
96  }