xref: /illumos-gate/usr/src/cmd/fs.d/nfs/lib/daemon.c (revision 2a8bcb4e)
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 /* NFS server */
30*250a0733Sth 
31*250a0733Sth #include <sys/param.h>
32*250a0733Sth #include <sys/types.h>
33*250a0733Sth #include <errno.h>
34*250a0733Sth #include <stdio.h>
35*250a0733Sth #include <stdio_ext.h>
36*250a0733Sth #include <stdlib.h>
37*250a0733Sth #include <signal.h>
38*250a0733Sth #include <unistd.h>
39*250a0733Sth #include <sys/wait.h>
40*250a0733Sth #include <sys/stat.h>
41*250a0733Sth #include <fcntl.h>
42*250a0733Sth #include <libscf.h>
43*250a0733Sth 
44*250a0733Sth /*
45*250a0733Sth  * The parent never returns from this function. It sits
46*250a0733Sth  * and waits for the child to send status on whether it
47*250a0733Sth  * loaded or not.
48*250a0733Sth  *
49*250a0733Sth  * We do not close down the standard file descriptors until
50*250a0733Sth  * we know the child is going to run.
51*250a0733Sth  */
52*250a0733Sth int
daemonize_init(void)53*250a0733Sth daemonize_init(void)
54*250a0733Sth {
55*250a0733Sth 	int status, pfds[2];
56*250a0733Sth 	sigset_t set, oset;
57*250a0733Sth 	pid_t pid;
58*250a0733Sth 
59*250a0733Sth 	/*
60*250a0733Sth 	 * Block all signals prior to the fork and leave them blocked in the
61*250a0733Sth 	 * parent so we don't get in a situation where the parent gets SIGINT
62*250a0733Sth 	 * and returns non-zero exit status and the child is actually running.
63*250a0733Sth 	 * In the child, restore the signal mask once we've done our setsid().
64*250a0733Sth 	 */
65*250a0733Sth 	(void) sigfillset(&set);
66*250a0733Sth 	(void) sigdelset(&set, SIGABRT);
67*250a0733Sth 	(void) sigprocmask(SIG_BLOCK, &set, &oset);
68*250a0733Sth 
69*250a0733Sth 	/*
70*250a0733Sth 	 * We need to do this before we open the pipe - it makes things
71*250a0733Sth 	 * easier in the long run.
72*250a0733Sth 	 */
73*250a0733Sth 	closefrom(STDERR_FILENO + 1);
74*250a0733Sth 
75*250a0733Sth 	if (pipe(pfds) == -1) {
76*250a0733Sth 		fprintf(stderr, "failed to create pipe for daemonize");
77*250a0733Sth 		exit(SMF_EXIT_ERR_FATAL);
78*250a0733Sth 	}
79*250a0733Sth 
80*250a0733Sth 	if ((pid = fork()) == -1) {
81*250a0733Sth 		fprintf(stderr, "failed to fork into background");
82*250a0733Sth 		exit(SMF_EXIT_ERR_FATAL);
83*250a0733Sth 	}
84*250a0733Sth 
85*250a0733Sth 	if (pid != 0) {
86*250a0733Sth 		(void) close(pfds[1]);
87*250a0733Sth 
88*250a0733Sth 		if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
89*250a0733Sth 			exit(status);
90*250a0733Sth 
91*250a0733Sth 		if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
92*250a0733Sth 			exit(WEXITSTATUS(status));
93*250a0733Sth 
94*250a0733Sth 		exit(SMF_EXIT_ERR_FATAL);
95*250a0733Sth 	}
96*250a0733Sth 
97*250a0733Sth 	/*
98*250a0733Sth 	 * All child from here on...
99*250a0733Sth 	 */
100*250a0733Sth 	(void) setsid();
101*250a0733Sth 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
102*250a0733Sth 	(void) close(pfds[0]);
103*250a0733Sth 
104*250a0733Sth 	return (pfds[1]);
105*250a0733Sth }
106*250a0733Sth 
107*250a0733Sth /*
108*250a0733Sth  * We are only a daemon if the file descriptor is valid.
109*250a0733Sth  */
110*250a0733Sth void
daemonize_fini(int fd)111*250a0733Sth daemonize_fini(int fd)
112*250a0733Sth {
113*250a0733Sth 	int	status = 0;
114*250a0733Sth 
115*250a0733Sth 	if (fd != -1) {
116*250a0733Sth 		(void) write(fd, &status, sizeof (status));
117*250a0733Sth 
118*250a0733Sth 		(void) close(fd);
119*250a0733Sth 
120*250a0733Sth 		if ((fd = open("/dev/null", O_RDWR)) >= 0) {
121*250a0733Sth 			(void) fcntl(fd, F_DUP2FD, STDIN_FILENO);
122*250a0733Sth 			(void) fcntl(fd, F_DUP2FD, STDOUT_FILENO);
123*250a0733Sth 			(void) fcntl(fd, F_DUP2FD, STDERR_FILENO);
124*250a0733Sth 			(void) close(fd);
125*250a0733Sth 		}
126*250a0733Sth 	}
127*250a0733Sth }
128