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