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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
module Bitv = Miou_bitv
module Poll = Miou_poll
module Logs = Miou.Logs.Make (struct
let src = "unix"
end)
type kind = STREAM | DGRAM
type file_descr = { fd: Unix.file_descr; kind: kind option; non_blocking: bool }
let is_dgram { kind; _ } = match kind with Some DGRAM -> true | _ -> false
let of_file_descr ?(non_blocking = true) fd =
if non_blocking then Unix.set_nonblock fd else Unix.clear_nonblock fd;
match Unix.getsockopt_int fd Unix.SO_TYPE with
| 1 -> { fd; kind= Some STREAM; non_blocking }
| 2 -> { fd; kind= Some DGRAM; non_blocking }
| _ ->
failwith
"Miou_unix.of_file_descr: invalid file-descriptor (neither TCP nor UDP)"
| exception _exn -> { fd; kind= None; non_blocking }
let to_file_descr { fd; _ } = fd
let nonblocking_stream fam =
let open Unix in
let fd = socket fam SOCK_STREAM 0 in
set_nonblock fd;
{ fd; non_blocking= true; kind= Some STREAM }
let nonblocking_dgram fam =
let open Unix in
let fd = socket fam SOCK_DGRAM 0 in
set_nonblock fd;
{ fd; non_blocking= true; kind= Some DGRAM }
let unix_socket () = nonblocking_stream Unix.PF_UNIX
let tcpv4 () = nonblocking_stream Unix.PF_INET
let tcpv6 () = nonblocking_stream Unix.PF_INET6
let udpv4 () = nonblocking_dgram Unix.PF_INET
let udpv6 () = nonblocking_dgram Unix.PF_INET6
let bind_and_listen ?(backlog = 64) ?(reuseaddr = true) ?(reuseport = true)
({ fd; _ } as file_descr) sockaddr =
let open Unix in
setsockopt fd SO_REUSEADDR reuseaddr;
setsockopt fd SO_REUSEPORT reuseport;
bind fd sockaddr;
if not (is_dgram file_descr) then listen fd backlog
module File_descrs = struct
type nonrec 'a t = { mutable contents: (Unix.file_descr * 'a) list }
let find tbl fd = List.assq fd tbl.contents
let find_opt tbl fd = List.assq_opt fd tbl.contents
let remove tbl fd =
let contents =
List.fold_left
(fun acc (k, v) -> if k == fd then acc else (k, v) :: acc)
[] tbl.contents
in
tbl.contents <- contents
let replace tbl fd v' =
let contents =
List.fold_left
(fun acc (k, v) -> if k == fd then (k, v') :: acc else (k, v) :: acc)
[] tbl.contents
in
tbl.contents <- contents
let add tbl k v = tbl.contents <- (k, v) :: tbl.contents
let clear tbl = tbl.contents <- []
let create _ = { contents= [] }
end
type elt = { time: float; syscall: Miou.syscall; mutable cancelled: bool }
module Heapq = Miou.Pqueue.Make (struct
type t = elt
let dummy = { time= 0.0; syscall= Obj.magic (); cancelled= false }
let compare { time= a; _ } { time= b; _ } = Float.compare a b
end)
type domain = {
readers: Miou.syscall list File_descrs.t
; writers: Miou.syscall list File_descrs.t
; sleepers: Heapq.t
; revert: (Miou.uid, Unix.file_descr * int) Hashtbl.t
; poll: Poll.t
; bitv: Bitv.t
}
let clean domain uids =
let clean uid' ((fd : Unix.file_descr), index) tbl =
match File_descrs.find tbl fd with
| exception Not_found -> ()
| syscalls -> (
Poll.invalidate_index domain.poll index;
Bitv.set domain.bitv index false;
match List.filter (fun s -> uid' <> Miou.uid s) syscalls with
| [] -> File_descrs.remove tbl fd
| syscalls -> File_descrs.replace tbl fd syscalls)
in
let clean uid =
match Hashtbl.find domain.revert uid with
| fd ->
clean uid fd domain.readers;
clean uid fd domain.writers
| exception Not_found -> ()
in
List.iter clean uids;
List.iter (Hashtbl.remove domain.revert) uids;
let clean ({ syscall; _ } as elt) =
if List.exists (( = ) (Miou.uid syscall)) uids then elt.cancelled <- true
in
Heapq.iter clean domain.sleepers
let rec drop_heapq heapq =
try Heapq.delete_min_exn heapq; drop_heapq heapq with _ -> ()
let domain =
let rec split_from_parent v =
File_descrs.clear v.readers;
File_descrs.clear v.writers;
drop_heapq v.sleepers;
Hashtbl.clear v.revert;
for i = 1 to Bitv.length v.bitv - 1 do
Poll.invalidate_index v.poll i;
Bitv.set v.bitv i false
done;
make ()
and make () =
let poll = Poll.create () in
Logs.debug (fun m ->
m "create a poll on [%d] with %d file-descriptors"
(Stdlib.Domain.self () :> int)
(Poll.maxfds poll));
{
readers= File_descrs.create 0x100
; writers= File_descrs.create 0x100
; sleepers= Heapq.create ()
; revert= Hashtbl.create 0x100
; poll
; bitv= Bitv.create (Poll.maxfds poll) false
}
in
let key = Stdlib.Domain.DLS.new_key ~split_from_parent make in
fun () -> Stdlib.Domain.DLS.get key
let append tbl fd syscall =
try
let syscalls = File_descrs.find tbl fd in
File_descrs.replace tbl fd (syscall :: syscalls)
with Not_found -> File_descrs.add tbl fd [ syscall ]
let blocking_read ?(name = "read") fd =
let syscall = Miou.syscall ~name () in
let uid = Miou.uid syscall in
let domain = domain () in
let fn () =
match Bitv.next domain.bitv with
| Some next ->
Logs.debug (fun m ->
m "poll.set-index on [%d:%d] with POLLIN"
(Stdlib.Domain.self () :> int)
next);
Hashtbl.replace domain.revert uid (fd, next);
append domain.readers fd syscall;
Poll.set_index domain.poll next fd Poll.Flags.pollin;
Bitv.set domain.bitv next true
| None -> failwith "Too many open files"
in
Miou.suspend ~fn syscall
let blocking_write ?(name = "write") fd =
let syscall = Miou.syscall ~name () in
let uid = Miou.uid syscall in
let domain = domain () in
let fn () =
match Bitv.next domain.bitv with
| Some next ->
Logs.debug (fun m ->
m "poll.set-index on [%d:%d] with POLLIN"
(Stdlib.Domain.self () :> int)
next);
Hashtbl.replace domain.revert uid (fd, next);
append domain.writers fd syscall;
Poll.set_index domain.poll next fd Poll.Flags.pollout;
Bitv.set domain.bitv next true
| None -> failwith "Too many open files"
in
Miou.suspend ~fn syscall
let rec unsafe_read ({ fd; non_blocking; _ } as file_descr) off len buf =
if non_blocking then
match Unix.read fd buf off len with
| exception Unix.(Unix_error (EINTR, _, _)) ->
unsafe_read file_descr off len buf
| exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) ->
blocking_read fd;
unsafe_read file_descr off len buf
| value -> value
else
let rec go () =
match Unix.read fd buf off len with
| exception Unix.(Unix_error (EINTR, _, _)) -> go ()
| value -> value
in
blocking_read fd; go ()
let read file_descr ?(off = 0) ?len buf =
let len = match len with None -> Bytes.length buf - off | Some len -> len in
if off < 0 || len < 0 || off > Bytes.length buf - len then
invalid_arg "Miou_unix.read";
if is_dgram file_descr then invalid_arg "Miou_unix.read: invalid file-descr";
unsafe_read file_descr off len buf
let rec unsafe_recvfrom ({ fd; non_blocking; _ } as file_descr) off len buf
flags =
if non_blocking then
match Unix.recvfrom fd buf off len flags with
| exception Unix.(Unix_error (EINTR, _, _)) ->
unsafe_recvfrom file_descr off len buf flags
| exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) ->
blocking_read fd;
unsafe_recvfrom file_descr off len buf flags
| value -> value
else
let rec go () =
match Unix.recvfrom fd buf off len flags with
| exception Unix.(Unix_error (EINTR, _, _)) -> go ()
| value -> value
in
blocking_read fd; go ()
let recvfrom file_descr ?(off = 0) ?len buf flags =
let len = match len with None -> Bytes.length buf - off | Some len -> len in
if off < 0 || len < 0 || off > Bytes.length buf - len then
invalid_arg "Miou_unix.recvfrom";
if not (is_dgram file_descr) then
invalid_arg "Miou_unix.recvfrom: invalid file-descr";
unsafe_recvfrom file_descr off len buf flags
let rec really_read_go file_descr off len buf =
let len' = unsafe_read file_descr off len buf in
if len' == 0 then raise End_of_file
else if len - len' > 0 then
really_read_go file_descr (off + len') (len - len') buf
let really_read file_descr ?(off = 0) ?len buf =
let len = match len with None -> Bytes.length buf - off | Some len -> len in
if off < 0 || len < 0 || off > Bytes.length buf - len then
invalid_arg "Miou_unix.really_read";
if is_dgram file_descr then invalid_arg "Miou_unix.read: invalid file-descr";
if len > 0 then really_read_go file_descr off len buf
let rec unsafe_write ({ fd; non_blocking; _ } as file_descr) off len str =
if non_blocking then
match Unix.write fd (Bytes.unsafe_of_string str) off len with
| exception Unix.(Unix_error (EINTR, _, _)) ->
unsafe_write file_descr off len str
| exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) ->
blocking_write fd;
unsafe_write file_descr off len str
| len' when len' < len ->
unsafe_write file_descr (off + len') (len - len') str
| _ -> ()
else
let rec go () =
match Unix.write fd (Bytes.unsafe_of_string str) off len with
| exception Unix.(Unix_error (EINTR, _, _)) -> go ()
| len' when len' < len ->
unsafe_write file_descr (off + len') (len - len') str
| _ -> ()
in
blocking_write fd; go ()
let write file_descr ?(off = 0) ?len str =
let len =
match len with None -> String.length str - off | Some len -> len
in
if off < 0 || len < 0 || off > String.length str - len then
invalid_arg "Miou_unix.write";
if is_dgram file_descr then invalid_arg "Miou_unix.write: invalid file-descr";
unsafe_write file_descr off len str
let rec unsafe_sendto ({ fd; non_blocking; _ } as file_descr) off len str flags
sockaddr =
if non_blocking then
match Unix.sendto_substring fd str off len flags sockaddr with
| exception Unix.(Unix_error (EINTR, _, _)) ->
unsafe_sendto file_descr off len str flags sockaddr
| exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) ->
blocking_write fd;
unsafe_sendto file_descr off len str flags sockaddr
| value -> value
else
let rec go () =
match Unix.sendto_substring fd str off len flags sockaddr with
| exception Unix.(Unix_error (EINTR, _, _)) -> go ()
| value -> value
in
blocking_write fd; go ()
let sendto file_descr ?(off = 0) ?len str flags sockaddr =
let len =
match len with None -> String.length str - off | Some len -> len
in
if off < 0 || len < 0 || off > String.length str - len then
invalid_arg "Miou_unix.sendto";
if not (is_dgram file_descr) then
invalid_arg "Miou_unix.sendto: invalid file-descr";
unsafe_sendto file_descr off len str flags sockaddr
let rec accept ?cloexec ({ fd; non_blocking; _ } as file_descr) =
if is_dgram file_descr then invalid_arg "Miou_unix.accept: invalid file-descr";
if non_blocking then (
match Unix.accept ?cloexec fd with
| exception Unix.(Unix_error (EINTR, _, _)) -> accept ?cloexec file_descr
| exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) ->
blocking_read ~name:"accept" fd;
accept ?cloexec file_descr
| fd, sockaddr ->
Unix.set_nonblock fd;
let file_descr = { fd; non_blocking= true; kind= Some STREAM } in
(file_descr, sockaddr))
else
let rec go () =
match Unix.accept ?cloexec fd with
| exception Unix.(Unix_error (EINTR, _, _)) -> go ()
| fd, sockaddr ->
Unix.set_nonblock fd;
let file_descr = { fd; non_blocking= true; kind= Some STREAM } in
(file_descr, sockaddr)
in
blocking_read fd; go ()
let rec connect ({ fd; non_blocking; _ } as file_descr) sockaddr =
if not non_blocking then
invalid_arg
"Miou_unix.connect: we expect a file descriptor in the non-blocking mode";
if is_dgram file_descr then
invalid_arg "Miou_unix.connect: invalid file-descr";
match Unix.connect fd sockaddr with
| () -> ()
| exception Unix.(Unix_error (EINTR, _, _)) -> connect file_descr sockaddr
| exception Unix.(Unix_error (EINPROGRESS, _, _)) -> (
blocking_write ~name:"connect" fd;
match Unix.getsockopt_error fd with
| None -> ()
| Some err -> raise (Unix.Unix_error (err, "connect", "")))
let close { fd; _ } = Unix.close fd
let sleep until =
let syscall = Miou.syscall ~name:"sleep" () in
let domain = domain () in
let elt =
{ time= Unix.gettimeofday () +. until; syscall; cancelled= false }
in
let fn () = Heapq.insert elt domain.sleepers in
Miou.suspend ~fn syscall
module Ownership = struct
type old = file_descr
type file_descr = {
fd: Unix.file_descr
; non_blocking: bool
; resource: Miou.Ownership.t
; kind: kind option
}
let bind_and_listen ?backlog { fd; non_blocking; resource; kind } sockaddr =
Miou.Ownership.check resource;
bind_and_listen ?backlog { fd; non_blocking; kind } sockaddr
let read { fd; non_blocking; resource; kind } ?off ?len buf =
Miou.Ownership.check resource;
read { fd; non_blocking; kind } ?off ?len buf
let really_read { fd; non_blocking; resource; kind } ?off ?len buf =
Miou.Ownership.check resource;
really_read { fd; non_blocking; kind } ?off ?len buf
let write { fd; non_blocking; resource; kind } ?off ?len str =
Miou.Ownership.check resource;
write { fd; non_blocking; kind } ?off ?len str
let accept ?cloexec { fd; non_blocking; resource; kind } =
Miou.Ownership.check resource;
let ({ fd; non_blocking; kind } : old), sockaddr =
accept ?cloexec { fd; non_blocking; kind }
in
let resource = Miou.Ownership.create ~finally:Unix.close fd in
Miou.Ownership.own resource;
({ fd; non_blocking; resource; kind }, sockaddr)
let connect { fd; non_blocking; resource; kind } sockaddr =
Miou.Ownership.check resource;
connect { fd; non_blocking; kind } sockaddr
let close { fd; resource; _ } =
Miou.Ownership.disown resource;
Unix.close fd
let of_file_descr ?(non_blocking = true) fd =
let resource = Miou.Ownership.create ~finally:Unix.close fd in
if non_blocking then Unix.set_nonblock fd else Unix.clear_nonblock fd;
Miou.Ownership.own resource;
match Unix.getsockopt_int fd Unix.SO_TYPE with
| 1 -> { fd; non_blocking; resource; kind= Some STREAM }
| 2 -> { fd; non_blocking; resource; kind= Some DGRAM }
| _ ->
invalid_arg
"Miou_unix.Ownership.of_file_descr: invalid file-descriptor (neither \
TCP nor UDP)"
| exception _ -> { fd; non_blocking; resource; kind= None }
let to_file_descr { fd; _ } = fd
let resource { resource; _ } = resource
let tcpv4 () =
let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
let resource = Miou.Ownership.create ~finally:Unix.close fd in
Unix.set_nonblock fd;
Miou.Ownership.own resource;
{ fd; non_blocking= true; resource; kind= Some STREAM }
let tcpv6 () =
let fd = Unix.socket Unix.PF_INET6 Unix.SOCK_STREAM 0 in
let resource = Miou.Ownership.create ~finally:Unix.close fd in
Unix.set_nonblock fd;
Miou.Ownership.own resource;
{ fd; non_blocking= true; resource; kind= Some STREAM }
let udpv4 () =
let fd = Unix.socket Unix.PF_INET Unix.SOCK_DGRAM 0 in
let resource = Miou.Ownership.create ~finally:Unix.close fd in
Unix.set_nonblock fd;
Miou.Ownership.own resource;
{ fd; non_blocking= true; resource; kind= Some DGRAM }
let udpv6 () =
let fd = Unix.socket Unix.PF_INET6 Unix.SOCK_DGRAM 0 in
let resource = Miou.Ownership.create ~finally:Unix.close fd in
Unix.set_nonblock fd;
Miou.Ownership.own resource;
{ fd; non_blocking= true; resource; kind= Some DGRAM }
let recvfrom { fd; non_blocking; resource; kind } ?off ?len buf flags =
Miou.Ownership.check resource;
recvfrom { fd; non_blocking; kind } ?off ?len buf flags
let sendto { fd; non_blocking; resource; kind } ?off ?len str flags sockaddr =
Miou.Ownership.check resource;
sendto { fd; non_blocking; kind } ?off ?len str flags sockaddr
end
let rec sleeper domain =
match Heapq.find_min_exn domain.sleepers with
| exception Heapq.Empty -> None
| { cancelled= true; _ } ->
Heapq.delete_min_exn domain.sleepers;
sleeper domain
| { time; _ } -> Some time
let in_the_past t = t = 0. || t <= Unix.gettimeofday ()
let rec collect domain signals =
match Heapq.find_min_exn domain.sleepers with
| exception Heapq.Empty -> signals
| { cancelled= true; _ } ->
Heapq.delete_min_exn domain.sleepers;
collect domain signals
| { time; syscall; _ } when in_the_past time ->
Heapq.delete_min_exn domain.sleepers;
collect domain (Miou.signal syscall :: signals)
| _ -> signals
let intr fd =
let buf = Bytes.create 0x100 in
match Unix.read fd buf 0 (Bytes.length buf) with
| _ -> ()
| exception Unix.(Unix_error (EAGAIN, _, _)) -> ()
let transmit_and_clean domain syscall =
Hashtbl.remove domain.revert (Miou.uid syscall);
Miou.signal syscall
let wakeup domain tbl fd =
match File_descrs.find_opt tbl fd with
| None -> []
| Some [] -> File_descrs.remove tbl fd; []
| Some syscalls ->
File_descrs.remove tbl fd;
List.rev_map (transmit_and_clean domain) syscalls
let select _uid ic ~block cancelled_syscalls =
let domain = domain () in
clean domain cancelled_syscalls;
let timeout : Poll.ppoll_timeout =
match (sleeper domain, block) with
| None, true -> Poll.Infinite
| (None | Some _), false -> Poll.No_wait
| Some value, true ->
let value = value -. Unix.gettimeofday () in
let value = Float.max value 0.0 *. 1e9 in
Poll.Nanoseconds (Int64.of_float value)
in
let nready = Bitv.max domain.bitv in
match Poll.ppoll_or_poll domain.poll nready timeout with
| exception Unix.(Unix_error (EINTR, _, _)) -> collect domain []
| nready ->
let acc = ref (collect domain []) in
let fn index fd flags =
if index == 0 then intr ic
else begin
let err = Poll.Flags.(mem flags (pollerr + pollhup + pollnval)) in
let readable = err || Poll.Flags.mem flags Poll.Flags.pollin in
let writable = err || Poll.Flags.mem flags Poll.Flags.pollout in
if readable || writable then begin
Poll.invalidate_index domain.poll index;
Bitv.set domain.bitv index false;
let x = if readable then wakeup domain domain.readers fd else [] in
let y = if writable then wakeup domain domain.writers fd else [] in
acc := List.rev_append y (List.rev_append x !acc)
end
end
in
Poll.iter domain.poll nready fn;
!acc
let signal = Bytes.make 1 '\000'
let interrupt oc () =
match Unix.single_write oc signal 0 1 with
| n -> assert (n = 1)
| exception Unix.(Unix_error (EAGAIN, _, _)) -> ()
| exception Unix.(Unix_error (EBADF, _, _)) ->
()
let events uid =
let domain = domain () in
let ic, oc = Unix.pipe ~cloexec:true () in
Unix.set_nonblock ic;
Unix.set_nonblock oc;
Bitv.set domain.bitv 0 true;
Poll.set_index domain.poll 0 ic Poll.Flags.pollin;
let select = select uid ic in
let finaliser () = Unix.close ic; Unix.close oc in
{ Miou.interrupt= interrupt oc; select; finaliser }
let run ?g ?domains fn = Miou.run ~events ?g ?domains fn