Skip to content

Commit c4ab26a

Browse files
committed
Merge pull request #4 from djs55/daemonize
Respect the --daemon option and use syslog for logging
2 parents bf8b6f0 + e7afbff commit c4ab26a

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

main.ml

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@ module R = Rpc
1717
open Core.Std
1818
open Async.Std
1919

20+
let use_syslog = ref false
21+
22+
let info fmt =
23+
Printf.ksprintf (fun s ->
24+
if !use_syslog then begin
25+
(* FIXME: this is synchronous and will block other I/O *)
26+
Core.Syslog.syslog ~level:Core.Syslog.Level.INFO ~add_stderr:true s;
27+
return ()
28+
end else begin
29+
let w = Lazy.force Writer.stderr in
30+
Writer.write w s;
31+
Writer.newline w;
32+
Writer.flushed w;
33+
end
34+
) fmt
35+
2036
let backend_error name args =
21-
Printf.fprintf stderr "backend_error %s [ %s ]\n%!" name (String.concat ~sep:"; " args);
2237
let open Storage_interface in
2338
let exnty = Exception.Backend_error (name, args) in
2439
Exception.rpc_of_exnty exnty
@@ -360,7 +375,8 @@ let process root_dir name x =
360375
Deferred.return (Error (backend_error "UNIMPLEMENTED" [ name ])))
361376
>>= function
362377
| Result.Error error ->
363-
Printf.fprintf stderr "returning %s\n%!" (Jsonrpc.string_of_response (R.failure error));
378+
info "returning error %s" (Jsonrpc.string_of_response (R.failure error))
379+
>>= fun () ->
364380
return (Jsonrpc.string_of_response (R.failure error))
365381
| Result.Ok rpc ->
366382
return (Jsonrpc.string_of_response rpc)
@@ -372,15 +388,17 @@ let create switch_port root_dir name =
372388
if Hashtbl.mem servers name
373389
then return ()
374390
else begin
375-
Printf.fprintf stderr "Adding %s\n%!" name;
391+
info "Adding %s" name
392+
>>= fun () ->
376393
Protocol_async.M.connect switch_port >>= fun c ->
377394
let server = Protocol_async.Server.listen (process root_dir name) c (Filename.basename name) in
378395
Hashtbl.add_exn servers name server;
379396
return ()
380397
end
381398

382399
let destroy switch_port name =
383-
Printf.fprintf stderr "Removing %s\n%!" name;
400+
info "Removing %s" name
401+
>>= fun () ->
384402
Protocol_async.M.connect switch_port >>= fun c ->
385403
Hashtbl.remove servers name;
386404
return ()
@@ -412,7 +430,8 @@ let main ~root_dir ~switch_port =
412430
let rec loop () =
413431
( Pipe.read pipe >>= function
414432
| `Eof ->
415-
Printf.fprintf stderr "Received EOF from inotify event pipe\n%!";
433+
info "Received EOF from inotify event pipe"
434+
>>= fun () ->
416435
Shutdown.exit 1
417436
| `Ok (Created name)
418437
| `Ok (Moved (Into name)) ->
@@ -457,13 +476,21 @@ let _ =
457476
}
458477
] in
459478

460-
match configure2
479+
(match configure2
461480
~name:"xapi-script-storage"
462481
~version:Version.version
463482
~doc:description
464483
~resources
465484
() with
466-
| `Ok () -> main ~root_dir:!root_dir ~switch_port:!Xcp_client.switch_port
485+
| `Ok () -> ()
467486
| `Error x ->
468487
Printf.fprintf stderr "Error: %s\n%!" x;
469-
Pervasives.exit 1
488+
Pervasives.exit 1);
489+
490+
if !Xcp_service.daemon then begin
491+
Daemon.daemonize ();
492+
use_syslog := true;
493+
Core.Syslog.openlog ~id:"xapi-storage-script" ~facility:Core.Syslog.Facility.DAEMON ();
494+
end;
495+
main ~root_dir:!root_dir ~switch_port:!Xcp_client.switch_port
496+

0 commit comments

Comments
 (0)