Skip site navigation (1)Skip section navigation (2)
Date:      16 Jul 2003 10:02:19 -0500
From:      Guy Helmer <ghelmer@palisadesys.com>
To:        freebsd-audit@freebsd.org
Subject:   Patch for lib/libc/gen/daemon.[3c] to resolve PR 25462
Message-ID:  <1058367738.35732.7.camel@volans>

next in thread | raw e-mail | index | archive | help
I'd like to offer this patch for review to see whether anyone objects or
whether it contains style issues.  It resolves the problem where
daemon(3) can cause a SIGHUP that kills the daemon process under certain
circumstances.  I've been running this patch on my 5-current machine for
a couple of months without any problems.

Guy
ghelmer@freebsd.org

Index: lib/libc/gen/daemon.3
===================================================================
RCS file: /home/ncvs/src/lib/libc/gen/daemon.3,v
retrieving revision 1.12
diff -u -r1.12 daemon.3
--- lib/libc/gen/daemon.3	16 Jan 2002 15:21:39 -0000	1.12
+++ lib/libc/gen/daemon.3	24 Apr 2003 21:52:06 -0000
@@ -71,12 +71,14 @@
 function may fail and set
 .Va errno
 for any of the errors specified for the library functions
-.Xr fork 2
+.Xr fork 2 ,
+.Xr setsid 2
 and
-.Xr setsid 2 .
+.Xr sigaction 2 .
 .Sh SEE ALSO
 .Xr fork 2 ,
-.Xr setsid 2
+.Xr setsid 2 ,
+.Xr sigaction 2
 .Sh HISTORY
 The
 .Fn daemon
@@ -101,3 +103,17 @@
 .Fn daemon
 before opening any files or sockets, or verify that any file
 descriptors obtained have values greater than 2.
+.Pp
+The
+.Fn daemon
+function temporarily ignores
+.Dv SIGHUP
+and then restores the original settings for
+.Dv SIGHUP
+after calling
+.Xr setsid 2
+to prevent a parent session group leader's calls to
+.Xr fork 2
+and then
+.Xr _exit 2
+from prematurely terminating the child process.
Index: lib/libc/gen/daemon.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/gen/daemon.c,v
retrieving revision 1.5
diff -u -r1.5 daemon.c
--- lib/libc/gen/daemon.c	1 Feb 2002 00:57:29 -0000	1.5
+++ lib/libc/gen/daemon.c	1 May 2003 20:54:29 -0000
@@ -38,8 +38,10 @@
 __FBSDID("$FreeBSD: src/lib/libc/gen/daemon.c,v 1.5 2002/02/01 00:57:29
obrien Exp $");
 
 #include "namespace.h"
+#include <errno.h>
 #include <fcntl.h>
 #include <paths.h>
+#include <signal.h>
 #include <unistd.h>
 #include "un-namespace.h"
 
@@ -48,6 +50,16 @@
 	int nochdir, noclose;
 {
 	int fd;
+	pid_t newgrp;
+	int oerrno;
+	int osa_ok;
+	struct sigaction sa, osa;
+
+	/* A SIGHUP may be thrown when the parent exits below. */
+	sigemptyset(&sa.sa_mask);
+	sa.sa_handler = SIG_IGN;
+	sa.sa_flags = 0;
+	osa_ok = sigaction(SIGHUP, &sa, &osa);
 
 	switch (fork()) {
 	case -1:
@@ -58,7 +70,13 @@
 		_exit(0);
 	}
 
-	if (setsid() == -1)
+	newgrp = setsid();
+	oerrno = errno;
+	if (osa_ok != -1)
+		if (sigaction(SIGHUP, &osa, (struct sigaction *)NULL) == -1)
+			return (-1);	/* Could not restore SIGHUP. */
+	errno = oerrno;
+	if (newgrp == -1)
 		return (-1);
 
 	if (!nochdir)




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1058367738.35732.7.camel>