[Libvir] [PATCH] Add bus attribute to disk target definition
by Soren Hansen
Hi!
I'd like to propose that the following patch gets applied against
libvirt. It adds the option of putting a bus attribute on a disk target.
To acommodate this, it also changes the way drives are defined for kvm
from the old "-hda /path/to/file -boot c" style to the new "-drive
file=/path/to/file,if=ide,boot=on". This makes it possible to specify
virtio, scsi, and ide disks.
=== modified file 'src/qemu_conf.c'
--- src/qemu_conf.c 2008-04-28 15:14:59 +0000
+++ src/qemu_conf.c 2008-04-29 07:43:11 +0000
@@ -568,6 +568,7 @@
xmlChar *source = NULL;
xmlChar *target = NULL;
xmlChar *type = NULL;
+ xmlChar *bus = NULL;
int typ = 0;
type = xmlGetProp(node, BAD_CAST "type");
@@ -598,6 +599,7 @@
} else if ((target == NULL) &&
(xmlStrEqual(cur->name, BAD_CAST "target"))) {
target = xmlGetProp(cur, BAD_CAST "dev");
+ bus = xmlGetProp(cur, BAD_CAST "bus");
} else if (xmlStrEqual(cur->name, BAD_CAST "readonly")) {
disk->readonly = 1;
}
@@ -646,7 +648,9 @@
strcmp((const char *)target, "hda") &&
strcmp((const char *)target, "hdb") &&
strcmp((const char *)target, "hdc") &&
- strcmp((const char *)target, "hdd")) {
+ strcmp((const char *)target, "hdd") &&
+ strcmp((const char *)target, "hdd") &&
+ strncmp((const char *)target, "vd", 2)) {
qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
_("Invalid harddisk device name: %s"), target);
goto error;
@@ -673,6 +677,20 @@
goto error;
}
+ if (!bus)
+ disk->bus = QEMUD_DISK_BUS_IDE;
+ else if (!strcmp((const char *)bus, "ide"))
+ disk->bus = QEMUD_DISK_BUS_IDE;
+ else if (!strcmp((const char *)bus, "scsi"))
+ disk->bus = QEMUD_DISK_BUS_SCSI;
+ else if (!strcmp((const char *)bus, "virtio"))
+ disk->bus = QEMUD_DISK_BUS_VIRTIO;
+ else {
+ qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "Invalid bus type: %s", bus);
+ goto error;
+ }
+
+ xmlFree(bus);
xmlFree(device);
xmlFree(target);
xmlFree(source);
@@ -688,6 +706,8 @@
xmlFree(source);
if (device)
xmlFree(device);
+ if (bus)
+ xmlFree(bus);
return -1;
}
@@ -1350,6 +1370,68 @@
return -1;
}
+static int qemudDiskCompare(const void *aptr, const void *bptr) {
+ struct qemud_vm_disk_def *a = (struct qemud_vm_disk_def *) aptr;
+ struct qemud_vm_disk_def *b = (struct qemud_vm_disk_def *) bptr;
+ if (a->device == b->device)
+ return virDiskNameToIndex(a->dst) - virDiskNameToIndex(b->dst);
+ else
+ return a->device - b->device;
+}
+
+static const char *qemudBusIdToName(int busId) {
+ const char *busnames[] = { "ide",
+ "scsi",
+ "virtio" };
+
+ if (busId >= 0 && busId < 3)
+ return busnames[busId];
+ else
+ return 0;
+}
+
+static char *qemudDriveOpt(struct qemud_vm_disk_def *disk, int boot)
+{
+ char opt[PATH_MAX];
+
+ switch (disk->device) {
+ case QEMUD_DISK_CDROM:
+ snprintf(opt, PATH_MAX, "file=%s,if=ide,media=cdrom%s",
+ disk->src, boot ? ",boot=on" : "");
+ break;
+ case QEMUD_DISK_FLOPPY:
+ snprintf(opt, PATH_MAX, "file=%s,if=floppy%s",
+ disk->src, boot ? ",boot=on" : "");
+ break;
+ case QEMUD_DISK_DISK:
+ snprintf(opt, PATH_MAX, "file=%s,if=%s%s",
+ disk->src, qemudBusIdToName(disk->bus), boot ? ",boot=on" : "");
+ break;
+ default:
+ return 0;
+ }
+ return strdup(opt);
+}
+
+static char *qemudAddBootDrive(virConnectPtr conn,
+ struct qemud_vm_def *def,
+ char *handledDisks,
+ int type) {
+ int j = 0;
+ struct qemud_vm_disk_def *disk = def->disks;
+
+ while (disk) {
+ if (!handledDisks[j] && disk->device == type) {
+ handledDisks[j] = 1;
+ return qemudDriveOpt(disk, 1);
+ }
+ j++;
+ disk = disk->next;
+ }
+ qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+ "Requested boot device type %d, but no such device defined.", type);
+ return 0;
+}
/*
* Parses a libvirt XML definition of a guest, and populates the
@@ -1739,7 +1821,6 @@
obj = xmlXPathEval(BAD_CAST "/domain/devices/disk", ctxt);
if ((obj != NULL) && (obj->type == XPATH_NODESET) &&
(obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) {
- struct qemud_vm_disk_def *prev = NULL;
for (i = 0; i < obj->nodesetval->nodeNr; i++) {
struct qemud_vm_disk_def *disk = calloc(1, sizeof(*disk));
if (!disk) {
@@ -1752,13 +1833,20 @@
goto error;
}
def->ndisks++;
- disk->next = NULL;
if (i == 0) {
+ disk->next = NULL;
def->disks = disk;
} else {
- prev->next = disk;
+ struct qemud_vm_disk_def *ptr = def->disks;
+ while (ptr) {
+ if (!ptr->next || qemudDiskCompare(ptr->next, disk) < 0) {
+ disk->next = ptr->next;
+ ptr->next = disk;
+ break;
+ }
+ ptr = ptr->next;
+ }
}
- prev = disk;
}
}
xmlXPathFreeObject(obj);
@@ -2207,30 +2295,32 @@
goto no_memory;
}
- for (i = 0 ; i < vm->def->os.nBootDevs ; i++) {
- switch (vm->def->os.bootDevs[i]) {
- case QEMUD_BOOT_CDROM:
- boot[i] = 'd';
- break;
- case QEMUD_BOOT_FLOPPY:
- boot[i] = 'a';
- break;
- case QEMUD_BOOT_DISK:
- boot[i] = 'c';
- break;
- case QEMUD_BOOT_NET:
- boot[i] = 'n';
- break;
- default:
- boot[i] = 'c';
- break;
+ if (vm->def->virtType != QEMUD_VIRT_KVM) {
+ for (i = 0 ; i < vm->def->os.nBootDevs ; i++) {
+ switch (vm->def->os.bootDevs[i]) {
+ case QEMUD_BOOT_CDROM:
+ boot[i] = 'd';
+ break;
+ case QEMUD_BOOT_FLOPPY:
+ boot[i] = 'a';
+ break;
+ case QEMUD_BOOT_DISK:
+ boot[i] = 'c';
+ break;
+ case QEMUD_BOOT_NET:
+ boot[i] = 'n';
+ break;
+ default:
+ boot[i] = 'c';
+ break;
+ }
}
+ boot[vm->def->os.nBootDevs] = '\0';
+ if (!((*argv)[++n] = strdup("-boot")))
+ goto no_memory;
+ if (!((*argv)[++n] = strdup(boot)))
+ goto no_memory;
}
- boot[vm->def->os.nBootDevs] = '\0';
- if (!((*argv)[++n] = strdup("-boot")))
- goto no_memory;
- if (!((*argv)[++n] = strdup(boot)))
- goto no_memory;
if (vm->def->os.kernel[0]) {
if (!((*argv)[++n] = strdup("-kernel")))
@@ -2251,28 +2341,74 @@
goto no_memory;
}
- while (disk) {
- char dev[NAME_MAX];
- char file[PATH_MAX];
- if (!strcmp(disk->dst, "hdc") &&
- disk->device == QEMUD_DISK_CDROM) {
- if (disk->src[0])
- snprintf(dev, NAME_MAX, "-%s", "cdrom");
- else {
- /* Don't put anything on the cmdline for an empty cdrom*/
- disk = disk->next;
- continue;
- }
- } else
- snprintf(dev, NAME_MAX, "-%s", disk->dst);
- snprintf(file, PATH_MAX, "%s", disk->src);
-
- if (!((*argv)[++n] = strdup(dev)))
- goto no_memory;
- if (!((*argv)[++n] = strdup(file)))
- goto no_memory;
-
- disk = disk->next;
+ if (vm->def->virtType == QEMUD_VIRT_KVM) {
+ char *handledDisks = NULL;
+ int j;
+
+ handledDisks = calloc(sizeof(*handledDisks), vm->def->ndisks);
+
+ if (!handledDisks)
+ goto no_memory;
+
+ /* When using -drive notation, we need to provide the devices in boot
+ * preference order. */
+ for (i = 0 ; i < vm->def->os.nBootDevs ; i++) {
+ if (!((*argv)[++n] = strdup("-drive")))
+ goto no_memory;
+
+ switch (vm->def->os.bootDevs[i]) {
+ case QEMUD_BOOT_CDROM:
+ if (!((*argv)[++n] = qemudAddBootDrive(conn, vm->def, handledDisks, QEMUD_DISK_CDROM)))
+ goto error;
+ break;
+ case QEMUD_BOOT_FLOPPY:
+ if (!((*argv)[++n] = qemudAddBootDrive(conn, vm->def, handledDisks, QEMUD_DISK_FLOPPY)))
+ goto error;
+ break;
+ case QEMUD_BOOT_DISK:
+ if (!((*argv)[++n] = qemudAddBootDrive(conn, vm->def, handledDisks, QEMUD_DISK_DISK)))
+ goto error;
+ break;
+ }
+ }
+
+ /* Pick up the rest of the devices */
+ j=0;
+ while (disk) {
+ if (!handledDisks[j]) {
+ handledDisks[j] = 1;
+ if (!((*argv)[++n] = strdup("-drive")))
+ goto no_memory;
+ if (!((*argv)[++n] = qemudDriveOpt(disk, 0)))
+ goto no_memory;
+ }
+ disk = disk->next;
+ j++;
+ }
+ } else {
+ while (disk) {
+ char dev[NAME_MAX];
+ char file[PATH_MAX];
+
+ if (!strcmp(disk->dst, "hdc") &&
+ disk->device == QEMUD_DISK_CDROM)
+ if (disk->src[0])
+ snprintf(dev, NAME_MAX, "-%s", "cdrom");
+ else {
+ disk = disk->next;
+ continue;
+ }
+ else
+ snprintf(dev, NAME_MAX, "-%s", disk->dst);
+ snprintf(file, PATH_MAX, "%s", disk->src);
+
+ if (!((*argv)[++n] = strdup(dev)))
+ goto no_memory;
+ if (!((*argv)[++n] = strdup(file)))
+ goto no_memory;
+
+ disk = disk->next;
+ }
}
if (!net) {
@@ -3565,6 +3701,7 @@
virBufferVSprintf(&buf, " <source %s='%s'/>\n",
typeAttrs[disk->type], disk->src);
+ virBufferVSprintf(&buf, " <target dev='%s' bus='%s'/>\n", disk->dst, qemudBusIdToName(disk->bus));
virBufferVSprintf(&buf, " <target dev='%s'/>\n", disk->dst);
if (disk->readonly)
=== modified file 'src/qemu_conf.h'
--- src/qemu_conf.h 2008-04-25 20:46:13 +0000
+++ src/qemu_conf.h 2008-04-29 07:13:16 +0000
@@ -56,10 +56,17 @@
QEMUD_DISK_FLOPPY,
};
+enum qemud_vm_disk_bus {
+ QEMUD_DISK_BUS_IDE,
+ QEMUD_DISK_BUS_SCSI,
+ QEMUD_DISK_BUS_VIRTIO
+};
+
/* Stores the virtual disk configuration */
struct qemud_vm_disk_def {
int type;
int device;
+ int bus;
char src[PATH_MAX];
char dst[NAME_MAX];
int readonly;
=== modified file 'src/util.c'
--- src/util.c 2008-04-25 14:53:05 +0000
+++ src/util.c 2008-04-29 06:59:49 +0000
@@ -771,3 +771,43 @@
return -1;
}
+
+/* Translates a device name of the form (regex) "[fhv]d[a-z]+" into
+ * the corresponding index (e.g. sda => 1, hdz => 26, vdaa => 27)
+ * @param name The name of the device
+ * @return name's index, or 0 on failure
+ */
+int virDiskNameToIndex(const char *name) {
+ const char *ptr = NULL;
+ int idx = 0;
+
+ if (strlen(name) < 3)
+ return 0;
+
+ switch (*name) {
+ case 'f':
+ case 'h':
+ case 'v':
+ break;
+ default:
+ return 0;
+ }
+
+ if (*(name + 1) != 'd')
+ return 0;
+
+ ptr = name+2;
+
+ while (*ptr) {
+ idx = idx * 26;
+
+ if ('a' > *ptr || 'z' < *ptr)
+ return 0;
+
+ idx += *ptr - 'a' + 1;
+ ptr++;
+ }
+
+ return idx;
+}
+
=== modified file 'src/util.h'
--- src/util.h 2008-04-25 14:53:05 +0000
+++ src/util.h 2008-04-29 06:59:57 +0000
@@ -92,4 +92,6 @@
int virParseMacAddr(const char* str, unsigned char *addr);
+int virDiskNameToIndex(const char* str);
+
#endif /* __VIR_UTIL_H__ */
--
Soren Hansen |
Virtualisation specialist | Ubuntu Server Team
Canonical Ltd. | http://www.ubuntu.com/
16 years, 3 months
[Libvir] Re: libvirt on mingw
by Richard W.M. Jones
Brecht Sanders wrote:
> Hi,
> I saw on the following link:
> http://www.mail-archive.com/libvir-list@redhat.com/msg04103.html
> that you are also trying to compile libvirt on win32.
> I'm also attempting to do this, and I guess I got stuck at the same
> point your post was about.
> Have you in the mean time found an XDR implementation that compiles on
> MinGW and that implements xdr_u_quad_t?
> If you did, can you please tell me where to find it?
You'll find the answer to this question and more if you look through the
libvir-list archives for the current month:
https://www.redhat.com/archives/libvir-list/2008-January/thread.html
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
16 years, 4 months
[Libvir] [PATCH] Fix USB device name mis-conversion from S-Expr to XML
by Hiroyuki Kaguchi
"tablet" and "mouse" are set as a value of the
/local/domain/<domid>/image/hvm/usbdevice,
but libvirt expects "usbdevice" and "usbmouse" as a value.
This causes the following.
If a USB device is attached by virt-manager,
the USB device is not seen from virt-manager.
This patch fixes expected value of libvirt
Thanks,
Hiroyuki Kaguchi
Index: xend_internal.c
===================================================================
RCS file: /data/cvs/libvirt/src/xend_internal.c,v
retrieving revision 1.162
diff -u -r1.162 xend_internal.c
--- xend_internal.c 21 Jan 2008 16:29:10 -0000 1.162
+++ xend_internal.c 23 Jan 2008 00:37:02 -0000
@@ -1743,11 +1743,9 @@
node = cur->u.s.car;
if (sexpr_lookup(node, "usbdevice")) {
tmp = sexpr_node(node, "usbdevice");
- if (tmp && *tmp) {
- if (!strcmp(tmp, "usbtablet"))
- virBufferAdd(&buf, " <input type='tablet'
bus='usb'/>\n", 37);
- else if (!strcmp(tmp, "usbmouse"))
- virBufferAdd(&buf, " <input type='mouse'
bus='usb'/>\n", 36);
+ if (!strcmp(tmp, "tablet") ||
+ !strcmp(tmp, "mouse")) {
+ virBufferVSprintf(&buf, " <input type='%s'
bus='usb'/>\n", tmp);
}
}
}
16 years, 5 months
[Libvir] Reopening the old discussion about virDomainBlockPeek
by Richard W.M. Jones
Virt-df[1] has now gained the ability to fully parse LVM2 partitions,
thus:
# virt-df -c qemu:///system -h
Filesystem Size Used Available Type
rhel51x32kvm:hda1 96.8 MiB 14.6 MiB 82.2 MiB Linux ext2/3
rhel51x32kvm:VolGroup00/LogVol00 6.4 GiB 3.6 GiB 2.8 GiB Linux ext2/3
rhel51x32kvm:VolGroup00/LogVol01 992.0 MiB Linux swap
However it still has to do it by opening the local partitions / files,
which means it isn't using a "proper" part of libvirt and more
importantly it cannot run remotely.
I'd like to find out whether the time has come for us to look again at
a virDomainBlockPeek call for libvirt. Here is the original thread
plus patch from 7 months ago:
http://www.redhat.com/archives/libvir-list/2007-October/thread.html#00089
(I've attached an updated patch against current CVS).
I appreciate that some cases might not be simple (iSCSI?), but the
same argument applies to block device statistics too, and we make
those available where possible. I think a best-effort call allowing
callers to peek into the block devices of guests would be very useful.
Rich.
[1] http://hg.et.redhat.com/applications/virt/applications/virt-df--devel
--
Richard Jones, Emerging Technologies, Red Hat http://et.redhat.com/~rjones
virt-top is 'top' for virtual machines. Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://et.redhat.com/~rjones/virt-top
16 years, 5 months
[libvirt] PATCH: Switch all remaining code to memory alloc APIs
by Daniel P. Berrange
This patch switches all remaining code over to use the memory allocation
APIs, with exception of virsh which is going to be slightly more complex
It was mostly a straight conversion - there were only a few places which
weren't checking for failure corecttly - the most notable being sexpr.c.
bridge.c | 18 +++----
conf.c | 51 ++++++++++----------
iptables.c | 60 ++++++++++--------------
lxc_conf.c | 45 ++++++++----------
lxc_container.c | 5 +-
lxc_driver.c | 25 ++++------
openvz_conf.c | 49 ++++++++-----------
openvz_driver.c | 3 -
proxy_internal.c | 8 +--
remote_internal.c | 85 +++++++++++++++-------------------
sexpr.c | 31 ++++++++----
storage_backend.c | 32 ++++++------
storage_backend_disk.c | 24 ++++-----
storage_backend_fs.c | 56 ++++++++++------------
storage_backend_iscsi.c | 30 +++++-------
storage_backend_logical.c | 20 +++-----
storage_conf.c | 114 +++++++++++++++++++++++-----------------------
storage_driver.c | 5 --
xen_internal.c | 38 +++++++--------
xen_unified.c | 23 ++++-----
xend_internal.c | 1
xmlrpc.c | 67 +++++++++++++--------------
22 files changed, 379 insertions(+), 411 deletions(-)
Regards,
Daniel.
diff -r ff6b92c70738 src/bridge.c
--- a/src/bridge.c Fri May 30 10:36:42 2008 -0400
+++ b/src/bridge.c Fri May 30 10:55:44 2008 -0400
@@ -45,6 +45,7 @@
#include <net/if_arp.h> /* ARPHRD_ETHER */
#include "internal.h"
+#include "memory.h"
#define MAX_BRIDGE_ID 256
@@ -84,8 +85,7 @@
return err;
}
- *ctlp = malloc(sizeof(**ctlp));
- if (!*ctlp) {
+ if (VIR_ALLOC(*ctlp) < 0) {
close(fd);
return ENOMEM;
}
@@ -110,7 +110,7 @@
close(ctl->fd);
ctl->fd = 0;
- free(ctl);
+ VIR_FREE(ctl);
}
/**
@@ -681,7 +681,7 @@
snprintf(delayStr, sizeof(delayStr), "%d", delay);
- if (!(argv = calloc(n + 1, sizeof(*argv))))
+ if (VIR_ALLOC_N(argv, n + 1) < 0)
goto error;
n = 0;
@@ -706,8 +706,8 @@
if (argv) {
n = 0;
while (argv[n])
- free(argv[n++]);
- free(argv);
+ VIR_FREE(argv[n++]);
+ VIR_FREE(argv);
}
return retval;
@@ -738,7 +738,7 @@
1 + /* brige name */
1; /* value */
- if (!(argv = calloc(n + 1, sizeof(*argv))))
+ if (VIR_ALLOC_N(argv, n + 1) < 0)
goto error;
n = 0;
@@ -763,8 +763,8 @@
if (argv) {
n = 0;
while (argv[n])
- free(argv[n++]);
- free(argv);
+ VIR_FREE(argv[n++]);
+ VIR_FREE(argv);
}
return retval;
diff -r ff6b92c70738 src/conf.c
--- a/src/conf.c Fri May 30 10:36:42 2008 -0400
+++ b/src/conf.c Fri May 30 10:55:44 2008 -0400
@@ -23,6 +23,7 @@
#include "conf.h"
#include "util.h"
#include "c-ctype.h"
+#include "memory.h"
/************************************************************************
* *
@@ -138,11 +139,11 @@
return;
if (val->type == VIR_CONF_STRING &&
val->str != NULL)
- free(val->str);
+ VIR_FREE(val->str);
if (val->type == VIR_CONF_LIST &&
val->list != NULL)
virConfFreeList(val->list);
- free(val);
+ VIR_FREE(val);
}
virConfPtr
@@ -150,8 +151,7 @@
{
virConfPtr ret;
- ret = calloc(1, sizeof(*ret));
- if (ret == NULL) {
+ if (VIR_ALLOC(ret) < 0) {
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0);
return(NULL);
}
@@ -199,8 +199,7 @@
if ((comm == NULL) && (name == NULL))
return(NULL);
- ret = calloc(1, sizeof(*ret));
- if (ret == NULL) {
+ if (VIR_ALLOC(ret) < 0) {
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0);
return(NULL);
}
@@ -441,7 +440,8 @@
NEXT;
SKIP_BLANKS_AND_EOL;
if ((ctxt->cur < ctxt->end) && (CUR != ']')) {
- lst = virConfParseValue(ctxt);
+ if ((lst = virConfParseValue(ctxt)) == NULL)
+ return(NULL);
SKIP_BLANKS_AND_EOL;
}
while ((ctxt->cur < ctxt->end) && (CUR != ']')) {
@@ -484,10 +484,10 @@
ctxt->line);
return(NULL);
}
- ret = calloc(1, sizeof(*ret));
- if (ret == NULL) {
+ if (VIR_ALLOC(ret) < 0) {
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0);
- free(str);
+ virConfFreeList(lst);
+ VIR_FREE(str);
return(NULL);
}
ret->type = type;
@@ -618,7 +618,7 @@
SKIP_BLANKS;
value = virConfParseValue(ctxt);
if (value == NULL) {
- free(name);
+ VIR_FREE(name);
return(-1);
}
SKIP_BLANKS;
@@ -630,15 +630,15 @@
if (comm == NULL) {
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"),
ctxt->line);
- free(name);
+ VIR_FREE(name);
virConfFreeValue(value);
return(-1);
}
}
if (virConfAddEntry(ctxt->conf, name, value, comm) == NULL) {
- free(name);
+ VIR_FREE(name);
virConfFreeValue(value);
- free(comm);
+ VIR_FREE(comm);
return(-1);
}
return(0);
@@ -720,7 +720,7 @@
conf = virConfParse(filename, content, len);
- free(content);
+ VIR_FREE(content);
return conf;
}
@@ -769,14 +769,14 @@
tmp = conf->entries;
while (tmp) {
virConfEntryPtr next;
- free(tmp->name);
+ VIR_FREE(tmp->name);
virConfFreeValue(tmp->value);
- free(tmp->comment);
+ VIR_FREE(tmp->comment);
next = tmp->next;
- free(tmp);
+ VIR_FREE(tmp);
tmp = next;
}
- free(conf);
+ VIR_FREE(conf);
return(0);
}
@@ -834,14 +834,14 @@
}
if (!cur) {
- if (!(cur = malloc(sizeof(*cur)))) {
+ if (VIR_ALLOC(cur) < 0) {
virConfFreeValue(value);
return (-1);
}
cur->comment = NULL;
if (!(cur->name = strdup(setting))) {
virConfFreeValue(value);
- free(cur);
+ VIR_FREE(cur);
return (-1);
}
cur->value = value;
@@ -897,15 +897,16 @@
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );
if (fd < 0) {
+ char *tmp = virBufferContentAndReset(&buf);
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"), 0);
- free(virBufferContentAndReset(&buf));
+ VIR_FREE(tmp);
return -1;
}
use = virBufferUse(&buf);
content = virBufferContentAndReset(&buf);
ret = safewrite(fd, content, use);
- free(content);
+ VIR_FREE(content);
close(fd);
if (ret != (int)use) {
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"), 0);
@@ -955,11 +956,11 @@
if ((int)use >= *len) {
*len = (int)use;
- free(content);
+ VIR_FREE(content);
return -1;
}
memcpy(memory, content, use);
- free(content);
+ VIR_FREE(content);
*len = use;
return use;
}
diff -r ff6b92c70738 src/iptables.c
--- a/src/iptables.c Fri May 30 10:36:42 2008 -0400
+++ b/src/iptables.c Fri May 30 10:55:44 2008 -0400
@@ -45,6 +45,7 @@
#include "internal.h"
#include "iptables.h"
#include "util.h"
+#include "memory.h"
#define qemudLog(level, msg...) fprintf(stderr, msg)
@@ -156,7 +157,7 @@
snprintf(arg, sizeof(arg), "--custom-rules=ipv4:%s:%s", table, path);
if (!stripLine(content, len, arg)) {
- free(content);
+ VIR_FREE(content);
return;
}
@@ -171,7 +172,7 @@
goto write_error;
}
- free(content);
+ VIR_FREE(content);
return;
@@ -181,7 +182,7 @@
strerror(errno));
if (f)
fclose(f);
- free(content);
+ VIR_FREE(content);
#undef MAX_FILE_LEN
}
@@ -264,14 +265,14 @@
static void
iptRuleFree(iptRule *rule)
{
- free(rule->rule);
+ VIR_FREE(rule->rule);
rule->rule = NULL;
if (rule->argv) {
int i = 0;
while (rule->argv[i])
- free(rule->argv[i++]);
- free(rule->argv);
+ VIR_FREE(rule->argv[i++]);
+ VIR_FREE(rule->argv);
rule->argv = NULL;
}
}
@@ -282,17 +283,13 @@
char **argv,
int command_idx)
{
- iptRule *r;
-
- if (!(r = realloc(rules->rules, sizeof(*r) * (rules->nrules+1)))) {
+ if (VIR_REALLOC_N(rules->rules, rules->nrules+1) < 0) {
int i = 0;
while (argv[i])
- free(argv[i++]);
- free(argv);
+ VIR_FREE(argv[i++]);
+ VIR_FREE(argv);
return ENOMEM;
}
-
- rules->rules = r;
rules->rules[rules->nrules].rule = rule;
rules->rules[rules->nrules].argv = argv;
@@ -332,23 +329,17 @@
{
int i;
- if (rules->table) {
- free(rules->table);
- rules->table = NULL;
- }
+ if (rules->table)
+ VIR_FREE(rules->table);
- if (rules->chain) {
- free(rules->chain);
- rules->chain = NULL;
- }
-
+ if (rules->chain)
+ VIR_FREE(rules->chain);
if (rules->rules) {
for (i = 0; i < rules->nrules; i++)
iptRuleFree(&rules->rules[i]);
- free(rules->rules);
- rules->rules = NULL;
+ VIR_FREE(rules->rules);
rules->nrules = 0;
}
@@ -358,7 +349,7 @@
rules->path[0] = '\0';
#endif /* ENABLE_IPTABLES_LOKKIT */
- free(rules);
+ VIR_FREE(rules);
}
static iptRules *
@@ -367,7 +358,7 @@
{
iptRules *rules;
- if (!(rules = calloc(1, sizeof (*rules))))
+ if (VIR_ALLOC(rules) < 0)
return NULL;
if (!(rules->table = strdup(table)))
@@ -404,8 +395,9 @@
for (len = 1, i = 0; argv[i]; i++)
len += strlen(argv[i]) + 1;
- if (!(p = ret = (char *)malloc(len)))
+ if (VIR_ALLOC_N(ret, len) < 0)
return NULL;
+ p = ret;
for (i = 0; argv[i]; i++) {
if (i != 0)
@@ -441,7 +433,7 @@
va_end(args);
- if (!(argv = calloc(n + 1, sizeof(*argv))))
+ if (VIR_ALLOC_N(argv, n + 1) < 0)
goto error;
n = 0;
@@ -478,7 +470,7 @@
goto error;
if (action == REMOVE) {
- free(argv[command_idx]);
+ VIR_FREE(argv[command_idx]);
if (!(argv[command_idx] = strdup("--delete")))
goto error;
}
@@ -497,13 +489,13 @@
}
error:
- free(rule);
+ VIR_FREE(rule);
if (argv) {
n = 0;
while (argv[n])
- free(argv[n++]);
- free(argv);
+ VIR_FREE(argv[n++]);
+ VIR_FREE(argv);
}
return retval;
@@ -521,7 +513,7 @@
{
iptablesContext *ctx;
- if (!(ctx = calloc(1, sizeof (*ctx))))
+ if (VIR_ALLOC(ctx) < 0)
return NULL;
if (!(ctx->input_filter = iptRulesNew("filter", "INPUT")))
@@ -555,7 +547,7 @@
iptRulesFree(ctx->forward_filter);
if (ctx->nat_postrouting)
iptRulesFree(ctx->nat_postrouting);
- free(ctx);
+ VIR_FREE(ctx);
}
/**
diff -r ff6b92c70738 src/lxc_conf.c
--- a/src/lxc_conf.c Fri May 30 10:36:42 2008 -0400
+++ b/src/lxc_conf.c Fri May 30 10:55:44 2008 -0400
@@ -42,7 +42,7 @@
#include "util.h"
#include "uuid.h"
#include "xml.h"
-
+#include "memory.h"
#include "lxc_conf.h"
/* debug macros */
@@ -183,10 +183,10 @@
if (virUUIDParse(res, uuid) < 0) {
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
_("invalid uuid element"));
- free(res);
+ VIR_FREE(res);
return(-1);
}
- free(res);
+ VIR_FREE(res);
}
return(0);
}
@@ -206,15 +206,14 @@
res = virXPathNodeSet("/domain/devices/filesystem", contextPtr, &list);
if (res > 0) {
for (i = 0; i < res; ++i) {
- mountObj = calloc(1, sizeof(lxc_mount_t));
- if (NULL == mountObj) {
+ if (VIR_ALLOC(mountObj) < 0) {
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "mount");
goto parse_complete;
}
rc = lxcParseMountXML(conn, list[i], mountObj);
if (0 > rc) {
- free(mountObj);
+ VIR_FREE(mountObj);
goto parse_complete;
}
@@ -228,7 +227,7 @@
}
prevObj = mountObj;
}
- free(list);
+ VIR_FREE(list);
}
rc = nmounts;
@@ -252,7 +251,7 @@
if (strlen(res) >= PATH_MAX - 1) {
lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR,
_("init string too long"));
- free(res);
+ VIR_FREE(res);
return(-1);
}
@@ -307,7 +306,7 @@
xmlChar *xmlProp = NULL;
lxc_vm_def_t *containerDef;
- if (!(containerDef = calloc(1, sizeof(*containerDef)))) {
+ if (VIR_ALLOC(containerDef) < 0) {
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "containerDef");
return NULL;
}
@@ -339,8 +338,7 @@
_("invalid domain type"));
goto error;
}
- free(xmlProp);
- xmlProp = NULL;
+ VIR_FREE(xmlProp);
if ((xmlProp = xmlGetProp(rootNodePtr, BAD_CAST "id"))) {
if (0 > virStrToLong_i((char*)xmlProp, NULL, 10, &(containerDef->id))) {
@@ -351,8 +349,7 @@
} else {
containerDef->id = -1;
}
- free(xmlProp);
- xmlProp = NULL;
+ VIR_FREE(xmlProp);
if (lxcParseDomainName(conn, &(containerDef->name), contextPtr) < 0) {
goto error;
@@ -385,7 +382,7 @@
return containerDef;
error:
- free(xmlProp);
+ VIR_FREE(xmlProp);
xmlXPathFreeContext(contextPtr);
lxcFreeVMDef(containerDef);
@@ -436,7 +433,7 @@
return vm;
}
- if (!(vm = calloc(1, sizeof(lxc_vm_t)))) {
+ if (VIR_ALLOC(vm) < 0) {
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "vm");
return NULL;
}
@@ -529,7 +526,7 @@
close(fd);
}
- free(xmlDef);
+ VIR_FREE(xmlDef);
return rc;
}
@@ -637,7 +634,7 @@
lxcLoadConfig(driver, file, tempPath, xmlData);
- free(xmlData);
+ VIR_FREE(xmlData);
load_complete:
return rc;
@@ -744,14 +741,14 @@
curMount = vmdef->mounts;
while (curMount) {
nextMount = curMount->next;
- free(curMount);
+ VIR_FREE(curMount);
curMount = nextMount;
}
- free(vmdef->name);
- free(vmdef->init);
- free(vmdef->tty);
- free(vmdef);
+ VIR_FREE(vmdef->name);
+ VIR_FREE(vmdef->init);
+ VIR_FREE(vmdef->tty);
+ VIR_FREE(vmdef);
}
void lxcFreeVMs(lxc_vm_t *vms)
@@ -769,8 +766,8 @@
void lxcFreeVM(lxc_vm_t *vm)
{
lxcFreeVMDef(vm->def);
- free(vm->containerTty);
- free(vm);
+ VIR_FREE(vm->containerTty);
+ VIR_FREE(vm);
}
lxc_vm_t *lxcFindVMByID(const lxc_driver_t *driver, int id)
diff -r ff6b92c70738 src/lxc_container.c
--- a/src/lxc_container.c Fri May 30 10:36:42 2008 -0400
+++ b/src/lxc_container.c Fri May 30 10:55:44 2008 -0400
@@ -35,6 +35,7 @@
#include "lxc_container.h"
#include "lxc_conf.h"
#include "util.h"
+#include "memory.h"
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__)
#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg)
@@ -54,8 +55,8 @@
char* execString;
size_t execStringLen = strlen(vmDef->init) + 1 + 5;
- if (NULL == (execString = calloc(execStringLen, sizeof(char)))) {
- lxcError(NULL, NULL, VIR_ERR_NO_MEMORY,
+ if (VIR_ALLOC_N(execString, execStringLen) < 0) {
+ lxcError(NULL, NULL, VIR_ERR_NO_MEMORY,
_("failed to calloc memory for init string: %s"),
strerror(errno));
goto error_out;
diff -r ff6b92c70738 src/lxc_driver.c
--- a/src/lxc_driver.c Fri May 30 10:36:42 2008 -0400
+++ b/src/lxc_driver.c Fri May 30 10:55:44 2008 -0400
@@ -42,6 +42,7 @@
#include "driver.h"
#include "internal.h"
#include "util.h"
+#include "memory.h"
/* debug macros */
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__)
@@ -85,8 +86,7 @@
char *stack;
int childStatus;
- stack = malloc(getpagesize() * 4);
- if(!stack) {
+ if (VIR_ALLOC_N(stack, getpagesize() * 4) < 0) {
DEBUG0("Unable to allocate stack");
rc = -1;
goto check_complete;
@@ -102,7 +102,7 @@
waitpid(cpid, &childStatus, 0);
}
- free(stack);
+ VIR_FREE(stack);
check_complete:
return rc;
@@ -263,7 +263,7 @@
cleanup:
for (i = 0 ; i < numDoms ; i++) {
- free(names[i]);
+ VIR_FREE(names[i]);
}
return -1;
@@ -398,16 +398,15 @@
int rc = -1;
int flags;
int stacksize = getpagesize() * 4;
- void *stack, *stacktop;
+ char *stack, *stacktop;
/* allocate a stack for the container */
- stack = malloc(stacksize);
- if (!stack) {
+ if (VIR_ALLOC_N(stack, stacksize) < 0) {
lxcError(conn, NULL, VIR_ERR_NO_MEMORY,
_("unable to allocate container stack"));
goto error_exit;
}
- stacktop = (char*)stack + stacksize;
+ stacktop = stack + stacksize;
flags = CLONE_NEWPID|CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWUSER|CLONE_NEWIPC|SIGCHLD;
@@ -572,8 +571,7 @@
goto cleanup;
}
- *ttyName = malloc(sizeof(char) * (strlen(tempTtyName) + 1));
- if (NULL == ttyName) {
+ if (VIR_ALLOC_N(*ttyName, strlen(tempTtyName) + 1) < 0) {
lxcError(conn, NULL, VIR_ERR_NO_MEMORY,
_("unable to allocate container name string"));
goto cleanup;
@@ -1034,8 +1032,7 @@
return -1;
}
- lxc_driver = calloc(1, sizeof(lxc_driver_t));
- if (NULL == lxc_driver) {
+ if (VIR_ALLOC(lxc_driver) < 0) {
return -1;
}
@@ -1062,8 +1059,8 @@
static void lxcFreeDriver(lxc_driver_t *driver)
{
- free(driver->configDir);
- free(driver);
+ VIR_FREE(driver->configDir);
+ VIR_FREE(driver);
}
static int lxcShutdown(void)
diff -r ff6b92c70738 src/openvz_conf.c
--- a/src/openvz_conf.c Fri May 30 10:36:42 2008 -0400
+++ b/src/openvz_conf.c Fri May 30 10:55:44 2008 -0400
@@ -53,6 +53,7 @@
#include "openvz_conf.h"
#include "uuid.h"
#include "buf.h"
+#include "memory.h"
#include <string.h>
@@ -152,23 +153,22 @@
struct ovz_quota *prev = quota;
quota = quota->next;
- free(prev);
+ VIR_FREE(prev);
}
while (ip) {
struct ovz_ip *prev = ip;
ip = ip->next;
- free(prev);
+ VIR_FREE(prev);
}
while (ns) {
struct ovz_ns *prev = ns;
ns = ns->next;
- free(prev);
+ VIR_FREE(prev);
}
- free(def);
- def = NULL;
+ VIR_FREE(def);
}
}
@@ -201,8 +201,7 @@
}
if (vms) {
openvzFreeVMDef(vm->vmdef);
- free(vm);
- vm = NULL;
+ VIR_FREE(vm);
}
}
@@ -217,8 +216,7 @@
if (driver->vms)
for(next = driver->vms->next; driver->vms; driver->vms = next)
openvzFreeVM(driver, driver->vms, 0);
- free(driver);
- driver = NULL;
+ VIR_FREE(driver);
}
struct openvz_vm *
@@ -247,7 +245,7 @@
return vm;
}
- if (!(vm = calloc(1, sizeof(*vm)))) {
+ if (VIR_ALLOC(vm) < 0) {
openvzFreeVMDef(def);
error(conn, VIR_ERR_NO_MEMORY, "vm");
return NULL;
@@ -299,7 +297,7 @@
struct ovz_ip *ovzIp;
struct ovz_ns *ovzNs;
- if (!(def = calloc(1, sizeof(*def)))) {
+ if (VIR_ALLOC(def) < 0) {
error(conn, VIR_ERR_NO_MEMORY, "xmlXPathContext");
return NULL;
}
@@ -328,8 +326,7 @@
error(conn, VIR_ERR_INTERNAL_ERROR, _("invalid domain type attribute"));
goto bail_out;
}
- free(prop);
- prop = NULL;
+ VIR_FREE(prop);
/* Extract domain name */
obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt);
@@ -396,7 +393,7 @@
error(conn, VIR_ERR_INTERNAL_ERROR, errorMessage);
goto bail_out;
}
- if (!(ovzIp = calloc(1, sizeof(*ovzIp)))) {
+ if (VIR_ALLOC(ovzIp) < 0) {
openvzLog(OPENVZ_ERR,
_("Failed to Create Memory for 'ovz_ip' structure"));
goto bail_out;
@@ -478,7 +475,7 @@
error(conn, VIR_ERR_INTERNAL_ERROR, errorMessage);
goto bail_out;
}
- if (!(ovzNs = calloc(1, sizeof(*ovzNs)))) {
+ if (VIR_ALLOC(ovzNs) < 0) {
openvzLog(OPENVZ_ERR,
_("Failed to Create Memory for 'ovz_ns' structure"));
goto bail_out;
@@ -509,7 +506,7 @@
return def;
bail_out:
- free(prop);
+ VIR_FREE(prop);
xmlXPathFreeObject(obj);
xmlXPathFreeContext(ctxt);
openvzFreeVMDef(def);
@@ -539,8 +536,7 @@
}
pnext = &vm;
while(!feof(fp)) {
- *pnext = calloc(1, sizeof(**pnext));
- if(!*pnext) {
+ if (VIR_ALLOC(*pnext) < 0) {
error(conn, VIR_ERR_INTERNAL_ERROR, _("calloc failed"));
goto error;
}
@@ -568,8 +564,7 @@
(*pnext)->vpsid = -1;
}
- vmdef = calloc(1, sizeof(*vmdef));
- if(!vmdef) {
+ if (VIR_ALLOC(vmdef) < 0) {
error(conn, VIR_ERR_INTERNAL_ERROR, _("calloc failed"));
goto error;
}
@@ -581,7 +576,7 @@
if(ret == -1) {
error(conn, VIR_ERR_INTERNAL_ERROR,
_("UUID in config file malformed"));
- free(vmdef);
+ VIR_FREE(vmdef);
goto error;
}
@@ -594,8 +589,8 @@
struct openvz_vm *next;
next = vm->next;
- free(vm->vmdef);
- free(vm);
+ VIR_FREE(vm->vmdef);
+ VIR_FREE(vm);
vm = next;
}
return NULL;
@@ -656,7 +651,7 @@
if (conf_dir == NULL)
return -1;
sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid);
- free(conf_dir);
+ VIR_FREE(conf_dir);
fd = open(conf_file, O_RDWR);
if(fd == -1)
@@ -697,7 +692,7 @@
if (conf_dir == NULL)
return -1;
sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid);
- free(conf_dir);
+ VIR_FREE(conf_dir);
if (openvzGetVPSUUID(vpsid, uuidstr))
return -1;
@@ -741,7 +736,7 @@
dp = opendir(conf_dir);
if(dp == NULL) {
- free(conf_dir);
+ VIR_FREE(conf_dir);
return 0;
}
@@ -753,7 +748,7 @@
openvzSetUUID(vpsid);
}
closedir(dp);
- free(conf_dir);
+ VIR_FREE(conf_dir);
return 0;
}
diff -r ff6b92c70738 src/openvz_driver.c
--- a/src/openvz_driver.c Fri May 30 10:36:42 2008 -0400
+++ b/src/openvz_driver.c Fri May 30 10:55:44 2008 -0400
@@ -49,8 +49,7 @@
#include <stdio.h>
#include <sys/wait.h>
-#include "libvirt/virterror.h"
-
+#include "internal.h"
#include "openvz_driver.h"
#include "event.h"
#include "buf.h"
diff -r ff6b92c70738 src/proxy_internal.c
--- a/src/proxy_internal.c Fri May 30 10:36:42 2008 -0400
+++ b/src/proxy_internal.c Fri May 30 10:55:44 2008 -0400
@@ -28,6 +28,7 @@
#include "proxy_internal.h"
#include "util.h"
#include "xen_unified.h"
+#include "memory.h"
#define STANDALONE
@@ -992,8 +993,7 @@
}
xmllen = ans.len - sizeof (virProxyPacket);
- xml = malloc (xmllen+1);
- if (!xml) {
+ if (VIR_ALLOC_N(xml, xmllen+1) < 0) {
virProxyError (conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
return NULL;
}
@@ -1044,7 +1044,7 @@
return (NULL);
}
xmllen = ans.len - sizeof(virProxyPacket);
- if (!(xml = malloc(xmllen+1))) {
+ if (VIR_ALLOC_N(xml, xmllen+1) < 0) {
virProxyError(domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
return NULL;
}
@@ -1097,7 +1097,7 @@
return (NULL);
}
oslen = ans.len - sizeof(virProxyPacket);
- if (!(ostype = malloc(oslen+1))) {
+ if (VIR_ALLOC_N(ostype, oslen+1) < 0) {
virProxyError(domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
return NULL;
}
diff -r ff6b92c70738 src/remote_internal.c
--- a/src/remote_internal.c Fri May 30 10:36:42 2008 -0400
+++ b/src/remote_internal.c Fri May 30 10:55:44 2008 -0400
@@ -71,6 +71,7 @@
#include "qparams.h"
#include "remote_internal.h"
#include "remote_protocol.h"
+#include "memory.h"
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt,__VA_ARGS__)
#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg)
@@ -638,8 +639,7 @@
// Generate the final command argv[] array.
// ssh -p $port [-l $username] $hostname $netcat -U $sockname [NULL]
- cmd_argv = malloc (nr_args * sizeof (*cmd_argv));
- if (cmd_argv == NULL) {
+ if (VIR_ALLOC_N(cmd_argv, nr_args) < 0) {
error (conn, VIR_ERR_SYSTEM_ERROR, strerror (errno));
goto failed;
}
@@ -702,8 +702,7 @@
// Run the external process.
if (!cmd_argv) {
- cmd_argv = malloc (2 * sizeof (*cmd_argv));
- if (cmd_argv == NULL) {
+ if (VIR_ALLOC_N(cmd_argv, 2) < 0) {
error (conn, VIR_ERR_SYSTEM_ERROR, strerror (errno));
goto failed;
}
@@ -811,8 +810,7 @@
if (inside_daemon)
return VIR_DRV_OPEN_DECLINED;
- priv = calloc (1, sizeof(*priv));
- if (!priv) {
+ if (VIR_ALLOC(priv) < 0) {
error (conn, VIR_ERR_NO_MEMORY, _("struct private_data"));
return VIR_DRV_OPEN_ERROR;
}
@@ -856,7 +854,7 @@
ret = doRemoteOpen(conn, priv, uri, auth, rflags);
if (ret != VIR_DRV_OPEN_SUCCESS) {
conn->privateData = NULL;
- free(priv);
+ VIR_FREE(priv);
} else {
priv->magic = MAGIC;
conn->privateData = priv;
@@ -2268,9 +2266,7 @@
/* Serialise the scheduler parameters. */
args.params.params_len = nparams;
- args.params.params_val = malloc (sizeof (*args.params.params_val)
- * nparams);
- if (args.params.params_val == NULL) {
+ if (VIR_ALLOC_N(args.params.params_val, nparams) < 0) {
error (domain->conn, VIR_ERR_RPC, _("out of memory allocating array"));
return -1;
}
@@ -2400,9 +2396,9 @@
* use the UNIX transport. This handles Xen driver
* which doesn't have its own impl of the network APIs.
*/
- struct private_data *priv = calloc (1, sizeof(*priv));
+ struct private_data *priv;
int ret, rflags = 0;
- if (!priv) {
+ if (VIR_ALLOC(priv) < 0) {
error (conn, VIR_ERR_NO_MEMORY, _("struct private_data"));
return VIR_DRV_OPEN_ERROR;
}
@@ -2415,7 +2411,7 @@
ret = doRemoteOpen(conn, priv, uri, auth, rflags);
if (ret != VIR_DRV_OPEN_SUCCESS) {
conn->networkPrivateData = NULL;
- free(priv);
+ VIR_FREE(priv);
} else {
priv->magic = MAGIC;
priv->localUses = 1;
@@ -2434,7 +2430,7 @@
priv->localUses--;
if (!priv->localUses) {
ret = doRemoteClose(conn, priv);
- free(priv);
+ VIR_FREE(priv);
conn->networkPrivateData = NULL;
}
}
@@ -2806,9 +2802,9 @@
* use the UNIX transport. This handles Xen driver
* which doesn't have its own impl of the network APIs.
*/
- struct private_data *priv = calloc (1, sizeof(struct private_data));
+ struct private_data *priv;
int ret, rflags = 0;
- if (!priv) {
+ if (VIR_ALLOC(priv) < 0) {
error (NULL, VIR_ERR_NO_MEMORY, _("struct private_data"));
return VIR_DRV_OPEN_ERROR;
}
@@ -2821,7 +2817,7 @@
ret = doRemoteOpen(conn, priv, uri, auth, rflags);
if (ret != VIR_DRV_OPEN_SUCCESS) {
conn->storagePrivateData = NULL;
- free(priv);
+ VIR_FREE(priv);
} else {
priv->magic = MAGIC;
priv->localUses = 1;
@@ -2840,7 +2836,7 @@
priv->localUses--;
if (!priv->localUses) {
ret = doRemoteClose(conn, priv);
- free(priv);
+ VIR_FREE(priv);
conn->storagePrivateData = NULL;
}
}
@@ -3559,7 +3555,7 @@
mech = authtype + 5;
if (remoteAuthSASL(conn, priv, in_open, auth, mech) < 0) {
- free(ret.types.types_val);
+ VIR_FREE(ret.types.types_val);
return -1;
}
break;
@@ -3569,7 +3565,7 @@
#if HAVE_POLKIT
case REMOTE_AUTH_POLKIT:
if (remoteAuthPolkit(conn, priv, in_open, auth) < 0) {
- free(ret.types.types_val);
+ VIR_FREE(ret.types.types_val);
return -1;
}
break;
@@ -3585,11 +3581,11 @@
NULL, NULL, NULL, 0, 0,
_("unsupported authentication type %d"),
ret.types.types_val[0]);
- free(ret.types.types_val);
- return -1;
- }
-
- free(ret.types.types_val);
+ VIR_FREE(ret.types.types_val);
+ return -1;
+ }
+
+ VIR_FREE(ret.types.types_val);
return 0;
}
@@ -3618,8 +3614,7 @@
return NULL;
}
- addr = malloc(strlen(host) + 1 + strlen(port) + 1);
- if (!addr) {
+ if (VIR_ALLOC_N(addr, strlen(host) + 1 + strlen(port) + 1) < 0) {
__virRaiseError (NULL, NULL, NULL, VIR_FROM_REMOTE,
VIR_ERR_NO_MEMORY, VIR_ERR_ERROR,
NULL, NULL, NULL, 0, 0,
@@ -3712,9 +3707,9 @@
*/
static sasl_callback_t *remoteAuthMakeCallbacks(int *credtype, int ncredtype)
{
- sasl_callback_t *cbs = calloc(ncredtype+1, sizeof (*cbs));
+ sasl_callback_t *cbs;
int i, n;
- if (!cbs) {
+ if (VIR_ALLOC_N(cbs, ncredtype+1) < 0) {
return NULL;
}
@@ -3749,14 +3744,13 @@
for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++)
; /* empty */
- *cred = calloc(ninteract, sizeof(*cred));
- if (!*cred)
+ if (VIR_ALLOC_N(*cred, ninteract) < 0)
return -1;
for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++) {
(*cred)[ninteract].type = remoteAuthCredSASL2Vir(interact[ninteract].id);
if (!(*cred)[ninteract].type) {
- free(*cred);
+ VIR_FREE(*cred);
return -1;
}
if (interact[ninteract].challenge)
@@ -3775,8 +3769,8 @@
{
int i;
for (i = 0 ; i < ncred ; i++)
- free(cred[i].result);
- free(cred);
+ VIR_FREE(cred[i].result);
+ VIR_FREE(cred);
}
@@ -3944,7 +3938,7 @@
NULL, NULL, NULL, 0, 0,
_("SASL mechanism %s not supported by server"),
wantmech);
- free(iret.mechlist);
+ VIR_FREE(iret.mechlist);
goto cleanup;
}
mechlist = wantmech;
@@ -3963,7 +3957,7 @@
VIR_ERR_AUTH_FAILED, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0,
_("Failed to start SASL negotiation: %d (%s)"),
err, sasl_errdetail(saslconn));
- free(iret.mechlist);
+ VIR_FREE(iret.mechlist);
goto cleanup;
}
@@ -3980,7 +3974,7 @@
VIR_ERR_AUTH_FAILED, VIR_ERR_ERROR,
NULL, NULL, NULL, 0, 0,
"%s", _("Failed to make auth credentials"));
- free(iret.mechlist);
+ VIR_FREE(iret.mechlist);
goto cleanup;
}
/* Run the authentication callback */
@@ -3998,7 +3992,7 @@
0, 0, "%s", msg);
goto cleanup;
}
- free(iret.mechlist);
+ VIR_FREE(iret.mechlist);
if (clientoutlen > REMOTE_AUTH_SASL_DATA_MAX) {
__virRaiseError (in_open ? NULL : conn, NULL, NULL, VIR_FROM_REMOTE,
@@ -4077,8 +4071,7 @@
}
if (serverin) {
- free(serverin);
- serverin = NULL;
+ VIR_FREE(serverin);
}
DEBUG("Client step result %d. Data %d bytes %p", err, clientoutlen, clientout);
@@ -4110,7 +4103,7 @@
/* This server call shows complete, and earlier client step was OK */
if (complete && err == SASL_OK) {
- free(serverin);
+ VIR_FREE(serverin);
break;
}
}
@@ -4140,11 +4133,11 @@
ret = 0;
cleanup:
- free(localAddr);
- free(remoteAddr);
- free(serverin);
-
- free(saslcb);
+ VIR_FREE(localAddr);
+ VIR_FREE(remoteAddr);
+ VIR_FREE(serverin);
+
+ VIR_FREE(saslcb);
remoteAuthFreeCredentials(cred, ncred);
if (ret != 0 && saslconn)
sasl_dispose(&saslconn);
diff -r ff6b92c70738 src/sexpr.c
--- a/src/sexpr.c Fri May 30 10:36:42 2008 -0400
+++ b/src/sexpr.c Fri May 30 10:55:44 2008 -0400
@@ -21,6 +21,7 @@
#include "internal.h"
#include "sexpr.h"
#include "util.h"
+#include "memory.h"
/**
* virSexprError:
@@ -55,8 +56,7 @@
{
struct sexpr *ret;
- ret = (struct sexpr *) malloc(sizeof(*ret));
- if (ret == NULL) {
+ if (VIR_ALLOC(ret) < 0) {
virSexprError(VIR_ERR_NO_MEMORY, _("failed to allocate a node"));
return (NULL);
}
@@ -85,13 +85,13 @@
sexpr_free(sexpr->u.s.cdr);
break;
case SEXPR_VALUE:
- free(sexpr->u.value);
+ VIR_FREE(sexpr->u.value);
break;
case SEXPR_NIL:
break;
}
- free(sexpr);
+ VIR_FREE(sexpr);
errno = serrno;
}
@@ -171,16 +171,23 @@
*
* Internal operation appending a value at the end of an existing list
*/
-static void
+static int
append(struct sexpr *lst, const struct sexpr *value)
{
+ struct sexpr *nil = sexpr_nil();
+
+ if (nil == NULL)
+ return -1;
+
while (lst->kind != SEXPR_NIL) {
lst = lst->u.s.cdr;
}
lst->kind = SEXPR_CONS;
lst->u.s.car = (struct sexpr *) value;
- lst->u.s.cdr = sexpr_nil();
+ lst->u.s.cdr = nil;
+
+ return 0;
}
/**
@@ -198,7 +205,8 @@
return (NULL);
if (value == NULL)
return (lst);
- append(lst, value);
+ if (append(lst, value) < 0)
+ return (NULL);
return (lst);
}
@@ -318,8 +326,11 @@
tmp = _string2sexpr(ptr, &tmp_len);
if (tmp == NULL)
- return NULL;
- append(ret, tmp);
+ goto error;
+ if (append(ret, tmp) < 0) {
+ sexpr_free(tmp);
+ goto error;
+ }
#if 0
if (0) {
char buf[4096];
@@ -351,6 +362,7 @@
if (ret->u.value == NULL) {
virSexprError(VIR_ERR_NO_MEMORY,
_("failed to copy a string"));
+ goto error;
}
if (*ptr == '\'')
@@ -367,6 +379,7 @@
if (ret->u.value == NULL) {
virSexprError(VIR_ERR_NO_MEMORY,
_("failed to copy a string"));
+ goto error;
}
}
diff -r ff6b92c70738 src/storage_backend.c
--- a/src/storage_backend.c Fri May 30 10:36:42 2008 -0400
+++ b/src/storage_backend.c Fri May 30 10:55:44 2008 -0400
@@ -50,6 +50,7 @@
#endif
#include "util.h"
+#include "memory.h"
#include "storage_backend.h"
#include "storage_backend_fs.h"
@@ -237,8 +238,7 @@
vol->target.perms.uid = sb.st_uid;
vol->target.perms.gid = sb.st_gid;
- free(vol->target.perms.label);
- vol->target.perms.label = NULL;
+ VIR_FREE(vol->target.perms.label);
#if HAVE_SELINUX
if (fgetfilecon(fd, &filecon) == -1) {
@@ -310,9 +310,8 @@
if (dent->d_name[0] == '.')
continue;
- stablepath = malloc(strlen(pool->def->target.path) +
- 1 + strlen(dent->d_name) + 1);
- if (stablepath == NULL) {
+ if (VIR_ALLOC_N(stablepath, strlen(pool->def->target.path) +
+ 1 + strlen(dent->d_name) + 1) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("path"));
closedir(dh);
return NULL;
@@ -327,7 +326,7 @@
return stablepath;
}
- free(stablepath);
+ VIR_FREE(stablepath);
}
closedir(dh);
@@ -365,7 +364,7 @@
char **groups;
/* Compile all regular expressions */
- if ((reg = calloc(nregex, sizeof(*reg))) == NULL) {
+ if (VIR_ALLOC_N(reg, nregex) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("regex"));
return -1;
}
@@ -379,7 +378,7 @@
_("Failed to compile regex %s"), error);
for (j = 0 ; j <= i ; j++)
regfree(®[j]);
- free(reg);
+ VIR_FREE(reg);
return -1;
}
@@ -390,12 +389,12 @@
}
/* Storage for matched variables */
- if ((groups = calloc(totgroups, sizeof(*groups))) == NULL) {
+ if (VIR_ALLOC_N(groups, totgroups) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("regex groups"));
goto cleanup;
}
- if ((vars = calloc(maxvars+1, sizeof(*vars))) == NULL) {
+ if (VIR_ALLOC_N(vars, maxvars+1) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("regex groups"));
goto cleanup;
@@ -445,7 +444,7 @@
/* Release matches & restart to matching the first regex */
for (j = 0 ; j < totgroups ; j++) {
- free(groups[j]);
+ VIR_FREE(groups[j]);
groups[j] = NULL;
}
maxReg = 0;
@@ -460,15 +459,15 @@
cleanup:
if (groups) {
for (j = 0 ; j < totgroups ; j++)
- free(groups[j]);
- free(groups);
+ VIR_FREE(groups[j]);
+ VIR_FREE(groups);
}
- free(vars);
+ VIR_FREE(vars);
for (i = 0 ; i < nregex ; i++)
regfree(®[i]);
- free(reg);
+ VIR_FREE(reg);
if (list)
fclose(list);
@@ -534,8 +533,7 @@
if (n_columns == 0)
return -1;
- if (n_columns > SIZE_MAX / sizeof *v
- || (v = malloc (n_columns * sizeof *v)) == NULL) {
+ if (VIR_ALLOC_N(v, n_columns) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("n_columns too large"));
return -1;
diff -r ff6b92c70738 src/storage_backend_disk.c
--- a/src/storage_backend_disk.c Fri May 30 10:36:42 2008 -0400
+++ b/src/storage_backend_disk.c Fri May 30 10:55:44 2008 -0400
@@ -26,6 +26,7 @@
#include "internal.h"
#include "storage_backend_disk.h"
#include "util.h"
+#include "memory.h"
enum {
VIR_STORAGE_POOL_DISK_DOS = 0,
@@ -174,7 +175,7 @@
char *tmp, *devpath;
if (vol == NULL) {
- if ((vol = calloc(1, sizeof(virStorageVolDef))) == NULL) {
+ if (VIR_ALLOC(vol) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, _("volume"));
return -1;
}
@@ -211,8 +212,7 @@
return -1;
if (devpath != vol->target.path)
- free(devpath);
- devpath = NULL;
+ VIR_FREE(devpath);
}
if (vol->key == NULL) {
@@ -224,8 +224,7 @@
}
if (vol->source.extents == NULL) {
- if ((vol->source.extents =
- calloc(1, sizeof(*(vol->source.extents)))) == NULL) {
+ if (VIR_ALLOC(vol->source.extents) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
_("volume extents"));
return -1;
@@ -275,16 +274,15 @@
virStoragePoolObjPtr pool,
char **const groups)
{
- virStoragePoolSourceDeviceExtentPtr tmp;
virStoragePoolSourceDevicePtr dev = &pool->def->source.devices[0];
- if ((tmp = realloc(dev->freeExtents,
- sizeof(*tmp) * (dev->nfreeExtent+1))) == NULL)
+ if (VIR_REALLOC_N(dev->freeExtents,
+ dev->nfreeExtent + 1) < 0)
return -1;
- dev->freeExtents = tmp;
memset(dev->freeExtents +
- dev->nfreeExtent, 0, sizeof(*tmp));
+ dev->nfreeExtent, 0,
+ sizeof(dev->freeExtents[0]));
if (virStrToLong_ull(groups[3], NULL, 10,
&dev->freeExtents[dev->nfreeExtent].start) < 0)
@@ -388,9 +386,8 @@
virStorageBackendDiskRefreshPool(virConnectPtr conn,
virStoragePoolObjPtr pool)
{
- free(pool->def->source.devices[0].freeExtents);
+ VIR_FREE(pool->def->source.devices[0].freeExtents);
pool->def->source.devices[0].nfreeExtent = 0;
- pool->def->source.devices[0].freeExtents = NULL;
return virStorageBackendDiskReadPartitions(conn, pool, NULL);
}
@@ -476,9 +473,8 @@
return -1;
/* Blow away free extent info, as we're about to re-populate it */
- free(pool->def->source.devices[0].freeExtents);
+ VIR_FREE(pool->def->source.devices[0].freeExtents);
pool->def->source.devices[0].nfreeExtent = 0;
- pool->def->source.devices[0].freeExtents = NULL;
/* Fetch actual extent info */
if (virStorageBackendDiskReadPartitions(conn, pool, vol) < 0)
diff -r ff6b92c70738 src/storage_backend_fs.c
--- a/src/storage_backend_fs.c Fri May 30 10:36:42 2008 -0400
+++ b/src/storage_backend_fs.c Fri May 30 10:55:44 2008 -0400
@@ -40,8 +40,7 @@
#include "storage_backend_fs.h"
#include "storage_conf.h"
#include "util.h"
-#include "config.h"
-
+#include "memory.h"
enum {
VIR_STORAGE_POOL_FS_AUTO = 0,
@@ -530,25 +529,27 @@
}
if (pool->def->type == VIR_STORAGE_POOL_NETFS) {
- src = malloc(strlen(pool->def->source.host.name) +
- 1 + strlen(pool->def->source.dir) + 1);
+ if (VIR_ALLOC_N(src, strlen(pool->def->source.host.name) +
+ 1 + strlen(pool->def->source.dir) + 1) < 0) {
+ virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("source"));
+ return -1;
+ }
strcpy(src, pool->def->source.host.name);
strcat(src, ":");
strcat(src, pool->def->source.dir);
} else {
- src = strdup(pool->def->source.devices[0].path);
- }
- if (src == NULL) {
- virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("source"));
- return -1;
+ if ((src = strdup(pool->def->source.devices[0].path)) == NULL) {
+ virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("source"));
+ return -1;
+ }
}
mntargv[3] = src;
if (virRun(conn, (char**)mntargv, NULL) < 0) {
- free(src);
+ VIR_FREE(src);
return -1;
}
- free(src);
+ VIR_FREE(src);
return 0;
}
@@ -679,8 +680,7 @@
virStorageVolDefPtr vol;
int ret;
- vol = calloc(1, sizeof(virStorageVolDef));
- if (vol == NULL) {
+ if (VIR_ALLOC(vol) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("volume"));
goto cleanup;
@@ -688,18 +688,17 @@
vol->name = strdup(ent->d_name);
if (vol->name == NULL) {
- free(vol);
+ VIR_FREE(vol);
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("volume name"));
goto cleanup;
}
vol->target.format = VIR_STORAGE_VOL_RAW; /* Real value is filled in during probe */
- vol->target.path = malloc(strlen(pool->def->target.path) +
- 1 + strlen(vol->name) + 1);
- if (vol->target.path == NULL) {
- free(vol->target.path);
- free(vol);
+ if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) +
+ 1 + strlen(vol->name) + 1) < 0) {
+ VIR_FREE(vol->target.path);
+ VIR_FREE(vol);
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("volume name"));
goto cleanup;
@@ -708,19 +707,19 @@
strcat(vol->target.path, "/");
strcat(vol->target.path, vol->name);
if ((vol->key = strdup(vol->target.path)) == NULL) {
- free(vol->name);
- free(vol->target.path);
- free(vol);
+ VIR_FREE(vol->name);
+ VIR_FREE(vol->target.path);
+ VIR_FREE(vol);
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("volume key"));
goto cleanup;
}
if ((ret = virStorageBackendProbeFile(conn, vol) < 0)) {
- free(vol->key);
- free(vol->name);
- free(vol->target.path);
- free(vol);
+ VIR_FREE(vol->key);
+ VIR_FREE(vol->name);
+ VIR_FREE(vol->target.path);
+ VIR_FREE(vol);
if (ret == -1)
goto cleanup;
else
@@ -821,9 +820,8 @@
{
int fd;
- vol->target.path = malloc(strlen(pool->def->target.path) +
- 1 + strlen(vol->name) + 1);
- if (vol->target.path == NULL) {
+ if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) +
+ 1 + strlen(vol->name) + 1) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("target"));
return -1;
}
diff -r ff6b92c70738 src/storage_backend_iscsi.c
--- a/src/storage_backend_iscsi.c Fri May 30 10:36:42 2008 -0400
+++ b/src/storage_backend_iscsi.c Fri May 30 10:55:44 2008 -0400
@@ -37,6 +37,7 @@
#include "internal.h"
#include "storage_backend_iscsi.h"
#include "util.h"
+#include "memory.h"
static int
virStorageBackendISCSITargetIP(virConnectPtr conn,
@@ -253,7 +254,7 @@
snprintf(lunid, sizeof(lunid)-1, "lun-%s", groups[3]);
- if ((vol = calloc(1, sizeof(virStorageVolDef))) == NULL) {
+ if (VIR_ALLOC(vol) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume"));
goto cleanup;
}
@@ -263,14 +264,13 @@
goto cleanup;
}
- if ((devpath = malloc(5 + strlen(dev) + 1)) == NULL) {
+ if (VIR_ALLOC_N(devpath, 5 + strlen(dev) + 1) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("devpath"));
goto cleanup;
}
strcpy(devpath, "/dev/");
strcat(devpath, dev);
- free(dev);
- dev = NULL;
+ VIR_FREE(dev);
/* It can take a little while between logging into the ISCSI
* server and udev creating the /dev nodes, so if we get ENOENT
* we must retry a few times - they should eventually appear.
@@ -303,8 +303,7 @@
goto cleanup;
if (devpath != vol->target.path)
- free(devpath);
- devpath = NULL;
+ VIR_FREE(devpath);
if (virStorageBackendUpdateVolInfoFD(conn, vol, fd, 1) < 0)
goto cleanup;
@@ -330,9 +329,9 @@
cleanup:
if (fd != -1) close(fd);
- free(devpath);
+ VIR_FREE(devpath);
virStorageVolDefFree(vol);
- free(dev);
+ VIR_FREE(dev);
return -1;
}
@@ -422,8 +421,7 @@
ipaddr, sizeof(ipaddr)) < 0)
return NULL;
- portal = malloc(strlen(ipaddr) + 1 + 4 + 2 + 1);
- if (portal == NULL) {
+ if (VIR_ALLOC_N(portal, strlen(ipaddr) + 1 + 4 + 2 + 1) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("portal"));
return NULL;
}
@@ -457,10 +455,10 @@
if ((portal = virStorageBackendISCSIPortal(conn, pool)) == NULL)
return -1;
if (virStorageBackendISCSILogin(conn, pool, portal) < 0) {
- free(portal);
+ VIR_FREE(portal);
return -1;
}
- free(portal);
+ VIR_FREE(portal);
return 0;
}
@@ -478,12 +476,12 @@
goto cleanup;
if (virStorageBackendISCSIFindLUNs(conn, pool, session) < 0)
goto cleanup;
- free(session);
+ VIR_FREE(session);
return 0;
cleanup:
- free(session);
+ VIR_FREE(session);
return -1;
}
@@ -498,10 +496,10 @@
return -1;
if (virStorageBackendISCSILogout(conn, pool, portal) < 0) {
- free(portal);
+ VIR_FREE(portal);
return -1;
}
- free(portal);
+ VIR_FREE(portal);
return 0;
}
diff -r ff6b92c70738 src/storage_backend_logical.c
--- a/src/storage_backend_logical.c Fri May 30 10:36:42 2008 -0400
+++ b/src/storage_backend_logical.c Fri May 30 10:55:44 2008 -0400
@@ -35,7 +35,7 @@
#include "storage_backend_logical.h"
#include "storage_conf.h"
#include "util.h"
-
+#include "memory.h"
#define PV_BLANK_SECTOR_SIZE 512
@@ -97,7 +97,6 @@
void *data)
{
virStorageVolDefPtr vol = NULL;
- virStorageVolSourceExtentPtr tmp;
unsigned long long offset, size, length;
/* See if we're only looking for a specific volume */
@@ -113,7 +112,7 @@
/* Or a completely new volume */
if (vol == NULL) {
- if ((vol = calloc(1, sizeof(*vol))) == NULL) {
+ if (VIR_ALLOC(vol) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume"));
return -1;
}
@@ -129,8 +128,8 @@
}
if (vol->target.path == NULL) {
- if ((vol->target.path = malloc(strlen(pool->def->target.path) +
- 1 + strlen(vol->name) + 1)) == NULL) {
+ if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) +
+ 1 + strlen(vol->name) + 1) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume"));
return -1;
}
@@ -150,12 +149,11 @@
/* Finally fill in extents information */
- if ((tmp = realloc(vol->source.extents, sizeof(*tmp)
- * (vol->source.nextent + 1))) == NULL) {
+ if (VIR_REALLOC_N(vol->source.extents,
+ vol->source.nextent + 1) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("extents"));
return -1;
}
- vol->source.extents = tmp;
if ((vol->source.extents[vol->source.nextent].path =
strdup(groups[2])) == NULL) {
@@ -266,7 +264,7 @@
memset(zeros, 0, sizeof(zeros));
/* XXX multiple pvs */
- if ((vgargv = malloc(sizeof(char*) * (1))) == NULL) {
+ if (VIR_ALLOC_N(vgargv, 1) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("command line"));
return -1;
}
@@ -318,12 +316,12 @@
if (virRun(conn, (char**)vgargv, NULL) < 0)
goto cleanup;
- free(vgargv);
+ VIR_FREE(vgargv);
return 0;
cleanup:
- free(vgargv);
+ VIR_FREE(vgargv);
return -1;
}
diff -r ff6b92c70738 src/storage_conf.c
--- a/src/storage_conf.c Fri May 30 10:36:42 2008 -0400
+++ b/src/storage_conf.c Fri May 30 10:55:44 2008 -0400
@@ -45,6 +45,7 @@
#include "uuid.h"
#include "buf.h"
#include "util.h"
+#include "memory.h"
#define virStorageLog(msg...) fprintf(stderr, msg)
@@ -70,40 +71,40 @@
void
virStorageVolDefFree(virStorageVolDefPtr def) {
int i;
- free(def->name);
- free(def->key);
+ VIR_FREE(def->name);
+ VIR_FREE(def->key);
for (i = 0 ; i < def->source.nextent ; i++) {
- free(def->source.extents[i].path);
+ VIR_FREE(def->source.extents[i].path);
}
- free(def->source.extents);
+ VIR_FREE(def->source.extents);
- free(def->target.path);
- free(def->target.perms.label);
- free(def);
+ VIR_FREE(def->target.path);
+ VIR_FREE(def->target.perms.label);
+ VIR_FREE(def);
}
void
virStoragePoolDefFree(virStoragePoolDefPtr def) {
int i;
- free(def->name);
- free(def->source.host.name);
+ VIR_FREE(def->name);
+ VIR_FREE(def->source.host.name);
for (i = 0 ; i < def->source.ndevice ; i++) {
- free(def->source.devices[i].freeExtents);
- free(def->source.devices[i].path);
+ VIR_FREE(def->source.devices[i].freeExtents);
+ VIR_FREE(def->source.devices[i].path);
}
- free(def->source.devices);
- free(def->source.dir);
+ VIR_FREE(def->source.devices);
+ VIR_FREE(def->source.dir);
if (def->source.authType == VIR_STORAGE_POOL_AUTH_CHAP) {
- free(def->source.auth.chap.login);
- free(def->source.auth.chap.passwd);
+ VIR_FREE(def->source.auth.chap.login);
+ VIR_FREE(def->source.auth.chap.passwd);
}
- free(def->target.path);
- free(def->target.perms.label);
- free(def);
+ VIR_FREE(def->target.path);
+ VIR_FREE(def->target.perms.label);
+ VIR_FREE(def);
}
@@ -114,9 +115,9 @@
if (obj->newDef)
virStoragePoolDefFree(obj->newDef);
- free(obj->configFile);
- free(obj->autostartLink);
- free(obj);
+ VIR_FREE(obj->configFile);
+ VIR_FREE(obj->autostartLink);
+ VIR_FREE(obj);
}
void
@@ -225,8 +226,11 @@
char *uuid = NULL;
char *authType = NULL;
- if ((ret = calloc(1, sizeof(virStoragePoolDef))) == NULL)
+ if (VIR_ALLOC(ret) < 0) {
+ virStorageReportError(conn, VIR_ERR_NO_MEMORY,
+ "%s", _("cannot allocate storage pool"));
return NULL;
+ }
if (STRNEQ((const char *)root->name, "pool")) {
virStorageReportError(conn, VIR_ERR_XML_ERROR,
@@ -263,17 +267,16 @@
"%s", _("malformed uuid element"));
goto cleanup;
}
- free(uuid);
- uuid = NULL;
+ VIR_FREE(uuid);
}
if (options->formatFromString) {
char *format = virXPathString("string(/pool/source/format/@type)", ctxt);
if ((ret->source.format = (options->formatFromString)(conn, format)) < 0) {
- free(format);
+ VIR_FREE(format);
goto cleanup;
}
- free(format);
+ VIR_FREE(format);
}
if (options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_HOST) {
@@ -292,22 +295,22 @@
"%s", _("cannot extract source devices"));
goto cleanup;
}
- if ((ret->source.devices = calloc(nsource, sizeof(*ret->source.devices))) == NULL) {
- free(nodeset);
+ if (VIR_ALLOC_N(ret->source.devices, nsource) < 0) {
+ VIR_FREE(nodeset);
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("device"));
goto cleanup;
}
for (i = 0 ; i < nsource ; i++) {
xmlChar *path = xmlGetProp(nodeset[i], BAD_CAST "path");
if (path == NULL) {
- free(nodeset);
+ VIR_FREE(nodeset);
virStorageReportError(conn, VIR_ERR_XML_ERROR,
"%s", _("missing source device path"));
goto cleanup;
}
ret->source.devices[i].path = (char *)path;
}
- free(nodeset);
+ VIR_FREE(nodeset);
ret->source.ndevice = nsource;
}
if (options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DIR) {
@@ -329,12 +332,10 @@
virStorageReportError(conn, VIR_ERR_XML_ERROR,
_("unknown auth type '%s'"),
(const char *)authType);
- free(authType);
- authType = NULL;
+ VIR_FREE(authType);
goto cleanup;
}
- free(authType);
- authType = NULL;
+ VIR_FREE(authType);
}
if (ret->source.authType == VIR_STORAGE_POOL_AUTH_CHAP) {
@@ -354,7 +355,7 @@
return ret;
cleanup:
- free(uuid);
+ VIR_FREE(uuid);
xmlFree(type);
virStoragePoolDefFree(ret);
return NULL;
@@ -649,8 +650,11 @@
if (options == NULL)
return NULL;
- if ((ret = calloc(1, sizeof(virStorageVolDef))) == NULL)
+ if (VIR_ALLOC(ret) < 0) {
+ virStorageReportError(conn, VIR_ERR_NO_MEMORY,
+ "%s", _("cannot allocate storage vol"));
return NULL;
+ }
if (STRNEQ((const char *)root->name, "volume")) {
virStorageReportError(conn, VIR_ERR_XML_ERROR,
@@ -677,20 +681,16 @@
}
if (virStorageSize(conn, unit, capacity, &ret->capacity) < 0)
goto cleanup;
- free(capacity);
- capacity = NULL;
- free(unit);
- unit = NULL;
+ VIR_FREE(capacity);
+ VIR_FREE(unit);
allocation = virXPathString("string(/volume/allocation)", ctxt);
if (allocation) {
unit = virXPathString("string(/volume/allocation/@unit)", ctxt);
if (virStorageSize(conn, unit, allocation, &ret->allocation) < 0)
goto cleanup;
- free(allocation);
- allocation = NULL;
- free(unit);
- unit = NULL;
+ VIR_FREE(allocation);
+ VIR_FREE(unit);
} else {
ret->allocation = ret->capacity;
}
@@ -699,10 +699,10 @@
if (options->formatFromString) {
char *format = virXPathString("string(/volume/target/format/@type)", ctxt);
if ((ret->target.format = (options->formatFromString)(conn, format)) < 0) {
- free(format);
+ VIR_FREE(format);
goto cleanup;
}
- free(format);
+ VIR_FREE(format);
}
if (virStorageVolDefParsePerms(conn, ctxt, &ret->target.perms) < 0)
@@ -711,9 +711,9 @@
return ret;
cleanup:
- free(allocation);
- free(capacity);
- free(unit);
+ VIR_FREE(allocation);
+ VIR_FREE(capacity);
+ VIR_FREE(unit);
virStorageVolDefFree(ret);
return NULL;
}
@@ -772,6 +772,7 @@
virStorageVolDefPtr def) {
virStorageBackendVolOptionsPtr options;
virBuffer buf = VIR_BUFFER_INITIALIZER;
+ char *tmp;
options = virStorageBackendVolOptionsForType(pool->type);
if (options == NULL)
@@ -849,7 +850,8 @@
no_memory:
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("xml"));
cleanup:
- free(virBufferContentAndReset(&buf));
+ tmp = virBufferContentAndReset(&buf);
+ VIR_FREE(tmp);
return NULL;
}
@@ -955,7 +957,7 @@
return pool;
}
- if (!(pool = calloc(1, sizeof(virStoragePoolObj)))) {
+ if (VIR_ALLOC(pool) < 0) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("pool"));
return NULL;
@@ -1063,7 +1065,7 @@
virStoragePoolObjLoad(driver, entry->d_name, path, xml, autostartLink);
- free(xml);
+ VIR_FREE(xml);
}
closedir(dir);
@@ -1108,15 +1110,13 @@
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
"%s", _("cannot construct "
"autostart link path"));
- free(pool->configFile);
- pool->configFile = NULL;
+ VIR_FREE(pool->configFile);
return -1;
}
if (!(pool->autostartLink = strdup(path))) {
virStorageReportError(conn, VIR_ERR_NO_MEMORY,
"%s", _("config file"));
- free(pool->configFile);
- pool->configFile = NULL;
+ VIR_FREE(pool->configFile);
return -1;
}
}
@@ -1157,7 +1157,7 @@
if (fd != -1)
close(fd);
- free(xml);
+ VIR_FREE(xml);
return ret;
}
diff -r ff6b92c70738 src/storage_driver.c
--- a/src/storage_driver.c Fri May 30 10:36:42 2008 -0400
+++ b/src/storage_driver.c Fri May 30 10:55:44 2008 -0400
@@ -37,7 +37,7 @@
#include "util.h"
#include "storage_driver.h"
#include "storage_conf.h"
-
+#include "memory.h"
#include "storage_backend.h"
#define storageLog(msg...) fprintf(stderr, msg)
@@ -104,9 +104,8 @@
char *base = NULL;
char driverConf[PATH_MAX];
- if (!(driverState = calloc(1, sizeof(virStorageDriverState)))) {
+ if (VIR_ALLOC(driverState) < 0)
return -1;
- }
if (!uid) {
if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL)
diff -r ff6b92c70738 src/xen_internal.c
--- a/src/xen_internal.c Fri May 30 10:36:42 2008 -0400
+++ b/src/xen_internal.c Fri May 30 10:55:44 2008 -0400
@@ -48,6 +48,7 @@
#include "buf.h"
#include "capabilities.h"
+#include "memory.h"
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__)
#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg)
@@ -223,17 +224,17 @@
#define XEN_GETDOMAININFOLIST_ALLOC(domlist, size) \
(hypervisor_version < 2 ? \
- ((domlist.v0 = malloc(sizeof(*domlist.v0)*(size))) != NULL) : \
+ (VIR_ALLOC_N(domlist.v0, (size)) == 0) : \
(dom_interface_version < 5 ? \
- ((domlist.v2 = malloc(sizeof(*domlist.v2)*(size))) != NULL) : \
- ((domlist.v2d5 = malloc(sizeof(*domlist.v2d5)*(size))) != NULL)))
+ (VIR_ALLOC_N(domlist.v2, (size)) == 0) : \
+ (VIR_ALLOC_N(domlist.v2d5, (size)) == 0)))
-#define XEN_GETDOMAININFOLIST_FREE(domlist) \
- (hypervisor_version < 2 ? \
- free(domlist.v0) : \
- (dom_interface_version < 5 ? \
- free(domlist.v2) : \
- free(domlist.v2d5)))
+#define XEN_GETDOMAININFOLIST_FREE(domlist) \
+ (hypervisor_version < 2 ? \
+ VIR_FREE(domlist.v0) : \
+ (dom_interface_version < 5 ? \
+ VIR_FREE(domlist.v2) : \
+ VIR_FREE(domlist.v2d5)))
#define XEN_GETDOMAININFOLIST_CLEAR(domlist, size) \
(hypervisor_version < 2 ? \
@@ -796,8 +797,7 @@
{
char *msg_s;
- msg_s = malloc (strlen (msg) + 10);
- if (msg_s) {
+ if (VIR_ALLOC_N(msg_s, strlen (msg) + 10) == 0) {
strcpy (msg_s, msg);
strcat (msg_s, ": %s");
}
@@ -1659,8 +1659,7 @@
/* The allocated memory to cpumap must be 'sizeof(uint64_t)' byte *
* for Xen, and also nr_cpus must be 'sizeof(uint64_t) * 8' */
if (maplen < 8) {
- new = calloc(1, sizeof(uint64_t));
- if (!new) {
+ if (VIR_ALLOC_N(new, sizeof(uint64_t)) < 0) {
virXenErrorFunc(NULL, VIR_ERR_NO_MEMORY, __FUNCTION__,
"allocating private data", 0);
return (-1);
@@ -1683,7 +1682,7 @@
op.u.setvcpumapd5.cpumap.nr_cpus = nr_cpus;
}
ret = xenHypervisorDoV2Dom(handle, &op);
- free(new);
+ VIR_FREE(new);
if (unlock_pages(cpumap, maplen) < 0) {
virXenError(NULL, VIR_ERR_XEN_CALL, " release", maplen);
@@ -1985,8 +1984,7 @@
*/
hypervisor_version = 2;
- ipt = malloc(sizeof(*ipt));
- if (ipt == NULL){
+ if (VIR_ALLOC(ipt) < 0) {
virXenError(NULL, VIR_ERR_NO_MEMORY, __FUNCTION__, 0);
return(-1);
}
@@ -2053,13 +2051,13 @@
virXenError(NULL, VIR_ERR_XEN_CALL, " ioctl ", IOCTL_PRIVCMD_HYPERCALL);
close(fd);
in_init = 0;
- free(ipt);
+ VIR_FREE(ipt);
return(-1);
done:
close(fd);
in_init = 0;
- free(ipt);
+ VIR_FREE(ipt);
return(0);
}
@@ -2647,7 +2645,7 @@
ret = virGetDomain(conn, name, XEN_GETDOMAININFO_UUID(dominfo));
if (ret)
ret->id = id;
- free(name);
+ VIR_FREE(name);
return ret;
}
@@ -2714,7 +2712,7 @@
ret = virGetDomain(conn, name, uuid);
if (ret)
ret->id = id;
- free(name);
+ VIR_FREE(name);
return ret;
}
#endif
diff -r ff6b92c70738 src/xen_unified.c
--- a/src/xen_unified.c Fri May 30 10:36:42 2008 -0400
+++ b/src/xen_unified.c Fri May 30 10:55:44 2008 -0400
@@ -40,6 +40,7 @@
#include "xm_internal.h"
#include "xml.h"
#include "util.h"
+#include "memory.h"
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt,__VA_ARGS__)
#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg)
@@ -172,15 +173,12 @@
if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0)
return(NULL);
- cpulist = calloc(nb_cpu, sizeof(*cpulist));
- if (cpulist == NULL)
+ if (VIR_ALLOC_N(cpulist, nb_cpu) < 0)
goto done;
- cpuinfo = malloc(sizeof(*cpuinfo) * nb_vcpu);
- if (cpuinfo == NULL)
+ if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0)
goto done;
cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo));
- cpumap = (unsigned char *) calloc(nb_vcpu, cpumaplen);
- if (cpumap == NULL)
+ if (VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0)
goto done;
if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu,
@@ -202,9 +200,9 @@
}
done:
- free(cpulist);
- free(cpumap);
- free(cpuinfo);
+ VIR_FREE(cpulist);
+ VIR_FREE(cpumap);
+ VIR_FREE(cpuinfo);
return(res);
}
@@ -262,8 +260,7 @@
return VIR_DRV_OPEN_DECLINED;
/* Allocate per-connection private data. */
- priv = calloc (1, sizeof *priv);
- if (!priv) {
+ if (VIR_ALLOC(priv) < 0) {
xenUnifiedError (NULL, VIR_ERR_NO_MEMORY, "allocating private data");
return VIR_DRV_OPEN_ERROR;
}
@@ -342,7 +339,7 @@
DEBUG0("Failed to activate a mandatory sub-driver");
for (i = 0 ; i < XEN_UNIFIED_NR_DRIVERS ; i++)
if (priv->opened[i]) drivers[i]->close(conn);
- free(priv);
+ VIR_FREE(priv);
return VIR_DRV_OPEN_ERROR;
}
@@ -961,7 +958,7 @@
char *cpus, *res;
cpus = xenDomainUsedCpus(dom);
res = xenDaemonDomainDumpXML(dom, flags, cpus);
- free(cpus);
+ VIR_FREE(cpus);
return(res);
}
if (priv->opened[XEN_UNIFIED_PROXY_OFFSET])
diff -r ff6b92c70738 src/xend_internal.c
--- a/src/xend_internal.c Fri May 30 10:36:42 2008 -0400
+++ b/src/xend_internal.c Fri May 30 10:55:44 2008 -0400
@@ -24,7 +24,6 @@
#include <stdbool.h>
#include <math.h>
#include <stdarg.h>
-#include <malloc.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
diff -r ff6b92c70738 src/xmlrpc.c
--- a/src/xmlrpc.c Fri May 30 10:36:42 2008 -0400
+++ b/src/xmlrpc.c Fri May 30 10:55:44 2008 -0400
@@ -12,6 +12,7 @@
#include "xmlrpc.h"
#include "internal.h"
+#include "memory.h"
#include <libxml/nanohttp.h>
@@ -47,9 +48,8 @@
static xmlRpcValuePtr xmlRpcValueNew(xmlRpcValueType type)
{
- xmlRpcValuePtr ret = malloc(sizeof(*ret));
-
- if (!ret)
+ xmlRpcValuePtr ret = NULL;
+ if (VIR_ALLOC(ret) < 0)
xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate value"), sizeof(*ret));
else
ret->kind = type;
@@ -115,7 +115,7 @@
if (ret && value)
ret->value.integer = atoi(value);
- free(value);
+ VIR_FREE(value);
return ret;
}
@@ -130,7 +130,7 @@
ret->value.boolean = true;
else
ret->value.boolean = false;
- free(value);
+ VIR_FREE(value);
return ret;
}
@@ -141,7 +141,7 @@
if (ret && value)
ret->value.real = atof(value);
- free(value);
+ VIR_FREE(value);
return ret;
}
@@ -158,11 +158,10 @@
for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur))
n_elements += 1;
- elems = malloc(n_elements * sizeof(*elems));
- if (!elems) {
+ if (VIR_ALLOC_N(elems, n_elements) < 0) {
xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate value array"),
n_elements * sizeof(*elems));
- free(ret);
+ VIR_FREE(ret);
return NULL;
}
n_elements = 0;
@@ -179,10 +178,10 @@
static xmlRpcValueDictElementPtr xmlRpcValueUnmarshalDictElement(xmlNodePtr node)
{
- xmlRpcValueDictElementPtr ret = malloc(sizeof(*ret));
+ xmlRpcValueDictElementPtr ret;
xmlNodePtr cur;
- if (!ret) {
+ if (VIR_ALLOC(ret) < 0) {
xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate dict"), sizeof(*ret));
return NULL;
}
@@ -195,10 +194,10 @@
ret->value = xmlRpcValueUnmarshal(cur);
} else {
xmlRpcError(VIR_ERR_XML_ERROR, _("unexpected dict node"), 0);
- free(ret->name);
+ VIR_FREE(ret->name);
if (ret->value)
xmlRpcValueFree(ret->value);
- free(ret);
+ VIR_FREE(ret);
return NULL;
}
}
@@ -283,26 +282,26 @@
case XML_RPC_ARRAY:
for (i = 0; i < value->value.array.n_elements; i++)
xmlRpcValueFree(value->value.array.elements[i]);
- free(value->value.array.elements);
+ VIR_FREE(value->value.array.elements);
break;
case XML_RPC_STRUCT:
next = value->value.dict.root;
while (next) {
cur = next;
next = next->next;
- free(cur->name);
+ VIR_FREE(cur->name);
xmlRpcValueFree(cur->value);
- free(cur);
+ VIR_FREE(cur);
}
break;
case XML_RPC_STRING:
- free(value->value.string);
+ VIR_FREE(value->value.string);
break;
default:
break;
}
- free(value);
+ VIR_FREE(value);
}
void xmlRpcValueMarshal(xmlRpcValuePtr value, virBufferPtr buf, int indent)
@@ -436,15 +435,14 @@
}
len = xmlNanoHTTPContentLength(cxt);
- response = malloc(len + 1);
- if (response == NULL) {
+ if (VIR_ALLOC_N(response, len + 1) < 0) {
xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate response"), len);
goto error;
}
ret = xmlNanoHTTPRead(cxt, response, len);
if (ret != len) {
errno = EINVAL;
- free(response);
+ VIR_FREE(response);
response = NULL;
xmlRpcError(VIR_ERR_POST_FAILED, _("read response"), 0);
}
@@ -455,7 +453,7 @@
serrno = errno;
if (cxt) {
xmlNanoHTTPClose(cxt);
- free(contentType);
+ VIR_FREE(contentType);
}
errno = serrno;
@@ -477,7 +475,7 @@
if (value->value.array.elements[i]->kind == XML_RPC_STRING)
size += strlen(value->value.array.elements[i]->value.string) + 1;
- if (!(ptr = malloc(size))) {
+ if (VIR_ALLOC_N(ptr, size) < 0) {
xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate string array"), size);
return NULL;
}
@@ -507,7 +505,7 @@
int i;
*argc = strlen(fmt);
- if (!(argv = malloc(sizeof(*argv) * *argc))) {
+ if (VIR_ALLOC_N(argv, *argc) < 0) {
xmlRpcError(VIR_ERR_NO_MEMORY, _("read response"), sizeof(*argv) * *argc);
return NULL;
}
@@ -552,7 +550,7 @@
for (i = 0; i < argc; i++)
xmlRpcValueFree(argv[i]);
- free(argv);
+ VIR_FREE(argv);
}
int xmlRpcCall(xmlRpcContextPtr context, const char *method,
@@ -589,7 +587,7 @@
content = virBufferContentAndReset(&buf);
ret = xmlRpcCallRaw(context->uri, content);
- free(content);
+ VIR_FREE(content);
if (!ret)
return -1;
@@ -597,7 +595,7 @@
xml = xmlReadDoc((const xmlChar *)ret, "response.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
- free(ret);
+ VIR_FREE(ret);
if (xml == NULL) {
errno = EINVAL;
@@ -659,13 +657,14 @@
xmlRpcContextPtr xmlRpcContextNew(const char *uri)
{
- xmlRpcContextPtr ret = malloc(sizeof(*ret));
+ xmlRpcContextPtr ret = NULL;
- if (ret) {
+ if (VIR_ALLOC(ret) < 0) {
+ xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate new context"), sizeof(*ret));
+ } else {
ret->uri = strdup(uri);
ret->faultMessage = NULL;
- } else
- xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate new context"), sizeof(*ret));
+ }
return ret;
}
@@ -673,9 +672,9 @@
void xmlRpcContextFree(xmlRpcContextPtr context)
{
if (context) {
- free(context->uri);
- free(context->faultMessage);
- free(context);
+ VIR_FREE(context->uri);
+ VIR_FREE(context->faultMessage);
+ VIR_FREE(context);
}
}
--
|: Red Hat, Engineering, Boston -o- http://people.redhat.com/berrange/ :|
|: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
16 years, 5 months
[libvirt] [PATCH] Add error message when the invalid XML file is defined.
by Hiroyuki Kaguchi
Hi,
When cpuset that is the attribute of vcpu tag of the XML file is the invalid
string, "virsh define" succeeds.
(Ex. <vcpu cpuset='aaa'>4</vcpu>
<vcpu cpuset=' '>4</vcpu>
<vcpu cpuset=',,,'>4</vcpu> )
The patch changes policy that the definition is failed and shows error message.
Thanks,
Signed-off-by: Hiroyuki Kaguchi <fj7025cf(a)aa.jp.fujitsu.com>
Index: src/xm_internal.c
===================================================================
RCS file: /data/cvs/libvirt/src/xm_internal.c,v
retrieving revision 1.79
diff -u -p -r1.79 xm_internal.c
--- src/xm_internal.c 29 May 2008 19:20:23 -0000 1.79
+++ src/xm_internal.c 30 May 2008 05:27:35 -0000
@@ -2004,20 +2004,15 @@ virConfPtr xenXMParseXMLToConfig(virConn
char *ranges;
ranges = virConvertCpuSet(conn, cpus, 0);
- if (ranges != NULL) {
- VIR_FREE(cpus);
- if (xenXMConfigSetString(conf, "cpus", ranges) < 0) {
- VIR_FREE(ranges);
- goto error;
- }
+ VIR_FREE(cpus);
+ if (ranges == NULL) {
+ goto error;
+ }
+ if (xenXMConfigSetString(conf, "cpus", ranges) < 0) {
VIR_FREE(ranges);
- } else {
- if (xenXMConfigSetString(conf, "cpus", cpus) < 0) {
- VIR_FREE(cpus);
- goto error;
- }
- VIR_FREE(cpus);
+ goto error;
}
+ VIR_FREE(ranges);
}
obj = xmlXPathEval(BAD_CAST "string(/domain/os/type)", ctxt);
16 years, 5 months
[libvirt] PATCH: Switch remote daemon to use memory alloc APIs
by Daniel P. Berrange
THis patch switches over the remote daemon to use the memory allocation
APIs. This required exporting the 4 apis in the .so. I discovered along
the way that none of the remote RPC dispatcher impls checked for malloc
failure, so I've had to add that in too.
qemud/event.c | 49 +++--------
qemud/mdns.c | 53 ++++++------
qemud/qemud.c | 43 +++++-----
qemud/remote.c | 200 ++++++++++++++++++++++++++++++------------------
src/libvirt_sym.version | 5 +
src/memory.c | 8 -
src/memory.h | 16 +--
7 files changed, 207 insertions(+), 167 deletions(-)
Regards,
Daniel
diff -r 06acc2c5c1fb qemud/event.c
--- a/qemud/event.c Thu May 29 16:05:55 2008 -0400
+++ b/qemud/event.c Fri May 30 10:36:42 2008 -0400
@@ -31,6 +31,7 @@
#include "qemud.h"
#include "event.h"
+#include "memory.h"
#define EVENT_DEBUG(fmt, ...) qemudDebug("EVENT: " fmt, __VA_ARGS__)
@@ -82,16 +83,11 @@
int virEventAddHandleImpl(int fd, int events, virEventHandleCallback cb, void *opaque) {
EVENT_DEBUG("Add handle %d %d %p %p", fd, events, cb, opaque);
if (eventLoop.handlesCount == eventLoop.handlesAlloc) {
- struct virEventHandle *tmp;
EVENT_DEBUG("Used %d handle slots, adding %d more",
eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT);
- tmp = realloc(eventLoop.handles,
- sizeof(struct virEventHandle) *
- (eventLoop.handlesAlloc + EVENT_ALLOC_EXTENT));
- if (!tmp) {
+ if (VIR_REALLOC_N(eventLoop.handles,
+ (eventLoop.handlesAlloc + EVENT_ALLOC_EXTENT)) < 0)
return -1;
- }
- eventLoop.handles = tmp;
eventLoop.handlesAlloc += EVENT_ALLOC_EXTENT;
}
@@ -152,16 +148,11 @@
}
if (eventLoop.timeoutsCount == eventLoop.timeoutsAlloc) {
- struct virEventTimeout *tmp;
EVENT_DEBUG("Used %d timeout slots, adding %d more",
eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT);
- tmp = realloc(eventLoop.timeouts,
- sizeof(struct virEventTimeout) *
- (eventLoop.timeoutsAlloc + EVENT_ALLOC_EXTENT));
- if (!tmp) {
+ if (VIR_REALLOC_N(eventLoop.timeouts,
+ (eventLoop.timeoutsAlloc + EVENT_ALLOC_EXTENT)) < 0)
return -1;
- }
- eventLoop.timeouts = tmp;
eventLoop.timeoutsAlloc += EVENT_ALLOC_EXTENT;
}
@@ -281,7 +272,7 @@
}
*retfds = NULL;
/* Setup the poll file handle data structs */
- if (!(fds = malloc(sizeof(*fds) * nfds)))
+ if (VIR_ALLOC_N(fds, nfds) < 0)
return -1;
for (i = 0, nfds = 0 ; i < eventLoop.handlesCount ; i++) {
@@ -398,16 +389,11 @@
/* Release some memory if we've got a big chunk free */
if ((eventLoop.timeoutsAlloc - EVENT_ALLOC_EXTENT) > eventLoop.timeoutsCount) {
- struct virEventTimeout *tmp;
EVENT_DEBUG("Releasing %d out of %d timeout slots used, releasing %d",
eventLoop.timeoutsCount, eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT);
- tmp = realloc(eventLoop.timeouts,
- sizeof(struct virEventTimeout) *
- (eventLoop.timeoutsAlloc - EVENT_ALLOC_EXTENT));
- if (!tmp) {
+ if (VIR_REALLOC_N(eventLoop.timeouts,
+ (eventLoop.timeoutsAlloc - EVENT_ALLOC_EXTENT)) < 0)
return -1;
- }
- eventLoop.timeouts = tmp;
eventLoop.timeoutsAlloc -= EVENT_ALLOC_EXTENT;
}
return 0;
@@ -439,16 +425,11 @@
/* Release some memory if we've got a big chunk free */
if ((eventLoop.handlesAlloc - EVENT_ALLOC_EXTENT) > eventLoop.handlesCount) {
- struct virEventHandle *tmp;
EVENT_DEBUG("Releasing %d out of %d handles slots used, releasing %d",
eventLoop.handlesCount, eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT);
- tmp = realloc(eventLoop.handles,
- sizeof(struct virEventHandle) *
- (eventLoop.handlesAlloc - EVENT_ALLOC_EXTENT));
- if (!tmp) {
+ if (VIR_REALLOC_N(eventLoop.handles,
+ (eventLoop.handlesAlloc - EVENT_ALLOC_EXTENT)) < 0)
return -1;
- }
- eventLoop.handles = tmp;
eventLoop.handlesAlloc -= EVENT_ALLOC_EXTENT;
}
return 0;
@@ -466,7 +447,7 @@
return -1;
if (virEventCalculateTimeout(&timeout) < 0) {
- free(fds);
+ VIR_FREE(fds);
return -1;
}
@@ -478,20 +459,20 @@
if (errno == EINTR) {
goto retry;
}
- free(fds);
+ VIR_FREE(fds);
return -1;
}
if (virEventDispatchTimeouts() < 0) {
- free(fds);
+ VIR_FREE(fds);
return -1;
}
if (ret > 0 &&
virEventDispatchHandles(fds) < 0) {
- free(fds);
+ VIR_FREE(fds);
return -1;
}
- free(fds);
+ VIR_FREE(fds);
if (virEventCleanupTimeouts() < 0)
return -1;
diff -r 06acc2c5c1fb qemud/mdns.c
--- a/qemud/mdns.c Thu May 29 16:05:55 2008 -0400
+++ b/qemud/mdns.c Fri May 30 10:36:42 2008 -0400
@@ -42,6 +42,7 @@
#include "mdns.h"
#include "event.h"
#include "remote_internal.h"
+#include "memory.h"
#define AVAHI_DEBUG(fmt, ...) qemudDebug("AVAHI: " fmt, __VA_ARGS__)
@@ -99,7 +100,7 @@
/* A service name collision happened. Let's pick a new name */
n = avahi_alternative_service_name(group->name);
- free(group->name);
+ VIR_FREE(group->name);
group->name = n;
AVAHI_DEBUG("Group name collision, renaming service to '%s'", group->name);
@@ -237,8 +238,8 @@
static AvahiWatch *libvirtd_mdns_watch_new(const AvahiPoll *api ATTRIBUTE_UNUSED,
int fd, AvahiWatchEvent event, AvahiWatchCallback cb, void *userdata) {
- AvahiWatch *w = malloc(sizeof(*w));
- if (!w)
+ AvahiWatch *w;
+ if (VIR_ALLOC(w) < 0)
return NULL;
w->fd = fd;
@@ -248,7 +249,7 @@
AVAHI_DEBUG("New handle %p FD %d Event %d", w, w->fd, event);
if (virEventAddHandleImpl(fd, event, libvirtd_mdns_watch_dispatch, w) < 0) {
- free(w);
+ VIR_FREE(w);
return NULL;
}
@@ -271,7 +272,7 @@
{
AVAHI_DEBUG("Free handle %p %d", w, w->fd);
virEventRemoveHandleImpl(w->fd);
- free(w);
+ VIR_FREE(w);
}
static void libvirtd_mdns_timeout_dispatch(int timer ATTRIBUTE_UNUSED, void *opaque)
@@ -287,15 +288,15 @@
AvahiTimeoutCallback cb,
void *userdata)
{
- AvahiTimeout *t = malloc(sizeof(*t));
+ AvahiTimeout *t;
struct timeval now;
long long nowms, thenms, timeout;
AVAHI_DEBUG("Add timeout %p TV %p", t, tv);
- if (!t)
+ if (VIR_ALLOC(t) < 0)
return NULL;
if (gettimeofday(&now, NULL) < 0) {
- free(t);
+ VIR_FREE(t);
return NULL;
}
@@ -317,7 +318,7 @@
t->userdata = userdata;
if (t->timer < 0) {
- free(t);
+ VIR_FREE(t);
return NULL;
}
@@ -330,7 +331,7 @@
long long nowms, thenms, timeout;
AVAHI_DEBUG("Update timeout %p TV %p", t, tv);
if (gettimeofday(&now, NULL) < 0) {
- free(t);
+ VIR_FREE(t);
return;
}
@@ -351,14 +352,14 @@
{
AVAHI_DEBUG("Free timeout %p", t);
virEventRemoveTimeoutImpl(t->timer);
- free(t);
+ VIR_FREE(t);
}
static AvahiPoll *libvirtd_create_poll(void)
{
- AvahiPoll *p = malloc(sizeof(*p));
- if (!p)
+ AvahiPoll *p;
+ if (VIR_ALLOC(p) < 0)
return NULL;
p->userdata = NULL;
@@ -377,14 +378,13 @@
struct libvirtd_mdns *libvirtd_mdns_new(void)
{
- struct libvirtd_mdns *mdns = malloc(sizeof(*mdns));
- if (!mdns)
+ struct libvirtd_mdns *mdns;
+ if (VIR_ALLOC(mdns) < 0)
return NULL;
- memset(mdns, 0, sizeof(*mdns));
/* Allocate main loop object */
if (!(mdns->poller = libvirtd_create_poll())) {
- free(mdns);
+ VIR_FREE(mdns);
return NULL;
}
@@ -406,15 +406,14 @@
}
struct libvirtd_mdns_group *libvirtd_mdns_add_group(struct libvirtd_mdns *mdns, const char *name) {
- struct libvirtd_mdns_group *group = malloc(sizeof(*group));
+ struct libvirtd_mdns_group *group;
AVAHI_DEBUG("Adding group '%s'", name);
- if (!group)
+ if (VIR_ALLOC(group) < 0)
return NULL;
- memset(group, 0, sizeof(*group));
if (!(group->name = strdup(name))) {
- free(group);
+ VIR_FREE(group);
return NULL;
}
group->mdns = mdns;
@@ -428,12 +427,12 @@
while (tmp) {
if (tmp == group) {
- free(group->name);
+ VIR_FREE(group->name);
if (prev)
prev->next = group->next;
else
group->mdns->group = group->next;
- free(group);
+ VIR_FREE(group);
return;
}
prev = tmp;
@@ -442,15 +441,15 @@
}
struct libvirtd_mdns_entry *libvirtd_mdns_add_entry(struct libvirtd_mdns_group *group, const char *type, int port) {
- struct libvirtd_mdns_entry *entry = malloc(sizeof(*entry));
+ struct libvirtd_mdns_entry *entry;
AVAHI_DEBUG("Adding entry %s %d to group %s", type, port, group->name);
- if (!entry)
+ if (VIR_ALLOC(entry) < 0)
return NULL;
entry->port = port;
if (!(entry->type = strdup(type))) {
- free(entry);
+ VIR_FREE(entry);
return NULL;
}
entry->next = group->entry;
@@ -463,7 +462,7 @@
while (tmp) {
if (tmp == entry) {
- free(entry->type);
+ VIR_FREE(entry->type);
if (prev)
prev->next = entry->next;
else
diff -r 06acc2c5c1fb qemud/qemud.c
--- a/qemud/qemud.c Thu May 29 16:05:55 2008 -0400
+++ b/qemud/qemud.c Fri May 30 10:36:42 2008 -0400
@@ -56,6 +56,7 @@
#include "remote_internal.h"
#include "conf.h"
#include "event.h"
+#include "memory.h"
#ifdef HAVE_AVAHI
#include "mdns.h"
#endif
@@ -466,12 +467,12 @@
static int qemudListenUnix(struct qemud_server *server,
const char *path, int readonly, int auth) {
- struct qemud_socket *sock = calloc(1, sizeof(*sock));
+ struct qemud_socket *sock;
struct sockaddr_un addr;
mode_t oldmask;
gid_t oldgrp;
- if (!sock) {
+ if (VIR_ALLOC(sock) < 0) {
qemudLog(QEMUD_ERR,
"%s", _("Failed to allocate memory for struct qemud_socket"));
return -1;
@@ -611,12 +612,10 @@
struct sockaddr_storage sa;
socklen_t salen = sizeof(sa);
- sock = calloc (1, sizeof *sock);
-
- if (!sock) {
+ if (VIR_ALLOC(sock) < 0) {
qemudLog (QEMUD_ERR,
_("remoteListenTCP: calloc: %s"), strerror (errno));
- return -1;
+ goto cleanup;
}
sock->readonly = 0;
@@ -629,7 +628,7 @@
sock->auth = auth;
if (getsockname(sock->fd, (struct sockaddr *)(&sa), &salen) < 0)
- return -1;
+ goto cleanup;
if (sa.ss_family == AF_INET)
sock->port = htons(((struct sockaddr_in*)&sa)->sin_port);
@@ -642,12 +641,12 @@
if (qemudSetCloseExec(sock->fd) < 0 ||
qemudSetNonBlock(sock->fd) < 0)
- return -1;
+ goto cleanup;
if (listen (sock->fd, 30) < 0) {
qemudLog (QEMUD_ERR,
_("remoteListenTCP: listen: %s"), strerror (errno));
- return -1;
+ goto cleanup;
}
if (virEventAddHandleImpl(sock->fd,
@@ -655,12 +654,17 @@
qemudDispatchServerEvent,
server) < 0) {
qemudLog(QEMUD_ERR, "%s", _("Failed to add server event callback"));
- return -1;
+ goto cleanup;
}
}
return 0;
+
+cleanup:
+ for (i = 0; i < nfds; ++i)
+ close(fds[0]);
+ return -1;
}
static int qemudInitPaths(struct qemud_server *server,
@@ -712,7 +716,7 @@
static struct qemud_server *qemudInitialize(int sigread) {
struct qemud_server *server;
- if (!(server = calloc(1, sizeof(*server)))) {
+ if (VIR_ALLOC(server) < 0) {
qemudLog(QEMUD_ERR, "%s", _("Failed to allocate struct qemud_server"));
return NULL;
}
@@ -1092,8 +1096,7 @@
return -1;
}
- client = calloc(1, sizeof(*client));
- if (client == NULL)
+ if (VIR_ALLOC(client) < 0)
goto cleanup;
client->magic = QEMUD_CLIENT_MAGIC;
client->fd = fd;
@@ -1733,8 +1736,7 @@
switch (p->type) {
case VIR_CONF_STRING:
- list = malloc (2 * sizeof (*list));
- if (list == NULL) {
+ if (VIR_ALLOC_N(list, 2) < 0) {
qemudLog (QEMUD_ERR,
_("failed to allocate memory for %s config list"), key);
return -1;
@@ -1745,7 +1747,7 @@
qemudLog (QEMUD_ERR,
_("failed to allocate memory for %s config list value"),
key);
- free (list);
+ VIR_FREE(list);
return -1;
}
break;
@@ -1755,8 +1757,7 @@
virConfValuePtr pp;
for (pp = p->list; pp; pp = pp->next)
len++;
- list = calloc (1+len, sizeof (*list));
- if (list == NULL) {
+ if (VIR_ALLOC_N(list, 1+len) < 0) {
qemudLog (QEMUD_ERR,
_("failed to allocate memory for %s config list"), key);
return -1;
@@ -1766,15 +1767,15 @@
qemudLog (QEMUD_ERR, _("remoteReadConfigFile: %s: %s:"
" must be a string or list of strings\n"),
filename, key);
- free (list);
+ VIR_FREE(list);
return -1;
}
list[i] = strdup (pp->str);
if (list[i] == NULL) {
int j;
for (j = 0 ; j < i ; j++)
- free (list[j]);
- free (list);
+ VIR_FREE(list[j]);
+ VIR_FREE(list);
qemudLog (QEMUD_ERR, _("failed to allocate memory"
" for %s config list value"), key);
return -1;
diff -r 06acc2c5c1fb qemud/remote.c
--- a/qemud/remote.c Thu May 29 16:05:55 2008 -0400
+++ b/qemud/remote.c Fri May 30 10:36:42 2008 -0400
@@ -50,6 +50,7 @@
#include "internal.h"
#include "qemud.h"
+#include "memory.h"
#define DEBUG 0
@@ -609,14 +610,19 @@
}
/* Allocate return buffer. */
- ret->freeMems.freeMems_val = calloc (args->maxCells, sizeof (*(ret->freeMems.freeMems_val)));
+ if (VIR_ALLOC_N(ret->freeMems.freeMems_val, args->maxCells) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
+ }
ret->freeMems.freeMems_len = virNodeGetCellsFreeMemory(client->conn,
(unsigned long long *)ret->freeMems.freeMems_val,
args->startCell,
args->maxCells);
- if (ret->freeMems.freeMems_len == 0)
+ if (ret->freeMems.freeMems_len == 0) {
+ VIR_FREE(ret->freeMems.freeMems_val);
return -1;
+ }
return 0;
}
@@ -688,15 +694,14 @@
remoteDispatchError (client, req, "%s", _("nparams too large"));
return -2;
}
- params = malloc (sizeof (*params) * nparams);
- if (params == NULL) {
- remoteDispatchError (client, req, "%s", _("out of memory allocating array"));
+ if (VIR_ALLOC_N(params, nparams) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
return -2;
}
dom = get_nonnull_domain (client->conn, args->dom);
if (dom == NULL) {
- free (params);
+ VIR_FREE(params);
remoteDispatchError (client, req, "%s", _("domain not found"));
return -2;
}
@@ -704,32 +709,21 @@
r = virDomainGetSchedulerParameters (dom, params, &nparams);
if (r == -1) {
virDomainFree(dom);
- free (params);
+ VIR_FREE(params);
return -1;
}
/* Serialise the scheduler parameters. */
ret->params.params_len = nparams;
- ret->params.params_val = malloc (sizeof (*(ret->params.params_val))
- * nparams);
- if (ret->params.params_val == NULL) {
- virDomainFree(dom);
- free (params);
- remoteDispatchError (client, req,
- "%s", _("out of memory allocating return array"));
- return -2;
- }
+ if (VIR_ALLOC_N(ret->params.params_val, nparams) < 0)
+ goto oom;
for (i = 0; i < nparams; ++i) {
// remoteDispatchClientRequest will free this:
ret->params.params_val[i].field = strdup (params[i].field);
- if (ret->params.params_val[i].field == NULL) {
- virDomainFree(dom);
- free (params);
- remoteDispatchError (client, req,
- "%s", _("out of memory allocating return array"));
- return -2;
- }
+ if (ret->params.params_val[i].field == NULL)
+ goto oom;
+
ret->params.params_val[i].value.type = params[i].type;
switch (params[i].type) {
case VIR_DOMAIN_SCHED_FIELD_INT:
@@ -745,16 +739,23 @@
case VIR_DOMAIN_SCHED_FIELD_BOOLEAN:
ret->params.params_val[i].value.remote_sched_param_value_u.b = params[i].value.b; break;
default:
- virDomainFree(dom);
- free (params);
remoteDispatchError (client, req, "%s", _("unknown type"));
- return -2;
+ goto cleanup;
}
}
virDomainFree(dom);
- free (params);
+ VIR_FREE(params);
return 0;
+
+oom:
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+cleanup:
+ virDomainFree(dom);
+ for (i = 0 ; i < nparams ; i++)
+ VIR_FREE(ret->params.params_val[i].field);
+ VIR_FREE(params);
+ return -2;
}
static int
@@ -775,9 +776,8 @@
remoteDispatchError (client, req, "%s", _("nparams too large"));
return -2;
}
- params = malloc (sizeof (*params) * nparams);
- if (params == NULL) {
- remoteDispatchError (client, req, "%s", _("out of memory allocating array"));
+ if (VIR_ALLOC_N(params, nparams)) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
return -2;
}
@@ -805,14 +805,14 @@
dom = get_nonnull_domain (client->conn, args->dom);
if (dom == NULL) {
- free (params);
+ VIR_FREE(params);
remoteDispatchError (client, req, "%s", _("domain not found"));
return -2;
}
r = virDomainSetSchedulerParameters (dom, params, nparams);
virDomainFree(dom);
- free (params);
+ VIR_FREE(params);
if (r == -1) return -1;
return 0;
@@ -1190,9 +1190,9 @@
remote_domain_get_vcpus_args *args,
remote_domain_get_vcpus_ret *ret)
{
- virDomainPtr dom;
- virVcpuInfoPtr info;
- unsigned char *cpumaps;
+ virDomainPtr dom = NULL;
+ virVcpuInfoPtr info = NULL;
+ unsigned char *cpumaps = NULL;
int info_len, i;
CHECK_CONN(client);
@@ -1215,20 +1215,25 @@
}
/* Allocate buffers to take the results. */
- info = calloc (args->maxinfo, sizeof (*info));
- cpumaps = calloc (args->maxinfo * args->maplen, sizeof (*cpumaps));
+ if (VIR_ALLOC_N(info, args->maxinfo) < 0)
+ goto oom;
+ if (VIR_ALLOC_N(cpumaps, args->maxinfo) < 0)
+ goto oom;
info_len = virDomainGetVcpus (dom,
info, args->maxinfo,
cpumaps, args->maplen);
if (info_len == -1) {
+ VIR_FREE(info);
+ VIR_FREE(cpumaps);
virDomainFree(dom);
return -1;
}
/* Allocate the return buffer for info. */
ret->info.info_len = info_len;
- ret->info.info_val = calloc (info_len, sizeof (*(ret->info.info_val)));
+ if (VIR_ALLOC_N(ret->info.info_val, info_len) < 0)
+ goto oom;
for (i = 0; i < info_len; ++i) {
ret->info.info_val[i].number = info[i].number;
@@ -1244,8 +1249,16 @@
ret->cpumaps.cpumaps_len = args->maxinfo * args->maplen;
ret->cpumaps.cpumaps_val = (char *) cpumaps;
+ VIR_FREE(info);
virDomainFree(dom);
return 0;
+
+oom:
+ VIR_FREE(info);
+ VIR_FREE(cpumaps);
+ virDomainFree(dom);
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
}
static int
@@ -1267,13 +1280,16 @@
dname = args->dname == NULL ? NULL : *args->dname;
/* Wacky world of XDR ... */
- uri_out = calloc (1, sizeof (*uri_out));
+ if (VIR_ALLOC(uri_out) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
+ }
r = __virDomainMigratePrepare (client->conn, &cookie, &cookielen,
uri_in, uri_out,
args->flags, dname, args->resource);
if (r == -1) {
- free(uri_out);
+ VIR_FREE(uri_out);
return -1;
}
@@ -1284,7 +1300,7 @@
ret->cookie.cookie_val = cookie;
if (*uri_out == NULL) {
ret->uri_out = NULL;
- free(uri_out);
+ VIR_FREE(uri_out);
} else {
ret->uri_out = uri_out;
}
@@ -1361,12 +1377,18 @@
}
/* Allocate return buffer. */
- ret->names.names_val = calloc (args->maxnames, sizeof (*(ret->names.names_val)));
+ if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
+ }
ret->names.names_len =
virConnectListDefinedDomains (client->conn,
ret->names.names_val, args->maxnames);
- if (ret->names.names_len == -1) return -1;
+ if (ret->names.names_len == -1) {
+ VIR_FREE(ret->names.names_val);
+ return -1;
+ }
return 0;
}
@@ -1769,12 +1791,18 @@
}
/* Allocate return buffer. */
- ret->names.names_val = calloc (args->maxnames, sizeof (*(ret->names.names_val)));
+ if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
+ }
ret->names.names_len =
virConnectListDefinedNetworks (client->conn,
ret->names.names_val, args->maxnames);
- if (ret->names.names_len == -1) return -1;
+ if (ret->names.names_len == -1) {
+ VIR_FREE(ret->names.names_val);
+ return -1;
+ }
return 0;
}
@@ -1795,11 +1823,17 @@
}
/* Allocate return buffer. */
- ret->ids.ids_val = calloc (args->maxids, sizeof (*(ret->ids.ids_val)));
+ if (VIR_ALLOC_N(ret->ids.ids_val, args->maxids) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
+ }
ret->ids.ids_len = virConnectListDomains (client->conn,
ret->ids.ids_val, args->maxids);
- if (ret->ids.ids_len == -1) return -1;
+ if (ret->ids.ids_len == -1) {
+ VIR_FREE(ret->ids.ids_val);
+ return -1;
+ }
return 0;
}
@@ -1820,12 +1854,18 @@
}
/* Allocate return buffer. */
- ret->names.names_val = calloc (args->maxnames, sizeof (*(ret->names.names_val)));
+ if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
+ }
ret->names.names_len =
virConnectListNetworks (client->conn,
ret->names.names_val, args->maxnames);
- if (ret->names.names_len == -1) return -1;
+ if (ret->names.names_len == -1) {
+ VIR_FREE(ret->names.names_len);
+ return -1;
+ }
return 0;
}
@@ -2128,8 +2168,8 @@
remote_auth_list_ret *ret)
{
ret->types.types_len = 1;
- if ((ret->types.types_val = calloc (ret->types.types_len, sizeof (*(ret->types.types_val)))) == NULL) {
- remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, "auth types");
+ if (VIR_ALLOC_N(ret->types.types_val, ret->types.types_len) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
return -2;
}
ret->types.types_val[0] = client->auth;
@@ -2158,9 +2198,8 @@
return NULL;
}
- addr = malloc(strlen(host) + 1 + strlen(port) + 1);
- if (!addr) {
- remoteDispatchError(client, req, "%s", _("cannot allocate address"));
+ if (VIR_ALLOC_N(addr, strlen(host) + 1 + strlen(port) + 1) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
return NULL;
}
@@ -2216,11 +2255,11 @@
if (getpeername(client->fd, (struct sockaddr*)&sa, &salen) < 0) {
remoteDispatchError(client, req, _("failed to get peer address %d (%s)"),
errno, strerror(errno));
- free(localAddr);
+ VIR_FREE(localAddr);
return -2;
}
if ((remoteAddr = addrToString(client, req, &sa, salen)) == NULL) {
- free(localAddr);
+ VIR_FREE(localAddr);
return -2;
}
@@ -2232,8 +2271,8 @@
NULL, /* XXX Callbacks */
SASL_SUCCESS_DATA,
&client->saslconn);
- free(localAddr);
- free(remoteAddr);
+ VIR_FREE(localAddr);
+ VIR_FREE(remoteAddr);
if (err != SASL_OK) {
qemudLog(QEMUD_ERR, _("sasl context setup failed %d (%s)"),
err, sasl_errstring(err, NULL, NULL));
@@ -2477,10 +2516,8 @@
/* NB, distinction of NULL vs "" is *critical* in SASL */
if (serverout) {
- ret->data.data_val = malloc(serveroutlen);
- if (!ret->data.data_val) {
- remoteDispatchError (client, req,
- "%s", _("out of memory allocating array"));
+ if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
return -2;
}
memcpy(ret->data.data_val, serverout, serveroutlen);
@@ -2558,10 +2595,8 @@
/* NB, distinction of NULL vs "" is *critical* in SASL */
if (serverout) {
- ret->data.data_val = malloc(serveroutlen);
- if (!ret->data.data_val) {
- remoteDispatchError (client, req,
- "%s", _("out of memory allocating array"));
+ if (VIR_ALLOC_N(ret->data.data_val, serveroutlen) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
return -2;
}
memcpy(ret->data.data_val, serverout, serveroutlen);
@@ -2778,12 +2813,18 @@
}
/* Allocate return buffer. */
- ret->names.names_val = calloc (args->maxnames, sizeof (char *));
+ if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
+ }
ret->names.names_len =
virConnectListDefinedStoragePools (client->conn,
- ret->names.names_val, args->maxnames);
- if (ret->names.names_len == -1) return -1;
+ ret->names.names_val, args->maxnames);
+ if (ret->names.names_len == -1) {
+ VIR_FREE(ret->names.names_val);
+ return -1;
+ }
return 0;
}
@@ -2804,12 +2845,18 @@
}
/* Allocate return buffer. */
- ret->names.names_val = calloc (args->maxnames, sizeof (char *));
+ if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
+ }
ret->names.names_len =
virConnectListStoragePools (client->conn,
ret->names.names_val, args->maxnames);
- if (ret->names.names_len == -1) return -1;
+ if (ret->names.names_len == -1) {
+ VIR_FREE(ret->names.names_val);
+ return -1;
+ }
return 0;
}
@@ -3213,13 +3260,20 @@
}
/* Allocate return buffer. */
- ret->names.names_val = calloc (args->maxnames, sizeof (char *));
+ if (VIR_ALLOC_N(ret->names.names_val, args->maxnames) < 0) {
+ virStoragePoolFree(pool);
+ remoteDispatchSendError(client, req, VIR_ERR_NO_MEMORY, NULL);
+ return -2;
+ }
ret->names.names_len =
virStoragePoolListVolumes (pool,
ret->names.names_val, args->maxnames);
virStoragePoolFree(pool);
- if (ret->names.names_len == -1) return -1;
+ if (ret->names.names_len == -1) {
+ VIR_FREE(ret->names.names_val);
+ return -1;
+ }
return 0;
}
diff -r 06acc2c5c1fb src/libvirt_sym.version
--- a/src/libvirt_sym.version Thu May 29 16:05:55 2008 -0400
+++ b/src/libvirt_sym.version Fri May 30 10:36:42 2008 -0400
@@ -190,5 +190,10 @@
__virMacAddrCompare;
+ __virAlloc;
+ __virAllocN;
+ __virReallocN;
+ __virFree;
+
local: *;
};
diff -r 06acc2c5c1fb src/memory.c
--- a/src/memory.c Thu May 29 16:05:55 2008 -0400
+++ b/src/memory.c Fri May 30 10:36:42 2008 -0400
@@ -104,7 +104,7 @@
*
* Returns -1 on failure to allocate, zero on success
*/
-int virAlloc(void *ptrptr, size_t size)
+int __virAlloc(void *ptrptr, size_t size)
{
#if TEST_OOM
if (virAllocTestFail()) {
@@ -137,7 +137,7 @@
*
* Returns -1 on failure to allocate, zero on success
*/
-int virAllocN(void *ptrptr, size_t size, size_t count)
+int __virAllocN(void *ptrptr, size_t size, size_t count)
{
#if TEST_OOM
if (virAllocTestFail()) {
@@ -171,7 +171,7 @@
*
* Returns -1 on failure to allocate, zero on success
*/
-int virReallocN(void *ptrptr, size_t size, size_t count)
+int __virReallocN(void *ptrptr, size_t size, size_t count)
{
void *tmp;
#if TEST_OOM
@@ -203,7 +203,7 @@
* the 'ptrptr' variable. After release, 'ptrptr' will be
* updated to point to NULL.
*/
-void virFree(void *ptrptr)
+void __virFree(void *ptrptr)
{
free(*(void**)ptrptr);
*(void**)ptrptr = NULL;
diff -r 06acc2c5c1fb src/memory.h
--- a/src/memory.h Thu May 29 16:05:55 2008 -0400
+++ b/src/memory.h Fri May 30 10:36:42 2008 -0400
@@ -26,10 +26,10 @@
#include "internal.h"
/* Don't call these directly - use the macros below */
-int virAlloc(void *ptrptr, size_t size) ATTRIBUTE_RETURN_CHECK;
-int virAllocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK;
-int virReallocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK;
-void virFree(void *ptrptr);
+int __virAlloc(void *ptrptr, size_t size) ATTRIBUTE_RETURN_CHECK;
+int __virAllocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK;
+int __virReallocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK;
+void __virFree(void *ptrptr);
/**
* VIR_ALLOC:
@@ -41,7 +41,7 @@
*
* Returns -1 on failure, 0 on success
*/
-#define VIR_ALLOC(ptr) virAlloc(&(ptr), sizeof(*(ptr)))
+#define VIR_ALLOC(ptr) __virAlloc(&(ptr), sizeof(*(ptr)))
/**
* VIR_ALLOC_N:
@@ -54,7 +54,7 @@
*
* Returns -1 on failure, 0 on success
*/
-#define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)), (count))
+#define VIR_ALLOC_N(ptr, count) __virAllocN(&(ptr), sizeof(*(ptr)), (count))
/**
* VIR_REALLOC_N:
@@ -67,7 +67,7 @@
*
* Returns -1 on failure, 0 on success
*/
-#define VIR_REALLOC_N(ptr, count) virReallocN(&(ptr), sizeof(*(ptr)), (count))
+#define VIR_REALLOC_N(ptr, count) __virReallocN(&(ptr), sizeof(*(ptr)), (count))
/**
* VIR_FREE:
@@ -76,7 +76,7 @@
* Free the memory stored in 'ptr' and update to point
* to NULL.
*/
-#define VIR_FREE(ptr) virFree(&(ptr));
+#define VIR_FREE(ptr) __virFree(&(ptr))
#if TEST_OOM
--
|: Red Hat, Engineering, Boston -o- http://people.redhat.com/berrange/ :|
|: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
16 years, 5 months
[libvirt] [PATCH]show the autostart status when display a 'virsh dominfo'.
by S.Sakamoto
http://www.redhat.com/archives/libvir-list/2008-May/msg00065.html
> It should really show the autostart
> status when display a 'virsh domiinfo'
I make a patch to show the autostart
status when display a 'virsh dominfo'.
*** autostart status = enable ***
# virsh dominfo guest_dom
Id: -
Name: guest_dom
UUID: 08b6f537-87f5-9617-1d66-cadd223fd4b6
OS Type: linux
State: shut off
CPU(s): 1
Max memory: 262144 kB
Used memory: 262144 kB
Auto Start: enable
^^^^^^^^^^^^^^^^^^^^^^
*** autostart status = disable ***
# virsh dominfo guest_dom
Id: -
Name: guest_dom
UUID: 08b6f537-87f5-9617-1d66-cadd223fd4b6
OS Type: linux
State: shut off
CPU(s): 1
Max memory: 262144 kB
Used memory: 262144 kB
Auto Start: disable
^^^^^^^^^^^^^^^^^^^^^^^
Thanks,
Shigeki Sakamoto.
Index: virsh.c
===================================================================
RCS file: /data/cvs/libvirt/src/virsh.c,v
retrieving revision 1.153
diff -u -p -r1.153 virsh.c
--- virsh.c 23 May 2008 08:24:44 -0000 1.153
+++ virsh.c 26 May 2008 05:59:54 -0000
@@ -1492,7 +1492,7 @@ cmdDominfo(vshControl * ctl, vshCmd * cm
{
virDomainInfo info;
virDomainPtr dom;
- int ret = TRUE;
+ int ret = TRUE, autostart;
unsigned int id;
char *str, uuid[VIR_UUID_STRING_BUFLEN];
@@ -1545,6 +1545,11 @@ cmdDominfo(vshControl * ctl, vshCmd * cm
ret = FALSE;
}
+ if (!virDomainGetAutostart(dom, &autostart)) {
+ vshPrint(ctl, "%-15s %-15s\n", _("Auto Start:"),
+ autostart ? "enable" : "disable" );
+ }
+
virDomainFree(dom);
return ret;
}
16 years, 5 months
[libvirt] [PATCH 4/4] lxc: store tty pid
by Dave Leskovec
This patch will use a file in the lxc configuration directory to store the tty
forwarding process pid. The pid is stored after the process is fork()'d. It's
loaded during startup when the config for a running container is loaded. The
file is deleted when the domain is undefined. This should avoid "losing" the
tty pid over a libvirtd restart.
--
Best Regards,
Dave Leskovec
IBM Linux Technology Center
Open Virtualization
16 years, 5 months