Path: blob/main/crypto/krb5/src/lib/krad/t_daemon.h
39536 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* lib/krad/t_daemon.h - Daemonization helper for RADIUS test programs */2/*3* Copyright 2013 Red Hat, Inc. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions are met:7*8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in13* the documentation and/or other materials provided with the14* distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS17* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED18* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A19* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER20* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,21* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,22* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR23* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS26* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/2829#ifndef T_DAEMON_H_30#define T_DAEMON_H_3132#include "t_test.h"33#include <libgen.h>34#include <sys/types.h>35#include <sys/stat.h>36#include <fcntl.h>3738static pid_t daemon_pid;3940static void41daemon_stop(void)42{43if (daemon_pid == 0)44return;45kill(daemon_pid, SIGTERM);46waitpid(daemon_pid, NULL, 0);47daemon_pid = 0;48}4950static krb5_boolean51daemon_start(int argc, const char **argv)52{53int fds[2];54char buf[1];5556if (argc != 3 || argv == NULL)57return FALSE;5859if (daemon_pid != 0)60return TRUE;6162if (pipe(fds) != 0)63return FALSE;6465/* Start the child process with the write end of the pipe as stdout. */66daemon_pid = fork();67if (daemon_pid == 0) {68dup2(fds[1], STDOUT_FILENO);69close(fds[0]);70close(fds[1]);71exit(execlp(argv[1], argv[1], argv[2], NULL));72}73close(fds[1]);7475/* The child will write a sentinel character when it is listening. */76if (read(fds[0], buf, 1) != 1 || *buf != '~')77return FALSE;78close(fds[0]);7980atexit(daemon_stop);81return TRUE;82}8384#endif /* T_DAEMON_H_ */858687