2 USING: io io.launcher io.unix.backend io.nonblocking
3 sequences kernel namespaces math system alien.c-types
4 debugger continuations ;
9 : with-fork ( quot -- pid )
10 fork [ zero? -rot if ] keep ; inline
12 : prepare-execve ( args -- cmd args envp )
13 #! Doesn't free any memory, so we only call this word
15 [ malloc-char-string ] map
20 : (spawn-process) ( args -- )
21 [ prepare-execve execve ] catch 1 exit ;
23 : spawn-process ( args -- pid )
24 [ (spawn-process) ] [ drop ] with-fork ;
26 : wait-for-process ( pid -- )
27 0 <int> 0 waitpid drop ;
29 : shell-command ( string -- args )
30 { "/bin/sh" "-c" } swap add ;
32 M: unix-io run-process ( string -- )
33 shell-command spawn-process wait-for-process ;
35 : detached-shell-command ( string -- args )
36 shell-command "&" add ;
38 M: unix-io run-detached ( string -- )
39 detached-shell-command spawn-process wait-for-process ;
41 : open-pipe ( -- pair )
42 2 "int" <c-array> dup pipe zero?
43 [ 2 c-int-array> ] [ drop f ] if ;
45 : setup-stdio-pipe ( stdin stdout -- )
46 2dup first close second close
47 >r first 0 dup2 drop r> second 1 dup2 drop ;
49 : spawn-process-stream ( args -- in out pid )
54 2dup second close first close
56 ] with-fork >r first swap second r> ;
58 TUPLE: pipe-stream pid ;
60 : <pipe-stream> ( in out pid -- stream )
61 pipe-stream construct-boa
62 -rot handle>duplex-stream over set-delegate ;
64 M: pipe-stream stream-close
65 dup delegate stream-close
66 pipe-stream-pid wait-for-process ;
68 M: unix-io <process-stream>
69 shell-command spawn-process-stream <pipe-stream> ;