Skip to content

Commit a9d1b90

Browse files
committed
Add customisable handler for script errors
Signed-off-by: Rob Hoes <[email protected]>
1 parent 2848002 commit a9d1b90

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

lib/network_utils.ml

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,22 @@ let igmp_query_maxresp_time = ref "5000"
6060
let enable_ipv6_mcast_snooping = ref false
6161
let mcast_snooping_disable_flood_unregistered = ref true
6262

63-
let check_n_run run_func script args =
63+
let default_error_handler script args stdout stderr status =
64+
let message =
65+
match status with
66+
| Unix.WEXITED n -> Printf.sprintf "Exit code %d" n
67+
| Unix.WSIGNALED s -> Printf.sprintf "Signaled %d" s (* Note that this is the internal ocaml signal number, see Sys module *)
68+
| Unix.WSTOPPED s -> Printf.sprintf "Stopped %d" s
69+
in
70+
error "Call '%s %s' exited badly: %s [stdout = '%s'; stderr = '%s']" script
71+
(String.concat " " args) message stdout stderr;
72+
raise (Network_error (Script_error ["script", script;
73+
"args", String.concat " " args;
74+
"code", message;
75+
"stdout", stdout;
76+
"stderr", stderr]))
77+
78+
let check_n_run ?(on_error=default_error_handler) run_func script args =
6479
try
6580
Unix.access script [ Unix.X_OK ];
6681
(* Use the same $PATH as xapi *)
@@ -72,34 +87,22 @@ let check_n_run run_func script args =
7287
error "Caught unix error: %s [%s, %s]" (Unix.error_message e) a b;
7388
error "Assuming script %s doesn't exist" script;
7489
raise (Network_error (Script_missing script))
75-
| Forkhelpers.Spawn_internal_error(stderr, stdout, e)->
76-
let message =
77-
match e with
78-
| Unix.WEXITED n -> Printf.sprintf "Exit code %d" n
79-
| Unix.WSIGNALED s -> Printf.sprintf "Signaled %d" s (* Note that this is the internal ocaml signal number, see Sys module *)
80-
| Unix.WSTOPPED s -> Printf.sprintf "Stopped %d" s
81-
in
82-
error "Call '%s %s' exited badly: %s [stdout = '%s'; stderr = '%s']" script
83-
(String.concat " " args) message stdout stderr;
84-
raise (Network_error (Script_error ["script", script;
85-
"args", String.concat " " args;
86-
"code", message;
87-
"stdout", stdout;
88-
"stderr", stderr]))
89-
90-
let call_script ?(timeout=Some 60.0) script args =
90+
| Forkhelpers.Spawn_internal_error(stderr, stdout, status)->
91+
on_error script args stdout stderr status
92+
93+
let call_script ?(timeout=Some 60.0) ?on_error script args =
9194
let call_script_internal env script args =
9295
let (out,err) = Forkhelpers.execute_command_get_output ~env ?timeout script args in
9396
out
9497
in
95-
check_n_run call_script_internal script args
98+
check_n_run ?on_error call_script_internal script args
9699

97-
let fork_script script args =
100+
let fork_script ?on_error script args =
98101
let fork_script_internal env script args =
99102
let pid = Forkhelpers.safe_close_and_exec ~env None None None [] script args in
100103
Forkhelpers.dontwaitpid pid;
101104
in
102-
check_n_run fork_script_internal script args
105+
check_n_run ?on_error fork_script_internal script args
103106

104107
module Sysfs = struct
105108
let list () =

0 commit comments

Comments
 (0)