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