From: Vincent Hanquez Date: Wed, 22 Jul 2009 16:56:54 +0000 (+0100) Subject: reorganize notify to send notification more cleanly on failure (fd leaking X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=4563e03c5a694b67a33819a9836fac21a8aff6f8;p=xenclient%2Ftoolstack.git reorganize notify to send notification more cleanly on failure (fd leaking otherwise) and also implement the dbus notification. --- diff --git a/xenvm/vmact.ml b/xenvm/vmact.ml index 56562ec..332a44a 100644 --- a/xenvm/vmact.ml +++ b/xenvm/vmact.ml @@ -35,31 +35,42 @@ exception Vm_is_already_created of int * vmlifestate 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 "::\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 "::\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);