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