Skip to content
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ stamp-h1
/cf-testd/Makefile
/cf-upgrade/Makefile
/cf-watchd/Makefile
/cf-reactor/Makefile
/contrib/vagrant-ci/centos-9s-x64/Makefile
/examples/Makefile
/ext/Makefile
Expand Down Expand Up @@ -115,6 +116,8 @@ xml_tmp_suite
/cf-upgrade/cf-upgrade.exe
/cf-watchd/cf-watchd
/cf-watchd/cf-watchd.exe
/cf-reactor/cf-reactor
/cf-reactor/cf-reactor.exe
/ext/rpmvercmp
/ext/rpmvercmp.exe

Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ SUBDIRS = \
cf-net \
cf-secret \
cf-watchd \
cf-reactor \
misc \
python \
ext \
Expand Down
54 changes: 54 additions & 0 deletions cf-reactor/Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# Copyright 2026 Northern.tech AS
#
# This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
#
# To the extent this program is licensed as part of the Enterprise
# versions of CFEngine, the applicable Commercial Open Source License
# (COSL) may apply to this file if you as a licensee so wish it. See
# included file COSL.txt.
#
noinst_LTLIBRARIES = libcf-reactor.la

AM_CPPFLAGS = -I$(srcdir)/../libpromises -I$(srcdir)/../libntech/libutils \
-I$(srcdir)/../libcfecompat \
-I$(srcdir)/../libcfnet \
$(OPENSSL_CPPFLAGS) \
$(PCRE2_CPPFLAGS) \
$(ENTERPRISE_CPPFLAGS)

AM_CFLAGS = $(CF3_CFLAGS) \
$(DEBUG_CFLAGS) \
$(OPENSSL_CFLAGS) \
$(ENTERPRISE_CFLAGS)

libcf_reactor_la_LIBADD = ../libpromises/libpromises.la

libcf_reactor_la_SOURCES = \
cf-reactor.c

if !BUILTIN_EXTENSIONS
bin_PROGRAMS = cf-reactor
cf_reactor_LDADD = libcf-reactor.la
cf_reactor_SOURCES =
endif

CLEANFILES = *.gcno *.gcda

#
# Some basic clean ups
#
MOSTLYCLEANFILES = *~ *.orig *.rej
195 changes: 195 additions & 0 deletions cf-reactor/cf-reactor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
Copyright 2026 Northern.tech AS

This file is part of CFEngine 3 - written and maintained by Northern.tech AS.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA

To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/


#include <platform.h>
#include <logging.h>
#include <eval_context.h>
#include <stdio.h>
#include <stdlib.h>
#include <writer.h>
#include <config.h>
#include <generic_agent.h>
#include <man.h>
#include <cleanup.h>
#include <prototypes3.h>

/*****************************************************************************/
/* Globals */
/*****************************************************************************/

int NO_FORK = false;

/*******************************************************************/
/* Command line options */
/*******************************************************************/

static const Component COMPONENT =
{
.name = "cf-reactor",
.website = CF_WEBSITE,
.copyright = CF_COPYRIGHT
};

static const char *const CF_REACTOR_SHORT_DESCRIPTION = "CFEngine event reaction daemon";

static const char *const CF_REACTOR_MANPAGE_LONG_DESCRIPTION =
"cf-reactor is a daemon reacting on events. It watches specific events defined in the policy "
"and runs policy code on reaction to these events.";

static const struct option OPTIONS[] =
{
{"debug", no_argument, 0, 'd'},
{"no-fork", no_argument, 0, 'F'},
{"log-level", required_argument, 0, 'g'},
{"help", no_argument, 0, 'h'},
{"inform", no_argument, 0, 'I'},
{"timestamp", no_argument, 0, 'l'},
{"man", no_argument, 0, 'M'},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{NULL, 0, 0, '\0'}
};

static const char *const HINTS[] =
{
"Enable debugging output and run in foreground",
"Run as a foreground process (do not fork)",
"Specify how detailed logs should be. Possible values: 'error', 'warning', 'notice', 'info', 'verbose', 'debug'",
"Print the help message",
"Print basic information about actions being taken",
"Log timestamps on each line of log output",
"Output verbose information about the behaviour of the agent",
"Output the version of the software",
NULL
};

static GenericAgentConfig *CheckOpts(int argc, char **argv)
{
extern char *optarg;
int c;
GenericAgentConfig *config = GenericAgentConfigNewDefault(AGENT_TYPE_REACTOR, GetTTYInteractive());

int longopt_idx;
while ((c = getopt_long(argc, argv, "dFg:hIlMvV",
OPTIONS, &longopt_idx)) != -1)
{
switch (c)
{
case 'd':
LogSetGlobalLevel(LOG_LEVEL_DEBUG);
NO_FORK = true;
break;

case 'I':
LogSetGlobalLevel(LOG_LEVEL_INFO);
break;

case 'v':
LogSetGlobalLevel(LOG_LEVEL_VERBOSE);
NO_FORK = true;
break;

case 'g':
LogSetGlobalLevelArgOrExit(optarg);
break;

case 'F':
NO_FORK = true;
break;

case 'V':
{
Writer *w = FileWriter(stdout);
GenericAgentWriteVersion(w);
FileWriterDetach(w);
}
DoCleanupAndExit(EXIT_SUCCESS);

case 'h':
{
Writer *w = FileWriter(stdout);
WriterWriteHelp(w, &COMPONENT, OPTIONS, HINTS, NULL, false, true);
FileWriterDetach(w);
}
DoCleanupAndExit(EXIT_SUCCESS);

case 'M':
{
Writer *out = FileWriter(stdout);
ManPageWrite(out, "cf-watchd", time(NULL),
CF_REACTOR_SHORT_DESCRIPTION,
CF_REACTOR_MANPAGE_LONG_DESCRIPTION,
OPTIONS, HINTS,
NULL, false,
true);
FileWriterDetach(out);
DoCleanupAndExit(EXIT_SUCCESS);
}

case 'l':
LoggingEnableTimestamps(true);
break;

/* long options only */
case 0:
{
// const char *const option_name = OPTIONS[longopt_idx].name;
break;
}

default:
{
Writer *w = FileWriter(stdout);
WriterWriteHelp(w, &COMPONENT, OPTIONS, HINTS, NULL, false, true);
FileWriterDetach(w);
}
DoCleanupAndExit(EXIT_FAILURE);
}
}

if (!GenericAgentConfigParseArguments(config, argc - optind, argv + optind))
{
Log(LOG_LEVEL_ERR, "Too many arguments");
DoCleanupAndExit(EXIT_FAILURE);
}

return config;
}

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

