diff options
author | Vladimir Kotal <Vladimir.Kotal@Sun.COM> | 2009-09-15 20:20:20 +0200 |
---|---|---|
committer | Vladimir Kotal <Vladimir.Kotal@Sun.COM> | 2009-09-15 20:20:20 +0200 |
commit | 44991a1c1bb35cccb6bf99cb6dce14864dcee19c (patch) | |
tree | e43389c0b05fa250ba59ff5a593557f7e2446ef5 /usr/src/lib/libc | |
parent | 9d48f5d37dfc1f8378818cf3d2baa2ccf7990c38 (diff) | |
download | illumos-gate-44991a1c1bb35cccb6bf99cb6dce14864dcee19c.tar.gz |
PSARC/2009/444 daemon() in libc
4471189 should have 4.4BSD's daemon() utility function
Diffstat (limited to 'usr/src/lib/libc')
-rw-r--r-- | usr/src/lib/libc/amd64/Makefile | 1 | ||||
-rw-r--r-- | usr/src/lib/libc/i386/Makefile.com | 1 | ||||
-rw-r--r-- | usr/src/lib/libc/port/gen/daemon.c | 103 | ||||
-rw-r--r-- | usr/src/lib/libc/port/llib-lc | 3 | ||||
-rw-r--r-- | usr/src/lib/libc/port/mapfile-vers | 1 | ||||
-rw-r--r-- | usr/src/lib/libc/sparc/Makefile.com | 1 | ||||
-rw-r--r-- | usr/src/lib/libc/sparcv9/Makefile.com | 1 |
7 files changed, 111 insertions, 0 deletions
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 \ |