From aa0a023cf1af298d0dbfb83d9abfef486131befc Mon Sep 17 00:00:00 2001 From: Victor Moene Date: Tue, 4 Aug 2026 12:49:32 +0200 Subject: [PATCH] Moved cf-reactor binary to core Changelog: Title Ticket: ENT-14275 Signed-off-by: Victor Moene --- .gitignore | 3 + Makefile.am | 1 + cf-reactor/Makefile.am | 52 +++++++++++++ cf-reactor/cf-reactor.c | 168 ++++++++++++++++++++++++++++++++++++++++ configure.ac | 1 + 5 files changed, 225 insertions(+) create mode 100644 cf-reactor/Makefile.am create mode 100644 cf-reactor/cf-reactor.c diff --git a/.gitignore b/.gitignore index b305318e13..ad8b3a2111 100644 --- a/.gitignore +++ b/.gitignore @@ -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 @@ -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 diff --git a/Makefile.am b/Makefile.am index 7313935af8..5d9656c2b9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -46,6 +46,7 @@ SUBDIRS = \ cf-net \ cf-secret \ cf-watchd \ + cf-reactor \ misc \ python \ ext \ diff --git a/cf-reactor/Makefile.am b/cf-reactor/Makefile.am new file mode 100644 index 0000000000..f055f635e2 --- /dev/null +++ b/cf-reactor/Makefile.am @@ -0,0 +1,52 @@ +# +# 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 = @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 diff --git a/cf-reactor/cf-reactor.c b/cf-reactor/cf-reactor.c new file mode 100644 index 0000000000..35f88dbadf --- /dev/null +++ b/cf-reactor/cf-reactor.c @@ -0,0 +1,168 @@ +/* + 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define REACTOR_NOVA_LIBRARY_NAME "cfengine-reactor-nova.so" + +typedef int (*ReactorNovaMainFn)(bool no_fork); + +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, currently only NOTIFY events from PostgreSQL."; + +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'}, + {"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 +}; + +int main(int argc, char *argv[]) +{ + bool no_fork = false; + + extern char *optarg; + int longopt_idx; + int c; + 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 'F': + no_fork = true; + break; + + case 'g': + LogSetGlobalLevelArgOrExit(optarg); + break; + + case 'h': + { + Writer *w = FileWriter(stdout); + WriterWriteHelp(w, &COMPONENT, OPTIONS, HINTS, NULL, false, true); + FileWriterDetach(w); + } + DoCleanupAndExit(EXIT_SUCCESS); + + case 'I': + LogSetGlobalLevel(LOG_LEVEL_INFO); + break; + + case 'l': + LoggingEnableTimestamps(true); + break; + + case 'M': + { + Writer *out = FileWriter(stdout); + ManPageWrite(out, "cf-reactor", time(NULL), + CF_REACTOR_SHORT_DESCRIPTION, + CF_REACTOR_MANPAGE_LONG_DESCRIPTION, + OPTIONS, HINTS, + NULL, false, + true); + FileWriterDetach(out); + DoCleanupAndExit(EXIT_SUCCESS); + } + + case 'v': + LogSetGlobalLevel(LOG_LEVEL_VERBOSE); + no_fork = true; + break; + + case 'V': + { + Writer *w = FileWriter(stdout); + GenericAgentWriteVersion(w); + FileWriterDetach(w); + } + DoCleanupAndExit(EXIT_SUCCESS); + + } + } + + void *handle = extension_library_open(REACTOR_NOVA_LIBRARY_NAME); + if (!handle) + { + Log(LOG_LEVEL_INFO, "Nova extension library '%s' not loaded or not available.", REACTOR_NOVA_LIBRARY_NAME); + Log(LOG_LEVEL_INFO, "Running open-source cf-reactor."); + return 0; + } + + ReactorNovaMainFn ReactorNovaMain = (ReactorNovaMainFn) shlib_load(handle, "ReactorNovaMain"); + if (!ReactorNovaMain) + { + Log(LOG_LEVEL_ERR, "Failed to load ReactorNovaMain symbol from '%s'.", REACTOR_NOVA_LIBRARY_NAME); + extension_library_close(handle); + return 1; + } + + + return ReactorNovaMain(no_fork); +} diff --git a/configure.ac b/configure.ac index f62b2ae986..aeed2e9106 100644 --- a/configure.ac +++ b/configure.ac @@ -2005,6 +2005,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