Create wrapper functions for each nwfilter C API that accepts a
virErrorPtr parameter. This avoids accessing a thread local from a
goroutine which may race with other goroutines doing native API calls in
the same OS thread.
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
nwfilter.go | 35 ++++++++++--------
nwfilter_wrapper.go | 88 +++++++++++++++++++++++++++++++++++++++++++++
nwfilter_wrapper.h | 33 +++++++++++++++++
3 files changed, 142 insertions(+), 14 deletions(-)
diff --git a/nwfilter.go b/nwfilter.go
index 441fcca..0b55c41 100644
--- a/nwfilter.go
+++ b/nwfilter.go
@@ -43,36 +43,40 @@ type NWFilter struct {
// See also
https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterFree
func (f *NWFilter) Free() error {
- ret := C.virNWFilterFree(f.ptr)
+ var err C.virError
+ ret := C.virNWFilterFreeWrapper(f.ptr, &err)
if ret == -1 {
- return GetLastError()
+ return makeError(&err)
}
return nil
}
// See also
https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterRef
func (c *NWFilter) Ref() error {
- ret := C.virNWFilterRef(c.ptr)
+ var err C.virError
+ ret := C.virNWFilterRefWrapper(c.ptr, &err)
if ret == -1 {
- return GetLastError()
+ return makeError(&err)
}
return nil
}
// See also
https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterGetName
func (f *NWFilter) GetName() (string, error) {
- name := C.virNWFilterGetName(f.ptr)
+ var err C.virError
+ name := C.virNWFilterGetNameWrapper(f.ptr, &err)
if name == nil {
- return "", GetLastError()
+ return "", makeError(&err)
}
return C.GoString(name), nil
}
// See also
https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterUndefine
func (f *NWFilter) Undefine() error {
- result := C.virNWFilterUndefine(f.ptr)
+ var err C.virError
+ result := C.virNWFilterUndefineWrapper(f.ptr, &err)
if result == -1 {
- return GetLastError()
+ return makeError(&err)
}
return nil
}
@@ -81,9 +85,10 @@ func (f *NWFilter) Undefine() error {
func (f *NWFilter) GetUUID() ([]byte, error) {
var cUuid [C.VIR_UUID_BUFLEN](byte)
cuidPtr := unsafe.Pointer(&cUuid)
- result := C.virNWFilterGetUUID(f.ptr, (*C.uchar)(cuidPtr))
+ var err C.virError
+ result := C.virNWFilterGetUUIDWrapper(f.ptr, (*C.uchar)(cuidPtr), &err)
if result != 0 {
- return []byte{}, GetLastError()
+ return []byte{}, makeError(&err)
}
return C.GoBytes(cuidPtr, C.VIR_UUID_BUFLEN), nil
}
@@ -92,18 +97,20 @@ func (f *NWFilter) GetUUID() ([]byte, error) {
func (f *NWFilter) GetUUIDString() (string, error) {
var cUuid [C.VIR_UUID_STRING_BUFLEN](C.char)
cuidPtr := unsafe.Pointer(&cUuid)
- result := C.virNWFilterGetUUIDString(f.ptr, (*C.char)(cuidPtr))
+ var err C.virError
+ result := C.virNWFilterGetUUIDStringWrapper(f.ptr, (*C.char)(cuidPtr), &err)
if result != 0 {
- return "", GetLastError()
+ return "", makeError(&err)
}
return C.GoString((*C.char)(cuidPtr)), nil
}
// See also
https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterGetXMLDesc
func (f *NWFilter) GetXMLDesc(flags uint32) (string, error) {
- result := C.virNWFilterGetXMLDesc(f.ptr, C.uint(flags))
+ var err C.virError
+ result := C.virNWFilterGetXMLDescWrapper(f.ptr, C.uint(flags), &err)
if result == nil {
- return "", GetLastError()
+ return "", makeError(&err)
}
xml := C.GoString(result)
C.free(unsafe.Pointer(result))
diff --git a/nwfilter_wrapper.go b/nwfilter_wrapper.go
index 78d0487..809e527 100644
--- a/nwfilter_wrapper.go
+++ b/nwfilter_wrapper.go
@@ -30,5 +30,93 @@ package libvirt
#include <assert.h>
#include "nwfilter_wrapper.h"
+
+int
+virNWFilterFreeWrapper(virNWFilterPtr nwfilter,
+ virErrorPtr err)
+{
+ int ret = virNWFilterFree(nwfilter);
+ if (ret < 0) {
+ virCopyLastError(err);
+ }
+ return ret;
+}
+
+
+const char *
+virNWFilterGetNameWrapper(virNWFilterPtr nwfilter,
+ virErrorPtr err)
+{
+ const char * ret = virNWFilterGetName(nwfilter);
+ if (!ret) {
+ virCopyLastError(err);
+ }
+ return ret;
+}
+
+
+int
+virNWFilterGetUUIDWrapper(virNWFilterPtr nwfilter,
+ unsigned char *uuid,
+ virErrorPtr err)
+{
+ int ret = virNWFilterGetUUID(nwfilter, uuid);
+ if (ret < 0) {
+ virCopyLastError(err);
+ }
+ return ret;
+}
+
+
+int
+virNWFilterGetUUIDStringWrapper(virNWFilterPtr nwfilter,
+ char *buf,
+ virErrorPtr err)
+{
+ int ret = virNWFilterGetUUIDString(nwfilter, buf);
+ if (ret < 0) {
+ virCopyLastError(err);
+ }
+ return ret;
+}
+
+
+char *
+virNWFilterGetXMLDescWrapper(virNWFilterPtr nwfilter,
+ unsigned int flags,
+ virErrorPtr err)
+{
+ char * ret = virNWFilterGetXMLDesc(nwfilter, flags);
+ if (!ret) {
+ virCopyLastError(err);
+ }
+ return ret;
+}
+
+
+int
+virNWFilterRefWrapper(virNWFilterPtr nwfilter,
+ virErrorPtr err)
+{
+ int ret = virNWFilterRef(nwfilter);
+ if (ret < 0) {
+ virCopyLastError(err);
+ }
+ return ret;
+}
+
+
+int
+virNWFilterUndefineWrapper(virNWFilterPtr nwfilter,
+ virErrorPtr err)
+{
+ int ret = virNWFilterUndefine(nwfilter);
+ if (ret < 0) {
+ virCopyLastError(err);
+ }
+ return ret;
+}
+
+
*/
import "C"
diff --git a/nwfilter_wrapper.h b/nwfilter_wrapper.h
index c470366..9a0651c 100644
--- a/nwfilter_wrapper.h
+++ b/nwfilter_wrapper.h
@@ -29,4 +29,37 @@
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
+
+int
+virNWFilterFreeWrapper(virNWFilterPtr nwfilter,
+ virErrorPtr err);
+
+const char *
+virNWFilterGetNameWrapper(virNWFilterPtr nwfilter,
+ virErrorPtr err);
+
+int
+virNWFilterGetUUIDWrapper(virNWFilterPtr nwfilter,
+ unsigned char *uuid,
+ virErrorPtr err);
+
+int
+virNWFilterGetUUIDStringWrapper(virNWFilterPtr nwfilter,
+ char *buf,
+ virErrorPtr err);
+
+char *
+virNWFilterGetXMLDescWrapper(virNWFilterPtr nwfilter,
+ unsigned int flags,
+ virErrorPtr err);
+
+int
+virNWFilterRefWrapper(virNWFilterPtr nwfilter,
+ virErrorPtr err);
+
+int
+virNWFilterUndefineWrapper(virNWFilterPtr nwfilter,
+ virErrorPtr err);
+
+
#endif /* LIBVIRT_GO_NWFILTER_WRAPPER_H__ */
--
2.17.1