diff --git a/compat/Makefile.am b/compat/Makefile.am index 8c04840..d4aa3b8 100644 --- a/compat/Makefile.am +++ b/compat/Makefile.am @@ -137,6 +137,12 @@ libcompat_la_SOURCES += getifaddrs_solaris.c endif endif +if !HAVE_DAEMON +if HOST_SOLARIS +libcompat_la_SOURCES += daemon_solaris.c +endif +endif + if !HAVE_ARC4RANDOM_UNIFORM libcompat_la_SOURCES += arc4random_uniform.c endif diff --git a/compat/daemon_solaris.c b/compat/daemon_solaris.c new file mode 100644 index 0000000..79f4264 --- /dev/null +++ b/compat/daemon_solaris.c @@ -0,0 +1,64 @@ +/* $OpenBSD: daemon.c,v 1.7 2010/07/27 22:29:09 marco Exp $ */ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors + * may 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 +#include +#include +#include + +int +daemon(int nochdir, int noclose) +{ + int fd; + + switch (fork()) { + case -1: + return (-1); + case 0: + break; + default: + _exit(0); + } + + if (setsid() == -1) + return (-1); + + if (!nochdir) + (void)chdir("/"); + + if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { + (void)dup2(fd, STDIN_FILENO); + (void)dup2(fd, STDOUT_FILENO); + (void)dup2(fd, STDERR_FILENO); + if (fd > 2) + (void)close(fd); + } + return (0); +} diff --git a/configure.ac b/configure.ac index b1f142e..cfc8aeb 100644 --- a/configure.ac +++ b/configure.ac @@ -101,7 +101,7 @@ LDFLAGS="$LDFLAGS $CLANG_FLAGS" # check functions that are expected to be in libc AC_CHECK_FUNCS([adjfreq ntp_adjtime adjtimex]) AC_CHECK_FUNCS([arc4random arc4random_uniform asprintf explicit_bzero]) -AC_CHECK_FUNCS([closefrom getentropy memmem poll reallocarray]) +AC_CHECK_FUNCS([closefrom daemon getentropy memmem poll reallocarray]) AC_CHECK_FUNCS([setproctitle setgroups]) AC_CHECK_FUNCS([setregid setresgid setreuid setresuid]) AC_CHECK_FUNCS([strlcat strlcpy strtonum sysconf]) @@ -128,6 +128,7 @@ AM_CONDITIONAL([HAVE_ASPRINTF], [test "x$ac_cv_func_asprintf" = xyes]) AM_CONDITIONAL([HAVE_CLOSEFROM], [test "x$ac_cv_func_closefrom" = xyes]) AM_CONDITIONAL([HAVE_CLOCK_GETRES], [test "x$ac_cv_func_clock_getres" = xyes]) AM_CONDITIONAL([HAVE_CLOCK_GETTIME], [test "x$ac_cv_func_clock_gettime" = xyes]) +AM_CONDITIONAL([HAVE_DAEMON], [test "x$ac_cv_func_daemon" = xyes]) AM_CONDITIONAL([HAVE_EXPLICIT_BZERO], [test "x$ac_cv_func_explicit_bzero" = xyes]) AM_CONDITIONAL([HAVE_GETENTROPY], [test "x$ac_cv_func_getentropy" = xyes]) AM_CONDITIONAL([HAVE_IFADDRS_H], [test "x$ac_cv_header_ifaddrs_h" = xyes])