cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

expand.c File Reference
#include <expand.h>
#include <misc_lib.h>
#include <eval_context.h>
#include <policy.h>
#include <promises.h>
#include <vars.h>
#include <syntax.h>
#include <files_names.h>
#include <scope.h>
#include <matching.h>
#include <unix.h>
#include <attributes.h>
#include <fncall.h>
#include <iteration.h>
#include <audit.h>
#include <verify_vars.h>
#include <string_lib.h>
#include <conversion.h>
#include <verify_classes.h>
Include dependency graph for expand.c:

Go to the source code of this file.


static char opposite (char c)
static void PutHandleVariable (EvalContext *ctx, const Promise *pp)
static void MapIteratorsFromRval (EvalContext *ctx, PromiseIterator *iterctx, Rval rval)
static PromiseResult ExpandPromiseAndDo (EvalContext *ctx, PromiseIterator *iterctx, PromiseActuator *act_on_promise, void *param, bool actuate_ifelse)
PromiseResult ExpandPromise (EvalContext *ctx, const Promise *pp, PromiseActuator *act_on_promise, void *param)
Rval ExpandPrivateRval (EvalContext *ctx, const char *ns, const char *scope, const void *rval_item, RvalType rval_type)
static bool VariableDataOrListReference (const char *str)
static Rval ExpandListEntry (EvalContext *ctx, const char *ns, const char *scope, int expandnaked, Rval entry)
RlistExpandList (EvalContext *ctx, const char *ns, const char *scope, const Rlist *list, int expandnaked)
Rval ExpandBundleReference (EvalContext *ctx, const char *ns, const char *scope, Rval rval)
char * ExpandScalar (const EvalContext *ctx, const char *ns, const char *scope, const char *string, Buffer *out)
Rval EvaluateFinalRval (EvalContext *ctx, const Policy *policy, const char *ns, const char *scope, Rval rval, bool forcelist, const Promise *pp)
void BundleResolvePromiseType (EvalContext *ctx, const Bundle *bundle, const char *type, PromiseActuator *actuator)
static int PointerCmp (const void *a, const void *b, void *user_data)
static void RemoveRemotelyInjectedVars (const EvalContext *ctx, const Bundle *bundle)
void BundleResolve (EvalContext *ctx, const Bundle *bundle)
static void ResolveControlBody (EvalContext *ctx, GenericAgentConfig *config, const Body *control_body)
static void ResolvePackageManagerBody (EvalContext *ctx, const Body *pm_body)
void PolicyResolve (EvalContext *ctx, const Policy *policy, GenericAgentConfig *config)
bool IsExpandable (const char *str)
bool IsNakedVar (const char *str, char vtype)
void GetNaked (char *dst, const char *s)
bool IsVarList (const char *var)
PromiseResult CommonEvalPromise (EvalContext *ctx, const Promise *pp, void *param)

Function Documentation

◆ BundleResolve()

◆ BundleResolvePromiseType()

