View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.jna.platform.unix;
6   
7   import java.nio.ByteBuffer;
8   
9   import com.sun.jna.Native;
10  
11  import oshi.util.FileUtil;
12  
13  /**
14   * C library. This class should be considered non-API as it may be removed if/when its code is incorporated into the JNA
15   * project.
16   */
17  public interface AixLibc extends CLibrary {
18  
19      AixLibc INSTANCE = Native.load("c", AixLibc.class);
20  
21      int PRCLSZ = 8;
22      int PRFNSZ = 16;
23      int PRARGSZ = 80;
24  
25      class AixPsInfo {
26          public int pr_flag; // process flags from proc struct p_flag
27          public int pr_flag2; // process flags from proc struct p_flag2
28          public int pr_nlwp; // number of threads in process
29          public int pr__pad1; // reserved for future use
30          public long pr_uid; // real user id
31          public long pr_euid; // effective user id
32          public long pr_gid; // real group id
33          public long pr_egid; // effective group id
34          public long pr_pid; // unique process id
35          public long pr_ppid; // process id of parent
36          public long pr_pgid; // pid of process group leader
37          public long pr_sid; // session id
38          public long pr_ttydev; // controlling tty device
39          public long pr_addr; // internal address of proc struct
40          public long pr_size; // size of process image in KB (1024) units
41          public long pr_rssize; // resident set size in KB (1024) units
42          public Timestruc pr_start; // process start time, time since epoch
43          public Timestruc pr_time; // usr+sys cpu time for this process
44          public short pr_cid; // corral id
45          public short pr__pad2; // reserved for future use
46          public int pr_argc; // initial argument count
47          public long pr_argv; // address of initial argument vector in user process
48          public long pr_envp; // address of initial environment vector in user process
49          public byte[] pr_fname = new byte[PRFNSZ]; // last component of exec()ed pathname
50          public byte[] pr_psargs = new byte[PRARGSZ]; // initial characters of arg list
51          public long[] pr__pad = new long[8]; // reserved for future use
52          public AixLwpsInfo pr_lwp; // "representative" thread info
53  
54          public AixPsInfo(ByteBuffer buff) {
55              this.pr_flag = FileUtil.readIntFromBuffer(buff);
56              this.pr_flag2 = FileUtil.readIntFromBuffer(buff);
57              this.pr_nlwp = FileUtil.readIntFromBuffer(buff);
58              this.pr__pad1 = FileUtil.readIntFromBuffer(buff);
59              this.pr_uid = FileUtil.readLongFromBuffer(buff);
60              this.pr_euid = FileUtil.readLongFromBuffer(buff);
61              this.pr_gid = FileUtil.readLongFromBuffer(buff);
62              this.pr_egid = FileUtil.readLongFromBuffer(buff);
63              this.pr_pid = FileUtil.readLongFromBuffer(buff);
64              this.pr_ppid = FileUtil.readLongFromBuffer(buff);
65              this.pr_pgid = FileUtil.readLongFromBuffer(buff);
66              this.pr_sid = FileUtil.readLongFromBuffer(buff);
67              this.pr_ttydev = FileUtil.readLongFromBuffer(buff);
68              this.pr_addr = FileUtil.readLongFromBuffer(buff);
69              this.pr_size = FileUtil.readLongFromBuffer(buff);
70              this.pr_rssize = FileUtil.readLongFromBuffer(buff);
71              this.pr_start = new Timestruc(buff);
72              this.pr_time = new Timestruc(buff);
73              this.pr_cid = FileUtil.readShortFromBuffer(buff);
74              this.pr__pad2 = FileUtil.readShortFromBuffer(buff);
75              this.pr_argc = FileUtil.readIntFromBuffer(buff);
76              this.pr_argv = FileUtil.readLongFromBuffer(buff);
77              this.pr_envp = FileUtil.readLongFromBuffer(buff);
78              FileUtil.readByteArrayFromBuffer(buff, this.pr_fname);
79              FileUtil.readByteArrayFromBuffer(buff, this.pr_psargs);
80              for (int i = 0; i < pr__pad.length; i++) {
81                  this.pr__pad[i] = FileUtil.readLongFromBuffer(buff);
82              }
83              this.pr_lwp = new AixLwpsInfo(buff);
84          }
85  
86      }
87  
88      class AixLwpsInfo {
89          public long pr_lwpid; // thread id
90          public long pr_addr; // internal address of thread
91          public long pr_wchan; // wait addr for sleeping thread
92          public int pr_flag; // thread flags
93          public byte pr_wtype; // type of thread wait
94          public byte pr_state; // numeric scheduling state
95          public byte pr_sname; // printable character representing pr_state
96          public byte pr_nice; // nice for cpu usage
97          public int pr_pri; // priority, high value = high priority
98          public int pr_policy; // scheduling policy
99          public byte[] pr_clname = new byte[PRCLSZ]; // printable character representing pr_policy
100         public int pr_onpro; // processor on which thread last ran
101         public int pr_bindpro; // processor to which thread is bound
102 
103         public AixLwpsInfo(ByteBuffer buff) {
104             this.pr_lwpid = FileUtil.readLongFromBuffer(buff);
105             this.pr_addr = FileUtil.readLongFromBuffer(buff);
106             this.pr_wchan = FileUtil.readLongFromBuffer(buff);
107             this.pr_flag = FileUtil.readIntFromBuffer(buff);
108             this.pr_wtype = FileUtil.readByteFromBuffer(buff);
109             this.pr_state = FileUtil.readByteFromBuffer(buff);
110             this.pr_sname = FileUtil.readByteFromBuffer(buff);
111             this.pr_nice = FileUtil.readByteFromBuffer(buff);
112             this.pr_pri = FileUtil.readIntFromBuffer(buff);
113             this.pr_policy = FileUtil.readIntFromBuffer(buff);
114             FileUtil.readByteArrayFromBuffer(buff, this.pr_clname);
115             this.pr_onpro = FileUtil.readIntFromBuffer(buff);
116             this.pr_bindpro = FileUtil.readIntFromBuffer(buff);
117         }
118     }
119 
120     /**
121      * 64-bit timestruc required for psinfo structure
122      */
123     class Timestruc {
124         public long tv_sec; // seconds
125         public int tv_nsec; // nanoseconds
126         public int pad; // nanoseconds
127 
128         public Timestruc(ByteBuffer buff) {
129             this.tv_sec = FileUtil.readLongFromBuffer(buff);
130             this.tv_nsec = FileUtil.readIntFromBuffer(buff);
131             this.pad = FileUtil.readIntFromBuffer(buff);
132         }
133     }
134 
135     /**
136      * Returns the caller's kernel thread ID.
137      *
138      * @return the caller's kernel thread ID.
139      */
140     int thread_self();
141 }