Skip to content

Commit

Permalink
Restart falco on SIGHUP. (#457)
Browse files Browse the repository at this point in the history
Add a signal handler for SIGHUP that sets a global variable g_restart.

All the real execution of falco was already centralized in a standalone
function falco_init(), so simply exit on g_restart=true and call
falco_init() in a loop that restarts if g_restart is set to true.

Take care to not daemonize more than once and to reset the getopt index
to 1 on restart.

This fixes #432.
  • Loading branch information
mstemm committed Nov 6, 2018
1 parent 53c7e10 commit 6eac49e
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions userspace/falco/falco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ limitations under the License.

bool g_terminate = false;
bool g_reopen_outputs = false;
bool g_restart = false;
bool g_daemonized = false;

//
// Helper functions
Expand All @@ -58,6 +60,11 @@ static void reopen_outputs(int signal)
g_reopen_outputs = true;
}

static void restart_falco(int signal)
{
g_restart = true;
}

//
// Program help
//
Expand Down Expand Up @@ -196,8 +203,9 @@ uint64_t do_inspect(falco_engine *engine,
g_reopen_outputs = false;
}

if (g_terminate)
if (g_terminate || g_restart)
{
falco_logger::log(LOG_INFO, "SIGHUP Received, restarting...\n");
break;
}
else if(rc == SCAP_TIMEOUT)
Expand Down Expand Up @@ -699,6 +707,13 @@ int falco_init(int argc, char **argv)
goto exit;
}

if(signal(SIGHUP, restart_falco) == SIG_ERR)
{
fprintf(stderr, "An error occurred while setting SIGHUP signal handler.\n");
result = EXIT_FAILURE;
goto exit;
}

if (scap_filename.size())
{
inspector->open(scap_filename);
Expand All @@ -721,7 +736,7 @@ int falco_init(int argc, char **argv)

// If daemonizing, do it here so any init errors will
// be returned in the foreground process.
if (daemon) {
if (daemon && !g_daemonized) {
pid_t pid, sid;

pid = fork();
Expand Down Expand Up @@ -772,6 +787,8 @@ int falco_init(int argc, char **argv)
open("/dev/null", O_RDONLY);
open("/dev/null", O_RDWR);
open("/dev/null", O_RDWR);

g_daemonized = true;
}

if(outfile != "")
Expand Down Expand Up @@ -887,5 +904,15 @@ int falco_init(int argc, char **argv)
//
int main(int argc, char **argv)
{
return falco_init(argc, argv);
int rc;

// g_restart will cause the falco loop to exit, but we
// should reload everything and start over.
while((rc = falco_init(argc, argv)) == EXIT_SUCCESS && g_restart)
{
g_restart = false;
optind = 1;
}

return rc;
}

0 comments on commit 6eac49e

Please sign in to comment.