1 /*
2 * Copyright 2021-2022 The OSHI Project Contributors
3 * SPDX-License-Identifier: MIT
4 */
5 package oshi.jna.platform.unix;
6
7 import com.sun.jna.Memory;
8 import com.sun.jna.Native;
9 import com.sun.jna.Pointer;
10 import com.sun.jna.Structure;
11 import com.sun.jna.Structure.FieldOrder;
12 import com.sun.jna.ptr.NativeLongByReference;
13
14 /**
15 * C library. This class should be considered non-API as it may be removed if/when its code is incorporated into the JNA
16 * project.
17 */
18 public interface FreeBsdLibc extends CLibrary {
19 FreeBsdLibc INSTANCE = Native.load("libc", FreeBsdLibc.class);
20
21 int UTX_USERSIZE = 32;
22 int UTX_LINESIZE = 16;
23 int UTX_IDSIZE = 8;
24 int UTX_HOSTSIZE = 128;
25
26 /**
27 * Connection info
28 */
29 @FieldOrder({ "ut_type", "ut_tv", "ut_id", "ut_pid", "ut_user", "ut_line", "ut_host", "ut_spare" })
30 class FreeBsdUtmpx extends Structure {
31 public short ut_type; // type of entry
32 public Timeval ut_tv; // time entry was made
33 public byte[] ut_id = new byte[UTX_IDSIZE]; // etc/inittab id (usually line #)
34 public int ut_pid; // process id
35 public byte[] ut_user = new byte[UTX_USERSIZE]; // user login name
36 public byte[] ut_line = new byte[UTX_LINESIZE]; // device name
37 public byte[] ut_host = new byte[UTX_HOSTSIZE]; // host name
38 public byte[] ut_spare = new byte[64];
39 }
40
41 /*
42 * Data size
43 */
44 /** Constant <code>UINT64_SIZE=Native.getNativeSize(long.class)</code> */
45 int UINT64_SIZE = Native.getNativeSize(long.class);
46 /** Constant <code>INT_SIZE=Native.getNativeSize(int.class)</code> */
47 int INT_SIZE = Native.getNativeSize(int.class);
48
49 /*
50 * CPU state indices
51 */
52 /** Constant <code>CPUSTATES=5</code> */
53 int CPUSTATES = 5;
54 /** Constant <code>CP_USER=0</code> */
55 int CP_USER = 0;
56 /** Constant <code>CP_NICE=1</code> */
57 int CP_NICE = 1;
58 /** Constant <code>CP_SYS=2</code> */
59 int CP_SYS = 2;
60 /** Constant <code>CP_INTR=3</code> */
61 int CP_INTR = 3;
62 /** Constant <code>CP_IDLE=4</code> */
63 int CP_IDLE = 4;
64
65 /**
66 * Return type for BSD sysctl kern.boottime
67 */
68 @FieldOrder({ "tv_sec", "tv_usec" })
69 class Timeval extends Structure {
70 public long tv_sec; // seconds
71 public long tv_usec; // microseconds
72 }
73
74 /**
75 * CPU Ticks
76 */
77 @FieldOrder({ "cpu_ticks" })
78 class CpTime extends Structure implements AutoCloseable {
79 public long[] cpu_ticks = new long[CPUSTATES];
80
81 @Override
82 public void close() {
83 Pointer p = this.getPointer();
84 if (p instanceof Memory) {
85 ((Memory) p).close();
86 }
87 }
88 }
89
90 /**
91 * Reads a line from the current file position in the utmp file. It returns a pointer to a structure containing the
92 * fields of the line.
93 * <p>
94 * Not thread safe
95 *
96 * @return a {@link FreeBsdUtmpx} on success, and NULL on failure (which includes the "record not found" case)
97 */
98 FreeBsdUtmpx getutxent();
99
100 /**
101 * Stores the system-wide thread identifier for the current kernel-scheduled thread in the variable pointed by the
102 * argument id.
103 *
104 * @param id The thread identifier is an integer in the range from PID_MAX + 2 (100001) to INT_MAX. The thread
105 * identifier is guaranteed to be unique at any given time, for each running thread in the system.
106 * @return If successful, returns zero, otherwise -1 is returned, and errno is set to indicate the error.
107 */
108 int thr_self(NativeLongByReference id);
109 }