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)  

eval_context.h
Go to the documentation of this file.
1 /*
2  Copyright 2019 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 #ifndef CFENGINE_EVAL_CONTEXT_H
26 #define CFENGINE_EVAL_CONTEXT_H
27 
28 #include <platform.h>
29 
30 #include <writer.h>
31 #include <set.h>
32 #include <sequence.h>
33 #include <var_expressions.h>
34 #include <logic_expressions.h>
35 #include <scope.h>
36 #include <variable.h>
37 #include <class.h>
38 #include <iteration.h>
39 #include <rb-tree.h>
40 #include <ring_buffer.h>
41 
42 typedef enum
43 {
51 
52 typedef struct
53 {
54  const Bundle *owner;
55 
59 
60 typedef struct
61 {
62  const Body *owner;
63 
66 
67 typedef struct
68 {
69  const Promise *owner;
70 
73 
74 typedef struct
75 {
78 
79 typedef struct
80 {
83  size_t index;
86 
87 typedef struct
88 {
90  bool inherits_previous; // whether or not this frame inherits context from the previous frame
91 
92  union
93  {
99  } data;
100 
101  char *path;
102 } StackFrame;
103 
104 typedef enum
105 {
107 
110 
111  EVAL_OPTION_FULL = 0xFFFFFFFF
113 
116 
117 void EvalContextHeapAddAbort(EvalContext *ctx, const char *context, const char *activated_on_context);
118 void EvalContextHeapAddAbortCurrentBundle(EvalContext *ctx, const char *context, const char *activated_on_context);
119 
120 void EvalContextHeapPersistentSave(EvalContext *ctx, const char *name, unsigned int ttl_minutes, PersistentClassPolicy policy, const char *tags);
121 void EvalContextHeapPersistentRemove(const char *context);
123 
124 bool EvalContextClassPutSoft(EvalContext *ctx, const char *name, ContextScope scope, const char *tags);
125 bool EvalContextClassPutHard(EvalContext *ctx, const char *name, const char *tags);
126 Class *EvalContextClassGet(const EvalContext *ctx, const char *ns, const char *name);
127 Class *EvalContextClassMatch(const EvalContext *ctx, const char *regex);
128 bool EvalContextClassRemove(EvalContext *ctx, const char *ns, const char *name);
129 StringSet *EvalContextClassTags(const EvalContext *ctx, const char *ns, const char *name);
130 
131 ClassTableIterator *EvalContextClassTableIteratorNewGlobal(const EvalContext *ctx, const char *ns, bool is_hard, bool is_soft);
133 
134 // Class Logging
136 void EvalContextAllClassesLoggingEnable(EvalContext *ctx, bool enable);
137 
138 void EvalContextPushBundleName(const EvalContext *ctx, const char *bundle_name);
140 
141 void EvalContextPushRemoteVarPromise(EvalContext *ctx, const char *bundle_name, const Promise *pp);
142 const Seq *EvalContextGetRemoteVarPromises(const EvalContext *ctx, const char *bundle_name);
143 
144 void EvalContextClear(EvalContext *ctx);
145 
147 
148 void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous);
149 void EvalContextStackPushBodyFrame(EvalContext *ctx, const Promise *caller, const Body *body, const Rlist *args);
151 void EvalContextStackPushPromiseFrame(EvalContext *ctx, const Promise *owner);
154 const char *EvalContextStackToString(EvalContext *ctx);
155 void EvalContextSetBundleArgs(EvalContext *ctx, const Rlist *args);
156 void EvalContextSetPass(EvalContext *ctx, int pass);
159 
160 char *EvalContextStackPath(const EvalContext *ctx);
165 
168 
169 bool EvalContextVariablePut(EvalContext *ctx, const VarRef *ref, const void *value, DataType type, const char *tags);
170 bool EvalContextVariablePutSpecial(EvalContext *ctx, SpecialScope scope, const char *lval, const void *value, DataType type, const char *tags);
171 const void *EvalContextVariableGetSpecial(const EvalContext *ctx, const SpecialScope scope, const char *varname, DataType *type_out);
172 const char *EvalContextVariableGetSpecialString(const EvalContext *ctx, const SpecialScope scope, const char *varname);
173 const void *EvalContextVariableGet(const EvalContext *ctx, const VarRef *ref, DataType *type_out);
174 const Promise *EvalContextVariablePromiseGet(const EvalContext *ctx, const VarRef *ref);
175 bool EvalContextVariableRemoveSpecial(const EvalContext *ctx, SpecialScope scope, const char *lval);
176 bool EvalContextVariableRemove(const EvalContext *ctx, const VarRef *ref);
177 StringSet *EvalContextVariableTags(const EvalContext *ctx, const VarRef *ref);
179 VariableTableIterator *EvalContextVariableTableIteratorNew(const EvalContext *ctx, const char *ns, const char *scope, const char *lval);
181 
182 bool EvalContextPromiseLockCacheContains(const EvalContext *ctx, const char *key);
183 void EvalContextPromiseLockCachePut(EvalContext *ctx, const char *key);
184 void EvalContextPromiseLockCacheRemove(EvalContext *ctx, const char *key);
185 bool EvalContextFunctionCacheGet(const EvalContext *ctx, const FnCall *fp, const Rlist *args, Rval *rval_out);
186 void EvalContextFunctionCachePut(EvalContext *ctx, const FnCall *fp, const Rlist *args, const Rval *rval);
187 
189 
190 /**
191  * @brief Find a bundle for a bundle call, given a callee reference (in the form of ns:bundle), and a type of bundle.
192  * This is requires EvalContext because the callee reference may be unqualified.
193  * Hopefully this should go away in the future if we make a more generalized API to simply call a bundle,
194  * but we have a few special rules around edit_line and so on.
195  */
196 const Bundle *EvalContextResolveBundleExpression(const EvalContext *ctx, const Policy *policy,
197  const char *callee_reference, const char *callee_type);
198 
199 const Body *EvalContextFindFirstMatchingBody(const Policy *policy, const char *type,
200  const char *namespace, const char *name);
201 
202 /**
203  @brief Returns a Sequence of const Body* elements, first the body and then its parents
204 
205  Uses `inherit_from` to figure out the parents.
206  */
207 Seq *EvalContextResolveBodyExpression(const EvalContext *ctx, const Policy *policy,
208  const char *callee_reference, const char *callee_type);
209 
210 /* - Parsing/evaluating expressions - */
211 void ValidateClassSyntax(const char *str);
212 ExpressionValue CheckClassExpression(const EvalContext *ctx, const char *context);
213 static inline bool IsDefinedClass(const EvalContext *ctx, const char *context)
214 {
215  return (CheckClassExpression(ctx, context) == EXPRESSION_VALUE_TRUE);
216 }
217 StringSet *ClassesMatching(const EvalContext *ctx, ClassTableIterator *iter, const char* regex, const Rlist *tags, bool first_only);
218 
219 bool EvalProcessResult(const char *process_result, StringSet *proc_attr);
220 bool EvalFileResult(const char *file_result, StringSet *leaf_attr);
221 
222 /* Various global options */
223 void SetChecksumUpdatesDefault(EvalContext *ctx, bool enabled);
224 bool GetChecksumUpdatesDefault(const EvalContext *ctx);
225 
226 /* IP addresses */
228 void EvalContextAddIpAddress(EvalContext *ctx, const char *address, const char *iface);
230 
231 /* - Rest - */
234 
235 bool EvalContextIsIgnoringLocks(const EvalContext *ctx);
236 void EvalContextSetIgnoreLocks(EvalContext *ctx, bool ignore);
237 
238 void EvalContextSetLaunchDirectory(EvalContext *ctx, const char *path);
239 void EvalContextSetEntryPoint(EvalContext* ctx, const char *entry_point);
240 const char *EvalContextGetEntryPoint(EvalContext* ctx);
241 
242 bool BundleAbort(EvalContext *ctx);
243 bool EvalAborted(const EvalContext *ctx);
244 void NotifyDependantPromises(EvalContext *ctx, const Promise *pp, PromiseResult result);
245 bool MissingDependencies(EvalContext *ctx, const Promise *pp);
246 void cfPS(EvalContext *ctx, LogLevel level, PromiseResult status, const Promise *pp, const Attributes *attr, const char *fmt, ...) FUNC_ATTR_PRINTF(6, 7);
247 
249 
251 void EvalContextSetSelectEndMatchEof(EvalContext *ctx, bool value);
252 
253 void AddDefaultPackageModuleToContext(const EvalContext *ctx, char *name);
254 void AddDefaultInventoryToContext(const EvalContext *ctx, Rlist *inventory);
255 
261 
262 /* This function is temporarily exported. It needs to be made an detail of
263  * evaluator again, once variables promises are no longer specially handled */
264 void ClassAuditLog(EvalContext *ctx, const Promise *pp, const Attributes *attr, PromiseResult status);
265 
267 
269  EvalContext *, ctx,
270  const Promise *, pp,
271  PromiseResult, result);
272 
274  EvalContext *, ctx);
275 char *MissionPortalLogHook(LoggingPrivContext *pctx, LogLevel level, const char *message);
276 
278 
279 #endif
#define ARG_UNUSED
Definition: cf-net.c:47
CommonControl
Definition: cf3.defs.h:415
PromiseResult
Definition: cf3.defs.h:122
DataType
Definition: cf3.defs.h:368
ContextScope
Definition: cf3.defs.h:945
#define FUNC_ATTR_PRINTF(string_index, first_to_check)
Definition: compiler.h:52
PersistentClassPolicy
Definition: db_structs.h:136
#define ENTERPRISE_VOID_FUNC_2ARG_DECLARE(__ret, __func, __t1, __p1, __t2, __p2)
#define ENTERPRISE_VOID_FUNC_3ARG_DECLARE(__ret, __func, __t1, __p1, __t2, __p2, __t3, __p3)
#define ENTERPRISE_VOID_FUNC_1ARG_DECLARE(__ret, __func, __t1, __p1)
void EvalContextHeapPersistentSave(EvalContext *ctx, const char *name, unsigned int ttl_minutes, PersistentClassPolicy policy, const char *tags)
Definition: eval_context.c:643
void EvalContextFunctionCachePut(EvalContext *ctx, const FnCall *fp, const Rlist *args, const Rval *rval)
void EvalContextSetupMissionPortalLogHook(EvalContext *ctx)
void ValidateClassSyntax(const char *str)
void AddPackageModuleToContext(const EvalContext *ctx, PackageModuleBody *pm)
Definition: eval_context.c:226
bool EvalContextVariableRemoveSpecial(const EvalContext *ctx, SpecialScope scope, const char *lval)
bool EvalContextPromiseLockCacheContains(const EvalContext *ctx, const char *key)
void EvalContextClear(EvalContext *ctx)
bool BundleAbort(EvalContext *ctx)
Definition: eval_context.c:803
StringSet * EvalContextStackPromisees(const EvalContext *ctx)
const void * EvalContextVariableGetSpecial(const EvalContext *ctx, const SpecialScope scope, const char *varname, DataType *type_out)
StringSet * ClassesMatching(const EvalContext *ctx, ClassTableIterator *iter, const char *regex, const Rlist *tags, bool first_only)
PackageModuleBody * GetDefaultPackageModuleFromContext(const EvalContext *ctx)
Definition: eval_context.c:264
void ClassAuditLog(EvalContext *ctx, const Promise *pp, const Attributes *attr, PromiseResult status)
void TrackTotalCompliance(PromiseResult status, const Promise *pp)
bool EvalContextGetSelectEndMatchEof(const EvalContext *ctx)
Definition: eval_context.c:189
const RingBuffer * EvalContextStackCurrentMessages(const EvalContext *ctx)
bool EvalContextFunctionCacheGet(const EvalContext *ctx, const FnCall *fp, const Rlist *args, Rval *rval_out)
const char * EvalContextVariableGetSpecialString(const EvalContext *ctx, const SpecialScope scope, const char *varname)
const char * EvalContextGetEntryPoint(EvalContext *ctx)
void AddDefaultInventoryToContext(const EvalContext *ctx, Rlist *inventory)
Definition: eval_context.c:210
void EvalContextAllClassesLoggingEnable(EvalContext *ctx, bool enable)
JsonElement * EvalContextGetPromiseCallers(EvalContext *ctx)
bool EvalFileResult(const char *file_result, StringSet *leaf_attr)
Definition: eval_context.c:635
Seq * EvalContextResolveBodyExpression(const EvalContext *ctx, const Policy *policy, const char *callee_reference, const char *callee_type)
Returns a Sequence of const Body* elements, first the body and then its parents.
void EvalContextSetSelectEndMatchEof(EvalContext *ctx, bool value)
Definition: eval_context.c:194
JsonElement * JsonExpandElement(EvalContext *ctx, const JsonElement *source)
PackagePromiseContext * GetPackageDefaultsFromCtx(const EvalContext *ctx)
void AddDefaultPackageModuleToContext(const EvalContext *ctx, char *name)
Definition: eval_context.c:200
void EvalContextHeapAddAbort(EvalContext *ctx, const char *context, const char *activated_on_context)
Definition: eval_context.c:839
void EvalContextStackPushPromiseFrame(EvalContext *ctx, const Promise *owner)
StackFrameType
Definition: eval_context.h:43
@ STACK_FRAME_TYPE_PROMISE_ITERATION
Definition: eval_context.h:48
@ STACK_FRAME_TYPE_PROMISE
Definition: eval_context.h:47
@ STACK_FRAME_TYPE_PROMISE_TYPE
Definition: eval_context.h:46
@ STACK_FRAME_TYPE_BODY
Definition: eval_context.h:45
@ STACK_FRAME_TYPE_MAX
Definition: eval_context.h:49
@ STACK_FRAME_TYPE_BUNDLE
Definition: eval_context.h:44
const StringSet * EvalContextGetBundleNames(const EvalContext *ctx)
bool EvalContextClassRemove(EvalContext *ctx, const char *ns, const char *name)
bool EvalProcessResult(const char *process_result, StringSet *proc_attr)
Definition: eval_context.c:620
void EvalContextAddIpAddress(EvalContext *ctx, const char *address, const char *iface)
void EvalContextLogPromiseIterationOutcome(EvalContext *ctx, const Promise *pp, PromiseResult result)
bool EvalContextGetEvalOption(EvalContext *ctx, EvalContextOption option)
void cfPS(EvalContext *ctx, LogLevel level, PromiseResult status, const Promise *pp, const Attributes *attr, const char *fmt,...)
EvalContext * EvalContextNew(void)
void EvalContextStackPushPromiseTypeFrame(EvalContext *ctx, const PromiseType *owner)
bool EvalAborted(const EvalContext *ctx)
Definition: eval_context.c:833
Class * EvalContextClassMatch(const EvalContext *ctx, const char *regex)
void NotifyDependantPromises(EvalContext *ctx, const Promise *pp, PromiseResult result)
void EvalContextHeapAddAbortCurrentBundle(EvalContext *ctx, const char *context, const char *activated_on_context)
Definition: eval_context.c:856
StringSet * EvalContextVariableTags(const EvalContext *ctx, const VarRef *ref)
VariableTableIterator * EvalContextVariableTableFromRefIteratorNew(const EvalContext *ctx, const VarRef *ref)
const void * EvalContextVariableControlCommonGet(const EvalContext *ctx, CommonControl lval)
const StringSet * EvalContextAllClassesGet(const EvalContext *ctx)
const Bundle * EvalContextResolveBundleExpression(const EvalContext *ctx, const Policy *policy, const char *callee_reference, const char *callee_type)
Find a bundle for a bundle call, given a callee reference (in the form of ns:bundle),...
Promise * EvalContextStackPushPromiseIterationFrame(EvalContext *ctx, const PromiseIterator *iter_ctx)
void EvalContextSetBundleArgs(EvalContext *ctx, const Rlist *args)
bool MissingDependencies(EvalContext *ctx, const Promise *pp)
Definition: eval_context.c:867
PackagePromiseContext * GetPackagePromiseContext(const EvalContext *ctx)
Definition: eval_context.c:275
ExpressionValue CheckClassExpression(const EvalContext *ctx, const char *context)
Definition: eval_context.c:534
ClassTableIterator * EvalContextClassTableIteratorNewGlobal(const EvalContext *ctx, const char *ns, bool is_hard, bool is_soft)
void EvalContextDeleteIpAddresses(EvalContext *ctx)
void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous)
const char * EvalContextStackToString(EvalContext *ctx)
Definition: eval_context.c:371
void EvalContextSetLaunchDirectory(EvalContext *ctx, const char *path)
static bool IsDefinedClass(const EvalContext *ctx, const char *context)
Definition: eval_context.h:213
bool EvalContextClassPutHard(EvalContext *ctx, const char *name, const char *tags)
void EvalContextPromiseLockCacheRemove(EvalContext *ctx, const char *key)
Rlist * EvalContextGetBundleArgs(EvalContext *ctx)
const Promise * EvalContextVariablePromiseGet(const EvalContext *ctx, const VarRef *ref)
void EvalContextDestroy(EvalContext *ctx)
void EvalContextSetPass(EvalContext *ctx, int pass)
bool EvalContextIsIgnoringLocks(const EvalContext *ctx)
Rlist * EvalContextGetPromiseCallerMethods(EvalContext *ctx)
EvalContextOption
Definition: eval_context.h:105
@ EVAL_OPTION_EVAL_FUNCTIONS
Definition: eval_context.h:108
@ EVAL_OPTION_NONE
Definition: eval_context.h:106
@ EVAL_OPTION_FULL
Definition: eval_context.h:111
@ EVAL_OPTION_CACHE_SYSTEM_FUNCTIONS
Definition: eval_context.h:109
const void * EvalContextVariableGet(const EvalContext *ctx, const VarRef *ref, DataType *type_out)
char * MissionPortalLogHook(LoggingPrivContext *pctx, LogLevel level, const char *message)
Definition: eval_context.c:987
void EvalContextPushRemoteVarPromise(EvalContext *ctx, const char *bundle_name, const Promise *pp)
bool EvalContextVariableRemove(const EvalContext *ctx, const VarRef *ref)
bool EvalContextVariableClearMatch(EvalContext *ctx)
Rlist * GetDefaultInventoryFromContext(const EvalContext *ctx)
Definition: eval_context.c:270
void EvalContextSetIgnoreLocks(EvalContext *ctx, bool ignore)
const Body * EvalContextFindFirstMatchingBody(const Policy *policy, const char *type, const char *namespace, const char *name)
bool GetChecksumUpdatesDefault(const EvalContext *ctx)
ClassTableIterator * EvalContextClassTableIteratorNewLocal(const EvalContext *ctx)
const Promise * EvalContextStackCurrentPromise(const EvalContext *ctx)
void EvalContextHeapPersistentLoadAll(EvalContext *ctx)
Definition: eval_context.c:731
void EvalContextStackPushBodyFrame(EvalContext *ctx, const Promise *caller, const Body *body, const Rlist *args)
Item * EvalContextGetIpAddresses(const EvalContext *ctx)
PackageModuleBody * GetPackageModuleFromContext(const EvalContext *ctx, const char *name)
Definition: eval_context.c:242
char * EvalContextStackPath(const EvalContext *ctx)
StringSet * EvalContextClassTags(const EvalContext *ctx, const char *ns, const char *name)
void EvalContextStackPopFrame(EvalContext *ctx)
void SetChecksumUpdatesDefault(EvalContext *ctx, bool enabled)
void EvalContextSetEntryPoint(EvalContext *ctx, const char *entry_point)
void EvalContextSetEvalOption(EvalContext *ctx, EvalContextOption option, bool value)
bool EvalContextVariablePutSpecial(EvalContext *ctx, SpecialScope scope, const char *lval, const void *value, DataType type, const char *tags)
void EvalContextHeapPersistentRemove(const char *context)
Definition: eval_context.c:715
const Bundle * EvalContextStackCurrentBundle(const EvalContext *ctx)
int EvalContextGetPass(EvalContext *ctx)
void EvalContextPushBundleName(const EvalContext *ctx, const char *bundle_name)
void EvalContextPromiseLockCachePut(EvalContext *ctx, const char *key)
const Seq * EvalContextGetRemoteVarPromises(const EvalContext *ctx, const char *bundle_name)
Class * EvalContextClassGet(const EvalContext *ctx, const char *ns, const char *name)
bool EvalContextVariablePut(EvalContext *ctx, const VarRef *ref, const void *value, DataType type, const char *tags)
bool EvalContextClassPutSoft(EvalContext *ctx, const char *name, ContextScope scope, const char *tags)
VariableTableIterator * EvalContextVariableTableIteratorNew(const EvalContext *ctx, const char *ns, const char *scope, const char *lval)
#define const
Definition: getopt.c:38
LogLevel
Definition: log.h:30
ExpressionValue
@ EXPRESSION_VALUE_TRUE
SpecialScope
Definition: scope.h:33
Definition: policy.h:85
Definition: policy.h:70
Definition: class.h:31
Definition: fncall.h:31
Definition: item_lib.h:33
Definition: policy.h:53
Definition: rlist.h:35
Definition: cf3.defs.h:614
Sequence data-structure.
Definition: sequence.h:50
const Body * owner
Definition: eval_context.h:62
VariableTable * vars
Definition: eval_context.h:64
VariableTable * vars
Definition: eval_context.h:57
const Bundle * owner
Definition: eval_context.h:54
ClassTable * classes
Definition: eval_context.h:56
const PromiseIterator * iter_ctx
Definition: eval_context.h:82
const PromiseType * owner
Definition: eval_context.h:76
VariableTable * vars
Definition: eval_context.h:71
const Promise * owner
Definition: eval_context.h:69
StackFramePromiseType promise_type
Definition: eval_context.h:96
StackFrameBody body
Definition: eval_context.h:95
StackFramePromiseIteration promise_iteration
Definition: eval_context.h:98
bool inherits_previous
Definition: eval_context.h:90
StackFramePromise promise
Definition: eval_context.h:97
char * path
Definition: eval_context.h:101
StackFrameType type
Definition: eval_context.h:89
StackFrameBundle bundle
Definition: eval_context.h:94
Definition: set.h:138
Definition: getopt.h:83