[libvirt] [libvirt-php 0/3] small improvements
by Vasiliy Tolstov
Add some more libvirt functions support to binding.
Also add some checks to prevent malloc SIZE_MAX memory
in case of errors
Vasiliy Tolstov (3):
add some checks to prevent overflow
add block_commit support and needed const
add libvirt_domain_block_job_info
src/libvirt-php.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++------
src/libvirt-php.h | 2 +
2 files changed, 138 insertions(+), 16 deletions(-)
--
2.7.0
8 years, 9 months
[libvirt] [PATCH 00/12] qemu_process cleanups and test improvement
by Pavel Hrdina
The first patch is unrelated but I've found it while working on this series.
Pavel Hrdina (12):
qemu_process: check for correct return value while starting domain
nvram: generate it's path in qemuDomainDefPostParse
tests: cleanup qemuxml2argvtest
qemu_process: introduce qemuProcessPrepareDomain
qemu_process: introduce qemuProcessPrepareHost
qemu: update callers of qemuProcessStartValidate to use
virDomainObjPtr
qemu_process: move checks to qemuProcessStartValidate
qemu_process: move qemuDomainSetPrivatePaths to qemuProcessInit
qemu_process: introduce qemuProcessCreateCmd
qemu_driver: cleanup qemuConnectDomainXMLToNative
qemuxml2argvtest: use driver.config and priv for
qemuDomainSetPrivatePaths
qemuxml2argvtest: cleanup test
src/qemu/qemu_domain.c | 10 +
src/qemu/qemu_driver.c | 61 +-
src/qemu/qemu_migration.c | 9 +-
src/qemu/qemu_process.c | 706 +++++++++++----------
src/qemu/qemu_process.h | 26 +-
.../qemuxml2argv-channel-virtio-unix.xml | 2 +-
tests/qemuxml2argvtest.c | 126 ++--
tests/testutilsqemu.c | 7 +
8 files changed, 494 insertions(+), 453 deletions(-)
--
2.7.2
8 years, 9 months
[libvirt] [PATCH 0/2] qemu: support SPICE+unix socket
by Cole Robinson
qemu has supported SPICE+unix socket listening since 2.3.0. Patch #1
wires it up from domain_conf.c down to qemu, patch #2 adds qemu.conf
spice_auto_unix_socket option
Full disclosure: this duplicates the logic of VNC socket usage, which has
some pre-existing warts:
- When the VM is running with graphics socket=FOO, the runtime XML
still lists <listen type='address' address='127.0.0.1'/>.
- If using vnc/spice auto socket, it looks like ports can still be
allocated in the code, though they are never used.
- In fact I suspect there's lots of issues with stray XML port/listen
being parsed and ending up in the device config, but selectively being
ignored later. We should probably explicitly ignore/error about those
combos when parsing the XML so we never have to ignore specific values
later on.
- The auto socket path is set in qemu_command.c but it should probably
be set in qemu_process.c with the port stuff. Though there are references
to vncListen in qemu_command.c which should probably be moved as well.
The issue with moving them to qemu_process.c is now there isn't any
pre-existing way to test those bits like via qemuxml2argvtest for
example. Though finding a way to test the graphics prep stuff would
be valuable anyways
- teuf suggests: converting <graphics socket=X/> to
<graphics><listen type='socket' path='FOO'/></graphics> like was done
with listen= address
- tuef: virsh domdisplay doesn't handle sockets
To avoid yet another rabbit hole I didn't touch any of the above issues,
which basically means spice+unix is no less operationally worse than
vnc+unix :)
Cole Robinson (2):
qemu: Support SPICE listen over unix socket
qemu: Add qemu.conf option spice_auto_unix_socket
docs/schemas/domaincommon.rng | 5 ++++
src/conf/domain_conf.c | 26 +++++++++++++--------
src/conf/domain_conf.h | 1 +
src/qemu/libvirtd_qemu.aug | 1 +
src/qemu/qemu.conf | 11 +++++++++
src/qemu/qemu_command.c | 15 +++++++++---
src/qemu/qemu_conf.c | 1 +
src/qemu/qemu_conf.h | 1 +
src/qemu/qemu_process.c | 3 +++
src/qemu/test_libvirtd_qemu.aug.in | 1 +
src/security/virt-aa-helper.c | 5 ++++
.../qemuxml2argv-graphics-spice-unix-auto.args | 21 +++++++++++++++++
.../qemuxml2argv-graphics-spice-unix-auto.xml | 27 ++++++++++++++++++++++
.../qemuxml2argv-graphics-spice-unix.args | 21 +++++++++++++++++
.../qemuxml2argv-graphics-spice-unix.xml | 27 ++++++++++++++++++++++
tests/qemuxml2argvtest.c | 10 ++++++++
16 files changed, 163 insertions(+), 13 deletions(-)
create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-graphics-spice-unix-auto.args
create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-graphics-spice-unix-auto.xml
create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-graphics-spice-unix.args
create mode 100644 tests/qemuxml2argvdata/qemuxml2argv-graphics-spice-unix.xml
--
2.5.0
8 years, 9 months
[libvirt] [PATCH v2] qemu-hotplug: fix eject media
by Pavel Hrdina
QEMU changed the error message to:
"Tray of device 'drive-sata0-0-1' is not open"
and they may change the error massage in the future.
This updates the code to not depend on the text from the error message
but only on error itself.
Signed-off-by: Pavel Hrdina <phrdina(a)redhat.com>
---
src/qemu/qemu_hotplug.c | 11 +++++------
src/qemu/qemu_monitor_json.c | 12 +-----------
src/qemu/qemu_monitor_text.c | 5 +----
3 files changed, 7 insertions(+), 21 deletions(-)
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index b580283..a78bc60 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -202,15 +202,14 @@ qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
if (qemuDomainObjExitMonitor(driver, vm) < 0)
goto cleanup;
- if (rc == -2) {
+ if (rc < 0) {
/* we've already tried, error out */
if (ejectRetry)
goto error;
- VIR_DEBUG("tray is locked, wait for the guest to unlock "
- "the tray and try to eject it again");
+
ejectRetry = true;
- } else if (rc < 0) {
- goto error;
+ VIR_DEBUG("tray may be locked, wait for the guest to unlock "
+ "the tray and try to eject it again");
}
if (virTimeMillisNow(&now) < 0)
@@ -220,7 +219,7 @@ qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
if (virDomainObjWaitUntil(vm, now + CHANGE_MEDIA_TIMEOUT) != 0)
goto error;
}
- } while (ejectRetry && rc != 0);
+ } while (rc < 0);
if (!virStorageSourceIsEmpty(newsrc)) {
if (qemuGetDriveSourceString(newsrc, conn, &sourcestr) < 0)
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index fe9288a..57c34f0 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -2272,8 +2272,7 @@ int qemuMonitorJSONSetCPU(qemuMonitorPtr mon,
* Run QMP command to eject a media from ejectable device.
*
* Returns:
- * -2 on error, when the tray is locked
- * -1 on all other errors
+ * -1 on error
* 0 on success
*/
int qemuMonitorJSONEjectMedia(qemuMonitorPtr mon,
@@ -2294,15 +2293,6 @@ int qemuMonitorJSONEjectMedia(qemuMonitorPtr mon,
if (ret == 0)
ret = qemuMonitorJSONCheckError(cmd, reply);
- if (ret < 0) {
- virJSONValuePtr error = virJSONValueObjectGet(reply, "error");
- if (error) {
- const char *errorStr = virJSONValueObjectGetString(error, "desc");
- if (errorStr && c_strcasestr(errorStr, "is locked"))
- ret = -2;
- }
- }
-
virJSONValueFree(cmd);
virJSONValueFree(reply);
return ret;
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index bb87397..3129427 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -1170,8 +1170,7 @@ int qemuMonitorTextSetCPU(qemuMonitorPtr mon, int cpu, bool online)
* Run HMP command to eject a media from ejectable device.
*
* Returns:
- * -2 on error, when the tray is locked
- * -1 on all other errors
+ * -1 on error
* 0 on success
*/
int qemuMonitorTextEjectMedia(qemuMonitorPtr mon,
@@ -1192,8 +1191,6 @@ int qemuMonitorTextEjectMedia(qemuMonitorPtr mon,
* device not found, device is locked ...
* No message is printed on success it seems */
if (c_strcasestr(reply, "device ")) {
- if (c_strcasestr(reply, "is locked"))
- ret = -2;
virReportError(VIR_ERR_OPERATION_FAILED,
_("could not eject media on %s: %s"), dev_name, reply);
goto cleanup;
--
2.7.4
8 years, 9 months
[libvirt] [PATCH] qemu-hotplug: fix eject media
by Pavel Hrdina
QEMU changed the error message to:
"Tray of device 'drive-sata0-0-1' is not open"
and they may change the error massage in the future.
This updates the code to not depend on the text from the error message
but only on error itself. One more improvement is that we will store
the original error in case if it's not about tray is not open.
Signed-off-by: Pavel Hrdina <phrdina(a)redhat.com>
---
src/qemu/qemu_hotplug.c | 17 +++++++++--------
src/qemu/qemu_monitor_json.c | 12 +-----------
src/qemu/qemu_monitor_text.c | 5 +----
3 files changed, 11 insertions(+), 23 deletions(-)
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index b580283..8122367 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -173,8 +173,8 @@ qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
qemuDomainObjPrivatePtr priv = vm->privateData;
const char *format = NULL;
char *sourcestr = NULL;
- bool ejectRetry = false;
unsigned long long now;
+ virErrorPtr ejectError = NULL;
if (!disk->info.alias) {
virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -202,15 +202,16 @@ qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
if (qemuDomainObjExitMonitor(driver, vm) < 0)
goto cleanup;
- if (rc == -2) {
+ if (rc < 0) {
/* we've already tried, error out */
- if (ejectRetry)
+ if (ejectError) {
+ virSetError(ejectError);
+ virFreeError(ejectError);
goto error;
- VIR_DEBUG("tray is locked, wait for the guest to unlock "
+ }
+ ejectError = virSaveLastError();
+ VIR_DEBUG("tray may be locked, wait for the guest to unlock "
"the tray and try to eject it again");
- ejectRetry = true;
- } else if (rc < 0) {
- goto error;
}
if (virTimeMillisNow(&now) < 0)
@@ -220,7 +221,7 @@ qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver,
if (virDomainObjWaitUntil(vm, now + CHANGE_MEDIA_TIMEOUT) != 0)
goto error;
}
- } while (ejectRetry && rc != 0);
+ } while (rc < 0);
if (!virStorageSourceIsEmpty(newsrc)) {
if (qemuGetDriveSourceString(newsrc, conn, &sourcestr) < 0)
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 8352e53..66a2922 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -2272,8 +2272,7 @@ int qemuMonitorJSONSetCPU(qemuMonitorPtr mon,
* Run QMP command to eject a media from ejectable device.
*
* Returns:
- * -2 on error, when the tray is locked
- * -1 on all other errors
+ * -1 on error
* 0 on success
*/
int qemuMonitorJSONEjectMedia(qemuMonitorPtr mon,
@@ -2294,15 +2293,6 @@ int qemuMonitorJSONEjectMedia(qemuMonitorPtr mon,
if (ret == 0)
ret = qemuMonitorJSONCheckError(cmd, reply);
- if (ret < 0) {
- virJSONValuePtr error = virJSONValueObjectGet(reply, "error");
- if (error) {
- const char *errorStr = virJSONValueObjectGetString(error, "desc");
- if (errorStr && c_strcasestr(errorStr, "is locked"))
- ret = -2;
- }
- }
-
virJSONValueFree(cmd);
virJSONValueFree(reply);
return ret;
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index bb87397..3129427 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -1170,8 +1170,7 @@ int qemuMonitorTextSetCPU(qemuMonitorPtr mon, int cpu, bool online)
* Run HMP command to eject a media from ejectable device.
*
* Returns:
- * -2 on error, when the tray is locked
- * -1 on all other errors
+ * -1 on error
* 0 on success
*/
int qemuMonitorTextEjectMedia(qemuMonitorPtr mon,
@@ -1192,8 +1191,6 @@ int qemuMonitorTextEjectMedia(qemuMonitorPtr mon,
* device not found, device is locked ...
* No message is printed on success it seems */
if (c_strcasestr(reply, "device ")) {
- if (c_strcasestr(reply, "is locked"))
- ret = -2;
virReportError(VIR_ERR_OPERATION_FAILED,
_("could not eject media on %s: %s"), dev_name, reply);
goto cleanup;
--
2.7.3
8 years, 9 months
[libvirt] [PATCH v4] Use-correct-pci-addresses-during-interface-detach
by Nitesh Konkar
The virsh attach virsh detach interface command fails when both live and config
are set and when the interface gets attached to different pci slots
on live and config xml respectively.
When we attach an interface with both --live and --config,
the first time they get the same PCI slots, but the second time
onwards it differs and hence the virsh detach-interface --live
--config command fails. This patch makes sure that when both
--live --config are set , qemuDomainDetachDeviceFlags is called
twice, once with config xml and once with live xml.
---
Change log:
v4:
* Code unchanged, updated with commit message,change log
and steps to reproduce the issue..
v3:
* Created function vshDomainDetachInterface and called
it once/twice from cmdDetachInterface depending on
number of flags set (live/config/both). Passed the
correct xml(live/persistent) to it.
v2:
* Changes only in cmdDetachInterface to pass the right domain xml
depending on the flag set (live/config/both).
v1:
* Changes only in qemuDomainDetachDeviceFlags to set the right value
in dev and dev_copy.
Steps to see the issue:
virsh attach-interface --domain DomainName --type network --source default --mac 52:54:00:4b:76:5f --live --config
virsh detach-interface --domain DomainName --type network --mac 52:54:00:4b:76:5f --live --config
virsh attach-interface --domain DomainName --type network --source default --mac 52:54:00:4b:76:5f --live --config
virsh detach-interface --domain DomainName --type network --mac 52:54:00:4b:76:5f --live --config
tools/virsh-domain.c | 104 +++++++++++++++++++++++++++++++--------------------
1 file changed, 64 insertions(+), 40 deletions(-)
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 62acecb..a6abaf5 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -10801,40 +10801,21 @@ static const vshCmdOptDef opts_detach_interface[] = {
{.name = NULL}
};
-static bool
-cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
+static bool
+vshDomainDetachInterface(char *doc, unsigned int flags, virDomainPtr dom, vshControl *ctl, const vshCmd *cmd)
{
- virDomainPtr dom = NULL;
xmlDocPtr xml = NULL;
xmlXPathObjectPtr obj = NULL;
xmlXPathContextPtr ctxt = NULL;
xmlNodePtr cur = NULL, matchNode = NULL;
- char *detach_xml = NULL;
const char *mac = NULL, *type = NULL;
- char *doc = NULL;
+ char *detach_xml = NULL;
+ bool current = vshCommandOptBool(cmd, "current");
char buf[64];
int diff_mac;
size_t i;
int ret;
bool functionReturn = false;
- unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
- bool current = vshCommandOptBool(cmd, "current");
- bool config = vshCommandOptBool(cmd, "config");
- bool live = vshCommandOptBool(cmd, "live");
- bool persistent = vshCommandOptBool(cmd, "persistent");
-
- VSH_EXCLUSIVE_OPTIONS_VAR(persistent, current);
-
- VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
- VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
-
- if (config || persistent)
- flags |= VIR_DOMAIN_AFFECT_CONFIG;
- if (live)
- flags |= VIR_DOMAIN_AFFECT_LIVE;
-
- if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
- return false;
if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0)
goto cleanup;
@@ -10842,15 +10823,6 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
if (vshCommandOptStringReq(ctl, cmd, "mac", &mac) < 0)
goto cleanup;
- if (persistent &&
- virDomainIsActive(dom) == 1)
- flags |= VIR_DOMAIN_AFFECT_LIVE;
-
- if (flags & VIR_DOMAIN_AFFECT_CONFIG)
- doc = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
- else
- doc = virDomainGetXMLDesc(dom, 0);
-
if (!doc)
goto cleanup;
@@ -10918,23 +10890,75 @@ cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
else
ret = virDomainDetachDevice(dom, detach_xml);
- if (ret != 0) {
- vshError(ctl, "%s", _("Failed to detach interface"));
- } else {
- vshPrint(ctl, "%s", _("Interface detached successfully\n"));
+ if (ret == 0)
functionReturn = true;
- }
cleanup:
- VIR_FREE(doc);
VIR_FREE(detach_xml);
- virDomainFree(dom);
+ xmlFreeDoc(xml);
xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt);
- xmlFreeDoc(xml);
return functionReturn;
}
+static bool
+cmdDetachInterface(vshControl *ctl, const vshCmd *cmd)
+{
+ virDomainPtr dom = NULL;
+ unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
+ char *doc_live = NULL, *doc_config = NULL;
+ bool current = vshCommandOptBool(cmd, "current");
+ bool config = vshCommandOptBool(cmd, "config");
+ bool live = vshCommandOptBool(cmd, "live");
+ bool persistent = vshCommandOptBool(cmd, "persistent");
+ bool functionReturn = false;
+
+ VSH_EXCLUSIVE_OPTIONS_VAR(persistent, current);
+
+ VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
+ VSH_EXCLUSIVE_OPTIONS_VAR(current, config);
+
+ if (config || persistent)
+ flags |= VIR_DOMAIN_AFFECT_CONFIG;
+
+ if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+ return false;
+
+ if (persistent &&
+ virDomainIsActive(dom) == 1)
+ flags |= VIR_DOMAIN_AFFECT_LIVE;
+
+ if (flags & VIR_DOMAIN_AFFECT_CONFIG) {
+ doc_config = virDomainGetXMLDesc(dom, VIR_DOMAIN_XML_INACTIVE);
+ if (!(functionReturn = vshDomainDetachInterface(doc_config, flags, dom, ctl, cmd)))
+ goto cleanup;
+ }
+
+ if (live)
+ flags |= VIR_DOMAIN_AFFECT_LIVE;
+
+ if (flags & VIR_DOMAIN_AFFECT_LIVE) {
+ doc_live = virDomainGetXMLDesc(dom, 0);
+
+ if (flags & VIR_DOMAIN_AFFECT_CONFIG)
+ flags &= (~VIR_DOMAIN_AFFECT_CONFIG);
+
+ functionReturn = vshDomainDetachInterface(doc_live, flags, dom, ctl, cmd);
+ }
+
+ cleanup:
+ if (functionReturn == false) {
+ vshError(ctl, "%s", _("Failed to detach interface"));
+ } else {
+ vshPrint(ctl, "%s", _("Interface detached successfully\n"));
+ functionReturn = true;
+ }
+ VIR_FREE(doc_live);
+ VIR_FREE(doc_config);
+ virDomainFree(dom);
+ return functionReturn;
+}
+
typedef enum {
VIRSH_FIND_DISK_NORMAL,
VIRSH_FIND_DISK_CHANGEABLE,
--
1.8.3.1
8 years, 9 months
Re: [libvirt] [PATCH v5 0/5] ARM: add query-gic-capabilities SMP command
by Peter Xu
On Mon, Mar 21, 2016 at 04:56:07PM +0100, Andrea Bolognani wrote:
> On Fri, 2016-03-18 at 11:27 +0800, Peter Xu wrote:
> > v5 changes:
> > - patch 2: moved to target-arm/monitor.c (from target-arm/machine.c)
> > [Peter]
> > - patch 3: splitted into three patches: [all from Peter's comments]
> > - patch 3 (new): leverage kvm_arm_create_scratch_host_vcpu(), tiny
> > enhancement of old one to suite our need
> > - patch 4: introduce kvm_support_device() in kvm-all.c
> > - patch 5: do the implementation.
>
> Tested on two separate aarch64 hosts, seems to work fine.
Thanks Andrea!
Sorry I forgot to CC libvir-list. To avoid duplication, not
re-sending but CCing in this reply. If anyone interested, please
review in the following link:
https://lists.gnu.org/archive/html/qemu-devel/2016-03/msg04465.html
(which points to exactly current thread.)
Sorry for the inconvenience!
-- peterx
8 years, 9 months
[libvirt] virtual machine won't autostart when using LVM with cache
by David Hlacik
Hello guys,
OS Fedora 23 with latest updates
Recently I have switched to use LVM cache feature on logical volume
/dev/hdd/windata1 to improve it performace using 32GB partition from SSD
disk.
However, when my computer will start, my virtual machine won't autostart
Mar 21 10:48:57 brutus-coreos libvirtd[956]: Cannot access storage file
'/dev/hdd/windata1' (as uid:107, gid:107): No such file or directory
Mar 21 10:48:57 brutus-coreos libvirtd[956]: Failed to autostart VM
'winos1': Cannot access storage file '/dev/hdd/windata1' (as uid:107,
gid:107): No such file or directory
Mar 21 10:48:57 brutus-coreos libvirtd[956]: Cannot access storage file
'/dev/hdd/windata1' (as uid:107, gid:107): No such file or directory
It seems that when using LVM cache, one have to wait till it will
inicialize?
When I will manually start virtual machine afterwards, everything works OK :
[root@brutus-coreos ~]# virsh start winos1
Domain winos1 started
I have tried to remove LVM cache from /dev/hdd/windata1 and afterward
autostart works! So it must be LVM cache related.
Can you please help me to solve this ?
Thanks in advance
David Hlacik
+420-777-307-745 | david(a)hlacik.cz
[image: View David Hlacik's profile on LinkedIn]
<http://cz.linkedin.com/in/hlacik>
8 years, 9 months
[libvirt] [PATCH v2] nss: Make aligning look nicer
by Martin Kletzander
Every aligning requires at least one cast and it's hard to read. Let's
make a function that makes sure the pointer is moved according to the
alignment and use that to move throughout the data buffer.
Signed-off-by: Martin Kletzander <mkletzan(a)redhat.com>
---
v2:
- Initialize @idx in _nss_libvirt_gethostbyname4_r as well
tools/nss/libvirt_nss.c | 53 +++++++++++++++++++++++++++++++------------------
1 file changed, 34 insertions(+), 19 deletions(-)
diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c
index ba3bb31b3569..218c62a64c88 100644
--- a/tools/nss/libvirt_nss.c
+++ b/tools/nss/libvirt_nss.c
@@ -252,17 +252,31 @@ _nss_libvirt_gethostbyname2_r(const char *name, int af, struct hostent *result,
errnop, herrnop, NULL, NULL);
}
+static inline void *
+move_and_align(void *buf, size_t len, size_t *idx)
+{
+ char *buffer = buf;
+ size_t move = ALIGN(len);
+
+ if (!idx)
+ return buffer + move;
+
+ *idx += move;
+
+ return buffer + *idx;
+}
+
enum nss_status
_nss_libvirt_gethostbyname3_r(const char *name, int af, struct hostent *result,
char *buffer, size_t buflen, int *errnop,
int *herrnop, int32_t *ttlp, char **canonp)
{
enum nss_status ret = NSS_STATUS_UNAVAIL;
- char *r_name, **r_aliases, *r_addr, **r_addr_list;
+ char *r_name, **r_aliases, *r_addr, *r_addr_next, **r_addr_list;
leaseAddress *addr = NULL;
size_t naddr, i;
bool found = false;
- size_t nameLen, need, idx;
+ size_t nameLen, need, idx = 0;
int alen;
int r;
@@ -319,23 +333,27 @@ _nss_libvirt_gethostbyname3_r(const char *name, int af, struct hostent *result,
/* First, append name */
r_name = buffer;
memcpy(r_name, name, nameLen + 1);
- idx = ALIGN(nameLen + 1);
+
+ r_aliases = move_and_align(buffer, nameLen + 1, &idx);
/* Second, create aliases array */
- r_aliases = (char **) buffer + idx;
r_aliases[0] = NULL;
- idx += sizeof(char*);
/* Third, append address */
- r_addr = buffer + idx;
- for (i = 0; i < naddr; i++)
- memcpy(r_addr + i * ALIGN(alen), addr[i].addr, alen);
- idx += naddr * ALIGN(alen);
+ r_addr = move_and_align(buffer, sizeof(char *), &idx);
+ r_addr_next = r_addr;
+ for (i = 0; i < naddr; i++) {
+ memcpy(r_addr_next, addr[i].addr, alen);
+ r_addr_next = move_and_align(buffer, alen, &idx);
+ }
+ r_addr_list = move_and_align(buffer, 0, &idx);
+ r_addr_next = r_addr;
/* Third, append address pointer array */
- r_addr_list = (char **) buffer + idx;
- for (i = 0; i < naddr; i++)
- r_addr_list[i] = r_addr + i * ALIGN(alen);
+ for (i = 0; i < naddr; i++) {
+ r_addr_list[i] = r_addr_next;
+ r_addr_next = move_and_align(r_addr_next, alen, NULL);
+ }
r_addr_list[i] = NULL;
idx += (naddr + 1) * sizeof(char*);
@@ -375,7 +393,7 @@ _nss_libvirt_gethostbyname4_r(const char *name, struct gaih_addrtuple **pat,
size_t naddr, i;
bool found = false;
int r;
- size_t nameLen, need, idx;
+ size_t nameLen, need, idx = 0;
struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL;
char *r_name;
@@ -420,25 +438,22 @@ _nss_libvirt_gethostbyname4_r(const char *name, struct gaih_addrtuple **pat,
/* First, append name */
r_name = buffer;
memcpy(r_name, name, nameLen + 1);
- idx = ALIGN(nameLen + 1);
-
/* Second, append addresses */
- r_tuple_first = (struct gaih_addrtuple*) (buffer + idx);
+ r_tuple_first = move_and_align(buffer, nameLen + 1, &idx);
for (i = 0; i < naddr; i++) {
int family = addr[i].af;
- r_tuple = (struct gaih_addrtuple*) (buffer + idx);
+ r_tuple = move_and_align(buffer, 0, &idx);
if (i == naddr - 1)
r_tuple->next = NULL;
else
- r_tuple->next = (struct gaih_addrtuple*) ((char*) r_tuple + ALIGN(sizeof(struct gaih_addrtuple)));
+ r_tuple->next = move_and_align(buffer, sizeof(struct gaih_addrtuple), &idx);
r_tuple->name = r_name;
r_tuple->family = family;
r_tuple->scopeid = 0;
memcpy(r_tuple->addr, addr[i].addr, FAMILY_ADDRESS_SIZE(family));
- idx += ALIGN(sizeof(struct gaih_addrtuple));
}
/* At this point, idx == need */
--
2.7.3
8 years, 9 months