remote: 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 d4e5b98330
commit 2b41a017ab
4 changed files with 30 additions and 52 deletions

View File

@ -225,26 +225,23 @@ daemonUnixSocketPaths(struct daemonConfig *config,
char *rundir = NULL; char *rundir = NULL;
if (config->unix_sock_dir) { if (config->unix_sock_dir) {
if (virAsprintf(sockfile, "%s/%s-sock", *sockfile = g_strdup_printf("%s/%s-sock", config->unix_sock_dir,
config->unix_sock_dir, SOCK_PREFIX) < 0) SOCK_PREFIX);
goto cleanup;
if (privileged) { if (privileged) {
if (virAsprintf(rosockfile, "%s/%s-sock-ro", *rosockfile = g_strdup_printf("%s/%s-sock-ro",
config->unix_sock_dir, SOCK_PREFIX) < 0 || config->unix_sock_dir, SOCK_PREFIX);
virAsprintf(admsockfile, "%s/%s-admin-sock", *admsockfile = g_strdup_printf("%s/%s-admin-sock",
config->unix_sock_dir, SOCK_PREFIX) < 0) config->unix_sock_dir, SOCK_PREFIX);
goto cleanup;
} }
} else { } else {
if (privileged) { if (privileged) {
if (virAsprintf(sockfile, "%s/libvirt/%s-sock", *sockfile = g_strdup_printf("%s/libvirt/%s-sock",
RUNSTATEDIR, SOCK_PREFIX) < 0 || RUNSTATEDIR, SOCK_PREFIX);
virAsprintf(rosockfile, "%s/libvirt/%s-sock-ro", *rosockfile = g_strdup_printf("%s/libvirt/%s-sock-ro",
RUNSTATEDIR, SOCK_PREFIX) < 0 || RUNSTATEDIR, SOCK_PREFIX);
virAsprintf(admsockfile, "%s/libvirt/%s-admin-sock", *admsockfile = g_strdup_printf("%s/libvirt/%s-admin-sock",
RUNSTATEDIR, SOCK_PREFIX) < 0) RUNSTATEDIR, SOCK_PREFIX);
goto cleanup;
} else { } else {
mode_t old_umask; mode_t old_umask;
@ -258,11 +255,8 @@ daemonUnixSocketPaths(struct daemonConfig *config,
} }
umask(old_umask); umask(old_umask);
if (virAsprintf(sockfile, "%s/%s-sock", *sockfile = g_strdup_printf("%s/%s-sock", rundir, SOCK_PREFIX);
rundir, SOCK_PREFIX) < 0 || *admsockfile = g_strdup_printf("%s/%s-admin-sock", rundir, SOCK_PREFIX);
virAsprintf(admsockfile, "%s/%s-admin-sock",
rundir, SOCK_PREFIX) < 0)
goto cleanup;
} }
} }
@ -659,8 +653,7 @@ daemonSetupLogging(struct daemonConfig *config,
/* Define the default output. This is only applied if there was no setting /* Define the default output. This is only applied if there was no setting
* from either the config or the environment. * from either the config or the environment.
*/ */
if (virLogSetDefaultOutput(DAEMON_NAME, godaemon, privileged) < 0) virLogSetDefaultOutput(DAEMON_NAME, godaemon, privileged);
return -1;
if (virLogGetNbOutputs() == 0) if (virLogGetNbOutputs() == 0)
virLogSetOutputs(virLogGetDefaultOutput()); virLogSetOutputs(virLogGetDefaultOutput());

View File

@ -84,10 +84,7 @@ daemonConfigFilePath(bool privileged, char **configfile)
if (!(configdir = virGetUserConfigDirectory())) if (!(configdir = virGetUserConfigDirectory()))
goto error; goto error;
if (virAsprintf(configfile, "%s/%s.conf", configdir, DAEMON_NAME) < 0) { *configfile = g_strdup_printf("%s/%s.conf", configdir, DAEMON_NAME);
VIR_FREE(configdir);
goto error;
}
VIR_FREE(configdir); VIR_FREE(configdir);
} }

View File

