Adapt to VIR_STRDUP and VIR_STRNDUP in src/locking/*

This commit is contained in:
Michal Privoznik 2013-05-03 14:43:12 +02:00
parent 08152a6982
commit f75ed996e7
6 changed files with 46 additions and 63 deletions

View File

@ -370,8 +370,8 @@ virLockDaemonPidFilePath(bool privileged,
char **pidfile) char **pidfile)
{ {
if (privileged) { if (privileged) {
if (!(*pidfile = strdup(LOCALSTATEDIR "/run/virtlockd.pid"))) if (VIR_STRDUP(*pidfile, LOCALSTATEDIR "/run/virtlockd.pid") < 0)
goto no_memory; goto error;
} else { } else {
char *rundir = NULL; char *rundir = NULL;
mode_t old_umask; mode_t old_umask;
@ -388,7 +388,8 @@ virLockDaemonPidFilePath(bool privileged,
if (virAsprintf(pidfile, "%s/virtlockd.pid", rundir) < 0) { if (virAsprintf(pidfile, "%s/virtlockd.pid", rundir) < 0) {
VIR_FREE(rundir); VIR_FREE(rundir);
goto no_memory; virReportOOMError();
goto error;
} }
VIR_FREE(rundir); VIR_FREE(rundir);
@ -396,8 +397,6 @@ virLockDaemonPidFilePath(bool privileged,
return 0; return 0;
no_memory:
virReportOOMError();
error: error:
return -1; return -1;
} }
@ -408,8 +407,8 @@ virLockDaemonUnixSocketPaths(bool privileged,
char **sockfile) char **sockfile)
{ {
if (privileged) { if (privileged) {
if (!(*sockfile = strdup(LOCALSTATEDIR "/run/libvirt/virtlockd-sock"))) if (VIR_STRDUP(*sockfile, LOCALSTATEDIR "/run/libvirt/virtlockd-sock") < 0)
goto no_memory; goto error;
} else { } else {
char *rundir = NULL; char *rundir = NULL;
mode_t old_umask; mode_t old_umask;
@ -426,15 +425,14 @@ virLockDaemonUnixSocketPaths(bool privileged,
if (virAsprintf(sockfile, "%s/virtlockd-sock", rundir) < 0) { if (virAsprintf(sockfile, "%s/virtlockd-sock", rundir) < 0) {
VIR_FREE(rundir); VIR_FREE(rundir);
goto no_memory; virReportOOMError();
goto error;
} }
VIR_FREE(rundir); VIR_FREE(rundir);
} }
return 0; return 0;
no_memory:
virReportOOMError();
error: error:
return -1; return -1;
} }
@ -869,10 +867,8 @@ virLockDaemonClientNewPostExecRestart(virNetServerClientPtr client,
_("Missing ownerName data in JSON document")); _("Missing ownerName data in JSON document"));
goto error; goto error;
} }
if (!(priv->ownerName = strdup(ownerName))) { if (VIR_STRDUP(priv->ownerName, ownerName) < 0)
virReportOOMError();
goto error; goto error;
}
if (!(ownerUUID = virJSONValueObjectGetString(object, "ownerUUID"))) { if (!(ownerUUID = virJSONValueObjectGetString(object, "ownerUUID"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing ownerUUID data in JSON document")); _("Missing ownerUUID data in JSON document"));
@ -1223,14 +1219,18 @@ int main(int argc, char **argv) {
case 'p': case 'p':
VIR_FREE(pid_file); VIR_FREE(pid_file);
if (!(pid_file = strdup(optarg))) if (VIR_STRDUP_QUIET(pid_file, optarg) < 0) {
VIR_ERROR(_("Can't allocate memory"));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
break; break;
case 'f': case 'f':
VIR_FREE(remote_config_file); VIR_FREE(remote_config_file);
if (!(remote_config_file = strdup(optarg))) if (VIR_STRDUP_QUIET(remote_config_file, optarg) < 0) {
VIR_ERROR(_("Can't allocate memory"));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
break; break;
case OPT_VERSION: case OPT_VERSION:
@ -1314,8 +1314,8 @@ int main(int argc, char **argv) {
/* Ensure the rundir exists (on tmpfs on some systems) */ /* Ensure the rundir exists (on tmpfs on some systems) */
if (privileged) { if (privileged) {
if (!(run_dir = strdup(LOCALSTATEDIR "/run/libvirt"))) { if (VIR_STRDUP_QUIET(run_dir, LOCALSTATEDIR "/run/libvirt") < 0) {
virReportOOMError(); VIR_ERROR(_("Can't allocate memory"));
goto cleanup; goto cleanup;
} }
} else { } else {

View File

@ -53,8 +53,8 @@ checkType(virConfValuePtr p, const char *filename,
} }
/* If there is no config data for the key, #var_name, then do nothing. /* If there is no config data for the key, #var_name, then do nothing.
If there is valid data of type VIR_CONF_STRING, and strdup succeeds, If there is valid data of type VIR_CONF_STRING, and VIR_STRDUP succeeds,
store the result in var_name. Otherwise, (i.e. invalid type, or strdup store the result in var_name. Otherwise, (i.e. invalid type, or VIR_STRDUP
failure), give a diagnostic and "goto" the cleanup-and-fail label. */ failure), give a diagnostic and "goto" the cleanup-and-fail label. */
#define GET_CONF_STR(conf, filename, var_name) \ #define GET_CONF_STR(conf, filename, var_name) \
do { \ do { \
@ -63,10 +63,8 @@ checkType(virConfValuePtr p, const char *filename,
if (checkType(p, filename, #var_name, VIR_CONF_STRING) < 0) \ if (checkType(p, filename, #var_name, VIR_CONF_STRING) < 0) \
goto error; \ goto error; \
VIR_FREE(data->var_name); \ VIR_FREE(data->var_name); \
if (!(data->var_name = strdup(p->str))) { \ if (VIR_STRDUP(data->var_name, p->str) < 0) \
virReportOOMError(); \
goto error; \ goto error; \
} \
} \ } \
} while (0) } while (0)
@ -85,8 +83,8 @@ int
virLockDaemonConfigFilePath(bool privileged, char **configfile) virLockDaemonConfigFilePath(bool privileged, char **configfile)
{ {
if (privileged) { if (privileged) {
if (!(*configfile = strdup(SYSCONFDIR "/libvirt/virtlockd.conf"))) if (VIR_STRDUP(*configfile, SYSCONFDIR "/libvirt/virtlockd.conf") < 0)
goto no_memory; goto error;
} else { } else {
char *configdir = NULL; char *configdir = NULL;

View File

@ -25,7 +25,7 @@
#include "rpc/virnetserver.h" #include "rpc/virnetserver.h"
#include "rpc/virnetserverclient.h" #include "rpc/virnetserverclient.h"
#include "virlog.h" #include "virlog.h"
#include "virstring.h"
#include "lock_daemon.h" #include "lock_daemon.h"
#include "lock_protocol.h" #include "lock_protocol.h"
#include "lock_daemon_dispatch_stubs.h" #include "lock_daemon_dispatch_stubs.h"
@ -275,10 +275,8 @@ virLockSpaceProtocolDispatchRegister(virNetServerPtr server ATTRIBUTE_UNUSED,
goto cleanup; goto cleanup;
} }
if (!(priv->ownerName = strdup(args->owner.name))) { if (VIR_STRDUP(priv->ownerName, args->owner.name) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
memcpy(priv->ownerUUID, args->owner.uuid, VIR_UUID_BUFLEN); memcpy(priv->ownerUUID, args->owner.uuid, VIR_UUID_BUFLEN);
priv->ownerId = args->owner.id; priv->ownerId = args->owner.id;
priv->ownerPid = args->owner.pid; priv->ownerPid = args->owner.pid;

View File

@ -129,8 +129,7 @@ static int virLockManagerLockDaemonLoadConfig(const char *configFile)
CHECK_TYPE("file_lockspace_dir", VIR_CONF_STRING); CHECK_TYPE("file_lockspace_dir", VIR_CONF_STRING);
if (p && p->str) { if (p && p->str) {
VIR_FREE(driver->fileLockSpaceDir); VIR_FREE(driver->fileLockSpaceDir);
if (!(driver->fileLockSpaceDir = strdup(p->str))) { if (VIR_STRDUP(driver->fileLockSpaceDir, p->str) < 0) {
virReportOOMError();
virConfFree(conf); virConfFree(conf);
return -1; return -1;
} }
@ -140,8 +139,7 @@ static int virLockManagerLockDaemonLoadConfig(const char *configFile)
CHECK_TYPE("lvm_lockspace_dir", VIR_CONF_STRING); CHECK_TYPE("lvm_lockspace_dir", VIR_CONF_STRING);
if (p && p->str) { if (p && p->str) {
VIR_FREE(driver->lvmLockSpaceDir); VIR_FREE(driver->lvmLockSpaceDir);
if (!(driver->lvmLockSpaceDir = strdup(p->str))) { if (VIR_STRDUP(driver->lvmLockSpaceDir, p->str) < 0) {
virReportOOMError();
virConfFree(conf); virConfFree(conf);
return -1; return -1;
} }
@ -151,8 +149,7 @@ static int virLockManagerLockDaemonLoadConfig(const char *configFile)
CHECK_TYPE("scsi_lockspace_dir", VIR_CONF_STRING); CHECK_TYPE("scsi_lockspace_dir", VIR_CONF_STRING);
if (p && p->str) { if (p && p->str) {
VIR_FREE(driver->scsiLockSpaceDir); VIR_FREE(driver->scsiLockSpaceDir);
if (!(driver->scsiLockSpaceDir = strdup(p->str))) { if (VIR_STRDUP(driver->scsiLockSpaceDir, p->str) < 0) {
virReportOOMError();
virConfFree(conf); virConfFree(conf);
return -1; return -1;
} }
@ -174,10 +171,8 @@ static char *virLockManagerLockDaemonPath(bool privileged)
{ {
char *path; char *path;
if (privileged) { if (privileged) {
if (!(path = strdup(LOCALSTATEDIR "/run/libvirt/virtlockd-sock"))) { if (VIR_STRDUP(path, LOCALSTATEDIR "/run/libvirt/virtlockd-sock") < 0)
virReportOOMError();
return NULL; return NULL;
}
} else { } else {
char *rundir = NULL; char *rundir = NULL;
@ -468,10 +463,8 @@ static int virLockManagerLockDaemonNew(virLockManagerPtr lock,
if (STREQ(params[i].key, "uuid")) { if (STREQ(params[i].key, "uuid")) {
memcpy(priv->uuid, params[i].value.uuid, VIR_UUID_BUFLEN); memcpy(priv->uuid, params[i].value.uuid, VIR_UUID_BUFLEN);
} else if (STREQ(params[i].key, "name")) { } else if (STREQ(params[i].key, "name")) {
if (!(priv->name = strdup(params[i].value.str))) { if (VIR_STRDUP(priv->name, params[i].value.str) < 0)
virReportOOMError();
return -1; return -1;
}
} else if (STREQ(params[i].key, "id")) { } else if (STREQ(params[i].key, "id")) {
priv->id = params[i].value.i; priv->id = params[i].value.i;
} else if (STREQ(params[i].key, "pid")) { } else if (STREQ(params[i].key, "pid")) {
@ -590,8 +583,8 @@ static int virLockManagerLockDaemonAddResource(virLockManagerPtr lock,
if (newName) { if (newName) {
VIR_DEBUG("Got an LVM UUID %s for %s", newName, name); VIR_DEBUG("Got an LVM UUID %s for %s", newName, name);
if (!(newLockspace = strdup(driver->lvmLockSpaceDir))) if (VIR_STRDUP(newLockspace, driver->lvmLockSpaceDir) < 0)
goto no_memory; goto error;
autoCreate = true; autoCreate = true;
break; break;
} }
@ -607,8 +600,8 @@ static int virLockManagerLockDaemonAddResource(virLockManagerPtr lock,
if (newName) { if (newName) {
VIR_DEBUG("Got an SCSI ID %s for %s", newName, name); VIR_DEBUG("Got an SCSI ID %s for %s", newName, name);
if (!(newLockspace = strdup(driver->scsiLockSpaceDir))) if (VIR_STRDUP(newLockspace, driver->scsiLockSpaceDir) < 0)
goto no_memory; goto error;
autoCreate = true; autoCreate = true;
break; break;
} }
@ -617,17 +610,17 @@ static int virLockManagerLockDaemonAddResource(virLockManagerPtr lock,
} }
if (driver->fileLockSpaceDir) { if (driver->fileLockSpaceDir) {
if (!(newLockspace = strdup(driver->fileLockSpaceDir))) if (VIR_STRDUP(newLockspace, driver->fileLockSpaceDir) < 0)
goto no_memory; goto error;
if (!(newName = virLockManagerLockDaemonDiskLeaseName(name))) if (!(newName = virLockManagerLockDaemonDiskLeaseName(name)))
goto no_memory; goto no_memory;
autoCreate = true; autoCreate = true;
VIR_DEBUG("Using indirect lease %s for %s", newName, name); VIR_DEBUG("Using indirect lease %s for %s", newName, name);
} else { } else {
if (!(newLockspace = strdup(""))) if (VIR_STRDUP(newLockspace, "") < 0)
goto no_memory; goto error;
if (!(newName = strdup(name))) if (VIR_STRDUP(newName, name) < 0)
goto no_memory; goto error;
VIR_DEBUG("Using direct lease for %s", name); VIR_DEBUG("Using direct lease for %s", name);
} }
@ -664,8 +657,8 @@ static int virLockManagerLockDaemonAddResource(virLockManagerPtr lock,
virReportOOMError(); virReportOOMError();
return -1; return -1;
} }
if (!(newName = strdup(name))) if (VIR_STRDUP(newName, name) < 0)
goto no_memory; goto error;
} break; } break;
default: default:

View File

@ -129,8 +129,7 @@ static int virLockManagerSanlockLoadConfig(const char *configFile)
CHECK_TYPE("disk_lease_dir", VIR_CONF_STRING); CHECK_TYPE("disk_lease_dir", VIR_CONF_STRING);
if (p && p->str) { if (p && p->str) {
VIR_FREE(driver->autoDiskLeasePath); VIR_FREE(driver->autoDiskLeasePath);
if (!(driver->autoDiskLeasePath = strdup(p->str))) { if (VIR_STRDUP(driver->autoDiskLeasePath, p->str) < 0) {
virReportOOMError();
virConfFree(conf); virConfFree(conf);
return -1; return -1;
} }
@ -150,8 +149,7 @@ static int virLockManagerSanlockLoadConfig(const char *configFile)
p = virConfGetValue(conf, "user"); p = virConfGetValue(conf, "user");
CHECK_TYPE("user", VIR_CONF_STRING); CHECK_TYPE("user", VIR_CONF_STRING);
if (p) { if (p) {
if (!(tmp = strdup(p->str))) { if (VIR_STRDUP(tmp, p->str) < 0) {
virReportOOMError();
virConfFree(conf); virConfFree(conf);
return -1; return -1;
} }
@ -167,8 +165,7 @@ static int virLockManagerSanlockLoadConfig(const char *configFile)
p = virConfGetValue(conf, "group"); p = virConfGetValue(conf, "group");
CHECK_TYPE("group", VIR_CONF_STRING); CHECK_TYPE("group", VIR_CONF_STRING);
if (p) { if (p) {
if (!(tmp = strdup(p->str))) { if (VIR_STRDUP(tmp, p->str) < 0) {
virReportOOMError();
virConfFree(conf); virConfFree(conf);
return -1; return -1;
} }
@ -408,9 +405,8 @@ static int virLockManagerSanlockInit(unsigned int version,
driver->autoDiskLease = false; driver->autoDiskLease = false;
driver->user = (uid_t) -1; driver->user = (uid_t) -1;
driver->group = (gid_t) -1; driver->group = (gid_t) -1;
if (!(driver->autoDiskLeasePath = strdup(LOCALSTATEDIR "/lib/libvirt/sanlock"))) { if (VIR_STRDUP(driver->autoDiskLeasePath, LOCALSTATEDIR "/lib/libvirt/sanlock") < 0) {
VIR_FREE(driver); VIR_FREE(driver);
virReportOOMError();
goto error; goto error;
} }

View File

@ -194,10 +194,8 @@ virLockManagerPluginPtr virLockManagerPluginNew(const char *name,
plugin->driver = driver; plugin->driver = driver;
plugin->handle = handle; plugin->handle = handle;
plugin->refs = 1; plugin->refs = 1;
if (!(plugin->name = strdup(name))) { if (VIR_STRDUP(plugin->name, name) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
VIR_FREE(configFile); VIR_FREE(configFile);
VIR_FREE(modfile); VIR_FREE(modfile);