Patchwork [5/5] qmp: add the BLOCK_MEDIUM_CHANGED event

login
register
mail settings
Submitter Luiz Capitulino
Date Feb. 7, 2012, 6:09 p.m.
Message ID <1328638189-9534-6-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/139992/
State New
Headers show

Comments

Luiz Capitulino - Feb. 7, 2012, 6:09 p.m.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 QMP/qmp-events.txt |   17 +++++++++++++++++
 block.c            |   12 ++++++++++++
 monitor.c          |    3 +++
 monitor.h          |    1 +
 4 files changed, 33 insertions(+), 0 deletions(-)

Patch

diff --git a/QMP/qmp-events.txt b/QMP/qmp-events.txt
index ab32e62..a1815b5 100644
--- a/QMP/qmp-events.txt
+++ b/QMP/qmp-events.txt
@@ -26,6 +26,23 @@  Example:
 Note: If action is "stop", a STOP event will eventually follow the
 BLOCK_IO_ERROR event.
 
+BLOCK_MEDIUM_CHANGED
+--------------------
+
+Emitted when a medium is either inserted or removed from a drive. This should
+happen as a result of the 'eject' and 'change' commands.
+
+Data:
+
+- "device": device name (json-string)
+- "medium-inserted": true if a medium has been inserted, false if it has been
+                     removed from the drive
+
+{ "event": "BLOCK_MEDIUM_CHANGED",
+    "data": { "device": "ide1-cd1",
+              "medium-inserted": true },
+    "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
+
 GUEST_MEDIUM_EJECTED
 --------------------
 
diff --git a/block.c b/block.c
index 7f5b56d..1d40f82 100644
--- a/block.c
+++ b/block.c
@@ -941,10 +941,22 @@  void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
     }
 }
 
+static void bdrv_emit_qmp_medium_changed_event(BlockDriverState *bs, bool load)
+{
+    QObject *data;
+
+    data = qobject_from_jsonf("{ 'device': %s, 'medium-inserted': %i }",
+                              bdrv_get_device_name(bs), load);
+    monitor_protocol_event(QEVENT_BLOCK_MEDIUM_CHANGED, data);
+
+    qobject_decref(data);
+}
+
 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
 {
     if (bs->dev_ops && bs->dev_ops->change_media_cb) {
         bs->dev_ops->change_media_cb(bs->dev_opaque, load);
+        bdrv_emit_qmp_medium_changed_event(bs, load);
     }
 }
 
diff --git a/monitor.c b/monitor.c
index 48b1dea..8701ec1 100644
--- a/monitor.c
+++ b/monitor.c
@@ -488,6 +488,9 @@  void monitor_protocol_event(MonitorEvent event, QObject *data)
         case QEVENT_GUEST_MEDIUM_EJECTED:
             event_name = "GUEST_MEDIUM_EJECTED";
             break;
+        case QEVENT_BLOCK_MEDIUM_CHANGED:
+            event_name = "BLOCK_MEDIUM_CHANGED";
+            break;
         default:
             abort();
             break;
diff --git a/monitor.h b/monitor.h
index 06be7bb..4f9e6f7 100644
--- a/monitor.h
+++ b/monitor.h
@@ -39,6 +39,7 @@  typedef enum MonitorEvent {
     QEVENT_BLOCK_JOB_COMPLETED,
     QEVENT_BLOCK_JOB_CANCELLED,
     QEVENT_GUEST_MEDIUM_EJECTED,
+    QEVENT_BLOCK_MEDIUM_CHANGED,
     QEVENT_MAX,
 } MonitorEvent;