]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/process/process.factor
Fix permission bits
[factor.git] / basis / unix / process / process.factor
1 USING: kernel alien.c-types alien.strings sequences math alien.syntax unix
2        vectors kernel namespaces continuations threads assocs vectors
3        io.unix.backend io.encodings.utf8 ;
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 : >argv ( seq -- alien )
19     [ utf8 malloc-string ] map f suffix >c-void*-array ;
20
21 : exec ( pathname argv -- int )
22     [ utf8 malloc-string ] [ >argv ] bi* execv ;
23
24 : exec-with-path ( filename argv -- int )
25     [ utf8 malloc-string ] [ >argv ] bi* execvp ;
26
27 : exec-with-env ( filename argv envp -- int )
28     [ utf8 malloc-string ] [ >argv ] [ >argv ] 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     >r [ first ] [ ] bi r> exec-with-env ;
38
39 : with-fork ( child parent -- )
40     [ [ fork-process dup zero? ] dip [ drop ] prepose ] dip
41     if ; inline
42
43 : SIGKILL 9 ; inline
44 : SIGTERM 15 ; inline
45
46 FUNCTION: int kill ( pid_t pid, int sig ) ;
47
48 : PRIO_PROCESS 0 ; inline
49 : PRIO_PGRP 1 ; inline
50 : PRIO_USER 2 ; inline
51
52 : PRIO_MIN -20 ; inline
53 : PRIO_MAX 20 ; inline
54
55 ! which/who = 0 for current process
56 FUNCTION: int getpriority ( int which, int who ) ;
57 FUNCTION: int setpriority ( int which, int who, int prio ) ;
58
59 : set-priority ( n -- )
60     0 0 rot setpriority io-error ;
61
62 ! Flags for waitpid
63
64 : WNOHANG   1 ; inline
65 : WUNTRACED 2 ; inline
66
67 : WSTOPPED   2 ; inline
68 : WEXITED    4 ; inline
69 : WCONTINUED 8 ; inline
70 : WNOWAIT    HEX: 1000000 ; inline
71
72 ! Examining status
73
74 : WTERMSIG ( status -- value )
75     HEX: 7f bitand ; inline
76
77 : WIFEXITED ( status -- ? )
78     WTERMSIG zero? ; inline
79
80 : WEXITSTATUS ( status -- value )
81     HEX: ff00 bitand -8 shift ; inline
82
83 : WIFSIGNALED ( status -- ? )
84     HEX: 7f bitand 1+ -1 shift 0 > ; inline
85
86 : WCOREFLAG ( -- value )
87     HEX: 80 ; inline
88
89 : WCOREDUMP ( status -- ? )
90     WCOREFLAG bitand zero? not ; inline
91
92 : WIFSTOPPED ( status -- ? )
93     HEX: ff bitand HEX: 7f = ; inline
94
95 : WSTOPSIG ( status -- value )
96     WEXITSTATUS ; inline
97
98 FUNCTION: pid_t wait ( int* status ) ;
99 FUNCTION: pid_t waitpid ( pid_t wpid, int* status, int options ) ;
100
101 : wait-for-pid ( pid -- status )
102     0 <int> [ 0 waitpid drop ] keep *int WEXITSTATUS ;