close fd early, no need to carry file descriptors around

This commit is contained in:
Hannes Mehnert 2019-01-18 16:33:27 +01:00
parent 094922f6b0
commit 7b8f8fafbd
6 changed files with 11 additions and 11 deletions

View file

@ -34,7 +34,7 @@ let create process cont =
state := state'' ; state := state'' ;
s := { !s with vm_created = succ !s.vm_created } ; s := { !s with vm_created = succ !s.vm_created } ;
Lwt.async (fun () -> Lwt.async (fun () ->
Vmm_lwt.wait_and_clear vm.Unikernel.pid vm.Unikernel.stdout >>= fun r -> Vmm_lwt.wait_and_clear vm.Unikernel.pid >>= fun r ->
let state', out' = Vmm_vmmd.handle_shutdown !state name vm r in let state', out' = Vmm_vmmd.handle_shutdown !state name vm r in
s := { !s with vm_destroyed = succ !s.vm_destroyed } ; s := { !s with vm_destroyed = succ !s.vm_destroyed } ;
state := state' ; state := state' ;

View file

@ -180,7 +180,6 @@ module Unikernel = struct
cmd : Bos.Cmd.t ; cmd : Bos.Cmd.t ;
pid : int ; pid : int ;
taps : string list ; taps : string list ;
stdout : Unix.file_descr (* ringbuffer thingy *)
} }
let pp ppf vm = let pp ppf vm =

View file

@ -75,7 +75,6 @@ module Unikernel : sig
cmd : Bos.Cmd.t; cmd : Bos.Cmd.t;
pid : int; pid : int;
taps : string list; taps : string list;
stdout : Unix.file_descr;
} }
val pp : t Fmt.t val pp : t Fmt.t

View file

@ -29,10 +29,9 @@ let rec waitpid pid =
(Printexc.to_string e) pid) ; (Printexc.to_string e) pid) ;
Lwt.return (Error ())) Lwt.return (Error ()))
let wait_and_clear pid stdout = let wait_and_clear pid =
Logs.debug (fun m -> m "waitpid() for pid %d" pid) ; Logs.debug (fun m -> m "waitpid() for pid %d" pid) ;
waitpid pid >|= fun r -> waitpid pid >|= fun r ->
Vmm_unix.close_no_err stdout ;
match r with match r with
| Error () -> | Error () ->
Logs.err (fun m -> m "waitpid() for %d returned error" pid) ; Logs.err (fun m -> m "waitpid() for %d returned error" pid) ;

View file

@ -8,7 +8,7 @@ val ret : Unix.process_status -> Vmm_core.process_exit
val waitpid : int -> (int * Lwt_unix.process_status, unit) result Lwt.t val waitpid : int -> (int * Lwt_unix.process_status, unit) result Lwt.t
val wait_and_clear : int -> Unix.file_descr -> Vmm_core.process_exit Lwt.t val wait_and_clear : int -> Vmm_core.process_exit Lwt.t
val read_wire : Lwt_unix.file_descr -> val read_wire : Lwt_unix.file_descr ->
(Vmm_commands.wire, [> `Eof | `Exception | `Toomuch ]) result Lwt.t (Vmm_commands.wire, [> `Eof | `Exception | `Toomuch ]) result Lwt.t

View file

@ -37,10 +37,10 @@ let null = match read_fd_for_file (Fpath.v "/dev/null") with
| Ok fd -> fd | Ok fd -> fd
| Error _ -> invalid_arg "cannot read /dev/null" | Error _ -> invalid_arg "cannot read /dev/null"
let rec create_process prog args stdout stderr = let rec create_process prog args stdout =
try Unix.create_process prog args null stdout stderr with try Unix.create_process prog args null stdout stdout with
| Unix.Unix_error (Unix.EINTR, _, _) -> | Unix.Unix_error (Unix.EINTR, _, _) ->
create_process prog args stdout stderr create_process prog args stdout
let rec close fd = let rec close fd =
try Unix.close fd with try Unix.close fd with
@ -175,11 +175,14 @@ let exec name vm taps block =
Logs.debug (fun m -> m "opened file descriptor!"); Logs.debug (fun m -> m "opened file descriptor!");
try try
Logs.debug (fun m -> m "creating process"); Logs.debug (fun m -> m "creating process");
let pid = create_process prog line stdout stdout in let pid = create_process prog line stdout in
Logs.debug (fun m -> m "created process %d: %a" pid Bos.Cmd.pp cmd) ; Logs.debug (fun m -> m "created process %d: %a" pid Bos.Cmd.pp cmd) ;
(* we gave a copy (well, two copies) of that file descriptor to the solo5
process and don't really need it here anymore... *)
close stdout ;
(* this should get rid of the vmimage from vmmd's memory! *) (* this should get rid of the vmimage from vmmd's memory! *)
let config = Unikernel.{ vm with image = (fst vm.Unikernel.image, Cstruct.create 0) } in let config = Unikernel.{ vm with image = (fst vm.Unikernel.image, Cstruct.create 0) } in
Ok Unikernel.{ config ; cmd ; pid ; taps ; stdout } Ok Unikernel.{ config ; cmd ; pid ; taps }
with with
Unix.Unix_error (e, _, _) -> Unix.Unix_error (e, _, _) ->
close_no_err stdout; close_no_err stdout;