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

@@ -20,7 +20,9 @@ if HAVE_DBHEADER
AM_CPPFLAGS += -I$(DBHEADER)
endif
noinst_PROGRAMS = snprintf-test resolve-test rkpty
noinst_PROGRAMS = snprintf-test resolve-test rkpty test-detach
CHECK_LOCAL = snprintf-test resolve-test rkpty make-roken
check_PROGRAMS = \
base64-test \
@@ -50,6 +52,8 @@ parse_reply_test_CFLAGS = -DTEST_RESOLVE
test_readenv_SOURCES = test-readenv.c test-mem.c
test_detach_SOURCES = test-detach.c
rkpty_LDADD = $(LIB_openpty) $(LDADD)
parse_time_test_SOURCES = parse_time-test.c test-mem.c
@@ -75,6 +79,7 @@ libroken_la_SOURCES = \
concat.c \
cloexec.c \
ct.c \
detach.c \
doxygen.c \
dumpdata.c \
environment.c \

View File

@@ -39,6 +39,7 @@ libroken_la_OBJS = \
$(OBJ)\concat.obj \
$(OBJ)\cloexec.obj \
$(OBJ)\ct.obj \
$(OBJ)\detach.obj \
$(OBJ)\dirent.obj \
$(OBJ)\dlfcn_w32.obj \
$(OBJ)\dumpdata.obj \
@@ -189,6 +190,7 @@ TEST_PROGS = \
$(OBJ)\getaddrinfo-test.exe \
$(OBJ)\getifaddrs-test.exe \
$(OBJ)\hex-test.exe \
$(OBJ)\test-detach.exe \
$(OBJ)\test-readenv.exe \
$(OBJ)\parse_bytes-test.exe \
$(OBJ)\parse_reply-test.exe \
@@ -252,6 +254,9 @@ $(OBJ)\hex-test.exe: $(OBJ)\hex-test.obj $(LIBROKEN)
$(OBJ)\parse_bytes-test.exe: $(OBJ)\parse_bytes-test.obj $(LIBROKEN)
$(EXECONLINK)
$(OBJ)\test-detach.exe: $(OBJ)\test-detach.obj $(OBJ)\detach.obj $(LIBROKEN)
$(EXECONLINK)
$(OBJ)\dirent-test.exe: $(OBJ)\dirent-test.obj $(LIBROKEN)
$(EXECONLINK)

View File

@@ -72,7 +72,7 @@ daemon(int nochdir, int noclose)
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > 2)
close (fd);
(void) close(fd);
}
return (0);
}

211
lib/roken/detach.c Normal file
View File

@@ -0,0 +1,211 @@
/*-
* Copyright (c) 2015
* Cryptonector LLC. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Cryptonector LLC may not be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#ifdef WIN32
#include <io.h>
#include <stdlib.h>
#else
#include <unistd.h>
#endif
#include "roken.h"
#ifdef WIN32
#define dup2 _dup2
#endif
static int pipefds[2] = {-1, -1};
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
roken_detach_prep(int argc, char **argv, char *special_arg)
{
pid_t child;
char buf[1];
ssize_t bytes;
int status;
pipefds[0] = -1;
pipefds[1] = -1;
#ifdef WIN32
if (_pipe(pipefds, 4, O_BINARY) == -1)
err(1, "failed to setup to detach daemon (_pipe failed)");
#else
if (pipe(pipefds) == -1)
err(1, "failed to setup to detach daemon (pipe failed)");
#endif
#ifndef WIN32
fflush(stdout);
child = fork();
#else
{
intptr_t child_handle;
int write_side;
size_t i;
char *fildes;
char **new_argv;
new_argv = calloc(argc + 2, sizeof(*new_argv));
if (new_argv == NULL)
err(1, "Out of memory");
write_side = _dup(pipefds[1]); /* The new fd will be inherited */
if (write_side == -1)
err(1, "Out of memory");
if (asprintf(&fildes, "%d", write_side) == -1 ||
fildes == NULL)
err(1, "failed to setup to detach daemon (_dup failed)");
new_argv[0] = argv[0];
new_argv[1] = special_arg;
new_argv[2] = fildes;
for (i = 1; argv[i] != NULL; i++)
new_argv[i + 1] = argv[i];
new_argv[argc + 2] = NULL;
_flushall();
child_handle = spawnvp(_P_NOWAIT, argv[0], new_argv);
if (child_handle == -1)
child = (pid_t)-1;
else
child = GetProcessId((HANDLE)child_handle);
}
#endif
if (child == (pid_t)-1)
err(1, "failed to setup to fork daemon (fork failed)");
#ifndef WIN32
if (child == 0) {
int fd;
(void) close(pipefds[0]);
pipefds[0] = -1;
/*
* Keep stdout/stderr for now so output and errors prior to
* detach_finish() can be seen by the user.
*/
fd = open(_PATH_DEVNULL, O_RDWR, 0);
if (fd == -1)
err(1, "failed to open /dev/null");
(void) dup2(fd, STDIN_FILENO);
if (fd > STDERR_FILENO)
(void) close(fd);
return;
}
#endif
(void) close(pipefds[1]);
pipefds[1] = -1;
do {
bytes = read(pipefds[0], buf, sizeof(buf));
} while (bytes == -1 && errno == EINTR);
if (bytes == -1) {
/*
* No need to wait for the process. We've killed it. If it
* doesn't want to exit, we'd have to wait potentially forever,
* but we want to indicate failure to the user as soon as
* possible. A wait with timeout would end the same way
* (attempting to kill the process).
*/
err(1, "failed to setup daemon child (read from child pipe)");
}
if (bytes == 0) {
warnx("daemon child preparation failed, waiting for child");
status = wait_for_process(child);
if (SE_IS_ERROR(status) || SE_PROCSTATUS(status) != 0)
errx(SE_PROCSTATUS(status),
"daemon child preparation failed (child exited)");
}
_exit(0);
}
#ifdef WIN32
#ifdef dup2
#undef dup2
#endif
#define dup2 _dup2
#endif
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
roken_detach_finish(const char *dir, int daemon_child_fd)
{
char buf[1];
ssize_t bytes;
int fd;
if (pipefds[1] == -1 && daemon_child_fd != -1)
pipefds[1] = daemon_child_fd;
if (pipefds[0] != -1)
(void) close(pipefds[0]);
if (pipefds[1] == -1)
return;
#ifdef HAVE_SETSID
if (setsid() == -1)
err(1, "failed to detach from tty");
#endif
#ifndef WIN32
/*
* Hopefully we've written any pidfiles by now, if they had to be in
* the current directory...
*
* The daemons do re-open logs and so on, therefore this chdir()
* call needs to be optional for testing.
*/
if (dir != NULL && chdir(dir) == -1)
err(1, "failed to chdir to /");
#endif
buf[1] = 0;
do {
bytes = write(pipefds[1], buf, sizeof(buf));
} while (bytes == -1 && errno == EINTR);
if (bytes == -1)
err(1, "failed to signal parent while detaching");
(void) close(pipefds[1]);
if (bytes != sizeof(buf))
errx(1, "failed to signal parent while detaching");
fd = open(_PATH_DEVNULL, O_RDWR, 0);
if (fd == -1)
err(1, "failed to open /dev/null");
/*
* Maybe we should check that our output got written, if redirected
* to a file. File utils normally do this.
*/
(void) dup2(fd, STDOUT_FILENO);
(void) dup2(fd, STDERR_FILENO);
if (fd > 2)
(void) close(fd);
}

