]> xenbits.xensource.com Git - xenclient/toolstack.git/commitdiff
reorganize notify to send notification more cleanly on failure (fd leaking
authorVincent Hanquez <vincent.hanquez@eu.citrix.com>
Wed, 22 Jul 2009 16:56:54 +0000 (17:56 +0100)
committerVincent Hanquez <vincent.hanquez@eu.citrix.com>
Wed, 22 Jul 2009 16:56:54 +0000 (17:56 +0100)
otherwise) and also implement the dbus notification.

xenvm/vmact.ml

index 56562ecbd46beedb4defd7fdef2b8542c9a43214..332a44a8aec51cbd88473c901b225efc471578c7 100644 (file)
@@ -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 "<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);