]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
hpux: provide a replacement for daemon() for HP-UX
authorVincent Bernat <vbe@deezer.com>
Wed, 2 Apr 2014 16:01:24 +0000 (18:01 +0200)
committerVincent Bernat <vbe@deezer.com>
Wed, 2 Apr 2014 16:01:24 +0000 (18:01 +0200)
configure.ac
src/compat/compat.h
src/compat/daemon.c [new file with mode: 0644]

index f4f8e9e77aa2965f1b5991dde2b431cd7d87ce36..e9527dfed5ed83f00244998251de2f8612de933a 100644 (file)
@@ -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
index e4092346d4757c8dd00080442e9aaab23bc9c4cf..ab6a28643c71116f7d401ff6807c53baeecf8d74 100644 (file)
@@ -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 (file)
index 0000000..18f3d8d
--- /dev/null
@@ -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 <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+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);
+}