aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2020-12-30 16:36:23 +0100
committerAlban Gruin2020-12-30 16:36:23 +0100
commitf6ee00c1b33b35f6adcd8c27cc4013636fd202a4 (patch)
tree5ed3960bd93e55b1d88ac7b1aa7003b8a35f4279
parent7a3cd6d029e73a28e1229ac24edb4c5bd87162b9 (diff)
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 <alban at pa1ch dot fr>
-rw-r--r--src/server.ml4
-rw-r--r--src/server.mli2
-rw-r--r--src/ucs.ml12
3 files changed, 14 insertions, 4 deletions
diff --git a/src/server.ml b/src/server.ml
index 2c1c4b9..d3cf00e 100644
--- a/src/server.ml
+++ b/src/server.ml
@@ -47,7 +47,7 @@ let respond ?(mime="text/html; charset=utf-8") ?(status=`OK) body =
let headers = Header.init_with "Content-Type" mime in
Server.respond_string ~status ~body ~headers ()
-let serve base_url celcat_url mode =
+let serve base_url celcat_url mode stop =
let fetch = fetch celcat_url in
let callback _conn req _body =
let meth = Request.meth req and
@@ -75,4 +75,4 @@ let serve base_url celcat_url mode =
| _ ->
Server.respond_string
~status:`Method_not_allowed ~body:"Method not allowed\n" () in
- Server.create ~mode (Server.make ~callback ())
+ Server.create ~stop ~mode (Server.make ~callback ())
diff --git a/src/server.mli b/src/server.mli
index af3f0e8..cf3bac2 100644
--- a/src/server.mli
+++ b/src/server.mli
@@ -15,4 +15,4 @@
* along with ucs. If not, see <http://www.gnu.org/licenses/>.
*)
-val serve : string -> Uri.t -> Conduit_lwt_unix.server -> unit Lwt.t
+val serve : string -> Uri.t -> Conduit_lwt_unix.server -> unit Lwt.t -> unit Lwt.t
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