Skip to content

Commit 797bed8

Browse files
committed
Allow top-down evaluation of promises
Ticket: ENT-10184 Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent b8e4c70 commit 797bed8

File tree

8 files changed

+68
-0
lines changed

8 files changed

+68
-0
lines changed

cf-agent/cf-agent.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,15 @@ static void AllClassesReport(const EvalContext *ctx)
15591559

15601560
PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp)
15611561
// NB - this function can be called recursively through "methods"
1562+
{
1563+
if (EvalContextGetEvalOption(ctx, EVAL_OPTION_NORMAL_EVALUATION))
1564+
{
1565+
return ScheduleAgentOperationsNormalOrder(ctx, bp);
1566+
}
1567+
return ScheduleAgentOperationsTopDownOrder(ctx, bp);
1568+
}
1569+
1570+
PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle *bp)
15621571
{
15631572
assert(bp != NULL);
15641573

@@ -1652,6 +1661,46 @@ PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp)
16521661
return result;
16531662
}
16541663

1664+
PromiseResult ScheduleAgentOperationsTopDownOrder(EvalContext *ctx, const Bundle *bp)
1665+
{
1666+
assert(bp != NULL);
1667+
1668+
int save_pr_kept = PR_KEPT;
1669+
int save_pr_repaired = PR_REPAIRED;
1670+
int save_pr_notkept = PR_NOTKEPT;
1671+
struct timespec start = BeginMeasure();
1672+
1673+
if (PROCESSREFRESH == NULL || (PROCESSREFRESH && IsRegexItemIn(ctx, PROCESSREFRESH, bp->name)))
1674+
{
1675+
ClearProcessTable();
1676+
}
1677+
1678+
PromiseResult result = PROMISE_RESULT_SKIPPED;
1679+
for (int pass = 1; pass < CF_DONEPASSES; pass++)
1680+
{
1681+
for (size_t ppi = 0; ppi < SeqLength(bp->all_promises); ppi++)
1682+
{
1683+
Promise *pp = SeqAt(bp->all_promises, ppi);
1684+
BundleSection *parent_section = pp->parent_section;
1685+
1686+
EvalContextStackPushBundleSectionFrame(ctx, parent_section);
1687+
1688+
PromiseResult promise_result = ExpandPromise(ctx, pp, KeepAgentPromise, NULL);
1689+
result = PromiseResultUpdate(result, promise_result);
1690+
if (EvalAborted(ctx) || BundleAbort(ctx))
1691+
{
1692+
EvalContextStackPopFrame(ctx);
1693+
NoteBundleCompliance(bp, save_pr_kept, save_pr_repaired, save_pr_notkept, start);
1694+
return result;
1695+
}
1696+
EvalContextStackPopFrame(ctx);
1697+
}
1698+
}
1699+
1700+
NoteBundleCompliance(bp, save_pr_kept, save_pr_repaired, save_pr_notkept, start);
1701+
return result;
1702+
}
1703+
16551704
/*********************************************************************/
16561705

16571706
#ifdef __MINGW32__

libpromises/cf3.defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ typedef enum
441441
COMMON_CONTROL_TLS_MIN_VERSION,
442442
COMMON_CONTROL_PACKAGE_INVENTORY,
443443
COMMON_CONTROL_PACKAGE_MODULE,
444+
COMMON_CONTROL_EVALUATION_ORDER,
444445
COMMON_CONTROL_MAX
445446
} CommonControl;
446447

libpromises/eval_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ typedef enum
108108

109109
EVAL_OPTION_EVAL_FUNCTIONS = 1 << 0,
110110
EVAL_OPTION_CACHE_SYSTEM_FUNCTIONS = 1 << 1,
111+
EVAL_OPTION_NORMAL_EVALUATION = 1 << 2,
111112

112113
EVAL_OPTION_FULL = 0xFFFFFFFF
113114
} EvalContextOption;

