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
[@@@alert "-unstable"]
open Eio.Std
module Fiber_context = Eio.Private.Fiber_context
module Trace = Eio.Private.Trace
module Fd = Eio_unix.Fd
module Suspended = Eio_utils.Suspended
module Zzz = Eio_utils.Zzz
module Lf_queue = Eio_utils.Lf_queue
module Low_level = Low_level
type ('t, _, _) Eio.Resource.pi += Dir_fd : ('t, 't -> Low_level.dir_fd, [> `Dir_fd]) Eio.Resource.pi
let get_dir_fd_opt (Eio.Resource.T (t, ops)) =
match Eio.Resource.get_opt ops Dir_fd with
| Some f -> Some (f t)
| None -> None
module Datagram_socket = struct
type tag = [`Generic | `Unix]
type t = Eio_unix.Fd.t
let fd t = t
let close = Eio_unix.Fd.close
let send t ?dst buf =
let dst = Option.map Eio_unix.Net.sockaddr_to_unix dst in
let sent = Low_level.send_msg t ?dst buf in
assert (sent = Cstruct.lenv buf)
let recv t buf =
let addr, recv = Low_level.recv_msg t [buf] in
Eio_unix.Net.sockaddr_of_unix_datagram (Uring.Sockaddr.get addr), recv
let shutdown t cmd =
Low_level.shutdown t @@ match cmd with
| `Receive -> Unix.SHUTDOWN_RECEIVE
| `Send -> Unix.SHUTDOWN_SEND
| `All -> Unix.SHUTDOWN_ALL
end
let datagram_handler = Eio_unix.Pi.datagram_handler (module Datagram_socket)
let datagram_socket fd =
Eio.Resource.T (fd, datagram_handler)
module Listening_socket = struct
type t = Fd.t
type tag = [`Generic | `Unix]
let fd t = t
let close = Fd.close
let accept t ~sw =
Switch.check sw;
let client, client_addr = Low_level.accept ~sw t in
let client_addr = match client_addr with
| Unix.ADDR_UNIX path -> `Unix path
| Unix.ADDR_INET (host, port) -> `Tcp (Eio_unix.Net.Ipaddr.of_unix host, port)
in
let flow = (Flow.of_fd client :> _ Eio.Net.stream_socket) in
flow, client_addr
let listening_addr fd =
Eio_unix.Fd.use_exn "listening_addr" fd
(fun fd -> Eio_unix.Net.sockaddr_of_unix_stream (Unix.getsockname fd))
end
let listening_handler = Eio_unix.Pi.listening_socket_handler (module Listening_socket)
let listening_socket fd =
Eio.Resource.T (fd, listening_handler)
let socket_domain_of = function
| `Unix _ -> Unix.PF_UNIX
| `UdpV4 -> Unix.PF_INET
| `UdpV6 -> Unix.PF_INET6
| `Udp (host, _)
| `Tcp (host, _) ->
Eio.Net.Ipaddr.fold host
~v4:(fun _ -> Unix.PF_INET)
~v6:(fun _ -> Unix.PF_INET6)
let connect ~sw connect_addr =
let addr = Eio_unix.Net.sockaddr_to_unix connect_addr in
let sock_unix = Unix.socket ~cloexec:true (socket_domain_of connect_addr) Unix.SOCK_STREAM 0 in
let sock = Fd.of_unix ~sw ~seekable:false ~close_unix:true sock_unix in
Low_level.connect sock addr;
(Flow.of_fd sock :> _ Eio_unix.Net.stream_socket)
module Impl = struct
type t = unit
type tag = [`Unix | `Generic]
let listen () ~reuse_addr ~reuse_port ~backlog ~sw listen_addr =
if reuse_addr then (
match listen_addr with
| `Tcp _ -> ()
| `Unix path ->
match Unix.lstat path with
| Unix.{ st_kind = S_SOCK; _ } -> Unix.unlink path
| _ -> ()
| exception Unix.Unix_error (Unix.ENOENT, _, _) -> ()
| exception Unix.Unix_error (code, name, arg) -> raise @@ Err.wrap code name arg
);
let addr = Eio_unix.Net.sockaddr_to_unix listen_addr in
let sock_unix = Unix.socket ~cloexec:true (socket_domain_of listen_addr) Unix.SOCK_STREAM 0 in
let sock = Fd.of_unix ~sw ~seekable:false ~close_unix:true sock_unix in
begin match listen_addr with
| `Unix path ->
if String.length path > 0 && path.[0] <> Char.chr 0 then
Switch.on_release sw (fun () -> Unix.unlink path)
| `Tcp _ -> ()
end;
if reuse_addr then
Unix.setsockopt sock_unix Unix.SO_REUSEADDR true;
if reuse_port then
Unix.setsockopt sock_unix Unix.SO_REUSEPORT true;
Unix.bind sock_unix addr;
Unix.listen sock_unix backlog;
(listening_socket sock :> _ Eio.Net.listening_socket_ty r)
let connect () ~sw addr = (connect ~sw addr :> [`Generic | `Unix] Eio.Net.stream_socket_ty r)
let datagram_socket () ~reuse_addr ~reuse_port ~sw saddr =
if reuse_addr then (
match saddr with
| `Udp _ | `UdpV4 | `UdpV6 -> ()
| `Unix path ->
match Unix.lstat path with
| Unix.{ st_kind = S_SOCK; _ } -> Unix.unlink path
| _ -> ()
| exception Unix.Unix_error (Unix.ENOENT, _, _) -> ()
| exception Unix.Unix_error (code, name, arg) -> raise @@ Err.wrap code name arg
);
let sock_unix = Unix.socket ~cloexec:true (socket_domain_of saddr) Unix.SOCK_DGRAM 0 in
let sock = Fd.of_unix ~sw ~seekable:false ~close_unix:true sock_unix in
begin match saddr with
| `Udp _ | `Unix _ as saddr ->
let addr = Eio_unix.Net.sockaddr_to_unix saddr in
if reuse_addr then
Unix.setsockopt sock_unix Unix.SO_REUSEADDR true;
if reuse_port then
Unix.setsockopt sock_unix Unix.SO_REUSEPORT true;
Unix.bind sock_unix addr
| `UdpV4 | `UdpV6 -> ()
end;
(datagram_socket sock :> [`Generic | `Unix] Eio.Net.datagram_socket_ty r)
let getaddrinfo () = Low_level.getaddrinfo
let getnameinfo () = Eio_unix.Net.getnameinfo
end
let net =
let handler = Eio.Net.Pi.network (module Impl) in
Eio.Resource.T ((), handler)
type stdenv = Eio_unix.Stdenv.base
module Process_impl = struct
type t = Low_level.Process.t
type tag = [ `Generic | `Unix ]
let pid = Low_level.Process.pid
let await t =
match Eio.Promise.await @@ Low_level.Process.exit_status t with
| Unix.WEXITED i -> `Exited i
| Unix.WSIGNALED i -> `Signaled i
| Unix.WSTOPPED _ -> assert false
let signal = Low_level.Process.signal
end
let process =
let handler = Eio.Process.Pi.process (module Process_impl) in
fun proc -> Eio.Resource.T (proc, handler)
let with_dir dir_fd path fn =
Switch.run ~name:"with_dir" @@ fun sw ->
Low_level.openat ~sw
~seekable:false
~access:`R
~perm:0
~flags:Uring.Open_flags.(cloexec + path + directory)
dir_fd (if path = "" then "." else path)
|> fn
module Process_mgr = struct
module T = struct
type t = unit
let spawn_unix () ~sw ?cwd ~env ~fds ~executable args =
let actions = Low_level.Process.Fork_action.[
Eio_unix.Private.Fork_action.inherit_fds fds;
execve executable ~argv:(Array.of_list args) ~env
] in
let with_actions cwd fn = match cwd with
| None -> fn actions
| Some (fd, s) ->
match get_dir_fd_opt fd with
| None -> Fmt.invalid_arg "cwd is not an OS directory!"
| Some dir_fd ->
with_dir dir_fd s @@ fun cwd ->
fn (Low_level.Process.Fork_action.fchdir cwd :: actions)
in
with_actions cwd @@ fun actions ->
process (Low_level.Process.spawn ~sw actions)
end
include Eio_unix.Process.Make_mgr (T)
end
let process_mgr : Eio_unix.Process.mgr_ty r =
let h = Eio_unix.Process.Pi.mgr_unix (module Process_mgr) in
Eio.Resource.T ((), h)
let wrap_backtrace fn x =
match fn x with
| x -> Ok x
| exception ex ->
let bt = Printexc.get_raw_backtrace () in
Error (ex, bt)
let unwrap_backtrace = function
| Ok x -> x
| Error (ex, bt) -> Printexc.raise_with_backtrace ex bt
module Domain_mgr = struct
type t = {
run_event_loop : (unit -> unit) -> unit -> unit;
}
let domain_spawn sched k fn =
Domain.spawn @@ fun () ->
Trace.domain_spawn ~parent:(Suspended.tid k);
Fun.protect fn ~finally:(fun () -> Sched.enqueue_thread sched k ())
let make ~run_event_loop = { run_event_loop }
let run_raw _t fn =
let domain = ref None in
Sched.enter "run-domain" (fun sched k ->
let fn = wrap_backtrace fn in
domain := Some (domain_spawn sched k fn)
);
unwrap_backtrace (Domain.join (Option.get !domain))
let run t fn =
let domain = ref None in
Sched.enter "run-domain" (fun sched k ->
let cancelled, set_cancelled = Promise.create () in
Fiber_context.set_cancel_fn k.fiber (Promise.resolve set_cancelled);
domain := Some (domain_spawn sched k (fun () ->
let result = ref None in
let fn = wrap_backtrace (fun () -> fn ~cancelled) in
t.run_event_loop (fun () -> result := Some (fn ())) ();
Option.get !result
))
);
Trace.with_span "Domain.join" @@ fun () ->
unwrap_backtrace (Domain.join (Option.get !domain))
end
let domain_mgr ~run_event_loop =
let handler = Eio.Domain_manager.Pi.mgr (module Domain_mgr) in
Eio.Resource.T (Domain_mgr.make ~run_event_loop, handler)
module Mono_clock = struct
type t = unit
type time = Mtime.t
let now () = Mtime_clock.now ()
let sleep_until () time = Low_level.sleep_until time
end
let mono_clock : Mtime.t Eio.Time.clock_ty r =
let handler = Eio.Time.Pi.clock (module Mono_clock) in
Eio.Resource.T ((), handler)
module Clock = struct
type t = unit
type time = float
let now () = Unix.gettimeofday ()
let sleep_until () time =
let d = time -. Unix.gettimeofday () in
Eio.Time.Mono.sleep mono_clock d
end
let clock : float Eio.Time.clock_ty r =
let handler = Eio.Time.Pi.clock (module Clock) in
Eio.Resource.T ((), handler)
module rec Dir : sig
include Eio.Fs.Pi.DIR
val v : label:string -> path:string -> Low_level.dir_fd -> t
val close : t -> unit
val fd : t -> Low_level.dir_fd
end = struct
type t = {
fd : Low_level.dir_fd;
label : string;
path : string;
}
let v ~label ~path fd = { fd; label; path }
let open_in t ~sw path =
let fd = Low_level.openat ~sw t.fd path
~access:`R
~flags:Uring.Open_flags.cloexec
~perm:0
in
(Flow.of_fd fd :> Eio.File.ro_ty r)
let open_out t ~sw ~append ~create path =
let perm, flags =
match create with
| `Never -> 0, Uring.Open_flags.empty
| `If_missing perm -> perm, Uring.Open_flags.creat
| `Or_truncate perm -> perm, Uring.Open_flags.(creat + trunc)
| `Exclusive perm -> perm, Uring.Open_flags.(creat + excl)
in
let flags = if append then Uring.Open_flags.(flags + append) else flags in
let fd = Low_level.openat ~sw t.fd path
~access:`RW
~flags:Uring.Open_flags.(cloexec + flags)
~perm
in
(Flow.of_fd fd :> Eio.File.rw_ty r)
let native_internal t path =
if Filename.is_relative path then (
let p = Filename.concat t.path path in
if p = "" then "."
else if p = "." then p
else if Filename.is_implicit p then "./" ^ p
else p
) else path
let open_dir t ~sw path =
let fd = Low_level.openat ~sw ~seekable:false t.fd (if path = "" then "." else path)
~access:`R
~flags:Uring.Open_flags.(cloexec + path + directory)
~perm:0
in
let label = Filename.basename path in
let d = v ~label ~path:(native_internal t path) (Low_level.FD fd) in
Eio.Resource.T (d, Dir_handler.v)
let mkdir t ~perm path = Low_level.mkdir ~perm t.fd path
let read_dir t path =
Switch.run ~name:"read_dir" @@ fun sw ->
let path = if path = "" then "." else path in
let fd =
Low_level.openat ~sw t.fd path
~seekable:false
~access:`R
~flags:Uring.Open_flags.(cloexec + directory)
~perm:0
in
Low_level.read_dir fd
let read_link t path = Low_level.read_link t.fd path
let close t =
match t.fd with
| FD x -> Fd.close x
| Cwd | Fs -> failwith "Can't close non-FD directory!"
let unlink t path = Low_level.unlink ~rmdir:false t.fd path
let rmdir t path = Low_level.unlink ~rmdir:true t.fd path
let float_of_time s ns =
let s = Int64.to_float s in
let f = s +. (float ns /. 1e9) in
if floor f = s then f
else Float.pred f
let stat t ~follow path =
if !Sched.statx_works then (
let module X = Uring.Statx in
let x = X.create () in
Low_level.statx ~follow ~mask:X.Mask.basic_stats t.fd path x;
{ Eio.File.Stat.
dev = X.dev x;
ino = X.ino x;
kind = X.kind x;
perm = X.perm x;
nlink = X.nlink x;
uid = X.uid x;
gid = X.gid x;
rdev = X.rdev x;
size = X.size x |> Optint.Int63.of_int64;
atime = float_of_time (X.atime_sec x) (X.atime_nsec x);
mtime = float_of_time (X.mtime_sec x) (X.mtime_nsec x);
ctime = float_of_time (X.ctime_sec x) (X.ctime_nsec x);
}
) else (
Switch.run ~name:"stat" @@ fun sw ->
let fd = Low_level.openat ~sw ~seekable:false t.fd (if path = "" then "." else path)
~access:`R
~flags:Uring.Open_flags.(cloexec + path + (if follow then empty else nofollow))
~perm:0
in
Low_level.fstat fd
)
let rename t old_path t2 new_path =
match get_dir_fd_opt t2 with
| Some fd2 -> Low_level.rename t.fd old_path fd2 new_path
| None -> raise (Unix.Unix_error (Unix.EXDEV, "rename-dst", new_path))
let symlink ~link_to t path =
Low_level.symlink ~link_to t.fd path
let pp f t = Fmt.string f (String.escaped t.label)
let fd t = t.fd
let native t path =
Some (native_internal t path)
end
and Dir_handler : sig
val v : (Dir.t, [`Dir | `Close]) Eio.Resource.handler
end = struct
let v = Eio.Resource.handler [
H (Eio.Fs.Pi.Dir, (module Dir));
H (Eio.Resource.Close, Dir.close);
H (Dir_fd, Dir.fd);
]
end
let dir ~label ~path fd = Eio.Resource.T (Dir.v ~label ~path fd, Dir_handler.v)
let stdenv ~run_event_loop =
let fs = (dir ~label:"fs" ~path:"" Fs, "") in
let cwd = (dir ~label:"cwd" ~path:"" Cwd, "") in
object (_ : stdenv)
method stdin = Flow.stdin
method stdout = Flow.stdout
method stderr = Flow.stderr
method net = net
method process_mgr = process_mgr
method domain_mgr = domain_mgr ~run_event_loop
method clock = clock
method mono_clock = mono_clock
method fs = (fs :> Eio.Fs.dir_ty Eio.Path.t)
method cwd = (cwd :> Eio.Fs.dir_ty Eio.Path.t)
method secure_random = Flow.secure_random
method debug = Eio.Private.Debug.v
method backend_id = "linux"
end
let run_event_loop (type a) ?fallback config (main : _ -> a) arg : a =
Sched.with_sched ?fallback config @@ fun st ->
let open Effect.Deep in
let : _ effect_handler = {
effc = fun (type a) (e : a Effect.t) : ((a, Sched.exit) continuation -> Sched.exit) option ->
match e with
| Eio_unix.Private.Get_monotonic_clock -> Some (fun k -> continue k mono_clock)
| Eio_unix.Net.Import_socket_stream (sw, close_unix, fd) -> Some (fun k ->
let fd = Fd.of_unix ~sw ~seekable:false ~close_unix fd in
continue k (Flow.of_fd fd :> _ Eio_unix.Net.stream_socket)
)
| Eio_unix.Net.Import_socket_listening (sw, close_unix, fd) -> Some (fun k ->
let fd = Fd.of_unix ~sw ~seekable:false ~close_unix fd in
continue k (listening_socket fd)
)
| Eio_unix.Net.Import_socket_datagram (sw, close_unix, fd) -> Some (fun k ->
let fd = Fd.of_unix ~sw ~seekable:false ~close_unix fd in
continue k (datagram_socket fd)
)
| Eio_unix.Net.Socketpair_stream (sw, domain, protocol) -> Some (fun k ->
match
let a, b = Unix.socketpair ~cloexec:true domain Unix.SOCK_STREAM protocol in
let a = Fd.of_unix ~sw ~seekable:false ~close_unix:true a |> Flow.of_fd in
let b = Fd.of_unix ~sw ~seekable:false ~close_unix:true b |> Flow.of_fd in
((a :> _ Eio_unix.Net.stream_socket), (b :> _ Eio_unix.Net.stream_socket))
with
| r -> continue k r
| exception Unix.Unix_error (code, name, arg) ->
discontinue k (Err.wrap code name arg)
)
| Eio_unix.Net.Socketpair_datagram (sw, domain, protocol) -> Some (fun k ->
match
let a, b = Unix.socketpair ~cloexec:true domain Unix.SOCK_DGRAM protocol in
let a = Fd.of_unix ~sw ~seekable:false ~close_unix:true a |> datagram_socket in
let b = Fd.of_unix ~sw ~seekable:false ~close_unix:true b |> datagram_socket in
((a :> _ Eio_unix.Net.datagram_socket), (b :> _ Eio_unix.Net.datagram_socket))
with
| r -> continue k r
| exception Unix.Unix_error (code, name, arg) ->
discontinue k (Err.wrap code name arg)
)
| Eio_unix.Private.Pipe sw -> Some (fun k ->
match
let r, w = Low_level.pipe ~sw in
let r = (Flow.of_fd r :> _ Eio_unix.source) in
let w = (Flow.of_fd w :> _ Eio_unix.sink) in
(r, w)
with
| r -> continue k r
| exception Unix.Unix_error (code, name, arg) ->
discontinue k (Err.wrap code name arg)
)
| _ -> None
} in
Sched.run ~extra_effects st main arg
let run ?queue_depth ?n_blocks ?block_size ?polling_timeout ?fallback main =
let config = Sched.config ?queue_depth ?n_blocks ?block_size ?polling_timeout () in
let stdenv = stdenv ~run_event_loop:(run_event_loop ?fallback:None config) in
Sys.(set_signal sigpipe Signal_ignore);
run_event_loop ?fallback config main stdenv