changeset 10547:1a61a72b11d5

PSARC/2009/444 daemon() in libc 4471189 should have 4.4BSD's daemon() utility function
author Vladimir Kotal <Vladimir.Kotal@Sun.COM>
date Tue, 15 Sep 2009 20:20:20 +0200
parents d800ba0f4aea
children e1a70121b226
files usr/src/cmd/cmd-inet/usr.lib/wpad/wpa_supplicant.c usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c usr/src/cmd/rmvolmgr/rmvolmgr.c usr/src/cmd/ssh/include/config.h usr/src/cmd/ssh/include/daemon.h usr/src/cmd/ssh/include/openbsd-compat.h usr/src/cmd/ssh/libopenbsd-compat/Makefile.com usr/src/cmd/ssh/libopenbsd-compat/common/daemon.c usr/src/cmd/ssh/libopenbsd-compat/common/llib-lopenbsd-compat usr/src/cmd/ssh/libssh/common/llib-lssh usr/src/head/stdlib.h usr/src/lib/libc/amd64/Makefile usr/src/lib/libc/i386/Makefile.com usr/src/lib/libc/port/gen/daemon.c usr/src/lib/libc/port/llib-lc usr/src/lib/libc/port/mapfile-vers usr/src/lib/libc/sparc/Makefile.com usr/src/lib/libc/sparcv9/Makefile.com
diffstat 18 files changed, 123 insertions(+), 230 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/cmd-inet/usr.lib/wpad/wpa_supplicant.c	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/cmd/cmd-inet/usr.lib/wpad/wpa_supplicant.c	Tue Sep 15 20:20:20 2009 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -762,34 +762,6 @@
 	free(config);
 }
 
-static int
-daemon(boolean_t nochdir, boolean_t noclose)
-{
-	int retv;
-
-	if ((retv = fork()) == -1)
-		return (-1);
-	if (retv != 0)
-		_exit(EXIT_SUCCESS);
-	if (setsid() == -1)
-		return (-1);
-
-	if (!nochdir && chdir("/") == -1)
-		return (-1);
-
-	if (!noclose) {
-		(void) close(0);
-		(void) close(1);
-		(void) close(2);
-		if ((retv = open("/dev/null", O_RDWR)) != -1) {
-			(void) dup2(retv, 1);
-			(void) dup2(retv, 2);
-		}
-	}
-
-	return (0);
-}
-
 /*
  * make sure wpad is running under SMF context.
  */
--- a/usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c	Tue Sep 15 20:20:20 2009 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  *
  * Copyright (c) 1983, 1988, 1993
@@ -38,8 +38,6 @@
  * " The Regents of the University of California.  All rights reserved.\n";
  */
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
 #include "defs.h"
 #include "pathnames.h"
 #include <signal.h>
@@ -104,35 +102,6 @@
 static void	sigalrm(int);
 static void	sigterm(int);
 
