Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ matrix:
env: JOB_TYPE=compile_and_unit_test COVERAGE=no
compiler: clang
- os: linux
before_script:
- export CFLAGS="-g3 -O0"
sudo: required
env: JOB_TYPE=acceptance_tests_common
# The unsafe acceptance tests don't work with SIMFS which is the default
Expand Down
15 changes: 15 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,18 @@ and run
ln -s contrib/dir-locals.el .dir-locals.el

in the top directory of the source code checkout.

atexit() and Windows
--------------------

On Windows the atexit function works but the functions registered there are
executed after or concurrently with DLL unloading. If registered functions
rely on DLLs such as pthreads to do locking/unlocking deadlock scenarios can
occur when exit is called.

In order to make behavior more explicit and predictable we migrated to always
using a homegrown atexit system. RegisterAtExitFunction instead of atexit and
CallAtExitFunctionsAndExit instead of exit.

If `_Exit` or `_exit` need to be called that is fine as they don't call atexit or
cleanup functions.
40 changes: 21 additions & 19 deletions cf-agent/cf-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#include <cfnet.h>

#include <mod_common.h>
#include <cleanup.h>

#ifdef HAVE_AVAHI_CLIENT_CLIENT_H
#ifdef HAVE_AVAHI_COMMON_ADDRESS_H
Expand Down Expand Up @@ -253,7 +254,7 @@ int main(int argc, char *argv[])
if (!policy)
{
Log(LOG_LEVEL_ERR, "Error reading CFEngine policy. Exiting...");
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}

GenericAgentPostLoadInit(ctx);
Expand Down Expand Up @@ -308,7 +309,7 @@ int main(int argc, char *argv[])
xmlCleanupParser();
#endif

return ret;
DoCleanupAndExit(ret);
}

/*******************************************************************/
Expand Down Expand Up @@ -372,30 +373,30 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
if (!BootstrapAllowed())
{
Log(LOG_LEVEL_ERR, "Not enough privileges to bootstrap CFEngine");
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}

if(strcmp(optarg, ":avahi") == 0)
{
if(!HasAvahiSupport())
{
Log(LOG_LEVEL_ERR, "Avahi support is not built in, please see options to the configure script and rebuild CFEngine");
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}

int err = AutomaticBootstrap(config);
if (err < 0)
{
Log(LOG_LEVEL_ERR, "Automatic bootstrap failed, error code '%d'", err);
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}
break;
}

if(IsLoopbackAddress(optarg))
{
Log(LOG_LEVEL_ERR, "Cannot bootstrap to a loopback address");
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}

// temporary assure that network functions are working
Expand All @@ -412,7 +413,7 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
Log(LOG_LEVEL_ERR,
"Could not resolve hostname '%s', unable to bootstrap",
host);
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}

CloseNetwork();
Expand Down Expand Up @@ -492,15 +493,15 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
GenericAgentWriteVersion(w);
FileWriterDetach(w);
}
exit(EXIT_SUCCESS);
DoCleanupAndExit(EXIT_SUCCESS);

case 'h':
{
Writer *w = FileWriter(stdout);
WriterWriteHelp(w, "cf-agent", OPTIONS, HINTS, true, NULL);
FileWriterDetach(w);
}
exit(EXIT_SUCCESS);
DoCleanupAndExit(EXIT_SUCCESS);

case 'M':
{
Expand All @@ -511,7 +512,7 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
OPTIONS, HINTS,
true);
FileWriterDetach(out);
exit(EXIT_SUCCESS);
DoCleanupAndExit(EXIT_SUCCESS);
}

case 'x':
Expand All @@ -530,12 +531,12 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
AgentDiagnosticsRunAllChecksNova(workdir, out, &AgentDiagnosticsRun, &AgentDiagnosticsResultNew);
FileWriterDetach(out);
}
exit(EXIT_SUCCESS);
DoCleanupAndExit(EXIT_SUCCESS);

case 'C':
if (!GenericAgentConfigParseColor(config, optarg))
{
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}
break;

Expand Down Expand Up @@ -570,7 +571,8 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
bool ret = LogEnableModulesFromString(optarg);
if (!ret)
{
exit(EXIT_FAILURE);
/* --log-modules=help prints out usage so we must exit */
DoCleanupAndExit(EXIT_SUCCESS);
}
}
else if (strcmp(OPTIONS[longopt_idx].name, "show-evaluated-classes") == 0)
Expand Down Expand Up @@ -601,23 +603,23 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
WriterWriteHelp(w, "cf-agent", OPTIONS, HINTS, true, NULL);
FileWriterDetach(w);
}
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}
}

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

if (option_trust_server &&
config->agent_specific.agent.bootstrap_argument == NULL)
{
Log(LOG_LEVEL_ERR,
"Option --trust-server can only be used when bootstrapping");
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}

