Introduce OOM reporting to virAsprintf

Actually, I'm turning this function into a macro as filename,
function name and line number needs to be passed. The new
function virAsprintfInternal is introduced with the extended set
of arguments.
This commit is contained in:
Michal Privoznik 2013-06-07 17:10:28 +02:00
parent 8290cbbc38
commit dc6f2dadac
30 changed files with 158 additions and 53 deletions

2
cfg.mk
View File

@ -900,7 +900,7 @@ exclude_file_name_regexp--sc_prohibit_always_true_header_tests = \
^python/(libvirt-(lxc-|qemu-)?override|typewrappers)\.c$$
exclude_file_name_regexp--sc_prohibit_asprintf = \
^(bootstrap.conf$$|src/util/virstring\.c$$|examples/domain-events/events-c/event-test\.c$$|tests/vircgroupmock\.c$$)
^(bootstrap.conf$$|src/util/virstring\.[ch]$$|examples/domain-events/events-c/event-test\.c$$|tests/vircgroupmock\.c$$)
exclude_file_name_regexp--sc_prohibit_strdup = \
^(docs/|examples/|python/|src/util/virstring\.c$$)

View File

@ -46,7 +46,7 @@ virDomainAuditGetRdev(const char *path)
(S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))) {
int maj = major(sb.st_rdev);
int min = minor(sb.st_rdev);
ignore_value(virAsprintf(&ret, "%02X:%02X", maj, min));
ignore_value(virAsprintfQuiet(&ret, "%02X:%02X", maj, min));
}
return ret;
}
@ -382,29 +382,29 @@ virDomainAuditHostdev(virDomainObjPtr vm, virDomainHostdevDefPtr hostdev,
case VIR_DOMAIN_HOSTDEV_MODE_SUBSYS:
switch (hostdev->source.subsys.type) {
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
if (virAsprintf(&address, "%.4x:%.2x:%.2x.%.1x",
hostdev->source.subsys.u.pci.addr.domain,
hostdev->source.subsys.u.pci.addr.bus,
hostdev->source.subsys.u.pci.addr.slot,
hostdev->source.subsys.u.pci.addr.function) < 0) {
if (virAsprintfQuiet(&address, "%.4x:%.2x:%.2x.%.1x",
hostdev->source.subsys.u.pci.addr.domain,
hostdev->source.subsys.u.pci.addr.bus,
hostdev->source.subsys.u.pci.addr.slot,
hostdev->source.subsys.u.pci.addr.function) < 0) {
VIR_WARN("OOM while encoding audit message");
goto cleanup;
}
break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
if (virAsprintf(&address, "%.3d.%.3d",
hostdev->source.subsys.u.usb.bus,
hostdev->source.subsys.u.usb.device) < 0) {
if (virAsprintfQuiet(&address, "%.3d.%.3d",
hostdev->source.subsys.u.usb.bus,
hostdev->source.subsys.u.usb.device) < 0) {
VIR_WARN("OOM while encoding audit message");
goto cleanup;
}
break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI:
if (virAsprintf(&address, "%s:%d:%d:%d",
hostdev->source.subsys.u.scsi.adapter,
hostdev->source.subsys.u.scsi.bus,
hostdev->source.subsys.u.scsi.target,
hostdev->source.subsys.u.scsi.unit) < 0) {
if (virAsprintfQuiet(&address, "%s:%d:%d:%d",
hostdev->source.subsys.u.scsi.adapter,
hostdev->source.subsys.u.scsi.bus,
hostdev->source.subsys.u.scsi.target,
hostdev->source.subsys.u.scsi.unit) < 0) {
VIR_WARN("OOM while encoding audit message");
goto cleanup;
}
@ -654,8 +654,8 @@ virDomainAuditCgroupMajor(virDomainObjPtr vm, virCgroupPtr cgroup,
{
char *extra;
if (virAsprintf(&extra, "major category=%s maj=%02X acl=%s",
name, maj, perms) < 0) {
if (virAsprintfQuiet(&extra, "major category=%s maj=%02X acl=%s",
name, maj, perms) < 0) {
VIR_WARN("OOM while encoding audit message");
return;
}
@ -693,8 +693,8 @@ virDomainAuditCgroupPath(virDomainObjPtr vm, virCgroupPtr cgroup,
rdev = virDomainAuditGetRdev(path);
if (!(detail = virAuditEncode("path", path)) ||
virAsprintf(&extra, "path %s rdev=%s acl=%s",
detail, VIR_AUDIT_STR(rdev), perms) < 0) {
virAsprintfQuiet(&extra, "path %s rdev=%s acl=%s",
detail, VIR_AUDIT_STR(rdev), perms) < 0) {
VIR_WARN("OOM while encoding audit message");
goto cleanup;
}

View File

@ -65,7 +65,7 @@ virDriverLoadModule(const char *name)
VIR_DEBUG("Module load %s", name);
if (virAsprintf(&modfile, "%s/libvirt_driver_%s.so", moddir, name) < 0)
if (virAsprintfQuiet(&modfile, "%s/libvirt_driver_%s.so", moddir, name) < 0)
return NULL;
if (access(modfile, R_OK) < 0) {
@ -79,7 +79,7 @@ virDriverLoadModule(const char *name)
goto cleanup;
}
if (virAsprintf(&regfunc, "%sRegister", name) < 0) {
if (virAsprintfQuiet(&regfunc, "%sRegister", name) < 0) {
goto cleanup;
}

View File

@ -1871,7 +1871,7 @@ virStorageFileResize;
# util/virstring.h
virArgvToString;
virAsprintf;
virAsprintfInternal;
virSkipSpaces;
virSkipSpacesAndBackslash;
virSkipSpacesBackwards;
@ -1892,7 +1892,7 @@ virStrToLong_ui;
virStrToLong_ul;
virStrToLong_ull;
virTrimSpaces;
virVasprintf;
virVasprintfInternal;
# util/virsysinfo.h

View File

@ -674,7 +674,7 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
} else {
va_list ap;
va_start(ap, fmt);
ignore_value(virVasprintf(&str, fmt, ap));
ignore_value(virVasprintfQuiet(&str, fmt, ap));
va_end(ap);
}

View File

@ -711,13 +711,13 @@ virLogFormatString(char **msg,
* to just grep for it to find the right place.
*/
if ((funcname != NULL)) {
ret = virAsprintf(msg, "%llu: %s : %s:%d : %s\n",
virThreadSelfID(), virLogPriorityString(priority),
funcname, linenr, str);
ret = virAsprintfQuiet(msg, "%llu: %s : %s:%d : %s\n",
virThreadSelfID(), virLogPriorityString(priority),
funcname, linenr, str);
} else {
ret = virAsprintf(msg, "%llu: %s : %s\n",
virThreadSelfID(), virLogPriorityString(priority),
str);
ret = virAsprintfQuiet(msg, "%llu: %s : %s\n",
virThreadSelfID(), virLogPriorityString(priority),
str);
}
return ret;
}
@ -833,7 +833,7 @@ virLogVMessage(virLogSource source,
/*
* serialize the error message, add level and timestamp
*/
if (virVasprintf(&str, fmt, vargs) < 0) {
if (virVasprintfQuiet(&str, fmt, vargs) < 0) {
goto cleanup;
}
@ -945,7 +945,7 @@ virLogOutputToFd(virLogSource source ATTRIBUTE_UNUSED,
if (fd < 0)
return;
if (virAsprintf(&msg, "%s: %s", timestamp, str) < 0)
if (virAsprintfQuiet(&msg, "%s: %s", timestamp, str) < 0)
return;
ignore_value(safewrite(fd, msg, strlen(msg)));

View File

@ -321,35 +321,41 @@ virStrToDouble(char const *s,
return 0;
}
/**
* virVasprintf
*
* like glibc's vasprintf but makes sure *strp == NULL on failure
*/
int
virVasprintf(char **strp, const char *fmt, va_list list)
virVasprintfInternal(bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr,
char **strp,
const char *fmt,
va_list list)
{
int ret;
if ((ret = vasprintf(strp, fmt, list)) == -1)
if ((ret = vasprintf(strp, fmt, list)) == -1) {
if (report)
virReportOOMErrorFull(domcode, filename, funcname, linenr);
*strp = NULL;
}
return ret;
}
/**
* virAsprintf
*
* like glibc's_asprintf but makes sure *strp == NULL on failure
*/
int
virAsprintf(char **strp, const char *fmt, ...)
virAsprintfInternal(bool report,
int domcode,
const char *filename,
const char *funcname,
size_t linenr,
char **strp,
const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = virVasprintf(strp, fmt, ap);
ret = virVasprintfInternal(report, domcode, filename,
funcname, linenr, strp, fmt, ap);
va_end(ap);
return ret;
}

View File

@ -76,12 +76,6 @@ void virTrimSpaces(char *str, char **endp) ATTRIBUTE_NONNULL(1);
void virSkipSpacesBackwards(const char *str, char **endp)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
int virAsprintf(char **strp, const char *fmt, ...)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3)
ATTRIBUTE_RETURN_CHECK;
int virVasprintf(char **strp, const char *fmt, va_list list)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 0)
ATTRIBUTE_RETURN_CHECK;
char *virStrncpy(char *dest, const char *src, size_t n, size_t destbytes)
ATTRIBUTE_RETURN_CHECK;
char *virStrcpy(char *dest, const char *src, size_t destbytes)
@ -96,6 +90,16 @@ int virStrdup(char **dest, const char *src, bool report, int domcode,
int virStrndup(char **dest, const char *src, ssize_t n, bool report, int domcode,
const char *filename, const char *funcname, size_t linenr)
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
int virAsprintfInternal(bool report, int domcode, const char *filename,
const char *funcname, size_t linenr, char **strp,
const char *fmt, ...)
ATTRIBUTE_NONNULL(6) ATTRIBUTE_NONNULL(7) ATTRIBUTE_FMT_PRINTF(7, 8)
ATTRIBUTE_RETURN_CHECK;
int virVasprintfInternal(bool report, int domcode, const char *filename,
const char *funcname, size_t linenr, char **strp,
const char *fmt, va_list list)
ATTRIBUTE_NONNULL(6) ATTRIBUTE_NONNULL(7) ATTRIBUTE_FMT_PRINTF(7, 0)
ATTRIBUTE_RETURN_CHECK;
/**
* VIR_STRDUP:
@ -166,4 +170,55 @@ int virStrndup(char **dest, const char *src, ssize_t n, bool report, int domcode
size_t virStringListLength(char **strings);
/**
* virVasprintf
*
* Like glibc's vasprintf but makes sure *strp == NULL on failure, in which
* case the OOM error is reported too.
*
* Returns -1 on failure (with OOM error reported), 0 on success.
*/
# define virVasprintf(strp, fmt, list) \
virVasprintfInternal(true, VIR_FROM_THIS, __FILE__, __FUNCTION__, \
__LINE__, strp, fmt, list)
/**
* virVasprintfQuiet
*
* Like glibc's vasprintf but makes sure *strp == NULL on failure.
*
* Returns -1 on failure, 0 on success.
*/
# define virVasprintfQuiet(strp, fmt, list) \
virVasprintfInternal(false, 0, NULL, NULL, 0, strp, fmt, list)
/**
* virAsprintf:
* @strp: variable to hold result (char **)
* @fmt: printf format
*
* Like glibc's_asprintf but makes sure *strp == NULL on failure, in which case
* the OOM error is reported too.
*
* Returns -1 on failure (with OOM error reported), 0 on success.
*/
# define virAsprintf(strp, ...) \
virAsprintfInternal(true, VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__, \
strp, __VA_ARGS__)
/**
* virAsprintfQuiet:
* @strp: variable to hold result (char **)
* @fmt: printf format
*
* Like glibc's_asprintf but makes sure *strp == NULL on failure.
*
* Returns -1 on failure, 0 on success.
*/
# define virAsprintfQuiet(strp, ...) \
virAsprintfInternal(false, 0, NULL, NULL, 0, \
strp, __VA_ARGS__)
#endif /* __VIR_STRING_H__ */

View File

@ -18,6 +18,8 @@
# include "testutilsqemu.h"
# include "virstring.h"
# define VIR_FROM_THIS VIR_FROM_NONE
static virQEMUDriver driver;
static int

View File

@ -23,6 +23,8 @@
#include "virutil.h"
#include "testutils.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static char *fchost_prefix;
#define TEST_FC_HOST_PREFIX fchost_prefix

View File

@ -14,6 +14,8 @@
#include "testutilsqemu.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareXMLToXMLFiles(const char *xml)
{

View File

@ -17,6 +17,8 @@
# include "testutilslxc.h"
# include "virstring.h"
# define VIR_FROM_THIS VIR_FROM_NONE
static virCapsPtr caps;
static virDomainXMLOptionPtr xmlopt;

View File

@ -14,6 +14,8 @@
#include "testutilsqemu.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareXMLToXMLFiles(const char *inxml, const char *outxml,
unsigned int flags)

View File

@ -14,6 +14,8 @@
#include "testutilsqemu.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareXMLToXMLFiles(const char *xml)
{

View File

@ -11,6 +11,8 @@
#include "virfile.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
#if ! (defined __linux__ && (defined(__x86_64__) || \
defined(__amd64__) || \
defined(__i386__) || \

View File

@ -17,6 +17,8 @@
#include "testutilsqemu.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareXMLToXMLFiles(const char *inxml, const char *outxml,
bool expect_error)

View File

@ -17,6 +17,8 @@
# include "testutilsqemu.h"
# include "virstring.h"
# define VIR_FROM_THIS VIR_FROM_NONE
static virQEMUDriver driver;
static int blankProblemElements(char *data)

View File

@ -11,6 +11,8 @@
# include "viralloc.h"
# include "virstring.h"
# define VIR_FROM_THIS VIR_FROM_NONE
struct testInfo {
const char *name;
virQEMUCapsPtr flags;

View File

@ -18,6 +18,8 @@
# include "testutilsqemu.h"
# include "virstring.h"
# define VIR_FROM_THIS VIR_FROM_NONE
static virQEMUDriver driver;
static int

View File

@ -14,6 +14,8 @@
#include "testutilsxen.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static virCapsPtr caps;
static int

View File

@ -14,6 +14,8 @@
#include "testutilsqemu.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareXMLToXMLFiles(const char *inxml, const char *outxml)
{

View File

@ -7,6 +7,8 @@
#include "testutilsqemu.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
const char create_tool[] = "qemu-img";
static int

View File

@ -14,6 +14,8 @@
#include "testutilsqemu.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareXMLToXMLFiles(const char *poolxml, const char *inxml,
const char *outxml)

View File

@ -36,6 +36,8 @@
#include "virfile.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
#if defined (__linux__)
# if defined(__s390__) || defined(__s390x__) || \

View File

@ -10,6 +10,8 @@
#include "viralloc.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
#define TEST_ERROR(...) \
do { \
if (virTestGetDebug()) \

View File

@ -13,6 +13,8 @@
#include "virlog.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
#define testError(...) \
do { \
char *str; \

View File

@ -9,6 +9,8 @@
#include "testutils.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
#ifdef WIN32
int

View File

@ -12,6 +12,8 @@
#include "virfile.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareFiles(virArch hostmachine, const char *xml_rel,
const char *cpuinfo_rel, const char *capabilities_rel)

View File

@ -16,6 +16,8 @@
#include "testutilsxen.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static virCapsPtr caps;
static virDomainXMLOptionPtr xmlopt;

View File

@ -33,6 +33,8 @@
#include "virt-host-validate-common.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
static bool quiet;
void virHostMsgSetQuiet(bool quietFlag)