Skip to content

Commit 109bb48

Browse files
committed
Accept an optional startup function in (maybe_)daemonize
The startup function, if specified, is executed before the PID file is written. If `maybe_daemonize` decides not to daemonise, it still executes the startup function. Signed-off-by: Rob Hoes <[email protected]>
1 parent 3358e5f commit 109bb48

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/xcp_service.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ let pidfile_write filename =
540540

541541
(* Cf Stevens et al, Advanced Programming in the UNIX Environment,
542542
Section 13.3 *)
543-
let daemonize () =
543+
let daemonize ?start_fn () =
544544
if not (have_daemonized ())
545545
then
546546
ign_int (Unix.umask 0);
@@ -550,6 +550,7 @@ let daemonize () =
550550
Sys.set_signal Sys.sighup Sys.Signal_ignore;
551551
(match Unix.fork () with
552552
| 0 ->
553+
Opt.iter (fun fn -> fn ()) start_fn;
553554
Unix.chdir "/";
554555
mkdir_rec (Filename.dirname !pidfile) 0o755;
555556
pidfile_write !pidfile;
@@ -560,10 +561,14 @@ let daemonize () =
560561
assert (nullfd = Unix.stdin);
561562
let (_:Unix.file_descr) = Unix.dup nullfd in ();
562563
let (_:Unix.file_descr) = Unix.dup nullfd in ();
563-
| _ -> exit 0)
564+
| _ -> exit 0)
564565
| _ -> exit 0
565566

566-
let maybe_daemonize () = if !daemon then daemonize ()
567+
let maybe_daemonize ?start_fn () =
568+
if !daemon then
569+
daemonize ?start_fn ()
570+
else
571+
Opt.iter (fun fn -> fn ()) start_fn
567572

568573
let wait_forever () =
569574
while true do

lib/xcp_service.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ val serve_forever: server -> unit
5959
val daemon: bool ref
6060
val loglevel: unit -> Syslog.level
6161

62-
val daemonize: unit -> unit
62+
val daemonize: ?start_fn:(unit -> unit) -> unit -> unit
6363

64-
val maybe_daemonize: unit -> unit
64+
val maybe_daemonize: ?start_fn:(unit -> unit) -> unit -> unit

0 commit comments

Comments
 (0)