1*6859Sth199096 /*
2*6859Sth199096 * CDDL HEADER START
3*6859Sth199096 *
4*6859Sth199096 * The contents of this file are subject to the terms of the
5*6859Sth199096 * Common Development and Distribution License (the "License").
6*6859Sth199096 * You may not use this file except in compliance with the License.
7*6859Sth199096 *
8*6859Sth199096 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*6859Sth199096 * or http://www.opensolaris.org/os/licensing.
10*6859Sth199096 * See the License for the specific language governing permissions
11*6859Sth199096 * and limitations under the License.
12*6859Sth199096 *
13*6859Sth199096 * When distributing Covered Code, include this CDDL HEADER in each
14*6859Sth199096 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*6859Sth199096 * If applicable, add the following below this CDDL HEADER, with the
16*6859Sth199096 * fields enclosed by brackets "[]" replaced with your own identifying
17*6859Sth199096 * information: Portions Copyright [yyyy] [name of copyright owner]
18*6859Sth199096 *
19*6859Sth199096 * CDDL HEADER END
20*6859Sth199096 */
21*6859Sth199096 /*
22*6859Sth199096 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23*6859Sth199096 * Use is subject to license terms.
24*6859Sth199096 */
25*6859Sth199096
26*6859Sth199096 /* LINTLIBRARY */
27*6859Sth199096 /* PROTOLIB1 */
28*6859Sth199096
29*6859Sth199096 #pragma ident "%Z%%M% %I% %E% SMI"
30*6859Sth199096
31*6859Sth199096 /* NFS server */
32*6859Sth199096
33*6859Sth199096 #include <sys/param.h>
34*6859Sth199096 #include <sys/types.h>
35*6859Sth199096 #include <errno.h>
36*6859Sth199096 #include <stdio.h>
37*6859Sth199096 #include <stdio_ext.h>
38*6859Sth199096 #include <stdlib.h>
39*6859Sth199096 #include <signal.h>
40*6859Sth199096 #include <unistd.h>
41*6859Sth199096 #include <sys/wait.h>
42*6859Sth199096 #include <sys/stat.h>
43*6859Sth199096 #include <fcntl.h>
44*6859Sth199096 #include <libscf.h>
45*6859Sth199096
46*6859Sth199096 /*
47*6859Sth199096 * The parent never returns from this function. It sits
48*6859Sth199096 * and waits for the child to send status on whether it
49*6859Sth199096 * loaded or not.
50*6859Sth199096 *
51*6859Sth199096 * We do not close down the standard file descriptors until
52*6859Sth199096 * we know the child is going to run.
53*6859Sth199096 */
54*6859Sth199096 int
daemonize_init(void)55*6859Sth199096 daemonize_init(void)
56*6859Sth199096 {
57*6859Sth199096 int status, pfds[2];
58*6859Sth199096 sigset_t set, oset;
59*6859Sth199096 pid_t pid;
60*6859Sth199096
61*6859Sth199096 /*
62*6859Sth199096 * Block all signals prior to the fork and leave them blocked in the
63*6859Sth199096 * parent so we don't get in a situation where the parent gets SIGINT
64*6859Sth199096 * and returns non-zero exit status and the child is actually running.
65*6859Sth199096 * In the child, restore the signal mask once we've done our setsid().
66*6859Sth199096 */
67*6859Sth199096 (void) sigfillset(&set);
68*6859Sth199096 (void) sigdelset(&set, SIGABRT);
69*6859Sth199096 (void) sigprocmask(SIG_BLOCK, &set, &oset);
70*6859Sth199096
71*6859Sth199096 /*
72*6859Sth199096 * We need to do this before we open the pipe - it makes things
73*6859Sth199096 * easier in the long run.
74*6859Sth199096 */
75*6859Sth199096 closefrom(STDERR_FILENO + 1);
76*6859Sth199096
77*6859Sth199096 if (pipe(pfds) == -1) {
78*6859Sth199096 fprintf(stderr, "failed to create pipe for daemonize");
79*6859Sth199096 exit(SMF_EXIT_ERR_FATAL);
80*6859Sth199096 }
81*6859Sth199096
82*6859Sth199096 if ((pid = fork()) == -1) {
83*6859Sth199096 fprintf(stderr, "failed to fork into background");
84*6859Sth199096 exit(SMF_EXIT_ERR_FATAL);
85*6859Sth199096 }
86*6859Sth199096
87*6859Sth199096 if (pid != 0) {
88*6859Sth199096 (void) close(pfds[1]);
89*6859Sth199096
90*6859Sth199096 if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
91*6859Sth199096 exit(status);
92*6859Sth199096
93*6859Sth199096 if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
94*6859Sth199096 exit(WEXITSTATUS(status));
95*6859Sth199096
96*6859Sth199096 exit(SMF_EXIT_ERR_FATAL);
97*6859Sth199096 }
98*6859Sth199096
99*6859Sth199096 /*
100*6859Sth199096 * All child from here on...
101*6859Sth199096 */
102*6859Sth199096 (void) setsid();
103*6859Sth199096 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
104*6859Sth199096 (void) close(pfds[0]);
105*6859Sth199096
106*6859Sth199096 return (pfds[1]);
107*6859Sth199096 }
108*6859Sth199096
109*6859Sth199096 /*
110*6859Sth199096 * We are only a daemon if the file descriptor is valid.
111*6859Sth199096 */
112*6859Sth199096 void
daemonize_fini(int fd)113*6859Sth199096 daemonize_fini(int fd)
114*6859Sth199096 {
115*6859Sth199096 int status = 0;
116*6859Sth199096
117*6859Sth199096 if (fd != -1) {
118*6859Sth199096 (void) write(fd, &status, sizeof (status));
119*6859Sth199096
120*6859Sth199096 (void) close(fd);
121*6859Sth199096
122*6859Sth199096 if ((fd = open("/dev/null", O_RDWR)) >= 0) {
123*6859Sth199096 (void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
124*6859Sth199096 (void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
125*6859Sth199096 (void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
126*6859Sth199096 (void) close(fd);
127*6859Sth199096 }
128*6859Sth199096 }
129*6859Sth199096 }
130