]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/process/process.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / basis / unix / process / process.factor
1 USING: kernel alien.c-types alien.strings sequences math alien.syntax
2 unix namespaces continuations threads assocs io.backend.unix
3 io.encodings.utf8 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 ( char* path, char** argv ) ;
15 FUNCTION: int execvp ( char* path, char** argv ) ;
16 FUNCTION: int execve ( char* path, char** argv, char** 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 dup zero? ] dip '[ drop @ ] ] dip
40     if ; inline
41
42 CONSTANT: SIGKILL 9
43 CONSTANT: SIGTERM 15
44
45 FUNCTION: int kill ( pid_t pid, int sig ) ;
46
47 CONSTANT: PRIO_PROCESS 0
48 CONSTANT: PRIO_PGRP 1
49 CONSTANT: PRIO_USER 2
50
51 CONSTANT: PRIO_MIN -20
52 CONSTANT: PRIO_MAX 20
53
54 ! which/who = 0 for current process
55 FUNCTION: int getpriority ( int which, int who ) ;
56 FUNCTION: int setpriority ( int which, int who, int prio ) ;
57
58 : set-priority ( n -- )
59     [ 0 0 ] dip setpriority io-error ;
60
61 ! Flags for waitpid
62
63 CONSTANT: WNOHANG   1
64 CONSTANT: WUNTRACED 2
65
66 CONSTANT: WSTOPPED   2
67 CONSTANT: WEXITED    4
68 CONSTANT: WCONTINUED 8
69 CONSTANT: WNOWAIT    HEX: 1000000
70
71 ! Examining status
72
73 : WTERMSIG ( status -- value )
74     HEX: 7f bitand ; inline
75
76 : WIFEXITED ( status -- ? )
77     WTERMSIG 0 = ; inline
78
79 : WEXITSTATUS ( status -- value )
80     HEX: ff00 bitand -8 shift ; inline
81
82 : WIFSIGNALED ( status -- ? )
83     HEX: 7f bitand 1 + -1 shift 0 > ; inline
84
85 : WCOREFLAG ( -- value )
86     HEX: 80 ; inline
87
88 : WCOREDUMP ( status -- ? )
89     WCOREFLAG bitand 0 = not ; inline
90
91 : WIFSTOPPED ( status -- ? )
92     HEX: ff bitand HEX: 7f = ; inline
93
94 : WSTOPSIG ( status -- value )
95     WEXITSTATUS ; inline
96
97 FUNCTION: pid_t wait ( int* status ) ;
98 FUNCTION: pid_t waitpid ( pid_t wpid, int* status, int options ) ;
99
100 : wait-for-pid ( pid -- status )
101     0 <int> [ 0 waitpid drop ] keep *int WEXITSTATUS ;