2008-05-14 00:09:39 -04:00
|
|
|
USING: kernel alien.c-types alien.strings sequences math alien.syntax unix
|
2008-10-18 22:15:43 -04:00
|
|
|
vectors kernel namespaces continuations threads assocs vectors
|
2008-12-14 21:03:00 -05:00
|
|
|
io.backend.unix io.encodings.utf8 unix.utilities ;
|
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
|
|
|
|
2008-05-14 00:09:39 -04:00
|
|
|
FUNCTION: pid_t fork ( ) ;
|
|
|
|
|
|
|
|
: fork-process ( -- pid ) [ fork ] unix-system-call ;
|
|
|
|
|
|
|
|
FUNCTION: int execv ( char* path, char** argv ) ;
|
|
|
|
FUNCTION: int execvp ( char* path, char** argv ) ;
|
|
|
|
FUNCTION: int execve ( char* path, char** argv, char** envp ) ;
|
|
|
|
|
2007-11-14 18:32:29 -05:00
|
|
|
: exec ( pathname argv -- int )
|
2008-10-18 22:15:43 -04:00
|
|
|
[ utf8 malloc-string ] [ utf8 strings>alien ] bi* execv ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
|
|
|
: exec-with-path ( filename argv -- int )
|
2008-10-18 22:15:43 -04:00
|
|
|
[ utf8 malloc-string ] [ utf8 strings>alien ] bi* execvp ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
|
|
|
: exec-with-env ( filename argv envp -- int )
|
2008-10-18 22:15:43 -04:00
|
|
|
[ utf8 malloc-string ]
|
|
|
|
[ utf8 strings>alien ]
|
|
|
|
[ utf8 strings>alien ] 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 )
|
2008-11-29 21:19:40 -05:00
|
|
|
[ [ first ] [ ] bi ] dip exec-with-env ;
|
2007-11-14 18:32:29 -05:00
|
|
|
|
2008-01-20 17:07:18 -05:00
|
|
|
: with-fork ( child parent -- )
|
2008-07-08 14:22:57 -04:00
|
|
|
[ [ fork-process dup zero? ] dip [ drop ] prepose ] dip
|
|
|
|
if ; inline
|
2007-11-15 18:49:43 -05:00
|
|
|
|
2008-05-14 01:45:43 -04:00
|
|
|
: SIGKILL 9 ; inline
|
|
|
|
: SIGTERM 15 ; inline
|
|
|
|
|
|
|
|
FUNCTION: int kill ( pid_t pid, int sig ) ;
|
|
|
|
|
|
|
|
: PRIO_PROCESS 0 ; inline
|
|
|
|
: PRIO_PGRP 1 ; inline
|
|
|
|
: PRIO_USER 2 ; inline
|
|
|
|
|
|
|
|
: PRIO_MIN -20 ; inline
|
|
|
|
: PRIO_MAX 20 ; inline
|
|
|
|
|
|
|
|
! which/who = 0 for current process
|
|
|
|
FUNCTION: int getpriority ( int which, int who ) ;
|
|
|
|
FUNCTION: int setpriority ( int which, int who, int prio ) ;
|
2008-03-24 19:02:39 -04:00
|
|
|
|
|
|
|
: set-priority ( n -- )
|
2008-05-14 01:45:43 -04:00
|
|
|
0 0 rot setpriority io-error ;
|
|
|
|
|
|
|
|
! Flags for waitpid
|
|
|
|
|
|
|
|
: WNOHANG 1 ; inline
|
|
|
|
: WUNTRACED 2 ; inline
|
|
|
|
|
|
|
|
: WSTOPPED 2 ; inline
|
|
|
|
: WEXITED 4 ; inline
|
|
|
|
: WCONTINUED 8 ; inline
|
|
|
|
: WNOWAIT HEX: 1000000 ; inline
|
|
|
|
|
|
|
|
! Examining status
|
|
|
|
|
|
|
|
: WTERMSIG ( status -- value )
|
|
|
|
HEX: 7f bitand ; inline
|
|
|
|
|
|
|
|
: WIFEXITED ( status -- ? )
|
2008-12-10 18:30:07 -05:00
|
|
|
WTERMSIG 0 = ; inline
|
2008-05-14 01:45:43 -04:00
|
|
|
|
|
|
|
: WEXITSTATUS ( status -- value )
|
|
|
|
HEX: ff00 bitand -8 shift ; inline
|
|
|
|
|
|
|
|
: WIFSIGNALED ( status -- ? )
|
|
|
|
HEX: 7f bitand 1+ -1 shift 0 > ; inline
|
|
|
|
|
|
|
|
: WCOREFLAG ( -- value )
|
|
|
|
HEX: 80 ; inline
|
|
|
|
|
|
|
|
: WCOREDUMP ( status -- ? )
|
2008-12-10 18:30:07 -05:00
|
|
|
WCOREFLAG bitand 0 = not ; inline
|
2008-05-14 01:45:43 -04:00
|
|
|
|
|
|
|
: WIFSTOPPED ( status -- ? )
|
|
|
|
HEX: ff bitand HEX: 7f = ; inline
|
|
|
|
|
|
|
|
: WSTOPSIG ( status -- value )
|
|
|
|
WEXITSTATUS ; inline
|
|
|
|
|
|
|
|
FUNCTION: pid_t wait ( int* status ) ;
|
|
|
|
FUNCTION: pid_t waitpid ( pid_t wpid, int* status, int options ) ;
|
|
|
|
|
|
|
|
: wait-for-pid ( pid -- status )
|
2008-10-18 22:15:43 -04:00
|
|
|
0 <int> [ 0 waitpid drop ] keep *int WEXITSTATUS ;
|