Skip to content

Commit e90a98e

Browse files
authored
CP-28369: Remove Unixext.daemonize (xapi-project#6026)
Last usage of `Unixext.daemonize` was in `cdrommon`, drop it and move the daemon to be fully handled by systemd (instead of `type=forking`). `cdrommon` service is only started by storage scripts, and I'm not sure if it's tested at all, but these changes pass BST+BVT.
2 parents 6e16163 + c261733 commit e90a98e

File tree

14 files changed

+8
-119
lines changed

14 files changed

+8
-119
lines changed

ocaml/cdrommon/cdrommon.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,5 @@ let () =
6363
Printf.eprintf "usage: %s <cdrompath>\n" Sys.argv.(0) ;
6464
exit 1
6565
) ;
66-
Xapi_stdext_unix.Unixext.daemonize () ;
6766
(* check every 2 seconds *)
6867
check 2 Sys.argv.(1)

ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/unixext.ml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,27 +94,6 @@ let with_file file mode perms f =
9494
(fun () -> f fd)
9595
(fun () -> Unix.close fd)
9696

97-
(* !! Must call this before spawning any threads !! *)
98-
99-
(** daemonize a process *)
100-
let daemonize () =
101-
match Unix.fork () with
102-
| 0 -> (
103-
if Unix.setsid () == -1 then
104-
failwith "Unix.setsid failed" ;
105-
match Unix.fork () with
106-
| 0 ->
107-
with_file "/dev/null" [Unix.O_WRONLY] 0 (fun nullfd ->
108-
Unix.close Unix.stdin ;
109-
Unix.dup2 nullfd Unix.stdout ;
110-
Unix.dup2 nullfd Unix.stderr
111-
)
112-
| _ ->
113-
exit 0
114-
)
115-
| _ ->
116-
exit 0
117-
11897
exception Break
11998

12099
let lines_fold f start input =

ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/unixext.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ val pidfile_write : string -> unit
3030

3131
val pidfile_read : string -> int option
3232

33-
val daemonize : unit -> unit
34-
3533
val with_file :
3634
string
3735
-> Unix.open_flag list
@@ -262,7 +260,7 @@ val test_open : int -> unit
262260
to [Xapi_stdext_unix.Unixext.select] that use file descriptors, because such calls will then immediately fail.
263261
264262
This assumes that [ulimit -n] has been suitably increased in the test environment.
265-
263+
266264
Can only be called once in a program, and will raise an exception otherwise.
267265
268266
The file descriptors will stay open until the program exits.

ocaml/networkd/bin/networkd.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,15 +224,11 @@ let () =
224224
~rpc_fn:(Idl.Exn.server Network_server.S.implementation)
225225
()
226226
in
227-
Xcp_service.maybe_daemonize
228-
~start_fn:(fun () ->
229-
Debug.set_facility Syslog.Local5 ;
230-
(* We should make the following configurable *)
231-
Debug.disable "http" ;
232-
handle_shutdown () ;
233-
Debug.with_thread_associated "main" start server
234-
)
235-
() ;
227+
Debug.set_facility Syslog.Local5 ;
228+
(* We should make the following configurable *)
229+
Debug.disable "http" ;
230+
handle_shutdown () ;
231+
Debug.with_thread_associated "main" start server ;
236232
let module Daemon = Xapi_stdext_unix.Unixext.Daemon in
237233
if Daemon.systemd_notify Daemon.State.Ready then
238234
()

ocaml/squeezed/src/squeezed.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ let _ =
110110
~rpc_fn:(Idl.Exn.server S.implementation)
111111
()
112112
in
113-
maybe_daemonize () ;
114-
(* NB Initialise the xenstore connection after daemonising, otherwise we lose
115-
our connection *)
116113
let _ = Thread.create Memory_server.record_boot_time_host_free_memory () in
117114
let rpc_server = Thread.create Xcp_service.serve_forever server in
118115
Memory_server.start_balance_thread balance_check_interval ;

ocaml/xapi-idl/lib/xcp_service.ml

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ let log_destination = ref "syslog:daemon"
3131

3232
let log_level = ref Syslog.Debug
3333

34-
let daemon = ref false
35-
36-
let have_daemonized () = Unix.getppid () = 1
37-
3834
let common_prefix = "org.xen.xapi."
3935

