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