]> gitweb.factorcode.org Git - factor.git/blob - basis/io/backend/unix/unix.factor
basis: ERROR: changes.
[factor.git] / basis / io / backend / unix / unix.factor
1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data alien.syntax
4 classes.struct combinators destructors destructors.private fry
5 io.backend io.backend.unix.multiplexers io.buffers io.files
6 io.ports io.timeouts kernel kernel.private libc locals make math
7 namespaces sequences summary system threads unix unix.ffi
8 unix.stat unix.types ;
9 QUALIFIED: io
10 IN: io.backend.unix
11
12 CONSTANT: file-mode 0o0666
13
14 GENERIC: handle-fd ( handle -- fd )
15
16 TUPLE: fd < disposable fd ;
17
18 : init-fd ( fd -- fd )
19     [
20         |dispose
21         dup fd>> F_SETFL O_NONBLOCK [ fcntl ] unix-system-call drop
22         dup fd>> F_SETFD FD_CLOEXEC [ fcntl ] unix-system-call drop
23     ] with-destructors ;
24
25 : <fd> ( n -- fd )
26     fd new-disposable swap >>fd ;
27
28 M: fd dispose
29     [
30         {
31             [ cancel-operation ]
32             [ t >>disposed drop ]
33             [ unregister-disposable ]
34             [ fd>> close-file ]
35         } cleave
36     ] unless-disposed ;
37
38 M: fd handle-fd check-disposed fd>> ;
39
40 M: fd cancel-operation ( fd -- )
41     [
42         fd>>
43         mx get-global
44         [ remove-input-callbacks [ t swap resume-with ] each ]
45         [ remove-output-callbacks [ t swap resume-with ] each ]
46         2bi
47     ] unless-disposed ;
48
49 M: unix tell-handle ( handle -- n )
50     fd>> 0 SEEK_CUR [ lseek ] unix-system-call [ io-error ] [ ] bi ;
51
52 M: unix seek-handle ( n seek-type handle -- )
53     swap {
54         { io:seek-absolute [ SEEK_SET ] }
55         { io:seek-relative [ SEEK_CUR ] }
56         { io:seek-end [ SEEK_END ] }
57         [ io:bad-seek-type ]
58     } case
59     [ fd>> swap ] dip [ lseek ] unix-system-call drop ;
60
61 M: unix can-seek-handle? ( handle -- ? )
62     fd>> SEEK_CUR 0 lseek -1 = not ;
63
64 M: unix handle-length ( handle -- n/f )
65     fd>> \ stat <struct> [ fstat -1 = not ] keep
66     swap [ st_size>> ] [ drop f ] if ;
67
68 ERROR: io-timeout ;
69
70 M: io-timeout summary drop "I/O operation timed out" ;
71
72 M: unix wait-for-fd ( handle event -- )
73     dup +retry+ eq? [ 2drop ] [
74         [ [ self ] dip handle-fd mx get-global ] dip {
75             { +input+ [ add-input-callback ] }
76             { +output+ [ add-output-callback ] }
77         } case
78         "I/O" suspend [ throw-io-timeout ] when
79     ] if ;
80
81 : wait-for-port ( port event -- )
82     '[ handle>> _ wait-for-fd ] with-timeout ;
83
84 ! Some general stuff
85
86 ERROR: not-a-buffered-port port ;
87
88 : check-buffered-port ( port -- port )
89     dup buffered-port? [ throw-not-a-buffered-port ] unless ; inline
90
91 M: fd refill
92     [ check-buffered-port buffer>> ] [ fd>> ] bi*
93     over [ buffer-end ] [ buffer-capacity ] bi read
94     { fixnum } declare dup 0 >= [
95         swap buffer+ f
96     ] [
97         errno {
98             { EINTR [ 2drop +retry+ ] }
99             { EAGAIN [ 2drop +input+ ] }
100             [ (throw-errno) ]
101         } case
102     ] if ;
103
104 M: unix (wait-to-read) ( port -- )
105     dup
106     dup handle>> check-disposed refill dup
107     [ dupd wait-for-port (wait-to-read) ] [ 2drop ] if ;
108
109 ! Writers
110 M: fd drain
111     [ check-buffered-port buffer>> ] [ fd>> ] bi*
112     over [ buffer@ ] [ buffer-length ] bi write
113     { fixnum } declare dup 0 >= [
114         over buffer-consume
115         buffer-empty? f +output+ ?
116     ] [
117         errno {
118             { EINTR [ 2drop +retry+ ] }
119             { EAGAIN [ 2drop +output+ ] }
120             [ (throw-errno) ]
121         } case
122     ] if ;
123
124 M: unix (wait-to-write) ( port -- )
125     dup
126     dup handle>> check-disposed drain
127     [ wait-for-port ] [ drop ] if* ;
128
129 M: unix io-multiplex ( nanos -- )
130     mx get-global wait-for-events ;
131
132 ! On Unix, you're not supposed to set stdin to non-blocking
133 ! because the fd might be shared with another process (either
134 ! parent or child). So what we do is have the VM start a thread
135 ! which pumps data from the real stdin to a pipe. We set the
136 ! pipe to non-blocking, and read from it instead of the real
137 ! stdin. Very crufty, but it will suffice until we get native
138 ! threading support at the language level.
139 TUPLE: stdin < disposable control size data ;
140
141 M: stdin dispose*
142     [
143         [ control>> &dispose drop ]
144         [ size>> &dispose drop ]
145         [ data>> &dispose drop ]
146         tri
147     ] with-destructors ;
148
149 : wait-for-stdin ( stdin -- size )
150     [ control>> CHAR: X over io:stream-write1 io:stream-flush ]
151     [ size>> ssize_t heap-size swap io:stream-read ssize_t deref ]
152     bi ;
153
154 :: refill-stdin ( buffer stdin size -- )
155     stdin data>> handle-fd buffer buffer-end size read
156     dup 0 < [
157         drop
158         errno EINTR = [
159             buffer stdin size refill-stdin
160         ] [
161             throw-errno
162         ] if
163     ] [
164         size = [ "Error reading stdin pipe" throw ] unless
165         size buffer buffer+
166     ] if ;
167
168 M: stdin refill
169     '[
170         buffer>> _ dup wait-for-stdin refill-stdin f
171     ] with-timeout ;
172
173 M: stdin cancel-operation
174     [ size>> ] [ control>> ] bi [ cancel-operation ] bi@ ;
175
176 : control-write-fd ( -- fd ) &: control_write uint deref ;
177
178 : size-read-fd ( -- fd ) &: size_read uint deref ;
179
180 : data-read-fd ( -- fd ) &: stdin_read uint deref ;
181
182 : <stdin> ( -- stdin )
183     stdin new-disposable
184         control-write-fd <fd> <output-port> >>control
185         size-read-fd <fd> init-fd <input-port> >>size
186         data-read-fd <fd> >>data ;
187
188 SYMBOL: dispatch-signal-hook
189
190 dispatch-signal-hook [ [ drop ] ] initialize
191
192 : signal-pipe-fd ( -- n )
193     OBJ-SIGNAL-PIPE special-object ; inline
194
195 : signal-pipe-loop ( port -- )
196     '[
197         int heap-size _ io:stream-read
198         dup [ int deref dispatch-signal-hook get call( x -- ) ] when*
199     ] loop ;
200
201 : start-signal-pipe-thread ( -- )
202     signal-pipe-fd [
203         <fd> init-fd <input-port>
204         '[ _ signal-pipe-loop ] "Signals" spawn drop
205     ] when* ;
206
207 M: unix init-stdio
208     <stdin> <input-port>
209     1 <fd> <output-port>
210     2 <fd> <output-port>
211     set-stdio ;
212
213 ! mx io-task for embedding an fd-based mx inside another mx
214 TUPLE: mx-port < port mx ;
215
216 : <mx-port> ( mx -- port )
217     dup fd>> mx-port <port> swap >>mx ;
218
219 : multiplexer-error ( n -- n )
220     dup 0 < [
221         errno [ EAGAIN = ] [ EINTR = ] bi or
222         [ drop 0 ] [ throw-errno ] if
223     ] when ;
224
225 :: ?flag ( n mask symbol -- n )
226     n mask bitand 0 > [ symbol , ] when n ;