xref: /netbsd-src/lib/librumpuser/rumpuser_daemonize.c (revision 46f5119e40af2e51998f686b2fdcc76b5488f7f3)
1 /*	$NetBSD: rumpuser_daemonize.c,v 1.2 2011/01/22 14:18:55 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #if !defined(lint)
30 __RCSID("$NetBSD: rumpuser_daemonize.c,v 1.2 2011/01/22 14:18:55 pooka Exp $");
31 #endif /* !lint */
32 
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <paths.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 
42 static int isdaemonizing;
43 static int daemonpipe[2];
44 
45 #include <rump/rumpuser.h>
46 
47 int
48 rumpuser_daemonize_begin(void)
49 {
50 	ssize_t n;
51 	int error;
52 
53 	if (isdaemonizing)
54 		return EINPROGRESS;
55 	isdaemonizing = 1;
56 
57 	/*
58 	 * For daemons we need to fork.  However, since we can't fork
59 	 * after rump_init (which creates threads), do it now.  Add
60 	 * a little pipe trickery to make sure we don't exit until the
61 	 * service is fully inited (i.e. interlocked daemonization).
62 	 * Actually, use sucketpair since that allows to easily steer
63 	 * clear of the dreaded sigpipe.
64 	 *
65 	 * Note: We do *NOT* host chdir("/").  It's up to the caller to
66 	 * take care of that or not.
67 	 */
68 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, daemonpipe) == -1) {
69 		return errno;
70 	}
71 
72 	switch (fork()) {
73 	case 0:
74 		if (setsid() == -1) {
75 			rumpuser_daemonize_done(errno);
76 		}
77 		return 0;
78 	case -1:
79 		return errno;
80 	default:
81 		close(daemonpipe[1]);
82 		n = recv(daemonpipe[0], &error, sizeof(error), MSG_NOSIGNAL);
83 		if (n == -1)
84 			error = errno;
85 		else if (n != sizeof(error))
86 			error = ESRCH;
87 		_exit(error);
88 		/*NOTREACHED*/
89 	}
90 }
91 
92 int
93 rumpuser_daemonize_done(int error)
94 {
95 	ssize_t n;
96 	int fd;
97 
98 	if (!isdaemonizing)
99 		return ENOENT;
100 
101 	if (error == 0) {
102 		fd = open(_PATH_DEVNULL, O_RDWR);
103 		if (fd == -1) {
104 			error = errno;
105 			goto out;
106 		}
107 		dup2(fd, STDIN_FILENO);
108 		dup2(fd, STDOUT_FILENO);
109 		dup2(fd, STDERR_FILENO);
110 		if (fd > STDERR_FILENO)
111 			close(fd);
112 	}
113 
114  out:
115 	n = send(daemonpipe[1], &error, sizeof(error), MSG_NOSIGNAL);
116 	if (n != sizeof(error))
117 		return EPIPE;
118 	else if (n == -1)
119 		return errno;
120 	close(daemonpipe[0]);
121 	close(daemonpipe[1]);
122 
123 	return 0;
124 }
125