From ac2255bdc44157af456a4f944e2c653cf715160c Mon Sep 17 00:00:00 2001 From: Christian Lindig Date: Thu, 19 Dec 2024 14:55:06 +0000 Subject: [PATCH] XSI-1773 improve logging if service file unexpectedly exists We have seen failures where a service file unexpectedly exists. It could have been left behind but a failed stop but we don't have evidence for that. To help with this, provide more details of the file found. Signed-off-by: Christian Lindig --- ocaml/forkexecd/lib/dune | 1 + ocaml/forkexecd/lib/fe_systemctl.ml | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ocaml/forkexecd/lib/dune b/ocaml/forkexecd/lib/dune index 749f173b977..e8dd8c8312e 100644 --- a/ocaml/forkexecd/lib/dune +++ b/ocaml/forkexecd/lib/dune @@ -13,6 +13,7 @@ xapi-log xapi-stdext-pervasives xapi-stdext-unix + xapi-stdext-date xapi-tracing ) (preprocess (per_module ((pps ppx_deriving_rpc) Fe)))) diff --git a/ocaml/forkexecd/lib/fe_systemctl.ml b/ocaml/forkexecd/lib/fe_systemctl.ml index b36ee6674ae..046396002ca 100644 --- a/ocaml/forkexecd/lib/fe_systemctl.ml +++ b/ocaml/forkexecd/lib/fe_systemctl.ml @@ -130,14 +130,28 @@ let is_active ~service = in Unix.WEXITED 0 = status -let exists ~service = - Sys.file_exists (Filename.concat run_path (service ^ ".service")) +(** path to service file *) +let path service = Filename.concat run_path (service ^ ".service") + +(** does [service] file exist *) +let exists ~service = Sys.file_exists (path service) + +(** creation time of [path] as a string *) +let ctime path = + let ctime = Unix.((stat path).st_ctime) in + Xapi_stdext_date.Date.(of_unix_time ctime |> to_rfc3339) let start_transient ?env ?properties ?(exec_ty = Type.Simple) ~service cmd args = - if exists ~service then - (* this can only happen if there is a bug in the caller *) - invalid_arg (Printf.sprintf "Tried to start %s twice" service) ; + ( match exists ~service with + | true -> + (* this can only happen if there is a bug in the caller *) + let path = path service in + let invalid fmt = Printf.ksprintf invalid_arg fmt in + invalid "Tried to start %s twice: %s exists (%s)" service path (ctime path) + | false -> + () + ) ; try start_transient ?env ?properties ~exec_ty ~service cmd args with e -> Backtrace.is_important e ;