1 ! Copyright (C) 2013 John Benediktsson
2 ! See https://factorcode.org/license.txt for BSD license
4 USING: accessors alien.c-types alien.data alien.syntax arrays
5 assocs byte-arrays classes.struct continuations fry grouping
6 kernel libc literals math sequences splitting strings system
7 system-info.macosx tools.ps unix unix.sysctl unix.time
10 QUALIFIED-WITH: alien.c-types c
16 : system-type ( -- str ) { 1 1 } sysctl-query-string ;
17 : system-release ( -- str ) { 1 2 } sysctl-query-string ;
18 : system-revision ( -- str ) { 1 3 } sysctl-query-string ;
19 : system-version ( -- str ) { 1 4 } sysctl-query-string ;
20 : max-vnodes ( -- n ) { 1 5 } sysctl-query-uint ;
21 : max-processes ( -- n ) { 1 6 } sysctl-query-uint ;
22 : max-open-files ( -- n ) { 1 7 } sysctl-query-uint ;
23 : max-arguments ( -- args ) { 1 8 } sysctl-query-uint ;
24 : system-security-level ( -- n ) { 1 9 } sysctl-query-uint ;
25 : hostname ( -- str ) { 1 10 } sysctl-query-string ;
27 : sysctl-query-bytes ( seq -- n )
28 [ int >c-array ] [ length ] bi f 0 uint <ref>
29 [ f 0 sysctl io-error ] keep uint deref ;
43 { cr_ngroups c:short }
44 { cr_groups gid_t[16] } ;
50 { dummy4 caddr_t[3] } ;
52 TYPEDEF: int32_t segsz_t
53 TYPEDEF: uint32_t fixpt_t
54 TYPEDEF: uint64_t u_quad_t
55 TYPEDEF: uint32_t sigset_t
58 { it_interval timeval }
59 { it_value timeval } ;
62 { __p_starttime timeval }
70 { user_stack caddr_t }
81 { p_realtimer itimerval }
91 { p_sigmask sigset_t }
92 { p_sigignore sigset_t }
93 { p_sigcatch sigset_t }
105 { kp_proc extern_proc }
119 { e_xrssize c:short }
120 { e_xccount c:short }
124 { e_spare int32_t[4] } ;
126 : head-split-skip ( seq n quot: ( elt -- ? ) -- pieces )
128 [ _ [ trim-head-slice ] [ split1-when-slice ] bi ]
130 ] produce 2nip ; inline
132 : args ( pid -- args )
133 [ 1 49 ] dip 0 4array max-arguments sysctl-query
134 4 cut-slice swap >byte-array uint deref
135 [ zero? ] head-split-skip [ >string ] map ;
138 { 1 14 0 0 } dup sysctl-query-bytes sysctl-query
139 kinfo_proc struct-size group
140 [ kinfo_proc memory>struct ] map ;
142 : ps-arg ( kp_proc -- arg )
143 [ p_pid>> args rest join-words ] [
144 drop p_comm>> 0 over index [ head ] when* >string
150 procs [ kp_proc>> p_pid>> 0 > ] filter
151 [ kp_proc>> [ p_pid>> ] [ ps-arg ] bi ] { } map>assoc ;