Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove possible file descriptor leak if safe_close_and_exec fails #5596

Merged
merged 6 commits into from
May 22, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 16 additions & 24 deletions ocaml/forkexecd/test/fe_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ let min_fds = 7

let max_fds = 1024 - 13 (* fe daemon has a bunch for its own use *)

let fail x =
freddy77 marked this conversation as resolved.
Show resolved Hide resolved
Xapi_stdext_unix.Unixext.write_string_to_file "/tmp/fe-test.log" x ;
Printf.fprintf stderr "%s\n" x ;
assert false

let fail fmt = Format.ksprintf fail fmt

let all_combinations fds =
let y =
{
Expand Down Expand Up @@ -117,7 +124,7 @@ let test_delay () =
let timeout = 1.7 in
try
Forkhelpers.execute_command_get_output ~timeout exe args |> ignore ;
failwith "Failed to timeout"
fail "Failed to timeout"
with
| Forkhelpers.Subprocess_timeout ->
let elapsed = Unix.gettimeofday () -. start in
Expand All @@ -127,39 +134,26 @@ let test_delay () =
if elapsed > timeout +. 0.2 then
failwith "Excessive time elapsed"
| e ->
failwith
(Printf.sprintf "Failed with unexpected exception: %s"
(Printexc.to_string e)
)
fail "Failed with unexpected exception: %s" (Printexc.to_string e)

let test_notimeout () =
let exe = Printf.sprintf "/proc/%d/exe" (Unix.getpid ()) in
let args = ["sleep"] in
try
Forkhelpers.execute_command_get_output exe args |> ignore ;
()
with e ->
failwith
(Printf.sprintf "Failed with unexpected exception: %s"
(Printexc.to_string e)
)

let fail x =
Xapi_stdext_unix.Unixext.write_string_to_file "/tmp/fe-test.log" x ;
Printf.fprintf stderr "%s\n" x ;
assert false
with e -> fail "Failed with unexpected exception: %s" (Printexc.to_string e)

let expect expected s =
if s <> expected ^ "\n" then
fail (Printf.sprintf "output %s expected %s" s expected)
fail "output %s expected %s" s expected

let test_exitcode () =
let run_expect cmd expected =
try Forkhelpers.execute_command_get_output cmd [] |> ignore
with Forkhelpers.Spawn_internal_error (_, _, Unix.WEXITED n) ->
if n <> expected then
fail
(Printf.sprintf "%s exited with code %d, expected %d" cmd n expected)
fail "%s exited with code %d, expected %d" cmd n expected
in
run_expect "/bin/false" 1 ;
run_expect "/bin/xe-fe-test-no-command" 127 ;
Expand Down Expand Up @@ -215,7 +209,7 @@ let master fds =

let slave = function
| [] ->
failwith "Error, at least one fd expected"
fail "Error, at least one fd expected"
| total_fds :: rest ->
let total_fds = int_of_string total_fds in
let fds =
Expand Down Expand Up @@ -257,18 +251,16 @@ let slave = function
List.iter
(fun fd ->
if not (List.mem fd (List.map fst filtered)) then
fail (Printf.sprintf "fd %s not in /proc/%d/fd [ %s ]" fd pid ls)
fail "fd %s not in /proc/%d/fd [ %s ]" fd pid ls
)
fds ;
(* Check that we have the expected number *)
(*
Printf.fprintf stderr "%s %d\n" total_fds (List.length present - 1)
*)
if total_fds <> List.length filtered then
fail
(Printf.sprintf "Expected %d fds; /proc/%d/fd has %d: %s" total_fds
pid (List.length filtered) ls
)
fail "Expected %d fds; /proc/%d/fd has %d: %s" total_fds pid
(List.length filtered) ls

let sleep () = Unix.sleep 3 ; Printf.printf "Ok\n"

Expand Down