char **server_duid)
{
VIR_AUTOFREE(char *) lease_entries = NULL;
- virJSONValuePtr leases_array = NULL;
+ VIR_AUTOPTR(virJSONValue) leases_array = NULL;
long long expirytime;
int custom_lease_file_len = 0;
virJSONValuePtr lease_tmp = NULL;
const char *ip_tmp = NULL;
const char *server_duid_tmp = NULL;
size_t i;
- int ret = -1;
/* Read entire contents */
if ((custom_lease_file_len = virFileReadAll(custom_lease_file,
VIR_NETWORK_DHCP_LEASE_FILE_SIZE_MAX,
&lease_entries)) < 0) {
- goto cleanup;
+ return -1;
}
/* Check for previous leases */
- if (custom_lease_file_len == 0) {
- ret = 0;
- goto cleanup;
- }
+ if (custom_lease_file_len == 0)
+ return 0;
if (!(leases_array = virJSONValueFromString(lease_entries))) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("invalid json in file: %s, rewriting it"),
custom_lease_file);
- ret = 0;
- goto cleanup;
+ return 0;
}
if (!virJSONValueIsArray(leases_array)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("couldn't fetch array of leases"));
- goto cleanup;
+ return -1;
}
i = 0;
if (!(lease_tmp = virJSONValueArrayGet(leases_array, i))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse json"));
- goto cleanup;
+ return -1;
}
if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address")) ||
(virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time", &expirytime) < 0)) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse json"));
- goto cleanup;
+ return -1;
}
/* Check whether lease has to be included or not */
/* Control reaches here when the 'action' is not for an
* ipv6 lease or, for some weird reason the env var
* DNSMASQ_SERVER_DUID wasn't set*/
- goto cleanup;
+ return -1;
}
} else {
/* Inject server-duid into those ipv6 leases which
* didn't have it previously, for example, those
* created by leaseshelper from libvirt 1.2.6 */
if (virJSONValueObjectAppendString(lease_tmp, "server-duid", *server_duid) < 0)
- goto cleanup;
+ return -1;
}
}
if (virJSONValueArrayAppend(leases_array_new, lease_tmp) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to create json"));
- goto cleanup;
+ return -1;
}
ignore_value(virJSONValueArraySteal(leases_array, i));
}
- ret = 0;
-
- cleanup:
- virJSONValueFree(leases_array);
- return ret;
+ return 0;
}
virJSONValuePtr lease_tmp = NULL;
const char *ip_tmp = NULL;
long long expirytime = 0;
- int ret = -1;
size_t i;
/* Man page of dnsmasq says: the script (helper program, in our case)
if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse json"));
- goto cleanup;
+ return -1;
}
if (!strchr(ip_tmp, ':')) {
if (virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time",
if (!(ip_tmp = virJSONValueObjectGetString(lease_tmp, "ip-address"))) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to parse json"));
- goto cleanup;
+ return -1;
}
if (strchr(ip_tmp, ':')) {
if (virJSONValueObjectGetNumberLong(lease_tmp, "expiry-time",
}
}
- ret = 0;
-
- cleanup:
- return ret;
+ return 0;
}
const char *iaid,
const char *server_duid)
{
- virJSONValuePtr lease_new = NULL;
+ VIR_AUTOPTR(virJSONValue) lease_new = NULL;
const char *exptime_tmp = virGetEnvAllowSUID("DNSMASQ_LEASE_EXPIRES");
long long expirytime = 0;
VIR_AUTOFREE(char *) exptime = NULL;
- int ret = -1;
/* In case hostname is still unknown, use the last known one */
if (!hostname)
hostname = virGetEnvAllowSUID("DNSMASQ_OLD_HOSTNAME");
- if (!mac) {
- ret = 0;
- goto cleanup;
- }
+ if (!mac)
+ return 0;
if (exptime_tmp) {
if (VIR_STRDUP(exptime, exptime_tmp) < 0)
- goto cleanup;
+ return -1;
/* Removed extraneous trailing space in DNSMASQ_LEASE_EXPIRES
* (dnsmasq < 2.52) */
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to convert lease expiry time to long long: %s"),
NULLSTR(exptime));
- goto cleanup;
+ return -1;
}
/* Create new lease */
if (!(lease_new = virJSONValueNewObject())) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to create json"));
- goto cleanup;
+ return -1;
}
if (iaid && virJSONValueObjectAppendString(lease_new, "iaid", iaid) < 0)
- goto cleanup;
+ return -1;
if (ip && virJSONValueObjectAppendString(lease_new, "ip-address", ip) < 0)
- goto cleanup;
+ return -1;
if (mac && virJSONValueObjectAppendString(lease_new, "mac-address", mac) < 0)
- goto cleanup;
+ return -1;
if (hostname && virJSONValueObjectAppendString(lease_new, "hostname", hostname) < 0)
- goto cleanup;
+ return -1;
if (clientid && virJSONValueObjectAppendString(lease_new, "client-id", clientid) < 0)
- goto cleanup;
+ return -1;
if (server_duid && virJSONValueObjectAppendString(lease_new, "server-duid", server_duid) < 0)
- goto cleanup;
+ return -1;
if (expirytime && virJSONValueObjectAppendNumberLong(lease_new, "expiry-time", expirytime) < 0)
- goto cleanup;
+ return -1;
- ret = 0;
*lease_ret = lease_new;
lease_new = NULL;
- cleanup:
- virJSONValueFree(lease_new);
- return ret;
+ return 0;
}