-static int
-daemon(boolean_t nochdir, boolean_t noclose)
-{
-	int retv;
-
-	if ((retv = fork()) == -1)
-		return (-1);
-	if (retv != 0)
-		_exit(EXIT_SUCCESS);
-	if (setsid() == -1)
-		return (-1);
-	if ((retv = fork()) == -1)
-		return (-1);
-	if (retv != 0)
-		_exit(EXIT_SUCCESS);
-	if (!nochdir && chdir("/") == -1)
-		return (-1);
-	if (!noclose) {
-		(void) close(0);
-		(void) close(1);
-		(void) close(2);
-		if ((retv = open("/dev/null", O_RDWR)) != -1) {
-			(void) dup2(retv, 1);
-			(void) dup2(retv, 2);
-		}
-	}
-	return (0);
-}
-
 int
 main(int argc, char *argv[])
 {
@@ -310,9 +279,9 @@
 		goto usage;
 	if (argc != 0) {
 usage:
-		(void) fprintf(stderr,
-		    gettext("usage: in.routed [-AdghmnqsStVvz] "
-			"[-T <tracefile>]\n"));
+		(void) fprintf(stderr, gettext(
+		    "usage: in.routed [-AdghmnqsStVvz] "
+		    "[-T <tracefile>]\n"));
 		(void) fprintf(stderr,
 		    gettext("\t[-F <net>[/<mask>][,<metric>]] [-P <parms>]\n"));
 		logbad(_B_FALSE, gettext("excess arguments"));
@@ -358,7 +327,7 @@
 		msglog("signal: %s", rip_strerror(sigerr));
 
 	/* get into the background */
-	if (background && daemon(_B_FALSE, _B_FALSE) < 0)
+	if (background && daemon(0, 0) < 0)
 		BADERR(_B_FALSE, "daemon()");
 
 	/* Store our process id, blow away any existing file if it exists. */
--- a/usr/src/cmd/rmvolmgr/rmvolmgr.c	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/cmd/rmvolmgr/rmvolmgr.c	Tue Sep 15 20:20:20 2009 +0200
@@ -74,7 +74,6 @@
 static boolean_t	rmm_prop_eject_button = B_TRUE;
 
 static void	get_smf_properties();
-static int	daemon(int nochdir, int noclose);
 static void	rmm_device_added(LibHalContext *ctx, const char *udi);
 static void	rmm_device_removed(LibHalContext *ctx, const char *udi);
 static void	rmm_property_modified(LibHalContext *ctx, const char *udi,
@@ -594,50 +593,6 @@
 	}
 }
 
-static 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) {
-		struct stat64 st;
-
-		if (((fd = open("/dev/null", O_RDWR, 0)) != -1) &&
-		    (fstat64(fd, &st) == 0)) {
-			if (S_ISCHR(st.st_mode) != 0) {
-				(void) dup2(fd, STDIN_FILENO);
-				(void) dup2(fd, STDOUT_FILENO);
-				(void) dup2(fd, STDERR_FILENO);
-				if (fd > 2)
-					(void) close(fd);
-			} else {
-				(void) close(fd);
-				(void) __set_errno(ENODEV);
-				return (-1);
-			}
-		} else {
-			(void) close(fd);
-			return (-1);
-		}
-	}
-	return (0);
-}
-
 int
 main(int argc, char **argv)
 {
--- a/usr/src/cmd/ssh/include/config.h	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/cmd/ssh/include/config.h	Tue Sep 15 20:20:20 2009 +0200
@@ -279,9 +279,6 @@
 /* Define if your libraries define login() */
 /* #undef HAVE_LOGIN */
 
-/* Define if your libraries define daemon() */
-/* #undef HAVE_DAEMON */
-
 /* Define if your libraries define getpagesize() */
 #define	HAVE_GETPAGESIZE 1
 
--- a/usr/src/cmd/ssh/include/daemon.h	Tue Sep 15 08:42:53 2009 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-/* $Id: daemon.h,v 1.2 2001/02/09 01:55:36 djm Exp $ */
-
-#ifndef	_DAEMON_H
-#define	_DAEMON_H
-
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include "config.h"
-#ifndef HAVE_DAEMON
-int daemon(int nochdir, int noclose);
-#endif /* !HAVE_DAEMON */
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _DAEMON_H */
--- a/usr/src/cmd/ssh/include/openbsd-compat.h	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/cmd/ssh/include/openbsd-compat.h	Tue Sep 15 20:20:20 2009 +0200
@@ -44,7 +44,6 @@
 #include "strlcat.h"
 #include "strmode.h"
 #include "mktemp.h"
-#include "daemon.h"
 #include "dirname.h"
 #include "base64.h"
 #include "sigact.h"
--- a/usr/src/cmd/ssh/libopenbsd-compat/Makefile.com	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/cmd/ssh/libopenbsd-compat/Makefile.com	Tue Sep 15 20:20:20 2009 +0200
@@ -18,7 +18,7 @@
 #
 # CDDL HEADER END
 #
-# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 # Use is subject to license terms.
 #
 # cmd/ssh/libopenbsd-compat/Makefile.com
@@ -41,7 +41,6 @@
 		xmmap.o \
 		base64.o \
 		bindresvport.o \
-		daemon.o \
 		dirname.o \
 		getcwd.o \
 		getgrouplist.o \
--- a/usr/src/cmd/ssh/libopenbsd-compat/common/daemon.c	Tue Sep 15 08:42:53 2009 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,86 +0,0 @@
-/*-
- * 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. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *	This product includes software developed by the University of
- *	California, Berkeley and its contributors.
- * 4. 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 "includes.h"
-
-#ifndef HAVE_DAEMON
-
-#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: daemon.c,v 1.2 1996/08/19 08:22:13 tholo Exp $";
-#endif /* LIBC_SCCS and not lint */
-
-int
-daemon(nochdir, noclose)
-	int nochdir, noclose;
-{
-	int fd;
-
-	switch (fork()) {
-	case -1:
-		return (-1);
-	case 0:
-#ifdef HAVE_CYGWIN
-		register_9x_service();
-#endif
-		break;
-	default:
-#ifdef HAVE_CYGWIN
-		/*
-		 * This sleep avoids a race condition which kills the
-		 * child process if parent is started by a NT/W2K service.
-		 */
-		sleep(1);
-#endif
-		_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);
-}
-
-#endif /* !HAVE_DAEMON */
-
-
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
--- a/usr/src/cmd/ssh/libopenbsd-compat/common/llib-lopenbsd-compat	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/cmd/ssh/libopenbsd-compat/common/llib-lopenbsd-compat	Tue Sep 15 20:20:20 2009 +0200
@@ -21,7 +21,7 @@
  *
  * CDDL HEADER END
  *
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -38,7 +38,6 @@
 #include <bsd-waitpid.h>
 #include <config.h>
 #include <crc32.h>
-#include <daemon.h>
 #include <deattack.h>
 #include <defines.h>
 #include <dirname.h>
--- a/usr/src/cmd/ssh/libssh/common/llib-lssh	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/cmd/ssh/libssh/common/llib-lssh	Tue Sep 15 20:20:20 2009 +0200
@@ -21,7 +21,7 @@
  *
  * CDDL HEADER END
  *
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -53,7 +53,6 @@
 #include <compress.h>
 #include <config.h>
 #include <crc32.h>
-#include <daemon.h>
 #include <deattack.h>
 #include <defines.h>
 #include <dh.h>
--- a/usr/src/head/stdlib.h	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/head/stdlib.h	Tue Sep 15 20:20:20 2009 +0200
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -30,8 +30,6 @@
 #ifndef _STDLIB_H
 #define	_STDLIB_H
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.22	*/
-
 #include <iso/stdlib_iso.h>
 #include <iso/stdlib_c99.h>
 
@@ -215,6 +213,7 @@
 #if defined(__EXTENSIONS__) || \
 	(!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX))
 extern void closefrom(int);
