[Libvir] [PATCH] check file format in virsh attach/detach-device

Hi virsh attach/detach-devich does not check file format now. This patch checks config file format is XML or not. (in virsh attach/detach-device) If file format is not XML, just return the error with message. Signed-off-by: Masayuki Sunou <fj1826dm@aa.jp.fujitsu.com> Atsushi SAKAI <sakaia@jp.fujitsu.com> Thanks, Masayuki Sunou.

On Tue, Jul 31, 2007 at 06:46:19PM +0900, Masayuki Sunou wrote:
Hi
virsh attach/detach-devich does not check file format now.
This patch checks config file format is XML or not. (in virsh attach/detach-device) If file format is not XML, just return the error with message.
Signed-off-by: Masayuki Sunou <fj1826dm@aa.jp.fujitsu.com> Atsushi SAKAI <sakaia@jp.fujitsu.com>
Sorry I will have to reject this ! You are breaking the layering, and the API. http://libvirt.org/html/libvirt-libvirt.html#virDomainAttachDevice Returns: 0 in case of success, -1 in case of failure. your patch makes it return -2 you are breaking the API, this is not acceptable. An error should be described as precisely as possible when detected. This should not be handled in an application specific way, so to me this is wrong in 2 ways. The routine inside the Xen driver should indicate *why* it could not load the file, and be as precise as possible, virParseXMLDevice should be refined to provide a better error report instead. We already have a specific error code for this VIR_ERR_XML_ERROR, just use it. Daniel -- Red Hat Virtualization group http://redhat.com/virtualization/ Daniel Veillard | virtualization library http://libvirt.org/ veillard@redhat.com | libxml GNOME XML XSLT toolkit http://xmlsoft.org/ http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/

Masayuki Sunou wrote:
Hi
virsh attach/detach-devich does not check file format now.
This patch checks config file format is XML or not. (in virsh attach/detach-device) If file format is not XML, just return the error with message.
Hello Masayuki, To summarise what your patch does: (1) It changes the semantics of virDomainAttachDevice and virDomainDetachDevice so that returning -2 as an error (instead of -1) means "the XML is invalid". (2) It changes the implementation of the xenDaemon{Attach,Detach}Device functions in xend_internal.c so that it returns -2 instead of -1 if virParseXMLDevice fails. My problems are: (1) is an ABI change (and undocumented!). In particular some of my own code depends on -1 meaning error (not just < 0). Really we can't accept patches which make ABI changes like this. You'll have seen a patch which I submitted last week get rejected because it made an egregious ABI change. (2) isn't quite correct. The function virParseXMLDevice can fail for many reasons (eg. out of memory). So I think a better solution would look like either: (a) Make virsh parse the XML file and do some simple sanity checks on it. I realise that virsh cannot do a full check on the XML, but it can at least check things like: Is it an XML file? Is the root node <disk> or <interface>? That should be enough to catch simple command-line errors. or: (b) Fix the error handling in virParseXMLDevice so that it generates reasonable errors. At the moment the error handling is mostly missing, and so error don't get propagated back to the user. or: (c) Modify libvirt to add a virDomainVerifyDeviceXML call which returns 0 (correct) or -1 (error) depending on whether the Device XML passed is reasonable. Then virsh can do this call before it does the attach/detach call. I hope that is helpful. Rich. -- Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/ Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 03798903

