2009-11-27 00:59:09 +00:00
|
|
|
# QEMU Monitor Protocol Python class
|
2013-09-10 20:39:23 +00:00
|
|
|
#
|
2010-10-27 19:43:34 +00:00
|
|
|
# Copyright (C) 2009, 2010 Red Hat Inc.
|
2009-11-27 00:59:09 +00:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Luiz Capitulino <lcapitulino@redhat.com>
|
|
|
|
#
|
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
# the COPYING file in the top-level directory.
|
|
|
|
|
2010-10-27 19:43:34 +00:00
|
|
|
import json
|
|
|
|
import errno
|
|
|
|
import socket
|
2016-07-20 13:23:09 +00:00
|
|
|
import sys
|
2009-11-27 00:59:09 +00:00
|
|
|
|
|
|
|
class QMPError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class QMPConnectError(QMPError):
|
|
|
|
pass
|
|
|
|
|
2010-10-27 19:43:34 +00:00
|
|
|
class QMPCapabilitiesError(QMPError):
|
|
|
|
pass
|
|
|
|
|
2015-04-17 23:50:06 +00:00
|
|
|
class QMPTimeoutError(QMPError):
|
|
|
|
pass
|
|
|
|
|
2009-11-27 00:59:09 +00:00
|
|
|
class QEMUMonitorProtocol:
|
2016-07-20 13:23:09 +00:00
|
|
|
def __init__(self, address, server=False, debug=False):
|
2010-10-27 19:43:34 +00:00
|
|
|
"""
|
|
|
|
Create a QEMUMonitorProtocol class.
|
|
|
|
|
|
|
|
@param address: QEMU address, can be either a unix socket path (string)
|
|
|
|
or a tuple in the form ( address, port ) for a TCP
|
|
|
|
connection
|
2011-05-25 18:48:01 +00:00
|
|
|
@param server: server mode listens on the socket (bool)
|
|
|
|
@raise socket.error on socket connection errors
|
|
|
|
@note No connection is established, this is done by the connect() or
|
|
|
|
accept() methods
|
2010-10-27 19:43:34 +00:00
|
|
|
"""
|
|
|
|
self.__events = []
|
|
|
|
self.__address = address
|
2016-07-20 13:23:09 +00:00
|
|
|
self._debug = debug
|
2010-10-27 19:43:34 +00:00
|
|
|
self.__sock = self.__get_sock()
|
2011-05-25 18:48:01 +00:00
|
|
|
if server:
|
|
|
|
self.__sock.bind(self.__address)
|
|
|
|
self.__sock.listen(1)
|
2010-10-27 19:43:34 +00:00
|
|
|
|
|
|
|
def __get_sock(self):
|
|
|
|
if isinstance(self.__address, tuple):
|
|
|
|
family = socket.AF_INET
|
|
|
|
else:
|
|
|
|
family = socket.AF_UNIX
|
|
|
|
return socket.socket(family, socket.SOCK_STREAM)
|
|
|
|
|
2011-05-25 18:48:01 +00:00
|
|
|
def __negotiate_capabilities(self):
|
|
|
|
greeting = self.__json_read()
|
|
|
|
if greeting is None or not greeting.has_key('QMP'):
|
|
|
|
raise QMPConnectError
|
|
|
|
# Greeting seems ok, negotiate capabilities
|
|
|
|
resp = self.cmd('qmp_capabilities')
|
|
|
|
if "return" in resp:
|
|
|
|
return greeting
|
|
|
|
raise QMPCapabilitiesError
|
|
|
|
|
2011-05-25 18:48:00 +00:00
|
|
|
def __json_read(self, only_event=False):
|
2010-10-27 19:43:34 +00:00
|
|
|
while True:
|
|
|
|
data = self.__sockfile.readline()
|
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
resp = json.loads(data)
|
|
|
|
if 'event' in resp:
|
2016-07-20 13:23:09 +00:00
|
|
|
if self._debug:
|
|
|
|
print >>sys.stderr, "QMP:<<< %s" % resp
|
2010-10-27 19:43:34 +00:00
|
|
|
self.__events.append(resp)
|
2011-05-25 18:48:00 +00:00
|
|
|
if not only_event:
|
|
|
|
continue
|
2010-10-27 19:43:34 +00:00
|
|
|
return resp
|
|
|
|
|
|
|
|
error = socket.error
|
|
|
|
|
2015-04-17 23:50:06 +00:00
|
|
|
def __get_events(self, wait=False):
|
|
|
|
"""
|
|
|
|
Check for new events in the stream and cache them in __events.
|
|
|
|
|
|
|
|
@param wait (bool): block until an event is available.
|
|
|
|
@param wait (float): If wait is a float, treat it as a timeout value.
|
|
|
|
|
|
|
|
@raise QMPTimeoutError: If a timeout float is provided and the timeout
|
|
|
|
period elapses.
|
|
|
|
@raise QMPConnectError: If wait is True but no events could be retrieved
|
|
|
|
or if some other error occurred.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Check for new events regardless and pull them into the cache:
|
|
|
|
self.__sock.setblocking(0)
|
|
|
|
try:
|
|
|
|
self.__json_read()
|
2015-12-18 07:52:42 +00:00
|
|
|
except socket.error as err:
|
2015-04-17 23:50:06 +00:00
|
|
|
if err[0] == errno.EAGAIN:
|
|
|
|
# No data available
|
|
|
|
pass
|
|
|
|
self.__sock.setblocking(1)
|
|
|
|
|
|
|
|
# Wait for new events, if needed.
|
|
|
|
# if wait is 0.0, this means "no wait" and is also implicitly false.
|
|
|
|
if not self.__events and wait:
|
|
|
|
if isinstance(wait, float):
|
|
|
|
self.__sock.settimeout(wait)
|
|
|
|
try:
|
|
|
|
ret = self.__json_read(only_event=True)
|
|
|
|
except socket.timeout:
|
|
|
|
raise QMPTimeoutError("Timeout waiting for event")
|
|
|
|
except:
|
|
|
|
raise QMPConnectError("Error while reading from socket")
|
|
|
|
if ret is None:
|
|
|
|
raise QMPConnectError("Error while reading from socket")
|
|
|
|
self.__sock.settimeout(None)
|
|
|
|
|
2012-09-14 12:44:20 +00:00
|
|
|
def connect(self, negotiate=True):
|
2010-10-27 19:43:34 +00:00
|
|
|
"""
|
|
|
|
Connect to the QMP Monitor and perform capabilities negotiation.
|
|
|
|
|
|
|
|
@return QMP greeting dict
|
|
|
|
@raise socket.error on socket connection errors
|
|
|
|
@raise QMPConnectError if the greeting is not received
|
|
|
|
@raise QMPCapabilitiesError if fails to negotiate capabilities
|
|
|
|
"""
|
|
|
|
self.__sock.connect(self.__address)
|
2012-09-14 12:44:20 +00:00
|
|
|
self.__sockfile = self.__sock.makefile()
|
|
|
|
if negotiate:
|
|
|
|
return self.__negotiate_capabilities()
|
2011-05-25 18:48:01 +00:00
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
"""
|
|
|
|
Await connection from QMP Monitor and perform capabilities negotiation.
|
|
|
|
|
|
|
|
@return QMP greeting dict
|
|
|
|
@raise socket.error on socket connection errors
|
|
|
|
@raise QMPConnectError if the greeting is not received
|
|
|
|
@raise QMPCapabilitiesError if fails to negotiate capabilities
|
|
|
|
"""
|
2016-07-20 13:23:11 +00:00
|
|
|
self.__sock.settimeout(15)
|
2011-05-25 18:48:01 +00:00
|
|
|
self.__sock, _ = self.__sock.accept()
|
2012-10-15 20:58:02 +00:00
|
|
|
self.__sockfile = self.__sock.makefile()
|
2011-05-25 18:48:01 +00:00
|
|
|
return self.__negotiate_capabilities()
|
2009-11-27 00:59:09 +00:00
|
|
|
|
2010-10-27 19:43:34 +00:00
|
|
|
def cmd_obj(self, qmp_cmd):
|
|
|
|
"""
|
|
|
|
Send a QMP command to the QMP Monitor.
|
2009-11-27 00:59:09 +00:00
|
|
|
|
2010-10-27 19:43:34 +00:00
|
|
|
@param qmp_cmd: QMP command to be sent as a Python dict
|
|
|
|
@return QMP response as a Python dict or None if the connection has
|
|
|
|
been closed
|
|
|
|
"""
|
2016-07-20 13:23:09 +00:00
|
|
|
if self._debug:
|
|
|
|
print >>sys.stderr, "QMP:>>> %s" % qmp_cmd
|
2010-10-27 19:43:34 +00:00
|
|
|
try:
|
|
|
|
self.__sock.sendall(json.dumps(qmp_cmd))
|
2015-12-18 07:52:42 +00:00
|
|
|
except socket.error as err:
|
2010-10-27 19:43:34 +00:00
|
|
|
if err[0] == errno.EPIPE:
|
|
|
|
return
|
|
|
|
raise socket.error(err)
|
2016-07-20 13:23:09 +00:00
|
|
|
resp = self.__json_read()
|
|
|
|
if self._debug:
|
|
|
|
print >>sys.stderr, "QMP:<<< %s" % resp
|
|
|
|
return resp
|
2009-11-27 00:59:09 +00:00
|
|
|
|
2010-10-27 19:43:34 +00:00
|
|
|
def cmd(self, name, args=None, id=None):
|
|
|
|
"""
|
|
|
|
Build a QMP command and send it to the QMP Monitor.
|
2009-11-27 00:59:09 +00:00
|
|
|
|
2010-10-27 19:43:34 +00:00
|
|
|
@param name: command name (string)
|
|
|
|
@param args: command arguments (dict)
|
|
|
|
@param id: command id (dict, list, string or int)
|
|
|
|
"""
|
|
|
|
qmp_cmd = { 'execute': name }
|
|
|
|
if args:
|
|
|
|
qmp_cmd['arguments'] = args
|
|
|
|
if id:
|
|
|
|
qmp_cmd['id'] = id
|
|
|
|
return self.cmd_obj(qmp_cmd)
|
|
|
|
|
2012-02-20 22:28:29 +00:00
|
|
|
def command(self, cmd, **kwds):
|
|
|
|
ret = self.cmd(cmd, kwds)
|
|
|
|
if ret.has_key('error'):
|
|
|
|
raise Exception(ret['error']['desc'])
|
|
|
|
return ret['return']
|
|
|
|
|
2012-10-18 14:49:29 +00:00
|
|
|
def pull_event(self, wait=False):
|
|
|
|
"""
|
|
|
|
Get and delete the first available QMP event.
|
|
|
|
|
2015-04-17 23:50:06 +00:00
|
|
|
@param wait (bool): block until an event is available.
|
|
|
|
@param wait (float): If wait is a float, treat it as a timeout value.
|
|
|
|
|
|
|
|
@raise QMPTimeoutError: If a timeout float is provided and the timeout
|
|
|
|
period elapses.
|
|
|
|
@raise QMPConnectError: If wait is True but no events could be retrieved
|
|
|
|
or if some other error occurred.
|
|
|
|
|
|
|
|
@return The first available QMP event, or None.
|
2012-10-18 14:49:29 +00:00
|
|
|
"""
|
2015-04-17 23:50:06 +00:00
|
|
|
self.__get_events(wait)
|
|
|
|
|
|
|
|
if self.__events:
|
|
|
|
return self.__events.pop(0)
|
|
|
|
return None
|
2012-10-18 14:49:29 +00:00
|
|
|
|
2011-05-25 18:48:00 +00:00
|
|
|
def get_events(self, wait=False):
|
2010-10-27 19:43:34 +00:00
|
|
|
"""
|
|
|
|
Get a list of available QMP events.
|
2011-05-25 18:48:00 +00:00
|
|
|
|
2015-04-17 23:50:06 +00:00
|
|
|
@param wait (bool): block until an event is available.
|
|
|
|
@param wait (float): If wait is a float, treat it as a timeout value.
|
2014-02-24 05:45:01 +00:00
|
|
|
|
2015-04-17 23:50:06 +00:00
|
|
|
@raise QMPTimeoutError: If a timeout float is provided and the timeout
|
|
|
|
period elapses.
|
|
|
|
@raise QMPConnectError: If wait is True but no events could be retrieved
|
|
|
|
or if some other error occurred.
|
|
|
|
|
|
|
|
@return The list of available QMP events.
|
|
|
|
"""
|
|
|
|
self.__get_events(wait)
|
2010-10-27 19:43:34 +00:00
|
|
|
return self.__events
|
|
|
|
|
|
|
|
def clear_events(self):
|
|
|
|
"""
|
|
|
|
Clear current list of pending events.
|
|
|
|
"""
|
|
|
|
self.__events = []
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.__sock.close()
|
|
|
|
self.__sockfile.close()
|
2012-09-14 12:44:21 +00:00
|
|
|
|
|
|
|
timeout = socket.timeout
|
|
|
|
|
|
|
|
def settimeout(self, timeout):
|
|
|
|
self.__sock.settimeout(timeout)
|
2013-09-06 03:24:33 +00:00
|
|
|
|
|
|
|
def get_sock_fd(self):
|
|
|
|
return self.__sock.fileno()
|
|
|
|
|
|
|
|
def is_scm_available(self):
|
|
|
|
return self.__sock.family == socket.AF_UNIX
|