Daemons detach atomically to avoid having to wait

Tests that start daemons have to "wait" for them to start.

This commit makes Heimdal daemons prep to detach (when requested) by
forking early, then having the child signal readiness to the parent when
the child really is ready.  The parent exits only which the child is
ready.  This means that tests will no longer need to wait for daemons.

However, tests will still need a pidfile or such so they can stop the
daemons.

Note that the --detach options should not be used on OS X from launchd,
only from tests.
This commit is contained in:
Nicolas Williams
2015-03-17 16:03:15 -05:00
parent 0778b19c3f
commit b48bed5f42
25 changed files with 522 additions and 197 deletions

View File

@@ -36,29 +36,42 @@
#include "roken.h"
ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL
pid_file_write (const char *progname)
pid_file_write(const char *progname)
{
const char *pidfile_dir = NULL;
char *ret = NULL;
FILE *fp;
if (asprintf (&ret, "%s%s.pid", _PATH_VARRUN, progname) < 0 || ret == NULL)
/*
* Maybe we could have a version of this function (and pidfile())
* where we get a directory from the caller. That would allow us to
* have command-line options for the daemons for this.
*
* For now we use an environment variable.
*/
if (!issuid())
pidfile_dir = getenv("HEIM_PIDFILE_DIR");
if (pidfile_dir == NULL)
pidfile_dir = _PATH_VARRUN;
if (asprintf(&ret, "%s%s.pid", pidfile_dir, progname) < 0 || ret == NULL)
return NULL;
fp = fopen (ret, "w");
fp = fopen(ret, "w");
if (fp == NULL) {
free (ret);
free(ret);
return NULL;
}
fprintf (fp, "%u", (unsigned)getpid());
fclose (fp);
fprintf(fp, "%u", (unsigned)getpid());
fclose(fp);
return ret;
}
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
pid_file_delete (char **filename)
pid_file_delete(char **filename)
{
if (*filename != NULL) {
unlink (*filename);
free (*filename);
unlink(*filename);
free(*filename);
*filename = NULL;
}
}
@@ -69,16 +82,16 @@ static char *pidfile_path;
static void
pidfile_cleanup(void)
{
if(pidfile_path != NULL)
if (pidfile_path != NULL)
pid_file_delete(&pidfile_path);
}
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
pidfile(const char *bname)
{
if(pidfile_path != NULL)
if (pidfile_path != NULL)
return;
if(bname == NULL)
if (bname == NULL)
bname = getprogname();
pidfile_path = pid_file_write(bname);
#if defined(HAVE_ATEXIT)