Skip to content

Commit 1463039

Browse files
committed
xcp-rrdd: Make parsing of cmd's output more robust
On the DCMI daemon, ignore any kind of exception, rather than only end_of_line. On exec_cmd, close the file descriptor even if exception are raised while reading lines. Signed-off-by: Pau Ruiz Safont <[email protected]>
1 parent e24a5cc commit 1463039

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

ocaml/xcp-rrdd/bin/rrdp-dcmi/rrdp_dcmi.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ let get_dcmi_power_reading () =
6363
let read_out_line line =
6464
(* example line: ' Instantaneous power reading: 34 Watts' *)
6565
try Scanf.sscanf line " Instantaneous power reading : %f Watts" Option.some
66-
with Scanf.Scan_failure _ | End_of_file -> None
66+
with _ -> None
6767
in
6868
let read_err_line _ = None in
6969
Utils.exec_cmd

ocaml/xcp-rrdd/lib/plugin/utils.ml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ let list_directory_entries_unsafe dir =
3333
let dirlist = list_directory_unsafe dir in
3434
List.filter (fun x -> x <> "." && x <> "..") dirlist
3535

36+
let protect ~finally fn = Xapi_stdext_pervasives.Pervasiveext.finally fn finally
37+
3638
let exec_cmd (module D : Debug.DEBUG) ~cmdstring
3739
~(read_out_line : string -> 'a option) ~(read_err_line : string -> 'b option)
3840
=
@@ -53,17 +55,13 @@ let exec_cmd (module D : Debug.DEBUG) ~cmdstring
5355
in
5456
Unix.close out_writeme ;
5557
Unix.close err_writeme ;
58+
5659
let read_and_close f fd =
5760
let in_channel = Unix.in_channel_of_descr fd in
58-
let vals = ref [] in
59-
let rec loop () =
60-
let line = input_line in_channel in
61-
let ret = f line in
62-
(match ret with None -> () | Some v -> vals := v :: !vals) ;
63-
loop ()
64-
in
65-
(try loop () with End_of_file -> ()) ;
66-
Unix.close fd ; List.rev !vals
61+
let f acc line = match f line with None -> acc | Some v -> v :: acc in
62+
protect
63+
~finally:(fun () -> Unix.close fd)
64+
(fun () -> Xapi_stdext_unix.Unixext.lines_fold f [] in_channel |> List.rev)
6765
in
6866
let stdout = read_and_close read_out_line out_readme in
6967
let stderr = read_and_close read_err_line err_readme in

0 commit comments

Comments
 (0)