]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/process/process.factor
use radix literals
[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 unix.ffi ;
5 IN: unix.process
6
7 ! Low-level Unix process launching utilities. These are used
8 ! to implement io.launcher on Unix. User code should use
9 ! io.launcher instead.
10
11 FUNCTION: pid_t fork ( ) ;
12
13 : fork-process ( -- pid ) [ fork ] unix-system-call ;
14
15 FUNCTION: int execv ( c-string path, c-string* argv ) ;
16 FUNCTION: int execvp ( c-string path, c-string* argv ) ;
17 FUNCTION: int execve ( c-string path, c-string* argv, c-string* envp ) ;
18
19 : exec ( pathname argv -- int )
20     [ utf8 malloc-string ] [ utf8 strings>alien ] bi* execv ;
21
22 : exec-with-path ( filename argv -- int )
23     [ utf8 malloc-string ] [ utf8 strings>alien ] bi* execvp ;
24
25 : exec-with-env ( filename argv envp -- int )
26     [ utf8 malloc-string ]
27     [ utf8 strings>alien ]
28     [ utf8 strings>alien ] tri* execve ;
29
30 : exec-args ( seq -- int )
31     [ first ] [ ] bi exec ;
32
33 : exec-args-with-path ( seq -- int )
34     [ first ] [ ] bi exec-with-path ;
35
36 : exec-args-with-env  ( seq seq -- int )
37     [ [ first ] [ ] bi ] dip exec-with-env ;
38
39 : with-fork ( child parent -- )
40     [ fork-process ] 2dip if-zero ; inline
41
42 FUNCTION: int kill ( pid_t pid, int sig ) ;
43 FUNCTION: int raise ( int sig ) ;
44
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    0x1000000
69
70 ! Examining status
71
72 : WTERMSIG ( status -- value )
73     0x7f bitand ; inline
74
75 : WIFEXITED ( status -- ? )
76     WTERMSIG 0 = ; inline
77
78 : WEXITSTATUS ( status -- value )
79     0xff00 bitand -8 shift ; inline
80
81 : WIFSIGNALED ( status -- ? )
82     0x7f bitand 1 + -1 shift 0 > ; inline
83
84 : WCOREFLAG ( -- value )
85     0x80 ; inline
86
87 : WCOREDUMP ( status -- ? )
88     WCOREFLAG bitand 0 = not ; inline
89
90 : WIFSTOPPED ( status -- ? )
91     0xff bitand 0x7f = ; 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 ) ;