@ -1974,8 +1974,7 @@ remoteDispatchProbeURI(bool readonly,
g_autofree char *daemonname = NULL; g_autofree char *daemonname = NULL;
g_autofree char *daemonpath = NULL; g_autofree char *daemonpath = NULL;
if (virAsprintf(&daemonname, "virt%sd", drivers[i]) < 0) daemonname = g_strdup_printf("virt%sd", drivers[i]);
return -1;
if (!(daemonpath = virFileFindResource(daemonname, "src", SBINDIR))) if (!(daemonpath = virFileFindResource(daemonname, "src", SBINDIR)))
return -1; return -1;
@ -1985,8 +1984,7 @@ remoteDispatchProbeURI(bool readonly,
continue; continue;
} }
if (virAsprintf(probeduri, "%s:///session", drivers[i]) < 0) *probeduri = g_strdup_printf("%s:///session", drivers[i]);
return -1;
VIR_DEBUG("Probed URI %s via daemon %s", *probeduri, daemonpath); VIR_DEBUG("Probed URI %s via daemon %s", *probeduri, daemonpath);
return 0; return 0;
@ -2019,18 +2017,15 @@ remoteDispatchProbeURI(bool readonly,
for (i = 0; i < (ssize_t) G_N_ELEMENTS(drivers) && !*probeduri; i++) { for (i = 0; i < (ssize_t) G_N_ELEMENTS(drivers) && !*probeduri; i++) {
g_autofree char *sockname = NULL; g_autofree char *sockname = NULL;
if (virAsprintf(&sockname, "%s/libvirt/virt%sd-%s", sockname = g_strdup_printf("%s/libvirt/virt%sd-%s", RUNSTATEDIR,
RUNSTATEDIR, drivers[i], drivers[i], readonly ? "sock-ro" : "sock");
readonly ? "sock-ro" : "sock") < 0)
return -1;
if (!virFileExists(sockname)) { if (!virFileExists(sockname)) {
VIR_DEBUG("Missing sock %s for driver %s", sockname, drivers[i]); VIR_DEBUG("Missing sock %s for driver %s", sockname, drivers[i]);
continue; continue;
} }
if (virAsprintf(probeduri, "%s:///system", drivers[i]) < 0) *probeduri = g_strdup_printf("%s:///system", drivers[i]);
return -1;
VIR_DEBUG("Probed URI %s via sock %s", *probeduri, sockname); VIR_DEBUG("Probed URI %s via sock %s", *probeduri, sockname);
return 0; return 0;
@ -3644,9 +3639,8 @@ remoteDispatchAuthList(virNetServerPtr server,
virResetLastError(); virResetLastError();
} else if (callerUid == 0) { } else if (callerUid == 0) {
char *ident; char *ident;
if (virAsprintf(&ident, "pid:%lld,uid:%d", ident = g_strdup_printf("pid:%lld,uid:%d", (long long)callerPid,
(long long) callerPid, (int) callerUid) < 0) (int)callerUid);
goto cleanup;
VIR_INFO("Bypass polkit auth for privileged client %s", ident); VIR_INFO("Bypass polkit auth for privileged client %s", ident);
virNetServerSetClientAuthenticated(server, client); virNetServerSetClientAuthenticated(server, client);
auth = VIR_NET_SERVER_SERVICE_AUTH_NONE; auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;

View File

@ -781,9 +781,7 @@ remoteGetUNIXSocketHelper(remoteDriverTransport transport,
if (!(userdir = virGetUserRuntimeDirectory())) if (!(userdir = virGetUserRuntimeDirectory()))
return NULL; return NULL;
if (virAsprintf(&sockname, "%s/%s-sock", sockname = g_strdup_printf("%s/%s-sock", userdir, sock_prefix);
userdir, sock_prefix) < 0)
return NULL;
} else { } else {
/* Intentionally do *NOT* use RUNSTATEDIR here. We might /* Intentionally do *NOT* use RUNSTATEDIR here. We might
* be connecting to a remote machine, and cannot assume * be connecting to a remote machine, and cannot assume
@ -791,11 +789,9 @@ remoteGetUNIXSocketHelper(remoteDriverTransport transport,
* any machine with /run will have a /var/run symlink. * any machine with /run will have a /var/run symlink.
* The portable option is to thus use $LOCALSTATEDIR/run * The portable option is to thus use $LOCALSTATEDIR/run
*/ */
if (virAsprintf(&sockname, "%s/run/libvirt/%s-%s", sockname = g_strdup_printf("%s/run/libvirt/%s-%s", LOCALSTATEDIR,
LOCALSTATEDIR, sock_prefix, sock_prefix,
flags & VIR_DRV_OPEN_REMOTE_RO ? flags & VIR_DRV_OPEN_REMOTE_RO ? "sock-ro" : "sock");
"sock-ro" : "sock") < 0)
return NULL;
} }
VIR_DEBUG("Built UNIX sockname %s for transport %s prefix %s flags=0x%x", VIR_DEBUG("Built UNIX sockname %s for transport %s prefix %s flags=0x%x",
@ -818,9 +814,8 @@ remoteGetUNIXSocket(remoteDriverTransport transport,
g_autofree char *direct_sock_name = NULL; g_autofree char *direct_sock_name = NULL;
g_autofree char *legacy_sock_name = NULL; g_autofree char *legacy_sock_name = NULL;
if (driver && if (driver)
virAsprintf(&direct_daemon, "virt%sd", driver) < 0) direct_daemon = g_strdup_printf("virt%sd", driver);
return NULL;
legacy_daemon = g_strdup("libvirtd"); legacy_daemon = g_strdup("libvirtd");
@ -1003,8 +998,7 @@ doRemoteOpen(virConnectPtr conn,
/* Remote server defaults to "localhost" if not specified. */ /* Remote server defaults to "localhost" if not specified. */
if (conn->uri && conn->uri->port != 0) { if (conn->uri && conn->uri->port != 0) {
if (virAsprintf(&port, "%d", conn->uri->port) < 0) port = g_strdup_printf("%d", conn->uri->port);
goto failed;
} else if (transport == REMOTE_DRIVER_TRANSPORT_TLS) { } else if (transport == REMOTE_DRIVER_TRANSPORT_TLS) {
port = g_strdup(LIBVIRTD_TLS_PORT); port = g_strdup(LIBVIRTD_TLS_PORT);
} else if (transport == REMOTE_DRIVER_TRANSPORT_TCP) { } else if (transport == REMOTE_DRIVER_TRANSPORT_TCP) {