FreeFixedStringArray(argc_new, argv_new);
Expand Down Expand Up @@ -1445,7 +1447,7 @@ static void CheckAgentAccess(const Rlist *list, const Policy *policy)
if (!access)
{
Log(LOG_LEVEL_ERR, "File '%s' is not owned by an authorized user (security exception)", input_file);
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}
}
else if (CFPARANOID && IsPrivileged())
Expand All @@ -1454,7 +1456,7 @@ static void CheckAgentAccess(const Rlist *list, const Policy *policy)
{
Log(LOG_LEVEL_ERR, "File '%s' is not owned by uid %ju (security exception)", input_file,
(uintmax_t)getuid());
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}
}
}
Expand All @@ -1463,7 +1465,7 @@ static void CheckAgentAccess(const Rlist *list, const Policy *policy)
}

Log(LOG_LEVEL_ERR, "You are denied access to run this policy");
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}
#endif /* !__MINGW32__ */

Expand Down
4 changes: 2 additions & 2 deletions cf-agent/findhub.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

#include <findhub.h>
#include <atexit.h>
#include <cleanup.h>
#include <string_lib.h>
#include <misc_lib.h>
#include <logging.h>
Expand Down Expand Up @@ -162,7 +162,7 @@ int ListHubs(List **list)
spoll = NULL;
avahi_handle = NULL;

RegisterAtExitFunction(&AtExitDlClose);
RegisterCleanupFunction(&AtExitDlClose);

if (loadavahi() == -1)
{
Expand Down
3 changes: 2 additions & 1 deletion cf-agent/verify_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <eval_context.h>
#include <retcode.h>
#include <timeout.h>
#include <cleanup.h>

typedef enum
{
Expand Down Expand Up @@ -462,7 +463,7 @@ static ActionResult RepairExec(EvalContext *ctx, Attributes a,
if ((a.transaction.background) && outsourced)
{
Log(LOG_LEVEL_VERBOSE, "Backgrounded command '%s' is done - exiting", cmdline);
exit(EXIT_SUCCESS);
DoCleanupAndExit(EXIT_SUCCESS);
}
#endif /* !__MINGW32__ */

Expand Down
17 changes: 9 additions & 8 deletions cf-execd/cf-execd.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <time_classes.h>
#include <loading.h>
#include <printsize.h>
#include <cleanup.h>

#include <cf-windows-functions.h>

Expand Down Expand Up @@ -138,7 +139,7 @@ int main(int argc, char *argv[])
if (!policy)
{
Log(LOG_LEVEL_ERR, "Error reading CFEngine policy. Exiting...");
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}

GenericAgentPostLoadInit(ctx);
Expand Down Expand Up @@ -270,15 +271,15 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
GenericAgentWriteVersion(w);
FileWriterDetach(w);
}
exit(EXIT_SUCCESS);
DoCleanupAndExit(EXIT_SUCCESS);

case 'h':
{
Writer *w = FileWriter(stdout);
WriterWriteHelp(w, "cf-execd", OPTIONS, HINTS, true, NULL);
FileWriterDetach(w);
}
exit(EXIT_SUCCESS);
DoCleanupAndExit(EXIT_SUCCESS);

case 'M':
{
Expand All @@ -289,17 +290,17 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
OPTIONS, HINTS,
true);
FileWriterDetach(out);
exit(EXIT_SUCCESS);
DoCleanupAndExit(EXIT_SUCCESS);
}

case 'x':
Log(LOG_LEVEL_ERR, "Self-diagnostic functionality is retired.");
exit(EXIT_SUCCESS);
DoCleanupAndExit(EXIT_SUCCESS);

case 'C':
if (!GenericAgentConfigParseColor(config, optarg))
{
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}
break;

Expand All @@ -313,15 +314,15 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv)
WriterWriteHelp(w, "cf-execd", OPTIONS, HINTS, true, NULL);
FileWriterDetach(w);
}
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);

}
}

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

return config;
Expand Down
4 changes: 2 additions & 2 deletions cf-monitord/env_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include <probes.h> /* MonOtherInit,MonOtherGatherData */
#include <history.h> /* HistoryUpdate */
#include <monitoring.h> /* GetObservable */

#include <cleanup.h>

/*****************************************************************************/
/* Globals */
Expand Down Expand Up @@ -372,7 +372,7 @@ static Averages EvalAvQ(EvalContext *ctx, char *t)
if ((lastweek_vals = GetCurrentAverages(t)) == NULL)
{
Log(LOG_LEVEL_ERR, "Error reading average database");
exit(EXIT_FAILURE);
DoCleanupAndExit(EXIT_FAILURE);
}

/* Discard any apparently anomalous behaviour before renormalizing database */
Expand Down
Loading