libpromises/expand.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,15 @@ static void ResolveControlBody(EvalContext *ctx, GenericAgentConfig *config,
10251025
/* Ignored */
10261026
}
10271027

1028+
if (strcmp(lval, CFG_CONTROLBODY[COMMON_CONTROL_EVALUATION_ORDER].lval) == 0)
1029+
{
1030+
Log(LOG_LEVEL_VERBOSE, "SET evaluation %s",
1031+
RvalScalarValue(evaluated_rval));
1032+
1033+
bool is_normal = (StringEqual(RvalScalarValue(evaluated_rval), "normal"));
1034+
EvalContextSetEvalOption(ctx, EVAL_OPTION_NORMAL_EVALUATION, is_normal);
1035+
}
1036+
10281037
RvalDestroy(evaluated_rval);
10291038
}
10301039

libpromises/mod_common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ const ConstraintSyntax CFG_CONTROLBODY[COMMON_CONTROL_MAX + 1] =
268268
ConstraintSyntaxNewString("tls_min_version", "", "Minimum acceptable TLS version for outgoing connections, defaults to OpenSSL's default", SYNTAX_STATUS_NORMAL),
269269
ConstraintSyntaxNewStringList("package_inventory", ".*", "Name of the package manager used for software inventory management", SYNTAX_STATUS_NORMAL),
270270
ConstraintSyntaxNewString("package_module", ".*", "Name of the default package manager", SYNTAX_STATUS_NORMAL),
271+
ConstraintSyntaxNewString("evaluation_order", "(normal|top_down)", "Order of evaluation of promises", SYNTAX_STATUS_NORMAL),
271272
ConstraintSyntaxNewNull()
272273
};
273274

libpromises/policy.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ Bundle *PolicyAppendBundle(Policy *policy,
13221322
bundle->source_path = SafeStringDuplicate(source_path);
13231323
bundle->sections = SeqNew(10, BundleSectionDestroy);
13241324
bundle->custom_sections = SeqNew(10, BundleSectionDestroy);
1325+
bundle->all_promises = SeqNew(10, NULL);
13251326

13261327
return bundle;
13271328
}
@@ -1437,6 +1438,7 @@ Promise *BundleSectionAppendPromise(BundleSection *section, const char *promiser
14371438
{
14381439
assert(promiser && "Missing promiser");
14391440
assert(section != NULL && "Missing promise type");
1441+
assert(section->parent_bundle != NULL);
14401442

14411443
Promise *pp = xcalloc(1, sizeof(Promise));
14421444

@@ -1452,6 +1454,7 @@ Promise *BundleSectionAppendPromise(BundleSection *section, const char *promiser
14521454
}
14531455

14541456
SeqAppend(section->promises, pp);
1457+
SeqAppend(section->parent_bundle->all_promises, pp);
14551458

14561459
pp->parent_section = section;
14571460

@@ -1479,6 +1482,7 @@ static void BundleDestroy(Bundle *bundle)
14791482
RlistDestroy(bundle->args);
14801483
SeqDestroy(bundle->sections);
14811484
SeqDestroy(bundle->custom_sections);
1485+
SeqDestroy(bundle->all_promises);
14821486

14831487
free(bundle);
14841488
}

libpromises/policy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct Bundle_
7979

8080
Seq *sections;
8181
Seq *custom_sections;
82+
Seq *all_promises;
8283

8384
char *source_path;
8485
SourceOffset offset;

libpromises/prototypes3.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ void yyerror(const char *s);
4444
/* agent.c */
4545

4646
PromiseResult ScheduleAgentOperations(EvalContext *ctx, const Bundle *bp);
47+
PromiseResult ScheduleAgentOperationsNormalOrder(EvalContext *ctx, const Bundle *bp);
48+
PromiseResult ScheduleAgentOperationsTopDownOrder(EvalContext *ctx, const Bundle *bp);
4749

4850
/* Only for agent.c */
4951

0 commit comments

Comments
 (0)