1 ! Copyright (C) 2013 John Benediktsson
2 ! See http://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.time unix.types ;
9 QUALIFIED-WITH: alien.c-types c
15 : system-type ( -- str ) { 1 1 } sysctl-query-string ;
16 : system-release ( -- str ) { 1 2 } sysctl-query-string ;
17 : system-revision ( -- str ) { 1 3 } sysctl-query-string ;
18 : system-version ( -- str ) { 1 4 } sysctl-query-string ;
19 : max-vnodes ( -- n ) { 1 5 } sysctl-query-uint ;
20 : max-processes ( -- n ) { 1 6 } sysctl-query-uint ;
21 : max-open-files ( -- n ) { 1 7 } sysctl-query-uint ;
22 : max-arguments ( -- args ) { 1 8 } sysctl-query-uint ;
23 : system-security-level ( -- n ) { 1 9 } sysctl-query-uint ;
24 : hostname ( -- str ) { 1 10 } sysctl-query-string ;
26 : sysctl-query-bytes ( seq -- n )
27 [ int >c-array ] [ length ] bi f 0 uint <ref>
28 [ f 0 sysctl io-error ] keep uint deref ;
42 { cr_ngroups c:short }
43 { cr_groups gid_t[16] } ;
49 { dummy4 caddr_t[3] } ;
51 TYPEDEF: int32_t segsz_t
52 TYPEDEF: uint32_t fixpt_t
53 TYPEDEF: uint64_t u_quad_t
54 TYPEDEF: uint32_t sigset_t
57 { it_interval timeval }
58 { it_value timeval } ;
61 { __p_starttime timeval }
69 { user_stack caddr_t }
80 { p_realtimer itimerval }
90 { p_sigmask sigset_t }
91 { p_sigignore sigset_t }
92 { p_sigcatch sigset_t }
104 { kp_proc extern_proc }
118 { e_xrssize c:short }
119 { e_xccount c:short }
123 { e_spare int32_t[4] } ;
125 : head-split-skip ( seq n quot: ( elt -- ? ) -- pieces )
127 [ _ [ trim-head-slice ] [ split1-when-slice ] bi ]
129 ] produce 2nip ; inline
131 : args ( pid -- args )
132 [ 1 49 ] dip 0 4array max-arguments sysctl-query
133 4 cut-slice swap >byte-array uint deref
134 [ zero? ] head-split-skip [ >string ] map ;
137 { 1 14 0 0 } dup sysctl-query-bytes sysctl-query
138 kinfo_proc struct-size group
139 [ kinfo_proc memory>struct ] map ;
141 : ps-arg ( kp_proc -- arg )
142 [ p_pid>> args rest unwords ] [
143 drop p_comm>> 0 over index [ head ] when* >string
149 procs [ kp_proc>> p_pid>> 0 > ] filter
150 [ kp_proc>> [ p_pid>> ] [ ps-arg ] bi ] { } map>assoc ;