//---------------------------------------------------------------------------------------- string Str_error(const int err, const char *str) { const size_t max_str(256); char buf[max_str] {}; #ifdef _MSC_VER ignore_value(strerror_s(buf, max_str - 1, err)); #else ignore_value(strerror_r(err, buf, max_str - 1)); #endif if (str && *str) { ostringstream ostr; ostr << str << ": " << buf; return ostr.str(); } return string(buf); }
char * canonicalize_file_name(const char *path) { if (getenv("LIBVIRT_MTAB")) { const char *p; char *ret; if ((p = STRSKIP(path, "/some/symlink"))) ignore_value(virAsprintfQuiet(&ret, "/gluster%s", p)); else ignore_value(VIR_STRDUP_QUIET(ret, path)); return ret; } return real_canonicalize_file_name(path); }
int main (void) { /* Remove any leftovers from a previous partial run. */ ignore_value (system ("rm -rf " BASE "*")); return test_rmdir_func (rmdir, true); }
int main (void) { /* Remove any garbage left from previous partial runs. */ ignore_value (system ("rm -rf " BASE "*")); return test_link (link, true); }
int main (void) { /* Clean up any trash from prior testsuite runs. */ ignore_value (system ("rm -rf " BASE "*")); return test_mkdir (mkdir, true); }
static virObjectEventQueuePtr virObjectEventQueueNew(void) { virObjectEventQueuePtr ret; ignore_value(VIR_ALLOC(ret)); return ret; }
static char * hypervDomainGetOSType(virDomainPtr domain ATTRIBUTE_UNUSED) { char *osType; ignore_value(VIR_STRDUP(osType, "hvm")); return osType; }
int main (void) { /* Remove any leftovers from a previous partial run. */ ignore_value (system ("rm -rf " BASE "*")); return test_areadlink (do_areadlink, true); }
int main (void) { char buf[80]; int result; /* Remove any leftovers from a previous partial run. */ ignore_value (system ("rm -rf " BASE "*")); /* Perform same checks as counterpart functions. */ result = test_readlink (do_readlink, false); ASSERT (test_symlink (do_symlink, false) == result); dfd = openat (AT_FDCWD, ".", O_RDONLY); ASSERT (0 <= dfd); ASSERT (test_readlink (do_readlink, false) == result); ASSERT (test_symlink (do_symlink, false) == result); /* Now perform some cross-directory checks. Skip everything else on mingw. */ if (HAVE_SYMLINK) { const char *contents = "don't matter!"; ssize_t exp = strlen (contents); /* Create link while cwd is '.', then read it in '..'. */ ASSERT (symlinkat (contents, AT_FDCWD, BASE "link") == 0); errno = 0; ASSERT (symlinkat (contents, dfd, BASE "link") == -1); ASSERT (errno == EEXIST); ASSERT (chdir ("..") == 0); errno = 0; ASSERT (readlinkat (AT_FDCWD, BASE "link", buf, sizeof buf) == -1); ASSERT (errno == ENOENT); ASSERT (readlinkat (dfd, BASE "link", buf, sizeof buf) == exp); ASSERT (strncmp (contents, buf, exp) == 0); ASSERT (unlinkat (dfd, BASE "link", 0) == 0); /* Create link while cwd is '..', then read it in '.'. */ ASSERT (symlinkat (contents, dfd, BASE "link") == 0); ASSERT (fchdir (dfd) == 0); errno = 0; ASSERT (symlinkat (contents, AT_FDCWD, BASE "link") == -1); ASSERT (errno == EEXIST); buf[0] = '\0'; ASSERT (readlinkat (AT_FDCWD, BASE "link", buf, sizeof buf) == exp); ASSERT (strncmp (contents, buf, exp) == 0); buf[0] = '\0'; ASSERT (readlinkat (dfd, BASE "link", buf, sizeof buf) == exp); ASSERT (strncmp (contents, buf, exp) == 0); ASSERT (unlink (BASE "link") == 0); } ASSERT (close (dfd) == 0); if (result == 77) fputs ("skipping test: symlinks not supported on this file system\n", stderr); return result; }
int virNumaGetNodeCPUs(int node, virBitmapPtr *cpus) { unsigned long *mask = NULL; unsigned long *allonesmask = NULL; virBitmapPtr cpumap = NULL; int ncpus = 0; int max_n_cpus = virNumaGetMaxCPUs(); int mask_n_bytes = max_n_cpus / 8; size_t i; int ret = -1; *cpus = NULL; if (VIR_ALLOC_N(mask, mask_n_bytes / sizeof(*mask)) < 0) goto cleanup; if (VIR_ALLOC_N(allonesmask, mask_n_bytes / sizeof(*mask)) < 0) goto cleanup; memset(allonesmask, 0xff, mask_n_bytes); /* The first time this returns -1, ENOENT if node doesn't exist... */ if (numa_node_to_cpus(node, mask, mask_n_bytes) < 0) { VIR_WARN("NUMA topology for cell %d is not available, ignoring", node); ret = -2; goto cleanup; } /* second, third... times it returns an all-1's mask */ if (memcmp(mask, allonesmask, mask_n_bytes) == 0) { VIR_DEBUG("NUMA topology for cell %d is invalid, ignoring", node); ret = -2; goto cleanup; } if (!(cpumap = virBitmapNew(max_n_cpus))) goto cleanup; for (i = 0; i < max_n_cpus; i++) { if (MASK_CPU_ISSET(mask, i)) { ignore_value(virBitmapSetBit(cpumap, i)); ncpus++; } } *cpus = cpumap; cpumap = NULL; ret = ncpus; cleanup: VIR_FREE(mask); VIR_FREE(allonesmask); virBitmapFree(cpumap); return ret; }
/** * Create the supermin appliance directory under cachedir, if it does * not exist. * * Sanity-check that the permissions on the cachedir are safe, in case * it has been pre-created maliciously or tampered with. * * Returns the directory name which the caller must free. */ char * guestfs_int_lazy_make_supermin_appliance_dir (guestfs_h *g) { CLEANUP_FREE char *tmpdir = guestfs_get_cachedir (g); char *ret = NULL; struct stat statbuf; uid_t uid = geteuid (); ret = safe_asprintf (g, "%s/.guestfs-%ju", tmpdir, (uintmax_t) uid); ignore_value (mkdir (ret, 0755)); ignore_value (chmod (ret, 0755)); /* RHBZ#921292 */ /* See if the cache directory exists and passes some simple checks * to make sure it has not been tampered with. */ if (lstat (ret, &statbuf) == -1) { perrorf (g, _("stat: %s"), ret); free (ret); return NULL; } if (statbuf.st_uid != uid) { error (g, _("security: cached appliance %s is not owned by UID %ju"), ret, (uintmax_t) uid); free (ret); return NULL; } if (!S_ISDIR (statbuf.st_mode)) { error (g, _("security: cached appliance %s is not a directory (mode %o)"), ret, statbuf.st_mode); free (ret); return NULL; } if ((statbuf.st_mode & 0022) != 0) { error (g, _("security: cached appliance %s is writable by group or other (mode %o)"), ret, statbuf.st_mode); free (ret); return NULL; } /* "Touch" the directory. */ ignore_value (utimes (ret, NULL)); return ret; }
char * virTypedParameterToString(virTypedParameterPtr param) { char *value = NULL; switch (param->type) { case VIR_TYPED_PARAM_INT: ignore_value(virAsprintf(&value, "%d", param->value.i)); break; case VIR_TYPED_PARAM_UINT: ignore_value(virAsprintf(&value, "%u", param->value.ui)); break; case VIR_TYPED_PARAM_LLONG: ignore_value(virAsprintf(&value, "%lld", param->value.l)); break; case VIR_TYPED_PARAM_ULLONG: ignore_value(virAsprintf(&value, "%llu", param->value.ul)); break; case VIR_TYPED_PARAM_DOUBLE: ignore_value(virAsprintf(&value, "%g", param->value.d)); break; case VIR_TYPED_PARAM_BOOLEAN: ignore_value(virAsprintf(&value, "%d", param->value.b)); break; case VIR_TYPED_PARAM_STRING: ignore_value(VIR_STRDUP(value, param->value.s)); break; default: virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected type %d for field %s"), param->type, param->field); } return value; }
/** * virShrinkN: * @ptrptr: pointer to pointer for address of allocated memory * @size: number of bytes per element * @countptr: pointer to number of elements in array * @toremove: number of elements to remove * * Resize the block of memory in 'ptrptr' to be an array of * '*countptr' - 'toremove' elements, each 'size' bytes in length. * Update 'ptrptr' and 'countptr' with the details of the newly * allocated memory. If 'toremove' is larger than 'countptr', free * the entire array. */ void virShrinkN(void *ptrptr, size_t size, size_t *countptr, size_t toremove) { if (toremove < *countptr) ignore_value(virReallocN(ptrptr, size, *countptr -= toremove)); else { virFree(ptrptr); *countptr = 0; } }
/* qemuAliasTLSObjFromChardevAlias * @chardev_alias: Pointer to the chardev alias string * * Generate and return a string to be used as the TLS object alias */ char * qemuAliasTLSObjFromChardevAlias(const char *chardev_alias) { char *ret; ignore_value(virAsprintf(&ret, "obj%s_tls0", chardev_alias)); return ret; }
/** * virBitmapNewEmpty: * * Allocate an empty bitmap. It can be used with self-expanding APIs. * * Returns a pointer to the allocated bitmap or NULL if memory cannot be * allocated. Reports libvirt errors. */ virBitmapPtr virBitmapNewEmpty(void) { virBitmapPtr ret; ignore_value(VIR_ALLOC(ret)); return ret; }
int main (void) { int i; test_func funcs[2] = { mkfifoat, test_mknodat }; int result; /* Remove any leftovers from a previous partial run. */ ignore_value (system ("rm -rf " BASE "*")); /* Basic tests. */ result = test_mkfifo (do_mkfifoat, true); ASSERT (test_mkfifo (do_mknodat, false) == result); dfd = open (".", O_RDONLY); ASSERT (0 <= dfd); ASSERT (test_mkfifo (do_mkfifoat, false) == result); ASSERT (test_mkfifo (do_mknodat, false) == result); /* Test directory-relative handling of both functions. */ for (i = 0; i < 2; i++) { struct stat st; test_func func = funcs[i]; /* Create fifo while cwd is '.', then stat it from '..'. */ ASSERT (func (AT_FDCWD, BASE "fifo", 0600) == 0); errno = 0; ASSERT (func (dfd, BASE "fifo", 0600) == -1); ASSERT (errno == EEXIST); ASSERT (chdir ("..") == 0); errno = 0; ASSERT (fstatat (AT_FDCWD, BASE "fifo", &st, 0) == -1); ASSERT (errno == ENOENT); memset (&st, 0, sizeof st); ASSERT (fstatat (dfd, BASE "fifo", &st, 0) == 0); ASSERT (S_ISFIFO (st.st_mode)); ASSERT (unlinkat (dfd, BASE "fifo", 0) == 0); /* Create fifo while cwd is '..', then stat it from '.'. */ ASSERT (func (dfd, BASE "fifo", 0600) == 0); ASSERT (fchdir (dfd) == 0); errno = 0; ASSERT (func (AT_FDCWD, BASE "fifo", 0600) == -1); ASSERT (errno == EEXIST); memset (&st, 0, sizeof st); ASSERT (fstatat (AT_FDCWD, BASE "fifo", &st, AT_SYMLINK_NOFOLLOW) == 0); ASSERT (S_ISFIFO (st.st_mode)); memset (&st, 0, sizeof st); ASSERT (fstatat (dfd, BASE "fifo", &st, AT_SYMLINK_NOFOLLOW) == 0); ASSERT (S_ISFIFO (st.st_mode)); ASSERT (unlink (BASE "fifo") == 0); } ASSERT (close (dfd) == 0); return 0; }
static char * testDnsmasqLeaseFileName(const char *netname) { char *leasefile; ignore_value(virAsprintf(&leasefile, "/var/lib/libvirt/dnsmasq/%s.leases", netname)); return leasefile; }
/* qemuAliasChardevFromDevAlias: * @devAlias: pointer do device alias * * Generate and return a string to be used as chardev alias. */ char * qemuAliasChardevFromDevAlias(const char *devAlias) { char *ret; ignore_value(virAsprintf(&ret, "char%s", devAlias)); return ret; }
virDomainNumaPtr virDomainNumaNew(void) { virDomainNumaPtr ret = NULL; ignore_value(VIR_ALLOC(ret)); return ret; }
/* * Internal helper to ensure a generic error code is stored * in case where API returns failure, but forgot to set an * error */ static void virErrorGenericFailure(virErrorPtr err) { err->code = VIR_ERR_INTERNAL_ERROR; err->domain = VIR_FROM_NONE; err->level = VIR_ERR_ERROR; ignore_value(VIR_STRDUP_QUIET(err->message, _("An error occurred, but the cause is unknown"))); }
int main (void) { int result1; /* Skip because of no symlink support. */ int result2; /* Skip because of no futimens support. */ int result3; /* Skip because of no lutimens support. */ int fd; /* Clean up any trash from prior testsuite runs. */ ignore_value (system ("rm -rf " BASE "*")); /* Basic tests. */ result1 = test_utimens (do_utimens, true); ASSERT (test_utimens (do_fdutimens, false) == result1); result2 = test_futimens (do_futimens, result1 == 0); result3 = test_lutimens (do_lutimens, (result1 + result2) == 0); /* We expect 0/0, 0/77, or 77/77, but not 77/0. */ ASSERT (result1 <= result3); ASSERT (test_lutimens (do_lutimens1, (result1 + result2) == 0) == result3); dfd = open (".", O_RDONLY); ASSERT (0 <= dfd); ASSERT (test_utimens (do_utimens, false) == result1); ASSERT (test_utimens (do_fdutimens, false) == result1); ASSERT (test_futimens (do_futimens, false) == result2); ASSERT (test_lutimens (do_lutimens, false) == result3); ASSERT (test_lutimens (do_lutimens1, false) == result3); /* Directory relative tests. */ ASSERT (mkdir (BASE "dir", 0700) == 0); ASSERT (chdir (BASE "dir") == 0); fd = creat ("file", 0600); ASSERT (0 <= fd); errno = 0; ASSERT (fdutimensat (AT_FDCWD, fd, ".", NULL, 0) == -1); ASSERT (errno == ENOTDIR); { struct timespec ts[2] = { { Y2K, 0 }, { Y2K, 0 } }; struct stat st; ASSERT (fdutimensat (fd, dfd, BASE "dir/file", ts, 0) == 0); ASSERT (stat ("file", &st) == 0); ASSERT (st.st_atime == Y2K); ASSERT (get_stat_atime_ns (&st) == 0); ASSERT (st.st_mtime == Y2K); ASSERT (get_stat_mtime_ns (&st) == 0); } ASSERT (close (fd) == 0); ASSERT (close (dfd) == 0); errno = 0; ASSERT (fdutimensat (-1, dfd, ".", NULL, 0) == -1); ASSERT (errno == EBADF); /* Cleanup. */ ASSERT (chdir ("..") == 0); ASSERT (unlink (BASE "dir/file") == 0); ASSERT (rmdir (BASE "dir") == 0); return result1 | result2 | result3; }
/* qemuDomainGetMasterKeyAlias: * * Generate and return the masterKey alias * * Returns NULL or a string containing the master key alias */ char * qemuDomainGetMasterKeyAlias(void) { char *alias; ignore_value(VIR_STRDUP(alias, "masterKey0")); return alias; }
void ask (char const *format, ...) { static int ttyfd = -2; ssize_t r; va_list args; va_start (args, format); vfprintf (stdout, format, args); va_end (args); fflush (stdout); if (ttyfd == -2) { /* If standard output is not a tty, don't bother opening /dev/tty, since it's unlikely that stdout will be seen by the tty user. The isatty test also works around a bug in GNU Emacs 19.34 under Linux which makes a call-process 'patch' hang when it reads from /dev/tty. POSIX.1-2001 XCU line 26599 requires that we read /dev/tty, though. */ ttyfd = (posixly_correct || isatty (STDOUT_FILENO) ? open (TTY_DEVICE, O_RDONLY) : -1); } if (ttyfd < 0) { /* No terminal at all -- default it. */ printf ("\n"); buf[0] = '\n'; buf[1] = '\0'; } else { size_t s = 0; while ((r = read (ttyfd, buf + s, bufsize - 1 - s)) == bufsize - 1 - s && buf[bufsize - 2] != '\n') { s = bufsize - 1; bufsize *= 2; buf = realloc (buf, bufsize); if (!buf) xalloc_die (); } if (r == 0) printf ("EOF\n"); else if (r < 0) { error (0, errno, "tty read failed"); ignore_value (close (ttyfd)); ttyfd = -1; r = 0; } buf[s + r] = '\0'; } }
static void virLogDumpAllFD(const char *msg, int len) { int i, found = 0; if (len <= 0) len = strlen(msg); for (i = 0; i < virLogNbOutputs;i++) { if (virLogOutputs[i].f == virLogOutputToFd) { int fd = (intptr_t) virLogOutputs[i].data; if (fd >= 0) { ignore_value (safewrite(fd, msg, len)); found = 1; } } } if (!found) ignore_value (safewrite(STDERR_FILENO, msg, len)); }
static void virLogStackTraceToFd(int fd) { void *array[100]; int size; static bool doneWarning = false; const char *msg = "Stack trace not available on this platform\n"; #define STRIP_DEPTH 3 size = backtrace(array, ARRAY_CARDINALITY(array)); if (size) { backtrace_symbols_fd(array + STRIP_DEPTH, size - STRIP_DEPTH, fd); ignore_value(safewrite(fd, "\n", 1)); } else if (!doneWarning) { ignore_value(safewrite(fd, msg, strlen(msg))); doneWarning = true; } #undef STRIP_DEPTH }
static void qemuAgentDispose(void *obj) { qemuAgentPtr mon = obj; VIR_DEBUG("mon=%p", mon); if (mon->cb && mon->cb->destroy) (mon->cb->destroy)(mon, mon->vm); ignore_value(virCondDestroy(&mon->notify)); virMutexDestroy(&mon->lock); VIR_FREE(mon->buffer); }
/* Recursively remove a temporary directory. If removal fails, just * return (it's a temporary directory so it'll eventually be cleaned * up by a temp cleaner). This is done using "rm -rf" because that's * simpler and safer. */ void guestfs_int_recursive_remove_dir (guestfs_h *g, const char *dir) { CLEANUP_CMD_CLOSE struct command *cmd = guestfs_int_new_command (g); guestfs_int_cmd_add_arg (cmd, "rm"); guestfs_int_cmd_add_arg (cmd, "-rf"); guestfs_int_cmd_add_arg (cmd, dir); ignore_value (guestfs_int_cmd_run (cmd)); }
static void qemuTPMDeleteEmulatorStorage(virDomainTPMDefPtr tpm) { char *path = qemuTPMGetTPMStorageDir(tpm->data.emulator.storagepath); if (path) { ignore_value(virFileDeleteTree(path)); VIR_FREE(path); } }
/* qemuDomainGetSecretAESAlias: * @srcalias: Source alias used to generate the secret alias * @isLuks: True when we are generating a secret for LUKS encrypt/decrypt * * Generate and return an alias for the encrypted secret * * Returns NULL or a string containing the alias */ char * qemuDomainGetSecretAESAlias(const char *srcalias, bool isLuks) { char *alias; if (!srcalias) { virReportError(VIR_ERR_INVALID_ARG, "%s", _("encrypted secret alias requires valid source alias")); return NULL; } if (isLuks) ignore_value(virAsprintf(&alias, "%s-luks-secret0", srcalias)); else ignore_value(virAsprintf(&alias, "%s-secret0", srcalias)); return alias; }
static int envsort(const void *a, const void *b) { const char *const*astrptr = a; const char *const*bstrptr = b; const char *astr = *astrptr; const char *bstr = *bstrptr; char *aeq = strchr(astr, '='); char *beq = strchr(bstr, '='); char *akey; char *bkey; int ret; ignore_value(VIR_STRNDUP_QUIET(akey, astr, aeq - astr)); ignore_value(VIR_STRNDUP_QUIET(bkey, bstr, beq - bstr)); ret = strcmp(akey, bkey); VIR_FREE(akey); VIR_FREE(bkey); return ret; }