1 # QEMU Monitor Protocol Python class
3 # Copyright (C) 2009, 2010 Red Hat Inc.
6 # Luiz Capitulino <lcapitulino@redhat.com>
8 # This work is licensed under the terms of the GNU GPL, version 2. See
9 # the COPYING file in the top-level directory.
16 class QMPError(Exception):
19 class QMPConnectError(QMPError
):
22 class QMPCapabilitiesError(QMPError
):
25 class QMPTimeoutError(QMPError
):
28 class QEMUMonitorProtocol
:
29 def __init__(self
, address
, server
=False, debug
=False):
31 Create a QEMUMonitorProtocol class.
33 @param address: QEMU address, can be either a unix socket path (string)
34 or a tuple in the form ( address, port ) for a TCP
36 @param server: server mode listens on the socket (bool)
37 @raise socket.error on socket connection errors
38 @note No connection is established, this is done by the connect() or
42 self
.__address
= address
44 self
.__sock
= self
.__get
_sock
()
46 self
.__sock
.setsockopt(socket
.SOL_SOCKET
, socket
.SO_REUSEADDR
, 1)
47 self
.__sock
.bind(self
.__address
)
51 if isinstance(self
.__address
, tuple):
52 family
= socket
.AF_INET
54 family
= socket
.AF_UNIX
55 return socket
.socket(family
, socket
.SOCK_STREAM
)
57 def __negotiate_capabilities(self
):
58 greeting
= self
.__json
_read
()
59 if greeting
is None or not greeting
.has_key('QMP'):
61 # Greeting seems ok, negotiate capabilities
62 resp
= self
.cmd('qmp_capabilities')
65 raise QMPCapabilitiesError
67 def __json_read(self
, only_event
=False):
69 data
= self
.__sockfile
.readline()
72 resp
= json
.loads(data
)
75 print >>sys
.stderr
, "QMP:<<< %s" % resp
76 self
.__events
.append(resp
)
83 def __get_events(self
, wait
=False):
85 Check for new events in the stream and cache them in __events.
87 @param wait (bool): block until an event is available.
88 @param wait (float): If wait is a float, treat it as a timeout value.
90 @raise QMPTimeoutError: If a timeout float is provided and the timeout
92 @raise QMPConnectError: If wait is True but no events could be retrieved
93 or if some other error occurred.
96 # Check for new events regardless and pull them into the cache:
97 self
.__sock
.setblocking(0)
100 except socket
.error
as err
:
101 if err
[0] == errno
.EAGAIN
:
104 self
.__sock
.setblocking(1)
106 # Wait for new events, if needed.
107 # if wait is 0.0, this means "no wait" and is also implicitly false.
108 if not self
.__events
and wait
:
109 if isinstance(wait
, float):
110 self
.__sock
.settimeout(wait
)
112 ret
= self
.__json
_read
(only_event
=True)
113 except socket
.timeout
:
114 raise QMPTimeoutError("Timeout waiting for event")
116 raise QMPConnectError("Error while reading from socket")
118 raise QMPConnectError("Error while reading from socket")
119 self
.__sock
.settimeout(None)
121 def connect(self
, negotiate
=True):
123 Connect to the QMP Monitor and perform capabilities negotiation.
125 @return QMP greeting dict
126 @raise socket.error on socket connection errors
127 @raise QMPConnectError if the greeting is not received
128 @raise QMPCapabilitiesError if fails to negotiate capabilities
130 self
.__sock
.connect(self
.__address
)
131 self
.__sockfile
= self
.__sock
.makefile()
133 return self
.__negotiate
_capabilities
()
137 Await connection from QMP Monitor and perform capabilities negotiation.
139 @return QMP greeting dict
140 @raise socket.error on socket connection errors
141 @raise QMPConnectError if the greeting is not received
142 @raise QMPCapabilitiesError if fails to negotiate capabilities
144 self
.__sock
.settimeout(15)
145 self
.__sock
, _
= self
.__sock
.accept()
146 self
.__sockfile
= self
.__sock
.makefile()
147 return self
.__negotiate
_capabilities
()
149 def cmd_obj(self
, qmp_cmd
):
151 Send a QMP command to the QMP Monitor.
153 @param qmp_cmd: QMP command to be sent as a Python dict
154 @return QMP response as a Python dict or None if the connection has
158 print >>sys
.stderr
, "QMP:>>> %s" % qmp_cmd
160 self
.__sock
.sendall(json
.dumps(qmp_cmd
))
161 except socket
.error
as err
:
162 if err
[0] == errno
.EPIPE
:
164 raise socket
.error(err
)
165 resp
= self
.__json
_read
()
167 print >>sys
.stderr
, "QMP:<<< %s" % resp
170 def cmd(self
, name
, args
=None, id=None):
172 Build a QMP command and send it to the QMP Monitor.
174 @param name: command name (string)
175 @param args: command arguments (dict)
176 @param id: command id (dict, list, string or int)
178 qmp_cmd
= { 'execute': name
}
180 qmp_cmd
['arguments'] = args
183 return self
.cmd_obj(qmp_cmd
)
185 def command(self
, cmd
, **kwds
):
186 ret
= self
.cmd(cmd
, kwds
)
187 if ret
.has_key('error'):
188 raise Exception(ret
['error']['desc'])
191 def pull_event(self
, wait
=False):
193 Get and delete the first available QMP event.
195 @param wait (bool): block until an event is available.
196 @param wait (float): If wait is a float, treat it as a timeout value.
198 @raise QMPTimeoutError: If a timeout float is provided and the timeout
200 @raise QMPConnectError: If wait is True but no events could be retrieved
201 or if some other error occurred.
203 @return The first available QMP event, or None.
205 self
.__get
_events
(wait
)
208 return self
.__events
.pop(0)
211 def get_events(self
, wait
=False):
213 Get a list of available QMP events.
215 @param wait (bool): block until an event is available.
216 @param wait (float): If wait is a float, treat it as a timeout value.
218 @raise QMPTimeoutError: If a timeout float is provided and the timeout
220 @raise QMPConnectError: If wait is True but no events could be retrieved
221 or if some other error occurred.
223 @return The list of available QMP events.
225 self
.__get
_events
(wait
)
228 def clear_events(self
):
230 Clear current list of pending events.
236 self
.__sockfile
.close()
238 timeout
= socket
.timeout
240 def settimeout(self
, timeout
):
241 self
.__sock
.settimeout(timeout
)
243 def get_sock_fd(self
):
244 return self
.__sock
.fileno()
246 def is_scm_available(self
):
247 return self
.__sock
.family
== socket
.AF_UNIX