1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data alien.syntax classes
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.signals unix.stat unix.types ;
12 CONSTANT: file-mode 0o0666
14 GENERIC: handle-fd ( handle -- fd )
16 TUPLE: fd < disposable fd ;
18 : init-fd ( fd -- fd )
21 dup fd>> F_SETFL O_NONBLOCK [ fcntl ] unix-system-call drop
22 dup fd>> F_SETFD FD_CLOEXEC [ fcntl ] unix-system-call drop
26 fd new-disposable swap >>fd ;
33 [ unregister-disposable ]
38 M: fd handle-fd check-disposed fd>> ;
40 M: fd cancel-operation
44 [ remove-input-callbacks [ t swap resume-with ] each ]
45 [ remove-output-callbacks [ t swap resume-with ] each ]
50 fd>> 0 SEEK_CUR [ lseek ] unix-system-call [ io-error ] [ ] bi ;
54 { io:seek-absolute [ SEEK_SET ] }
55 { io:seek-relative [ SEEK_CUR ] }
56 { io:seek-end [ SEEK_END ] }
59 [ fd>> swap ] dip [ lseek ] unix-system-call drop ;
61 M: unix can-seek-handle?
62 fd>> SEEK_CUR 0 lseek -1 = not ;
65 fd>> \ stat new [ fstat -1 = not ] keep
66 swap [ st_size>> ] [ drop f ] if ;
70 M: io-timeout summary drop "I/O operation timed out" ;
73 dup +retry+ eq? [ 2drop ] [
74 [ [ self ] dip handle-fd mx get-global ] dip {
75 { +input+ [ add-input-callback ] }
76 { +output+ [ add-output-callback ] }
78 "I/O" suspend [ io-timeout ] when
84 [ buffered-port check-instance buffer>> ] [ fd>> ] bi*
85 over [ buffer-end ] [ buffer-capacity ] bi read
86 { fixnum } declare dup 0 >= [
90 { EINTR [ 2drop +retry+ ] }
91 { EAGAIN [ 2drop +input+ ] }
96 M: unix (wait-to-read)
98 dup handle>> check-disposed refill dup
99 [ dupd wait-for-port (wait-to-read) ] [ 2drop ] if ;
103 [ buffered-port check-instance buffer>> ] [ fd>> ] bi*
104 over [ buffer@ ] [ buffer-length ] bi write
105 { fixnum } declare dup 0 >= [
107 buffer-empty? f +output+ ?
110 { EINTR [ 2drop +retry+ ] }
111 { EAGAIN [ 2drop +output+ ] }
112 { ENOBUFS [ 2drop +output+ ] }
117 M: unix (wait-to-write)
119 dup handle>> check-disposed drain
120 [ wait-for-port ] [ drop ] if* ;
123 mx get-global wait-for-events ;
125 ! On Unix, you're not supposed to set stdin to non-blocking
126 ! because the fd might be shared with another process (either
127 ! parent or child). So what we do is have the VM start a thread
128 ! which pumps data from the real stdin to a pipe. We set the
129 ! pipe to non-blocking, and read from it instead of the real
130 ! stdin. Very crufty, but it will suffice until we get native
131 ! threading support at the language level.
132 TUPLE: stdin < disposable control size data ;
136 [ control>> &dispose drop ]
137 [ size>> &dispose drop ]
138 [ data>> &dispose drop ]
142 : wait-for-stdin ( stdin -- size )
143 [ control>> CHAR: X over io:stream-write1 io:stream-flush ]
144 [ size>> ssize_t swap stream-read-c-ptr ]
147 :: refill-stdin ( buffer stdin size -- )
148 stdin data>> handle-fd buffer buffer-end size read
152 buffer stdin size refill-stdin
157 size = [ "Error reading stdin pipe" throw ] unless
163 buffer>> _ dup wait-for-stdin refill-stdin f
166 M: stdin cancel-operation
167 [ size>> ] [ control>> ] bi [ cancel-operation ] bi@ ;
169 : control-write-fd ( -- fd ) &: control_write uint deref ;
171 : size-read-fd ( -- fd ) &: size_read uint deref ;
173 : data-read-fd ( -- fd ) &: stdin_read uint deref ;
175 : <stdin> ( -- stdin )
177 control-write-fd <fd> <output-port> >>control
178 size-read-fd <fd> init-fd <input-port> >>size
179 data-read-fd <fd> >>data ;
181 : signal-pipe-fd ( -- n )
182 OBJ-SIGNAL-PIPE special-object ; inline
184 : signal-pipe-loop ( port -- )
186 int heap-size _ io:stream-read
187 dup [ int deref dispatch-signal-hook get-global call( x -- ) ] when*
190 : start-signal-pipe-thread ( -- )
192 <fd> init-fd <input-port>
193 '[ _ signal-pipe-loop ] "Signals" spawn drop
202 ! mx io-task for embedding an fd-based mx inside another mx
203 TUPLE: mx-port < port mx ;
205 : <mx-port> ( mx -- port )
206 dup fd>> mx-port <port> swap >>mx ;
208 : multiplexer-error ( n -- n )
210 errno [ EAGAIN = ] [ EINTR = ] bi or
211 [ drop 0 ] [ throw-errno ] if
214 :: ?flag ( n mask symbol -- n )
215 n mask bitand 0 > [ symbol , ] when n ;