Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions lib/xapi-stdext-threads/semaphore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,8 @@ let release s k =

let execute_with_weight s k f =
acquire s k;
try
let x = f () in
release s k;
x
with e ->
release s k;
raise e
Xapi_stdext_pervasives.Pervasiveext.finally f
(fun () -> release s k)

let execute s f =
execute_with_weight s 1 f
4 changes: 1 addition & 3 deletions lib/xapi-stdext-threads/threadext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ module Mutex = struct
(** execute the function f with the mutex hold *)
let execute lock f =
Mutex.lock lock;
let r = begin try f () with exn -> Mutex.unlock lock; raise exn end; in
Mutex.unlock lock;
r
Xapi_stdext_pervasives.Pervasiveext.finally f (fun () -> Mutex.unlock lock)
end


Expand Down
44 changes: 20 additions & 24 deletions lib/xapi-stdext-unix/unixext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ let pidfile_read filename =
with _ -> None)
(fun () -> Unix.close fd)

(** open a file, and make sure the close is always done *)
let with_file file mode perms f =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could even expose this in the interface

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it's there!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It already is, I just moved it.

let fd = Unix.openfile file mode perms in
Xapi_stdext_pervasives.Pervasiveext.finally
(fun () -> f fd)
(fun () -> Unix.close fd)

(** daemonize a process *)
(* !! Must call this before spawning any threads !! *)
let daemonize () =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really deprecate this function... nowadays it's systemd duty to damonize

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a ticket, CP-28369

Expand All @@ -73,14 +80,11 @@ let daemonize () =

begin match Unix.fork () with
| 0 ->
let nullfd = Unix.openfile "/dev/null" [ Unix.O_WRONLY ] 0 in
begin try
Unix.close Unix.stdin;
Unix.dup2 nullfd Unix.stdout;
Unix.dup2 nullfd Unix.stderr;
with exn -> Unix.close nullfd; raise exn
end;
Unix.close nullfd
with_file "/dev/null" [ Unix.O_WRONLY ] 0
(fun nullfd ->
Unix.close Unix.stdin;
Unix.dup2 nullfd Unix.stdout;
Unix.dup2 nullfd Unix.stderr)
| _ -> exit 0
end
| _ -> exit 0
Expand Down Expand Up @@ -115,15 +119,6 @@ let with_input_channel file f =
(fun () -> f input)
(fun () -> close_in input)

(** open a file, and make sure the close is always done *)
let with_file file mode perms f =
let fd = Unix.openfile file mode perms in
let r =
try f fd
with exn -> Unix.close fd; raise exn
in
Unix.close fd;
r

let file_lines_fold f start file_path = with_input_channel file_path (lines_fold f start)

Expand All @@ -148,12 +143,9 @@ let fd_blocks_fold block_size f start fd =

let with_directory dir f =
let dh = Unix.opendir dir in
let r =
try f dh
with exn -> Unix.closedir dh; raise exn
in
Unix.closedir dh;
r
Xapi_stdext_pervasives.Pervasiveext.finally
(fun () -> f dh)
(fun () -> Unix.closedir dh)

let buffer_of_fd fd =
fd_blocks_fold 1024 (fun b s -> Buffer.add_bytes b s; b) (Buffer.create 1024) fd
Expand Down Expand Up @@ -262,6 +254,7 @@ let open_connection_fd host port =
connect s ai.ai_addr;
s
with e ->
Backtrace.is_important e;
close s;
raise e

Expand All @@ -271,7 +264,10 @@ let open_connection_unix_fd filename =
let addr = Unix.ADDR_UNIX(filename) in
Unix.connect s addr;
s
with e -> Unix.close s; raise e
with e ->
Backtrace.is_important e;
Unix.close s;
raise e

module CBuf = struct
(** A circular buffer constructed from a string *)
Expand Down