Skip to content

Commit a39afb5

Browse files
CP-13469: upgrade/downgrade: Use flock() instead of lockf()
Signed-off-by: Si Beaumont <[email protected]>
1 parent 5aea889 commit a39afb5

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

idl/flock.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(*
2+
* Copyright (C) 2015 Citrix Systems Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*)
14+
open Core.Std
15+
16+
exception Could_not_obtain_lock of string
17+
18+
let with_flock path f =
19+
Unix.mkdir_p ~perm:0o700 (Filename.dirname path);
20+
let fd = Unix.(openfile path ~mode:[O_WRONLY; O_CREAT] ~perm:0o644) in
21+
let rec try_lock tries =
22+
if tries > 0 then
23+
if Unix.(flock fd Flock_command.lock_exclusive) then ()
24+
else begin Unix.sleep 1; try_lock (tries - 1) end
25+
else raise (Could_not_obtain_lock path)
26+
in
27+
try_lock 5;
28+
protect ~f ~finally:(fun () -> Unix.(ignore (flock fd Flock_command.unlock)))

xenvm/xenvm.ml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,8 @@ let format config name filenames =
8282
(* LVM locking code can be seen here:
8383
* https://git.fedorahosted.org/cgit/lvm2.git/tree/lib/misc/lvm-flock.c#n141 *)
8484
let with_lvm_lock vg_name f =
85-
let lock_dir = "/run/lock/lvm" in
86-
let lock_path = Filename.concat lock_dir ("V_" ^ vg_name ^ ":aux") in
87-
Lwt.catch (fun () ->
88-
mkdir_rec lock_dir 0o0700;
89-
Lwt_unix.(openfile lock_path [O_CREAT; O_TRUNC; O_RDWR] 0o644)
90-
>>= fun fd ->
91-
Lwt_unix.(lockf fd F_LOCK) 0;
92-
) (function _ -> fail (Failure "Could_not_obtain_lvm_lock")) >>= fun () ->
93-
Lwt.finalize f (fun () -> Lwt_unix.unlink lock_path)
85+
let lock_path = Filename.concat "/run/lock/lvm" ("V_" ^ vg_name ^ ":aux") in
86+
Flock.with_flock lock_path f
9487

9588
(* Change the label on the PV and revert it if the function [f] fails *)
9689
let with_label_change block magic f =

0 commit comments

Comments
 (0)