1 USING: alien.c-types alien.data alien.syntax classes.struct
2 environment.unix generalizations io.encodings.utf8 kernel libc
3 math sequences simple-tokenizer strings unix unix.types
5 QUALIFIED-WITH: alien.c-types ac
8 ! Low-level Unix process launching utilities. These are used
9 ! to implement io.launcher on Unix. User code should use
10 ! io.launcher instead.
12 FUNCTION: pid_t fork ( )
14 : call-fork ( -- pid ) [ fork ] unix-system-call ;
16 FUNCTION: int execv ( c-string path, c-string* argv )
17 FUNCTION: int execvp ( c-string path, c-string* argv )
18 FUNCTION: int execve ( c-string path, c-string* argv, c-string* envp )
20 FUNCTION: int posix_spawn_file_actions_init ( posix_spawn_file_actions_t* file_actions )
21 FUNCTION: int posix_spawn_file_actions_destroy ( posix_spawn_file_actions_t* file_actions )
23 FUNCTION: int posix_spawnattr_init ( posix_spawnattr_t* attr )
24 FUNCTION: int posix_spawnattr_destroy ( posix_spawnattr_t* attr )
26 FUNCTION: int posix_spawn_file_actions_addclose (
27 posix_spawn_file_actions_t *file_actions, int filedes )
28 FUNCTION: int posix_spawn_file_actions_addopen (
29 posix_spawn_file_actions_t* file_actions, int intfiledes, char* path, int oflag, mode_t mode )
30 FUNCTION: int posix_spawn_file_actions_adddup2 (
31 posix_spawn_file_actions_t *file_actions, int filedes, int intnewfiledes )
32 FUNCTION: int posix_spawn_file_actions_addinherit_np (
33 posix_spawn_file_actions_t *file_actions, int filedes )
34 FUNCTION: int posix_spawn_file_actions_addchdir_np (
35 posix_spawn_file_actions_t *file_actions char* path )
36 FUNCTION: int posix_spawn_file_actions_addfchdir_np (
37 posix_spawn_file_actions_t *file_actions, int filedes )
39 FUNCTION: int posix_spawnattr_getsigdefault ( posix_spawnattr_t* attr, sigset_t* sigdefault )
40 FUNCTION: int posix_spawnattr_setsigdefault ( posix_spawnattr_t* attr, sigset_t* sigdefault )
42 FUNCTION: int posix_spawnattr_getflags ( posix_spawnattr_t* attr, ac:short* flags )
43 FUNCTION: int posix_spawnattr_setflags ( posix_spawnattr_t* attr, ac:short flags )
45 FUNCTION: int posix_spawnattr_getpgroup ( posix_spawnattr_t* attr, pid_t* pgroup )
46 FUNCTION: int posix_spawnattr_setpgroup ( posix_spawnattr_t* attr, pid_t pgroup )
48 FUNCTION: int posix_spawnattr_getsigmask ( posix_spawnattr_t* attr, sigset_t* sigmask )
49 FUNCTION: int posix_spawnattr_setsigmask ( posix_spawnattr_t* attr, sigset_t* sigmask )
51 FUNCTION: int sigaddset ( sigset_t* set, int signo )
52 FUNCTION: int sigdelset ( sigset_t* set, int signo )
53 FUNCTION: int sigemptyset ( sigset_t* set )
54 FUNCTION: int sigfillset ( sigset_t* set )
55 FUNCTION: int sigismember ( sigset_t* set, int signo )
58 FUNCTION: int posix_spawnattr_getschedparam ( posix_spawnattr_t* attr )
59 FUNCTION: int posix_spawnattr_setschedparam ( posix_spawnattr_t* attr )
60 FUNCTION: int posix_spawnattr_getschedpolicy ( posix_spawnattr_t* attr )
61 FUNCTION: int posix_spawnattr_setschedpolicy ( posix_spawnattr_t* attr )
63 CONSTANT: POSIX_SPAWN_RESETIDS 0x0001
64 CONSTANT: POSIX_SPAWN_SETPGROUP 0x0002
65 CONSTANT: POSIX_SPAWN_SETSIGDEF 0x0004
66 CONSTANT: POSIX_SPAWN_SETSIGMASK 0x0008
68 CONSTANT: POSIX_SPAWN_SETSCHEDPARAM 0x0010
69 CONSTANT: POSIX_SPAWN_SETSCHEDULER 0x0020
71 ! Darwin-specific flags
72 CONSTANT: POSIX_SPAWN_SETEXEC 0x0040
73 CONSTANT: POSIX_SPAWN_START_SUSPENDED 0x0080
74 CONSTANT: POSIX_SPAWN_SETSID 0x0400
75 CONSTANT: POSIX_SPAWN_CLOEXEC_DEFAULT 0x4000
77 CONSTANT: POSIX_SPAWN_PCONTROL_NONE 0x0000
78 CONSTANT: POSIX_SPAWN_PCONTROL_THROTTLE 0x0001
79 CONSTANT: POSIX_SPAWN_PCONTROL_SUSPEND 0x0002
80 CONSTANT: POSIX_SPAWN_PCONTROL_KILL 0x0003
82 : check-posix ( n -- )
83 dup 0 = [ drop ] [ (throw-errno) ] if ;
85 : posix-spawn-file-actions-init ( -- posix_spawn_file_actions_t )
86 posix_spawn_file_actions_t new
87 [ posix_spawn_file_actions_init check-posix ] keep ;
89 : posix-spawn-file-actions-destroy ( posix_spawn_file_actions_t -- )
90 posix_spawn_file_actions_destroy check-posix ;
92 : posix-spawnattr-init ( -- posix_spawnattr_t )
93 f posix_spawnattr_t <ref>
94 [ posix_spawnattr_init check-posix ] keep ;
96 : posix-spawnattr-destroy ( posix_spawnattr_t -- )
97 posix_spawnattr_destroy check-posix ;
99 FUNCTION: int posix_spawn ( pid_t* pid, c-string path,
100 posix_spawn_file_actions_t* file_actions,
101 posix_spawnattr_t* attrp,
102 c-string* argv, c-string* envp )
104 FUNCTION: int posix_spawnp ( pid_t* pid, c-string file,
105 posix_spawn_file_actions_t* file_actions,
106 posix_spawnattr_t* attrp,
107 c-string* argv, c-string* envp )
109 : posix-spawn-call ( path posix_spawn_file_actions_t* posix_spawnattr_t* argv envp -- pid_t )
110 [ [ 0 pid_t <ref> ] dip utf8 malloc-string ] 4dip
111 [ utf8 strings>alien ]
112 [ dup sequence? [ utf8 strings>alien ] when ] bi*
113 [ posix_spawnp check-posix ] 6 nkeep 5drop pid_t deref ;
115 : posix-spawn-custom-env ( cmd env -- int )
116 [ dup string? [ tokenize ] when ] dip
120 posix-spawn-file-actions-init
123 ] dip posix-spawn-call ;
125 : posix-spawn ( cmd -- int )
126 environ posix-spawn-custom-env ;
128 : exec ( pathname argv -- int )
129 [ utf8 malloc-string ] [ utf8 strings>alien ] bi* execv ;
131 : exec-with-path ( filename argv -- int )
132 [ utf8 malloc-string ] [ utf8 strings>alien ] bi* execvp ;
134 : exec-with-env ( filename argv envp -- int )
135 [ utf8 malloc-string ]
136 [ utf8 strings>alien ]
137 [ utf8 strings>alien ] tri* execve ;
139 : exec-args ( seq -- int )
140 [ first ] keep exec ;
142 : exec-args-with-path ( seq -- int )
143 [ first ] keep exec-with-path ;
145 : exec-args-with-env ( seq seq -- int )
146 [ [ first ] keep ] dip exec-with-env ;
148 : with-fork ( child parent -- )
149 [ call-fork ] 2dip if-zero ; inline
151 FUNCTION: int kill ( pid_t pid, int sig )
152 FUNCTION: int raise ( int sig )
155 CONSTANT: PRIO_PROCESS 0
156 CONSTANT: PRIO_PGRP 1
157 CONSTANT: PRIO_USER 2
159 CONSTANT: PRIO_MIN -20
160 CONSTANT: PRIO_MAX 20
162 ! which/who = 0 for current process
163 FUNCTION: int getpriority ( int which, int who )
164 FUNCTION: int setpriority ( int which, int who, int prio )
166 : set-priority ( n -- )
167 [ 0 0 ] dip setpriority io-error ;
172 CONSTANT: WUNTRACED 2
176 CONSTANT: WCONTINUED 8
177 CONSTANT: WNOWAIT 0x1000000
181 : WTERMSIG ( status -- value )
184 : WIFEXITED ( status -- ? )
185 WTERMSIG 0 = ; inline
187 : WEXITSTATUS ( status -- value )
188 0xff00 bitand -8 shift ; inline
190 : WIFSIGNALED ( status -- ? )
191 0x7f bitand 1 + -1 shift 0 > ; inline
193 : WCOREFLAG ( -- value )
196 : WCOREDUMP ( status -- ? )
197 WCOREFLAG bitand 0 = not ; inline
199 : WIFSTOPPED ( status -- ? )
200 0xff bitand 0x7f = ; inline
202 : WSTOPSIG ( status -- value )
205 FUNCTION: pid_t wait ( int* status )
206 FUNCTION: pid_t waitpid ( pid_t wpid, int* status, int options )