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
74 changes: 72 additions & 2 deletions cf-reactor/cf-reactor.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include <man.h>
#include <cleanup.h>
#include <prototypes3.h>
#include <signals.h> /* HandleSignalsForDaemon, IsPendingTermination */
#include <unistd.h> /* sleep */
#include <signal.h> /* signal, kill */
#include <exec_tools.h>

/*****************************************************************************/
/* Globals */
Expand Down Expand Up @@ -177,6 +181,15 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
return config;
}

static void TerminateReactorEnterprise(int pid)
{
#ifndef __MINGW32__
kill((pid_t) pid, SIGINT);
#else
(void) pid;
#endif /* !__MINGW32__ */
}

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

int main(int argc, char *argv[])
Expand All @@ -185,10 +198,67 @@ int main(int argc, char *argv[])
EvalContext *ctx = EvalContextNew();
GenericAgentConfigApply(ctx, config);

int ret = ReactorEnterpriseMain(NO_FORK);
#ifdef __MINGW32__

if (!NO_FORK)
{
Log(LOG_LEVEL_VERBOSE, "Windows does not support starting processes in the background - starting in foreground");
}

#else /* !__MINGW32__ */
pid_t existing_pid = ReadPID("cf-reactor.pid");
if ((existing_pid != -1) && (kill(existing_pid, 0) == 0))
{
Log(LOG_LEVEL_ERR, "Another instance of cf-reactor is already running, terminating");
return 1;
}

if ((!NO_FORK) && (fork() != 0))
{
Log(LOG_LEVEL_INFO, "cf-reactor: starting");
_exit(EXIT_SUCCESS);
}

if (!NO_FORK)
{
ActAsDaemon();
}

#endif /* !__MINGW32__ */

umask(077);
WritePID("cf-reactor.pid");

signal(SIGINT, HandleSignalsForDaemon);
signal(SIGTERM, HandleSignalsForDaemon);
signal(SIGBUS, HandleSignalsForDaemon);
signal(SIGHUP, HandleSignalsForDaemon);
signal(SIGPIPE, SIG_IGN);
signal(SIGUSR1, HandleSignalsForDaemon);
signal(SIGUSR2, HandleSignalsForDaemon);

int child = ReactorEnterpriseMain();
if (child == -1)
{
return 1;
}

for (int i = 0; !IsPendingTermination(); i++)
{
/* Do something */
Log(LOG_LEVEL_INFO, "cf-reactor: %d", i);
sleep(1);
}

/* child == 0 means in this case that no child process was created.
It is the default value returned by enterprise stubs */
if (child > 0)
{
TerminateReactorEnterprise(child);
}

GenericAgentFinalize(ctx, config);
CallCleanupFunctions();

return ret;
return 0;
}
2 changes: 1 addition & 1 deletion libpromises/enterprise_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ ENTERPRISE_VOID_FUNC_2ARG_DEFINE_STUB(void, Nova_ClassHistoryEnable,
{
}

ENTERPRISE_FUNC_1ARG_DEFINE_STUB(int, ReactorEnterpriseMain, ARG_UNUSED bool, no_fork)
ENTERPRISE_FUNC_0ARG_DEFINE_STUB(int, ReactorEnterpriseMain)
{
Log(LOG_LEVEL_VERBOSE, "Nova extension library is not available.");
Log(LOG_LEVEL_VERBOSE, "Running cf-reactor community edition.");
Expand Down
2 changes: 1 addition & 1 deletion libpromises/prototypes3.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ ENTERPRISE_VOID_FUNC_0ARG_DECLARE(void, ReloadHAConfig);
ENTERPRISE_VOID_FUNC_2ARG_DECLARE(void, Nova_ClassHistoryAddContextName, const StringSet *, list, const char *, context_name);
ENTERPRISE_VOID_FUNC_2ARG_DECLARE(void, Nova_ClassHistoryEnable, StringSet **, list, bool, enable);

ENTERPRISE_FUNC_1ARG_DECLARE(int, ReactorEnterpriseMain, bool, no_fork);
ENTERPRISE_FUNC_0ARG_DECLARE(int, ReactorEnterpriseMain);

/* manual.c */

Expand Down
Loading