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