let mntdir_path uuid = "/var/lib/xenvm/mnt-" ^ uuid
let _notify state code l =
- let fdopt =
- match state.vm_cfg.notify with
- | NotifyUnix path ->
- let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
- Unix.connect fd (Unix.ADDR_UNIX path);
- Some fd
- | NotifyTcp (host, port) ->
- let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
- Unix.connect fd (Unix.ADDR_INET (host, port));
- Some fd
- | NotifyTcp6 _ ->
- None
- | NotifyDBus path ->
- None
- | NotifyNone ->
- None
+ let connect_and_send addr fd =
+ finally (fun () ->
+ Unix.connect fd addr;
+ (* notification format is "<uuid>:<code>:<message>\n" *)
+ let s = String.concat ":" (state.vm_uuid :: string_of_int code :: l) ^ "\n" in
+ let (_: int) = Unix.write fd s 0 (String.length s) in
+ ()
+ ) (fun () -> Unix.close fd)
in
- match fdopt with
- | None -> ()
- | Some fd ->
- (* notification format is "<uuid>:<code>:<message>\n" *)
- let s = String.concat ":" (state.vm_uuid :: string_of_int code :: l) ^ "\n" in
- let (_: int) = Unix.write fd s 0 (String.length s) in
- (* FIXME we might want to cache it later on .. and catch EBADF or ECONNRESET for reopen signal. *)
- Unix.close fd
+ match state.vm_cfg.notify with
+ | NotifyUnix path ->
+ let fd = Unix.socket Unix.PF_UNIX Unix.SOCK_STREAM 0 in
+ let addr = Unix.ADDR_UNIX path in
+ connect_and_send addr fd
+ | NotifyTcp (host, port) ->
+ let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
+ let addr = Unix.ADDR_INET (host, port) in
+ connect_and_send addr fd
+ | NotifyTcp6 _ ->
+ ()
+ | NotifyDBus path ->
+ (* path/intf/method arbitrary for now. *)
+ let dbus_path = sprintf "/org/xen/vm/%s" state.vm_uuid in
+ let dbus_intf = "xenvm.signal.notify" in
+ let dbus_meth = "notify" in
+ let bus = DBus.Bus.get DBus.Bus.System in
+ let msg = DBus.Message.new_signal dbus_path dbus_intf dbus_meth in
+ DBus.Message.append msg [ (DBus.String state.vm_uuid);
+ (DBus.Int32 (Int32.of_int code));
+ (DBus.String (String.concat ":" l)) ];
+ let serial = DBus.Connection.send bus msg in
+ ignore serial;
+ DBus.Connection.flush bus;
+ ()
+ | NotifyNone ->
+ ()
let notify state code l =
info "sending notification %s" (String.concat ":" l);