4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright (c) 2016 by Delphix. All rights reserved.
40 * Use fork/setsid/fork to go into background and permanently remove
41 * controlling terminal.
44 daemon(int nochdir
, int noclose
)
49 * By the first fork+setsid, we disconnect from our current controlling
50 * terminal and become a session group leader.
60 * By forking again without calling setsid again, we make certain
61 * that we are not the session group leader and can never reacquire
62 * a controlling terminal.
75 * Missing the PRIV_FILE_READ privilege may be one of the
76 * reasons that prevent the opening of /dev/null to succeed.
78 if ((fd
= open("/dev/null", O_RDWR
)) == -1)
82 * Also, if any of the descriptor redirects fails we should
83 * return with error to signal to the caller that its request
84 * cannot be fulfilled properly. It is up to the caller to
87 if ((fd
!= STDIN_FILENO
) && (dup2(fd
, STDIN_FILENO
) < 0)) {
91 if ((fd
!= STDOUT_FILENO
) && (dup2(fd
, STDOUT_FILENO
) < 0)) {
95 if ((fd
!= STDERR_FILENO
) && (dup2(fd
, STDERR_FILENO
) < 0)) {
100 if (fd
> STDERR_FILENO
)