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