From 46f306132cca7b0dc43aa64af8cd20e3de584ad4 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 25 Apr 2013 09:12:42 -0700 Subject: [PATCH] tools.ps: implemented on macosx. --- basis/tools/ps/linux/authors.txt | 1 + basis/tools/ps/linux/linux.factor | 22 ++++ basis/tools/ps/{ => linux}/platforms.txt | 0 basis/tools/ps/macosx/authors.txt | 1 + basis/tools/ps/macosx/macosx.factor | 155 +++++++++++++++++++++++ basis/tools/ps/macosx/platforms.txt | 1 + basis/tools/ps/ps.factor | 26 ++-- basis/tools/ps/summary.txt | 2 +- basis/tools/ps/windows/platforms.txt | 1 + basis/tools/ps/windows/windows.factor | 4 + 10 files changed, 194 insertions(+), 19 deletions(-) create mode 100644 basis/tools/ps/linux/authors.txt create mode 100644 basis/tools/ps/linux/linux.factor rename basis/tools/ps/{ => linux}/platforms.txt (100%) create mode 100644 basis/tools/ps/macosx/authors.txt create mode 100644 basis/tools/ps/macosx/macosx.factor create mode 100644 basis/tools/ps/macosx/platforms.txt create mode 100644 basis/tools/ps/windows/platforms.txt create mode 100644 basis/tools/ps/windows/windows.factor diff --git a/basis/tools/ps/linux/authors.txt b/basis/tools/ps/linux/authors.txt new file mode 100644 index 0000000000..7c1b2f2279 --- /dev/null +++ b/basis/tools/ps/linux/authors.txt @@ -0,0 +1 @@ +Doug Coleman diff --git a/basis/tools/ps/linux/linux.factor b/basis/tools/ps/linux/linux.factor new file mode 100644 index 0000000000..016d3d51ac --- /dev/null +++ b/basis/tools/ps/linux/linux.factor @@ -0,0 +1,22 @@ +! Copyright (C) 2012 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors assocs io.directories io.pathnames kernel +math.parser prettyprint sequences splitting unix.linux.proc ; +IN: tools.ps.linux + +! If cmdline is empty, read the filename from /proc/pid/stat +: ps-cmdline ( path -- path string ) + dup parse-proc-pid-cmdline [ + dup parse-proc-pid-stat filename>> + [ "()" member? ] trim + "[" "]" surround + ] [ + "\0" split " " join + ] if-empty ; + +M: linux ps ( -- assoc ) + "/proc" [ + "." directory-files + [ file-name string>number ] filter + [ ps-cmdline ] { } map>assoc + ] with-directory ; diff --git a/basis/tools/ps/platforms.txt b/basis/tools/ps/linux/platforms.txt similarity index 100% rename from basis/tools/ps/platforms.txt rename to basis/tools/ps/linux/platforms.txt diff --git a/basis/tools/ps/macosx/authors.txt b/basis/tools/ps/macosx/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/basis/tools/ps/macosx/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/basis/tools/ps/macosx/macosx.factor b/basis/tools/ps/macosx/macosx.factor new file mode 100644 index 0000000000..86e9406b0e --- /dev/null +++ b/basis/tools/ps/macosx/macosx.factor @@ -0,0 +1,155 @@ +! Copyright (C) 2013 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: accessors alien.c-types alien.data alien.syntax arrays +assocs byte-arrays classes.struct continuations fry grouping +kernel literals math sequences sorting splitting strings system +system-info.macosx tools.ps unix unix.time unix.types ; + +QUALIFIED-WITH: alien.c-types c + +IN: tools.ps.macosx + + + [ f 0 sysctl io-error ] keep uint deref ; + +STRUCT: _pcred + { pc_lock char[72] } + { pc_ucred void* } + { p_ruid uid_t } + { p_svuid uid_t } + { p_rgid gid_t } + { p_svgid gid_t } + { p_refcnt int } ; + +STRUCT: _ucred + { cr_ref int32_t } + { cr_uid uid_t } + { cr_ngroups c:short } + { cr_groups gid_t[16] } ; + +STRUCT: vmspace + { dummy int32_t } + { dummy2 caddr_t } + { dummy3 int32_t[5] } + { dummy4 caddr_t[3] } ; + +TYPEDEF: int32_t segsz_t +TYPEDEF: uint32_t fixpt_t +TYPEDEF: uint64_t u_quad_t +TYPEDEF: uint32_t sigset_t + +STRUCT: itimerval + { it_interval timeval } + { it_value timeval } ; + +STRUCT: extern_proc + { __p_starttime timeval } + { p_vmspace void* } + { p_sigacts void* } + { p_flag int } + { p_stat char } + { p_pid pid_t } + { p_oppid pid_t } + { p_dupfd int } + { user_stack caddr_t } + { exit_thread void* } + { p_debugger int } + { sigwait boolean_t } + { p_estcpu uint } + { p_cpticks int } + { p_pctcpu fixpt_t } + { p_wchan void* } + { p_wmesg void* } + { p_swtime uint } + { p_slptime uint } + { p_realtimer itimerval } + { p_rtime timeval } + { p_uticks u_quad_t } + { p_sticks u_quad_t } + { p_iticks u_quad_t } + { p_traceflag int } + { p_tracep void* } + { p_siglist int } + { p_textvp void* } + { p_holdcnt int } + { p_sigmask sigset_t } + { p_sigignore sigset_t } + { p_sigcatch sigset_t } + { p_priority uchar } + { p_usrpri uchar } + { p_nice char } + { p_comm char[16] } + { p_pgrp void* } + { p_addr void* } + { p_xstat ushort } + { p_acflag ushort } + { p_ru void* } ; + +STRUCT: kinfo_proc + { kp_proc extern_proc } + { e_paddr void* } + { e_sess void* } + { e_pcred _pcred } + { e_ucred _ucred } + { e_vm vmspace } + { e_ppid pid_t } + { e_pgid pid_t } + { e_joc c:short } + { e_tdev dev_t } + { e_tpgid pid_t } + { e_tsess void* } + { e_mesg char[8] } + { e_xsize segsz_t } + { e_xrssize c:short } + { e_xccount c:short } + { e_xswrss c:short } + { e_flag int32_t } + { e_login char[12] } + { e_spare int32_t[4] } ; + +: split1-skip-slice ( seq quot: ( elt -- ? ) -- before-slice after-slice ) + [ find drop dup ] + [ [ not ] compose find-from drop over or ] + [ drop snip-slice ] 2tri ; inline + +: head-split-skip ( seq n quot: ( elt -- ? ) -- pieces ) + [ dup 0 >= ] swap '[ + [ _ split1-skip-slice ] [ 1 - rot ] bi* + ] produce 2nip ; inline + +: args ( pid -- args ) + [ 1 49 ] dip 0 4array max-arguments sysctl-query + 4 cut-slice swap >byte-array uint deref + [ zero? ] head-split-skip [ >string ] map ; + +: procs ( -- seq ) + { 1 14 0 0 } dup sysctl-query-bytes sysctl-query + kinfo_proc struct-size group + [ kinfo_proc memory>struct ] map ; + +: ps-arg ( kp_proc -- arg ) + [ p_pid>> args rest " " join ] [ + drop p_comm>> 0 over index [ head ] when* >string + ] recover ; + +PRIVATE> + +M: macosx ps ( -- assoc ) + procs [ kp_proc>> p_pid>> 0 > ] filter + [ kp_proc>> [ p_pid>> ] [ ps-arg ] bi ] { } map>assoc + sort-keys ; diff --git a/basis/tools/ps/macosx/platforms.txt b/basis/tools/ps/macosx/platforms.txt new file mode 100644 index 0000000000..6e806f449e --- /dev/null +++ b/basis/tools/ps/macosx/platforms.txt @@ -0,0 +1 @@ +macosx diff --git a/basis/tools/ps/ps.factor b/basis/tools/ps/ps.factor index f0c7125909..b6e41f424e 100644 --- a/basis/tools/ps/ps.factor +++ b/basis/tools/ps/ps.factor @@ -1,25 +1,15 @@ -! Copyright (C) 2012 Doug Coleman. +! Copyright (C) 2012-2013 Doug Coleman, John Benediktsson. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors assocs io.directories io.pathnames kernel -math.parser prettyprint sequences splitting unix.linux.proc ; +USING: combinators prettyprint system vocabs ; IN: tools.ps -! If cmdline is empty, read the filename from /proc/pid/stat -: ps-cmdline ( path -- path string ) - dup parse-proc-pid-cmdline [ - dup parse-proc-pid-stat filename>> - [ "()" member? ] trim - "[" "]" surround - ] [ - "\0" split " " join - ] if-empty ; +HOOK: ps os ( -- assoc ) -: ps ( -- assoc ) - "/proc" [ - "." directory-files - [ file-name string>number ] filter - [ ps-cmdline ] { } map>assoc - ] with-directory ; +{ + { [ os macosx? ] [ "tools.ps.macosx" ] } + { [ os linux? ] [ "tools.ps.linux" ] } + { [ os windows? ] [ "tools.ps.windows" ] } +} cond require : ps. ( -- ) ps simple-table. ; diff --git a/basis/tools/ps/summary.txt b/basis/tools/ps/summary.txt index b6c670821b..19af4b04cd 100644 --- a/basis/tools/ps/summary.txt +++ b/basis/tools/ps/summary.txt @@ -1 +1 @@ -A basic ps utility for Linux. +Process listing utility diff --git a/basis/tools/ps/windows/platforms.txt b/basis/tools/ps/windows/platforms.txt new file mode 100644 index 0000000000..8e1a55995e --- /dev/null +++ b/basis/tools/ps/windows/platforms.txt @@ -0,0 +1 @@ +windows diff --git a/basis/tools/ps/windows/windows.factor b/basis/tools/ps/windows/windows.factor new file mode 100644 index 0000000000..97b21b38b4 --- /dev/null +++ b/basis/tools/ps/windows/windows.factor @@ -0,0 +1,4 @@ +USING: system tools.ps ; +IN: tools.ps.windows + +M: windows ps ( -- assoc ) { } ;