/** * virSaveLastError: * * Save the last error into a new error object. On success, errno is * unchanged; on failure, errno is ENOMEM. * * Returns a pointer to the copied error or NULL if allocation failed. * It is the caller's responsibility to free the error with * virFreeError(). */ virErrorPtr virSaveLastError(void) { virErrorPtr to; int saved_errno = errno; if (VIR_ALLOC_QUIET(to) < 0) return NULL; virCopyLastError(to); errno = saved_errno; return to; }
virErrorPtr virErrorCopyNew(virErrorPtr err) { virErrorPtr ret; if (VIR_ALLOC_QUIET(ret) < 0) return NULL; if (virCopyError(err, ret) < 0) VIR_FREE(ret); return ret; }
static virErrorPtr virLastErrorObject(void) { virErrorPtr err; err = virThreadLocalGet(&virLastErr); if (!err) { if (VIR_ALLOC_QUIET(err) < 0) return NULL; if (virThreadLocalSet(&virLastErr, err) < 0) VIR_FREE(err); } return err; }
static int virLogOnceInit(void) { if (virMutexInit(&virLogMutex) < 0) return -1; virLogLock(); virLogDefaultPriority = VIR_LOG_DEFAULT; if (VIR_ALLOC_QUIET(virLogRegex) >= 0) { if (regcomp(virLogRegex, VIR_LOG_REGEX, REG_EXTENDED) != 0) VIR_FREE(virLogRegex); } virLogUnlock(); return 0; }
void virNetMessageSaveError(virNetMessageErrorPtr rerr) { /* This func may be called several times & the first * error is the one we want because we don't want * cleanup code overwriting the first one. */ if (rerr->code != VIR_ERR_OK) return; memset(rerr, 0, sizeof(*rerr)); virErrorPtr verr = virGetLastError(); if (verr) { rerr->code = verr->code; rerr->domain = verr->domain; if (verr->message && VIR_ALLOC(rerr->message) == 0 && VIR_STRDUP_QUIET(*rerr->message, verr->message) < 0) VIR_FREE(rerr->message); rerr->level = verr->level; if (verr->str1 && VIR_ALLOC(rerr->str1) == 0 && VIR_STRDUP_QUIET(*rerr->str1, verr->str1) < 0) VIR_FREE(rerr->str1); if (verr->str2 && VIR_ALLOC(rerr->str2) == 0 && VIR_STRDUP_QUIET(*rerr->str2, verr->str2) < 0) VIR_FREE(rerr->str2); if (verr->str3 && VIR_ALLOC(rerr->str3) == 0 && VIR_STRDUP_QUIET(*rerr->str3, verr->str3) < 0) VIR_FREE(rerr->str3); rerr->int1 = verr->int1; rerr->int2 = verr->int2; } else { rerr->code = VIR_ERR_INTERNAL_ERROR; rerr->domain = VIR_FROM_RPC; if (VIR_ALLOC_QUIET(rerr->message) == 0 && VIR_STRDUP_QUIET(*rerr->message, _("Library function returned error but did not set virError")) < 0) VIR_FREE(rerr->message); rerr->level = VIR_ERR_ERROR; } }
static int virLogOnceInit(void) { const char *pbm = NULL; if (virMutexInit(&virLogMutex) < 0) return -1; virLogLock(); if (VIR_ALLOC_N_QUIET(virLogBuffer, virLogSize + 1) < 0) { /* * The debug buffer is not a critical component, allow startup * even in case of failure to allocate it in case of a * configuration mistake. */ virLogSize = 64 * 1024; if (VIR_ALLOC_N_QUIET(virLogBuffer, virLogSize + 1) < 0) { pbm = "Failed to allocate debug buffer: deactivating debug log\n"; virLogSize = 0; } else { pbm = "Failed to allocate debug buffer: reduced to 64 kB\n"; } } virLogLen = 0; virLogStart = 0; virLogEnd = 0; virLogDefaultPriority = VIR_LOG_DEFAULT; if (VIR_ALLOC_QUIET(virLogRegex) >= 0) { if (regcomp(virLogRegex, VIR_LOG_REGEX, REG_EXTENDED) != 0) VIR_FREE(virLogRegex); } virLogUnlock(); if (pbm) VIR_WARN("%s", pbm); return 0; }
/** * virBitmapNewQuiet: * @size: number of bits * * Allocate a bitmap capable of containing @size bits. * * Returns a pointer to the allocated bitmap or NULL if memory cannot be * allocated. Does not report libvirt errors. */ virBitmapPtr virBitmapNewQuiet(size_t size) { virBitmapPtr bitmap; size_t sz; if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size || size == 0) return NULL; sz = VIR_DIV_UP(size, VIR_BITMAP_BITS_PER_UNIT); if (VIR_ALLOC_QUIET(bitmap) < 0) return NULL; if (VIR_ALLOC_N_QUIET(bitmap->map, sz) < 0) { VIR_FREE(bitmap); return NULL; } bitmap->max_bit = size; bitmap->map_len = sz; return bitmap; }
int virThreadCreate(virThreadPtr thread, bool joinable, virThreadFunc func, void *opaque) { struct virThreadArgs *args; pthread_attr_t attr; int ret = -1; int err; if ((err = pthread_attr_init(&attr)) != 0) goto cleanup; if (VIR_ALLOC_QUIET(args) < 0) { err = ENOMEM; goto cleanup; } args->func = func; args->opaque = opaque; if (!joinable) pthread_attr_setdetachstate(&attr, 1); err = pthread_create(&thread->thread, &attr, virThreadHelper, args); if (err != 0) { VIR_FREE(args); goto cleanup; } /* New thread owns 'args' in success case, so don't free */ ret = 0; cleanup: pthread_attr_destroy(&attr); if (ret < 0) errno = err; return ret; }