View File

@@ -595,7 +595,7 @@ struct getargs args[] = {
int main(int argc, char **argv)
{
int goptind = 0;
while(getarg(args, 5, argc, argv, &goptind))
while (getarg(args, 5, argc, argv, &goptind))
printf("Bad arg: %s\n", argv[goptind]);
printf("flag1 = %d\n", flag1);
printf("flag2 = %d\n", flag2);

View File

@@ -126,7 +126,11 @@
#define O_NOFOLLOW 0
#endif
#ifndef _WIN32
#ifdef _WIN32
#define _PATH_DEVNULL "\\\\.\\NUL"
#else
#ifndef _PATH_DEV
#define _PATH_DEV "/dev/"

View File

@@ -810,6 +810,9 @@ ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL roken_vconcat (char *, size_t, va_list);
ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL
roken_vmconcat (char **, size_t, va_list);
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL roken_detach_prep(int, char **, char *);
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL roken_detach_finish(const char *, int);
ROKEN_LIB_FUNCTION ssize_t ROKEN_LIB_CALL
net_write (rk_socket_t, const void *, size_t);

82
lib/roken/test-detach.c Normal file
View File

@@ -0,0 +1,82 @@
/***********************************************************************
* Copyright (c) 2015, Cryptonector LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
**********************************************************************/
#include <config.h>
#include <sys/types.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifdef WIN32
#include <process.h>
#ifdef getpid
#undef getpid
#endif
#define getpid _getpid
#else
#include <unistd.h>
#endif
#include "roken.h"
int main(int argc, char **argv)
{
char *ends;
long n;
int fd = -1;
if (argc > 1) {
if (argc != 3)
errx(1, "Usage: test-detach [--daemon-child fd]");
fprintf(stderr, "Child started (argv[1] = %s, argv[2] = %s)!\n", argv[1], argv[2]);
errno = 0;
n = strtol(argv[2], &ends, 10);
fd = n;
if (errno != 0)
err(1, "Usage: test-detach [--daemon-child fd]");
if (n < 0 || ends == NULL || *ends != '\0' || n != fd)
errx(1, "Usage: test-detach [--daemon-child fd]");
} else {
fprintf(stderr, "Parent started as %ld\n", (long)getpid());
roken_detach_prep(argc, argv, "--daemon-child");
}
fprintf(stderr, "Now should be the child: %ld\n", (long)getpid());
roken_detach_finish(NULL, fd);
/*
* These printfs will not appear: stderr will have been replaced
* with /dev/null.
*/
fprintf(stderr, "Now should be the child: %ld, wrote to parent\n", (long)getpid());
sleep(5);
fprintf(stderr, "Daemon child done\n");
return 0;
}

View File

@@ -162,6 +162,8 @@ HEIMDAL_ROKEN_1.0 {
rk_warnerr;
rk_xfree;
roken_concat;
roken_detach_prep;
roken_detach_finish;
roken_getaddrinfo_hostspec2;
roken_getaddrinfo_hostspec;
roken_gethostby_setup;

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)