Hi Daniel, Rich Thanks for reviewing and suggestion. I adopt (b) in consideration Daniel's suggestion. --> Error message exists already. This patch fixes virParseXMLDevice() for attach-device and virDomainXMLDevID() for detach-device. Thanks, Masayuki Sunou. ---------------------------------------------------------------------- Index: src/xml.c =================================================================== RCS file: /data/cvs/libvirt/src/xml.c,v retrieving revision 1.86 diff -u -p -r1.86 xml.c --- src/xml.c 31 Jul 2007 14:27:12 -0000 1.86 +++ src/xml.c 2 Aug 2007 05:41:21 -0000 @@ -1400,8 +1400,10 @@ virParseXMLDevice(virConnectPtr conn, ch xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL, XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOERROR | XML_PARSE_NOWARNING); - if (xml == NULL) + if (xml == NULL) { + virXMLError(conn, VIR_ERR_XML_ERROR, NULL, 0); goto error; + } node = xmlDocGetRootElement(xml); if (node == NULL) goto error; @@ -1457,8 +1459,10 @@ virDomainXMLDevID(virDomainPtr domain, c xml = xmlReadDoc((const xmlChar *) xmldesc, "domain.xml", NULL, XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOERROR | XML_PARSE_NOWARNING); - if (xml == NULL) + if (xml == NULL) { + virXMLError(NULL, VIR_ERR_XML_ERROR, NULL, 0); goto error; + } node = xmlDocGetRootElement(xml); if (node == NULL) goto error; @@ -1499,6 +1503,9 @@ virDomainXMLDevID(virDomainPtr domain, c goto error; } + } else { + virXMLError(NULL, VIR_ERR_XML_ERROR, (const char *) node->name, 0); + goto error; } error: ret = -1; ---------------------------------------------------------------------- In message <46AF1E04.2030703@redhat.com> "Re: [Libvir] [PATCH] check file format in virsh attach/detach-device" ""Richard W.M. Jones" <rjones@redhat.com>" wrote:
Masayuki Sunou wrote:
Hi
virsh attach/detach-devich does not check file format now.
This patch checks config file format is XML or not. (in virsh attach/detach-device) If file format is not XML, just return the error with message.
Hello Masayuki,
To summarise what your patch does:
(1) It changes the semantics of virDomainAttachDevice and virDomainDetachDevice so that returning -2 as an error (instead of -1) means "the XML is invalid".
(2) It changes the implementation of the xenDaemon{Attach,Detach}Device functions in xend_internal.c so that it returns -2 instead of -1 if virParseXMLDevice fails.
My problems are:
(1) is an ABI change (and undocumented!). In particular some of my own code depends on -1 meaning error (not just < 0). Really we can't accept patches which make ABI changes like this. You'll have seen a patch which I submitted last week get rejected because it made an egregious ABI change.
(2) isn't quite correct. The function virParseXMLDevice can fail for many reasons (eg. out of memory).
So I think a better solution would look like either:
(a) Make virsh parse the XML file and do some simple sanity checks on it. I realise that virsh cannot do a full check on the XML, but it can at least check things like: Is it an XML file? Is the root node <disk> or <interface>? That should be enough to catch simple command-line errors.
or:
(b) Fix the error handling in virParseXMLDevice so that it generates reasonable errors. At the moment the error handling is mostly missing, and so error don't get propagated back to the user.
or:
(c) Modify libvirt to add a virDomainVerifyDeviceXML call which returns 0 (correct) or -1 (error) depending on whether the Device XML passed is reasonable. Then virsh can do this call before it does the attach/detach call.
I hope that is helpful.
Rich.
-- Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/ Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 03798903

On Thu, Aug 02, 2007 at 03:01:07PM +0900, Masayuki Sunou wrote:
Hi Daniel, Rich
Thanks for reviewing and suggestion.
I adopt (b) in consideration Daniel's suggestion. --> Error message exists already.
This patch fixes virParseXMLDevice() for attach-device and virDomainXMLDevID() for detach-device.
Looks fine to me, I applied it after just a couple of cosmetic changes. thanks a lot, it's in CVS ! Daniel -- Red Hat Virtualization group http://redhat.com/virtualization/ Daniel Veillard | virtualization library http://libvirt.org/ veillard@redhat.com | libxml GNOME XML XSLT toolkit http://xmlsoft.org/ http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/
participants (3)
-
Daniel Veillard
-
Masayuki Sunou
-
Richard W.M. Jones