virLogDaemonPostExecRestart: Refactor cleanup

Move the unlinking of the state file right after reading it so that we
can get rid of the cleanup section.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2021-12-03 09:35:55 +01:00
parent 8db183f6c7
commit 8fe1d5d4c9

View File

@ -436,61 +436,56 @@ virLogDaemonPostExecRestart(const char *state_file,
{ {
const char *gotmagic; const char *gotmagic;
g_autofree char *wantmagic = NULL; g_autofree char *wantmagic = NULL;
int ret = -1;
g_autofree char *state = NULL; g_autofree char *state = NULL;
g_autoptr(virJSONValue) object = NULL; g_autoptr(virJSONValue) object = NULL;
int rc;
VIR_DEBUG("Running post-restart exec"); VIR_DEBUG("Running post-restart exec");
if (!virFileExists(state_file)) { if (!virFileExists(state_file)) {
VIR_DEBUG("No restart state file %s present", VIR_DEBUG("No restart state file %s present",
state_file); state_file);
ret = 0; return 0;
goto cleanup;
} }
if (virFileReadAll(state_file, rc = virFileReadAll(state_file, 1024 * 1024 * 10, /* 10 MB */ &state);
1024 * 1024 * 10, /* 10 MB */ unlink(state_file);
&state) < 0)
goto cleanup; if (rc < 0)
return -1;
VIR_DEBUG("Loading state %s", state); VIR_DEBUG("Loading state %s", state);
if (!(object = virJSONValueFromString(state))) if (!(object = virJSONValueFromString(state)))
goto cleanup; return -1;
gotmagic = virJSONValueObjectGetString(object, "magic"); gotmagic = virJSONValueObjectGetString(object, "magic");
if (!gotmagic) { if (!gotmagic) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Missing magic data in JSON document")); _("Missing magic data in JSON document"));
goto cleanup; return -1;
} }
if (!(wantmagic = virLogDaemonGetExecRestartMagic())) if (!(wantmagic = virLogDaemonGetExecRestartMagic()))
goto cleanup; return -1;
if (STRNEQ(gotmagic, wantmagic)) { if (STRNEQ(gotmagic, wantmagic)) {
VIR_WARN("Found restart exec file with old magic %s vs wanted %s", VIR_WARN("Found restart exec file with old magic %s vs wanted %s",
gotmagic, wantmagic); gotmagic, wantmagic);
ret = 0; return 0;
goto cleanup;
} }
/* Re-claim PID file now as we will not be daemonizing */ /* Re-claim PID file now as we will not be daemonizing */
if (pid_file && if (pid_file &&
(*pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0) (*pid_file_fd = virPidFileAcquirePath(pid_file, false, getpid())) < 0)
goto cleanup; return -1;
if (!(logDaemon = virLogDaemonNewPostExecRestart(object, if (!(logDaemon = virLogDaemonNewPostExecRestart(object,
privileged, privileged,
config))) config)))
goto cleanup; return -1;
ret = 1; return 1;
cleanup:
unlink(state_file);
return ret;
} }