1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

util: Use g_strdup_printf() instead of virAsprintf()

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Michal Privoznik 2019-10-22 15:26:14 +02:00
parent ad1118ebd2
commit 91d88aaf23
54 changed files with 432 additions and 741 deletions

View File

@ -158,8 +158,7 @@ char *virAuditEncode(const char *key, const char *value)
return audit_encode_nv_string(key, value, 0); return audit_encode_nv_string(key, value, 0);
#else #else
char *str; char *str;
if (virAsprintf(&str, "%s=%s", key, value) < 0) str = g_strdup_printf("%s=%s", key, value);
return NULL;
return str; return str;
#endif #endif
} }

View File

@ -69,8 +69,7 @@ virAuthGetConfigFilePathURI(virURIPtr uri,
if (!(userdir = virGetUserConfigDirectory())) if (!(userdir = virGetUserConfigDirectory()))
return -1; return -1;
if (virAsprintf(path, "%s/auth.conf", userdir) < 0) *path = g_strdup_printf("%s/auth.conf", userdir);
return -1;
VIR_DEBUG("Checking for readability of '%s'", *path); VIR_DEBUG("Checking for readability of '%s'", *path);
if (access(*path, R_OK) == 0) if (access(*path, R_OK) == 0)
@ -158,13 +157,10 @@ virAuthGetUsernamePath(const char *path,
memset(&cred, 0, sizeof(virConnectCredential)); memset(&cred, 0, sizeof(virConnectCredential));
if (defaultUsername != NULL) { if (defaultUsername != NULL) {
if (virAsprintf(&prompt, _("Enter username for %s [%s]"), hostname, prompt = g_strdup_printf(_("Enter username for %s [%s]"), hostname,
defaultUsername) < 0) { defaultUsername);
return NULL;
}
} else { } else {
if (virAsprintf(&prompt, _("Enter username for %s"), hostname) < 0) prompt = g_strdup_printf(_("Enter username for %s"), hostname);
return NULL;
} }
for (ncred = 0; ncred < auth->ncredtype; ncred++) { for (ncred = 0; ncred < auth->ncredtype; ncred++) {
@ -241,10 +237,7 @@ virAuthGetPasswordPath(const char *path,
memset(&cred, 0, sizeof(virConnectCredential)); memset(&cred, 0, sizeof(virConnectCredential));
if (virAsprintf(&prompt, _("Enter %s's password for %s"), username, prompt = g_strdup_printf(_("Enter %s's password for %s"), username, hostname);
hostname) < 0) {
return NULL;
}
for (ncred = 0; ncred < auth->ncredtype; ncred++) { for (ncred = 0; ncred < auth->ncredtype; ncred++) {
if (auth->credtype[ncred] != VIR_CRED_PASSPHRASE && if (auth->credtype[ncred] != VIR_CRED_PASSPHRASE &&

View File

@ -113,13 +113,11 @@ int virAuthConfigLookup(virAuthConfigPtr auth,
if (!hostname) if (!hostname)
hostname = "localhost"; hostname = "localhost";
if (virAsprintf(&authgroup, "auth-%s-%s", service, hostname) < 0) authgroup = g_strdup_printf("auth-%s-%s", service, hostname);
return -1;
if (!virKeyFileHasGroup(auth->keyfile, authgroup)) { if (!virKeyFileHasGroup(auth->keyfile, authgroup)) {
VIR_FREE(authgroup); VIR_FREE(authgroup);
if (virAsprintf(&authgroup, "auth-%s-%s", service, "default") < 0) authgroup = g_strdup_printf("auth-%s-%s", service, "default");
return -1;
} }
if (!virKeyFileHasGroup(auth->keyfile, authgroup)) if (!virKeyFileHasGroup(auth->keyfile, authgroup))
@ -132,8 +130,7 @@ int virAuthConfigLookup(virAuthConfigPtr auth,
return -1; return -1;
} }
if (virAsprintf(&credgroup, "credentials-%s", authcred) < 0) credgroup = g_strdup_printf("credentials-%s", authcred);
return -1;
if (!virKeyFileHasGroup(auth->keyfile, credgroup)) { if (!virKeyFileHasGroup(auth->keyfile, credgroup)) {
virReportError(VIR_ERR_CONF_SYNTAX, virReportError(VIR_ERR_CONF_SYNTAX,

View File

@ -206,8 +206,7 @@ virCgroupPartitionEscape(char **path)
if ((rc = virCgroupPartitionNeedsEscaping(*path)) <= 0) if ((rc = virCgroupPartitionNeedsEscaping(*path)) <= 0)
return rc; return rc;
if (virAsprintf(&newstr, "_%s", *path) < 0) newstr = g_strdup_printf("_%s", *path);
return -1;
VIR_FREE(*path); VIR_FREE(*path);
*path = newstr; *path = newstr;
@ -291,9 +290,7 @@ virCgroupDetectPlacement(virCgroupPtr group,
if (pid == -1) { if (pid == -1) {
procfile = g_strdup("/proc/self/cgroup"); procfile = g_strdup("/proc/self/cgroup");
} else { } else {
if (virAsprintf(&procfile, "/proc/%lld/cgroup", procfile = g_strdup_printf("/proc/%lld/cgroup", (long long)pid);
(long long) pid) < 0)
goto cleanup;
} }
mapping = fopen(procfile, "r"); mapping = fopen(procfile, "r");
@ -446,8 +443,7 @@ virCgroupGetBlockDevString(const char *path)
/* Automatically append space after the string since all callers /* Automatically append space after the string since all callers
* use it anyway */ * use it anyway */
if (virAsprintf(&ret, "%d:%d ", major(sb.st_rdev), minor(sb.st_rdev)) < 0) ret = g_strdup_printf("%d:%d ", major(sb.st_rdev), minor(sb.st_rdev));
return NULL;
return ret; return ret;
} }
@ -563,8 +559,7 @@ virCgroupSetValueU64(virCgroupPtr group,
{ {
g_autofree char *strval = NULL; g_autofree char *strval = NULL;
if (virAsprintf(&strval, "%llu", value) < 0) strval = g_strdup_printf("%llu", value);
return -1;
return virCgroupSetValueStr(group, controller, key, strval); return virCgroupSetValueStr(group, controller, key, strval);
} }
@ -578,8 +573,7 @@ virCgroupSetValueI64(virCgroupPtr group,
{ {
g_autofree char *strval = NULL; g_autofree char *strval = NULL;
if (virAsprintf(&strval, "%lld", value) < 0) strval = g_strdup_printf("%lld", value);
return -1;
return virCgroupSetValueStr(group, controller, key, strval); return virCgroupSetValueStr(group, controller, key, strval);
} }
@ -682,11 +676,8 @@ virCgroupNew(pid_t pid,
if (path[0] == '/' || !parent) { if (path[0] == '/' || !parent) {
(*group)->path = g_strdup(path); (*group)->path = g_strdup(path);
} else { } else {
if (virAsprintf(&(*group)->path, "%s%s%s", (*group)->path = g_strdup_printf("%s%s%s", parent->path,
parent->path, STREQ(parent->path, "") ? "" : "/", path);
STREQ(parent->path, "") ? "" : "/",
path) < 0)
goto error;
} }
if (virCgroupDetect(*group, pid, controllers, path, parent) < 0) if (virCgroupDetect(*group, pid, controllers, path, parent) < 0)
@ -918,9 +909,7 @@ virCgroupNewDomainPartition(virCgroupPtr partition,
{ {
g_autofree char *grpname = NULL; g_autofree char *grpname = NULL;
if (virAsprintf(&grpname, "%s.libvirt-%s", grpname = g_strdup_printf("%s.libvirt-%s", name, driver);
name, driver) < 0)
return -1;
if (virCgroupPartitionEscape(&grpname) < 0) if (virCgroupPartitionEscape(&grpname) < 0)
return -1; return -1;
@ -971,15 +960,13 @@ virCgroupNewThread(virCgroupPtr domain,
switch (nameval) { switch (nameval) {
case VIR_CGROUP_THREAD_VCPU: case VIR_CGROUP_THREAD_VCPU:
if (virAsprintf(&name, "vcpu%d", id) < 0) name = g_strdup_printf("vcpu%d", id);
return -1;
break; break;
case VIR_CGROUP_THREAD_EMULATOR: case VIR_CGROUP_THREAD_EMULATOR:
name = g_strdup("emulator"); name = g_strdup("emulator");
break; break;
case VIR_CGROUP_THREAD_IOTHREAD: case VIR_CGROUP_THREAD_IOTHREAD:
if (virAsprintf(&name, "iothread%d", id) < 0) name = g_strdup_printf("iothread%d", id);
return -1;
break; break;
case VIR_CGROUP_THREAD_LAST: case VIR_CGROUP_THREAD_LAST:
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
@ -2356,10 +2343,8 @@ virCgroupRemoveRecursively(char *grppath)
if (ent->d_type != DT_DIR) continue; if (ent->d_type != DT_DIR) continue;
if (virAsprintf(&path, "%s/%s", grppath, ent->d_name) == -1) { path = g_strdup_printf("%s/%s", grppath, ent->d_name);
rc = -ENOMEM;
break;
}
rc = virCgroupRemoveRecursively(path); rc = virCgroupRemoveRecursively(path);
if (rc != 0) if (rc != 0)
break; break;

View File

@ -97,16 +97,15 @@ virCgroupV1ValidateMachineGroup(virCgroupPtr group,
g_autofree char *scopename_new = NULL; g_autofree char *scopename_new = NULL;
g_autofree char *partmachinename = NULL; g_autofree char *partmachinename = NULL;
if (virAsprintf(&partname, "%s.libvirt-%s", partname = g_strdup_printf("%s.libvirt-%s", name, drivername);
name, drivername) < 0)
return false;
if (virCgroupPartitionEscape(&partname) < 0) if (virCgroupPartitionEscape(&partname) < 0)
return false; return false;
if (virAsprintf(&partmachinename, "%s.libvirt-%s", partmachinename = g_strdup_printf("%s.libvirt-%s",
machinename, drivername) < 0 || machinename, drivername);
virCgroupPartitionEscape(&partmachinename) < 0)
if (virCgroupPartitionEscape(&partmachinename) < 0)
return false; return false;
if (!(scopename_old = virSystemdMakeScopeName(name, drivername, true))) if (!(scopename_old = virSystemdMakeScopeName(name, drivername, true)))
@ -199,18 +198,16 @@ virCgroupV1CopyPlacement(virCgroupPtr group,
if (path[0] == '/') { if (path[0] == '/') {
group->legacy[i].placement = g_strdup(path); group->legacy[i].placement = g_strdup(path);
} else { } else {
bool delim = STREQ(parent->legacy[i].placement, "/") || STREQ(path, "");
/* /*
* parent == "/" + path="" => "/" * parent == "/" + path="" => "/"
* parent == "/libvirt.service" + path == "" => "/libvirt.service" * parent == "/libvirt.service" + path == "" => "/libvirt.service"
* parent == "/libvirt.service" + path == "foo" => "/libvirt.service/foo" * parent == "/libvirt.service" + path == "foo" => "/libvirt.service/foo"
*/ */
if (virAsprintf(&group->legacy[i].placement, group->legacy[i].placement = g_strdup_printf("%s%s%s",
"%s%s%s", parent->legacy[i].placement,
parent->legacy[i].placement, delim ? "" : "/",
(STREQ(parent->legacy[i].placement, "/") || path);
STREQ(path, "") ? "" : "/"),
path) < 0)
return -1;
} }
} }
@ -241,8 +238,7 @@ virCgroupV1ResolveMountLink(const char *mntDir,
return 0; return 0;
*dirName = '\0'; *dirName = '\0';
if (virAsprintf(&linkSrc, "%s/%s", tmp, typeStr) < 0) linkSrc = g_strdup_printf("%s/%s", tmp, typeStr);
return -1;
*dirName = '/'; *dirName = '/';
if (lstat(linkSrc, &sb) < 0) { if (lstat(linkSrc, &sb) < 0) {
@ -354,12 +350,11 @@ virCgroupV1DetectPlacement(virCgroupPtr group,
if (i == VIR_CGROUP_CONTROLLER_SYSTEMD) { if (i == VIR_CGROUP_CONTROLLER_SYSTEMD) {
group->legacy[i].placement = g_strdup(selfpath); group->legacy[i].placement = g_strdup(selfpath);
} else { } else {
if (virAsprintf(&group->legacy[i].placement, bool delim = STREQ(selfpath, "/") || STREQ(path, "");
"%s%s%s", selfpath,
(STREQ(selfpath, "/") || group->legacy[i].placement = g_strdup_printf("%s%s%s", selfpath,
STREQ(path, "") ? "" : "/"), delim ? "" : "/",
path) < 0) path);
return -1;
} }
} }
} }
@ -520,11 +515,8 @@ virCgroupV1PathOfController(virCgroupPtr group,
return -1; return -1;
} }
if (virAsprintf(path, "%s%s/%s", *path = g_strdup_printf("%s%s/%s", group->legacy[controller].mountPoint,
group->legacy[controller].mountPoint, group->legacy[controller].placement, NULLSTR_EMPTY(key));
group->legacy[controller].placement,
NULLSTR_EMPTY(key)) < 0)
return -1;
return 0; return 0;
} }
@ -813,9 +805,7 @@ virCgroupV1BindMount(virCgroupPtr group,
return -1; return -1;
} }
if (virAsprintf(&opts, opts = g_strdup_printf("mode=755,size=65536%s", mountopts);
"mode=755,size=65536%s", mountopts) < 0)
return -1;
if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, opts) < 0) { if (mount("tmpfs", root, "tmpfs", MS_NOSUID|MS_NODEV|MS_NOEXEC, opts) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -830,10 +820,7 @@ virCgroupV1BindMount(virCgroupPtr group,
if (!virFileExists(group->legacy[i].mountPoint)) { if (!virFileExists(group->legacy[i].mountPoint)) {
g_autofree char *src = NULL; g_autofree char *src = NULL;
if (virAsprintf(&src, "%s%s", src = g_strdup_printf("%s%s", oldroot, group->legacy[i].mountPoint);
oldroot,
group->legacy[i].mountPoint) < 0)
return -1;
VIR_DEBUG("Create mount point '%s'", VIR_DEBUG("Create mount point '%s'",
group->legacy[i].mountPoint); group->legacy[i].mountPoint);
@ -893,9 +880,8 @@ virCgroupV1SetOwner(virCgroupPtr cgroup,
if (!cgroup->legacy[i].mountPoint) if (!cgroup->legacy[i].mountPoint)
continue; continue;
if (virAsprintf(&base, "%s%s", cgroup->legacy[i].mountPoint, base = g_strdup_printf("%s%s", cgroup->legacy[i].mountPoint,
cgroup->legacy[i].placement) < 0) cgroup->legacy[i].placement);
goto cleanup;
if (virDirOpen(&dh, base) < 0) if (virDirOpen(&dh, base) < 0)
goto cleanup; goto cleanup;
@ -903,8 +889,7 @@ virCgroupV1SetOwner(virCgroupPtr cgroup,
while ((direrr = virDirRead(dh, &de, base)) > 0) { while ((direrr = virDirRead(dh, &de, base)) > 0) {
g_autofree char *entry = NULL; g_autofree char *entry = NULL;
if (virAsprintf(&entry, "%s/%s", base, de->d_name) < 0) entry = g_strdup_printf("%s/%s", base, de->d_name);
goto cleanup;
if (chown(entry, uid, gid) < 0) { if (chown(entry, uid, gid) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -961,8 +946,7 @@ virCgroupV1SetBlkioWeight(virCgroupPtr group,
return -1; return -1;
} }
if (virAsprintf(&value, "%u", weight) < 0) value = g_strdup_printf("%u", weight);
return -1;
return virCgroupSetValueRaw(path, value); return virCgroupSetValueRaw(path, value);
} }
@ -1204,8 +1188,7 @@ virCgroupV1SetBlkioDeviceWeight(virCgroupPtr group,
if (!(blkstr = virCgroupGetBlockDevString(devPath))) if (!(blkstr = virCgroupGetBlockDevString(devPath)))
return -1; return -1;
if (virAsprintf(&str, "%s%d", blkstr, weight) < 0) str = g_strdup_printf("%s%d", blkstr, weight);
return -1;
if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO, if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
"blkio.weight_device", &path) < 0) { "blkio.weight_device", &path) < 0) {
@ -1272,8 +1255,7 @@ virCgroupV1SetBlkioDeviceReadIops(virCgroupPtr group,
if (!(blkstr = virCgroupGetBlockDevString(path))) if (!(blkstr = virCgroupGetBlockDevString(path)))
return -1; return -1;
if (virAsprintf(&str, "%s%u", blkstr, riops) < 0) str = g_strdup_printf("%s%u", blkstr, riops);
return -1;
return virCgroupSetValueStr(group, return virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
@ -1324,8 +1306,7 @@ virCgroupV1SetBlkioDeviceWriteIops(virCgroupPtr group,
if (!(blkstr = virCgroupGetBlockDevString(path))) if (!(blkstr = virCgroupGetBlockDevString(path)))
return -1; return -1;
if (virAsprintf(&str, "%s%u", blkstr, wiops) < 0) str = g_strdup_printf("%s%u", blkstr, wiops);
return -1;
return virCgroupSetValueStr(group, return virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
@ -1376,8 +1357,7 @@ virCgroupV1SetBlkioDeviceReadBps(virCgroupPtr group,
if (!(blkstr = virCgroupGetBlockDevString(path))) if (!(blkstr = virCgroupGetBlockDevString(path)))
return -1; return -1;
if (virAsprintf(&str, "%s%llu", blkstr, rbps) < 0) str = g_strdup_printf("%s%llu", blkstr, rbps);
return -1;
return virCgroupSetValueStr(group, return virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
@ -1428,8 +1408,7 @@ virCgroupV1SetBlkioDeviceWriteBps(virCgroupPtr group,
if (!(blkstr = virCgroupGetBlockDevString(path))) if (!(blkstr = virCgroupGetBlockDevString(path)))
return -1; return -1;
if (virAsprintf(&str, "%s%llu", blkstr, wbps) < 0) str = g_strdup_printf("%s%llu", blkstr, wbps);
return -1;
return virCgroupSetValueStr(group, return virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_BLKIO, VIR_CGROUP_CONTROLLER_BLKIO,
@ -1780,17 +1759,16 @@ virCgroupV1AllowDevice(virCgroupPtr group,
if (major < 0) if (major < 0)
majorstr = g_strdup("*"); majorstr = g_strdup("*");
if (major >= 0 && virAsprintf(&majorstr, "%i", major) < 0) else
return -1; majorstr = g_strdup_printf("%i", major);
if (minor < 0) if (minor < 0)
minorstr = g_strdup("*"); minorstr = g_strdup("*");
if (minor >= 0 && virAsprintf(&minorstr, "%i", minor) < 0) else
return -1; minorstr = g_strdup_printf("%i", minor);
if (virAsprintf(&devstr, "%c %s:%s %s", type, majorstr, minorstr, devstr = g_strdup_printf("%c %s:%s %s", type, majorstr, minorstr,
virCgroupGetDevicePermsString(perms)) < 0) virCgroupGetDevicePermsString(perms));
return -1;
if (virCgroupSetValueStr(group, if (virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_DEVICES, VIR_CGROUP_CONTROLLER_DEVICES,
@ -1815,17 +1793,16 @@ virCgroupV1DenyDevice(virCgroupPtr group,
if (major < 0) if (major < 0)
majorstr = g_strdup("*"); majorstr = g_strdup("*");
if (major >= 0 && virAsprintf(&majorstr, "%i", major) < 0) else
return -1; majorstr = g_strdup_printf("%i", major);
if (minor < 0) if (minor < 0)
minorstr = g_strdup("*"); minorstr = g_strdup("*");
if (minor >= 0 && virAsprintf(&minorstr, "%i", minor) < 0) else
return -1; minorstr = g_strdup_printf("%i", minor);
if (virAsprintf(&devstr, "%c %s:%s %s", type, majorstr, minorstr, devstr = g_strdup_printf("%c %s:%s %s", type, majorstr, minorstr,
virCgroupGetDevicePermsString(perms)) < 0) virCgroupGetDevicePermsString(perms));
return -1;
if (virCgroupSetValueStr(group, if (virCgroupSetValueStr(group,
VIR_CGROUP_CONTROLLER_DEVICES, VIR_CGROUP_CONTROLLER_DEVICES,

View File

@ -75,8 +75,7 @@ virCgroupV2Available(void)
/* Systemd uses cgroup v2 for process tracking but no controller is /* Systemd uses cgroup v2 for process tracking but no controller is
* available. We should consider this configuration as cgroup v2 is * available. We should consider this configuration as cgroup v2 is
* not available. */ * not available. */
if (virAsprintf(&contFile, "%s/cgroup.controllers", entry.mnt_dir) < 0) contFile = g_strdup_printf("%s/cgroup.controllers", entry.mnt_dir);
goto cleanup;
if (virFileReadAll(contFile, 1024 * 1024, &contStr) < 0) if (virFileReadAll(contFile, 1024 * 1024, &contStr) < 0)
goto cleanup; goto cleanup;
@ -104,10 +103,7 @@ virCgroupV2ValidateMachineGroup(virCgroupPtr group,
g_autofree char *scopename = NULL; g_autofree char *scopename = NULL;
char *tmp; char *tmp;
if (virAsprintf(&partmachinename, "%s.libvirt-%s", machinename, partmachinename = g_strdup_printf("%s.libvirt-%s", machinename, drivername);
drivername) < 0) {
return false;
}
if (virCgroupPartitionEscape(&partmachinename) < 0) if (virCgroupPartitionEscape(&partmachinename) < 0)
return false; return false;
@ -161,17 +157,16 @@ virCgroupV2CopyPlacement(virCgroupPtr group,
if (path[0] == '/') { if (path[0] == '/') {
group->unified.placement = g_strdup(path); group->unified.placement = g_strdup(path);
} else { } else {
bool delim = STREQ(parent->unified.placement, "/") || STREQ(path, "");
/* /*
* parent == "/" + path="" => "/" * parent == "/" + path="" => "/"
* parent == "/libvirt.service" + path == "" => "/libvirt.service" * parent == "/libvirt.service" + path == "" => "/libvirt.service"
* parent == "/libvirt.service" + path == "foo" => "/libvirt.service/foo" * parent == "/libvirt.service" + path == "foo" => "/libvirt.service/foo"
*/ */
if (virAsprintf(&group->unified.placement, "%s%s%s", group->unified.placement = g_strdup_printf("%s%s%s",
parent->unified.placement, parent->unified.placement,
(STREQ(parent->unified.placement, "/") || delim ? "" : "/",
STREQ(path, "") ? "" : "/"), path);
path) < 0)
return -1;
} }
return 0; return 0;
@ -215,12 +210,8 @@ virCgroupV2DetectPlacement(virCgroupPtr group,
* selfpath == "/libvirt.service" + path == "" -> "/libvirt.service" * selfpath == "/libvirt.service" + path == "" -> "/libvirt.service"
* selfpath == "/libvirt.service" + path == "foo" -> "/libvirt.service/foo" * selfpath == "/libvirt.service" + path == "foo" -> "/libvirt.service/foo"
*/ */
if (virAsprintf(&group->unified.placement, group->unified.placement = g_strdup_printf("%s%s%s", selfpath,
"%s%s%s", selfpath, (STREQ(selfpath, "/") || STREQ(path, "") ? "" : "/"), path);
(STREQ(selfpath, "/") ||
STREQ(path, "") ? "" : "/"),
path) < 0)
return -1;
return 0; return 0;
} }
@ -258,15 +249,13 @@ virCgroupV2ParseControllersFile(virCgroupPtr group,
char **tmp; char **tmp;
if (parent) { if (parent) {
if (virAsprintf(&contFile, "%s%s/cgroup.subtree_control", contFile = g_strdup_printf("%s%s/cgroup.subtree_control",
parent->unified.mountPoint, parent->unified.mountPoint,
NULLSTR_EMPTY(parent->unified.placement)) < 0) NULLSTR_EMPTY(parent->unified.placement));
return -1;
} else { } else {
if (virAsprintf(&contFile, "%s%s/cgroup.controllers", contFile = g_strdup_printf("%s%s/cgroup.controllers",
group->unified.mountPoint, group->unified.mountPoint,
NULLSTR_EMPTY(group->unified.placement)) < 0) NULLSTR_EMPTY(group->unified.placement));
return -1;
} }
rc = virFileReadAll(contFile, 1024 * 1024, &contStr); rc = virFileReadAll(contFile, 1024 * 1024, &contStr);
@ -353,11 +342,8 @@ virCgroupV2PathOfController(virCgroupPtr group,
return -1; return -1;
} }
if (virAsprintf(path, "%s%s/%s", *path = g_strdup_printf("%s%s/%s", group->unified.mountPoint,
group->unified.mountPoint, group->unified.placement, NULLSTR_EMPTY(key));
group->unified.placement,
NULLSTR_EMPTY(key)) < 0)
return -1;
return 0; return 0;
} }
@ -379,10 +365,7 @@ virCgroupV2EnableController(virCgroupPtr group,
g_autofree char *val = NULL; g_autofree char *val = NULL;
g_autofree char *path = NULL; g_autofree char *path = NULL;
if (virAsprintf(&val, "+%s", val = g_strdup_printf("+%s", virCgroupV2ControllerTypeToString(controller));
virCgroupV2ControllerTypeToString(controller)) < 0) {
return -1;
}
if (virCgroupPathOfController(parent, controller, if (virCgroupPathOfController(parent, controller,
"cgroup.subtree_control", &path) < 0) { "cgroup.subtree_control", &path) < 0) {
@ -564,11 +547,9 @@ virCgroupV2BindMount(virCgroupPtr group,
return -1; return -1;
} }
if (virAsprintf(&opts, "mode=755,size=65536%s", mountopts) < 0) opts = g_strdup_printf("mode=755,size=65536%s", mountopts);
return -1;
if (virAsprintf(&src, "%s%s", oldroot, group->unified.mountPoint) < 0) src = g_strdup_printf("%s%s", oldroot, group->unified.mountPoint);
return -1;
if (mount(src, group->unified.mountPoint, "none", MS_BIND, NULL) < 0) { if (mount(src, group->unified.mountPoint, "none", MS_BIND, NULL) < 0) {
virReportSystemError(errno, _("Failed to bind cgroup '%s' on '%s'"), virReportSystemError(errno, _("Failed to bind cgroup '%s' on '%s'"),
@ -588,10 +569,8 @@ virCgroupV2SetOwner(virCgroupPtr cgroup,
{ {
g_autofree char *base = NULL; g_autofree char *base = NULL;
if (virAsprintf(&base, "%s%s", cgroup->unified.mountPoint, base = g_strdup_printf("%s%s", cgroup->unified.mountPoint,
cgroup->unified.placement) < 0) { cgroup->unified.placement);
return -1;
}
if (virFileChownFiles(base, uid, gid) < 0) if (virFileChownFiles(base, uid, gid) < 0)
return -1; return -1;
@ -635,8 +614,7 @@ virCgroupV2SetBlkioWeight(virCgroupPtr group,
return -1; return -1;
} }
if (virAsprintf(&value, format, weight) < 0) value = g_strdup_printf(format, weight);
return -1;
return virCgroupSetValueRaw(path, value); return virCgroupSetValueRaw(path, value);
} }
@ -829,8 +807,7 @@ virCgroupV2SetBlkioDeviceWeight(virCgroupPtr group,
if (!(blkstr = virCgroupGetBlockDevString(devPath))) if (!(blkstr = virCgroupGetBlockDevString(devPath)))
return -1; return -1;
if (virAsprintf(&str, "%s%d", blkstr, weight) < 0) str = g_strdup_printf("%s%d", blkstr, weight);
return -1;
if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO, if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
"io.weight", &path) < 0) { "io.weight", &path) < 0) {
@ -899,11 +876,9 @@ virCgroupV2SetBlkioDeviceReadIops(virCgroupPtr group,
return -1; return -1;
if (riops == 0) { if (riops == 0) {
if (virAsprintf(&str, "%sriops=max", blkstr) < 0) str = g_strdup_printf("%sriops=max", blkstr);
return -1;
} else { } else {
if (virAsprintf(&str, "%sriops=%u", blkstr, riops) < 0) str = g_strdup_printf("%sriops=%u", blkstr, riops);
return -1;
} }
return virCgroupSetValueStr(group, return virCgroupSetValueStr(group,
@ -970,11 +945,9 @@ virCgroupV2SetBlkioDeviceWriteIops(virCgroupPtr group,
return -1; return -1;
if (wiops == 0) { if (wiops == 0) {
if (virAsprintf(&str, "%swiops=max", blkstr) < 0) str = g_strdup_printf("%swiops=max", blkstr);
return -1;
} else { } else {
if (virAsprintf(&str, "%swiops=%u", blkstr, wiops) < 0) str = g_strdup_printf("%swiops=%u", blkstr, wiops);
return -1;
} }
return virCgroupSetValueStr(group, return virCgroupSetValueStr(group,
@ -1041,11 +1014,9 @@ virCgroupV2SetBlkioDeviceReadBps(virCgroupPtr group,
return -1; return -1;
if (rbps == 0) { if (rbps == 0) {
if (virAsprintf(&str, "%srbps=max", blkstr) < 0) str = g_strdup_printf("%srbps=max", blkstr);
return -1;
} else { } else {
if (virAsprintf(&str, "%srbps=%llu", blkstr, rbps) < 0) str = g_strdup_printf("%srbps=%llu", blkstr, rbps);
return -1;
} }
return virCgroupSetValueStr(group, return virCgroupSetValueStr(group,
@ -1112,11 +1083,9 @@ virCgroupV2SetBlkioDeviceWriteBps(virCgroupPtr group,
return -1; return -1;
if (wbps == 0) { if (wbps == 0) {
if (virAsprintf(&str, "%swbps=max", blkstr) < 0) str = g_strdup_printf("%swbps=max", blkstr);
return -1;
} else { } else {
if (virAsprintf(&str, "%swbps=%llu", blkstr, wbps) < 0) str = g_strdup_printf("%swbps=%llu", blkstr, wbps);
return -1;
} }
return virCgroupSetValueStr(group, return virCgroupSetValueStr(group,
@ -1516,8 +1485,7 @@ virCgroupV2SetCpuCfsPeriod(virCgroupPtr group,
} }
*tmp = '\0'; *tmp = '\0';
if (virAsprintf(&value, "%s %llu", str, cfs_period) < 0) value = g_strdup_printf("%s %llu", str, cfs_period);
return -1;
return virCgroupSetValueStr(group, VIR_CGROUP_CONTROLLER_CPU, return virCgroupSetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
"cpu.max", value); "cpu.max", value);

View File

@ -1514,19 +1514,13 @@ virConfLoadConfigPath(const char *name)
{ {
char *path; char *path;
if (geteuid() == 0) { if (geteuid() == 0) {
if (virAsprintf(&path, "%s/libvirt/%s", path = g_strdup_printf("%s/libvirt/%s", SYSCONFDIR, name);
SYSCONFDIR, name) < 0)
return NULL;
} else { } else {
char *userdir = virGetUserConfigDirectory(); char *userdir = virGetUserConfigDirectory();
if (!userdir) if (!userdir)
return NULL; return NULL;
if (virAsprintf(&path, "%s/%s", path = g_strdup_printf("%s/%s", userdir, name);
userdir, name) < 0) {
VIR_FREE(userdir);
return NULL;
}
VIR_FREE(userdir); VIR_FREE(userdir);
} }

View File

@ -127,10 +127,9 @@ virDevMapperGetTargetsImpl(const char *path,
goto cleanup; goto cleanup;
for (i = 0; i < deps->count; i++) { for (i = 0; i < deps->count; i++) {
if (virAsprintfQuiet(&devPaths[i], "/dev/block/%u:%u", devPaths[i] = g_strdup_printf("/dev/block/%u:%u",
major(deps->device[i]), major(deps->device[i]),
minor(deps->device[i])) < 0) minor(deps->device[i]));
goto cleanup;
} }
recursiveDevPaths = NULL; recursiveDevPaths = NULL;

View File

@ -177,8 +177,7 @@ addnhostsWrite(const char *path,
* for runtime addition. * for runtime addition.
*/ */
if (virAsprintf(&tmp, "%s.new", path) < 0) tmp = g_strdup_printf("%s.new", path);
return -ENOMEM;
if (!(f = fopen(tmp, "w"))) { if (!(f = fopen(tmp, "w"))) {
istmp = false; istmp = false;
@ -309,30 +308,24 @@ hostsfileAdd(dnsmasqHostsfile *hostsfile,
/* the first test determines if it is a dhcpv6 host */ /* the first test determines if it is a dhcpv6 host */
if (ipv6) { if (ipv6) {
if (name && id) { if (name && id) {
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, hostsfile->hosts[hostsfile->nhosts].host = g_strdup_printf("id:%s,%s,[%s]",
"id:%s,%s,[%s]", id, name, ipstr) < 0) id, name, ipstr);
goto error;
} else if (name && !id) { } else if (name && !id) {
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, hostsfile->hosts[hostsfile->nhosts].host = g_strdup_printf("%s,[%s]",
"%s,[%s]", name, ipstr) < 0) name, ipstr);
goto error;
} else if (!name && id) { } else if (!name && id) {
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, hostsfile->hosts[hostsfile->nhosts].host = g_strdup_printf("id:%s,[%s]",
"id:%s,[%s]", id, ipstr) < 0) id, ipstr);
goto error;
} }
} else if (name && mac) { } else if (name && mac) {
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s,%s", hostsfile->hosts[hostsfile->nhosts].host = g_strdup_printf("%s,%s,%s",
mac, ipstr, name) < 0) mac, ipstr, name);
goto error;
} else if (name && !mac) { } else if (name && !mac) {
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s", hostsfile->hosts[hostsfile->nhosts].host = g_strdup_printf("%s,%s", name,
name, ipstr) < 0) ipstr);
goto error;
} else { } else {
if (virAsprintf(&hostsfile->hosts[hostsfile->nhosts].host, "%s,%s", hostsfile->hosts[hostsfile->nhosts].host = g_strdup_printf("%s,%s", mac,
mac, ipstr) < 0) ipstr);
goto error;
} }
VIR_FREE(ipstr); VIR_FREE(ipstr);
@ -387,8 +380,7 @@ hostsfileWrite(const char *path,
* for runtime addition. * for runtime addition.
*/ */
if (virAsprintf(&tmp, "%s.new", path) < 0) tmp = g_strdup_printf("%s.new", path);
return -ENOMEM;
if (!(f = fopen(tmp, "w"))) { if (!(f = fopen(tmp, "w"))) {
istmp = false; istmp = false;
@ -752,8 +744,7 @@ dnsmasqCapsRefreshInternal(dnsmasqCapsPtr caps, bool force)
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; goto cleanup;
if (virAsprintf(&complete, "%s\n%s", version, help) < 0) complete = g_strdup_printf("%s\n%s", version, help);
goto cleanup;
ret = dnsmasqCapsSetFromBuffer(caps, complete); ret = dnsmasqCapsSetFromBuffer(caps, complete);

View File

@ -58,10 +58,7 @@ ebtablesContextNew(const char *driver)
if (VIR_ALLOC(ctx) < 0) if (VIR_ALLOC(ctx) < 0)
return NULL; return NULL;
if (virAsprintf(&ctx->chain, "libvirt_%s_FORWARD", driver) < 0) { ctx->chain = g_strdup_printf("libvirt_%s_FORWARD", driver);
VIR_FREE(ctx);
return NULL;
}
return ctx; return ctx;
} }

View File

@ -1488,8 +1488,7 @@ virLastErrorPrefixMessage(const char *fmt, ...)
if (virVasprintfQuiet(&fmtmsg, fmt, args) < 0) if (virVasprintfQuiet(&fmtmsg, fmt, args) < 0)
goto cleanup; goto cleanup;
if (virAsprintfQuiet(&newmsg, "%s: %s", fmtmsg, err->message) < 0) newmsg = g_strdup_printf("%s: %s", fmtmsg, err->message);
goto cleanup;
VIR_FREE(err->message); VIR_FREE(err->message);
err->message = g_steal_pointer(&newmsg); err->message = g_steal_pointer(&newmsg);

View File

@ -508,8 +508,7 @@ virFileRewrite(const char *path,
int fd = -1; int fd = -1;
int ret = -1; int ret = -1;
if (virAsprintf(&newfile, "%s.new", path) < 0) newfile = g_strdup_printf("%s.new", path);
goto cleanup;
if ((fd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) { if ((fd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC, mode)) < 0) {
virReportSystemError(errno, _("cannot create file '%s'"), virReportSystemError(errno, _("cannot create file '%s'"),
@ -666,8 +665,7 @@ static int virFileLoopDeviceOpenLoopCtl(char **dev_name, int *fd)
VIR_DEBUG("Found free loop device number %i", devnr); VIR_DEBUG("Found free loop device number %i", devnr);
if (virAsprintf(&looppath, "/dev/loop%i", devnr) < 0) looppath = g_strdup_printf("/dev/loop%i", devnr);
return -1;
if ((*fd = open(looppath, O_RDWR)) < 0) { if ((*fd = open(looppath, O_RDWR)) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -703,8 +701,7 @@ static int virFileLoopDeviceOpenSearch(char **dev_name)
!c_isdigit(de->d_name[4])) !c_isdigit(de->d_name[4]))
continue; continue;
if (virAsprintf(&looppath, "/dev/%s", de->d_name) < 0) looppath = g_strdup_printf("/dev/%s", de->d_name);
goto cleanup;
VIR_DEBUG("Checking up on device %s", looppath); VIR_DEBUG("Checking up on device %s", looppath);
if ((fd = open(looppath, O_RDWR)) < 0) { if ((fd = open(looppath, O_RDWR)) < 0) {
@ -834,9 +831,7 @@ virFileNBDDeviceIsBusy(const char *dev_name)
{ {
g_autofree char *path = NULL; g_autofree char *path = NULL;
if (virAsprintf(&path, SYSFS_BLOCK_DIR "/%s/pid", path = g_strdup_printf(SYSFS_BLOCK_DIR "/%s/pid", dev_name);
dev_name) < 0)
return -1;
if (!virFileExists(path)) { if (!virFileExists(path)) {
if (errno == ENOENT) if (errno == ENOENT)
@ -868,7 +863,7 @@ virFileNBDDeviceFindUnused(void)
if (rv < 0) if (rv < 0)
goto cleanup; goto cleanup;
if (rv == 0) { if (rv == 0) {
ignore_value(virAsprintf(&ret, "/dev/%s", de->d_name)); ret = g_strdup_printf("/dev/%s", de->d_name);
goto cleanup; goto cleanup;
} }
} }
@ -1013,9 +1008,7 @@ int virFileDeleteTree(const char *dir)
g_autofree char *filepath = NULL; g_autofree char *filepath = NULL;
struct stat sb; struct stat sb;
if (virAsprintf(&filepath, "%s/%s", filepath = g_strdup_printf("%s/%s", dir, de->d_name);
dir, de->d_name) < 0)
goto cleanup;
if (lstat(filepath, &sb) < 0) { if (lstat(filepath, &sb) < 0) {
virReportSystemError(errno, _("Cannot access '%s'"), virReportSystemError(errno, _("Cannot access '%s'"),
@ -1556,8 +1549,7 @@ virFileRelLinkPointsTo(const char *directory,
checkLink); checkLink);
return -1; return -1;
} }
if (virAsprintf(&candidate, "%s/%s", directory, checkLink) < 0) candidate = g_strdup_printf("%s/%s", directory, checkLink);
return -1;
return virFileLinkPointsTo(candidate, checkDest); return virFileLinkPointsTo(candidate, checkDest);
} }
@ -1693,8 +1685,8 @@ virFindFileInPath(const char *file)
*/ */
pathiter = path; pathiter = path;
while ((pathseg = strsep(&pathiter, ":")) != NULL) { while ((pathseg = strsep(&pathiter, ":")) != NULL) {
if (virAsprintf(&fullpath, "%s/%s", pathseg, file) < 0 || fullpath = g_strdup_printf("%s/%s", pathseg, file);
virFileIsExecutable(fullpath)) if (virFileIsExecutable(fullpath))
break; break;
VIR_FREE(fullpath); VIR_FREE(fullpath);
} }
@ -1757,8 +1749,7 @@ virFileFindResourceFull(const char *filename,
else else
path = installdir; path = installdir;
if (virAsprintf(&ret, "%s/%s%s%s", path, prefix, filename, suffix) < 0) ret = g_strdup_printf("%s/%s%s%s", path, prefix, filename, suffix);
return NULL;
VIR_DEBUG("Resolved '%s' to '%s'", filename, ret); VIR_DEBUG("Resolved '%s' to '%s'", filename, ret);
return ret; return ret;
@ -2985,8 +2976,7 @@ int virFileChownFiles(const char *name,
while ((direrr = virDirRead(dir, &ent, name)) > 0) { while ((direrr = virDirRead(dir, &ent, name)) > 0) {
g_autofree char *path = NULL; g_autofree char *path = NULL;
if (virAsprintf(&path, "%s/%s", name, ent->d_name) < 0) path = g_strdup_printf("%s/%s", name, ent->d_name);
goto cleanup;
if (!virFileIsRegular(path)) if (!virFileIsRegular(path))
continue; continue;
@ -3103,9 +3093,9 @@ virFileBuildPath(const char *dir, const char *name, const char *ext)
char *path; char *path;
if (ext == NULL) { if (ext == NULL) {
ignore_value(virAsprintf(&path, "%s/%s", dir, name)); path = g_strdup_printf("%s/%s", dir, name);
} else { } else {
ignore_value(virAsprintf(&path, "%s/%s%s", dir, name, ext)); path = g_strdup_printf("%s/%s%s", dir, name, ext);
} }
return path; return path;
@ -3293,8 +3283,7 @@ virFileAbsPath(const char *path, char **abspath)
if (buf == NULL) if (buf == NULL)
return -1; return -1;
if (virAsprintf(abspath, "%s/%s", buf, path) < 0) *abspath = g_strdup_printf("%s/%s", buf, path);
return -1;
} }
return 0; return 0;

View File

@ -627,8 +627,7 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo,
/* OK, we've parsed clock speed out of /proc/cpuinfo. Get the /* OK, we've parsed clock speed out of /proc/cpuinfo. Get the
* core, node, socket, thread and topology information from /sys * core, node, socket, thread and topology information from /sys
*/ */
if (virAsprintf(&sysfs_nodedir, "%s/node", SYSFS_SYSTEM_PATH) < 0) sysfs_nodedir = g_strdup_printf("%s/node", SYSFS_SYSTEM_PATH);
goto cleanup;
if (virDirOpenQuiet(&nodedir, sysfs_nodedir) < 0) { if (virDirOpenQuiet(&nodedir, sysfs_nodedir) < 0) {
/* the host isn't probably running a NUMA architecture */ /* the host isn't probably running a NUMA architecture */
@ -671,9 +670,8 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo,
(*nodes)++; (*nodes)++;
if (virAsprintf(&sysfs_cpudir, "%s/node/%s", SYSFS_SYSTEM_PATH, sysfs_cpudir = g_strdup_printf("%s/node/%s", SYSFS_SYSTEM_PATH,
nodedirent->d_name) < 0) nodedirent->d_name);
goto cleanup;
if ((nodecpus = virHostCPUParseNode(sysfs_cpudir, arch, if ((nodecpus = virHostCPUParseNode(sysfs_cpudir, arch,
present_cpus_map, present_cpus_map,
@ -706,8 +704,7 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo,
fallback: fallback:
VIR_FREE(sysfs_cpudir); VIR_FREE(sysfs_cpudir);
if (virAsprintf(&sysfs_cpudir, "%s/cpu", SYSFS_SYSTEM_PATH) < 0) sysfs_cpudir = g_strdup_printf("%s/cpu", SYSFS_SYSTEM_PATH);
goto cleanup;
if ((nodecpus = virHostCPUParseNode(sysfs_cpudir, arch, if ((nodecpus = virHostCPUParseNode(sysfs_cpudir, arch,
present_cpus_map, present_cpus_map,

View File

@ -184,8 +184,7 @@ virHostdevManagerNew(void)
if (!(rundir = virGetUserRuntimeDirectory())) if (!(rundir = virGetUserRuntimeDirectory()))
return NULL; return NULL;
if (virAsprintf(&hostdevMgr->stateDir, "%s/hostdevmgr", rundir) < 0) hostdevMgr->stateDir = g_strdup_printf("%s/hostdevmgr", rundir);
return NULL;
old_umask = umask(077); old_umask = umask(077);

View File

@ -280,10 +280,8 @@ virHostMemGetStats(int cellNum G_GNUC_UNUSED,
return -1; return -1;
} }
if (virAsprintf(&meminfo_path, meminfo_path = g_strdup_printf(
SYSFS_SYSTEM_PATH "/node/node%d/meminfo", SYSFS_SYSTEM_PATH "/node/node%d/meminfo", cellNum);
cellNum) < 0)
return -1;
} }
meminfo = fopen(meminfo_path, "r"); meminfo = fopen(meminfo_path, "r");
@ -318,12 +316,9 @@ virHostMemSetParameterValue(virTypedParameterPtr param)
char *field = strchr(param->field, '_'); char *field = strchr(param->field, '_');
sa_assert(field); sa_assert(field);
field++; field++;
if (virAsprintf(&path, "%s/%s", path = g_strdup_printf("%s/%s", SYSFS_MEMORY_SHARED_PATH, field);
SYSFS_MEMORY_SHARED_PATH, field) < 0)
return -2;
if (virAsprintf(&strval, "%u", param->value.ui) == -1) strval = g_strdup_printf("%u", param->value.ui);
return -2;
if ((rc = virFileWriteStr(path, strval, 0)) < 0) { if ((rc = virFileWriteStr(path, strval, 0)) < 0) {
virReportSystemError(-rc, _("failed to set %s"), param->field); virReportSystemError(-rc, _("failed to set %s"), param->field);
@ -346,9 +341,7 @@ virHostMemParametersAreAllSupported(virTypedParameterPtr params,
char *field = strchr(param->field, '_'); char *field = strchr(param->field, '_');
sa_assert(field); sa_assert(field);
field++; field++;
if (virAsprintf(&path, "%s/%s", path = g_strdup_printf("%s/%s", SYSFS_MEMORY_SHARED_PATH, field);
SYSFS_MEMORY_SHARED_PATH, field) < 0)
return false;
if (!virFileExists(path)) { if (!virFileExists(path)) {
virReportError(VIR_ERR_OPERATION_INVALID, virReportError(VIR_ERR_OPERATION_INVALID,
@ -412,9 +405,7 @@ virHostMemGetParameterValue(const char *field,
char *tmp = NULL; char *tmp = NULL;
int rc = -1; int rc = -1;
if (virAsprintf(&path, "%s/%s", path = g_strdup_printf("%s/%s", SYSFS_MEMORY_SHARED_PATH, field);
SYSFS_MEMORY_SHARED_PATH, field) < 0)
return -1;
if (!virFileExists(path)) if (!virFileExists(path))
return -2; return -2;

View File

@ -401,7 +401,7 @@ static char *iptablesFormatNetwork(virSocketAddr *netaddr,
if (!netstr) if (!netstr)
return NULL; return NULL;
ignore_value(virAsprintf(&ret, "%s/%d", netstr, prefix)); ret = g_strdup_printf("%s/%d", netstr, prefix);
return ret; return ret;
} }
@ -905,31 +905,25 @@ iptablesForwardMasquerade(virFirewallPtr fw,
} }
if (port->start < port->end && port->end < 65536) { if (port->start < port->end && port->end < 65536) {
if (virAsprintf(&portRangeStr, ":%u-%u", portRangeStr = g_strdup_printf(":%u-%u", port->start, port->end);
port->start, port->end) < 0)
return -1;
} else { } else {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Invalid port range '%u-%u'."), _("Invalid port range '%u-%u'."),
port->start, port->end); port->start, port->end);
return -1;
} }
} }
/* Use --jump SNAT if public addr is specified */ /* Use --jump SNAT if public addr is specified */
if (addrStartStr && addrStartStr[0]) { if (addrStartStr && addrStartStr[0]) {
int r = 0;
if (addrEndStr && addrEndStr[0]) { if (addrEndStr && addrEndStr[0]) {
r = virAsprintf(&natRangeStr, "%s-%s%s", addrStartStr, addrEndStr, natRangeStr = g_strdup_printf("%s-%s%s", addrStartStr, addrEndStr,
portRangeStr ? portRangeStr : ""); portRangeStr ? portRangeStr : "");
} else { } else {
r = virAsprintf(&natRangeStr, "%s%s", addrStartStr, natRangeStr = g_strdup_printf("%s%s", addrStartStr,
portRangeStr ? portRangeStr : ""); portRangeStr ? portRangeStr : "");
} }
if (r < 0)
return -1;
virFirewallRuleAddArgList(fw, rule, virFirewallRuleAddArgList(fw, rule,
"--jump", "SNAT", "--jump", "SNAT",
"--to-source", natRangeStr, NULL); "--to-source", natRangeStr, NULL);

View File

@ -213,10 +213,8 @@ virStorageBackendCreateIfaceIQN(const char *initiatoriqn,
g_autofree char *temp_ifacename = NULL; g_autofree char *temp_ifacename = NULL;
g_autoptr(virCommand) cmd = NULL; g_autoptr(virCommand) cmd = NULL;
if (virAsprintf(&temp_ifacename, temp_ifacename = g_strdup_printf("libvirt-iface-%08llx",
"libvirt-iface-%08llx", (unsigned long long)virRandomBits(32));
(unsigned long long)virRandomBits(32)) < 0)
return -1;
VIR_DEBUG("Attempting to create interface '%s' with IQN '%s'", VIR_DEBUG("Attempting to create interface '%s' with IQN '%s'",
temp_ifacename, initiatoriqn); temp_ifacename, initiatoriqn);

View File

@ -497,8 +497,7 @@ virJSONValuePtr
virJSONValueNewNumberInt(int data) virJSONValueNewNumberInt(int data)
{ {
g_autofree char *str = NULL; g_autofree char *str = NULL;
if (virAsprintf(&str, "%i", data) < 0) str = g_strdup_printf("%i", data);
return NULL;
return virJSONValueNewNumber(str); return virJSONValueNewNumber(str);
} }
@ -507,8 +506,7 @@ virJSONValuePtr
virJSONValueNewNumberUint(unsigned int data) virJSONValueNewNumberUint(unsigned int data)
{ {
g_autofree char *str = NULL; g_autofree char *str = NULL;
if (virAsprintf(&str, "%u", data) < 0) str = g_strdup_printf("%u", data);
return NULL;
return virJSONValueNewNumber(str); return virJSONValueNewNumber(str);
} }
@ -517,8 +515,7 @@ virJSONValuePtr
virJSONValueNewNumberLong(long long data) virJSONValueNewNumberLong(long long data)
{ {
g_autofree char *str = NULL; g_autofree char *str = NULL;
if (virAsprintf(&str, "%lld", data) < 0) str = g_strdup_printf("%lld", data);
return NULL;
return virJSONValueNewNumber(str); return virJSONValueNewNumber(str);
} }
@ -527,8 +524,7 @@ virJSONValuePtr
virJSONValueNewNumberUlong(unsigned long long data) virJSONValueNewNumberUlong(unsigned long long data)
{ {
g_autofree char *str = NULL; g_autofree char *str = NULL;
if (virAsprintf(&str, "%llu", data) < 0) str = g_strdup_printf("%llu", data);
return NULL;
return virJSONValueNewNumber(str); return virJSONValueNewNumber(str);
} }

View File

@ -149,8 +149,7 @@ virKModIsBlacklisted(const char *module)
g_autofree char *drvblklst = NULL; g_autofree char *drvblklst = NULL;
g_autofree char *outbuf = NULL; g_autofree char *outbuf = NULL;
if (virAsprintfQuiet(&drvblklst, "blacklist %s\n", module) < 0) drvblklst = g_strdup_printf("blacklist %s\n", module);
return false;
/* modprobe will convert all '-' into '_', so we need to as well */ /* modprobe will convert all '-' into '_', so we need to as well */
for (i = 0; i < drvblklst[i]; i++) for (i = 0; i < drvblklst[i]; i++)

View File

@ -67,7 +67,7 @@ static char *virLockSpaceGetResourcePath(virLockSpacePtr lockspace,
{ {
char *ret; char *ret;
if (lockspace->dir) if (lockspace->dir)
ignore_value(virAsprintf(&ret, "%s/%s", lockspace->dir, resname)); ret = g_strdup_printf("%s/%s", lockspace->dir, resname);
else else
ret = g_strdup(resname); ret = g_strdup(resname);
return ret; return ret;

View File

@ -149,15 +149,15 @@ virLogUnlock(void)
} }
static int static void
virLogSetDefaultOutputToStderr(void) virLogSetDefaultOutputToStderr(void)
{ {
return virAsprintf(&virLogDefaultOutput, "%d:stderr", virLogDefaultOutput = g_strdup_printf("%d:stderr",
virLogDefaultPriority); virLogDefaultPriority);
} }
static int static void
virLogSetDefaultOutputToJournald(void) virLogSetDefaultOutputToJournald(void)
{ {
virLogPriority priority = virLogDefaultPriority; virLogPriority priority = virLogDefaultPriority;
@ -167,7 +167,7 @@ virLogSetDefaultOutputToJournald(void)
if (priority == VIR_LOG_DEBUG) if (priority == VIR_LOG_DEBUG)
priority = VIR_LOG_INFO; priority = VIR_LOG_INFO;
return virAsprintf(&virLogDefaultOutput, "%d:journald", priority); virLogDefaultOutput = g_strdup_printf("%d:journald", priority);
} }
@ -179,10 +179,8 @@ virLogSetDefaultOutputToFile(const char *binary, bool privileged)
mode_t old_umask; mode_t old_umask;
if (privileged) { if (privileged) {
if (virAsprintf(&virLogDefaultOutput, virLogDefaultOutput = g_strdup_printf("%d:file:%s/log/libvirt/%s.log",
"%d:file:%s/log/libvirt/%s.log", virLogDefaultPriority, virLogDefaultPriority, LOCALSTATEDIR, binary);
LOCALSTATEDIR, binary) < 0)
goto cleanup;
} else { } else {
if (!(logdir = virGetUserCacheDirectory())) if (!(logdir = virGetUserCacheDirectory()))
goto cleanup; goto cleanup;
@ -194,9 +192,8 @@ virLogSetDefaultOutputToFile(const char *binary, bool privileged)
} }
umask(old_umask); umask(old_umask);
if (virAsprintf(&virLogDefaultOutput, "%d:file:%s/%s.log", virLogDefaultOutput = g_strdup_printf("%d:file:%s/%s.log",
virLogDefaultPriority, logdir, binary) < 0) virLogDefaultPriority, logdir, binary);
goto cleanup;
} }
ret = 0; ret = 0;
@ -216,25 +213,23 @@ virLogSetDefaultOutputToFile(const char *binary, bool privileged)
* Decides on what the default output (journald, file, stderr) should be * Decides on what the default output (journald, file, stderr) should be
* according to @binary, @godaemon, @privileged. This function should be run * according to @binary, @godaemon, @privileged. This function should be run
* exactly once at daemon startup, so no locks are used. * exactly once at daemon startup, so no locks are used.
*
* Returns 0 on success, -1 in case of a failure.
*/ */
int void
virLogSetDefaultOutput(const char *binary, bool godaemon, bool privileged) virLogSetDefaultOutput(const char *binary, bool godaemon, bool privileged)
{ {
bool have_journald = access("/run/systemd/journal/socket", W_OK) >= 0; bool have_journald = access("/run/systemd/journal/socket", W_OK) >= 0;
if (godaemon) { if (godaemon) {
if (have_journald) if (have_journald)
return virLogSetDefaultOutputToJournald(); virLogSetDefaultOutputToJournald();
else
virLogSetDefaultOutputToFile(binary, privileged);
} else { } else {
if (!isatty(STDIN_FILENO) && have_journald) if (!isatty(STDIN_FILENO) && have_journald)
return virLogSetDefaultOutputToJournald(); virLogSetDefaultOutputToJournald();
else
return virLogSetDefaultOutputToStderr(); virLogSetDefaultOutputToStderr();
} }
return virLogSetDefaultOutputToFile(binary, privileged);
} }
@ -443,15 +438,13 @@ virLogOutputListFree(virLogOutputPtr *list, int count)
} }
static int static void
virLogFormatString(char **msg, virLogFormatString(char **msg,
int linenr, int linenr,
const char *funcname, const char *funcname,
virLogPriority priority, virLogPriority priority,
const char *str) const char *str)
{ {
int ret;
/* /*
* Be careful when changing the following log message formatting, we rely * Be careful when changing the following log message formatting, we rely
* on it when stripping libvirt debug messages from qemu log files. So when * on it when stripping libvirt debug messages from qemu log files. So when
@ -460,44 +453,38 @@ virLogFormatString(char **msg,
* to just grep for it to find the right place. * to just grep for it to find the right place.
*/ */
if ((funcname != NULL)) { if ((funcname != NULL)) {
ret = virAsprintfQuiet(msg, "%llu: %s : %s:%d : %s\n", *msg = g_strdup_printf("%llu: %s : %s:%d : %s\n",
virThreadSelfID(), virLogPriorityString(priority), virThreadSelfID(), virLogPriorityString(priority),
funcname, linenr, str); funcname, linenr, str);
} else { } else {
ret = virAsprintfQuiet(msg, "%llu: %s : %s\n", *msg = g_strdup_printf("%llu: %s : %s\n",
virThreadSelfID(), virLogPriorityString(priority), virThreadSelfID(), virLogPriorityString(priority),
str); str);
} }
return ret;
} }
static int static void
virLogVersionString(const char **rawmsg, virLogVersionString(const char **rawmsg,
char **msg) char **msg)
{ {
*rawmsg = VIR_LOG_VERSION_STRING; *rawmsg = VIR_LOG_VERSION_STRING;
return virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, VIR_LOG_VERSION_STRING); virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, VIR_LOG_VERSION_STRING);
} }
/* Similar to virGetHostname() but avoids use of error /* Similar to virGetHostname() but avoids use of error
* reporting APIs or logging APIs, to prevent recursion * reporting APIs or logging APIs, to prevent recursion
*/ */
static int static void
virLogHostnameString(char **rawmsg, virLogHostnameString(char **rawmsg,
char **msg) char **msg)
{ {
char *hoststr; char *hoststr;
if (virAsprintfQuiet(&hoststr, "hostname: %s", virLogHostname) < 0) hoststr = g_strdup_printf("hostname: %s", virLogHostname);
return -1;
if (virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, hoststr) < 0) { virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, hoststr);
VIR_FREE(hoststr);
return -1;
}
*rawmsg = hoststr; *rawmsg = hoststr;
return 0;
} }
@ -582,7 +569,6 @@ virLogVMessage(virLogSourcePtr source,
char *str = NULL; char *str = NULL;
char *msg = NULL; char *msg = NULL;
char timestamp[VIR_TIME_STRING_BUFLEN]; char timestamp[VIR_TIME_STRING_BUFLEN];
int ret;
size_t i; size_t i;
int saved_errno = errno; int saved_errno = errno;
@ -611,9 +597,7 @@ virLogVMessage(virLogSourcePtr source,
if (virVasprintfQuiet(&str, fmt, vargs) < 0) if (virVasprintfQuiet(&str, fmt, vargs) < 0)
goto cleanup; goto cleanup;
ret = virLogFormatString(&msg, linenr, funcname, priority, str); virLogFormatString(&msg, linenr, funcname, priority, str);
if (ret < 0)
goto cleanup;
if (virTimeStringNowRaw(timestamp) < 0) if (virTimeStringNowRaw(timestamp) < 0)
timestamp[0] = '\0'; timestamp[0] = '\0';
@ -630,17 +614,18 @@ virLogVMessage(virLogSourcePtr source,
const char *rawinitmsg; const char *rawinitmsg;
char *hoststr = NULL; char *hoststr = NULL;
char *initmsg = NULL; char *initmsg = NULL;
if (virLogVersionString(&rawinitmsg, &initmsg) >= 0) virLogVersionString(&rawinitmsg, &initmsg);
virLogOutputs[i]->f(&virLogSelf, VIR_LOG_INFO, virLogOutputs[i]->f(&virLogSelf, VIR_LOG_INFO,
__FILE__, __LINE__, __func__, __FILE__, __LINE__, __func__,
timestamp, NULL, rawinitmsg, initmsg, timestamp, NULL, rawinitmsg, initmsg,
virLogOutputs[i]->data); virLogOutputs[i]->data);
VIR_FREE(initmsg); VIR_FREE(initmsg);
if (virLogHostnameString(&hoststr, &initmsg) >= 0)
virLogOutputs[i]->f(&virLogSelf, VIR_LOG_INFO, virLogHostnameString(&hoststr, &initmsg);
__FILE__, __LINE__, __func__, virLogOutputs[i]->f(&virLogSelf, VIR_LOG_INFO,
timestamp, NULL, hoststr, initmsg, __FILE__, __LINE__, __func__,
virLogOutputs[i]->data); timestamp, NULL, hoststr, initmsg,
virLogOutputs[i]->data);
VIR_FREE(hoststr); VIR_FREE(hoststr);
VIR_FREE(initmsg); VIR_FREE(initmsg);
virLogOutputs[i]->logInitMessage = false; virLogOutputs[i]->logInitMessage = false;
@ -656,17 +641,18 @@ virLogVMessage(virLogSourcePtr source,
const char *rawinitmsg; const char *rawinitmsg;
char *hoststr = NULL; char *hoststr = NULL;
char *initmsg = NULL; char *initmsg = NULL;
if (virLogVersionString(&rawinitmsg, &initmsg) >= 0) virLogVersionString(&rawinitmsg, &initmsg);
virLogOutputToFd(&virLogSelf, VIR_LOG_INFO, virLogOutputToFd(&virLogSelf, VIR_LOG_INFO,
__FILE__, __LINE__, __func__, __FILE__, __LINE__, __func__,
timestamp, NULL, rawinitmsg, initmsg, timestamp, NULL, rawinitmsg, initmsg,
(void *) STDERR_FILENO); (void *) STDERR_FILENO);
VIR_FREE(initmsg); VIR_FREE(initmsg);
if (virLogHostnameString(&hoststr, &initmsg) >= 0)
virLogOutputToFd(&virLogSelf, VIR_LOG_INFO, virLogHostnameString(&hoststr, &initmsg);
__FILE__, __LINE__, __func__, virLogOutputToFd(&virLogSelf, VIR_LOG_INFO,
timestamp, NULL, hoststr, initmsg, __FILE__, __LINE__, __func__,
(void *) STDERR_FILENO); timestamp, NULL, hoststr, initmsg,
(void *) STDERR_FILENO);
VIR_FREE(hoststr); VIR_FREE(hoststr);
VIR_FREE(initmsg); VIR_FREE(initmsg);
logInitMessageStderr = false; logInitMessageStderr = false;
@ -703,9 +689,7 @@ virLogOutputToFd(virLogSourcePtr source G_GNUC_UNUSED,
if (fd < 0) if (fd < 0)
return; return;
if (virAsprintfQuiet(&msg, "%s: %s", timestamp, str) < 0) msg = g_strdup_printf("%s: %s", timestamp, str);
return;
ignore_value(safewrite(fd, msg, strlen(msg))); ignore_value(safewrite(fd, msg, strlen(msg)));
VIR_FREE(msg); VIR_FREE(msg);
} }

View File

@ -181,7 +181,7 @@ void virLogFilterListFree(virLogFilterPtr *list, int count);
int virLogSetOutputs(const char *outputs); int virLogSetOutputs(const char *outputs);
int virLogSetFilters(const char *filters); int virLogSetFilters(const char *filters);
char *virLogGetDefaultOutput(void); char *virLogGetDefaultOutput(void);
int virLogSetDefaultOutput(const char *fname, bool godaemon, bool privileged); void virLogSetDefaultOutput(const char *fname, bool godaemon, bool privileged);
/* /*
* Internal logging API * Internal logging API

View File

@ -290,7 +290,7 @@ virMacMapFileName(const char *dnsmasqStateDir,
{ {
char *filename; char *filename;
ignore_value(virAsprintf(&filename, "%s/%s.macs", dnsmasqStateDir, bridge)); filename = g_strdup_printf("%s/%s.macs", dnsmasqStateDir, bridge);
return filename; return filename;
} }

View File

@ -80,8 +80,7 @@ virMediatedDeviceGetSysfsDeviceAPI(virMediatedDevicePtr dev,
g_autofree char *file = NULL; g_autofree char *file = NULL;
char *tmp = NULL; char *tmp = NULL;
if (virAsprintf(&file, "%s/mdev_type/device_api", dev->path) < 0) file = g_strdup_printf("%s/mdev_type/device_api", dev->path);
return -1;
/* TODO - make this a generic method to access sysfs files for various /* TODO - make this a generic method to access sysfs files for various
* kinds of devices * kinds of devices
@ -215,8 +214,7 @@ virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr)
if (!dev_path) if (!dev_path)
return NULL; return NULL;
if (virAsprintf(&iommu_path, "%s/iommu_group", dev_path) < 0) iommu_path = g_strdup_printf("%s/iommu_group", dev_path);
return NULL;
if (!virFileExists(iommu_path)) { if (!virFileExists(iommu_path)) {
virReportSystemError(errno, _("failed to access '%s'"), iommu_path); virReportSystemError(errno, _("failed to access '%s'"), iommu_path);
@ -228,8 +226,7 @@ virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr)
return NULL; return NULL;
} }
if (virAsprintf(&vfio_path, "/dev/vfio/%s", last_component(result_path)) < 0) vfio_path = g_strdup_printf("/dev/vfio/%s", last_component(result_path));
return NULL;
return vfio_path; return vfio_path;
} }
@ -425,7 +422,7 @@ virMediatedDeviceGetSysfsPath(const char *uuidstr)
{ {
char *ret = NULL; char *ret = NULL;
ignore_value(virAsprintf(&ret, MDEV_SYSFS_DEVICES "%s", uuidstr)); ret = g_strdup_printf(MDEV_SYSFS_DEVICES "%s", uuidstr);
return ret; return ret;
} }

View File

@ -511,8 +511,7 @@ int virNetDevSetNamespace(const char *ifname, pid_t pidInNs)
char *phy_path = NULL; char *phy_path = NULL;
int len; int len;
if (virAsprintf(&pid, "%lld", (long long) pidInNs) == -1) pid = g_strdup_printf("%lld", (long long) pidInNs);
return -1;
/* The 802.11 wireless devices only move together with their PHY. */ /* The 802.11 wireless devices only move together with their PHY. */
if (virNetDevSysfsFile(&phy_path, ifname, "phy80211/name") < 0) if (virNetDevSysfsFile(&phy_path, ifname, "phy80211/name") < 0)
@ -1069,8 +1068,7 @@ int
virNetDevSysfsFile(char **pf_sysfs_device_link, const char *ifname, virNetDevSysfsFile(char **pf_sysfs_device_link, const char *ifname,
const char *file) const char *file)
{ {
if (virAsprintf(pf_sysfs_device_link, SYSFS_NET_DIR "%s/%s", ifname, file) < 0) *pf_sysfs_device_link = g_strdup_printf(SYSFS_NET_DIR "%s/%s", ifname, file);
return -1;
return 0; return 0;
} }
@ -1078,9 +1076,8 @@ static int
virNetDevSysfsDeviceFile(char **pf_sysfs_device_link, const char *ifname, virNetDevSysfsDeviceFile(char **pf_sysfs_device_link, const char *ifname,
const char *file) const char *file)
{ {
if (virAsprintf(pf_sysfs_device_link, SYSFS_NET_DIR "%s/device/%s", ifname, *pf_sysfs_device_link = g_strdup_printf(SYSFS_NET_DIR "%s/device/%s", ifname,
file) < 0) file);
return -1;
return 0; return 0;
} }
@ -1102,8 +1099,7 @@ virNetDevIsPCIDevice(const char *devpath)
char *subsys = NULL; char *subsys = NULL;
bool ret = false; bool ret = false;
if (virAsprintf(&subsys_link, "%s/subsystem", devpath) < 0) subsys_link = g_strdup_printf("%s/subsystem", devpath);
return false;
if (!virFileExists(subsys_link)) if (!virFileExists(subsys_link))
goto cleanup; goto cleanup;
@ -1404,8 +1400,7 @@ virNetDevPFGetVF(const char *pfname, int vf, char **vfname)
if (virNetDevGetPhysPortID(pfname, &pfPhysPortID) < 0) if (virNetDevGetPhysPortID(pfname, &pfPhysPortID) < 0)
goto cleanup; goto cleanup;
if (virAsprintf(&virtfnName, "virtfn%d", vf) < 0) virtfnName = g_strdup_printf("virtfn%d", vf);
goto cleanup;
/* this provides the path to the VF's directory in sysfs, /* this provides the path to the VF's directory in sysfs,
* e.g. "/sys/class/net/enp2s0f0/virtfn3" * e.g. "/sys/class/net/enp2s0f0/virtfn3"
@ -1893,8 +1888,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
*/ */
if (pfDevName && saveVlan) { if (pfDevName && saveVlan) {
if (virAsprintf(&filePath, "%s/%s_vf%d", stateDir, pfDevName, vf) < 0) filePath = g_strdup_printf("%s/%s_vf%d", stateDir, pfDevName, vf);
goto cleanup;
/* get admin MAC and vlan tag */ /* get admin MAC and vlan tag */
if (virNetDevGetVfConfig(pfDevName, vf, &oldMAC, &oldVlanTag) < 0) if (virNetDevGetVfConfig(pfDevName, vf, &oldMAC, &oldVlanTag) < 0)
@ -1910,8 +1904,7 @@ virNetDevSaveNetConfig(const char *linkdev, int vf,
} }
} else { } else {
if (virAsprintf(&filePath, "%s/%s", stateDir, linkdev) < 0) filePath = g_strdup_printf("%s/%s", stateDir, linkdev);
goto cleanup;
} }
if (linkdev) { if (linkdev) {
@ -2016,8 +2009,7 @@ virNetDevReadNetConfig(const char *linkdev, int vf,
*/ */
if (pfDevName) { if (pfDevName) {
if (virAsprintf(&filePath, "%s/%s_vf%d", stateDir, pfDevName, vf) < 0) filePath = g_strdup_printf("%s/%s_vf%d", stateDir, pfDevName, vf);
goto cleanup;
if (linkdev && !virFileExists(filePath)) { if (linkdev && !virFileExists(filePath)) {
/* the device may have been stored in a file named for the /* the device may have been stored in a file named for the
@ -2030,10 +2022,8 @@ virNetDevReadNetConfig(const char *linkdev, int vf,
} }
} }
if (!pfDevName) { if (!pfDevName)
if (virAsprintf(&filePath, "%s/%s", stateDir, linkdev) < 0) filePath = g_strdup_printf("%s/%s", stateDir, linkdev);
goto cleanup;
}
if (!virFileExists(filePath)) { if (!virFileExists(filePath)) {
/* having no file to read is not necessarily an error, so we /* having no file to read is not necessarily an error, so we
@ -2913,8 +2903,7 @@ virNetDevRDMAFeature(const char *ifname,
if (virDirOpen(&dirp, SYSFS_INFINIBAND_DIR) < 0) if (virDirOpen(&dirp, SYSFS_INFINIBAND_DIR) < 0)
return -1; return -1;
if (virAsprintf(&eth_devpath, SYSFS_NET_DIR "%s/device/resource", ifname) < 0) eth_devpath = g_strdup_printf(SYSFS_NET_DIR "%s/device/resource", ifname);
goto cleanup;
/* If /sys/class/net/<ifname>/device/resource doesn't exist it is not a PCI /* If /sys/class/net/<ifname>/device/resource doesn't exist it is not a PCI
* device and therefore it will not have RDMA. */ * device and therefore it will not have RDMA. */
@ -2927,9 +2916,8 @@ virNetDevRDMAFeature(const char *ifname,
goto cleanup; goto cleanup;
while (virDirRead(dirp, &dp, SYSFS_INFINIBAND_DIR) > 0) { while (virDirRead(dirp, &dp, SYSFS_INFINIBAND_DIR) > 0) {
if (virAsprintf(&ib_devpath, SYSFS_INFINIBAND_DIR "%s/device/resource", ib_devpath = g_strdup_printf(SYSFS_INFINIBAND_DIR "%s/device/resource",
dp->d_name) < 0) dp->d_name);
continue;
if (virFileReadAll(ib_devpath, RESOURCE_FILE_LEN, &ib_res_buf) > 0 && if (virFileReadAll(ib_devpath, RESOURCE_FILE_LEN, &ib_res_buf) > 0 &&
STREQ(eth_res_buf, ib_res_buf)) { STREQ(eth_res_buf, ib_res_buf)) {
ignore_value(virBitmapSetBit(*out, VIR_NET_DEV_FEAT_RDMA)); ignore_value(virBitmapSetBit(*out, VIR_NET_DEV_FEAT_RDMA));

View File

@ -114,8 +114,7 @@ virNetDevBandwidthManipulateFilter(const char *ifname,
} }
/* u32 filters must have 800:: prefix. Don't ask. */ /* u32 filters must have 800:: prefix. Don't ask. */
if (virAsprintf(&filter_id, "800::%u", id) < 0) filter_id = g_strdup_printf("800::%u", id);
goto cleanup;
if (remove_old) { if (remove_old) {
int cmd_ret = 0; int cmd_ret = 0;
@ -132,10 +131,9 @@ virNetDevBandwidthManipulateFilter(const char *ifname,
if (create_new) { if (create_new) {
virMacAddrGetRaw(ifmac_ptr, ifmac); virMacAddrGetRaw(ifmac_ptr, ifmac);
if (virAsprintf(&mac[0], "0x%02x%02x%02x%02x", ifmac[2], mac[0] = g_strdup_printf("0x%02x%02x%02x%02x", ifmac[2],
ifmac[3], ifmac[4], ifmac[5]) < 0 || ifmac[3], ifmac[4], ifmac[5]);
virAsprintf(&mac[1], "0x%02x%02x", ifmac[0], ifmac[1]) < 0) mac[1] = g_strdup_printf("0x%02x%02x", ifmac[0], ifmac[1]);
goto cleanup;
virCommandFree(cmd); virCommandFree(cmd);
cmd = virCommandNew(TC); cmd = virCommandNew(TC);
@ -232,14 +230,11 @@ virNetDevBandwidthSet(const char *ifname,
virNetDevBandwidthClear(ifname); virNetDevBandwidthClear(ifname);
if (tx && tx->average) { if (tx && tx->average) {
if (virAsprintf(&average, "%llukbps", tx->average) < 0) average = g_strdup_printf("%llukbps", tx->average);
goto cleanup; if (tx->peak)
if (tx->peak && peak = g_strdup_printf("%llukbps", tx->peak);
(virAsprintf(&peak, "%llukbps", tx->peak) < 0)) if (tx->burst)
goto cleanup; burst = g_strdup_printf("%llukb", tx->burst);
if (tx->burst &&
(virAsprintf(&burst, "%llukb", tx->burst) < 0))
goto cleanup;
cmd = virCommandNew(TC); cmd = virCommandNew(TC);
virCommandAddArgList(cmd, "qdisc", "add", "dev", ifname, "root", virCommandAddArgList(cmd, "qdisc", "add", "dev", ifname, "root",
@ -362,10 +357,8 @@ virNetDevBandwidthSet(const char *ifname,
} }
if (rx) { if (rx) {
if (virAsprintf(&average, "%llukbps", rx->average) < 0) average = g_strdup_printf("%llukbps", rx->average);
goto cleanup; burst = g_strdup_printf("%llukb", rx->burst ? rx->burst : rx->average);
if (virAsprintf(&burst, "%llukb", rx->burst ? rx->burst : rx->average) < 0)
goto cleanup;
virCommandFree(cmd); virCommandFree(cmd);
cmd = virCommandNew(TC); cmd = virCommandNew(TC);
@ -575,13 +568,12 @@ virNetDevBandwidthPlug(const char *brname,
return -1; return -1;
} }
if (virAsprintf(&class_id, "1:%x", id) < 0 || class_id = g_strdup_printf("1:%x", id);
virAsprintf(&qdisc_id, "%x:", id) < 0 || qdisc_id = g_strdup_printf("%x:", id);
virAsprintf(&floor, "%llukbps", bandwidth->in->floor) < 0 || floor = g_strdup_printf("%llukbps", bandwidth->in->floor);
virAsprintf(&ceil, "%llukbps", net_bandwidth->in->peak ? ceil = g_strdup_printf("%llukbps", net_bandwidth->in->peak ?
net_bandwidth->in->peak : net_bandwidth->in->peak :
net_bandwidth->in->average) < 0) net_bandwidth->in->average);
goto cleanup;
cmd = virCommandNew(TC); cmd = virCommandNew(TC);
virCommandAddArgList(cmd, "class", "add", "dev", brname, "parent", "1:1", virCommandAddArgList(cmd, "class", "add", "dev", brname, "parent", "1:1",
@ -640,9 +632,8 @@ virNetDevBandwidthUnplug(const char *brname,
return -1; return -1;
} }
if (virAsprintf(&class_id, "1:%x", id) < 0 || class_id = g_strdup_printf("1:%x", id);
virAsprintf(&qdisc_id, "%x:", id) < 0) qdisc_id = g_strdup_printf("%x:", id);
goto cleanup;
cmd = virCommandNew(TC); cmd = virCommandNew(TC);
virCommandAddArgList(cmd, "qdisc", "del", "dev", brname, virCommandAddArgList(cmd, "qdisc", "del", "dev", brname,
@ -700,12 +691,11 @@ virNetDevBandwidthUpdateRate(const char *ifname,
char *rate = NULL; char *rate = NULL;
char *ceil = NULL; char *ceil = NULL;
if (virAsprintf(&class_id, "1:%x", id) < 0 || class_id = g_strdup_printf("1:%x", id);
virAsprintf(&rate, "%llukbps", new_rate) < 0 || rate = g_strdup_printf("%llukbps", new_rate);
virAsprintf(&ceil, "%llukbps", bandwidth->in->peak ? ceil = g_strdup_printf("%llukbps", bandwidth->in->peak ?
bandwidth->in->peak : bandwidth->in->peak :
bandwidth->in->average) < 0) bandwidth->in->average);
goto cleanup;
cmd = virCommandNew(TC); cmd = virCommandNew(TC);
virCommandAddArgList(cmd, "class", "change", "dev", ifname, virCommandAddArgList(cmd, "class", "change", "dev", ifname,
@ -751,8 +741,7 @@ virNetDevBandwidthUpdateFilter(const char *ifname,
int ret = -1; int ret = -1;
char *class_id = NULL; char *class_id = NULL;
if (virAsprintf(&class_id, "1:%x", id) < 0) class_id = g_strdup_printf("1:%x", id);
goto cleanup;
if (virNetDevBandwidthManipulateFilter(ifname, ifmac_ptr, id, if (virNetDevBandwidthManipulateFilter(ifname, ifmac_ptr, id,
class_id, true, true) < 0) class_id, true, true) < 0)

View File

@ -122,8 +122,7 @@ static int virNetDevBridgeSet(const char *brname,
{ {
g_autofree char *path = NULL; g_autofree char *path = NULL;
if (virAsprintf(&path, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 0) path = g_strdup_printf(SYSFS_NET_DIR "%s/bridge/%s", brname, paramname);
return -1;
if (virFileExists(path)) { if (virFileExists(path)) {
char valuestr[INT_BUFSIZE_BOUND(value)]; char valuestr[INT_BUFSIZE_BOUND(value)];
@ -165,8 +164,7 @@ static int virNetDevBridgeGet(const char *brname,
g_autofree char *path = NULL; g_autofree char *path = NULL;
VIR_AUTOCLOSE fd = -1; VIR_AUTOCLOSE fd = -1;
if (virAsprintf(&path, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname) < 0) path = g_strdup_printf(SYSFS_NET_DIR "%s/bridge/%s", brname, paramname);
return -1;
if (virFileExists(path)) { if (virFileExists(path)) {
g_autofree char *valuestr = NULL; g_autofree char *valuestr = NULL;
@ -223,9 +221,8 @@ virNetDevBridgePortSet(const char *brname,
snprintf(valuestr, sizeof(valuestr), "%lu", value); snprintf(valuestr, sizeof(valuestr), "%lu", value);
if (virAsprintf(&path, SYSFS_NET_DIR "%s/brif/%s/%s", path = g_strdup_printf(SYSFS_NET_DIR "%s/brif/%s/%s", brname, ifname,
brname, ifname, paramname) < 0) paramname);
return -1;
if (!virFileExists(path)) if (!virFileExists(path))
errno = EINVAL; errno = EINVAL;
@ -251,9 +248,8 @@ virNetDevBridgePortGet(const char *brname,
g_autofree char *path = NULL; g_autofree char *path = NULL;
g_autofree char *valuestr = NULL; g_autofree char *valuestr = NULL;
if (virAsprintf(&path, SYSFS_NET_DIR "%s/brif/%s/%s", path = g_strdup_printf(SYSFS_NET_DIR "%s/brif/%s/%s", brname, ifname,
brname, ifname, paramname) < 0) paramname);
return -1;
if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), &valuestr) < 0) if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), &valuestr) < 0)
return -1; return -1;

View File

@ -487,9 +487,8 @@ virNetDevIPGetAcceptRA(const char *ifname)
char *suffix; char *suffix;
int accept_ra = -1; int accept_ra = -1;
if (virAsprintf(&path, "/proc/sys/net/ipv6/conf/%s/accept_ra", path = g_strdup_printf("/proc/sys/net/ipv6/conf/%s/accept_ra",
ifname ? ifname : "all") < 0) ifname ? ifname : "all");
return -1;
if ((virFileReadAll(path, 512, &buf) < 0) || if ((virFileReadAll(path, 512, &buf) < 0) ||
(virStrToLong_i(buf, &suffix, 10, &accept_ra) < 0)) (virStrToLong_i(buf, &suffix, 10, &accept_ra) < 0))

View File

@ -294,8 +294,7 @@ virNetDevMacVLanIsMacvtap(const char *ifname)
if (virNetDevGetIndex(ifname, &ifindex) < 0) if (virNetDevGetIndex(ifname, &ifindex) < 0)
return false; return false;
if (virAsprintf(&tapname, "/dev/tap%d", ifindex) < 0) tapname = g_strdup_printf("/dev/tap%d", ifindex);
return false;
return virFileExists(tapname); return virFileExists(tapname);
} }
@ -393,8 +392,7 @@ virNetDevMacVLanTapOpen(const char *ifname,
if (virNetDevGetIndex(ifname, &ifindex) < 0) if (virNetDevGetIndex(ifname, &ifindex) < 0)
return -1; return -1;
if (virAsprintf(&tapname, "/dev/tap%d", ifindex) < 0) tapname = g_strdup_printf("/dev/tap%d", ifindex);
goto cleanup;
for (i = 0; i < tapfdSize; i++) { for (i = 0; i < tapfdSize; i++) {
int fd = -1; int fd = -1;

View File

@ -148,19 +148,13 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
virUUIDFormat(ovsport->interfaceID, ifuuidstr); virUUIDFormat(ovsport->interfaceID, ifuuidstr);
virUUIDFormat(vmuuid, vmuuidstr); virUUIDFormat(vmuuid, vmuuidstr);
if (virAsprintf(&attachedmac_ex_id, "external-ids:attached-mac=\"%s\"", attachedmac_ex_id = g_strdup_printf("external-ids:attached-mac=\"%s\"",
macaddrstr) < 0) macaddrstr);
return -1; ifaceid_ex_id = g_strdup_printf("external-ids:iface-id=\"%s\"", ifuuidstr);
if (virAsprintf(&ifaceid_ex_id, "external-ids:iface-id=\"%s\"", vmid_ex_id = g_strdup_printf("external-ids:vm-id=\"%s\"", vmuuidstr);
ifuuidstr) < 0)
return -1;
if (virAsprintf(&vmid_ex_id, "external-ids:vm-id=\"%s\"",
vmuuidstr) < 0)
return -1;
if (ovsport->profileID[0] != '\0') { if (ovsport->profileID[0] != '\0') {
if (virAsprintf(&profile_ex_id, "external-ids:port-profile=\"%s\"", profile_ex_id = g_strdup_printf("external-ids:port-profile=\"%s\"",
ovsport->profileID) < 0) ovsport->profileID);
return -1;
} }
cmd = virCommandNew(OVSVSCTL); cmd = virCommandNew(OVSVSCTL);

View File

@ -395,8 +395,7 @@ int virNetDevTapCreate(char **ifname,
for (i = 0; i <= IF_MAXUNIT; i++) { for (i = 0; i <= IF_MAXUNIT; i++) {
g_autofree char *newname = NULL; g_autofree char *newname = NULL;
if (virAsprintf(&newname, *ifname, i) < 0) newname = g_strdup_printf(*ifname, i);
goto cleanup;
if (virNetDevExists(newname) == 0) { if (virNetDevExists(newname) == 0) {
newifname = g_steal_pointer(&newname); newifname = g_steal_pointer(&newname);
@ -416,8 +415,7 @@ int virNetDevTapCreate(char **ifname,
if (tapfd) { if (tapfd) {
g_autofree char *dev_path = NULL; g_autofree char *dev_path = NULL;
if (virAsprintf(&dev_path, "/dev/%s", ifr.ifr_name) < 0) dev_path = g_strdup_printf("/dev/%s", ifr.ifr_name);
goto cleanup;
if ((*tapfd = open(dev_path, O_RDWR)) < 0) { if ((*tapfd = open(dev_path, O_RDWR)) < 0) {
virReportSystemError(errno, virReportSystemError(errno,

View File

@ -44,8 +44,7 @@ static int virNetDevVethExists(int devNum)
int ret; int ret;
g_autofree char *path = NULL; g_autofree char *path = NULL;
if (virAsprintf(&path, SYSFS_NET_DIR "vnet%d/", devNum) < 0) path = g_strdup_printf(SYSFS_NET_DIR "vnet%d/", devNum);
return -1;
ret = virFileExists(path) ? 1 : 0; ret = virFileExists(path) ? 1 : 0;
VIR_DEBUG("Checked dev vnet%d usage: %d", devNum, ret); VIR_DEBUG("Checked dev vnet%d usage: %d", devNum, ret);
return ret; return ret;
@ -128,8 +127,7 @@ int virNetDevVethCreate(char** veth1, char** veth2)
if ((veth1num = virNetDevVethGetFreeNum(vethNum)) < 0) if ((veth1num = virNetDevVethGetFreeNum(vethNum)) < 0)
goto cleanup; goto cleanup;
if (virAsprintf(&veth1auto, "vnet%d", veth1num) < 0) veth1auto = g_strdup_printf("vnet%d", veth1num);
goto cleanup;
vethNum = veth1num + 1; vethNum = veth1num + 1;
} }
if (!*veth2) { if (!*veth2) {
@ -137,8 +135,7 @@ int virNetDevVethCreate(char** veth1, char** veth2)
if ((veth2num = virNetDevVethGetFreeNum(vethNum)) < 0) if ((veth2num = virNetDevVethGetFreeNum(vethNum)) < 0)
goto cleanup; goto cleanup;
if (virAsprintf(&veth2auto, "vnet%d", veth2num) < 0) veth2auto = g_strdup_printf("vnet%d", veth2num);
goto cleanup;
vethNum = veth2num + 1; vethNum = veth2num + 1;
} }

View File

@ -538,23 +538,18 @@ virNumaGetHugePageInfoPath(char **path,
unsigned int page_size, unsigned int page_size,
const char *suffix) const char *suffix)
{ {
int ret;
if (node == -1) { if (node == -1) {
/* We are aiming at overall system info */ /* We are aiming at overall system info */
ret = virAsprintf(path, *path = g_strdup_printf(HUGEPAGES_SYSTEM_PREFIX HUGEPAGES_PREFIX "%ukB/%s",
HUGEPAGES_SYSTEM_PREFIX HUGEPAGES_PREFIX "%ukB/%s", page_size, NULLSTR_EMPTY(suffix));
page_size, NULLSTR_EMPTY(suffix));
} else { } else {
/* We are aiming on specific NUMA node */ /* We are aiming on specific NUMA node */
ret = virAsprintf(path, *path = g_strdup_printf(HUGEPAGES_NUMA_PREFIX "node%d/hugepages/"
HUGEPAGES_NUMA_PREFIX "node%d/hugepages/" HUGEPAGES_PREFIX "%ukB/%s",
HUGEPAGES_PREFIX "%ukB/%s", node, page_size, NULLSTR_EMPTY(suffix));
node, page_size, NULLSTR_EMPTY(suffix));
} }
if (ret >= 0 && !virFileExists(*path)) { if (!virFileExists(*path)) {
ret = -1;
if (node != -1) { if (node != -1) {
if (!virNumaNodeIsAvailable(node)) { if (!virNumaNodeIsAvailable(node)) {
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
@ -570,9 +565,10 @@ virNumaGetHugePageInfoPath(char **path,
_("page size %u is not available"), _("page size %u is not available"),
page_size); page_size);
} }
return -1;
} }
return ret; return 0;
} }
static int static int
@ -582,9 +578,9 @@ virNumaGetHugePageInfoDir(char **path, int node)
*path = g_strdup(HUGEPAGES_SYSTEM_PREFIX); *path = g_strdup(HUGEPAGES_SYSTEM_PREFIX);
return 0; return 0;
} else { } else {
return virAsprintf(path, *path = g_strdup_printf(HUGEPAGES_NUMA_PREFIX "node%d/hugepages/",
HUGEPAGES_NUMA_PREFIX "node%d/hugepages/", node);
node); return 0;
} }
} }
@ -930,8 +926,7 @@ virNumaSetPagePoolSize(int node,
* all the pages we wanted. So do the second read to check. * all the pages we wanted. So do the second read to check.
*/ */
VIR_FREE(nr_buf); VIR_FREE(nr_buf);
if (virAsprintf(&nr_buf, "%llu", page_count) < 0) nr_buf = g_strdup_printf("%llu", page_count);
return -1;
if (virFileWriteStr(nr_path, nr_buf, 0) < 0) { if (virFileWriteStr(nr_path, nr_buf, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,

View File

@ -213,7 +213,7 @@ virPCIDriverDir(const char *driver)
{ {
char *buffer; char *buffer;
ignore_value(virAsprintf(&buffer, PCI_SYSFS "drivers/%s", driver)); buffer = g_strdup_printf(PCI_SYSFS "drivers/%s", driver);
return buffer; return buffer;
} }
@ -223,7 +223,7 @@ virPCIFile(const char *device, const char *file)
{ {
char *buffer; char *buffer;
ignore_value(virAsprintf(&buffer, PCI_SYSFS "devices/%s/%s", device, file)); buffer = g_strdup_printf(PCI_SYSFS "devices/%s/%s", device, file);
return buffer; return buffer;
} }
@ -604,8 +604,7 @@ virPCIDeviceDetectFunctionLevelReset(virPCIDevicePtr dev, int cfgfd)
* device is a VF, we just assume FLR works * device is a VF, we just assume FLR works
*/ */
if (virAsprintf(&path, PCI_SYSFS "devices/%s/physfn", dev->name) < 0) path = g_strdup_printf(PCI_SYSFS "devices/%s/physfn", dev->name);
return -1;
found = virFileExists(path); found = virFileExists(path);
if (found) { if (found) {
@ -1346,12 +1345,11 @@ virPCIDeviceAddressAsString(const virPCIDeviceAddress *addr)
{ {
char *str; char *str;
ignore_value(virAsprintf(&str, str = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT,
VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain,
addr->domain, addr->bus,
addr->bus, addr->slot,
addr->slot, addr->function);
addr->function));
return str; return str;
} }
@ -1373,14 +1371,10 @@ virPCIDeviceNew(unsigned int domain,
dev->address.slot = slot; dev->address.slot = slot;
dev->address.function = function; dev->address.function = function;
if (virAsprintf(&dev->name, dev->name = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT, domain, bus, slot,
VIR_PCI_DEVICE_ADDRESS_FMT, function);
domain, bus, slot, function) < 0)
return NULL;
if (virAsprintf(&dev->path, PCI_SYSFS "devices/%s/config", dev->path = g_strdup_printf(PCI_SYSFS "devices/%s/config", dev->name);
dev->name) < 0)
return NULL;
if (!virFileExists(dev->path)) { if (!virFileExists(dev->path)) {
virReportSystemError(errno, virReportSystemError(errno,
@ -1725,10 +1719,9 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
struct dirent *ent; struct dirent *ent;
int direrr; int direrr;
if (virAsprintf(&pcidir, "/sys/bus/pci/devices/" VIR_PCI_DEVICE_ADDRESS_FMT, pcidir = g_strdup_printf("/sys/bus/pci/devices/" VIR_PCI_DEVICE_ADDRESS_FMT,
dev->address.domain, dev->address.bus, dev->address.domain, dev->address.bus, dev->address.slot,
dev->address.slot, dev->address.function) < 0) dev->address.function);
goto cleanup;
if (virDirOpen(&dir, pcidir) < 0) if (virDirOpen(&dir, pcidir) < 0)
goto cleanup; goto cleanup;
@ -1745,8 +1738,7 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
STREQ(ent->d_name, "vendor") || STREQ(ent->d_name, "vendor") ||
STREQ(ent->d_name, "device") || STREQ(ent->d_name, "device") ||
STREQ(ent->d_name, "reset")) { STREQ(ent->d_name, "reset")) {
if (virAsprintf(&file, "%s/%s", pcidir, ent->d_name) < 0) file = g_strdup_printf("%s/%s", pcidir, ent->d_name);
goto cleanup;
if ((actor)(dev, file, opaque) < 0) if ((actor)(dev, file, opaque) < 0)
goto cleanup; goto cleanup;
} }
@ -1778,10 +1770,8 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
struct dirent *ent; struct dirent *ent;
int direrr; int direrr;
if (virAsprintf(&groupPath, groupPath = g_strdup_printf(PCI_SYSFS "devices/" VIR_PCI_DEVICE_ADDRESS_FMT "/iommu_group/devices",
PCI_SYSFS "devices/" VIR_PCI_DEVICE_ADDRESS_FMT "/iommu_group/devices", orig->domain, orig->bus, orig->slot, orig->function);
orig->domain, orig->bus, orig->slot, orig->function) < 0)
goto cleanup;
if (virDirOpenQuiet(&groupDir, groupPath) < 0) { if (virDirOpenQuiet(&groupDir, groupPath) < 0) {
/* just process the original device, nothing more */ /* just process the original device, nothing more */
@ -1928,10 +1918,8 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
const char *groupNumStr; const char *groupNumStr;
unsigned int groupNum; unsigned int groupNum;
if (virAsprintf(&devName, devName = g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain, addr->bus,
VIR_PCI_DEVICE_ADDRESS_FMT, addr->slot, addr->function);
addr->domain, addr->bus, addr->slot, addr->function) < 0)
return -1;
if (!(devPath = virPCIFile(devName, "iommu_group"))) if (!(devPath = virPCIFile(devName, "iommu_group")))
return -1; return -1;
@ -1981,9 +1969,7 @@ virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev)
dev->name, devPath); dev->name, devPath);
return NULL; return NULL;
} }
if (virAsprintf(&groupDev, "/dev/vfio/%s", groupDev = g_strdup_printf("/dev/vfio/%s", last_component(groupPath));
last_component(groupPath)) < 0)
return NULL;
return groupDev; return groupDev;
} }
@ -2282,8 +2268,7 @@ virPCIGetVirtualFunctions(const char *sysfs_path,
*num_virtual_functions = 0; *num_virtual_functions = 0;
*max_virtual_functions = 0; *max_virtual_functions = 0;
if (virAsprintf(&totalvfs_file, "%s/sriov_totalvfs", sysfs_path) < 0) totalvfs_file = g_strdup_printf("%s/sriov_totalvfs", sysfs_path);
goto error;
if (virFileExists(totalvfs_file)) { if (virFileExists(totalvfs_file)) {
char *end = NULL; /* so that terminating \n doesn't create error */ char *end = NULL; /* so that terminating \n doesn't create error */
@ -2300,8 +2285,8 @@ virPCIGetVirtualFunctions(const char *sysfs_path,
do { do {
g_autofree char *device_link = NULL; g_autofree char *device_link = NULL;
/* look for virtfn%d links until one isn't found */ /* look for virtfn%d links until one isn't found */
if (virAsprintf(&device_link, "%s/virtfn%zu", sysfs_path, *num_virtual_functions) < 0) device_link = g_strdup_printf("%s/virtfn%zu", sysfs_path,
goto error; *num_virtual_functions);
if (!virFileExists(device_link)) if (!virFileExists(device_link))
break; break;
@ -2342,9 +2327,7 @@ virPCIIsVirtualFunction(const char *vf_sysfs_device_link)
{ {
g_autofree char *vf_sysfs_physfn_link = NULL; g_autofree char *vf_sysfs_physfn_link = NULL;
if (virAsprintf(&vf_sysfs_physfn_link, "%s/physfn", vf_sysfs_physfn_link = g_strdup_printf("%s/physfn", vf_sysfs_device_link);
vf_sysfs_device_link) < 0)
return -1;
return virFileExists(vf_sysfs_physfn_link); return virFileExists(vf_sysfs_physfn_link);
} }
@ -2402,9 +2385,8 @@ virPCIGetVirtualFunctionIndex(const char *pf_sysfs_device_link,
int int
virPCIGetSysfsFile(char *virPCIDeviceName, char **pci_sysfs_device_link) virPCIGetSysfsFile(char *virPCIDeviceName, char **pci_sysfs_device_link)
{ {
if (virAsprintf(pci_sysfs_device_link, PCI_SYSFS "devices/%s", *pci_sysfs_device_link = g_strdup_printf(PCI_SYSFS "devices/%s",
virPCIDeviceName) < 0) virPCIDeviceName);
return -1;
return 0; return 0;
} }
@ -2412,11 +2394,8 @@ int
virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr addr, virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr addr,
char **pci_sysfs_device_link) char **pci_sysfs_device_link)
{ {
if (virAsprintf(pci_sysfs_device_link, *pci_sysfs_device_link = g_strdup_printf(PCI_SYSFS "devices/" VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain,
PCI_SYSFS "devices/" VIR_PCI_DEVICE_ADDRESS_FMT, addr->bus, addr->slot, addr->function);
addr->domain, addr->bus,
addr->slot, addr->function) < 0)
return -1;
return 0; return 0;
} }
@ -2601,8 +2580,7 @@ virPCIGetMdevTypes(const char *sysfspath,
size_t ntypes = 0; size_t ntypes = 0;
size_t i; size_t i;
if (virAsprintf(&types_path, "%s/mdev_supported_types", sysfspath) < 0) types_path = g_strdup_printf("%s/mdev_supported_types", sysfspath);
return -1;
if ((dirret = virDirOpenIfExists(&dir, types_path)) < 0) if ((dirret = virDirOpenIfExists(&dir, types_path)) < 0)
goto cleanup; goto cleanup;
@ -2615,8 +2593,7 @@ virPCIGetMdevTypes(const char *sysfspath,
while ((dirret = virDirRead(dir, &entry, types_path)) > 0) { while ((dirret = virDirRead(dir, &entry, types_path)) > 0) {
g_autofree char *tmppath = NULL; g_autofree char *tmppath = NULL;
/* append the type id to the path and read the attributes from there */ /* append the type id to the path and read the attributes from there */
if (virAsprintf(&tmppath, "%s/%s", types_path, entry->d_name) < 0) tmppath = g_strdup_printf("%s/%s", types_path, entry->d_name);
goto cleanup;
if (virMediatedDeviceTypeReadAttrs(tmppath, &mdev_type) < 0) if (virMediatedDeviceTypeReadAttrs(tmppath, &mdev_type) < 0)
goto cleanup; goto cleanup;

View File

@ -226,8 +226,7 @@ int virPidFileReadPathIfAlive(const char *path,
goto cleanup; goto cleanup;
} }
if (virAsprintf(&procPath, "/proc/%lld/exe", (long long)retPid) < 0) procPath = g_strdup_printf("/proc/%lld/exe", (long long)retPid);
return -ENOMEM;
if ((ret = virFileIsLink(procPath)) < 0) if ((ret = virFileIsLink(procPath)) < 0)
return ret; return ret;
@ -488,8 +487,7 @@ virPidFileConstructPath(bool privileged,
"%s", _("No runstatedir specified")); "%s", _("No runstatedir specified"));
return -1; return -1;
} }
if (virAsprintf(pidfile, "%s/%s.pid", runstatedir, progname) < 0) *pidfile = g_strdup_printf("%s/%s.pid", runstatedir, progname);
return -1;
} else { } else {
if (!(rundir = virGetUserRuntimeDirectory())) if (!(rundir = virGetUserRuntimeDirectory()))
return -1; return -1;
@ -501,8 +499,7 @@ virPidFileConstructPath(bool privileged,
return -1; return -1;
} }
if (virAsprintf(pidfile, "%s/%s.pid", rundir, progname) < 0) *pidfile = g_strdup_printf("%s/%s.pid", rundir, progname);
return -1;
} }
return 0; return 0;

View File

@ -129,13 +129,13 @@ virProcessTranslateStatus(int status)
{ {
char *buf; char *buf;
if (WIFEXITED(status)) { if (WIFEXITED(status)) {
ignore_value(virAsprintfQuiet(&buf, _("exit status %d"), buf = g_strdup_printf(_("exit status %d"),
WEXITSTATUS(status))); WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) { } else if (WIFSIGNALED(status)) {
ignore_value(virAsprintfQuiet(&buf, _("fatal signal %d"), buf = g_strdup_printf(_("fatal signal %d"),
WTERMSIG(status))); WTERMSIG(status));
} else { } else {
ignore_value(virAsprintfQuiet(&buf, _("invalid value %d"), status)); buf = g_strdup_printf(_("invalid value %d"), status);
} }
return buf; return buf;
} }
@ -586,8 +586,7 @@ int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
*npids = 0; *npids = 0;
*pids = NULL; *pids = NULL;
if (virAsprintf(&taskPath, "/proc/%llu/task", (long long) pid) < 0) taskPath = g_strdup_printf("/proc/%llu/task", (long long)pid);
goto cleanup;
if (virDirOpen(&dir, taskPath) < 0) if (virDirOpen(&dir, taskPath) < 0)
goto cleanup; goto cleanup;
@ -632,10 +631,7 @@ int virProcessGetNamespaces(pid_t pid,
int fd; int fd;
g_autofree char *nsfile = NULL; g_autofree char *nsfile = NULL;
if (virAsprintf(&nsfile, "/proc/%llu/ns/%s", nsfile = g_strdup_printf("/proc/%llu/ns/%s", (long long)pid, ns[i]);
(long long) pid,
ns[i]) < 0)
goto cleanup;
if ((fd = open(nsfile, O_RDONLY)) >= 0) { if ((fd = open(nsfile, O_RDONLY)) >= 0) {
if (VIR_EXPAND_N(*fdlist, *nfdlist, 1) < 0) { if (VIR_EXPAND_N(*fdlist, *nfdlist, 1) < 0) {
@ -954,8 +950,7 @@ int virProcessGetStartTime(pid_t pid,
g_autofree char *buf = NULL; g_autofree char *buf = NULL;
VIR_AUTOSTRINGLIST tokens = NULL; VIR_AUTOSTRINGLIST tokens = NULL;
if (virAsprintf(&filename, "/proc/%llu/stat", (long long) pid) < 0) filename = g_strdup_printf("/proc/%llu/stat", (long long)pid);
return -1;
if ((len = virFileReadAll(filename, 1024, &buf)) < 0) if ((len = virFileReadAll(filename, 1024, &buf)) < 0)
return -1; return -1;
@ -1054,8 +1049,7 @@ static int virProcessNamespaceHelper(pid_t pid G_GNUC_UNUSED,
int ret = -1; int ret = -1;
g_autofree char *path = NULL; g_autofree char *path = NULL;
if (virAsprintf(&path, "/proc/%lld/ns/mnt", (long long) data->pid) < 0) path = g_strdup_printf("/proc/%lld/ns/mnt", (long long)data->pid);
goto cleanup;
if ((fd = open(path, O_RDONLY)) < 0) { if ((fd = open(path, O_RDONLY)) < 0) {
virReportSystemError(errno, "%s", virReportSystemError(errno, "%s",

View File

@ -89,8 +89,7 @@ virQEMUBuildCommandLineJSONArrayNumbered(const char *key,
member = virJSONValueArrayGet((virJSONValuePtr) array, i); member = virJSONValueArrayGet((virJSONValuePtr) array, i);
g_autofree char *prefix = NULL; g_autofree char *prefix = NULL;
if (virAsprintf(&prefix, "%s.%zu", key, i) < 0) prefix = g_strdup_printf("%s.%zu", key, i);
return 0;
if (virQEMUBuildCommandLineJSONRecurse(prefix, member, buf, if (virQEMUBuildCommandLineJSONRecurse(prefix, member, buf,
virQEMUBuildCommandLineJSONArrayNumbered, virQEMUBuildCommandLineJSONArrayNumbered,
@ -113,8 +112,7 @@ virQEMUBuildCommandLineJSONIterate(const char *key,
if (data->prefix) { if (data->prefix) {
g_autofree char *tmpkey = NULL; g_autofree char *tmpkey = NULL;
if (virAsprintf(&tmpkey, "%s.%s", data->prefix, key) < 0) tmpkey = g_strdup_printf("%s.%s", data->prefix, key);
return -1;
return virQEMUBuildCommandLineJSONRecurse(tmpkey, value, data->buf, return virQEMUBuildCommandLineJSONRecurse(tmpkey, value, data->buf,
data->arrayFunc, false); data->arrayFunc, false);

View File

@ -194,8 +194,7 @@ virRandomGenerateWWN(char **wwn,
return -1; return -1;
} }
if (virAsprintf(wwn, "5" "%s%09llx", oui, *wwn = g_strdup_printf("5" "%s%09llx", oui,
(unsigned long long)virRandomBits(36)) < 0) (unsigned long long)virRandomBits(36));
return -1;
return 0; return 0;
} }

View File

@ -2319,8 +2319,7 @@ virResctrlDeterminePath(const char *parentpath,
return NULL; return NULL;
} }
if (virAsprintf(&path, "%s/%s-%s", parentpath, prefix, id) < 0) path = g_strdup_printf("%s/%s-%s", parentpath, prefix, id);
return NULL;
return path; return path;
} }
@ -2416,8 +2415,7 @@ virResctrlAllocCreate(virResctrlInfoPtr resctrl,
if (!alloc_str) if (!alloc_str)
goto cleanup; goto cleanup;
if (virAsprintf(&schemata_path, "%s/schemata", alloc->path) < 0) schemata_path = g_strdup_printf("%s/schemata", alloc->path);
goto cleanup;
VIR_DEBUG("Writing resctrl schemata '%s' into '%s'", alloc_str, schemata_path); VIR_DEBUG("Writing resctrl schemata '%s' into '%s'", alloc_str, schemata_path);
if (virFileWriteStr(schemata_path, alloc_str, 0) < 0) { if (virFileWriteStr(schemata_path, alloc_str, 0) < 0) {
@ -2451,11 +2449,9 @@ virResctrlAddPID(const char *path,
return -1; return -1;
} }
if (virAsprintf(&tasks, "%s/tasks", path) < 0) tasks = g_strdup_printf("%s/tasks", path);
return -1;
if (virAsprintf(&pidstr, "%lld", (long long int) pid) < 0) pidstr = g_strdup_printf("%lld", (long long int)pid);
goto cleanup;
if (virFileWriteStr(tasks, pidstr, 0) < 0) { if (virFileWriteStr(tasks, pidstr, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -2566,8 +2562,7 @@ virResctrlMonitorDeterminePath(virResctrlMonitorPtr monitor,
return 0; return 0;
} }
if (virAsprintf(&parentpath, "%s/mon_groups", monitor->alloc->path) < 0) parentpath = g_strdup_printf("%s/mon_groups", monitor->alloc->path);
return -1;
monitor->path = virResctrlDeterminePath(parentpath, machinename, monitor->path = virResctrlDeterminePath(parentpath, machinename,
monitor->id); monitor->id);
@ -2699,8 +2694,7 @@ virResctrlMonitorGetStats(virResctrlMonitorPtr monitor,
return -1; return -1;
} }
if (virAsprintf(&datapath, "%s/mon_data", monitor->path) < 0) datapath = g_strdup_printf("%s/mon_data", monitor->path);
return -1;
if (virDirOpen(&dirp, datapath) < 0) if (virDirOpen(&dirp, datapath) < 0)
goto cleanup; goto cleanup;
@ -2717,8 +2711,7 @@ virResctrlMonitorGetStats(virResctrlMonitorPtr monitor,
* "mon_L3_01" are two target directories for a two nodes system * "mon_L3_01" are two target directories for a two nodes system
* with resource utilization data file for each node respectively. * with resource utilization data file for each node respectively.
*/ */
if (virAsprintf(&filepath, "%s/%s", datapath, ent->d_name) < 0) filepath = g_strdup_printf("%s/%s", datapath, ent->d_name);
goto cleanup;
if (!virFileIsDir(filepath)) if (!virFileIsDir(filepath))
continue; continue;

View File

@ -195,8 +195,7 @@ virRotatingFileWriterDelete(virRotatingFileWriterPtr file)
for (i = 0; i < file->maxbackup; i++) { for (i = 0; i < file->maxbackup; i++) {
char *oldpath; char *oldpath;
if (virAsprintf(&oldpath, "%s.%zu", file->basepath, i) < 0) oldpath = g_strdup_printf("%s.%zu", file->basepath, i);
return -1;
if (unlink(oldpath) < 0 && if (unlink(oldpath) < 0 &&
errno != ENOENT) { errno != ENOENT) {
@ -308,8 +307,7 @@ virRotatingFileReaderNew(const char *path,
for (i = 0; i < maxbackup; i++) { for (i = 0; i < maxbackup; i++) {
char *tmppath; char *tmppath;
if (virAsprintf(&tmppath, "%s.%zu", path, i) < 0) tmppath = g_strdup_printf("%s.%zu", path, i);
goto error;
file->entries[file->nentries - (i + 2)] = virRotatingFileReaderEntryNew(tmppath); file->entries[file->nentries - (i + 2)] = virRotatingFileReaderEntryNew(tmppath);
VIR_FREE(tmppath); VIR_FREE(tmppath);
@ -382,15 +380,13 @@ virRotatingFileWriterRollover(virRotatingFileWriterPtr file)
goto cleanup; goto cleanup;
} }
} else { } else {
if (virAsprintf(&nextpath, "%s.%zu", file->basepath, file->maxbackup - 1) < 0) nextpath = g_strdup_printf("%s.%zu", file->basepath, file->maxbackup - 1);
return -1;
for (i = file->maxbackup; i > 0; i--) { for (i = file->maxbackup; i > 0; i--) {
if (i == 1) { if (i == 1) {
thispath = g_strdup(file->basepath); thispath = g_strdup(file->basepath);
} else { } else {
if (virAsprintf(&thispath, "%s.%zu", file->basepath, i - 2) < 0) thispath = g_strdup_printf("%s.%zu", file->basepath, i - 2);
goto cleanup;
} }
VIR_DEBUG("Rollover %s -> %s", thispath, nextpath); VIR_DEBUG("Rollover %s -> %s", thispath, nextpath);

View File

@ -117,10 +117,8 @@ virSCSIDeviceGetSgName(const char *sysfs_prefix,
if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0) if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0)
return NULL; return NULL;
if (virAsprintf(&path, path = g_strdup_printf("%s/%d:%u:%u:%llu/scsi_generic", prefix, adapter_id,
"%s/%d:%u:%u:%llu/scsi_generic", bus, target, unit);
prefix, adapter_id, bus, target, unit) < 0)
return NULL;
if (virDirOpen(&dir, path) < 0) if (virDirOpen(&dir, path) < 0)
goto cleanup; goto cleanup;
@ -156,10 +154,8 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0) if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0)
return NULL; return NULL;
if (virAsprintf(&path, path = g_strdup_printf("%s/%d:%u:%u:%llu/block", prefix, adapter_id, bus,
"%s/%d:%u:%u:%llu/block", target, unit);
prefix, adapter_id, bus, target, unit) < 0)
return NULL;
if (virDirOpen(&dir, path) < 0) if (virDirOpen(&dir, path) < 0)
goto cleanup; goto cleanup;
@ -206,11 +202,10 @@ virSCSIDeviceNew(const char *sysfs_prefix,
if (virSCSIDeviceGetAdapterId(adapter, &dev->adapter) < 0) if (virSCSIDeviceGetAdapterId(adapter, &dev->adapter) < 0)
return NULL; return NULL;
if (virAsprintf(&dev->name, "%d:%u:%u:%llu", dev->adapter, dev->name = g_strdup_printf("%d:%u:%u:%llu", dev->adapter,
dev->bus, dev->target, dev->unit) < 0 || dev->bus, dev->target, dev->unit);
virAsprintf(&dev->sg_path, "%s/%s", dev->sg_path = g_strdup_printf("%s/%s",
sysfs_prefix ? sysfs_prefix : "/dev", sg) < 0) sysfs_prefix ? sysfs_prefix : "/dev", sg);
return NULL;
if (!virFileExists(dev->sg_path)) { if (!virFileExists(dev->sg_path)) {
virReportSystemError(errno, virReportSystemError(errno,
@ -219,11 +214,8 @@ virSCSIDeviceNew(const char *sysfs_prefix,
return NULL; return NULL;
} }
if (virAsprintf(&vendor_path, vendor_path = g_strdup_printf("%s/%s/vendor", prefix, dev->name);
"%s/%s/vendor", prefix, dev->name) < 0 || model_path = g_strdup_printf("%s/%s/model", prefix, dev->name);
virAsprintf(&model_path,
"%s/%s/model", prefix, dev->name) < 0)
return NULL;
if (virFileReadAll(vendor_path, 1024, &vendor) < 0) if (virFileReadAll(vendor_path, 1024, &vendor) < 0)
return NULL; return NULL;
@ -234,8 +226,7 @@ virSCSIDeviceNew(const char *sysfs_prefix,
virTrimSpaces(vendor, NULL); virTrimSpaces(vendor, NULL);
virTrimSpaces(model, NULL); virTrimSpaces(model, NULL);
if (virAsprintf(&dev->id, "%s:%s", vendor, model) < 0) dev->id = g_strdup_printf("%s:%s", vendor, model);
return NULL;
return g_steal_pointer(&dev); return g_steal_pointer(&dev);
} }

View File

@ -52,10 +52,8 @@ virSCSIHostGetUniqueId(const char *sysfs_prefix,
char *buf = NULL; char *buf = NULL;
int unique_id; int unique_id;
if (virAsprintf(&sysfs_path, "%s/host%d/unique_id", sysfs_path = g_strdup_printf("%s/host%d/unique_id",
sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_HOST_PATH, sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_HOST_PATH, host);
host) < 0)
return -1;
if (virFileReadAll(sysfs_path, 1024, &buf) < 0) if (virFileReadAll(sysfs_path, 1024, &buf) < 0)
goto cleanup; goto cleanup;
@ -118,8 +116,7 @@ virSCSIHostFindByPCI(const char *sysfs_prefix,
if (!virFileIsLink(entry->d_name)) if (!virFileIsLink(entry->d_name))
continue; continue;
if (virAsprintf(&host_link, "%s/%s", prefix, entry->d_name) < 0) host_link = g_strdup_printf("%s/%s", prefix, entry->d_name);
goto cleanup;
if (virFileResolveLink(host_link, &host_path) < 0) if (virFileResolveLink(host_link, &host_path) < 0)
goto cleanup; goto cleanup;
@ -132,9 +129,7 @@ virSCSIHostFindByPCI(const char *sysfs_prefix,
VIR_FREE(host_link); VIR_FREE(host_link);
VIR_FREE(host_path); VIR_FREE(host_path);
if (virAsprintf(&unique_path, "%s/%s/unique_id", prefix, unique_path = g_strdup_printf("%s/%s/unique_id", prefix, entry->d_name);
entry->d_name) < 0)
goto cleanup;
if (!virFileExists(unique_path)) { if (!virFileExists(unique_path)) {
VIR_FREE(unique_path); VIR_FREE(unique_path);
@ -240,9 +235,8 @@ virSCSIHostGetNameByParentaddr(unsigned int domain,
char *name = NULL; char *name = NULL;
char *parentaddr = NULL; char *parentaddr = NULL;
if (virAsprintf(&parentaddr, "%04x:%02x:%02x.%01x", parentaddr = g_strdup_printf("%04x:%02x:%02x.%01x", domain, bus, slot,
domain, bus, slot, function) < 0) function);
goto cleanup;
if (!(name = virSCSIHostFindByPCI(NULL, parentaddr, unique_id))) { if (!(name = virSCSIHostFindByPCI(NULL, parentaddr, unique_id))) {
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("Failed to find scsi_host using PCI '%s' " _("Failed to find scsi_host using PCI '%s' "

View File

@ -255,9 +255,7 @@ virSCSIVHostDeviceNew(const char *name)
dev->name = g_strdup(name); dev->name = g_strdup(name);
if (virAsprintf(&dev->path, "%s/%s", dev->path = g_strdup_printf("%s/%s", SYSFS_VHOST_SCSI_DEVICES, name);
SYSFS_VHOST_SCSI_DEVICES, name) < 0)
return NULL;
VIR_DEBUG("%s: initialized", dev->name); VIR_DEBUG("%s: initialized", dev->name);

View File

@ -477,9 +477,8 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
* nicely for UNIX sockets */ * nicely for UNIX sockets */
if (addr->data.sa.sa_family == AF_UNIX) { if (addr->data.sa.sa_family == AF_UNIX) {
if (withService) { if (withService) {
if (virAsprintf(&addrstr, VIR_LOOPBACK_IPV4_ADDR"%s0", addrstr = g_strdup_printf(VIR_LOOPBACK_IPV4_ADDR "%s0",
separator ? separator : ":") < 0) separator ? separator : ":");
return NULL;
} else { } else {
addrstr = g_strdup(VIR_LOOPBACK_IPV4_ADDR); addrstr = g_strdup(VIR_LOOPBACK_IPV4_ADDR);
} }
@ -503,16 +502,12 @@ virSocketAddrFormatFull(const virSocketAddr *addr,
* a.b.c.d;port or e:f:g:h:i:j:k:l;port, so use square brackets for * a.b.c.d;port or e:f:g:h:i:j:k:l;port, so use square brackets for
* IPv6 only if no separator is passed to the function * IPv6 only if no separator is passed to the function
*/ */
if (!separator && VIR_SOCKET_ADDR_FAMILY(addr) == AF_INET6) { if (!separator && VIR_SOCKET_ADDR_FAMILY(addr) == AF_INET6)
if (virAsprintf(&ipv6_host, "[%s]", host) < 0) ipv6_host = g_strdup_printf("[%s]", host);
return NULL;
}
if (virAsprintf(&addrstr, "%s%s%s", addrstr = g_strdup_printf("%s%s%s",
ipv6_host ? ipv6_host : host, ipv6_host ? ipv6_host : host,
separator ? separator : ":", port) == -1) { separator ? separator : ":", port);
return NULL;
}
} else { } else {
addrstr = g_strdup(host); addrstr = g_strdup(host);
} }

View File

@ -1536,7 +1536,7 @@ virStorageFileGetNPIVKey(const char *path,
*tmp = '\0'; *tmp = '\0';
if (*serial != '\0' && *port != '\0') if (*serial != '\0' && *port != '\0')
ignore_value(virAsprintf(key, "%s_PORT%s", serial, port)); *key = g_strdup_printf("%s_PORT%s", serial, port);
} }
return 0; return 0;
@ -2610,11 +2610,9 @@ virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent,
} }
if (STRNEQ(dirname, "/")) { if (STRNEQ(dirname, "/")) {
if (virAsprintf(&def->path, "%s/%s", dirname, rel) < 0) def->path = g_strdup_printf("%s/%s", dirname, rel);
return NULL;
} else { } else {
if (virAsprintf(&def->path, "/%s", rel) < 0) def->path = g_strdup_printf("/%s", rel);
return NULL;
} }
if (virStorageSourceGetActualType(parent) == VIR_STORAGE_TYPE_NETWORK) { if (virStorageSourceGetActualType(parent) == VIR_STORAGE_TYPE_NETWORK) {
@ -3272,8 +3270,7 @@ virStorageSourceParseBackingJSONiSCSI(virStorageSourcePtr src,
*port = '\0'; *port = '\0';
} }
if (virAsprintf(&src->path, "%s/%s", target, lun) < 0) src->path = g_strdup_printf("%s/%s", target, lun);
return -1;
/* Libvirt doesn't handle inline authentication. Make the caller aware. */ /* Libvirt doesn't handle inline authentication. Make the caller aware. */
if (virJSONValueObjectGetString(json, "user") || if (virJSONValueObjectGetString(json, "user") ||
@ -4182,8 +4179,7 @@ virStorageFileGetRelativeBackingPath(virStorageSourcePtr top,
VIR_FREE(path); VIR_FREE(path);
if (virAsprintf(&path, "%s%s", tmp, next->relPath) < 0) path = g_strdup_printf("%s%s", tmp, next->relPath);
return -1;
VIR_FREE(tmp); VIR_FREE(tmp);

View File

@ -707,18 +707,15 @@ int
virDoubleToStr(char **strp, double number) virDoubleToStr(char **strp, double number)
{ {
virLocale oldlocale; virLocale oldlocale;
int rc;
if (virLocaleSetRaw(&oldlocale) < 0) if (virLocaleSetRaw(&oldlocale) < 0)
return -1; return -1;
rc = virAsprintf(strp, "%lf", number); *strp = g_strdup_printf("%lf", number);
virLocaleRevert(&oldlocale); virLocaleRevert(&oldlocale);
virLocaleFixupRadix(strp); virLocaleFixupRadix(strp);
if (rc < 0)
return -1;
return 0; return 0;
} }

View File

@ -111,7 +111,7 @@ int virStrToDouble(char const *s,
G_GNUC_WARN_UNUSED_RESULT; G_GNUC_WARN_UNUSED_RESULT;
int virDoubleToStr(char **strp, double number) int virDoubleToStr(char **strp, double number)
ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT; ATTRIBUTE_NONNULL(1);
void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1); void virSkipSpaces(const char **str) ATTRIBUTE_NONNULL(1);
void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1); void virSkipSpacesAndBackslash(const char **str) ATTRIBUTE_NONNULL(1);

View File

@ -295,8 +295,7 @@ int virSystemdCreateMachine(const char *name,
ret = -1; ret = -1;
if (virAsprintf(&creatorname, "libvirt-%s", drivername) < 0) creatorname = g_strdup_printf("libvirt-%s", drivername);
goto cleanup;
if (partition) { if (partition) {
if (!(slicename = virSystemdMakeSliceName(partition))) if (!(slicename = virSystemdMakeSliceName(partition)))

View File

@ -59,33 +59,31 @@ virTPMCreateCancelPath(const char *devpath)
const char *dev; const char *dev;
const char *prefix[] = {"misc/", "tpm/"}; const char *prefix[] = {"misc/", "tpm/"};
size_t i; size_t i;
if (!devpath) {
if (devpath) {
dev = strrchr(devpath, '/');
if (dev) {
dev++;
for (i = 0; i < G_N_ELEMENTS(prefix); i++) {
if (virAsprintf(&path, "/sys/class/%s%s/device/cancel",
prefix[i], dev) < 0)
goto cleanup;
if (virFileExists(path))
break;
VIR_FREE(path);
}
if (!path)
path = g_strdup("/dev/null");
} else {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("TPM device path %s is invalid"), devpath);
}
} else {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing TPM device path")); _("Missing TPM device path"));
return NULL;
} }
cleanup: if (!(dev = strrchr(devpath, '/'))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("TPM device path %s is invalid"), devpath);
return NULL;
}
dev++;
for (i = 0; i < G_N_ELEMENTS(prefix); i++) {
path = g_strdup_printf("/sys/class/%s%s/device/cancel", prefix[i],
dev);
if (virFileExists(path))
break;
VIR_FREE(path);
}
if (!path)
path = g_strdup("/dev/null");
return path; return path;
} }

View File

@ -176,22 +176,22 @@ virTypedParameterToString(virTypedParameterPtr param)
switch (param->type) { switch (param->type) {
case VIR_TYPED_PARAM_INT: case VIR_TYPED_PARAM_INT:
ignore_value(virAsprintf(&value, "%d", param->value.i)); value = g_strdup_printf("%d", param->value.i);
break; break;
case VIR_TYPED_PARAM_UINT: case VIR_TYPED_PARAM_UINT:
ignore_value(virAsprintf(&value, "%u", param->value.ui)); value = g_strdup_printf("%u", param->value.ui);
break; break;
case VIR_TYPED_PARAM_LLONG: case VIR_TYPED_PARAM_LLONG:
ignore_value(virAsprintf(&value, "%lld", param->value.l)); value = g_strdup_printf("%lld", param->value.l);
break; break;
case VIR_TYPED_PARAM_ULLONG: case VIR_TYPED_PARAM_ULLONG:
ignore_value(virAsprintf(&value, "%llu", param->value.ul)); value = g_strdup_printf("%llu", param->value.ul);
break; break;
case VIR_TYPED_PARAM_DOUBLE: case VIR_TYPED_PARAM_DOUBLE:
ignore_value(virAsprintf(&value, "%g", param->value.d)); value = g_strdup_printf("%g", param->value.d);
break; break;
case VIR_TYPED_PARAM_BOOLEAN: case VIR_TYPED_PARAM_BOOLEAN:
ignore_value(virAsprintf(&value, "%d", param->value.b)); value = g_strdup_printf("%d", param->value.b);
break; break;
case VIR_TYPED_PARAM_STRING: case VIR_TYPED_PARAM_STRING:
value = g_strdup(param->value.s); value = g_strdup(param->value.s);

View File

@ -234,8 +234,7 @@ virURIFormat(virURIPtr uri)
if (xmluri.server != NULL && if (xmluri.server != NULL &&
strchr(xmluri.server, ':') != NULL) { strchr(xmluri.server, ':') != NULL) {
if (virAsprintf(&tmpserver, "[%s]", xmluri.server) < 0) tmpserver = g_strdup_printf("[%s]", xmluri.server);
return NULL;
xmluri.server = tmpserver; xmluri.server = tmpserver;
} }

View File

@ -85,14 +85,11 @@ VIR_ONCE_GLOBAL_INIT(virUSB);
static int virUSBSysReadFile(const char *f_name, const char *d_name, static int virUSBSysReadFile(const char *f_name, const char *d_name,
int base, unsigned int *value) int base, unsigned int *value)
{ {
int tmp;
g_autofree char *buf = NULL; g_autofree char *buf = NULL;
g_autofree char *filename = NULL; g_autofree char *filename = NULL;
char *ignore = NULL; char *ignore = NULL;
tmp = virAsprintf(&filename, USB_SYSFS "/devices/%s/%s", d_name, f_name); filename = g_strdup_printf(USB_SYSFS "/devices/%s/%s", d_name, f_name);
if (tmp < 0)
return -1;
if (virFileReadAll(filename, 1024, &buf) < 0) if (virFileReadAll(filename, 1024, &buf) < 0)
return -1; return -1;
@ -315,7 +312,6 @@ virUSBDeviceNew(unsigned int bus,
const char *vroot) const char *vroot)
{ {
virUSBDevicePtr dev; virUSBDevicePtr dev;
int rc;
if (VIR_ALLOC(dev) < 0) if (VIR_ALLOC(dev) < 0)
return NULL; return NULL;
@ -333,16 +329,11 @@ virUSBDeviceNew(unsigned int bus,
} }
if (vroot) { if (vroot) {
rc = virAsprintf(&dev->path, "%s/%03d/%03d", dev->path = g_strdup_printf("%s/%03d/%03d",
vroot, dev->bus, dev->dev); vroot, dev->bus, dev->dev);
} else { } else {
rc = virAsprintf(&dev->path, USB_DEVFS "%03d/%03d", dev->path = g_strdup_printf(USB_DEVFS "%03d/%03d",
dev->bus, dev->dev); dev->bus, dev->dev);
}
if (rc < 0) {
virUSBDeviceFree(dev);
return NULL;
} }
/* XXX fixme. this should be product/vendor */ /* XXX fixme. this should be product/vendor */

View File

@ -744,11 +744,11 @@ static char *virGetXDGDirectory(const char *xdgenvname, const char *xdgdefdir)
char *home = NULL; char *home = NULL;
if (path && path[0]) { if (path && path[0]) {
ignore_value(virAsprintf(&ret, "%s/libvirt", path)); ret = g_strdup_printf("%s/libvirt", path);
} else { } else {
home = virGetUserDirectory(); home = virGetUserDirectory();
if (home) if (home)
ignore_value(virAsprintf(&ret, "%s/%s/libvirt", home, xdgdefdir)); ret = g_strdup_printf("%s/%s/libvirt", home, xdgdefdir);
} }
VIR_FREE(home); VIR_FREE(home);
@ -774,7 +774,7 @@ char *virGetUserRuntimeDirectory(void)
} else { } else {
char *ret; char *ret;
ignore_value(virAsprintf(&ret, "%s/libvirt", path)); ret = g_strdup_printf("%s/libvirt", path);
return ret; return ret;
} }
} }
@ -1587,9 +1587,9 @@ virGetUnprivSGIOSysfsPath(const char *path,
return NULL; return NULL;
} }
ignore_value(virAsprintf(&sysfs_path, "%s/%d:%d/queue/unpriv_sgio", sysfs_path = g_strdup_printf("%s/%d:%d/queue/unpriv_sgio",
sysfs_dir ? sysfs_dir : SYSFS_DEV_BLOCK_PATH, sysfs_dir ? sysfs_dir : SYSFS_DEV_BLOCK_PATH,
maj, min)); maj, min);
return sysfs_path; return sysfs_path;
} }
@ -1612,8 +1612,7 @@ virSetDeviceUnprivSGIO(const char *path,
goto cleanup; goto cleanup;
} }
if (virAsprintf(&val, "%d", unpriv_sgio) < 0) val = g_strdup_printf("%d", unpriv_sgio);
goto cleanup;
if ((rc = virFileWriteStr(sysfs_path, val, 0)) < 0) { if ((rc = virFileWriteStr(sysfs_path, val, 0)) < 0) {
virReportSystemError(-rc, _("failed to set %s"), sysfs_path); virReportSystemError(-rc, _("failed to set %s"), sysfs_path);
@ -1878,8 +1877,7 @@ virHostGetDRMRenderNode(void)
goto cleanup; goto cleanup;
} }
if (virAsprintf(&ret, "%s/%s", driPath, ent->d_name) < 0) ret = g_strdup_printf("%s/%s", driPath, ent->d_name);
goto cleanup;
cleanup: cleanup:
VIR_DIR_CLOSE(driDir); VIR_DIR_CLOSE(driDir);

View File

@ -52,10 +52,8 @@ virVHBAPathExists(const char *sysfs_prefix,
char *sysfs_path = NULL; char *sysfs_path = NULL;
bool ret = false; bool ret = false;
if (virAsprintf(&sysfs_path, "%s/host%d", sysfs_path = g_strdup_printf("%s/host%d",
sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH, sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH, host);
host) < 0)
return false;
if (virFileExists(sysfs_path)) if (virFileExists(sysfs_path))
ret = true; ret = true;
@ -85,24 +83,17 @@ virVHBAIsVportCapable(const char *sysfs_prefix,
char *fc_host_path = NULL; char *fc_host_path = NULL;
bool ret = false; bool ret = false;
if (virAsprintf(&fc_host_path, fc_host_path = g_strdup_printf("%s/host%d/%s",
"%s/host%d/%s", sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH, host,
sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH, "vport_create");
host,
"vport_create") < 0)
return false;
if (virAsprintf(&scsi_host_path, scsi_host_path = g_strdup_printf("%s/host%d/%s",
"%s/host%d/%s", sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_HOST_PATH, host,
sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_HOST_PATH, "vport_create");
host,
"vport_create") < 0)
goto cleanup;
if (virFileExists(fc_host_path) || virFileExists(scsi_host_path)) if (virFileExists(fc_host_path) || virFileExists(scsi_host_path))
ret = true; ret = true;
cleanup:
VIR_FREE(fc_host_path); VIR_FREE(fc_host_path);
VIR_FREE(scsi_host_path); VIR_FREE(scsi_host_path);
return ret; return ret;
@ -129,10 +120,8 @@ virVHBAGetConfig(const char *sysfs_prefix,
char *buf = NULL; char *buf = NULL;
char *result = NULL; char *result = NULL;
if (virAsprintf(&sysfs_path, "%s/host%d/%s", sysfs_path = g_strdup_printf("%s/host%d/%s",
sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH, sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH, host, entry);
host, entry) < 0)
goto cleanup;
if (!virFileExists(sysfs_path)) if (!virFileExists(sysfs_path))
goto cleanup; goto cleanup;
@ -269,15 +258,13 @@ virVHBAManageVport(const int parent_host,
goto cleanup; goto cleanup;
} }
if (virAsprintf(&operation_path, "%s/host%d/%s", operation_path = g_strdup_printf("%s/host%d/%s", SYSFS_FC_HOST_PATH,
SYSFS_FC_HOST_PATH, parent_host, operation_file) < 0) parent_host, operation_file);
goto cleanup;
if (!virFileExists(operation_path)) { if (!virFileExists(operation_path)) {
VIR_FREE(operation_path); VIR_FREE(operation_path);
if (virAsprintf(&operation_path, "%s/host%d/%s", operation_path = g_strdup_printf("%s/host%d/%s", SYSFS_SCSI_HOST_PATH,
SYSFS_SCSI_HOST_PATH, parent_host, operation_file) < 0) parent_host, operation_file);
goto cleanup;
if (!virFileExists(operation_path)) { if (!virFileExists(operation_path)) {
virReportError(VIR_ERR_OPERATION_INVALID, virReportError(VIR_ERR_OPERATION_INVALID,
@ -294,8 +281,7 @@ virVHBAManageVport(const int parent_host,
* in calling either the Add or Remove device functions. This translates * in calling either the Add or Remove device functions. This translates
* into either adding or removing a node device object and a node device * into either adding or removing a node device object and a node device
* lifecycle event for applications to consume. */ * lifecycle event for applications to consume. */
if (virAsprintf(&vport_name, "%s:%s", wwpn, wwnn) < 0) vport_name = g_strdup_printf("%s:%s", wwpn, wwnn);
goto cleanup;
if (virFileWriteStr(operation_path, vport_name, 0) == 0) if (virFileWriteStr(operation_path, vport_name, 0) == 0)
ret = 0; ret = 0;
@ -335,8 +321,7 @@ vhbaReadCompareWWN(const char *prefix,
char *p; char *p;
int ret = -1; int ret = -1;
if (virAsprintf(&path, "%s/%s/%s", prefix, d_name, f_name) < 0) path = g_strdup_printf("%s/%s/%s", prefix, d_name, f_name);
return -1;
if (!virFileExists(path)) { if (!virFileExists(path)) {
ret = 0; ret = 0;
@ -440,9 +425,8 @@ virVHBAGetHostByFabricWWN(const char *sysfs_prefix,
/* Existing vHBA's will have the same fabric_name, but won't /* Existing vHBA's will have the same fabric_name, but won't
* have the vport_create file - so we check for both */ * have the vport_create file - so we check for both */
if (virAsprintf(&vport_create_path, "%s/%s/vport_create", prefix, vport_create_path = g_strdup_printf("%s/%s/vport_create", prefix,
entry->d_name) < 0) entry->d_name);
goto cleanup;
if (!virFileExists(vport_create_path)) if (!virFileExists(vport_create_path))
continue; continue;