2017-05-26 14:30:34 +00:00
|
|
|
(* (c) 2017 Hannes Mehnert, all rights reserved *)
|
|
|
|
|
|
|
|
(* the process responsible for buffering console IO *)
|
|
|
|
|
2018-09-19 19:16:44 +00:00
|
|
|
(* communication channel is a single unix domain socket. The following commands
|
|
|
|
can be issued:
|
|
|
|
- Add name (by vmmd) --> creates a new console slurper for name,
|
|
|
|
and starts a read_console task
|
|
|
|
- Attach name --> attaches console of name: send existing stuff to client,
|
|
|
|
and record the requesting socket to receive further messages. A potential
|
|
|
|
earlier subscriber to the same console is closed. *)
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
open Lwt.Infix
|
|
|
|
|
|
|
|
open Astring
|
|
|
|
|
|
|
|
let pp_unix_error ppf e = Fmt.string ppf (Unix.error_message e)
|
|
|
|
|
2018-05-02 17:52:18 +00:00
|
|
|
let active = ref String.Map.empty
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2019-10-27 22:23:54 +00:00
|
|
|
let read_console id name ring fd =
|
2017-05-26 14:30:34 +00:00
|
|
|
Lwt.catch (fun () ->
|
2019-10-27 22:23:54 +00:00
|
|
|
Lwt_unix.wait_read fd >>= fun () ->
|
|
|
|
let channel = Lwt_io.of_fd ~mode:Lwt_io.Input fd in
|
2017-05-26 14:30:34 +00:00
|
|
|
let rec loop () =
|
|
|
|
Lwt_io.read_line channel >>= fun line ->
|
|
|
|
Logs.debug (fun m -> m "read %s" line) ;
|
|
|
|
let t = Ptime_clock.now () in
|
|
|
|
Vmm_ring.write ring (t, line) ;
|
2018-05-02 17:52:18 +00:00
|
|
|
(match String.Map.find name !active with
|
|
|
|
| None -> Lwt.return_unit
|
2019-11-11 20:49:51 +00:00
|
|
|
| Some (version, fd) ->
|
|
|
|
let header = Vmm_commands.header ~version id in
|
2018-10-23 21:11:22 +00:00
|
|
|
Vmm_lwt.write_wire fd (header, `Data (`Console_data (t, line))) >>= function
|
2018-09-09 18:52:04 +00:00
|
|
|
| Error _ ->
|
2018-09-21 22:26:52 +00:00
|
|
|
Vmm_lwt.safe_close fd >|= fun () ->
|
2018-09-09 18:52:04 +00:00
|
|
|
active := String.Map.remove name !active
|
2018-05-02 17:52:18 +00:00
|
|
|
| Ok () -> Lwt.return_unit) >>=
|
|
|
|
loop
|
2017-05-26 14:30:34 +00:00
|
|
|
in
|
|
|
|
loop ())
|
|
|
|
(fun e ->
|
|
|
|
begin match e with
|
|
|
|
| Unix.Unix_error (e, f, _) ->
|
|
|
|
Logs.err (fun m -> m "%s error in %s: %a" name f pp_unix_error e)
|
|
|
|
| End_of_file ->
|
|
|
|
Logs.debug (fun m -> m "%s end of file while reading" name)
|
|
|
|
| exn ->
|
|
|
|
Logs.err (fun m -> m "%s error while reading %s" name (Printexc.to_string exn))
|
|
|
|
end ;
|
2019-10-27 22:23:54 +00:00
|
|
|
Vmm_lwt.safe_close fd)
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
let open_fifo name =
|
2018-11-11 01:33:00 +00:00
|
|
|
let fifo = Vmm_core.Name.fifo_file name in
|
2017-05-26 14:30:34 +00:00
|
|
|
Lwt.catch (fun () ->
|
|
|
|
Logs.debug (fun m -> m "opening %a for reading" Fpath.pp fifo) ;
|
2019-10-27 22:23:54 +00:00
|
|
|
Lwt_unix.openfile (Fpath.to_string fifo) [Lwt_unix.O_RDONLY; Lwt_unix.O_NONBLOCK] 0 >>= fun fd ->
|
|
|
|
Lwt.return (Some fd))
|
2017-05-26 14:30:34 +00:00
|
|
|
(function
|
|
|
|
| Unix.Unix_error (e, f, _) ->
|
|
|
|
Logs.err (fun m -> m "%a error in %s: %a" Fpath.pp fifo f pp_unix_error e) ;
|
|
|
|
Lwt.return None
|
|
|
|
| exn ->
|
|
|
|
Logs.err (fun m -> m "%a error while reading %s" Fpath.pp fifo (Printexc.to_string exn)) ;
|
|
|
|
Lwt.return None)
|
|
|
|
|
|
|
|
let t = ref String.Map.empty
|
|
|
|
|
2019-10-12 00:15:44 +00:00
|
|
|
let fifos = Vmm_core.conn_metrics "fifo"
|
2019-10-10 20:26:36 +00:00
|
|
|
|
2018-09-09 18:52:04 +00:00
|
|
|
let add_fifo id =
|
2018-11-11 00:21:12 +00:00
|
|
|
let name = Vmm_core.Name.to_string id in
|
2018-11-11 01:33:00 +00:00
|
|
|
open_fifo id >|= function
|
2019-01-27 11:55:23 +00:00
|
|
|
| None -> Error (`Msg "opening")
|
2017-05-26 14:30:34 +00:00
|
|
|
| Some f ->
|
2019-01-27 11:55:23 +00:00
|
|
|
let ring = match String.Map.find name !t with
|
|
|
|
| None ->
|
|
|
|
let ring = Vmm_ring.create "" () in
|
|
|
|
let map = String.Map.add name ring !t in
|
|
|
|
t := map ;
|
|
|
|
ring
|
|
|
|
| Some ring -> ring
|
|
|
|
in
|
2019-10-10 20:26:36 +00:00
|
|
|
fifos `Open;
|
|
|
|
Lwt.async (fun () -> read_console id name ring f >|= fun () -> fifos `Close) ;
|
2018-10-25 14:02:04 +00:00
|
|
|
Ok ()
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2019-11-11 20:49:51 +00:00
|
|
|
let subscribe s version id =
|
2018-11-11 00:21:12 +00:00
|
|
|
let name = Vmm_core.Name.to_string id in
|
|
|
|
Logs.debug (fun m -> m "attempting to subscribe %a" Vmm_core.Name.pp id) ;
|
2017-05-26 14:30:34 +00:00
|
|
|
match String.Map.find name !t with
|
2018-09-21 22:26:52 +00:00
|
|
|
| None ->
|
2019-11-11 20:49:51 +00:00
|
|
|
active := String.Map.add name (version, s) !active ;
|
2018-10-25 14:02:04 +00:00
|
|
|
Lwt.return (None, "waiting for VM")
|
2017-05-26 14:30:34 +00:00
|
|
|
| Some r ->
|
2018-09-19 19:16:44 +00:00
|
|
|
(match String.Map.find name !active with
|
|
|
|
| None -> Lwt.return_unit
|
2019-11-11 20:49:51 +00:00
|
|
|
| Some (_, s) -> Vmm_lwt.safe_close s) >|= fun () ->
|
|
|
|
active := String.Map.add name (version, s) !active ;
|
2018-10-25 14:02:04 +00:00
|
|
|
(Some r, "subscribed")
|
|
|
|
|
2019-11-11 20:49:51 +00:00
|
|
|
let send_history s version r id since =
|
2018-10-25 14:02:04 +00:00
|
|
|
let entries =
|
|
|
|
match since with
|
2019-10-29 18:42:55 +00:00
|
|
|
| `Count n -> Vmm_ring.read_last r n
|
|
|
|
| `Since ts -> Vmm_ring.read_history r ts
|
2018-10-25 14:02:04 +00:00
|
|
|
in
|
2018-11-11 00:21:12 +00:00
|
|
|
Logs.debug (fun m -> m "%a found %d history" Vmm_core.Name.pp id (List.length entries)) ;
|
2018-10-25 14:02:04 +00:00
|
|
|
Lwt_list.iter_s (fun (i, v) ->
|
2019-11-11 20:49:51 +00:00
|
|
|
let header = Vmm_commands.header ~version id in
|
2018-10-25 14:02:04 +00:00
|
|
|
Vmm_lwt.write_wire s (header, `Data (`Console_data (i, v))) >>= function
|
|
|
|
| Ok () -> Lwt.return_unit
|
|
|
|
| Error _ -> Vmm_lwt.safe_close s)
|
|
|
|
entries
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2019-10-10 20:26:36 +00:00
|
|
|
let handle s addr =
|
2018-09-09 18:52:04 +00:00
|
|
|
Logs.info (fun m -> m "handling connection %a" Vmm_lwt.pp_sockaddr addr) ;
|
2017-05-26 14:30:34 +00:00
|
|
|
let rec loop () =
|
2018-09-09 18:52:04 +00:00
|
|
|
Vmm_lwt.read_wire s >>= function
|
2018-03-18 17:30:43 +00:00
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "exception while reading") ;
|
|
|
|
Lwt.return_unit
|
2018-10-22 22:02:05 +00:00
|
|
|
| Ok (header, `Command (`Console_cmd cmd)) ->
|
2019-11-11 20:49:51 +00:00
|
|
|
begin
|
2018-11-11 00:21:12 +00:00
|
|
|
let name = header.Vmm_commands.name in
|
2018-10-25 14:02:04 +00:00
|
|
|
match cmd with
|
|
|
|
| `Console_add ->
|
|
|
|
begin
|
|
|
|
add_fifo name >>= fun res ->
|
|
|
|
let reply = match res with
|
|
|
|
| Ok () -> `Success `Empty
|
|
|
|
| Error (`Msg msg) -> `Failure msg
|
2019-11-11 20:49:51 +00:00
|
|
|
in
|
|
|
|
Vmm_lwt.write_wire s (header, reply) >>= function
|
|
|
|
| Ok () -> loop ()
|
|
|
|
| Error _ ->
|
|
|
|
Logs.err (fun m -> m "error while writing") ;
|
|
|
|
Lwt.return_unit
|
2018-10-25 14:02:04 +00:00
|
|
|
end
|
|
|
|
| `Console_subscribe ts ->
|
2019-11-11 20:49:51 +00:00
|
|
|
subscribe s header.Vmm_commands.version name >>= fun (ring, res) ->
|
2018-10-25 14:02:04 +00:00
|
|
|
Vmm_lwt.write_wire s (header, `Success (`String res)) >>= function
|
|
|
|
| Error _ -> Vmm_lwt.safe_close s
|
|
|
|
| Ok () ->
|
|
|
|
(match ring with
|
|
|
|
| None -> Lwt.return_unit
|
2019-11-11 20:49:51 +00:00
|
|
|
| Some r -> send_history s header.Vmm_commands.version r name ts) >>= fun () ->
|
2018-10-25 14:02:04 +00:00
|
|
|
(* now we wait for the next read and terminate*)
|
|
|
|
Vmm_lwt.read_wire s >|= fun _ -> ()
|
2018-10-22 22:02:05 +00:00
|
|
|
end
|
|
|
|
| Ok wire ->
|
2018-10-25 14:02:04 +00:00
|
|
|
Logs.err (fun m -> m "unexpected wire %a" Vmm_commands.pp_wire wire) ;
|
|
|
|
Lwt.return ()
|
2017-05-26 14:30:34 +00:00
|
|
|
in
|
2018-03-18 17:30:43 +00:00
|
|
|
loop () >>= fun () ->
|
2018-09-21 22:26:52 +00:00
|
|
|
Vmm_lwt.safe_close s >|= fun () ->
|
2018-04-01 21:13:11 +00:00
|
|
|
Logs.warn (fun m -> m "disconnected")
|
2017-05-26 14:30:34 +00:00
|
|
|
|
2019-10-12 00:15:44 +00:00
|
|
|
let m = Vmm_core.conn_metrics "unix"
|
2019-10-10 20:26:36 +00:00
|
|
|
|
2019-10-29 22:37:42 +00:00
|
|
|
let jump _ influx tmpdir =
|
2017-05-26 14:30:34 +00:00
|
|
|
Sys.(set_signal sigpipe Signal_ignore) ;
|
2019-10-29 22:37:42 +00:00
|
|
|
Albatross_cli.set_tmpdir tmpdir;
|
2017-05-26 14:30:34 +00:00
|
|
|
Lwt_main.run
|
2019-10-10 20:26:36 +00:00
|
|
|
(Albatross_cli.init_influx "albatross_console" influx;
|
|
|
|
Vmm_lwt.server_socket `Console >>= fun s ->
|
2017-05-26 14:30:34 +00:00
|
|
|
let rec loop () =
|
|
|
|
Lwt_unix.accept s >>= fun (cs, addr) ->
|
2019-10-10 20:26:36 +00:00
|
|
|
m `Open;
|
|
|
|
Lwt.async (fun () -> handle cs addr >|= fun () -> m `Close) ;
|
2017-05-26 14:30:34 +00:00
|
|
|
loop ()
|
|
|
|
in
|
|
|
|
loop ())
|
|
|
|
|
|
|
|
open Cmdliner
|
|
|
|
|
2019-03-27 23:11:43 +00:00
|
|
|
open Albatross_cli
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
let cmd =
|
2019-10-29 22:37:42 +00:00
|
|
|
Term.(term_result (const jump $ setup_log $ influx $ tmpdir)),
|
2019-11-11 20:49:51 +00:00
|
|
|
Term.info "albatross_console" ~version
|
2017-05-26 14:30:34 +00:00
|
|
|
|
|
|
|
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1
|