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