[libvirt] [libvirt-java] [PATCH] Avoid calling processError for functions that cannot fail
by Claudio Bley
The libvirt functions virNodeDeviceNumOfCaps, virNodeDeviceGetParent
and virNodeDeviceListCaps never indicate an error because they do not
fail.
---
src/main/java/org/libvirt/Device.java | 19 ++++++-------------
src/main/java/org/libvirt/jna/Libvirt.java | 2 +-
2 files changed, 7 insertions(+), 14 deletions(-)
diff --git a/src/main/java/org/libvirt/Device.java b/src/main/java/org/libvirt/Device.java
index fe49ce9..5492b69 100644
--- a/src/main/java/org/libvirt/Device.java
+++ b/src/main/java/org/libvirt/Device.java
@@ -98,23 +98,19 @@ public class Device {
/**
* Returns the number of capabilities which the instance has.
*
- * @throws LibvirtException
+ * @throws LibvirtException <em>never</em>
*/
public int getNumberOfCapabilities() throws LibvirtException {
- int num = libvirt.virNodeDeviceNumOfCaps(VDP);
- processError();
- return num;
+ return libvirt.virNodeDeviceNumOfCaps(VDP);
}
/**
* Returns the parent of the device
*
- * @throws LibvirtException
+ * @throws LibvirtException <em>never</em>
*/
public String getParent() throws LibvirtException {
- String parent = libvirt.virNodeDeviceGetParent(VDP);
- processError();
- return parent;
+ return libvirt.virNodeDeviceGetParent(VDP);
}
/**
@@ -123,15 +119,13 @@ public class Device {
* @throws LibvirtException
*/
public String getXMLDescription() throws LibvirtException {
- String desc = libvirt.virNodeDeviceGetXMLDesc(VDP);
- processError();
- return desc;
+ return processError(libvirt.virNodeDeviceGetXMLDesc(VDP));
}
/**
* List the capabilities of the device
*
- * @throws LibvirtException
+ * @throws LibvirtException <em>never</em>
*/
public String[] listCapabilities() throws LibvirtException {
int maxCaps = getNumberOfCapabilities();
@@ -139,7 +133,6 @@ public class Device {
if (maxCaps > 0) {
libvirt.virNodeDeviceListCaps(VDP, names, maxCaps);
- processError();
}
return names;
}
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index b026b04..a2bf42e 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -281,7 +281,7 @@ public interface Libvirt extends Library {
String virNodeDeviceGetName(DevicePointer virDevicePointer);
String virNodeDeviceGetParent(DevicePointer virDevicePointer);
int virNodeDeviceNumOfCaps(DevicePointer virDevicePointer);
- int virNodeDeviceListCaps(DevicePointer virDevicePointer, String[] names, int maxNames);
+ int virNodeDeviceListCaps(DevicePointer virDevicePointer, Pointer[] names, int maxNames);
String virNodeDeviceGetXMLDesc(DevicePointer virDevicePointer);
int virNodeDeviceFree(DevicePointer virDevicePointer);
int virNodeDeviceDettach(DevicePointer virDevicePointer);
--
1.8.5.2.msysgit.0
10 years, 10 months
[libvirt] [libvirt-java] [PATCH] Use virFree in order to release memory acquired from libvirt
by Claudio Bley
When JNA is linked to a different runtime library than libvirt, using
JNA's Native.free will probably lead to crashes as witnessed on
Windows:
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000077363290, pid=10180, tid=9908
#
# JRE version: 7.0_25-b16
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.25-b01 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [ntdll.dll+0x53290] RtlFreeHeap+0xd0
The root cause is that the libvirt DLL uses MSVCRT as its runtime
library, whereas the jnidispatch DLL of JNA uses a different one.
At runtime, when calling org.sun.com.jna.Native.free() the OS function
RtlFreeHeap is called with an invalid Pointer that was actually
allocated by MSVCRT's malloc.
Basically, we cannot simply mix and match memory allocation functions
from different runtime libraries, but have to call virFree for
pointers orignating from libvirt itself.
So, this patch re-adds and uses the virFree method which had been
removed in commit 3220de292990bed71828fba2f3700bc846d440f2.
Signed-off-by: Claudio Bley <cbley(a)av-test.de>
---
src/main/java/org/libvirt/Library.java | 4 ++--
src/main/java/org/libvirt/jna/Libvirt.java | 2 ++
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/libvirt/Library.java b/src/main/java/org/libvirt/Library.java
index 0136095..33d3042 100644
--- a/src/main/java/org/libvirt/Library.java
+++ b/src/main/java/org/libvirt/Library.java
@@ -4,6 +4,7 @@ import org.libvirt.jna.Libvirt;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
+import com.sun.jna.ptr.PointerByReference;
/**
* This class represents an instance of the JNA mapped libvirt
@@ -38,8 +39,7 @@ final class Library {
* Free memory pointed to by ptr.
*/
static void free(Pointer ptr) {
- Native.free(Pointer.nativeValue(ptr));
- Pointer.nativeValue(ptr, 0L);
+ libvirt.virFree(new PointerByReference(ptr));
}
/**
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index 813f09b..b026b04 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -7,6 +7,7 @@ import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
+import com.sun.jna.ptr.PointerByReference;
/**
* The libvirt interface which is exposed via JNA. The complete API is
@@ -171,6 +172,7 @@ public interface Libvirt extends Library {
int virGetVersion(LongByReference libVer, String type, LongByReference typeVer);
int virInitialize();
int virCopyLastError(virError error);
+ void virFree(PointerByReference ptr);
virError virGetLastError();
void virResetLastError();
void virSetErrorFunc(Pointer userData, VirErrorCallback callback);
--
1.8.5.2.msysgit.0
10 years, 10 months
[libvirt] [PATCH] util: Use new array management macros
by Osier Yang
Like commit 94a26c7e from Eric Blake, the old fuzzy code should
be replaced by the new array management macros now.
And the type of scsi->count should be changed into "size_t", and
thus virSCSIDeviceListCount should return size_t instead, similar
for vir{PCI,USB}DeviceListCount.
---
src/util/virpci.c | 2 +-
src/util/virpci.h | 2 +-
src/util/virscsi.c | 35 ++++++++++-------------------------
src/util/virscsi.h | 2 +-
src/util/virusb.c | 2 +-
src/util/virusb.h | 2 +-
6 files changed, 15 insertions(+), 30 deletions(-)
diff --git a/src/util/virpci.c b/src/util/virpci.c
index 8ec642f..ea93771 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -1721,7 +1721,7 @@ virPCIDeviceListGet(virPCIDeviceListPtr list,
return list->devs[idx];
}
-int
+size_t
virPCIDeviceListCount(virPCIDeviceListPtr list)
{
return list->count;
diff --git a/src/util/virpci.h b/src/util/virpci.h
index 0479f0b..08bf4c3 100644
--- a/src/util/virpci.h
+++ b/src/util/virpci.h
@@ -86,7 +86,7 @@ int virPCIDeviceListAdd(virPCIDeviceListPtr list,
int virPCIDeviceListAddCopy(virPCIDeviceListPtr list, virPCIDevicePtr dev);
virPCIDevicePtr virPCIDeviceListGet(virPCIDeviceListPtr list,
int idx);
-int virPCIDeviceListCount(virPCIDeviceListPtr list);
+size_t virPCIDeviceListCount(virPCIDeviceListPtr list);
virPCIDevicePtr virPCIDeviceListSteal(virPCIDeviceListPtr list,
virPCIDevicePtr dev);
virPCIDevicePtr virPCIDeviceListStealIndex(virPCIDeviceListPtr list,
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
index 7aca9e6..f12a2a5 100644
--- a/src/util/virscsi.c
+++ b/src/util/virscsi.c
@@ -62,7 +62,7 @@ struct _virSCSIDevice {
struct _virSCSIDeviceList {
virObjectLockable parent;
- unsigned int count;
+ size_t count;
virSCSIDevicePtr *devs;
};
@@ -356,12 +356,7 @@ virSCSIDeviceListAdd(virSCSIDeviceListPtr list,
return -1;
}
- if (VIR_REALLOC_N(list->devs, list->count + 1) < 0)
- return -1;
-
- list->devs[list->count++] = dev;
-
- return 0;
+ return VIR_APPEND_ELEMENT(list->devs, list->count, dev);
}
virSCSIDevicePtr
@@ -373,7 +368,7 @@ virSCSIDeviceListGet(virSCSIDeviceListPtr list, int idx)
return list->devs[idx];
}
-int
+size_t
virSCSIDeviceListCount(virSCSIDeviceListPtr list)
{
return list->count;
@@ -387,24 +382,14 @@ virSCSIDeviceListSteal(virSCSIDeviceListPtr list,
size_t i;
for (i = 0; i < list->count; i++) {
- if (list->devs[i]->adapter != dev->adapter ||
- list->devs[i]->bus != dev->bus ||
- list->devs[i]->target != dev->target ||
- list->devs[i]->unit != dev->unit)
- continue;
-
- ret = list->devs[i];
-
- if (i != list->count--)
- memmove(&list->devs[i],
- &list->devs[i+1],
- sizeof(*list->devs) * (list->count - i));
-
- if (VIR_REALLOC_N(list->devs, list->count) < 0) {
- ; /* not fatal */
+ if (list->devs[i]->adapter == dev->adapter ||
+ list->devs[i]->bus == dev->bus ||
+ list->devs[i]->target == dev->target ||
+ list->devs[i]->unit == dev->unit) {
+ ret = list->devs[i];
+ VIR_DELETE_ELEMENT(list->devs, i, list->count);
+ break;
}
-
- break;
}
return ret;
diff --git a/src/util/virscsi.h b/src/util/virscsi.h
index cce5df4..4c461f8 100644
--- a/src/util/virscsi.h
+++ b/src/util/virscsi.h
@@ -77,7 +77,7 @@ int virSCSIDeviceListAdd(virSCSIDeviceListPtr list,
virSCSIDevicePtr dev);
virSCSIDevicePtr virSCSIDeviceListGet(virSCSIDeviceListPtr list,
int idx);
-int virSCSIDeviceListCount(virSCSIDeviceListPtr list);
+size_t virSCSIDeviceListCount(virSCSIDeviceListPtr list);
virSCSIDevicePtr virSCSIDeviceListSteal(virSCSIDeviceListPtr list,
virSCSIDevicePtr dev);
void virSCSIDeviceListDel(virSCSIDeviceListPtr list,
diff --git a/src/util/virusb.c b/src/util/virusb.c
index 3c82200..bb5980d 100644
--- a/src/util/virusb.c
+++ b/src/util/virusb.c
@@ -464,7 +464,7 @@ virUSBDeviceListGet(virUSBDeviceListPtr list,
return list->devs[idx];
}
-int
+size_t
virUSBDeviceListCount(virUSBDeviceListPtr list)
{
return list->count;
diff --git a/src/util/virusb.h b/src/util/virusb.h
index aa59d12..e0a7c4c 100644
--- a/src/util/virusb.h
+++ b/src/util/virusb.h
@@ -86,7 +86,7 @@ int virUSBDeviceListAdd(virUSBDeviceListPtr list,
virUSBDevicePtr dev);
virUSBDevicePtr virUSBDeviceListGet(virUSBDeviceListPtr list,
int idx);
-int virUSBDeviceListCount(virUSBDeviceListPtr list);
+size_t virUSBDeviceListCount(virUSBDeviceListPtr list);
virUSBDevicePtr virUSBDeviceListSteal(virUSBDeviceListPtr list,
virUSBDevicePtr dev);
void virUSBDeviceListDel(virUSBDeviceListPtr list,
--
1.8.1.4
10 years, 10 months
[libvirt] [libvirt-java] [PATCH] Fix wrapping of virStreamSend
by Claudio Bley
The native virStreamSend function is meant to be used to transfer
byte data.
Wrapping the function to use a Java String as the data depends on
the default charset of the JVM and might impose further problems
transferring \0 characters.
Use a byte Array as the parameter type instead, as for virStreamRecv.
Add the Stream.receive(byte[]) method which should be used instead
of the legacy Stream.receive(String) method. The old method is kept
for backward compatibility, but marked as deprecated.
---
src/main/java/org/libvirt/Stream.java | 27 +++++++++++++++++++++++++--
src/main/java/org/libvirt/jna/Libvirt.java | 2 +-
2 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/src/main/java/org/libvirt/Stream.java b/src/main/java/org/libvirt/Stream.java
index bd8e87f..404c9a0 100644
--- a/src/main/java/org/libvirt/Stream.java
+++ b/src/main/java/org/libvirt/Stream.java
@@ -142,6 +142,27 @@ public class Stream {
}
/**
+ * Write the data of the given string to a stream.
+ *
+ * @param data
+ * the string data to write
+ * @return the number of bytes written, -1 on error, -2 if the buffer is
+ * full
+ * @throws LibvirtException
+ *
+ * @deprecated This libvirt API was previously wrapped incorrectly, since
+ * the native function is meant to transfer bytes, not string
+ * data.<p>
+ * Its effect depends on the default Charset on your platform
+ * which is determined on JVM startup.<p>
+ * Use the {@link #send(byte[])} method instead.
+ */
+ @Deprecated
+ public int send(String data) throws LibvirtException {
+ return send(data.getBytes(java.nio.charset.Charset.defaultCharset()));
+ }
+
+ /**
* Write a series of bytes to the stream.
*
* @param data
@@ -149,9 +170,11 @@ public class Stream {
* @return the number of bytes written, -1 on error, -2 if the buffer is
* full
* @throws LibvirtException
+ *
+ * @since 1.5.2
*/
- public int send(String data) throws LibvirtException {
- int returnValue = libvirt.virStreamSend(VSP, data, new NativeLong(data.length()));
+ public int send(byte[] data) throws LibvirtException {
+ int returnValue = libvirt.virStreamSend(VSP, data, new NativeLong(data.length));
processError();
return returnValue;
}
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index 813f09b..98f2125 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -368,7 +368,7 @@ public interface Libvirt extends Library {
int virStreamFinish(StreamPointer virStreamPtr) ;
int virStreamFree(StreamPointer virStreamPtr) ;
StreamPointer virStreamNew(ConnectionPointer virConnectPtr, int flags) ;
- int virStreamSend(StreamPointer virStreamPtr, String data, NativeLong size);
+ int virStreamSend(StreamPointer virStreamPtr, byte[] data, NativeLong size);
int virStreamSendAll(StreamPointer virStreamPtr, Libvirt.VirStreamSourceFunc handler, Pointer opaque);
int virStreamRecv(StreamPointer virStreamPtr, byte[] data, NativeLong length);
int virStreamRecvAll(StreamPointer virStreamPtr, Libvirt.VirStreamSinkFunc handler, Pointer opaque);
--
1.8.5.2.msysgit.0
10 years, 10 months
[libvirt] [libvirt-java] [PATCH] Fix javadoc comments
by Claudio Bley
Correct some typos and paste the right documentation from
virInterfaceCreate, virInterfaceDestroy to the Interface.create
and Interface.destroy doc comments.
---
Pushing under the trivial rule.
src/main/java/org/libvirt/Domain.java | 2 +-
.../java/org/libvirt/DomainInterfaceStats.java | 2 +-
src/main/java/org/libvirt/DomainSnapshot.java | 2 +-
src/main/java/org/libvirt/Interface.java | 22 ++++++++++++++++++----
src/main/java/org/libvirt/Stream.java | 4 ++--
5 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/src/main/java/org/libvirt/Domain.java b/src/main/java/org/libvirt/Domain.java
index e4c45f6..2f70bf2 100644
--- a/src/main/java/org/libvirt/Domain.java
+++ b/src/main/java/org/libvirt/Domain.java
@@ -370,7 +370,7 @@ public class Domain {
}
/**
- * Provides a boolean value indicating whether the network is configured to
+ * Provides a boolean value indicating whether the domain is configured to
* be automatically started when the host machine boots.
*
* @return the result
diff --git a/src/main/java/org/libvirt/DomainInterfaceStats.java b/src/main/java/org/libvirt/DomainInterfaceStats.java
index 0e948d5..eab5c44 100644
--- a/src/main/java/org/libvirt/DomainInterfaceStats.java
+++ b/src/main/java/org/libvirt/DomainInterfaceStats.java
@@ -3,7 +3,7 @@ package org.libvirt;
import org.libvirt.jna.virDomainInterfaceStats;
/**
- * The Domain.interfaceStats method returns th network counters in this object
+ * The Domain.interfaceStats method returns the network counters in this object.
*
* @author stoty
*
diff --git a/src/main/java/org/libvirt/DomainSnapshot.java b/src/main/java/org/libvirt/DomainSnapshot.java
index b58fd20..9409fef 100644
--- a/src/main/java/org/libvirt/DomainSnapshot.java
+++ b/src/main/java/org/libvirt/DomainSnapshot.java
@@ -27,7 +27,7 @@ public class DomainSnapshot {
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainSnapshotDelete">Libvirt
* Documentation</a>
* @param flags
- * controls teh deletion
+ * controls the deletion
* @return 0 if the selected snapshot(s) were successfully deleted, -1 on error.
* @throws LibvirtException
*/
diff --git a/src/main/java/org/libvirt/Interface.java b/src/main/java/org/libvirt/Interface.java
index 7782fb9..6ea21b6 100644
--- a/src/main/java/org/libvirt/Interface.java
+++ b/src/main/java/org/libvirt/Interface.java
@@ -40,8 +40,13 @@ public class Interface {
}
/**
- * Create and start a defined network. If the call succeed the network moves
- * from the defined to the running networks pools.
+ * Activate an interface (i.e. call "ifup").
+ * <p>
+ * If there was an open network config transaction at the time
+ * this interface was defined (that is, if
+ * virInterfaceChangeBegin() had been called), the interface will
+ * be brought back down (and then undefined) if
+ * virInterfaceChangeRollback() is called.
*
* @throws LibvirtException
*/
@@ -52,8 +57,17 @@ public class Interface {
}
/**
- * Destroy the network object. The running instance is shutdown if not down
- * already and all resources used by it are given back to the hypervisor.
+ * Deactivate an interface (i.e. call "ifdown").
+ * <p>
+ * This does not remove the interface from the config, and does
+ * not free the associated virInterfacePtr object.
+ * <p>
+ * If there is an open network config transaction at the time this
+ * interface is destroyed (that is, if virInterfaceChangeBegin()
+ * had been called), and if the interface is later undefined and
+ * then virInterfaceChangeRollback() is called, the restoral of
+ * the interface definition will also bring the interface back
+ * up.
*
* @throws LibvirtException
*/
diff --git a/src/main/java/org/libvirt/Stream.java b/src/main/java/org/libvirt/Stream.java
index 84e300c..bd8e87f 100644
--- a/src/main/java/org/libvirt/Stream.java
+++ b/src/main/java/org/libvirt/Stream.java
@@ -100,10 +100,10 @@ public class Stream {
}
/**
- * Receieves data from teh stream into the buffer provided.
+ * Receives data from the stream into the buffer provided.
*
* @param data
- * the put the sata into
+ * buffer to put the data into
* @return the number of bytes read, -1 on error, -2 if the buffer is empty
* @throws LibvirtException
*/
--
1.8.5.2.msysgit.0
10 years, 10 months
[libvirt] [libvirt-java] [PATCH] Fix javadoc warning about snapshotCreateXML(int)
by Claudio Bley
Javadoc complains about:
[javadoc] x:\src\libvirt-java1\src\main\java\org\libvirt\Domain.java:1212:
warning - Tag @see: can't find snapshotCreateXML(int) in org.libvirt.Domain
There is no such method, snapshotCreateXML(String, int) was intended.
---
Pushing under the trivial rule.
src/main/java/org/libvirt/Domain.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main/java/org/libvirt/Domain.java b/src/main/java/org/libvirt/Domain.java
index ee26a95..e4c45f6 100644
--- a/src/main/java/org/libvirt/Domain.java
+++ b/src/main/java/org/libvirt/Domain.java
@@ -1189,7 +1189,7 @@ public class Domain {
* This is just a convenience method, it has the same effect
* as calling {@code snapshotCreateXML(xmlDesc, 0);}.
*
- * @see #snapshotCreateXML(int)
+ * @see #snapshotCreateXML(String, int)
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainSnapshotCreateXML">Libvirt
* Documentation</a>
--
1.8.5.2.msysgit.0
10 years, 10 months
[libvirt] [PATCH] conf: trivial typo fix
by Martin Kletzander
Signed-off-by: Martin Kletzander <mkletzan(a)redhat.com>
---
Notes:
Pushed as trivial.
src/conf/domain_conf.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index e65f3e3..416d96e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1,7 +1,7 @@
/*
* domain_conf.c: domain XML processing
*
- * Copyright (C) 2006-2013 Red Hat, Inc.
+ * Copyright (C) 2006-2014 Red Hat, Inc.
* Copyright (C) 2006-2008 Daniel P. Berrange
*
* This library is free software; you can redistribute it and/or
@@ -2692,7 +2692,7 @@ virDomainDeviceInfoIterateInternal(virDomainDefPtr def,
/* This switch statement is here to trigger compiler warning when adding
* a new device type. When you are adding a new field to the switch you
- * also have to add a iteration statement above. Otherwise the switch
+ * also have to add an iteration statement above. Otherwise the switch
* statement has no real function here and should be optimized out by the
* compiler. */
i = VIR_DOMAIN_DEVICE_LAST;
--
1.8.5.2
10 years, 10 months
[libvirt] Redefining the XML of VM
by kangta123
Hi,
I'm a newbie to Libvirt, i want to know if i can change the vm definition xml dynamically. like change cpu, network value.
I found the hook in Libvirt, and i can get the xml from standard input. but i change it and out put to standard output, it no effect.
I was try to execute “virsh define” shell command in python script on hook, but it was blocked and unabled to execute another Libvirt command.
Is there create vm hook in Libvirt?
Any help is appreciated.
Regards,
10 years, 10 months
[libvirt] Redefining the XML of VM
by kangta123
Hi,
I'm a newbie to Libvirt, i want to know if i can change the vm definition xml dynamically. like change cpu, network value.
I found the hook in Libvirt, and i can get the xml from standard input. but i change it and out put to standard output, it no effect.
I was try to execute “virsh define” shell command in python script on hook, but it was blocked and unabled to execute another Libvirt command.
Is there create vm hook in Libvirt?
Any help is appreciated.
Regards,
10 years, 10 months