summaryrefslogtreecommitdiff
path: root/usr/src
diff options
context:
space:
mode:
authorVladimir Kotal <Vladimir.Kotal@Sun.COM>2009-09-15 20:20:20 +0200
committerVladimir Kotal <Vladimir.Kotal@Sun.COM>2009-09-15 20:20:20 +0200
commit44991a1c1bb35cccb6bf99cb6dce14864dcee19c (patch)
treee43389c0b05fa250ba59ff5a593557f7e2446ef5 /usr/src
parent9d48f5d37dfc1f8378818cf3d2baa2ccf7990c38 (diff)
downloadillumos-joyent-44991a1c1bb35cccb6bf99cb6dce14864dcee19c.tar.gz
PSARC/2009/444 daemon() in libc
4471189 should have 4.4BSD's daemon() utility function
Diffstat (limited to 'usr/src')
-rw-r--r--usr/src/cmd/cmd-inet/usr.lib/wpad/wpa_supplicant.c30
-rw-r--r--usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c41
-rw-r--r--usr/src/cmd/rmvolmgr/rmvolmgr.c45
-rw-r--r--usr/src/cmd/ssh/include/config.h3
-rw-r--r--usr/src/cmd/ssh/include/daemon.h21
-rw-r--r--usr/src/cmd/ssh/include/openbsd-compat.h1
-rw-r--r--usr/src/cmd/ssh/libopenbsd-compat/Makefile.com3
-rw-r--r--usr/src/cmd/ssh/libopenbsd-compat/common/daemon.c86
-rw-r--r--usr/src/cmd/ssh/libopenbsd-compat/common/llib-lopenbsd-compat3
-rw-r--r--usr/src/cmd/ssh/libssh/common/llib-lssh3
-rw-r--r--usr/src/head/stdlib.h6
-rw-r--r--usr/src/lib/libc/amd64/Makefile1
-rw-r--r--usr/src/lib/libc/i386/Makefile.com1
-rw-r--r--usr/src/lib/libc/port/gen/daemon.c103
-rw-r--r--usr/src/lib/libc/port/llib-lc3
-rw-r--r--usr/src/lib/libc/port/mapfile-vers1
-rw-r--r--usr/src/lib/libc/sparc/Makefile.com1
-rw-r--r--usr/src/lib/libc/sparcv9/Makefile.com1
18 files changed, 123 insertions, 230 deletions
diff --git a/usr/src/cmd/cmd-inet/usr.lib/wpad/wpa_supplicant.c b/usr/src/cmd/cmd-inet/usr.lib/wpad/wpa_supplicant.c
index 4e3071debd..3fb8b133a7 100644
--- a/usr/src/cmd/cmd-inet/usr.lib/wpad/wpa_supplicant.c
+++ b/usr/src/cmd/cmd-inet/usr.lib/wpad/wpa_supplicant.c
@@ -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 @@ wpa_config_free(struct wpa_config *config)
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.
*/
diff --git a/usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c b/usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c
index 0bbf016111..c37cf1caff 100644
--- a/usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c
+++ b/usr/src/cmd/cmd-inet/usr.sbin/in.routed/main.c
@@ -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 timevalsub(struct timeval *, struct timeval *, struct timeval *);
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 @@ main(int argc, char *argv[])
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 @@ usage:
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. */
diff --git a/usr/src/cmd/rmvolmgr/rmvolmgr.c b/usr/src/cmd/rmvolmgr/rmvolmgr.c
index 3475066a97..3587d77dfd 100644
--- a/usr/src/cmd/rmvolmgr/rmvolmgr.c
+++ b/usr/src/cmd/rmvolmgr/rmvolmgr.c
@@ -74,7 +74,6 @@ static boolean_t opt_s; /* system instance */
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 @@ rmm_unmount_all()
}
}
-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)
{
diff --git a/usr/src/cmd/ssh/include/config.h b/usr/src/cmd/ssh/include/config.h
index 978559f2d0..8bf9962434 100644
--- a/usr/src/cmd/ssh/include/config.h
+++ b/usr/src/cmd/ssh/include/config.h
@@ -279,9 +279,6 @@ extern "C" {
/* 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
diff --git a/usr/src/cmd/ssh/include/daemon.h b/usr/src/cmd/ssh/include/daemon.h
deleted file mode 100644
index fd22d587fd..0000000000
--- a/usr/src/cmd/ssh/include/daemon.h
+++ /dev/null
@@ -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 */
diff --git a/usr/src/cmd/ssh/include/openbsd-compat.h b/usr/src/cmd/ssh/include/openbsd-compat.h
index df2472da0e..aab8e36c62 100644
--- a/usr/src/cmd/ssh/include/openbsd-compat.h
+++ b/usr/src/cmd/ssh/include/openbsd-compat.h
@@ -44,7 +44,6 @@ extern "C" {
#include "strlcat.h"
#include "strmode.h"
#include "mktemp.h"
-#include "daemon.h"
#include "dirname.h"
#include "base64.h"
#include "sigact.h"
diff --git a/usr/src/cmd/ssh/libopenbsd-compat/Makefile.com b/usr/src/cmd/ssh/libopenbsd-compat/Makefile.com
index bfee4459c7..0b8ad1622b 100644
--- a/usr/src/cmd/ssh/libopenbsd-compat/Makefile.com
+++ b/usr/src/cmd/ssh/libopenbsd-compat/Makefile.com
@@ -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 @@ OBJECTS = \
xmmap.o \
base64.o \
bindresvport.o \
- daemon.o \
dirname.o \
getcwd.o \
getgrouplist.o \
diff --git a/usr/src/cmd/ssh/libopenbsd-compat/common/daemon.c b/usr/src/cmd/ssh/libopenbsd-compat/common/daemon.c
deleted file mode 100644
index a7e44333f0..0000000000
--- a/usr/src/cmd/ssh/libopenbsd-compat/common/daemon.c
+++ /dev/null
@@ -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"
diff --git a/usr/src/cmd/ssh/libopenbsd-compat/common/llib-lopenbsd-compat b/usr/src/cmd/ssh/libopenbsd-compat/common/llib-lopenbsd-compat
index 7d6ae70cda..3c5912997f 100644
--- a/usr/src/cmd/ssh/libopenbsd-compat/common/llib-lopenbsd-compat
+++ b/usr/src/cmd/ssh/libopenbsd-compat/common/llib-lopenbsd-compat
@@ -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>
diff --git a/usr/src/cmd/ssh/libssh/common/llib-lssh b/usr/src/cmd/ssh/libssh/common/llib-lssh
index 3d8e8d5525..c44090abe6 100644
--- a/usr/src/cmd/ssh/libssh/common/llib-lssh
+++ b/usr/src/cmd/ssh/libssh/common/llib-lssh
@@ -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>
diff --git a/usr/src/head/stdlib.h b/usr/src/head/stdlib.h
index 94ac6d8018..ba1343433d 100644
--- a/usr/src/head/stdlib.h
+++ b/usr/src/head/stdlib.h
@@ -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 @@ extern int unsetenv(const char *);
#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 @@ extern int unsetenv();
#if defined(__EXTENSIONS__) || !defined(__XOPEN_OR_POSIX)
extern void closefrom();
+extern int daemon();
extern int dup2();
extern int fdwalk();
extern char *qecvt();
diff --git a/usr/src/lib/libc/amd64/Makefile b/usr/src/lib/libc/amd64/Makefile
index e30dbdd760..0bcff6db21 100644
--- a/usr/src/lib/libc/amd64/Makefile
+++ b/usr/src/lib/libc/amd64/Makefile
@@ -378,6 +378,7 @@ PORTGEN= \
csetlen.o \
ctime.o \
ctime_r.o \
+ daemon.o \
deflt.o \
directio.o \
dirname.o \
diff --git a/usr/src/lib/libc/i386/Makefile.com b/usr/src/lib/libc/i386/Makefile.com
index 772d09b00e..c90b5e3e3a 100644
--- a/usr/src/lib/libc/i386/Makefile.com
+++ b/usr/src/lib/libc/i386/Makefile.com
@@ -412,6 +412,7 @@ PORTGEN= \
csetlen.o \
ctime.o \
ctime_r.o \
+ daemon.o \
deflt.o \
directio.o \
dirname.o \
diff --git a/usr/src/lib/libc/port/gen/daemon.c b/usr/src/lib/libc/port/gen/daemon.c
new file mode 100644
index 0000000000..f1e6926c91
--- /dev/null
+++ b/usr/src/lib/libc/port/gen/daemon.c
@@ -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);
+}
diff --git a/usr/src/lib/libc/port/llib-lc b/usr/src/lib/libc/port/llib-lc
index ffd9185d61..0d639fc49d 100644
--- a/usr/src/lib/libc/port/llib-lc
+++ b/usr/src/lib/libc/port/llib-lc
@@ -345,6 +345,9 @@ int _toupper(int c);
int _tolower(int c);
int toascii(int c);
+/* daemon.c */
+int daemon(int nochdir, int noclose);
+
/* directio.c */
int directio(int filedes, int advice);
diff --git a/usr/src/lib/libc/port/mapfile-vers b/usr/src/lib/libc/port/mapfile-vers
index 92e89d5614..222afe0972 100644
--- a/usr/src/lib/libc/port/mapfile-vers
+++ b/usr/src/lib/libc/port/mapfile-vers
@@ -86,6 +86,7 @@ SUNW_1.23 { # SunOS 5.11 (Solaris 11)
clock_gettime;
clock_nanosleep;
clock_settime;
+ daemon;
dirfd;
door_bind;
door_call;
diff --git a/usr/src/lib/libc/sparc/Makefile.com b/usr/src/lib/libc/sparc/Makefile.com
index f4e58ca2e9..9ee0e063c5 100644
--- a/usr/src/lib/libc/sparc/Makefile.com
+++ b/usr/src/lib/libc/sparc/Makefile.com
@@ -429,6 +429,7 @@ PORTGEN= \
csetlen.o \
ctime.o \
ctime_r.o \
+ daemon.o \
deflt.o \
directio.o \
dirname.o \
diff --git a/usr/src/lib/libc/sparcv9/Makefile.com b/usr/src/lib/libc/sparcv9/Makefile.com
index c2455ed9d8..3956149134 100644
--- a/usr/src/lib/libc/sparcv9/Makefile.com
+++ b/usr/src/lib/libc/sparcv9/Makefile.com
@@ -390,6 +390,7 @@ PORTGEN= \
csetlen.o \
ctime.o \
ctime_r.o \
+ daemon.o \
deflt.o \
directio.o \
dirname.o \