From f6ee00c1b33b35f6adcd8c27cc4013636fd202a4 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Wed, 30 Dec 2020 16:36:23 +0100 Subject: ucs: properly remove the unix socket when a signal is received When using a unix socket to serve µCS, the file would not be removed when the server was shut down. Teach µCS to catch sigints and sigterms, and call unlink on the socket if it exists. Signed-off-by: Alban Gruin --- src/ucs.ml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/ucs.ml') diff --git a/src/ucs.ml b/src/ucs.ml index 3b6027c..84d5988 100644 --- a/src/ucs.ml +++ b/src/ucs.ml @@ -49,7 +49,17 @@ let () = mode = match socket with | Some s -> `Unix_domain_socket (`File s) | None -> `TCP (`Port port) in - Lwt_main.run (Server.serve base_url celcat_uri mode) in + let stop, r = Lwt.wait () in + Lwt_unix.on_signal Sys.sigint + (fun _ -> Lwt.wakeup r ()) |> ignore; + Lwt_unix.on_signal Sys.sigterm + (fun _ -> Lwt.wakeup r ()) |> ignore; + Lwt_main.run ( + Lwt.finalize + (fun () -> Server.serve base_url celcat_uri mode stop) + (fun () -> match socket with + | Some s -> Lwt_unix.unlink s + | None -> Lwt.return_unit)) in let cmd = let doc = "micro celcatsanitizer: convert celcat \ calendar to ICS files, on the fly." and -- cgit v1.2.1