b6449cae87
* `lib_stdlib`: basic extended OCaml stdlib and generic data structures * `lib_data_encoding`: almost independant 'Data_encoding' * `lib_error_monad`: almost independant 'Error_monad' * `lib_stdlib_lwt`: extended Lwt library * `lib_crypto`: all the crypto stuff (hashing, signing, cryptobox). * `lib_base`: - basic type definitions (Block_header, Operation, ...) - a module `TzPervasives` to bind them all and to be the single module opened everywhere. In the process, I splitted `Tezos_data` and `Hash` in multiple submodules, thus removing a lot of `-open`. The following two modules may not have found their place yet: - Base58 (currently in `lib_crypto`) - Cli_entries (currently in `lib_stdlib_lwt`)
59 lines
2.1 KiB
OCaml
59 lines
2.1 KiB
OCaml
(**************************************************************************)
|
|
(* *)
|
|
(* Copyright (c) 2014 - 2017. *)
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
(* *)
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
(* *)
|
|
(**************************************************************************)
|
|
|
|
open Error_monad
|
|
|
|
let create_inner
|
|
lock_command
|
|
?(close_on_exec=true)
|
|
?(unlink_on_exit=false) fn =
|
|
protect begin fun () ->
|
|
Lwt_unix.openfile fn Unix.[O_CREAT ; O_WRONLY; O_TRUNC] 0o644 >>= fun fd ->
|
|
if close_on_exec then Lwt_unix.set_close_on_exec fd ;
|
|
Lwt_unix.lockf fd lock_command 0 >>= fun () ->
|
|
if unlink_on_exit then
|
|
Lwt_main.at_exit (fun () -> Lwt_unix.unlink fn) ;
|
|
let pid_str = string_of_int @@ Unix.getpid () in
|
|
Lwt_unix.write_string fd pid_str 0 (String.length pid_str) >>= fun _ ->
|
|
return ()
|
|
end
|
|
|
|
let create = create_inner Unix.F_TLOCK
|
|
|
|
let blocking_create
|
|
?timeout
|
|
?(close_on_exec=true)
|
|
?(unlink_on_exit=false) fn =
|
|
let create () =
|
|
create_inner Unix.F_LOCK ~close_on_exec ~unlink_on_exit fn in
|
|
match timeout with
|
|
| None -> create ()
|
|
| Some duration -> Lwt_utils.with_timeout duration (fun _ -> create ())
|
|
|
|
let is_locked fn =
|
|
if not @@ Sys.file_exists fn then return false else
|
|
protect begin fun () ->
|
|
Lwt_unix.openfile fn [Unix.O_RDONLY] 0o644 >>= fun fd ->
|
|
Lwt.finalize (fun () ->
|
|
Lwt.try_bind
|
|
(fun () -> Lwt_unix.(lockf fd F_TEST 0))
|
|
(fun () -> return false)
|
|
(fun _ -> return true))
|
|
(fun () -> Lwt_unix.close fd)
|
|
end
|
|
|
|
let get_pid fn =
|
|
let open Lwt_io in
|
|
Lwt_utils.protect begin fun () ->
|
|
with_file ~mode:Input fn begin fun ic ->
|
|
read ic >>= fun content ->
|
|
return (int_of_string content)
|
|
end
|
|
end
|