+extern int daemon(int, int);
 extern int dup2(int, int);
 extern int fdwalk(int (*)(void *, int), void *);
 extern char *qecvt(long double, int, int *, int *);
@@ -317,6 +316,7 @@
 
 #if defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX)
 extern void closefrom();
+extern int daemon();
 extern int dup2();
 extern int fdwalk();
 extern char *qecvt();
--- a/usr/src/lib/libc/amd64/Makefile	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/lib/libc/amd64/Makefile	Tue Sep 15 20:20:20 2009 +0200
@@ -378,6 +378,7 @@
 	csetlen.o		\
 	ctime.o			\
 	ctime_r.o		\
+	daemon.o		\
 	deflt.o			\
 	directio.o		\
 	dirname.o		\
--- a/usr/src/lib/libc/i386/Makefile.com	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/lib/libc/i386/Makefile.com	Tue Sep 15 20:20:20 2009 +0200
@@ -412,6 +412,7 @@
 	csetlen.o		\
 	ctime.o			\
 	ctime_r.o		\
+	daemon.o		\
 	deflt.o			\
 	directio.o		\
 	dirname.o		\
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/lib/libc/port/gen/daemon.c	Tue Sep 15 20:20:20 2009 +0200
@@ -0,0 +1,103 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#include "lint.h"
+#include "file64.h"
+#include "mtlib.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "stdiom.h"
+
+/*
+ * Use fork/setsid/fork to go into background and permanently remove
+ * controlling terminal.
+ */
+int
+daemon(int nochdir, int noclose)
+{
+	int retv, fd;
+
+	/*
+	 * By the first fork+setsid, we disconnect from our current controlling
+	 * terminal and become a session group leader.
+	 */
+	retv = fork();
+	if (retv == -1)
+		return (-1);
+	if (retv != 0)
+		_exit(EXIT_SUCCESS);
+	if (setsid() == -1)
+		return (-1);
+	/*
+	 * By forking again without calling setsid again, we make certain
+	 * that we are not the session group leader and can never reacquire
+	 * a controlling terminal.
+	 */
+	retv = fork();
+	if (retv == -1)
+		return (-1);
+	if (retv != 0)
+		_exit(EXIT_SUCCESS);
+
+	if (nochdir == 0)
+		(void) chdir("/");
+
+	if (noclose == 0) {
+		/*
+		 * Missing the PRIV_FILE_READ privilege may be one of the
+		 * reasons that prevent the opening of /dev/null to succeed.
+		 */
+		if ((fd = open("/dev/null", O_RDWR)) == -1)
+			return (-1);
+
+		/*
+		 * Also, if any of the descriptor redirects fails we should
+		 * return with error to signal to the caller that his request
+		 * cannot be fulfilled properly. It is up to the caller to
+		 * do the cleanup.
+		 */
+		if ((fd != STDIN_FILENO) && (dup2(fd, STDIN_FILENO) < 0)) {
+			(void) close(fd);
+			return (-1);
+		}
+		if ((fd != STDOUT_FILENO) && (dup2(fd, STDOUT_FILENO) < 0)) {
+			(void) close(fd);
+			return (-1);
+		}
+		if ((fd != STDERR_FILENO) && (dup2(fd, STDERR_FILENO) < 0)) {
+			(void) close(fd);
+			return (-1);
+		}
+
+		if (fd > STDERR_FILENO)
+			(void) close(fd);
+	}
+	return (0);
+}
--- a/usr/src/lib/libc/port/llib-lc	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/lib/libc/port/llib-lc	Tue Sep 15 20:20:20 2009 +0200
@@ -345,6 +345,9 @@
 int _tolower(int c);
 int toascii(int c);
 
