|
| 1 | +/* |
| 2 | + Copyright 2026 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 | + |
| 26 | +#include <platform.h> |
| 27 | +#include <logging.h> |
| 28 | +#include <stdio.h> |
| 29 | +#include <stdlib.h> |
| 30 | +#include <writer.h> |
| 31 | +#include <config.h> |
| 32 | +#include <generic_agent.h> |
| 33 | +#include <man.h> |
| 34 | +#include <cleanup.h> |
| 35 | +#include <enterprise_extension.h> |
| 36 | + |
| 37 | +#define REACTOR_NOVA_LIBRARY_NAME "cfengine-reactor-nova.so" |
| 38 | + |
| 39 | +typedef int (*ReactorNovaMainFn)(bool no_fork); |
| 40 | + |
| 41 | +static const Component COMPONENT = |
| 42 | +{ |
| 43 | + .name = "cf-reactor", |
| 44 | + .website = CF_WEBSITE, |
| 45 | + .copyright = CF_COPYRIGHT |
| 46 | +}; |
| 47 | + |
| 48 | +static const char *const CF_REACTOR_SHORT_DESCRIPTION = "CFEngine event reaction daemon"; |
| 49 | + |
| 50 | +static const char *const CF_REACTOR_MANPAGE_LONG_DESCRIPTION = |
| 51 | + "cf-reactor is a daemon reacting on events, currently only NOTIFY events from PostgreSQL."; |
| 52 | + |
| 53 | +static const struct option OPTIONS[] = |
| 54 | +{ |
| 55 | + {"debug", no_argument, 0, 'd'}, |
| 56 | + {"no-fork", no_argument, 0, 'F'}, |
| 57 | + {"log-level", required_argument, 0, 'g'}, |
| 58 | + {"help", no_argument, 0, 'h'}, |
| 59 | + {"inform", no_argument, 0, 'I'}, |
| 60 | + {"timestamp", no_argument, 0, 'l'}, |
| 61 | + {"verbose", no_argument, 0, 'v'}, |
| 62 | + {"version", no_argument, 0, 'V'}, |
| 63 | + {NULL, 0, 0, '\0'} |
| 64 | +}; |
| 65 | + |
| 66 | +static const char *const HINTS[] = |
| 67 | +{ |
| 68 | + "Enable debugging output and run in foreground", |
| 69 | + "Run as a foreground process (do not fork)", |
| 70 | + "Specify how detailed logs should be. Possible values: 'error', 'warning', 'notice', 'info', 'verbose', 'debug'", |
| 71 | + "Print the help message", |
| 72 | + "Print basic information about actions being taken", |
| 73 | + "Log timestamps on each line of log output", |
| 74 | + "Output verbose information about the behaviour of the agent", |
| 75 | + "Output the version of the software", |
| 76 | + NULL |
| 77 | +}; |
| 78 | + |
| 79 | +ENTERPRISE_FUNC_1ARG_DEFINE_STUB(int, ReactorNovaMain, ARG_UNUSED bool, no_fork) |
| 80 | +{ |
| 81 | + Log(LOG_LEVEL_INFO, "Nova extension library is not available."); |
| 82 | + Log(LOG_LEVEL_INFO, "Running open-source cf-reactor."); |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +int main(int argc, char *argv[]) |
| 87 | +{ |
| 88 | + bool no_fork = false; |
| 89 | + |
| 90 | + extern char *optarg; |
| 91 | + int longopt_idx; |
| 92 | + int c; |
| 93 | + while ((c = getopt_long(argc, argv, "dFg:hIlMvV", |
| 94 | + OPTIONS, &longopt_idx)) |
| 95 | + != -1) |
| 96 | + { |
| 97 | + switch (c) |
| 98 | + { |
| 99 | + case 'd': |
| 100 | + LogSetGlobalLevel(LOG_LEVEL_DEBUG); |
| 101 | + no_fork = true; |
| 102 | + break; |
| 103 | + |
| 104 | + case 'F': |
| 105 | + no_fork = true; |
| 106 | + break; |
| 107 | + |
| 108 | + case 'g': |
| 109 | + LogSetGlobalLevelArgOrExit(optarg); |
| 110 | + break; |
| 111 | + |
| 112 | + case 'h': |
| 113 | + { |
| 114 | + Writer *w = FileWriter(stdout); |
| 115 | + WriterWriteHelp(w, &COMPONENT, OPTIONS, HINTS, NULL, false, true); |
| 116 | + FileWriterDetach(w); |
| 117 | + } |
| 118 | + DoCleanupAndExit(EXIT_SUCCESS); |
| 119 | + |
| 120 | + case 'I': |
| 121 | + LogSetGlobalLevel(LOG_LEVEL_INFO); |
| 122 | + break; |
| 123 | + |
| 124 | + case 'l': |
| 125 | + LoggingEnableTimestamps(true); |
| 126 | + break; |
| 127 | + |
| 128 | + case 'M': |
| 129 | + { |
| 130 | + Writer *out = FileWriter(stdout); |
| 131 | + ManPageWrite(out, "cf-reactor", time(NULL), |
| 132 | + CF_REACTOR_SHORT_DESCRIPTION, |
| 133 | + CF_REACTOR_MANPAGE_LONG_DESCRIPTION, |
| 134 | + OPTIONS, HINTS, |
| 135 | + NULL, false, |
| 136 | + true); |
| 137 | + FileWriterDetach(out); |
| 138 | + DoCleanupAndExit(EXIT_SUCCESS); |
| 139 | + } |
| 140 | + |
| 141 | + case 'v': |
| 142 | + LogSetGlobalLevel(LOG_LEVEL_VERBOSE); |
| 143 | + no_fork = true; |
| 144 | + break; |
| 145 | + |
| 146 | + case 'V': |
| 147 | + { |
| 148 | + Writer *w = FileWriter(stdout); |
| 149 | + GenericAgentWriteVersion(w); |
| 150 | + FileWriterDetach(w); |
| 151 | + } |
| 152 | + DoCleanupAndExit(EXIT_SUCCESS); |
| 153 | + |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return ReactorNovaMain(no_fork); |
| 158 | +} |
0 commit comments