void BundleResolvePromiseType ( EvalContext ctx,
const Bundle bundle,
const char *  type,
PromiseActuator actuator 

◆ CommonEvalPromise()

PromiseResult CommonEvalPromise ( EvalContext ctx,
const Promise pp,
void *  param 

Definition at line 1291 of file expand.c.

References NULL, PROMISE_RESULT_NOOP, and PromiseRecheckAllConstraints().

Referenced by ExpandPromiseAndDo(), and LoadPolicy().

◆ EvaluateFinalRval()

◆ ExpandBundleReference()

Rval ExpandBundleReference ( EvalContext ctx,
const char *  ns,
const char *  scope,
Rval  rval 

◆ ExpandList()

Rlist* ExpandList ( EvalContext ctx,
const char *  ns,
const char *  scope,
const Rlist list,
int  expandnaked 

◆ ExpandListEntry()

◆ ExpandPrivateRval()

◆ ExpandPromise()

◆ ExpandPromiseAndDo()

◆ ExpandScalar()

◆ GetNaked()

void GetNaked ( char *  dst,
const char *  s 

Copy @(listname) -> listname.

This function performs no validations, it is necessary to call the validation functions before calling this function.

@NOTE make sure sizeof(dst) >= sizeof(s)

Definition at line 1253 of file expand.c.

References CF_MAXVARSIZE, Log(), LOG_LEVEL_ERR, and strlcpy().

Referenced by EvaluateFinalRval(), ExpandListEntry(), FnCallSelectServers(), ResolveAndQualifyVarName(), RlistFlatten(), and ScopeAugment().

◆ IsExpandable()

◆ IsNakedVar()

bool IsNakedVar ( const char *  str,
char  vtype 

Check if #str contains one and only one variable expansion of #vtype kind (it's usually either '$' or '@'). It can contain nested expansions which are not checked properly. Examples: true: "$(whatever)", "${whatever}", "$(blah$(blue))" false: "$(blah)blue", "blah$(blue)", "$(blah)$(blue)", "$(blah}"

Definition at line 1198 of file expand.c.

References opposite().

Referenced by CheckParseOpts(), CheckParseString(), EvaluateFinalRval(), ExpandListEntry(), RlistFlatten(), ScopeAugment(), and VerifyVarPromise().

◆ IsVarList()

bool IsVarList ( const char *  var)

Checks if a variable is an -list and returns true or false.

Definition at line 1274 of file expand.c.

Referenced by FnCallSelectServers(), ResolveAndQualifyVarName(), and SelectClass().

◆ MapIteratorsFromRval()

static void MapIteratorsFromRval ( EvalContext ctx,
PromiseIterator iterctx,
Rval  rval 

Recursively go down the #rval and run PromiseIteratorPrepare() to take note of all iterables and mangle all rvals than need to be mangled before iterating.

Definition at line 136 of file expand.c.

References FnCall_::name, NULL, PromiseIteratorPrepare(), RVAL_TYPE_CONTAINER, RVAL_TYPE_FNCALL, RVAL_TYPE_LIST, RVAL_TYPE_NOPROMISEE, RVAL_TYPE_SCALAR, RvalFnCallValue(), RvalRlistValue(), RvalScalarValue(), and Rval::type.

Referenced by ExpandPromise().

◆ opposite()

static char opposite ( char  c)


Expanding variables is easy – expanding lists automagically requires some thought. Remember that

promiser <=> RVAL_TYPE_SCALAR promisee <=> RVAL_TYPE_LIST

For bodies we have


Any list or container variable occurring within a scalar or in place of a scalar is assumed to be iterated i.e. . See comments in iteration.c.

Any list variable @(name) is not iterated, but dropped into place (see DeRefCopyPromise()).

Please note that bodies cannot contain iterators.

The full process of promise and variable expansion is mostly outlined in ExpandPromise() and ExpandPromiseAndDo() and the basic steps are:

  • Skip everything if the class guard is not defined.
  • DeRefCopyPromise(): Copy the promise while expanding '@' slists and body arguments and handling body inheritance. This requires one round of expansion with scopeid "body".
  • Push promise frame
  • MapIteratorsFromRval(): Parse all strings (promiser-promisee-constraints), find all unexpanded variables, mangle them if needed (if they are namespaced/scoped), and initialise the wheels in the iteration engine (iterctx) to iterate over iterable variables (slists and containers). See comments in iteration.c for further details.
  • For every iteration:
    • Push iteration frame
    • EvalContextStackPushPromiseIterationFrame()->ExpandDeRefPromise(): Make another copy of the promise with all constraints evaluated and variables expanded.

      – NOTE: As a result all functions are also evaluated, even if they are not to be used immediately (for example promises that the actuator skips because of ifvarclass, see promises.c:ExpandDeRefPromise() ).

      – (TODO IS IT CORRECT?) In a sub-bundle, create a new context and make hashes of the the transferred variables in the temporary context

    • Run the actuator (=act_on_promise= i.e. =VerifyWhateverPromise()=)
    • Pop iteration frame
  • Pop promise frame

Definition at line 1180 of file expand.c.

References ProgrammingError.

Referenced by IsNakedVar(), and VariableDataOrListReference().

◆ PointerCmp()

static int PointerCmp ( const void *  a,
const void *  b,
void *  user_data 

Definition at line 734 of file expand.c.

Referenced by RemoveRemotelyInjectedVars().

◆ PolicyResolve()

◆ PutHandleVariable()

static void PutHandleVariable ( EvalContext ctx,
const Promise pp 

◆ RemoveRemotelyInjectedVars()

◆ ResolveControlBody()

static void ResolveControlBody ( EvalContext ctx,
GenericAgentConfig config,
const Body control_body 

Evaluate the relevant control body, and set the relevant fields in #ctx and #config.

Definition at line 831 of file expand.c.

References AddDefaultInventoryToContext(), AddDefaultPackageModuleToContext(), BodySyntax_::body_type, BooleanFromString(), CF_DATA_TYPE_STRING, CF_MAXVARSIZE, CFG_CONTROLBODY, Constraint_::classes, COMMON_CONTROL_BUNDLESEQUENCE, COMMON_CONTROL_CACHE_SYSTEM_FUNCTIONS, COMMON_CONTROL_DOMAIN, COMMON_CONTROL_GOALPATTERNS, COMMON_CONTROL_IGNORE_MISSING_BUNDLES, COMMON_CONTROL_IGNORE_MISSING_INPUTS, COMMON_CONTROL_MAX, COMMON_CONTROL_OUTPUT_PREFIX, COMMON_CONTROL_PACKAGE_INVENTORY, COMMON_CONTROL_PACKAGE_MODULE, COMMON_CONTROL_PROTOCOL_VERSION, Body_::conlist, BodySyntax_::constraints, ConstraintSyntaxGetDataType(), CONTROL_BODIES, DataTypeToRvalType(), EVAL_OPTION_CACHE_SYSTEM_FUNCTIONS, EvalContextClassPutHard(), EvalContextSetEvalOption(), EvalContextStackPopFrame(), EvalContextStackPushBodyFrame(), EvalContextVariablePut(), EvalContextVariablePutSpecial(), EvalContextVariableRemove(), EvalContextVariableRemoveSpecial(), EvaluateFinalRval(), ExpandPrivateRval(), FatalError(), free(), GenericAgentConfig::ignore_missing_bundles, GenericAgentConfig::ignore_missing_inputs, IsDefinedClass(), Rval::item, SourceOffset::line, Log(), LOG_LEVEL_DEBUG, LOG_LEVEL_ERR, LOG_LEVEL_VERBOSE, Constraint_::lval, Body_::name, NULL, Constraint_::offset, Body_::parent_policy, GenericAgentConfig::protocol_version, ProtocolVersionParse, ProtocolVersionString(), Constraint_::rval, RvalDestroy(), RvalRlistValue(), RvalScalarValue(), SeqAt(), SeqLength(), Body_::source_path, SPECIAL_SCOPE_SYS, strlcpy(), Rval::type, Body_::type, VarRefDestroy(), VarRefParseFromScope(), VDOMAIN, VFQNAME, VPREFIX, VUQNAME, and xasprintf().

Referenced by PolicyResolve().

◆ ResolvePackageManagerBody()

◆ VariableDataOrListReference()

static bool VariableDataOrListReference ( const char *  str)

Detects a variable expansion inside of a data/list reference, for example "@(${container_name})" or "@(prefix${container_name})" or "@(nspace:${container_name})" or "@(container_name[${field}])".

This function doesn't have to be bullet-proof, it only needs to properly detect valid cases. The rest is left to the parser and code expanding variables.

Definition at line 357 of file expand.c.

References NULL, and opposite().

Referenced by ExpandListEntry().