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