1*10547SVladimir.Kotal@Sun.COM /*
2*10547SVladimir.Kotal@Sun.COM * CDDL HEADER START
3*10547SVladimir.Kotal@Sun.COM *
4*10547SVladimir.Kotal@Sun.COM * The contents of this file are subject to the terms of the
5*10547SVladimir.Kotal@Sun.COM * Common Development and Distribution License (the "License").
6*10547SVladimir.Kotal@Sun.COM * You may not use this file except in compliance with the License.
7*10547SVladimir.Kotal@Sun.COM *
8*10547SVladimir.Kotal@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10547SVladimir.Kotal@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*10547SVladimir.Kotal@Sun.COM * See the License for the specific language governing permissions
11*10547SVladimir.Kotal@Sun.COM * and limitations under the License.
12*10547SVladimir.Kotal@Sun.COM *
13*10547SVladimir.Kotal@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*10547SVladimir.Kotal@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10547SVladimir.Kotal@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*10547SVladimir.Kotal@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*10547SVladimir.Kotal@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*10547SVladimir.Kotal@Sun.COM *
19*10547SVladimir.Kotal@Sun.COM * CDDL HEADER END
20*10547SVladimir.Kotal@Sun.COM */
21*10547SVladimir.Kotal@Sun.COM
22*10547SVladimir.Kotal@Sun.COM /*
23*10547SVladimir.Kotal@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24*10547SVladimir.Kotal@Sun.COM * Use is subject to license terms.
25*10547SVladimir.Kotal@Sun.COM */
26*10547SVladimir.Kotal@Sun.COM
27*10547SVladimir.Kotal@Sun.COM #include "lint.h"
28*10547SVladimir.Kotal@Sun.COM #include "file64.h"
29*10547SVladimir.Kotal@Sun.COM #include "mtlib.h"
30*10547SVladimir.Kotal@Sun.COM
31*10547SVladimir.Kotal@Sun.COM #include <stdio.h>
32*10547SVladimir.Kotal@Sun.COM #include <stdlib.h>
33*10547SVladimir.Kotal@Sun.COM #include <unistd.h>
34*10547SVladimir.Kotal@Sun.COM #include <fcntl.h>
35*10547SVladimir.Kotal@Sun.COM
36*10547SVladimir.Kotal@Sun.COM #include "stdiom.h"
37*10547SVladimir.Kotal@Sun.COM
38*10547SVladimir.Kotal@Sun.COM /*
39*10547SVladimir.Kotal@Sun.COM * Use fork/setsid/fork to go into background and permanently remove
40*10547SVladimir.Kotal@Sun.COM * controlling terminal.
41*10547SVladimir.Kotal@Sun.COM */
42*10547SVladimir.Kotal@Sun.COM int
daemon(int nochdir,int noclose)43*10547SVladimir.Kotal@Sun.COM daemon(int nochdir, int noclose)
44*10547SVladimir.Kotal@Sun.COM {
45*10547SVladimir.Kotal@Sun.COM int retv, fd;
46*10547SVladimir.Kotal@Sun.COM
47*10547SVladimir.Kotal@Sun.COM /*
48*10547SVladimir.Kotal@Sun.COM * By the first fork+setsid, we disconnect from our current controlling
49*10547SVladimir.Kotal@Sun.COM * terminal and become a session group leader.
50*10547SVladimir.Kotal@Sun.COM */
51*10547SVladimir.Kotal@Sun.COM retv = fork();
52*10547SVladimir.Kotal@Sun.COM if (retv == -1)
53*10547SVladimir.Kotal@Sun.COM return (-1);
54*10547SVladimir.Kotal@Sun.COM if (retv != 0)
55*10547SVladimir.Kotal@Sun.COM _exit(EXIT_SUCCESS);
56*10547SVladimir.Kotal@Sun.COM if (setsid() == -1)
57*10547SVladimir.Kotal@Sun.COM return (-1);
58*10547SVladimir.Kotal@Sun.COM /*
59*10547SVladimir.Kotal@Sun.COM * By forking again without calling setsid again, we make certain
60*10547SVladimir.Kotal@Sun.COM * that we are not the session group leader and can never reacquire
61*10547SVladimir.Kotal@Sun.COM * a controlling terminal.
62*10547SVladimir.Kotal@Sun.COM */
63*10547SVladimir.Kotal@Sun.COM retv = fork();
64*10547SVladimir.Kotal@Sun.COM if (retv == -1)
65*10547SVladimir.Kotal@Sun.COM return (-1);
66*10547SVladimir.Kotal@Sun.COM if (retv != 0)
67*10547SVladimir.Kotal@Sun.COM _exit(EXIT_SUCCESS);
68*10547SVladimir.Kotal@Sun.COM
69*10547SVladimir.Kotal@Sun.COM if (nochdir == 0)
70*10547SVladimir.Kotal@Sun.COM (void) chdir("/");
71*10547SVladimir.Kotal@Sun.COM
72*10547SVladimir.Kotal@Sun.COM if (noclose == 0) {
73*10547SVladimir.Kotal@Sun.COM /*
74*10547SVladimir.Kotal@Sun.COM * Missing the PRIV_FILE_READ privilege may be one of the
75*10547SVladimir.Kotal@Sun.COM * reasons that prevent the opening of /dev/null to succeed.
76*10547SVladimir.Kotal@Sun.COM */
77*10547SVladimir.Kotal@Sun.COM if ((fd = open("/dev/null", O_RDWR)) == -1)
78*10547SVladimir.Kotal@Sun.COM return (-1);
79*10547SVladimir.Kotal@Sun.COM
80*10547SVladimir.Kotal@Sun.COM /*
81*10547SVladimir.Kotal@Sun.COM * Also, if any of the descriptor redirects fails we should
82*10547SVladimir.Kotal@Sun.COM * return with error to signal to the caller that his request
83*10547SVladimir.Kotal@Sun.COM * cannot be fulfilled properly. It is up to the caller to
84*10547SVladimir.Kotal@Sun.COM * do the cleanup.
85*10547SVladimir.Kotal@Sun.COM */
86*10547SVladimir.Kotal@Sun.COM if ((fd != STDIN_FILENO) && (dup2(fd, STDIN_FILENO) < 0)) {
87*10547SVladimir.Kotal@Sun.COM (void) close(fd);
88*10547SVladimir.Kotal@Sun.COM return (-1);
89*10547SVladimir.Kotal@Sun.COM }
90*10547SVladimir.Kotal@Sun.COM if ((fd != STDOUT_FILENO) && (dup2(fd, STDOUT_FILENO) < 0)) {
91*10547SVladimir.Kotal@Sun.COM (void) close(fd);
92*10547SVladimir.Kotal@Sun.COM return (-1);
93*10547SVladimir.Kotal@Sun.COM }
94*10547SVladimir.Kotal@Sun.COM if ((fd != STDERR_FILENO) && (dup2(fd, STDERR_FILENO) < 0)) {
95*10547SVladimir.Kotal@Sun.COM (void) close(fd);
96*10547SVladimir.Kotal@Sun.COM return (-1);
97*10547SVladimir.Kotal@Sun.COM }
98*10547SVladimir.Kotal@Sun.COM
99*10547SVladimir.Kotal@Sun.COM if (fd > STDERR_FILENO)
100*10547SVladimir.Kotal@Sun.COM (void) close(fd);
101*10547SVladimir.Kotal@Sun.COM }
102*10547SVladimir.Kotal@Sun.COM return (0);
103*10547SVladimir.Kotal@Sun.COM }
104