Source file picos_stdio.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
let nonblock_fds = Picos_htbl.create ~hashed_type:(module Picos_fd.Resource) ()
module Unix = struct
include Unix
type file_descr = Picos_fd.t
let is_oob flag = MSG_OOB == flag
let is_nonblock flag = O_NONBLOCK == flag
let[@inline] intr_req fd =
if Sys.win32 || Picos_htbl.mem nonblock_fds (Picos_fd.unsafe_get fd) then
Picos_select.Intr.nothing
else Picos_select.Intr.req ~seconds:0.000_01
let rec again_0 fd fn op =
let intr = intr_req fd in
match fn (Picos_fd.unsafe_get fd) with
| result ->
Picos_select.Intr.clr intr;
result
| exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) ->
Picos_select.Intr.clr intr;
again_0 (Picos_select.await_on fd op) fn op
| exception exn ->
Picos_select.Intr.clr intr;
raise exn
let rec again_cloexec_0 ?cloexec fd fn op =
let intr = intr_req fd in
match fn ?cloexec (Picos_fd.unsafe_get fd) with
| result ->
Picos_select.Intr.clr intr;
result
| exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) ->
Picos_select.Intr.clr intr;
again_cloexec_0 ?cloexec (Picos_select.await_on fd op) fn op
| exception exn ->
Picos_select.Intr.clr intr;
raise exn
let rec again_3 fd x1 x2 x3 fn op =
let intr = intr_req fd in
match fn (Picos_fd.unsafe_get fd) x1 x2 x3 with
| result ->
Picos_select.Intr.clr intr;
result
| exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) ->
Picos_select.Intr.clr intr;
again_3 (Picos_select.await_on fd op) x1 x2 x3 fn op
| exception exn ->
Picos_select.Intr.clr intr;
raise exn
let rec again_4 fd x1 x2 x3 x4 fn op =
let intr = intr_req fd in
match fn (Picos_fd.unsafe_get fd) x1 x2 x3 x4 with
| result ->
Picos_select.Intr.clr intr;
result
| exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) ->
Picos_select.Intr.clr intr;
again_4 (Picos_select.await_on fd op) x1 x2 x3 x4 fn op
| exception exn ->
Picos_select.Intr.clr intr;
raise exn
let rec again_5 fd x1 x2 x3 x4 x5 fn op =
let intr = intr_req fd in
match fn (Picos_fd.unsafe_get fd) x1 x2 x3 x4 x5 with
| result ->
Picos_select.Intr.clr intr;
result
| exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) ->
Picos_select.Intr.clr intr;
again_5 (Picos_select.await_on fd op) x1 x2 x3 x4 x5 fn op
| exception exn ->
Picos_select.Intr.clr intr;
raise exn
let progress_1 fd x1 fn op name =
let intr = intr_req fd in
match fn (Picos_fd.unsafe_get fd) x1 with
| () -> Picos_select.Intr.clr intr
| exception
Unix.Unix_error ((EAGAIN | EINPROGRESS | EINTR | EWOULDBLOCK), _, _) ->
begin
Picos_select.Intr.clr intr;
let fd = Picos_select.await_on fd op in
match Unix.getsockopt_error (Picos_fd.unsafe_get fd) with
| None -> ()
| Some error -> raise (Unix.Unix_error (error, name, ""))
end
| exception exn ->
Picos_select.Intr.clr intr;
raise exn
let stdin = Picos_fd.create ~dispose:false Unix.stdin
and stdout = Picos_fd.create ~dispose:false Unix.stdout
and stderr = Picos_fd.create ~dispose:false Unix.stderr
bs.opengroup.org/onlinepubs/9699919799/functions/open.html *)
let openfile path flags file_perm =
let fd = Unix.openfile path flags file_perm in
if List.exists is_nonblock flags then
Picos_htbl.try_add nonblock_fds fd () |> ignore;
Picos_fd.create fd
let close fd =
let _ : bool =
Picos_htbl.try_remove nonblock_fds (Picos_fd.unsafe_get fd)
in
Picos_fd.decr ~close:true fd
let fsync fd = again_0 fd Unix.fsync `W
let read fd bytes pos len = again_3 fd bytes pos len Unix.read `R
let write fd bytes pos len = again_3 fd bytes pos len Unix.write `W
let single_write fd bytes pos len =
again_3 fd bytes pos len Unix.single_write `W
let write_substring fd string pos len =
again_3 fd string pos len Unix.write_substring `W
let single_write_substring fd string pos len =
again_3 fd string pos len Unix.single_write_substring `W
let lseek fd amount seek_command =
Unix.lseek (Picos_fd.unsafe_get fd) amount seek_command
let ftruncate fd size = Unix.ftruncate (Picos_fd.unsafe_get fd) size
let fstat fd = Unix.fstat (Picos_fd.unsafe_get fd)
let isatty fd = Unix.isatty (Picos_fd.unsafe_get fd)
module LargeFile = struct
include Unix.LargeFile
let lseek fd amount seek_command =
Unix.LargeFile.lseek (Picos_fd.unsafe_get fd) amount seek_command
let ftruncate fd size =
Unix.LargeFile.ftruncate (Picos_fd.unsafe_get fd) size
let fstat fd = Unix.LargeFile.fstat (Picos_fd.unsafe_get fd)
end
let map_file fd ?pos kind layout shared dims =
Unix.map_file (Picos_fd.unsafe_get fd) ?pos kind layout shared dims
let fchmod fd file_perm = Unix.fchmod (Picos_fd.unsafe_get fd) file_perm
let fchown fd uid gid = Unix.fchown (Picos_fd.unsafe_get fd) uid gid
let dup ?cloexec fd =
Picos_fd.create (Unix.dup ?cloexec (Picos_fd.unsafe_get fd))
let dup2 ?cloexec src dst =
Unix.dup2 ?cloexec (Picos_fd.unsafe_get src) (Picos_fd.unsafe_get dst)
let set_nonblock fd =
Unix.set_nonblock (Picos_fd.unsafe_get fd);
Picos_htbl.try_add nonblock_fds (Picos_fd.unsafe_get fd) () |> ignore
let clear_nonblock fd =
Unix.clear_nonblock (Picos_fd.unsafe_get fd);
Picos_htbl.try_remove nonblock_fds (Picos_fd.unsafe_get fd) |> ignore
let set_close_on_exec fd = Unix.set_close_on_exec (Picos_fd.unsafe_get fd)
let clear_close_on_exec fd = Unix.clear_close_on_exec (Picos_fd.unsafe_get fd)
let pipe ?cloexec () =
let inn, out = Unix.pipe ?cloexec () in
(Picos_fd.create inn, Picos_fd.create out)
let create_process prog args stdin stdout stderr =
Unix.create_process prog args
(Picos_fd.unsafe_get stdin)
(Picos_fd.unsafe_get stdout)
(Picos_fd.unsafe_get stderr)
let create_process_env prog args env stdin stdout stderr =
Unix.create_process_env prog args env
(Picos_fd.unsafe_get stdin)
(Picos_fd.unsafe_get stdout)
(Picos_fd.unsafe_get stderr)
let lockf fd lock_command length =
Unix.lockf (Picos_fd.unsafe_get fd) lock_command length
let socket ?cloexec socket_domain socket_type protocol =
Picos_fd.create (Unix.socket ?cloexec socket_domain socket_type protocol)
let socketpair ?cloexec socket_domain socket_type mystery =
let fst, snd = Unix.socketpair ?cloexec socket_domain socket_type mystery in
(Picos_fd.create fst, Picos_fd.create snd)
let accept ?cloexec fd =
let fd, sockaddr = again_cloexec_0 ?cloexec fd Unix.accept `R in
(Picos_fd.create fd, sockaddr)
let bind fd sockaddr = progress_1 fd sockaddr Unix.bind `W "bind"
let connect fd sockaddr = progress_1 fd sockaddr Unix.connect `W "connect"
let listen fd max_pending = Unix.listen (Picos_fd.unsafe_get fd) max_pending
let shutdown fd shutdown_command =
Unix.shutdown (Picos_fd.unsafe_get fd) shutdown_command
let getsockname fd = Unix.getsockname (Picos_fd.unsafe_get fd)
let getpeername fd = Unix.getpeername (Picos_fd.unsafe_get fd)
let recv fd bytes offset length flags =
again_4 fd bytes offset length flags Unix.recv
(if List.exists is_oob flags then `E else `R)
let recvfrom fd bytes offset length flags =
again_4 fd bytes offset length flags Unix.recvfrom
(if List.exists is_oob flags then `E else `R)
let send fd bytes offset length flags =
again_4 fd bytes offset length flags Unix.send `W
let send_substring fd string offset length flags =
again_4 fd string offset length flags Unix.send_substring `W
let sendto fd bytes offset length flags sockaddr =
again_5 fd bytes offset length flags sockaddr Unix.sendto `W
let sendto_substring fd string offset length flags sockaddr =
again_5 fd string offset length flags sockaddr Unix.sendto_substring `W
let getsockopt fd option = Unix.getsockopt (Picos_fd.unsafe_get fd) option
let setsockopt fd option bool =
Unix.setsockopt (Picos_fd.unsafe_get fd) option bool
let getsockopt_int fd option =
Unix.getsockopt_int (Picos_fd.unsafe_get fd) option
let setsockopt_int fd option int =
Unix.setsockopt_int (Picos_fd.unsafe_get fd) option int
let getsockopt_optint fd option =
Unix.getsockopt_optint (Picos_fd.unsafe_get fd) option
let setsockopt_optint fd option optint =
Unix.setsockopt_optint (Picos_fd.unsafe_get fd) option optint
let getsockopt_float fd option =
Unix.getsockopt_float (Picos_fd.unsafe_get fd) option
let setsockopt_float fd option float =
Unix.setsockopt_float (Picos_fd.unsafe_get fd) option float
let getsockopt_error fd = Unix.getsockopt_error (Picos_fd.unsafe_get fd)
let tcgetattr fd = Unix.tcgetattr (Picos_fd.unsafe_get fd)
let tcsetattr fd setattr_when terminal_io =
Unix.tcsetattr (Picos_fd.unsafe_get fd) setattr_when terminal_io
let tcsendbreak fd duration =
Unix.tcsendbreak (Picos_fd.unsafe_get fd) duration
let tcdrain fd = Unix.tcdrain (Picos_fd.unsafe_get fd)
let tcflush fd flush_queue = Unix.tcflush (Picos_fd.unsafe_get fd) flush_queue
let tcflow fd flow_action = Unix.tcflow (Picos_fd.unsafe_get fd) flow_action
end