]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/process/process.factor
classes.struct: moving to new/boa instead of <struct>/<struct-boa>
[factor.git] / basis / unix / process / process.factor
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
4 unix.utilities ;
5 QUALIFIED-WITH: alien.c-types ac
6 IN: unix.process
7
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.
11
12 FUNCTION: pid_t fork ( )
13
14 : call-fork ( -- pid ) [ fork ] unix-system-call ;
15
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 )
19
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 )
22
23 FUNCTION: int posix_spawnattr_init ( posix_spawnattr_t* attr )
24 FUNCTION: int posix_spawnattr_destroy ( posix_spawnattr_t* attr )
25
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 )
38
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 )
41
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 )
44
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 )
47
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 )
50
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 )
56
57 ! Not on macOS
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 )
62
63 CONSTANT: POSIX_SPAWN_RESETIDS            0x0001
64 CONSTANT: POSIX_SPAWN_SETPGROUP           0x0002
65 CONSTANT: POSIX_SPAWN_SETSIGDEF           0x0004
66 CONSTANT: POSIX_SPAWN_SETSIGMASK          0x0008
67
68 CONSTANT: POSIX_SPAWN_SETSCHEDPARAM       0x0010
69 CONSTANT: POSIX_SPAWN_SETSCHEDULER        0x0020
70
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
76
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
81
82 : check-posix ( n -- )
83     dup 0 = [ drop ] [ (throw-errno) ] if ;
84
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 ;
88
89 : posix-spawn-file-actions-destroy ( posix_spawn_file_actions_t -- )
90     posix_spawn_file_actions_destroy check-posix ;
91
92 : posix-spawnattr-init ( -- posix_spawnattr_t )
93     f posix_spawnattr_t <ref>
94     [ posix_spawnattr_init check-posix ] keep ;
95
96 : posix-spawnattr-destroy ( posix_spawnattr_t -- )
97     posix_spawnattr_destroy check-posix ;
98
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 )
103
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 )
108
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 ;
114
115 : posix-spawn-custom-env ( cmd env -- int )
116     [ dup string? [ tokenize ] when ] dip
117     [
118         [
119             first
120             posix-spawn-file-actions-init
121             posix-spawnattr-init
122         ] keep
123     ] dip posix-spawn-call ;
124
125 : posix-spawn ( cmd -- int )
126     environ posix-spawn-custom-env ;
127
128 : exec ( pathname argv -- int )
129     [ utf8 malloc-string ] [ utf8 strings>alien ] bi* execv ;
130
131 : exec-with-path ( filename argv -- int )
132     [ utf8 malloc-string ] [ utf8 strings>alien ] bi* execvp ;
133
134 : exec-with-env ( filename argv envp -- int )
135     [ utf8 malloc-string ]
136     [ utf8 strings>alien ]
137     [ utf8 strings>alien ] tri* execve ;
138
139 : exec-args ( seq -- int )
140     [ first ] keep exec ;
141
142 : exec-args-with-path ( seq -- int )
143     [ first ] keep exec-with-path ;
144
145 : exec-args-with-env  ( seq seq -- int )
146     [ [ first ] keep ] dip exec-with-env ;
147
148 : with-fork ( child parent -- )
149     [ call-fork ] 2dip if-zero ; inline
150
151 FUNCTION: int kill ( pid_t pid, int sig )
152 FUNCTION: int raise ( int sig )
153
154
155 CONSTANT: PRIO_PROCESS 0
156 CONSTANT: PRIO_PGRP 1
157 CONSTANT: PRIO_USER 2
158
159 CONSTANT: PRIO_MIN -20
160 CONSTANT: PRIO_MAX 20
161
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 )
165
166 : set-priority ( n -- )
167     [ 0 0 ] dip setpriority io-error ;
168
169 ! Flags for waitpid
170
171 CONSTANT: WNOHANG   1
172 CONSTANT: WUNTRACED 2
173
174 CONSTANT: WSTOPPED   2
175 CONSTANT: WEXITED    4
176 CONSTANT: WCONTINUED 8
177 CONSTANT: WNOWAIT    0x1000000
178
179 ! Examining status
180
181 : WTERMSIG ( status -- value )
182     0x7f bitand ; inline
183
184 : WIFEXITED ( status -- ? )
185     WTERMSIG 0 = ; inline
186
187 : WEXITSTATUS ( status -- value )
188     0xff00 bitand -8 shift ; inline
189
190 : WIFSIGNALED ( status -- ? )
191     0x7f bitand 1 + -1 shift 0 > ; inline
192
193 : WCOREFLAG ( -- value )
194     0x80 ; inline
195
196 : WCOREDUMP ( status -- ? )
197     WCOREFLAG bitand 0 = not ; inline
198
199 : WIFSTOPPED ( status -- ? )
200     0xff bitand 0x7f = ; inline
201
202 : WSTOPSIG ( status -- value )
203     WEXITSTATUS ; inline
204
205 FUNCTION: pid_t wait ( int* status )
206 FUNCTION: pid_t waitpid ( pid_t wpid, int* status, int options )