+/* daemon.c */
+int daemon(int nochdir, int noclose);
+
 /* directio.c */
 int directio(int filedes, int advice);
 
--- a/usr/src/lib/libc/port/mapfile-vers	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/lib/libc/port/mapfile-vers	Tue Sep 15 20:20:20 2009 +0200
@@ -86,6 +86,7 @@
 	clock_gettime;
 	clock_nanosleep;
 	clock_settime;
+	daemon;
 	dirfd;
 	door_bind;
 	door_call;
--- a/usr/src/lib/libc/sparc/Makefile.com	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/lib/libc/sparc/Makefile.com	Tue Sep 15 20:20:20 2009 +0200
@@ -429,6 +429,7 @@
 	csetlen.o		\
 	ctime.o			\
 	ctime_r.o		\
+	daemon.o		\
 	deflt.o			\
 	directio.o		\
 	dirname.o		\
--- a/usr/src/lib/libc/sparcv9/Makefile.com	Tue Sep 15 08:42:53 2009 -0600
+++ b/usr/src/lib/libc/sparcv9/Makefile.com	Tue Sep 15 20:20:20 2009 +0200
@@ -390,6 +390,7 @@
 	csetlen.o		\
 	ctime.o			\
 	ctime_r.o		\
+	daemon.o		\
 	deflt.o			\
 	directio.o		\
 	dirname.o		\