Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cf-agent/cf-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ static const struct option OPTIONS[] =
{"skip-bootstrap-service-start", no_argument, 0, 0 },
{"skip-db-check", optional_argument, 0, 0 },
{"simulate", required_argument, 0, 0},
{"simulate-json", required_argument, 0, 0},
{NULL, 0, 0, '\0'}
};

Expand Down Expand Up @@ -221,6 +222,7 @@ static const char *const HINTS[] =
"Do not start CFEngine services as part of the bootstrap process",
"Do not run database integrity checks and repairs at startup",
"Run in simulate mode, either 'manifest', 'manifest-full' or 'diff'",
"Write the change set of a --simulate run to the given file as JSON",
NULL
};

Expand Down Expand Up @@ -338,6 +340,23 @@ int main(int argc, char *argv[])

Nova_NoteAgentExecutionPerformance(config->input_file, start);

/* Before GenericAgentFinalize(), which deinitializes the crypto library
* needed for the file digests. */
if (config->agent_specific.agent.simulate_json_file != NULL)
{
const Class *failsafe_class =
EvalContextClassGet(ctx, NULL, "failsafe_fallback");
const bool failsafe_fallback =
((failsafe_class != NULL) && !failsafe_class->is_soft);
if (!WriteChangesJson(config->agent_specific.agent.simulate_json_file,
failsafe_fallback, EvalAborted(ctx)) &&
(ret == 0))
{
ret = EXIT_FAILURE;
}
free(config->agent_specific.agent.simulate_json_file);
}

GenericAgentFinalize(ctx, config);

StringSetDestroy(SINGLE_COPY_CACHE);
Expand Down Expand Up @@ -748,6 +767,18 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
DoCleanupAndExit(EXIT_FAILURE);
}
}
else if (StringEqual(option_name, "simulate-json"))
{
if (!IsAbsoluteFileName(optarg))
{
Log(LOG_LEVEL_ERR,
"Invalid argument for --simulate-json, an absolute path is required, not '%s'",
optarg);
DoCleanupAndExit(EXIT_FAILURE);
}
free(config->agent_specific.agent.simulate_json_file);
config->agent_specific.agent.simulate_json_file = xstrdup(optarg);
}
break;
}
default:
Expand Down Expand Up @@ -775,6 +806,14 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
DoCleanupAndExit(EXIT_FAILURE);
}

if ((config->agent_specific.agent.simulate_json_file != NULL) &&
!ChrootChanges())
{
Log(LOG_LEVEL_ERR,
"Option --simulate-json can only be used together with --simulate");
DoCleanupAndExit(EXIT_FAILURE);
}

FreeFixedStringArray(argc_new, argv_new);

return config;
Expand Down
Loading
Loading