Landin library reference discuss source

hosted/events/windows_programs.ldn

1--  The event provider's `io.programs` on Windows (D479, D487): a child is
2--  started by the system handle this provider is built over, which does
3--  not wait, and the parent's ends of its pipes are overlapped already, so
4--  reading and writing them park the fibre on the port as any overlapped
5--  call does.  A read of a child with both streams open asks each with a
6--  read of no bytes, which ends when the pipe has something or has ended,
7--  and reads the one that has; the output when both have.  Waiting for the
8--  child's end parks the fibre too: the child is put in a job whose
9--  messages go to the loop's port, and the job's message that the process
10--  ended answers it.  A child the system will not put in the job, which a
11--  job of the program's own may forbid, is waited for by a wait that holds
12--  the thread, as Linux before `pidfd_open` reaps one.
13
14import core/fibre
15import core/io
16import core/mem
17import core/text
18import hosted/system
19import platform/windows
20
21fixed if compiler.os == windows then
22    events_spawn: (self: host, path: text.os_string,
23                   arguments: []text.os_string,
24                   environment: []text.os_variable, streams: io.child_streams)
25                  -> (started: io.child)
26                  ! mem.not_found | io.no_access | io.io_failed =
27        started = try system.start(self.services, path, arguments,
28                                   environment, streams)
29    end events_spawn
30
31    --  Close one of a child's pipes, waking whoever waits on it first; it
32    --  is closed even when the system reports a failure.
33    close_pipe: (self: host, opened: io.file) -> none ! io.io_failed =
34        closing: windows.handle = system.handle_of(opened)
35        forget(self, closing)
36        if windows.close_handle(closing) == 0 then
37            record(self, windows.last_error())
38            fail io.io_failed
39        end if
40    end close_pipe
41
42    events_write_child: (self: host, inout started: io.child, bytes: []u8)
43                        -> none ! io.io_failed =
44        cleared(self)
45        fail io.io_failed when not started.input_open
46        try write_all(self, started.input, bytes)
47    end events_write_child
48
49    events_close_input: (self: host, inout started: io.child)
50                        -> none ! io.io_failed =
51        cleared(self)
52        fail io.io_failed when not started.input_open
53        started.input_open = false
54        try close_pipe(self, started.input)
55    end events_close_input
56
57    --  Whether the pipe `made` has something to read, or has ended, now.
58    has_now: (made: windows.handle) -> (yes: bool) =
59        mut available: u32 = 0
60        yes = windows.peek_named_pipe(made, 0, 0, 0, addr available, 0) == 0
61              or available > 0
62    end has_now
63
64    --  Whether a read of no bytes ended with the pipe having something or
65    --  having ended, rather than being cancelled.
66    ended_ready: (made: windows.handle, entry: ptr mut waiting) -> (yes: bool) =
67        mut got: u32 = 0
68        yes = windows.overlapped_result(made, addr entry.val.asked, addr got,
69                                        0) <> 0
70              or windows.last_error() <> windows.error_operation_aborted
71    end ended_ready
72
73    --  Whether the child's errors, and not its output, is what to read
74    --  next, both being open: the output when it has anything, its end
75    --  included, and the errors only when they alone do.  When neither has
76    --  now, each is asked by a read of no bytes, and the fibre waits for
77    --  the first to end; the other is cancelled, and its packet waited for.
78    errors_first: (self: host, started: io.child) -> (yes: bool)
79                  ! io.io_failed =
80        yes = false
81        output: windows.handle = system.handle_of(started.output)
82        errors: windows.handle = system.handle_of(started.errors)
83        return when has_now(output)
84        yes = true
85        return when has_now(errors)
86        yes = false
87        try joined(self, output)
88        try joined(self, errors)
89        mut spare: [2]u8 = zeroed
90        mut first: waiting = on_handle(output, 0)
91        mut second: waiting = on_handle(errors, 0)
92        state: ptr mut loop_state = self.state
93        link(state, addr first)
94        mut got: u32 = 0
95        if windows.read_file(output, addr spare[0], 0, addr got,
96                             usize(addr first.asked)) == 0
97          and windows.last_error() <> windows.error_io_pending then
98            --  Refused at once: an ended pipe, read next to find its end.
99            unlink(state, addr first)
100            return
101        end if
102        link(state, addr second)
103        if windows.read_file(errors, addr spare[1], 0, addr got,
104                             usize(addr second.asked)) == 0
105          and windows.last_error() <> windows.error_io_pending then
106            unlink(state, addr second)
107            cut_short(addr first, ready)
108            _ = answered(self, addr first)
109            yes = not ended_ready(output, addr first)
110            return
111        end if
112        while first.answer == pending and second.answer == pending do
113            fibre.yield(self.round)
114        end while
115        if first.answer == pending then cut_short(addr first, ready) end if
116        if second.answer == pending then cut_short(addr second, ready) end if
117        _ = answered(self, addr first)
118        _ = answered(self, addr second)
119        yes = not ended_ready(output, addr first)
120              and ended_ready(errors, addr second)
121    end errors_first
122
123    events_read_child: (self: host, inout started: io.child, into: []mut u8)
124                       -> (taken: io.child_read) ! io.io_failed =
125        cleared(self)
126        taken = (count: 0, stream: io.child_output,
127                 finished: not started.output_open and not started.errors_open)
128        return when lenof into == 0
129        loop do
130            if not started.output_open and not started.errors_open then
131                taken = (count: 0, stream: io.child_output, finished: true)
132                return
133            end if
134            mut from_errors: bool = not started.output_open
135            if started.output_open and started.errors_open then
136                from_errors = try errors_first(self, started)
137            end if
138            opened: io.file = if from_errors then started.errors
139                              else started.output end if
140            count: usize = try read_once(self, opened, into)
141            if count > 0 then
142                taken = (count: count,
143                         stream: if from_errors then io.child_errors
144                                 else io.child_output end if,
145                         finished: false)
146                return
147            end if
148            if from_errors then
149                started.errors_open = false
150            else
151                started.output_open = false
152            end if
153            try close_pipe(self, opened)
154        end loop
155    end events_read_child
156
157    --  Close every pipe the child still has, keeping the first failure.
158    close_pipes: (self: host, started: io.child) -> (closed: bool) =
159        closed = true
160        if started.input_open then
161            close_pipe(self, started.input) else (problem)
162                _ = problem
163                closed = false
164            end
165        end if
166        if started.output_open then
167            close_pipe(self, started.output) else (problem)
168                _ = problem
169                closed = false
170            end
171        end if
172        if started.errors_open then
173            close_pipe(self, started.errors) else (problem)
174                _ = problem
175                closed = false
176            end
177        end if
178    end close_pipes
179
180    --  The loop's job, made at the first wait for a child: its processes'
181    --  ends go to the port, and a process one of them starts is not in it,
182    --  so only the children this provider started are.  Zero when the
183    --  system makes none.
184    job_of: (state: ptr mut loop_state) -> (job: windows.handle) =
185        job = state.val.job
186        return when job <> windows.no_handle
187        made: windows.handle = windows.create_job(0, 0)
188        return when made == windows.no_handle
189        mut limits: windows.job_limits = zeroed
190        limits.flags = windows.job_silent_breakaway
191        mut port: windows.job_port = (key: job_key, port: state.val.queue)
192        if windows.set_job(made, windows.job_basic_limits, usize(addr limits),
193                           u32(sizeof windows.job_limits)) == 0
194          or windows.set_job(made, windows.job_completion_port,
195                             usize(addr port),
196                             u32(sizeof windows.job_port)) == 0 then
197            _ = windows.close_handle(made)
198            return
199        end if
200        state.val.job = made
201        job = made
202    end job_of
203
204    --  Close the child's handle, whatever the close says.
205    let_go: (child: windows.handle) -> none =
206        _ = windows.close_handle(child)
207    end let_go
208
209    --  Whether `child` has ended.
210    over_now: (child: windows.handle) -> (yes: bool) =
211        yes = windows.wait_for(child, 0) == windows.wait_object_0
212    end over_now
213
214    --  Wait for `child`'s end, taking turns: in the loop's job, or, when it
215    --  cannot be put there, by a wait that holds the thread.  A process that
216    --  ended before it was put in the job sends no message, so the child is
217    --  asked after it is put there; a message about an earlier child whose
218    --  ID it has now is not taken for its own end, since it is asked again.
219    child_ended: (self: host, child: windows.handle) -> none ! io.io_failed =
220        return when over_now(child)
221        job: windows.handle = job_of(self.state)
222        if job == windows.no_handle
223          or windows.assign_to_job(job, child) == 0 then
224            if windows.wait_for(child, windows.infinite)
225              == windows.wait_failed then
226                record(self, windows.last_error())
227                fail io.io_failed
228            end if
229            return
230        end if
231        identity: i32 = i32(windows.process_id_of(child))
232        while not over_now(child) do
233            mut entry: waiting = on_deadline(0)
234            entry.child = identity
235            parked(self, addr entry)
236        end while
237    end child_ended
238
239    --  The pipes are closed before the wait, so a child still writing to
240    --  one meets a closed pipe rather than waiting for a reader that never
241    --  comes.  The child's handle is closed once its code is read.
242    events_wait_child: (self: host, sink started: io.child)
243                       -> (ended: io.ending) ! io.io_failed =
244        cleared(self)
245        closed: bool = close_pipes(self, started)
246        child: windows.handle = system.process_of(started)
247        defer let_go(child)
248        try child_ended(self, child)
249        mut code: u32 = 0
250        if windows.exit_code_of(child, addr code) == 0 then
251            record(self, windows.last_error())
252            fail io.io_failed
253        end if
254        ended = system.ended(self.services, code)
255        fail io.io_failed when not closed
256    end events_wait_child
257
258    host is io.programs
259        (spawn: events_spawn, write_child: events_write_child,
260         close_input: events_close_input, read_child: events_read_child,
261         wait: events_wait_child)
262end if