Skip to content
Merged
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
3 changes: 1 addition & 2 deletions cf-reactor/cf-reactor.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <exec_tools.h>
#include <reactor_context.h>
#include <reactor_transform.h>
#include <watcher.h> /* WatcherRegistryClear */
#include <watcher.h> /* EventWatcherPause, EventWatcherResume */

/*****************************************************************************/
/* Globals */
Expand Down Expand Up @@ -242,7 +242,6 @@ static void CheckPolicyUpdates(EvalContext *ctx, Policy **policy, GenericAgentCo
* events detected so far with the current policy, and don't detect new
* ones until the watchers of the new policy are registered */
EventWatcherPause(ctx, HandleReactorEvent);
WatcherRegistryClear();
EvalContextClear(ctx);
PolicyDestroy(*policy);
*policy = NULL;
Expand Down
2 changes: 1 addition & 1 deletion cf-reactor/file_watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void *FileWatcherStateNew(const char *path)
return (void *) fws;
}

WatcherCheckResult CheckFileDeleted(void *state)
WatcherCheckResult FileWatcherCheckFileDeleted(void *state)
{
assert(state != NULL);
FileWatcherState *fws = state;
Expand Down
2 changes: 1 addition & 1 deletion cf-reactor/file_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


void *FileWatcherStateNew(const char *path);
WatcherCheckResult CheckFileDeleted(void *state);
WatcherCheckResult FileWatcherCheckFileDeleted(void *state);
void DestroyFileWatcherState(void *state);

#endif
42 changes: 16 additions & 26 deletions cf-reactor/reactor_transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,17 @@ static PromiseResult KeepEventsPromise(EvalContext *ctx, const Promise *pp)
return PROMISE_RESULT_FAIL;
}

WatcherOptions opt = {
.state = FileWatcherStateNew(path),
.check_callback = FileWatcherCheckFileDeleted,
.destroy_state = DestroyFileWatcherState,
};

// register watcher
Log(LOG_LEVEL_INFO, "Registering a file_deleted watcher for events promise '%s', on file '%s'", pp->promiser, path);
bool kept = WatcherRegister(pp->promiser, EVENT_FILE_DELETED, FileWatcherStateNew(path), pp->org_pp, 1);
WatcherRegister(pp, opt, 1);

return (kept) ? PROMISE_RESULT_NOOP : PROMISE_RESULT_FAIL;
return PROMISE_RESULT_NOOP;
}

static PromiseResult KeepReactorPromise(EvalContext *ctx, const Promise *pp, ARG_UNUSED void *param)
Expand Down Expand Up @@ -250,7 +256,7 @@ static PromiseResult RunThenBundle(EvalContext *ctx, const Promise *pp)

typedef struct
{
const char *promiser;
const char *promise_hash;
WatcherCheckResult check;
} ReactorEventParam;

Expand All @@ -263,36 +269,20 @@ static PromiseResult KeepEventsPromiseOnEvent(EvalContext *ctx, const Promise *p
assert(param != NULL);
const ReactorEventParam *event = param;

if (!StringEqual(pp->promiser, event->promiser))
char promise_hash[CF_HOSTKEY_STRING_SIZE];
WatcherPromiseHash(pp, promise_hash, sizeof(promise_hash));
if (!StringEqual(promise_hash, event->promise_hash))
{
return PROMISE_RESULT_SKIPPED;
}

PromiseResult result;
switch (event->check)
{
case WATCHER_CHECK_EVENT:
result = RunThenBundle(ctx, pp);
break;

case WATCHER_CHECK_ERROR:
Log(LOG_LEVEL_ERR, "Reactor events promise '%s' failed to check for its event", pp->promiser);
result = PROMISE_RESULT_FAIL;
break;

default:
ProgrammingError("Unexpected watcher check result %d for reactor events promise '%s'",
(int) event->check, pp->promiser);
}

RecordEventsPromiseOutcome(ctx, pp, result);
return result;
return RunThenBundle(ctx, pp);
}

void HandleReactorEvent(EvalContext *ctx, const Promise *pp, const char *promiser, WatcherCheckResult check)
void HandleReactorEvent(EvalContext *ctx, const Promise *pp, const char *promise_hash, WatcherCheckResult check)
{
assert(pp != NULL);
assert(promiser != NULL);
assert(promise_hash != NULL);

/* cf-reactor keeps the same EvalContext across events. Like an agent run,
* the bundle runs see the time classes of now and the persistent classes
Expand All @@ -304,7 +294,7 @@ void HandleReactorEvent(EvalContext *ctx, const Promise *pp, const char *promise
VariableTable *variables = NULL;
EvalContextSnapshotTake(ctx, &classes, &variables);

ReactorEventParam event = { .promiser = promiser, .check = check };
ReactorEventParam event = { .promise_hash = promise_hash, .check = check };

EvalContextStackPushBundleFrame(ctx, PromiseGetBundle(pp), NULL, false, NULL);
EvalContextStackPushBundleSectionFrame(ctx, pp->parent_section);
Expand Down
13 changes: 7 additions & 6 deletions cf-reactor/reactor_transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
* (see watcher.h) for each of its "events" promises.
*
* Expects no watchers to be registered: on re-read of the policy, the
* watchers of the previous policy must be discarded with
* WatcherRegistryClear() before that policy is destroyed, as they refer to
* its promises.
* watchers of the previous policy must be set aside with
* EventWatcherPause() before that policy is destroyed, as they refer to its
* promises.
*/
void KeepReactorPromises(EvalContext *ctx, const Policy *policy);

Expand All @@ -48,11 +48,12 @@ void KeepReactorPromises(EvalContext *ctx, const Policy *policy);
* failed to check for its event, record a failure instead.
*
* @param pp the unexpanded events promise the watcher was registered for
* @param promiser the expanded promiser the watcher was registered with,
* selecting the iteration of the events promise to keep
* @param promise_hash the hash of the expanded promise the watcher was
* registered with, selecting the iteration of the events
* promise to keep
* @param check whether the event happened or the watcher failed to check for
* it (see WatcherEventFn)
*/
void HandleReactorEvent(EvalContext *ctx, const Promise *pp, const char *promiser, WatcherCheckResult check);
void HandleReactorEvent(EvalContext *ctx, const Promise *pp, const char *promise_hash, WatcherCheckResult check);

#endif
Loading
Loading