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