]> gitweb.factorcode.org Git - factor.git/blob - basis/io/unix/launcher/launcher.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / basis / io / unix / launcher / launcher.factor
1 ! Copyright (C) 2007, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel namespaces math system sequences
4 continuations arrays assocs combinators alien.c-types strings
5 threads accessors environment
6 io io.backend io.launcher io.ports io.files
7 io.files.private io.unix.files io.unix.backend
8 io.unix.launcher.parser
9 unix unix.process ;
10 IN: io.unix.launcher
11
12 ! Search unix first
13 USE: unix
14
15 : get-arguments ( process -- seq )
16     command>> dup string? [ tokenize-command ] when ;
17
18 : assoc>env ( assoc -- env )
19     [ "=" glue ] { } assoc>map ;
20
21 : setup-priority ( process -- process )
22     dup priority>> [
23         H{
24             { +lowest-priority+ 20 }
25             { +low-priority+ 10 }
26             { +normal-priority+ 0 }
27             { +high-priority+ -10 }
28             { +highest-priority+ -20 }
29             { +realtime-priority+ -20 }
30         } at set-priority
31     ] when* ;
32
33 : reset-fd ( fd -- )
34     [ F_SETFL 0 fcntl io-error ] [ F_SETFD 0 fcntl io-error ] bi ;
35
36 : redirect-fd ( oldfd fd -- )
37     2dup = [ 2drop ] [ dup2 io-error ] if ;
38
39 : redirect-file ( obj mode fd -- )
40     [ [ normalize-path ] dip file-mode open-file ] dip redirect-fd ;
41
42 : redirect-file-append ( obj mode fd -- )
43     [ drop path>> normalize-path open-append ] dip redirect-fd ;
44
45 : redirect-closed ( obj mode fd -- )
46     [ drop "/dev/null" ] 2dip redirect-file ;
47
48 : redirect ( obj mode fd -- )
49     {
50         { [ pick not ] [ 3drop ] }
51         { [ pick string? ] [ redirect-file ] }
52         { [ pick appender? ] [ redirect-file-append ] }
53         { [ pick +closed+ eq? ] [ redirect-closed ] }
54         { [ pick fd? ] [ [ drop fd>> dup reset-fd ] dip redirect-fd ] }
55         [ [ underlying-handle ] 2dip redirect ]
56     } cond ;
57
58 : ?closed ( obj -- obj' )
59     dup +closed+ eq? [ drop "/dev/null" ] when ;
60
61 : setup-redirection ( process -- process )
62     dup stdin>> ?closed read-flags 0 redirect
63     dup stdout>> ?closed write-flags 1 redirect
64     dup stderr>> dup +stdout+ eq? [
65         drop 1 2 dup2 io-error
66     ] [
67         ?closed write-flags 2 redirect
68     ] if ;
69
70 : setup-environment ( process -- process )
71     dup pass-environment? [
72         dup get-environment set-os-envs
73     ] when ;
74
75 : spawn-process ( process -- * )
76     [ setup-priority ] [ 250 _exit ] recover
77     [ setup-redirection ] [ 251 _exit ] recover
78     [ current-directory get (normalize-path) cd ] [ 252 _exit ] recover
79     [ setup-environment ] [ 253 _exit ] recover
80     [ get-arguments exec-args-with-path ] [ 254 _exit ] recover
81     255 _exit ;
82
83 M: unix current-process-handle ( -- handle ) getpid ;
84
85 M: unix run-process* ( process -- pid )
86     [ spawn-process ] curry [ ] with-fork ;
87
88 M: unix kill-process* ( pid -- )
89     SIGTERM kill io-error ;
90
91 : find-process ( handle -- process )
92     processes get swap [ nip swap handle>> = ] curry
93     assoc-find 2drop ;
94
95 TUPLE: signal n ;
96
97 : code>status ( code -- obj )
98     dup WIFEXITED [ WEXITSTATUS ] [ WTERMSIG signal boa ] if ;
99
100 M: unix wait-for-processes ( -- ? )
101     -1 0 <int> tuck WNOHANG waitpid
102     dup 0 <= [
103         2drop t
104     ] [
105         find-process dup
106         [ swap *int code>status notify-exit f ] [ 2drop f ] if
107     ] if ;