4036
let finally f g =
@@ -196,11 +192,6 @@ let common_options =
196192
, (fun () -> !log_destination)
197193
, "Where to write log messages"
198194
)
199-
; ( "daemon"
200-
, Arg.Bool (fun x -> daemon := x)
201-
, (fun () -> string_of_bool !daemon)
202-
, "True if we are to daemonise"
203-
)
204195
; ( "disable-logging-for"
205196
, Arg.String
206197
(fun x ->
@@ -552,8 +543,6 @@ let http_handler call_of_string string_of_response process s =
552543
Response.write (fun _t -> ()) response oc
553544
)
554545

555-
let ign_int (t : int) = ignore t
556-
557546
let default_raw_fn rpc_fn s =
558547
http_handler Xmlrpc.call_of_string Xmlrpc.string_of_response rpc_fn s
559548

@@ -635,52 +624,6 @@ let serve_forever = function
635624
let rec forever () = Thread.delay 3600. ; forever () in
636625
forever ()
637626

638-
let pidfile_write filename =
639-
let fd =
640-
Unix.openfile filename [Unix.O_WRONLY; Unix.O_CREAT; Unix.O_TRUNC] 0o640
641-
in
642-
finally
643-
(fun () ->
644-
let pid = Unix.getpid () in
645-
let buf = string_of_int pid ^ "\n" |> Bytes.of_string in
646-
let len = Bytes.length buf in
647-
if Unix.write fd buf 0 len <> len then
648-
failwith "pidfile_write failed"
649-
)
650-
(fun () -> Unix.close fd)
651-
652-
(* Cf Stevens et al, Advanced Programming in the UNIX Environment,
653-
Section 13.3 *)
654-
let daemonize ?start_fn () =
655-
if not (have_daemonized ()) then
656-
ign_int (Unix.umask 0) ;
657-
match Unix.fork () with
658-
| 0 -> (
659-
if Unix.setsid () == -1 then failwith "Unix.setsid failed" ;
660-
Sys.set_signal Sys.sighup Sys.Signal_ignore ;
661-
match Unix.fork () with
662-
| 0 ->
663-
Option.iter (fun fn -> fn ()) start_fn ;
664-
Unix.chdir "/" ;
665-
mkdir_rec (Filename.dirname !pidfile) 0o755 ;
666-
pidfile_write !pidfile ;
667-
let nullfd = Unix.openfile "/dev/null" [Unix.O_RDWR] 0 in
668-
Unix.dup2 nullfd Unix.stdin ;
669-
Unix.dup2 nullfd Unix.stdout ;
670-
Unix.dup2 nullfd Unix.stderr ;
671-
Unix.close nullfd
672-
| _ ->
673-
exit 0
674-
)
675-
| _ ->
676-
exit 0
677-
678-
let maybe_daemonize ?start_fn () =
679-
if !daemon then
680-
daemonize ?start_fn ()
681-
else
682-
Option.iter (fun fn -> fn ()) start_fn
683-
684627
let cli ~name ~doc ~version ~cmdline_gen =
685628
let default = Term.(ret (const (fun _ -> `Help (`Pager, None)) $ const ())) in
686629
let version =

ocaml/xapi-idl/lib/xcp_service.mli

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,8 @@ val make :
5454

5555
val serve_forever : server -> unit
5656

57-
val daemon : bool ref
58-
5957
val loglevel : unit -> Syslog.level
6058

61-
val daemonize : ?start_fn:(unit -> unit) -> unit -> unit
62-
63-
val maybe_daemonize : ?start_fn:(unit -> unit) -> unit -> unit
64-
6559
val cli :
6660
name:string
6761
-> doc:string

ocaml/xapi-storage-script/main.ml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,11 +2043,6 @@ let _ =
20432043
in
20442044
configure2 ~name:"xapi-script-storage" ~version:Xapi_version.version
20452045
~doc:description ~resources ~options () ;
2046-
if !Xcp_service.daemon then (
2047-
Xcp_service.maybe_daemonize () ;
2048-
use_syslog := true ;
2049-
info "Daemonisation successful."
2050-
) ;
20512046
let run () =
20522047
let ( let* ) = ( >>= ) in
20532048
let* observer_enabled = observer_is_component_enabled () in

ocaml/xapi/xapi_main.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ let _ =
2222
Debug.set_facility Syslog.Local5 ;
2323
Sys.enable_runtime_warnings true ;
2424
init_args () ;
25-
(* need to read args to find out whether to daemonize or not *)
26-
Xcp_service.maybe_daemonize () ;
2725
(* Disable logging for the module requested in the config *)
2826
List.iter
2927
(fun m ->

ocaml/xcp-rrdd/bin/rrdd/xcp_rrdd.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,6 @@ let _ =
747747
debug "Reading configuration file .." ;
748748
Xcp_service.configure2 ~name:Sys.argv.(0) ~version:Xapi_version.version ~doc
749749
~options () ;
750-
Xcp_service.maybe_daemonize () ;
751750
debug "Starting the HTTP server .." ;
752751
(* Eventually we should switch over to xcp_service to declare our services,
753752
but since it doesn't support HTTP GET and PUT we keep the old code for now.

0 commit comments

Comments
 (0)