int main(int argc, char *argv[])
{
GenericAgentConfig *config = CheckOpts(argc, argv);
EvalContext *ctx = EvalContextNew();
GenericAgentConfigApply(ctx, config);

int ret = ReactorEnterpriseMain(NO_FORK);

GenericAgentFinalize(ctx, config);
CallCleanupFunctions();

return ret;
}
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,7 @@ AC_CONFIG_FILES([Makefile
cf-net/Makefile
cf-secret/Makefile
cf-watchd/Makefile
cf-reactor/Makefile
config.post.h
contrib/vagrant-ci/centos-9s-x64/Makefile
misc/Makefile
Expand Down
1 change: 1 addition & 0 deletions libpromises/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ libpromises_la_SOURCES = \
mod_knowledge.c mod_knowledge.h \
mod_users.c mod_users.h \
mod_watch.c mod_watch.h \
mod_reactor.c mod_reactor.h \
modes.c \
monitoring_read.c monitoring_read.h \
ornaments.c ornaments.h \
Expand Down
2 changes: 2 additions & 0 deletions libpromises/cf3.defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ typedef enum
#define CF_KEYGEN "keygenerator"
#define CF_HUBC "hub"
#define CF_WATCHC "watch"
#define CF_REACTORC "reactor"

typedef enum
{
Expand All @@ -411,6 +412,7 @@ typedef enum
AGENT_TYPE_KEYGEN,
AGENT_TYPE_HUB,
AGENT_TYPE_WATCH,
AGENT_TYPE_REACTOR,
AGENT_TYPE_NOAGENT
} AgentType;

Expand Down
1 change: 1 addition & 0 deletions libpromises/constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const char *const CF_AGENTTYPES[] = /* see enum cfagenttype */
CF_KEYGEN,
CF_HUBC,
CF_WATCHC,
CF_REACTORC,
"<notype>",
};

Expand Down
7 changes: 7 additions & 0 deletions libpromises/enterprise_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,10 @@ ENTERPRISE_VOID_FUNC_2ARG_DEFINE_STUB(void, Nova_ClassHistoryEnable,
ARG_UNUSED bool, enable)
{
}

ENTERPRISE_FUNC_1ARG_DEFINE_STUB(int, ReactorEnterpriseMain, ARG_UNUSED bool, no_fork)
{
Log(LOG_LEVEL_VERBOSE, "Nova extension library is not available.");
Log(LOG_LEVEL_VERBOSE, "Running open-source cf-reactor.");
return 0;
}
2 changes: 2 additions & 0 deletions libpromises/mod_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <mod_knowledge.h>
#include <mod_users.h>
#include <mod_watch.h>
#include <mod_reactor.h>

#include <conversion.h>
#include <policy.h>
Expand Down Expand Up @@ -551,6 +552,7 @@ const PromiseTypeSyntax *const CF_ALL_PROMISE_TYPES[] =
CF_KNOWLEDGE_PROMISE_TYPES, /* mod_knowledge.c */
CF_USERS_PROMISE_TYPES, /* mod_users.c */
CF_WATCH_PROMISE_TYPES, /* mod_watch.c */
CF_REACTOR_PROMISE_TYPES /* mod_reactor.c */
};

const int CF3_MODULES = (sizeof(CF_ALL_PROMISE_TYPES) / sizeof(CF_ALL_PROMISE_TYPES[0]));
Expand Down
51 changes: 51 additions & 0 deletions libpromises/mod_reactor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2026 Northern.tech AS

This file is part of CFEngine 3 - written and maintained by Northern.tech AS.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA

To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/

#include <mod_reactor.h>

#include <syntax.h>

static const ConstraintSyntax when_constraints[] =
{
CONSTRAINT_SYNTAX_GLOBAL,

/* Row models */
ConstraintSyntaxNewStringList("files_deleted", CF_ANYSTRING, "List of files to react for on deletion", SYNTAX_STATUS_NORMAL),
ConstraintSyntaxNewNull()
};

static const BodySyntax when_body = BodySyntaxNew("when", when_constraints, NULL, SYNTAX_STATUS_NORMAL);

static const ConstraintSyntax CF_EVENT_BODIES[] =
{
ConstraintSyntaxNewBody("when", &when_body, "Event to react to", SYNTAX_STATUS_NORMAL),
ConstraintSyntaxNewBundle("then", "Bundle to run on event", SYNTAX_STATUS_NORMAL),
ConstraintSyntaxNewNull()
};

const PromiseTypeSyntax CF_REACTOR_PROMISE_TYPES[] =
{
PromiseTypeSyntaxNew("reactor", "events", CF_EVENT_BODIES, NULL, SYNTAX_STATUS_NORMAL),
PromiseTypeSyntaxNewNull()
};
Loading
Loading