View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.jna.platform.mac;
6   
7   import com.sun.jna.Native;
8   import com.sun.jna.Structure;
9   import com.sun.jna.Structure.FieldOrder;
10  import com.sun.jna.Union;
11  
12  import oshi.jna.platform.unix.CLibrary;
13  import oshi.util.Util;
14  
15  /**
16   * System class. This class should be considered non-API as it may be removed if/when its code is incorporated into the
17   * JNA project.
18   */
19  public interface SystemB extends com.sun.jna.platform.mac.SystemB, CLibrary {
20  
21      SystemB INSTANCE = Native.load("System", SystemB.class);
22  
23      int PROC_PIDLISTFDS = 1;
24      int PROX_FDTYPE_SOCKET = 2;
25      int PROC_PIDFDSOCKETINFO = 3;
26      int TSI_T_NTIMERS = 4;
27      int SOCKINFO_IN = 1;
28      int SOCKINFO_TCP = 2;
29  
30      int UTX_USERSIZE = 256;
31      int UTX_LINESIZE = 32;
32      int UTX_IDSIZE = 4;
33      int UTX_HOSTSIZE = 256;
34  
35      int AF_INET = 2; // The Internet Protocol version 4 (IPv4) address family.
36      int AF_INET6 = 30; // The Internet Protocol version 6 (IPv6) address family.
37  
38      /**
39       * Mac connection info
40       */
41      @FieldOrder({ "ut_user", "ut_id", "ut_line", "ut_pid", "ut_type", "ut_tv", "ut_host", "ut_pad" })
42      class MacUtmpx extends Structure {
43          public byte[] ut_user = new byte[UTX_USERSIZE]; // login name
44          public byte[] ut_id = new byte[UTX_IDSIZE]; // id
45          public byte[] ut_line = new byte[UTX_LINESIZE]; // tty name
46          public int ut_pid; // process id creating the entry
47          public short ut_type; // type of this entry
48          public Timeval ut_tv; // time entry was created
49          public byte[] ut_host = new byte[UTX_HOSTSIZE]; // host name
50          public byte[] ut_pad = new byte[16]; // reserved for future use
51      }
52  
53      /**
54       * Mac file descriptor info
55       */
56      @FieldOrder({ "proc_fd", "proc_fdtype" })
57      class ProcFdInfo extends Structure {
58          public int proc_fd;
59          public int proc_fdtype;
60      }
61  
62      /**
63       * Mac internet socket info
64       */
65      @FieldOrder({ "insi_fport", "insi_lport", "insi_gencnt", "insi_flags", "insi_flow", "insi_vflag", "insi_ip_ttl",
66              "rfu_1", "insi_faddr", "insi_laddr", "insi_v4", "insi_v6" })
67      class InSockInfo extends Structure {
68          public int insi_fport; /* foreign port */
69          public int insi_lport; /* local port */
70          public long insi_gencnt; /* generation count of this instance */
71          public int insi_flags; /* generic IP/datagram flags */
72          public int insi_flow;
73  
74          public byte insi_vflag; /* ini_IPV4 or ini_IPV6 */
75          public byte insi_ip_ttl; /* time to live proto */
76          public int rfu_1; /* reserved */
77          /* protocol dependent part, v4 only in last element */
78          public int[] insi_faddr = new int[4]; /* foreign host table entry */
79          public int[] insi_laddr = new int[4]; /* local host table entry */
80          public byte insi_v4; /* type of service */
81          public byte[] insi_v6 = new byte[9];
82      }
83  
84      /**
85       * Mac TCP socket info
86       */
87      @FieldOrder({ "tcpsi_ini", "tcpsi_state", "tcpsi_timer", "tcpsi_mss", "tcpsi_flags", "rfu_1", "tcpsi_tp" })
88      class TcpSockInfo extends Structure {
89          public InSockInfo tcpsi_ini;
90          public int tcpsi_state;
91          public int[] tcpsi_timer = new int[TSI_T_NTIMERS];
92          public int tcpsi_mss;
93          public int tcpsi_flags;
94          public int rfu_1; /* reserved */
95          public long tcpsi_tp; /* opaque handle of TCP protocol control block */
96      }
97  
98      /**
99       * Mack IP Socket Info
100      */
101     @FieldOrder({ "soi_stat", "soi_so", "soi_pcb", "soi_type", "soi_protocol", "soi_family", "soi_options",
102             "soi_linger", "soi_state", "soi_qlen", "soi_incqlen", "soi_qlimit", "soi_timeo", "soi_error", "soi_oobmark",
103             "soi_rcv", "soi_snd", "soi_kind", "rfu_1", "soi_proto" })
104     class SocketInfo extends Structure {
105         public long[] soi_stat = new long[17]; // vinfo_stat
106         public long soi_so; /* opaque handle of socket */
107         public long soi_pcb; /* opaque handle of protocol control block */
108         public int soi_type;
109         public int soi_protocol;
110         public int soi_family;
111         public short soi_options;
112         public short soi_linger;
113         public short soi_state;
114         public short soi_qlen;
115         public short soi_incqlen;
116         public short soi_qlimit;
117         public short soi_timeo;
118         public short soi_error;
119         public int soi_oobmark;
120         public int[] soi_rcv = new int[6]; // sockbuf_info
121         public int[] soi_snd = new int[6]; // sockbuf_info
122         public int soi_kind;
123         public int rfu_1; /* reserved */
124         public Pri soi_proto;
125     }
126 
127     /**
128      * Mac file info
129      */
130     @FieldOrder({ "fi_openflags", "fi_status", "fi_offset", "fi_type", "fi_guardflags" })
131     class ProcFileInfo extends Structure {
132         public int fi_openflags;
133         public int fi_status;
134         public long fi_offset;
135         public int fi_type;
136         public int fi_guardflags;
137     }
138 
139     /**
140      * Mac socket info
141      */
142     @FieldOrder({ "pfi", "psi" })
143     class SocketFdInfo extends Structure implements AutoCloseable {
144         public ProcFileInfo pfi;
145         public SocketInfo psi;
146 
147         @Override
148         public void close() {
149             Util.freeMemory(getPointer());
150         }
151     }
152 
153     /**
154      * Union for TCP or internet socket info
155      */
156     class Pri extends Union {
157         public InSockInfo pri_in;
158         public TcpSockInfo pri_tcp;
159         // max element is 524 bytes
160         public byte[] max_size = new byte[524];
161     }
162 
163     /**
164      * Reads a line from the current file position in the utmp file. It returns a pointer to a structure containing the
165      * fields of the line.
166      * <p>
167      * Not thread safe
168      *
169      * @return a {@link MacUtmpx} on success, and NULL on failure (which includes the "record not found" case)
170      */
171     MacUtmpx getutxent();
172 
173     int proc_pidfdinfo(int pid, int fd, int flavor, Structure buffer, int buffersize);
174 }