diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 5d39d50ef29be0e8d516e8ee4d8a66c379023cf9..2a38d891195718d1433c4aee85748cfac39f068e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1318,6 +1318,8 @@ ebtablesRemoveForwardAllowIn; # util/virerror.h virDispatchError; virErrorInitialize; +virErrorSetErrnoFromLastError; +virLastErrorIsSystemErrno; virRaiseErrorFull; virReportErrorHelper; virReportOOMErrorFull; diff --git a/src/util/virerror.c b/src/util/virerror.c index 36d256bbf73e53203cb8715aed17a949075e2d8a..b92c6d10526a3af0fc0629fc0716fb9267ac1347 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -1404,3 +1404,51 @@ void virSetErrorLogPriorityFunc(virErrorLogPriorityFunc func) { virErrorLogPriorityFilter = func; } + + +/** + * virErrorSetErrnoFromLastError: + * + * If the last error had a code of VIR_ERR_SYSTEM_ERROR + * then set errno to the value saved in the error object. + * + * If the last error had a code of VIR_ERR_NO_MEMORY + * then set errno to ENOMEM + * + * Otherwise set errno to EIO. + */ +void virErrorSetErrnoFromLastError(void) +{ + virErrorPtr err = virGetLastError(); + if (err && err->code == VIR_ERR_SYSTEM_ERROR) { + errno = err->int1; + } else if (err && err->code == VIR_ERR_NO_MEMORY) { + errno = ENOMEM; + } else { + errno = EIO; + } +} + + +/** + * virLastErrorIsSystemErrno: + * @errnum: the errno value + * + * Check if the last error reported is a system + * error with the specific errno value. + * + * If @errnum is zero, any system error will pass. + * + * Returns true if the last error was a system error with errno == @errnum + */ +bool virLastErrorIsSystemErrno(int errnum) +{ + virErrorPtr err = virGetLastError(); + if (!err) + return false; + if (err->code != VIR_ERR_SYSTEM_ERROR) + return false; + if (errnum != 0 && err->int1 != errnum) + return false; + return true; +} diff --git a/src/util/virerror.h b/src/util/virerror.h index 6ea456b8f51a97c2869f3b3a18432e7751cc33ac..05e9950a9baadf32239650216d9b9d354dcf2782 100644 --- a/src/util/virerror.h +++ b/src/util/virerror.h @@ -175,4 +175,8 @@ const char *virStrerror(int theerrno, char *errBuf, size_t errBufLen); typedef int (*virErrorLogPriorityFunc)(virErrorPtr, int); void virSetErrorLogPriorityFunc(virErrorLogPriorityFunc func); +void virErrorSetErrnoFromLastError(void); + +bool virLastErrorIsSystemErrno(int errnum); + #endif