From 554d650db8dcfcbcabc3187ca1d8bf2f64cebf47 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Wed, 2 Apr 2014 18:01:24 +0200 Subject: [PATCH] hpux: provide a replacement for daemon() for HP-UX --- configure.ac | 11 +++++++- src/compat/compat.h | 4 +++ src/compat/daemon.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/compat/daemon.c diff --git a/configure.ac b/configure.ac index f4f8e9e7..e9527dfe 100644 --- a/configure.ac +++ b/configure.ac @@ -111,10 +111,19 @@ AC_CONFIG_LIBOBJ_DIR([src/compat]) AC_FUNC_MALLOC AC_FUNC_REALLOC AC_FUNC_FORK +# setproctitle (maybe through libbsd) AC_SEARCH_LIBS([setproctitle], [util bsd]) AC_REPLACE_FUNCS([setproctitle]) AC_CHECK_FUNCS([setproctitle_init]) -AC_REPLACE_FUNCS([strlcpy strnlen strndup fgetln asprintf vsyslog]) +# Other functions +AC_REPLACE_FUNCS([strlcpy + strnlen + strndup + fgetln + asprintf + vsyslog + daemon]) +# Optional functions AC_CHECK_FUNCS([setresuid setresgid]) case " $LIBS " in diff --git a/src/compat/compat.h b/src/compat/compat.h index e4092346..ab6a2864 100644 --- a/src/compat/compat.h +++ b/src/compat/compat.h @@ -54,6 +54,10 @@ int asprintf (char **, const char *, ...) __attribute__ ((format (printf, 2, 3)) void vsyslog(int, const char *, va_list) __attribute__ ((format (printf, 2, 0))); #endif +#if !HAVE_DAEMON +int daemon(int, int); +#endif + #if !HAVE_STRLCPY size_t strlcpy(char *, const char *, size_t); #endif diff --git a/src/compat/daemon.c b/src/compat/daemon.c new file mode 100644 index 00000000..18f3d8d5 --- /dev/null +++ b/src/compat/daemon.c @@ -0,0 +1,66 @@ +/* $OpenBSD: daemon.c,v 1.6 2005/08/08 08:05:33 espie 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. + */ + +/* OPENBSD ORIGINAL: lib/libc/gen/daemon.c */ + +#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("/dev/null", 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); +} -- 2.39.5