diff options
author | Daniil Baturin <daniil@baturin.org> | 2018-02-16 21:21:24 +0700 |
---|---|---|
committer | Daniil Baturin <daniil@baturin.org> | 2018-02-16 21:21:24 +0700 |
commit | d4ee9b96e02e1413cc19ce638aec07408468ca30 (patch) | |
tree | fd5fa774ed6c2052eb430d714c316f8fe9c5c2e7 | |
parent | e4cac118645ffb290ec78e4cde6c9757219d3a10 (diff) | |
download | vyconf-d4ee9b96e02e1413cc19ce638aec07408468ca30.tar.gz vyconf-d4ee9b96e02e1413cc19ce638aec07408468ca30.zip |
Delete the old socket file if it exists.
Since there's no REUSEADDR semantic for UNIX domain sockets,
that's best we can do.
-rw-r--r-- | src/startup.ml | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/startup.ml b/src/startup.ml index 34c1127..cea5f02 100644 --- a/src/startup.ml +++ b/src/startup.ml @@ -46,9 +46,18 @@ let check_dirs dirs = | Ok _ -> () | Error err -> panic err +let delete_socket_if_exists sockfile = + try + let _ = Unix.stat sockfile in + Unix.unlink sockfile + with + | Unix.Unix_error (Unix.ENOENT, _, _) -> () + | _ -> panic "Could not delete old socket file, exiting" + (** Bind to a UNIX socket *) let create_socket sockfile = let open Lwt_unix in + let () = delete_socket_if_exists sockfile in let backlog = 10 in let%lwt sock = socket PF_UNIX SOCK_STREAM 0 |> Lwt.return in let%lwt () = Lwt_unix.bind sock @@ ADDR_UNIX(sockfile) in |