diff --git a/albatross.opam b/albatross.opam index 9a63a9b..23dc2cf 100644 --- a/albatross.opam +++ b/albatross.opam @@ -32,6 +32,7 @@ depends: [ "metrics" {>= "0.2.0"} "metrics-lwt" {>= "0.2.0"} "metrics-influx" {>= "0.2.0"} + "ocaml-systemd" ] build: [ ["dune" "subst"] {pinned} diff --git a/daemon/albatross_log.ml b/daemon/albatross_log.ml index 98cfcc0..67d5e95 100644 --- a/daemon/albatross_log.ml +++ b/daemon/albatross_log.ml @@ -148,7 +148,15 @@ let handle mvar ring s addr = let m = Vmm_core.conn_metrics "unix" -let jump _ file read_only influx tmpdir = +let server_socket systemd = + if systemd then + match Daemon.listen_fds () with + | [fd] -> Lwt.return (Lwt_unix.of_unix_file_descr fd) + | _ -> failwith "Unexpected number of sockets" + else + Vmm_lwt.server_socket `Log + +let jump _ systemd file read_only influx tmpdir = Sys.(set_signal sigpipe Signal_ignore) ; Albatross_cli.set_tmpdir tmpdir; Lwt_main.run @@ -161,7 +169,7 @@ let jump _ file read_only influx tmpdir = Lwt.return_unit end else begin Albatross_cli.init_influx "albatross_log" influx; - Vmm_lwt.server_socket `Log >>= fun s -> + server_socket systemd >>= fun s -> let ring = Vmm_ring.create `Startup () in List.iter (Vmm_ring.write ring) entries ; let mvar = Lwt_mvar.create_empty () in @@ -191,8 +199,12 @@ let read_only = let doc = "Only read log file and present entries" in Arg.(value & flag & info [ "read-only" ] ~doc) +let systemd = + let doc = "Pass this flag when systemd socket activation is being used" in + Arg.(value & flag & info [ "systemd-socket-activation" ] ~doc) + let cmd = - Term.(const jump $ setup_log $ file $ read_only $ influx $ tmpdir), + Term.(const jump $ setup_log $ systemd $ file $ read_only $ influx $ tmpdir), Term.info "albatross_log" ~version let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1 diff --git a/daemon/dune b/daemon/dune index 832d3c6..b0b4eeb 100644 --- a/daemon/dune +++ b/daemon/dune @@ -24,4 +24,4 @@ (public_name albatross-log) (package albatross) (modules albatross_log) - (libraries albatross.cli albatross)) + (libraries albatross.cli albatross systemd)) diff --git a/packaging/Linux/albatross_log.service b/packaging/Linux/albatross_log.service index 56a6408..b5a09b7 100644 --- a/packaging/Linux/albatross_log.service +++ b/packaging/Linux/albatross_log.service @@ -17,4 +17,5 @@ PIDFile=%t/albatross/log.pid RestrictAddressFamilies=AF_UNIX [Install] +Also=albatross_log.socket WantedBy=multi-user.target diff --git a/packaging/Linux/albatross_log.socket b/packaging/Linux/albatross_log.socket new file mode 100644 index 0000000..731b0b0 --- /dev/null +++ b/packaging/Linux/albatross_log.socket @@ -0,0 +1,12 @@ +[Unit] +Description=Albatross log socket +PartOf=albatross_log.service + +[Socket] +ListenStream=%t/albatross/util/log.sock +SocketUser=albatross +#SocketMode= # FIXME: permissions +Accept=no + +[Install] +WantedBy=sockets.target