Skip to content

ENT-9980: Properly handle promise locking with edit line #5815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions cf-agent/files_edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <files_editxml.h>
#include <item_lib.h>
#include <policy.h>
#include <cf3.defs.h>

/*****************************************************************************/

Expand Down Expand Up @@ -102,9 +103,9 @@ EditContext *NewEditContext(char *filename, const Attributes *a)
/*****************************************************************************/

void FinishEditContext(EvalContext *ctx, EditContext *ec, const Attributes *a, const Promise *pp,
PromiseResult *result)
PromiseResult *result, bool save_file)
{
if ((*result != PROMISE_RESULT_NOOP) && (*result != PROMISE_RESULT_CHANGE))
if (!save_file || ((*result != PROMISE_RESULT_NOOP) && (*result != PROMISE_RESULT_CHANGE)))
{
// Failure or skipped. Don't update the file.
goto end;
Expand Down
2 changes: 1 addition & 1 deletion cf-agent/files_edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef struct
EditContext *NewEditContext(char *filename, const Attributes *a);
void FinishEditContext(EvalContext *ctx, EditContext *ec,
const Attributes *a, const Promise *pp,
PromiseResult *result);
PromiseResult *result, bool save_file);

#ifdef HAVE_LIBXML2
bool LoadFileAsXmlDoc(xmlDocPtr *doc, const char *file, EditDefaults ed, bool only_checks);
Expand Down
16 changes: 10 additions & 6 deletions cf-agent/verify_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include <known_dirs.h>
#include <evalfunction.h>
#include <changes_chroot.h> /* PrepareChangesChroot(), RecordFileChangedInChroot() */
#include <cf3.defs.h>

static PromiseResult FindFilePromiserObjects(EvalContext *ctx, const Promise *pp);
static PromiseResult VerifyFilePromise(EvalContext *ctx, char *path, const Promise *pp);
Expand Down Expand Up @@ -728,7 +729,8 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,
const Rlist *bundle_args,
const Attributes *attr,
EditContext *edcontext,
bool file_exists)
bool file_exists,
bool *save_file)
{
assert(edcontext != NULL);
assert(attr != NULL);
Expand All @@ -741,7 +743,7 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,
{
if (!file_exists && !CfCreateFile(ctx, edcontext->changes_filename,
pp, attr, &result))
{
{
RecordFailure(ctx, pp, attr,
"Failed to create file '%s' for rendering cfengine template '%s'",
edcontext->filename, attr->edit_template);
Expand All @@ -753,7 +755,7 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,
EvalContextStackPushBundleFrame(ctx, bp, bundle_args, a.edits.inherit);
BundleResolve(ctx, bp);

ScheduleEditLineOperations(ctx, bp, &a, pp, edcontext);
*save_file = ScheduleEditLineOperations(ctx, bp, &a, pp, edcontext);

EvalContextStackPopFrame(ctx);

Expand Down Expand Up @@ -853,7 +855,7 @@ static PromiseResult RenderTemplateMustache(EvalContext *ctx,
if (!file_exists && !CfCreateFile(ctx,
edcontext->changes_filename,
pp, attr, &result))
{
{
RecordFailure(ctx, pp, attr,
"Failed to create file '%s' for rendering mustache template '%s'",
edcontext->filename, message);
Expand Down Expand Up @@ -964,6 +966,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
Rlist *args = NULL;
char edit_bundle_name[CF_BUFSIZE], lockname[CF_BUFSIZE];
CfLock thislock;
bool save_file = true;

snprintf(lockname, CF_BUFSIZE - 1, "fileedit-%s", filename);
thislock = AcquireLock(ctx, lockname, VUQNAME, CFSTARTTIME, a->transaction.ifelapsed, a->transaction.expireafter, pp, false);
Expand Down Expand Up @@ -1066,7 +1069,8 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
PromiseResult render_result = RenderTemplateCFEngine(ctx, pp,
args, a,
edcontext,
file_exists);
file_exists,
&save_file);
result = PromiseResultUpdate(result, render_result);
}
}
Expand Down Expand Up @@ -1098,7 +1102,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
}

exit:
FinishEditContext(ctx, edcontext, a, pp, &result);
FinishEditContext(ctx, edcontext, a, pp, &result, save_file);
YieldCurrentLock(thislock);
if (result == PROMISE_RESULT_CHANGE)
{
Expand Down
Loading