]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/process/process.factor
Updating code to use with-out-parameters
[factor.git] / basis / unix / process / process.factor
1 USING: kernel alien.c-types alien.data alien.strings sequences
2 math alien.syntax unix namespaces continuations threads assocs
3 io.backend.unix io.encodings.utf8 unix.types unix.utilities fry ;
4 IN: unix.process
5
6 ! Low-level Unix process launching utilities. These are used
7 ! to implement io.launcher on Unix. User code should use
8 ! io.launcher instead.
9
10 FUNCTION: pid_t fork ( ) ;
11
12 : fork-process ( -- pid ) [ fork ] unix-system-call ;
13
14 FUNCTION: int execv ( c-string path, c-string* argv ) ;
15 FUNCTION: int execvp ( c-string path, c-string* argv ) ;
16 FUNCTION: int execve ( c-string path, c-string* argv, c-string* envp ) ;
17
18 : exec ( pathname argv -- int )
19     [ utf8 malloc-string ] [ utf8 strings>alien ] bi* execv ;
20
21 : exec-with-path ( filename argv -- int )
22     [ utf8 malloc-string ] [ utf8 strings>alien ] bi* execvp ;
23
24 : exec-with-env ( filename argv envp -- int )
25     [ utf8 malloc-string ]
26     [ utf8 strings>alien ]
27     [ utf8 strings>alien ] tri* execve ;
28
29 : exec-args ( seq -- int )
30     [ first ] [ ] bi exec ;
31
32 : exec-args-with-path ( seq -- int )
33     [ first ] [ ] bi exec-with-path ;
34
35 : exec-args-with-env  ( seq seq -- int )
36     [ [ first ] [ ] bi ] dip exec-with-env ;
37
38 : with-fork ( child parent -- )
39     [ fork-process ] 2dip if-zero ; inline
40
41 CONSTANT: SIGKILL 9
42 CONSTANT: SIGTERM 15
43
44 FUNCTION: int kill ( pid_t pid, int sig ) ;
45
46 CONSTANT: PRIO_PROCESS 0
47 CONSTANT: PRIO_PGRP 1
48 CONSTANT: PRIO_USER 2
49
50 CONSTANT: PRIO_MIN -20
51 CONSTANT: PRIO_MAX 20
52
53 ! which/who = 0 for current process
54 FUNCTION: int getpriority ( int which, int who ) ;
55 FUNCTION: int setpriority ( int which, int who, int prio ) ;
56
57 : set-priority ( n -- )
58     [ 0 0 ] dip setpriority io-error ;
59
60 ! Flags for waitpid
61
62 CONSTANT: WNOHANG   1
63 CONSTANT: WUNTRACED 2
64
65 CONSTANT: WSTOPPED   2
66 CONSTANT: WEXITED    4
67 CONSTANT: WCONTINUED 8
68 CONSTANT: WNOWAIT    HEX: 1000000
69
70 ! Examining status
71
72 : WTERMSIG ( status -- value )
73     HEX: 7f bitand ; inline
74
75 : WIFEXITED ( status -- ? )
76     WTERMSIG 0 = ; inline
77
78 : WEXITSTATUS ( status -- value )
79     HEX: ff00 bitand -8 shift ; inline
80
81 : WIFSIGNALED ( status -- ? )
82     HEX: 7f bitand 1 + -1 shift 0 > ; inline
83
84 : WCOREFLAG ( -- value )
85     HEX: 80 ; inline
86
87 : WCOREDUMP ( status -- ? )
88     WCOREFLAG bitand 0 = not ; inline
89
90 : WIFSTOPPED ( status -- ? )
91     HEX: ff bitand HEX: 7f = ; inline
92
93 : WSTOPSIG ( status -- value )
94     WEXITSTATUS ; inline
95
96 FUNCTION: pid_t wait ( int* status ) ;
97 FUNCTION: pid_t waitpid ( pid_t wpid, int* status, int options ) ;