|
Packit |
fd8b60 |
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
Packit |
fd8b60 |
/* lib/krad/t_daemon.h - Daemonization helper for RADIUS test programs */
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Copyright 2013 Red Hat, Inc. All rights reserved.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
fd8b60 |
* modification, are permitted provided that the following conditions are met:
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
fd8b60 |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
fd8b60 |
* notice, this list of conditions and the following disclaimer in
|
|
Packit |
fd8b60 |
* the documentation and/or other materials provided with the
|
|
Packit |
fd8b60 |
* distribution.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
Packit |
fd8b60 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
Packit |
fd8b60 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
Packit |
fd8b60 |
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
Packit |
fd8b60 |
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
Packit |
fd8b60 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
Packit |
fd8b60 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
Packit |
fd8b60 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
Packit |
fd8b60 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
Packit |
fd8b60 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
Packit |
fd8b60 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#ifndef T_DAEMON_H_
|
|
Packit |
fd8b60 |
#define T_DAEMON_H_
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#include "t_test.h"
|
|
Packit |
fd8b60 |
#include <libgen.h>
|
|
Packit |
fd8b60 |
#include <sys/types.h>
|
|
Packit |
fd8b60 |
#include <sys/stat.h>
|
|
Packit |
fd8b60 |
#include <fcntl.h>
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static pid_t daemon_pid;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static void
|
|
Packit |
fd8b60 |
daemon_stop(void)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
if (daemon_pid == 0)
|
|
Packit |
fd8b60 |
return;
|
|
Packit |
fd8b60 |
kill(daemon_pid, SIGTERM);
|
|
Packit |
fd8b60 |
waitpid(daemon_pid, NULL, 0);
|
|
Packit |
fd8b60 |
daemon_pid = 0;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static krb5_boolean
|
|
Packit |
fd8b60 |
daemon_start(int argc, const char **argv)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
int fds[2];
|
|
Packit |
fd8b60 |
char buf[1];
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (argc != 3 || argv == NULL)
|
|
Packit |
fd8b60 |
return FALSE;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (daemon_pid != 0)
|
|
Packit |
fd8b60 |
return TRUE;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (pipe(fds) != 0)
|
|
Packit |
fd8b60 |
return FALSE;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Start the child process with the write end of the pipe as stdout. */
|
|
Packit |
fd8b60 |
daemon_pid = fork();
|
|
Packit |
fd8b60 |
if (daemon_pid == 0) {
|
|
Packit |
fd8b60 |
dup2(fds[1], STDOUT_FILENO);
|
|
Packit |
fd8b60 |
close(fds[0]);
|
|
Packit |
fd8b60 |
close(fds[1]);
|
|
Packit |
fd8b60 |
exit(execlp(argv[1], argv[1], argv[2], NULL));
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
close(fds[1]);
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* The child will write a sentinel character when it is listening. */
|
|
Packit |
fd8b60 |
if (read(fds[0], buf, 1) != 1 || *buf != '~')
|
|
Packit |
fd8b60 |
return FALSE;
|
|
Packit |
fd8b60 |
close(fds[0]);
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
atexit(daemon_stop);
|
|
Packit |
fd8b60 |
return TRUE;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#endif /* T_DAEMON_H_ */
|