2008-01-20 17:07:18 -05:00
|
|
|
USING: kernel alien.c-types sequences math unix
|
|
|
|
combinators.cleave vectors kernel namespaces continuations
|
|
|
|
threads assocs vectors ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
|
|
|
IN: unix.process
|
|
|
|
|
2008-01-20 17:07:18 -05:00
|
|
|
! Low-level Unix process launching utilities. These are used
|
|
|
|
! to implement io.launcher on Unix. User code should use
|
|
|
|
! io.launcher instead.
|
2007-11-14 18:32:29 -05:00
|
|
|
|
|
|
|
: >argv ( seq -- alien ) [ malloc-char-string ] map f add >c-void*-array ;
|
|
|
|
|
|
|
|
: exec ( pathname argv -- int )
|
2008-01-20 17:07:18 -05:00
|
|
|
[ malloc-char-string ] [ >argv ] bi* execv ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
|
|
|
: exec-with-path ( filename argv -- int )
|
2008-01-20 17:07:18 -05:00
|
|
|
[ malloc-char-string ] [ >argv ] bi* execvp ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
|
|
|
: exec-with-env ( filename argv envp -- int )
|
2008-01-20 17:07:18 -05:00
|
|
|
[ malloc-char-string ] [ >argv ] [ >argv ] tri* execve ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
2008-01-20 17:07:18 -05:00
|
|
|
: exec-args ( seq -- int )
|
|
|
|
[ first ] [ ] bi exec ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
2008-01-20 17:07:18 -05:00
|
|
|
: exec-args-with-path ( seq -- int )
|
|
|
|
[ first ] [ ] bi exec-with-path ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
2008-01-20 17:07:18 -05:00
|
|
|
: exec-args-with-env ( seq seq -- int )
|
|
|
|
>r [ first ] [ ] bi r> exec-with-env ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
2008-01-20 17:07:18 -05:00
|
|
|
: with-fork ( child parent -- )
|
|
|
|
fork dup zero? -roll swap curry if ; inline
|
2007-11-15 18:49:43 -05:00
|
|
|
|
2008-01-20 17:07:18 -05:00
|
|
|
! Lame polling strategy for getting process exit codes. On
|
|
|
|
! BSD, we use kqueue which is more efficient.
|
2007-11-15 18:49:43 -05:00
|
|
|
|
2007-11-20 02:58:20 -05:00
|
|
|
SYMBOL: pid-wait
|
2007-11-15 18:49:43 -05:00
|
|
|
|
2008-01-20 17:07:18 -05:00
|
|
|
: (wait-for-pid) ( pid -- status )
|
|
|
|
0 <int> [ 0 waitpid drop ] keep *int ;
|
2007-11-20 02:58:20 -05:00
|
|
|
|
2008-01-20 17:07:18 -05:00
|
|
|
: wait-for-pid ( pid -- status )
|
|
|
|
[ pid-wait get-global [ ?push ] change-at stop ] curry
|
|
|
|
callcc1 ;
|
2007-11-20 02:58:20 -05:00
|
|
|
|
|
|
|
: wait-loop ( -- )
|
2008-01-20 17:07:18 -05:00
|
|
|
-1 0 <int> tuck WNOHANG waitpid ! &status return
|
|
|
|
[ *int ] [ pid-wait get delete-at* drop ] bi* ! status ?
|
|
|
|
[ schedule-thread-with ] with each
|
|
|
|
250 sleep
|
|
|
|
wait-loop ;
|
|
|
|
|
|
|
|
: start-wait-loop ( -- )
|
|
|
|
H{ } clone pid-wait set-global
|
|
|
|
[ wait-loop ] in-thread ;
|