Preserve errno across calls to error reporting functions & VIR_FREE

There are cases when we want log an error message, and possibly free
some memory as part of the cleanup, while still preserving errno for a
caller, but the functions that log errors, and virFree (VIR_FREE) make
system calls that will clear errno. This patch preserves errno during
those most basic functions (corresponding to virReportSystemError(),
virReportOOMError(), networkReportError(), etc, as well as
virStrError()). It does *not preserve errno across calls to higher
level items such as virDispatchError(), as it's assumed the caller is
all finished with any need for errno by the time it dispatches the
error.
This commit is contained in:
Laine Stump 2010-12-23 13:23:42 -05:00
parent 8090a56890
commit 17e19adde2
2 changed files with 25 additions and 7 deletions

View File

@ -306,6 +306,9 @@ int virAllocVar(void *ptrptr, size_t struct_size, size_t element_size, size_t co
*/
void virFree(void *ptrptr)
{
int save_errno = errno;
free(*(void**)ptrptr);
*(void**)ptrptr = NULL;
errno = save_errno;
}

View File

@ -690,6 +690,7 @@ virRaiseErrorFull(virConnectPtr conn ATTRIBUTE_UNUSED,
int int2,
const char *fmt, ...)
{
int save_errno = errno;
virErrorPtr to;
char *str;
int priority;
@ -700,13 +701,17 @@ virRaiseErrorFull(virConnectPtr conn ATTRIBUTE_UNUSED,
* to the per-connection error object when necessary
*/
to = virLastErrorObject();
if (!to)
if (!to) {
errno = save_errno;
return; /* Hit OOM allocating thread error object, sod all we can do now */
}
virResetError(to);
if (code == VIR_ERR_OK)
if (code == VIR_ERR_OK) {
errno = save_errno;
return;
}
/*
* formats the message
@ -749,6 +754,8 @@ virRaiseErrorFull(virConnectPtr conn ATTRIBUTE_UNUSED,
to->str3 = strdup(str3);
to->int1 = int1;
to->int2 = int2;
errno = save_errno;
}
/**
@ -1216,6 +1223,7 @@ void virReportErrorHelper(virConnectPtr conn,
size_t linenr,
const char *fmt, ...)
{
int save_errno = errno;
va_list args;
char errorMessage[1024];
const char *virerr;
@ -1233,7 +1241,7 @@ void virReportErrorHelper(virConnectPtr conn,
domcode, errcode, VIR_ERR_ERROR,
virerr, errorMessage, NULL,
-1, -1, virerr, errorMessage);
errno = save_errno;
}
/**
@ -1249,13 +1257,16 @@ void virReportErrorHelper(virConnectPtr conn,
*/
const char *virStrerror(int theerrno, char *errBuf, size_t errBufLen)
{
int save_errno = errno;
const char *ret;
#ifdef HAVE_STRERROR_R
# ifdef __USE_GNU
/* Annoying linux specific API contract */
return strerror_r(theerrno, errBuf, errBufLen);
ret = strerror_r(theerrno, errBuf, errBufLen);
# else
strerror_r(theerrno, errBuf, errBufLen);
return errBuf;
ret = errBuf;
# endif
#else
/* Mingw lacks strerror_r and its strerror is definitely not
@ -1264,9 +1275,11 @@ const char *virStrerror(int theerrno, char *errBuf, size_t errBufLen)
* header files for debug purposes
*/
int n = snprintf(errBuf, errBufLen, "errno=%d", theerrno);
return (0 < n && n < errBufLen
ret = (0 < n && n < errBufLen
? errBuf : _("internal error: buffer too small"));
#endif
errno = save_errno;
return ret;
}
/**
@ -1288,6 +1301,7 @@ void virReportSystemErrorFull(int domcode,
size_t linenr,
const char *fmt, ...)
{
int save_errno = errno;
char strerror_buf[1024];
char msgDetailBuf[1024];
@ -1318,6 +1332,7 @@ void virReportSystemErrorFull(int domcode,
virRaiseErrorFull(NULL, filename, funcname, linenr,
domcode, VIR_ERR_SYSTEM_ERROR, VIR_ERR_ERROR,
msg, msgDetail, NULL, -1, -1, msg, msgDetail);
errno = save_errno;
}
/**