xref: /netbsd-src/lib/librumpuser/rumpuser_daemonize.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: rumpuser_daemonize.c,v 1.4 2012/11/18 19:29:40 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 "rumpuser_port.h"
29 
30 #if !defined(lint)
31 __RCSID("$NetBSD: rumpuser_daemonize.c,v 1.4 2012/11/18 19:29:40 pooka Exp $");
32 #endif /* !lint */
33 
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 
42 #ifdef __sun__
43 #define _PATH_DEVNULL "/dev/null"
44 #else
45 #include <paths.h>
46 #endif
47 
48 static int isdaemonizing;
49 static int daemonpipe[2];
50 
51 #include <rump/rumpuser.h>
52 
53 int
54 rumpuser_daemonize_begin(void)
55 {
56 	ssize_t n;
57 	int error;
58 
59 	if (isdaemonizing)
60 		return EINPROGRESS;
61 	isdaemonizing = 1;
62 
63 	/*
64 	 * For daemons we need to fork.  However, since we can't fork
65 	 * after rump_init (which creates threads), do it now.  Add
66 	 * a little pipe trickery to make sure we don't exit until the
67 	 * service is fully inited (i.e. interlocked daemonization).
68 	 * Actually, use sucketpair since that allows to easily steer
69 	 * clear of the dreaded sigpipe.
70 	 *
71 	 * Note: We do *NOT* host chdir("/").  It's up to the caller to
72 	 * take care of that or not.
73 	 */
74 	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, daemonpipe) == -1) {
75 		return errno;
76 	}
77 
78 	switch (fork()) {
79 	case 0:
80 		if (setsid() == -1) {
81 			rumpuser_daemonize_done(errno);
82 		}
83 		return 0;
84 	case -1:
85 		return errno;
86 	default:
87 		close(daemonpipe[1]);
88 		n = recv(daemonpipe[0], &error, sizeof(error), MSG_NOSIGNAL);
89 		if (n == -1)
90 			error = errno;
91 		else if (n != sizeof(error))
92 			error = ESRCH;
93 		_exit(error);
94 		/*NOTREACHED*/
95 	}
96 }
97 
98 int
99 rumpuser_daemonize_done(int error)
100 {
101 	ssize_t n;
102 	int fd;
103 
104 	if (!isdaemonizing)
105 		return ENOENT;
106 
107 	if (error == 0) {
108 		fd = open(_PATH_DEVNULL, O_RDWR);
109 		if (fd == -1) {
110 			error = errno;
111 			goto out;
112 		}
113 		dup2(fd, STDIN_FILENO);
114 		dup2(fd, STDOUT_FILENO);
115 		dup2(fd, STDERR_FILENO);
116 		if (fd > STDERR_FILENO)
117 			close(fd);
118 	}
119 
120  out:
121 	n = send(daemonpipe[1], &error, sizeof(error), MSG_NOSIGNAL);
122 	if (n != sizeof(error))
123 		return EPIPE;
124 	else if (n == -1)
125 		return errno;
126 	close(daemonpipe[0]);
127 	close(daemonpipe[1]);
128 
129 	return 0;
130 }
131