| 1 |
/*- |
| 2 |
* Copyright (c) 2015 |
| 3 |
* Cryptonector LLC. All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* 3. Cryptonector LLC may not be used to endorse or promote products |
| 14 |
* derived from this software without specific prior written |
| 15 |
* permission. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
* SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <config.h> |
| 31 |
#include <errno.h> |
| 32 |
#include <fcntl.h> |
| 33 |
#ifdef WIN32 |
| 34 |
#include <io.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#else |
| 37 |
#include <unistd.h> |
| 38 |
#endif |
| 39 |
#include "roken.h" |
| 40 |
|
| 41 |
#ifdef WIN32 |
| 42 |
#define dup2 _dup2 |
| 43 |
#endif |
| 44 |
|
| 45 |
static int pipefds[2] = {-1, -1}; |
| 46 |
|
| 47 |
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL |
| 48 |
roken_detach_prep(int argc, char **argv, char *special_arg) |
| 49 |
{ |
| 50 |
pid_t child; |
| 51 |
char buf[1]; |
| 52 |
ssize_t bytes; |
| 53 |
int status; |
| 54 |
|
| 55 |
pipefds[0] = -1; |
| 56 |
pipefds[1] = -1; |
| 57 |
|
| 58 |
#ifdef WIN32 |
| 59 |
if (_pipe(pipefds, 4, O_BINARY) == -1) |
| 60 |
err(1, "failed to setup to detach daemon (_pipe failed)"); |
| 61 |
#else |
| 62 |
if (pipe(pipefds) == -1) |
| 63 |
err(1, "failed to setup to detach daemon (pipe failed)"); |
| 64 |
#endif |
| 65 |
|
| 66 |
#ifndef WIN32 |
| 67 |
fflush(stdout); |
| 68 |
child = fork(); |
| 69 |
#else |
| 70 |
{ |
| 71 |
intptr_t child_handle; |
| 72 |
int write_side; |
| 73 |
size_t i; |
| 74 |
char *fildes; |
| 75 |
char **new_argv; |
| 76 |
|
| 77 |
new_argv = calloc(argc + 2, sizeof(*new_argv)); |
| 78 |
if (new_argv == NULL) |
| 79 |
err(1, "Out of memory"); |
| 80 |
|
| 81 |
write_side = _dup(pipefds[1]); /* The new fd will be inherited */ |
| 82 |
if (write_side == -1) |
| 83 |
err(1, "Out of memory"); |
| 84 |
|
| 85 |
if (asprintf(&fildes, "%d", write_side) == -1 || |
| 86 |
fildes == NULL) |
| 87 |
err(1, "failed to setup to detach daemon (_dup failed)"); |
| 88 |
|
| 89 |
new_argv[0] = argv[0]; |
| 90 |
new_argv[1] = special_arg; |
| 91 |
new_argv[2] = fildes; |
| 92 |
for (i = 1; argv[i] != NULL; i++) |
| 93 |
new_argv[i + 1] = argv[i]; |
| 94 |
new_argv[argc + 2] = NULL; |
| 95 |
|
| 96 |
_flushall(); |
| 97 |
child_handle = spawnvp(_P_NOWAIT, argv[0], new_argv); |
| 98 |
if (child_handle == -1) |
| 99 |
child = (pid_t)-1; |
| 100 |
else |
| 101 |
child = GetProcessId((HANDLE)child_handle); |
| 102 |
} |
| 103 |
#endif |
| 104 |
if (child == (pid_t)-1) |
| 105 |
err(1, "failed to setup to fork daemon (fork failed)"); |
| 106 |
|
| 107 |
#ifndef WIN32 |
| 108 |
if (child == 0) { |
| 109 |
int fd; |
| 110 |
|
| 111 |
(void) close(pipefds[0]); |
| 112 |
pipefds[0] = -1; |
| 113 |
/* |
| 114 |
* Keep stdout/stderr for now so output and errors prior to |
| 115 |
* detach_finish() can be seen by the user. |
| 116 |
*/ |
| 117 |
fd = open(_PATH_DEVNULL, O_RDWR, 0); |
| 118 |
if (fd == -1) |
| 119 |
err(1, "failed to open /dev/null"); |
| 120 |
(void) dup2(fd, STDIN_FILENO); |
| 121 |
if (fd > STDERR_FILENO) |
| 122 |
(void) close(fd); |
| 123 |
return; |
| 124 |
} |
| 125 |
#endif |
| 126 |
|
| 127 |
(void) close(pipefds[1]); |
| 128 |
pipefds[1] = -1; |
| 129 |
do { |
| 130 |
bytes = read(pipefds[0], buf, sizeof(buf)); |
| 131 |
} while (bytes == -1 && errno == EINTR); |
| 132 |
(void) close(pipefds[0]); |
| 133 |
pipefds[0] = -1; |
| 134 |
if (bytes == -1) { |
| 135 |
/* |
| 136 |
* No need to wait for the process. We've killed it. If it |
| 137 |
* doesn't want to exit, we'd have to wait potentially forever, |
| 138 |
* but we want to indicate failure to the user as soon as |
| 139 |
* possible. A wait with timeout would end the same way |
| 140 |
* (attempting to kill the process). |
| 141 |
*/ |
| 142 |
err(1, "failed to setup daemon child (read from child pipe)"); |
| 143 |
} |
| 144 |
if (bytes == 0) { |
| 145 |
warnx("daemon child preparation failed, waiting for child"); |
| 146 |
status = wait_for_process(child); |
| 147 |
if (SE_IS_ERROR(status) || SE_PROCSTATUS(status) != 0) |
| 148 |
errx(SE_PROCSTATUS(status), |
| 149 |
"daemon child preparation failed (child exited)"); |
| 150 |
} |
| 151 |
_exit(0); |
| 152 |
} |
| 153 |
|
| 154 |
#ifdef WIN32 |
| 155 |
#ifdef dup2 |
| 156 |
#undef dup2 |
| 157 |
#endif |
| 158 |
#define dup2 _dup2 |
| 159 |
#endif |
| 160 |
|
| 161 |
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL |
| 162 |
roken_detach_finish(const char *dir, int daemon_child_fd) |
| 163 |
{ |
| 164 |
char buf[1] = ""; |
| 165 |
ssize_t bytes; |
| 166 |
int fd; |
| 167 |
|
| 168 |
rk_pidfile(NULL); |
| 169 |
if (pipefds[1] == -1 && daemon_child_fd != -1) |
| 170 |
pipefds[1] = daemon_child_fd; |
| 171 |
if (pipefds[0] != -1) |
| 172 |
(void) close(pipefds[0]); |
| 173 |
if (pipefds[1] == -1) |
| 174 |
return; |
| 175 |
|
| 176 |
#ifdef HAVE_SETSID |
| 177 |
if (setsid() == -1) |
| 178 |
err(1, "failed to detach from tty"); |
| 179 |
#endif |
| 180 |
|
| 181 |
#ifndef WIN32 |
| 182 |
/* |
| 183 |
* Hopefully we've written any pidfiles by now, if they had to be in |
| 184 |
* the current directory... |
| 185 |
* |
| 186 |
* The daemons do re-open logs and so on, therefore this chdir() |
| 187 |
* call needs to be optional for testing. |
| 188 |
*/ |
| 189 |
if (dir != NULL && chdir(dir) == -1) |
| 190 |
err(1, "failed to chdir to /"); |
| 191 |
#endif |
| 192 |
|
| 193 |
do { |
| 194 |
bytes = write(pipefds[1], buf, sizeof(buf)); |
| 195 |
} while (bytes == -1 && errno == EINTR); |
| 196 |
if (bytes == -1) |
| 197 |
err(1, "failed to signal parent while detaching"); |
| 198 |
(void) close(pipefds[1]); |
| 199 |
if (bytes != sizeof(buf)) |
| 200 |
errx(1, "failed to signal parent while detaching"); |
| 201 |
|
| 202 |
fd = open(_PATH_DEVNULL, O_RDWR, 0); |
| 203 |
if (fd == -1) |
| 204 |
err(1, "failed to open /dev/null"); |
| 205 |
/* |
| 206 |
* Maybe we should check that our output got written, if redirected |
| 207 |
* to a file. File utils normally do this. |
| 208 |
*/ |
| 209 |
(void) dup2(fd, STDOUT_FILENO); |
| 210 |
(void) dup2(fd, STDERR_FILENO); |
| 211 |
if (fd > 2) |
| 212 |
(void) close(fd); |
| 213 |
} |