1 /*
2  * Copyright (c) 2015 Daniel Widdis
3  *
4  * The contents of this file is dual-licensed under 2
5  * alternative Open Source/Free licenses: LGPL 2.1 or later and
6  * Apache License 2.0. (starting with JNA version 4.0.0).
7  *
8  * You can freely decide which license you want to apply to
9  * the project.
10  *
11  * You may obtain a copy of the LGPL License at:
12  *
13  * http://www.gnu.org/licenses/licenses.html
14  *
15  * A copy is also included in the downloadable source code package
16  * containing JNA, in file "LGPL2.1".
17  *
18  * You may obtain a copy of the Apache License at:
19  *
20  * http://www.apache.org/licenses/
21  *
22  * A copy is also included in the downloadable source code package
23  * containing JNA, in file "AL2.0".
24  */
25 
26 package com.sun.jna.platform.mac;
27 
28 import com.sun.jna.Library;
29 import com.sun.jna.Native;
30 import com.sun.jna.NativeLong;
31 import com.sun.jna.Pointer;
32 import com.sun.jna.Structure;
33 import com.sun.jna.platform.unix.LibCAPI;
34 import com.sun.jna.ptr.IntByReference;
35 import com.sun.jna.ptr.LongByReference;
36 import com.sun.jna.ptr.PointerByReference;
37 
38 public interface SystemB extends LibCAPI, Library {
39 
40     SystemB INSTANCE = Native.load("System", SystemB.class);
41 
42     // host_statistics()
43     int HOST_LOAD_INFO = 1;// System loading stats
44     int HOST_VM_INFO = 2; // Virtual memory stats
45     int HOST_CPU_LOAD_INFO = 3;// CPU load stats
46 
47     // host_statistics64()
48     int HOST_VM_INFO64 = 4; // 64-bit virtual memory stats
49 
50     // host_cpu_load_info()
51     int CPU_STATE_MAX = 4;
52     int CPU_STATE_USER = 0;
53     int CPU_STATE_SYSTEM = 1;
54     int CPU_STATE_IDLE = 2;
55     int CPU_STATE_NICE = 3;
56 
57     // host_processor_info() flavor
58     int PROCESSOR_BASIC_INFO = 1;
59     int PROCESSOR_CPU_LOAD_INFO = 2;
60 
61     // Data size
62     int UINT64_SIZE = Native.getNativeSize(long.class);
63     int INT_SIZE = Native.getNativeSize(int.class);
64 
65     // params.h
66     int MAXCOMLEN = 16;
67     int MAXPATHLEN = 1024;
68     int PROC_PIDPATHINFO_MAXSIZE = MAXPATHLEN * 4;
69 
70     // proc_info.h
71     int PROC_ALL_PIDS = 1;
72     int PROC_PIDTASKALLINFO = 2;
73     int PROC_PIDTBSDINFO = 3;
74     int PROC_PIDTASKINFO = 4;
75     int PROC_PIDVNODEPATHINFO = 9;
76 
77     // length of fs type name including null
78     int MFSTYPENAMELEN = 16;
79     // length of buffer for returned name
80     int MNAMELEN = MAXPATHLEN;
81 
82     // fsstat paths
83     int MNT_WAIT = 0x0001;
84     int MNT_NOWAIT = 0x0010;
85     int MNT_DWAIT = 0x0100;
86 
87     // resource.h
88     int RUSAGE_INFO_V2 = 2;
89 
90     @Structure.FieldOrder({ "cpu_ticks" })
91     public static class HostCpuLoadInfo extends Structure {
92         public int cpu_ticks[] = new int[CPU_STATE_MAX];
93     }
94 
95     @Structure.FieldOrder({ "avenrun", "mach_factor" })
96     public static class HostLoadInfo extends Structure {
97         public int[] avenrun = new int[3]; // scaled by LOAD_SCALE
98         public int[] mach_factor = new int[3]; // scaled by LOAD_SCALE
99     }
100 
101     @Structure.FieldOrder({ "free_count", "active_count", "inactive_count", "wire_count", "zero_fill_count",
102             "reactivations", "pageins", "pageouts", "faults", "cow_faults", "lookups", "hits", "purgeable_count",
103             "purges", "speculative_count" })
104     public static class VMStatistics extends Structure {
105         public int free_count; // # of pages free
106         public int active_count; // # of pages active
107         public int inactive_count; // # of pages inactive
108         public int wire_count; // # of pages wired down
109         public int zero_fill_count; // # of zero fill pages
110         public int reactivations; // # of pages reactivated
111         public int pageins; // # of pageins
112         public int pageouts; // # of pageouts
113         public int faults; // # of faults
114         public int cow_faults; // # of copy-on-writes
115         public int lookups; // object cache lookups
116         public int hits; // object cache hits
117         public int purgeable_count; // # of pages purgeable
118         public int purges; // # of pages purged
119         // # of pages speculative (included in free_count)
120         public int speculative_count;
121     }
122 
123     @Structure.FieldOrder({ "free_count", "active_count", "inactive_count", "wire_count", "zero_fill_count",
124             "reactivations", "pageins", "pageouts", "faults", "cow_faults", "lookups", "hits", "purges",
125             "purgeable_count", "speculative_count", "decompressions", "compressions", "swapins", "swapouts",
126             "compressor_page_count", "throttled_count", "external_page_count", "internal_page_count",
127             "total_uncompressed_pages_in_compressor" })
128     public static class VMStatistics64 extends Structure {
129         public int free_count; // # of pages free
130         public int active_count; // # of pages active
131         public int inactive_count; // # of pages inactive
132         public int wire_count; // # of pages wired down
133         public long zero_fill_count; // # of zero fill pages
134         public long reactivations; // # of pages reactivated
135         public long pageins; // # of pageins
136         public long pageouts; // # of pageouts
137         public long faults; // # of faults
138         public long cow_faults; // # of copy-on-writes
139         public long lookups; // object cache lookups
140         public long hits; // object cache hits
141         public long purges; // # of pages purged
142         public int purgeable_count;
143         public int speculative_count;
144         public long decompressions; // # of pages decompressed
145         public long compressions; // # of pages compressed
146         // # of pages swapped in (via compression segments)
147         public long swapins;
148         // # of pages swapped out (via compression segments)
149         public long swapouts;
150         // # of pages used by the compressed pager to hold all the
151         // compressed data
152         public int compressor_page_count;
153         public int throttled_count; // # of pages throttled
154         // # of pages that are file-backed (non-swap)
155         public int external_page_count;
156         public int internal_page_count; // # of pages that are anonymous
157         // # of pages (uncompressed) held within the compressor.
158         public long total_uncompressed_pages_in_compressor;
159     }
160 
161     @Structure.FieldOrder({ "pbsd", "ptinfo" })
162     class ProcTaskAllInfo extends Structure {
163         public ProcBsdInfo pbsd;
164         public ProcTaskInfo ptinfo;
165     }
166 
167     @Structure.FieldOrder({ "pbi_flags", "pbi_status", "pbi_xstatus", "pbi_pid", "pbi_ppid", "pbi_uid", "pbi_gid",
168             "pbi_ruid", "pbi_rgid", "pbi_svuid", "pbi_svgid", "rfu_1", "pbi_comm", "pbi_name", "pbi_nfiles", "pbi_pgid",
169             "pbi_pjobc", "e_tdev", "e_tpgid", "pbi_nice", "pbi_start_tvsec", "pbi_start_tvusec" })
170     class ProcBsdInfo extends Structure {
171         public int pbi_flags;
172         public int pbi_status;
173         public int pbi_xstatus;
174         public int pbi_pid;
175         public int pbi_ppid;
176         public int pbi_uid;
177         public int pbi_gid;
178         public int pbi_ruid;
179         public int pbi_rgid;
180         public int pbi_svuid;
181         public int pbi_svgid;
182         public int rfu_1;
183         public byte[] pbi_comm = new byte[MAXCOMLEN];
184         public byte[] pbi_name = new byte[2 * MAXCOMLEN];
185         public int pbi_nfiles;
186         public int pbi_pgid;
187         public int pbi_pjobc;
188         public int e_tdev;
189         public int e_tpgid;
190         public int pbi_nice;
191         public long pbi_start_tvsec;
192         public long pbi_start_tvusec;
193     }
194 
195     @Structure.FieldOrder({ "pti_virtual_size", "pti_resident_size", "pti_total_user", "pti_total_system",
196             "pti_threads_user", "pti_threads_system", "pti_policy", "pti_faults", "pti_pageins", "pti_cow_faults",
197             "pti_messages_sent", "pti_messages_received", "pti_syscalls_mach", "pti_syscalls_unix", "pti_csw",
198             "pti_threadnum", "pti_numrunning", "pti_priority" })
199     class ProcTaskInfo extends Structure {
200         public long pti_virtual_size; /* virtual memory size (bytes) */
201         public long pti_resident_size; /* resident memory size (bytes) */
202         public long pti_total_user; /* total time (nanoseconds) */
203         public long pti_total_system;
204         public long pti_threads_user; /* existing threads only */
205         public long pti_threads_system;
206         public int pti_policy; /* default policy for new threads */
207         public int pti_faults; /* number of page faults */
208         public int pti_pageins; /* number of actual pageins */
209         public int pti_cow_faults; /* number of copy-on-write faults */
210         public int pti_messages_sent; /* number of messages sent */
211         public int pti_messages_received; /* number of messages received */
212         public int pti_syscalls_mach; /* number of mach system calls */
213         public int pti_syscalls_unix; /* number of unix system calls */
214         public int pti_csw; /* number of context switches */
215         public int pti_threadnum; /* number of threads in the task */
216         public int pti_numrunning; /* number of running threads */
217         public int pti_priority; /* task priority */
218     }
219 
220     @Structure.FieldOrder({ "v_swtch", "v_trap", "v_syscall", "v_intr", "v_soft", "v_faults", "v_lookups", "v_hits",
221             "v_vm_faults", "v_cow_faults", "v_swpin", "v_swpout", "v_pswpin", "v_pswpout", "v_pageins", "v_pageouts",
222             "v_pgpgin", "v_pgpgout", "v_intrans", "v_reactivated", "v_rev", "v_scan", "v_dfree", "v_pfree", "v_zfod",
223             "v_nzfod", "v_page_size", "v_kernel_pages", "v_free_target", "v_free_min", "v_free_count", "v_wire_count",
224             "v_active_count", "v_inactive_target", "v_inactive_count" })
225     class VMMeter extends Structure {
226         /*
227          * General system activity.
228          */
229         public int v_swtch; /* context switches */
230         public int v_trap; /* calls to trap */
231         public int v_syscall; /* calls to syscall() */
232         public int v_intr; /* device interrupts */
233         public int v_soft; /* software interrupts */
234         public int v_faults; /* total faults taken */
235         /*
236          * Virtual memory activity.
237          */
238         public int v_lookups; /* object cache lookups */
239         public int v_hits; /* object cache hits */
240         public int v_vm_faults; /* number of address memory faults */
241         public int v_cow_faults; /* number of copy-on-writes */
242         public int v_swpin; /* swapins */
243         public int v_swpout; /* swapouts */
244         public int v_pswpin; /* pages swapped in */
245         public int v_pswpout; /* pages swapped out */
246         public int v_pageins; /* number of pageins */
247         public int v_pageouts; /* number of pageouts */
248         public int v_pgpgin; /* pages paged in */
249         public int v_pgpgout; /* pages paged out */
250         public int v_intrans; /* intransit blocking page faults */
251         public int v_reactivated; /*
252                                    * number of pages reactivated from free list
253                                    */
254         public int v_rev; /* revolutions of the hand */
255         public int v_scan; /* scans in page out daemon */
256         public int v_dfree; /* pages freed by daemon */
257         public int v_pfree; /* pages freed by exiting processes */
258         public int v_zfod; /* pages zero filled on demand */
259         public int v_nzfod; /* number of zfod's created */
260         /*
261          * Distribution of page usages.
262          */
263         public int v_page_size; /* page size in bytes */
264         public int v_kernel_pages; /* number of pages in use by kernel */
265         public int v_free_target; /* number of pages desired free */
266         public int v_free_min; /* minimum number of pages desired free */
267         public int v_free_count; /* number of pages free */
268         public int v_wire_count; /* number of pages wired down */
269         public int v_active_count; /* number of pages active */
270         public int v_inactive_target; /* number of pages desired inactive */
271         public int v_inactive_count; /* number of pages inactive */
272     }
273 
274     @Structure.FieldOrder({ "ri_uuid", "ri_user_time", "ri_system_time", "ri_pkg_idle_wkups", "ri_interrupt_wkups",
275             "ri_pageins", "ri_wired_size", "ri_resident_size", "ri_phys_footprint", "ri_proc_start_abstime",
276             "ri_proc_exit_abstime", "ri_child_user_time", "ri_child_system_time", "ri_child_pkg_idle_wkups",
277             "ri_child_interrupt_wkups", "ri_child_pageins", "ri_child_elapsed_abstime", "ri_diskio_bytesread",
278             "ri_diskio_byteswritten" })
279     class RUsageInfoV2 extends Structure {
280         public byte[] ri_uuid = new byte[16];
281         public long ri_user_time;
282         public long ri_system_time;
283         public long ri_pkg_idle_wkups;
284         public long ri_interrupt_wkups;
285         public long ri_pageins;
286         public long ri_wired_size;
287         public long ri_resident_size;
288         public long ri_phys_footprint;
289         public long ri_proc_start_abstime;
290         public long ri_proc_exit_abstime;
291         public long ri_child_user_time;
292         public long ri_child_system_time;
293         public long ri_child_pkg_idle_wkups;
294         public long ri_child_interrupt_wkups;
295         public long ri_child_pageins;
296         public long ri_child_elapsed_abstime;
297         public long ri_diskio_bytesread;
298         public long ri_diskio_byteswritten;
299     }
300 
301     @Structure.FieldOrder({ "vip_vi", "vip_path" })
302     class VnodeInfoPath extends Structure {
303         public byte[] vip_vi = new byte[152]; // vnode_info but we don't
304                                               // need its data
305         public byte[] vip_path = new byte[MAXPATHLEN];
306     }
307 
308     @Structure.FieldOrder({ "pvi_cdir", "pvi_rdir" })
309     class VnodePathInfo extends Structure {
310         public VnodeInfoPath pvi_cdir;
311         public VnodeInfoPath pvi_rdir;
312     }
313 
314     /**
315      * The statfs() routine returns information about a mounted file system. The
316      * path argument is the path name of any file or directory within the mounted
317      * file system. The buf argument is a pointer to a statfs structure.
318      */
319     @Structure.FieldOrder({ "f_bsize", "f_iosize", "f_blocks", "f_bfree", "f_bavail", "f_files", "f_ffree", "f_fsid",
320             "f_owner", "f_type", "f_flags", "f_fssubtype", "f_fstypename", "f_mntonname", "f_mntfromname",
321             "f_reserved" })
322     class Statfs extends Structure {
323         public int f_bsize; /* fundamental file system block size */
324         public int f_iosize; /* optimal transfer block size */
325         public long f_blocks; /* total data blocks in file system */
326         public long f_bfree; /* free blocks in fs */
327         public long f_bavail; /* free blocks avail to non-superuser */
328         public long f_files; /* total file nodes in file system */
329         public long f_ffree; /* free file nodes in fs */
330         public int[] f_fsid = new int[2]; /* file system id */
331         public int f_owner; /* user that mounted the filesystem */
332         public int f_type; /* type of filesystem */
333         public int f_flags; /* copy of mount exported flags */
334         public int f_fssubtype; /* fs sub-type (flavor) */
335         /* fs type name */
336         public byte[] f_fstypename = new byte[MFSTYPENAMELEN];
337         /* directory on which mounted */
338         public byte[] f_mntonname = new byte[MAXPATHLEN];
339         /* mounted filesystem */
340         public byte[] f_mntfromname = new byte[MAXPATHLEN];
341         public int[] f_reserved = new int[8]; /* For future use */
342     }
343 
344     /**
345      * Return type for sysctl vm.swapusage
346      */
347     @Structure.FieldOrder({ "xsu_total", "xsu_avail", "xsu_used", "xsu_pagesize", "xsu_encrypted" })
348     class XswUsage extends Structure {
349         public long xsu_total;
350         public long xsu_avail;
351         public long xsu_used;
352         public int xsu_pagesize;
353         public boolean xsu_encrypted;
354     }
355 
356     /**
357      * Data type as part of IFmsgHdr
358      */
359     @Structure.FieldOrder({ "ifi_type", "ifi_typelen", "ifi_physical", "ifi_addrlen", "ifi_hdrlen", "ifi_recvquota",
360             "ifi_xmitquota", "ifi_unused1", "ifi_mtu", "ifi_metric", "ifi_baudrate", "ifi_ipackets", "ifi_ierrors",
361             "ifi_opackets", "ifi_oerrors", "ifi_collisions", "ifi_ibytes", "ifi_obytes", "ifi_imcasts", "ifi_omcasts",
362             "ifi_iqdrops", "ifi_noproto", "ifi_recvtiming", "ifi_xmittiming", "ifi_lastchange", "ifi_unused2",
363             "ifi_hwassist", "ifi_reserved1", "ifi_reserved2" })
364     class IFdata extends Structure {
365         public byte ifi_type; // ethernet, tokenring, etc
366         public byte ifi_typelen; // Length of frame type id
367         public byte ifi_physical; // e.g., AUI, Thinnet, 10base-T, etc
368         public byte ifi_addrlen; // media address length
369         public byte ifi_hdrlen; // media header length
370         public byte ifi_recvquota; // polling quota for receive intrs
371         public byte ifi_xmitquota; // polling quota for xmit intrs
372         public byte ifi_unused1; // for future use
373         public int ifi_mtu; // maximum transmission unit
374         public int ifi_metric; // routing metric (external only)
375         public int ifi_baudrate; // linespeed
376         public int ifi_ipackets; // packets received on interface
377         public int ifi_ierrors; // input errors on interface
378         public int ifi_opackets; // packets sent on interface
379         public int ifi_oerrors; // output errors on interface
380         public int ifi_collisions; // collisions on csma interfaces
381         public int ifi_ibytes; // total number of octets received
382         public int ifi_obytes; // total number of octets sent
383         public int ifi_imcasts; // packets received via multicast
384         public int ifi_omcasts; // packets sent via multicast
385         public int ifi_iqdrops; // dropped on input, this interface
386         public int ifi_noproto; // destined for unsupported protocol
387         public int ifi_recvtiming; // usec spent receiving when timing
388         public int ifi_xmittiming; // usec spent xmitting when timing
389         public Timeval ifi_lastchange; // time of last administrative change
390         public int ifi_unused2; // used to be the default_proto
391         public int ifi_hwassist; // HW offload capabilities
392         public int ifi_reserved1; // for future use
393         public int ifi_reserved2; // for future use
394     }
395 
396     /**
397      * Return type for sysctl CTL_NET,PF_ROUTE
398      */
399     @Structure.FieldOrder({ "ifm_msglen", "ifm_version", "ifm_type", "ifm_addrs", "ifm_flags", "ifm_index",
400             "ifm_data" })
401     class IFmsgHdr extends Structure {
402         public short ifm_msglen; // to skip over non-understood messages
403         public byte ifm_version; // future binary compatability
404         public byte ifm_type; // message type
405         public int ifm_addrs; // like rtm_addrs
406         public int ifm_flags; // value of if_flags
407         public short ifm_index; // index for associated ifp
408         public IFdata ifm_data; // statistics and other data about if
409 
IFmsgHdr()410         public IFmsgHdr() {
411             super();
412         }
413 
IFmsgHdr(Pointer p)414         public IFmsgHdr(Pointer p) {
415             super(p);
416         }
417     }
418 
419     /**
420      * Data type as part of IFmsgHdr
421      */
422     @Structure.FieldOrder({ "ifi_type", "ifi_typelen", "ifi_physical", "ifi_addrlen", "ifi_hdrlen", "ifi_recvquota",
423             "ifi_xmitquota", "ifi_unused1", "ifi_mtu", "ifi_metric", "ifi_baudrate", "ifi_ipackets", "ifi_ierrors",
424             "ifi_opackets", "ifi_oerrors", "ifi_collisions", "ifi_ibytes", "ifi_obytes", "ifi_imcasts", "ifi_omcasts",
425             "ifi_iqdrops", "ifi_noproto", "ifi_recvtiming", "ifi_xmittiming", "ifi_lastchange" })
426     class IFdata64 extends Structure {
427         public byte ifi_type; // ethernet, tokenring, etc
428         public byte ifi_typelen; // Length of frame type id
429         public byte ifi_physical; // e.g., AUI, Thinnet, 10base-T, etc
430         public byte ifi_addrlen; // media address length
431         public byte ifi_hdrlen; // media header length
432         public byte ifi_recvquota; // polling quota for receive intrs
433         public byte ifi_xmitquota; // polling quota for xmit intrs
434         public byte ifi_unused1; // for future use
435         public int ifi_mtu; // maximum transmission unit
436         public int ifi_metric; // routing metric (external only)
437         public long ifi_baudrate; // linespeed
438         public long ifi_ipackets; // packets received on interface
439         public long ifi_ierrors; // input errors on interface
440         public long ifi_opackets; // packets sent on interface
441         public long ifi_oerrors; // output errors on interface
442         public long ifi_collisions; // collisions on csma interfaces
443         public long ifi_ibytes; // total number of octets received
444         public long ifi_obytes; // total number of octets sent
445         public long ifi_imcasts; // packets received via multicast
446         public long ifi_omcasts; // packets sent via multicast
447         public long ifi_iqdrops; // dropped on input, this interface
448         public long ifi_noproto; // destined for unsupported protocol
449         public int ifi_recvtiming; // usec spent receiving when timing
450         public int ifi_xmittiming; // usec spent xmitting when timing
451         public Timeval ifi_lastchange; // time of last administrative change
452     }
453 
454     /**
455      * Return type for sysctl CTL_NET,PF_ROUTE
456      */
457     @Structure.FieldOrder({ "ifm_msglen", "ifm_version", "ifm_type", "ifm_addrs", "ifm_flags", "ifm_index",
458             "ifm_snd_len", "ifm_snd_maxlen", "ifm_snd_drops", "ifm_timer", "ifm_data" })
459     class IFmsgHdr2 extends Structure {
460         public short ifm_msglen; // to skip over non-understood messages
461         public byte ifm_version; // future binary compatability
462         public byte ifm_type; // message type
463         public int ifm_addrs; // like rtm_addrs
464         public int ifm_flags; // value of if_flags
465         public short ifm_index; // index for associated ifp
466         public int ifm_snd_len; // instantaneous length of send queue
467         public int ifm_snd_maxlen; // maximum length of send queue
468         public int ifm_snd_drops; // number of drops in send queue
469         public int ifm_timer; // time until if_watchdog called
470         public IFdata64 ifm_data; // statistics and other data about if
471 
IFmsgHdr2(Pointer p)472         public IFmsgHdr2(Pointer p) {
473             super(p);
474         }
475     }
476 
477     /**
478      * Return type for getpwuid
479      */
480     @Structure.FieldOrder({ "pw_name", "pw_passwd", "pw_uid", "pw_gid", "pw_change", "pw_class", "pw_gecos", "pw_dir",
481             "pw_shell", "pw_expire", "pw_fields" })
482     class Passwd extends Structure {
483         public String pw_name; // user name
484         public String pw_passwd; // encrypted password
485         public int pw_uid; // user uid
486         public int pw_gid; // user gid
487         public NativeLong pw_change; // password change time
488         public String pw_class; // user access class
489         public String pw_gecos; // Honeywell login info
490         public String pw_dir; // home directory
491         public String pw_shell; // default shell
492         public NativeLong pw_expire; // account expiration
493         public int pw_fields; // internal: fields filled in
494     }
495 
496     /**
497      * Return type for getgrgid
498      */
499     @Structure.FieldOrder({ "gr_name", "gr_passwd", "gr_gid", "gr_mem" })
500     class Group extends Structure {
501         public String gr_name; /* group name */
502         public String gr_passwd; /* group password */
503         public int gr_gid; /* group id */
504         public PointerByReference gr_mem; /* group members */
505     }
506 
507     /**
508      * Time value
509      */
510     @Structure.FieldOrder({ "tv_sec", "tv_usec" })
511     class Timeval extends Structure {
512         public NativeLong tv_sec; // seconds
513         public int tv_usec; // microseconds
514     }
515 
516     /**
517      * Time Zone
518      */
519     @Structure.FieldOrder({ "tz_minuteswest", "tz_dsttime" })
520     class Timezone extends Structure {
521         public int tz_minuteswest; /* of Greenwich */
522         public int tz_dsttime; /* type of dst correction to apply */
523     }
524 
525     /**
526      * The system's notion of the current Greenwich time and the current time zone
527      * is obtained with the gettimeofday() call, and set with the settimeofday()
528      * call. The time is expressed in seconds and microseconds since midnight (0
529      * hour), January 1, 1970. The resolution of the system clock is hardware
530      * dependent, and the time may be updated continuously or in ``ticks.'' If tp is
531      * NULL and tzp is non-NULL, gettimeofday() will populate the timezone struct in
532      * tzp. If tp is non-NULL and tzp is NULL, then only the timeval struct in tp is
533      * populated. If both tp and tzp are NULL, nothing is returned.
534      *
535      * @param tp
536      *            Timeval structure
537      * @param tzp
538      *            Timezone structure
539      * @return A 0 return value indicates that the call succeeded. A -1 return value
540      *         indicates an error occurred, and in this case an error code is stored
541      *         into the global variable errno.
542      */
gettimeofday(Timeval tp, Timezone tzp)543     int gettimeofday(Timeval tp, Timezone tzp);
544 
545     /**
546      * The mach_host_self system call returns the calling thread's host name port.
547      * It has an effect equivalent to receiving a send right for the host port.
548      *
549      * @return the host's name port
550      */
mach_host_self()551     int mach_host_self();
552 
553     /**
554      * The mach_task_self system call returns the calling thread's task_self port.
555      * It has an effect equivalent to receiving a send right for the task's kernel
556      * port.
557      *
558      * @return the task's kernel port
559      */
mach_task_self()560     int mach_task_self();
561 
562     /**
563      * Decrement the target port right's user reference count.
564      *
565      * @param port
566      *            The port holding the right.
567      * @param name
568      *            The port's name for the right.
569      * @return 0 if successful, a {@code kern_return_t} code otherwise.
570      */
mach_port_deallocate(int port, int name)571     int mach_port_deallocate(int port, int name);
572 
573     /**
574      * The host_page_size function returns the page size for the given host.
575      *
576      * @param hostPort
577      *            The name (or control) port for the host for which the page size is
578      *            desired.
579      * @param pPageSize
580      *            The host's page size (in bytes), set on success.
581      * @return 0 on success; sets errno on failure
582      */
host_page_size(int hostPort, LongByReference pPageSize)583     int host_page_size(int hostPort, LongByReference pPageSize);
584 
585     /**
586      * The host_statistics function returns scheduling and virtual memory statistics
587      * concerning the host as specified by hostStat.
588      *
589      * @param hostPort
590      *            The control port for the host for which information is to be
591      *            obtained.
592      * @param hostStat
593      *            The type of statistics desired ({@link #HOST_LOAD_INFO},
594      *            {@link #HOST_VM_INFO}, or {@link #HOST_CPU_LOAD_INFO})
595      * @param stats
596      *            Statistics about the specified host.
597      * @param count
598      *            On input, the maximum size of the buffer; on output, the size
599      *            returned (in natural-sized units).
600      * @return 0 on success; sets errno on failure
601      */
host_statistics(int hostPort, int hostStat, Structure stats, IntByReference count)602     int host_statistics(int hostPort, int hostStat, Structure stats, IntByReference count);
603 
604     /**
605      * The host_statistics64 function returns 64-bit virtual memory statistics
606      * concerning the host as specified by hostStat.
607      *
608      * @param hostPort
609      *            The control port for the host for which information is to be
610      *            obtained.
611      * @param hostStat
612      *            The type of statistics desired ({@link #HOST_VM_INFO64})
613      * @param stats
614      *            Statistics about the specified host.
615      * @param count
616      *            On input, the maximum size of the buffer; on output, the size
617      *            returned (in natural-sized units).
618      * @return 0 on success; sets errno on failure
619      */
host_statistics64(int hostPort, int hostStat, Structure stats, IntByReference count)620     int host_statistics64(int hostPort, int hostStat, Structure stats, IntByReference count);
621 
622     /**
623      * The sysctl() function retrieves system information and allows processes with
624      * appropriate privileges to set system information. The information available
625      * from sysctl() consists of integers, strings, and tables.
626      *
627      * The state is described using a "Management Information Base" (MIB) style
628      * name, listed in name, which is a namelen length array of integers.
629      *
630      * The information is copied into the buffer specified by oldp. The size of the
631      * buffer is given by the location specified by oldlenp before the call, and
632      * that location gives the amount of data copied after a successful call and
633      * after a call that returns with the error code ENOMEM. If the amount of data
634      * available is greater than the size of the buffer supplied, the call supplies
635      * as much data as fits in the buffer provided and returns with the error code
636      * ENOMEM. If the old value is not desired, oldp and oldlenp should be set to
637      * NULL.
638      *
639      * The size of the available data can be determined by calling sysctl() with the
640      * NULL argument for oldp. The size of the available data will be returned in
641      * the location pointed to by oldlenp. For some operations, the amount of space
642      * may change often. For these operations, the system attempts to round up so
643      * that the returned size is large enough for a call to return the data shortly
644      * thereafter.
645      *
646      * To set a new value, newp is set to point to a buffer of length newlen from
647      * which the requested value is to be taken. If a new value is not to be set,
648      * newp should be set to NULL and newlen set to 0.
649      *
650      * @param name
651      *            MIB array of integers
652      * @param namelen
653      *            length of the MIB array
654      * @param oldp
655      *            Information retrieved
656      * @param oldlenp
657      *            Size of information retrieved
658      * @param newp
659      *            Information to be written
660      * @param newlen
661      *            Size of information to be written
662      * @return 0 on success; sets errno on failure
663      */
sysctl(int[] name, int namelen, Pointer oldp, IntByReference oldlenp, Pointer newp, int newlen)664     int sysctl(int[] name, int namelen, Pointer oldp, IntByReference oldlenp, Pointer newp, int newlen);
665 
666     /**
667      * The sysctlbyname() function accepts an ASCII representation of the name and
668      * internally looks up the integer name vector. Apart from that, it behaves the
669      * same as the standard sysctl() function.
670      *
671      * @param name
672      *            ASCII representation of the MIB name
673      * @param oldp
674      *            Information retrieved
675      * @param oldlenp
676      *            Size of information retrieved
677      * @param newp
678      *            Information to be written
679      * @param newlen
680      *            Size of information to be written
681      * @return 0 on success; sets errno on failure
682      */
sysctlbyname(String name, Pointer oldp, IntByReference oldlenp, Pointer newp, int newlen)683     int sysctlbyname(String name, Pointer oldp, IntByReference oldlenp, Pointer newp, int newlen);
684 
685     /**
686      * This function accepts an ASCII representation of the name, looks up the
687      * integer name vector, and returns the numeric representation in the mib array
688      * pointed to by mibp. The number of elements in the mib array is given by the
689      * location specified by sizep before the call, and that location gives the
690      * number of entries copied after a successful call. The resulting mib and size
691      * may be used in subsequent sysctl() calls to get the data associated with the
692      * requested ASCII name. This interface is intended for use by applications that
693      * want to repeatedly request the same variable (the sysctl() function runs in
694      * about a third the time as the same request made via the sysctlbyname()
695      * function).
696      * <p>
697      * The number of elements in the mib array can be determined by calling
698      * sysctlnametomib() with the NULL argument for mibp.
699      * <p>
700      * The sysctlnametomib() function is also useful for fetching mib prefixes. If
701      * size on input is greater than the number of elements written, the array still
702      * contains the additional elements which may be written programmatically.
703      *
704      * @param name
705      *            ASCII representation of the name
706      * @param mibp
707      *            Integer array containing the corresponding name vector.
708      * @param size
709      *            On input, number of elements in the returned array; on output, the
710      *            number of entries copied.
711      * @return 0 on success; sets errno on failure
712      */
sysctlnametomib(String name, Pointer mibp, IntByReference size)713     int sysctlnametomib(String name, Pointer mibp, IntByReference size);
714 
715     /**
716      * The host_processor_info function returns information about processors.
717      *
718      * @param hostPort
719      *            The control port for the host for which information is to be
720      *            obtained.
721      * @param flavor
722      *            The type of information requested.
723      * @param procCount
724      *            Pointer to the number of processors
725      * @param procInfo
726      *            Pointer to the structure corresponding to the requested flavor
727      * @param procInfoCount
728      *            Pointer to number of elements in the returned structure
729      * @return 0 on success; sets errno on failure
730      */
host_processor_info(int hostPort, int flavor, IntByReference procCount, PointerByReference procInfo, IntByReference procInfoCount)731     int host_processor_info(int hostPort, int flavor, IntByReference procCount, PointerByReference procInfo,
732             IntByReference procInfoCount);
733 
734     /**
735      * This function searches the password database for the given user uid, always
736      * returning the first one encountered.
737      *
738      * @param uid
739      *            The user ID
740      * @return a Passwd structure matching that user
741      */
getpwuid(int uid)742     Passwd getpwuid(int uid);
743 
744     /**
745      * This function searches the group database for the given group name pointed to
746      * by the group id given by gid, returning the first one encountered. Identical
747      * group gids may result in undefined behavior.
748      *
749      * @param gid
750      *            The group ID
751      * @return a Group structure matching that group
752      */
getgrgid(int gid)753     Group getgrgid(int gid);
754 
755     /**
756      * Search through the current processes
757      *
758      * @param type
759      *            types of processes to be searched
760      * @param typeinfo
761      *            adjunct information for type
762      * @param buffer
763      *            a C array of int-sized values to be filled with process
764      *            identifiers that hold an open file reference matching the
765      *            specified path or volume. Pass NULL to obtain the minimum buffer
766      *            size needed to hold the currently active processes.
767      * @param buffersize
768      *            the size (in bytes) of the provided buffer.
769      * @return the number of bytes of data returned in the provided buffer; -1 if an
770      *         error was encountered;
771      */
proc_listpids(int type, int typeinfo, int[] buffer, int buffersize)772     int proc_listpids(int type, int typeinfo, int[] buffer, int buffersize);
773 
774     /**
775      * Return in buffer a proc_*info structure corresponding to the flavor for the
776      * specified process
777      *
778      * @param pid
779      *            the process identifier
780      * @param flavor
781      *            the type of information requested
782      * @param arg
783      *            argument possibly needed for some flavors
784      * @param buffer
785      *            holds results
786      * @param buffersize
787      *            size of results
788      * @return the number of bytes of data returned in the provided buffer; -1 if an
789      *         error was encountered;
790      */
proc_pidinfo(int pid, int flavor, long arg, Structure buffer, int buffersize)791     int proc_pidinfo(int pid, int flavor, long arg, Structure buffer, int buffersize);
792 
793     /**
794      * Return in buffer the name of the specified process
795      *
796      * @param pid
797      *            the process identifier
798      * @param buffer
799      *            holds results
800      * @param buffersize
801      *            size of results
802      * @return the length of the name returned in buffer if successful; 0 otherwise
803      */
proc_pidpath(int pid, Pointer buffer, int buffersize)804     int proc_pidpath(int pid, Pointer buffer, int buffersize);
805 
806     /**
807      * Return resource usage information for the given pid, which can be a live
808      * process or a zombie.
809      *
810      * @param pid
811      *            the process identifier
812      * @param flavor
813      *            the type of information requested
814      * @param buffer
815      *            holds results
816      * @return 0 on success; or -1 on failure, with errno set to indicate the
817      *         specific error.
818      */
proc_pid_rusage(int pid, int flavor, RUsageInfoV2 buffer)819     int proc_pid_rusage(int pid, int flavor, RUsageInfoV2 buffer);
820 
821     /**
822      * The getfsstat() function returns information about all mounted file systems.
823      * The buf argument is a pointer to an array of statfs structures.
824      *
825      * Fields that are undefined for a particular file system are set to -1. The
826      * buffer is filled with an array of statfs structures, one for each mounted
827      * file system up to the size specified by bufsize.
828      *
829      * @param buf
830      *            Array of statfs structures that will be filled with results. If
831      *            buf is given as NULL, getfsstat() returns just the number of
832      *            mounted file systems.
833      * @param bufsize
834      *            Size of the buffer to fill
835      * @param flags
836      *            If flags is set to MNT_NOWAIT, getfsstat() will directly return
837      *            the information retained in the kernel to avoid delays caused by
838      *            waiting for updated information from a file system that is perhaps
839      *            temporarily unable to respond. Some of the information returned
840      *            may be out of date, however; if flags is set to MNT_WAIT or
841      *            MNT_DWAIT instead, getfsstat() will request updated information
842      *            from each mounted filesystem before returning.
843      * @return Upon successful completion, the number of statfs structures is
844      *         returned. Otherwise, -1 is returned and the global variable errno is
845      *         set to indicate the error.
846      */
getfsstat64(Statfs[] buf, int bufsize, int flags)847     int getfsstat64(Statfs[] buf, int bufsize, int flags);
848 
849     /**
850      * Returns the process ID of the calling process. The ID is guaranteed to be
851      * unique and is useful for constructing temporary file names.
852      *
853      * @return the process ID of the calling process.
854      */
getpid()855     int getpid();
856 }
857