3 * Cryptonector LLC. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
45 static int pipefds
[2] = {-1, -1};
47 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
48 roken_detach_prep(int argc
, char **argv
, char *special_arg
)
62 if (_pipe(pipefds
, 4, _O_NOINHERIT
| O_BINARY
) == -1)
63 err(1, "failed to setup to detach daemon (_pipe failed)");
65 if (pipe(pipefds
) == -1)
66 err(1, "failed to setup to detach daemon (pipe failed)");
69 new_argv
= calloc(argc
+ 3, sizeof(*new_argv
));
71 err(1, "Out of memory");
74 pipefds
[1] = _dup(pipefds
[1]); /* The new fd will be inherited */
76 err(1, "Out of memory");
78 (void) fcntl(pipefds
[1], F_SETFD
,
79 fcntl(pipefds
[1], F_GETFD
& ~(O_CLOEXEC
)));
82 if (snprintf(fildes
, sizeof(fildes
), "%d", pipefds
[1]) >= sizeof(fildes
))
83 err(1, "failed to setup to detach daemon (fd number %d too large)",
86 new_argv
[0] = argv
[0];
87 new_argv
[1] = special_arg
;
89 for (i
= 1; argv
[i
] != NULL
; i
++)
90 new_argv
[i
+ 2] = argv
[i
];
91 new_argv
[argc
+ 2] = NULL
;
98 intptr_t child_handle
;
101 child_handle
= spawnvp(_P_NOWAIT
, argv
[0], new_argv
);
102 if (child_handle
== -1)
105 child
= GetProcessId((HANDLE
)child_handle
);
108 if (child
== (pid_t
)-1)
109 err(1, "failed to setup to fork daemon (fork failed)");
115 (void) close(pipefds
[0]);
118 * Keep stdout/stderr for now so output and errors prior to
119 * detach_finish() can be seen by the user.
121 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
123 err(1, "failed to open /dev/null");
124 (void) dup2(fd
, STDIN_FILENO
);
125 if (fd
> STDERR_FILENO
)
127 if (getenv("ROKEN_DETACH_USE_EXEC")) {
128 (void) execvp(argv
[0], new_argv
);
129 err(1, "failed to self-re-exec");
138 (void) close(pipefds
[1]);
141 bytes
= read(pipefds
[0], buf
, sizeof(buf
));
142 } while (bytes
== -1 && errno
== EINTR
);
143 (void) close(pipefds
[0]);
147 * No need to wait for the process. We've killed it. If it
148 * doesn't want to exit, we'd have to wait potentially forever,
149 * but we want to indicate failure to the user as soon as
150 * possible. A wait with timeout would end the same way
151 * (attempting to kill the process).
153 err(1, "failed to setup daemon child (read from child pipe)");
156 warnx("daemon child preparation failed, waiting for child");
157 status
= wait_for_process(child
);
158 if (SE_IS_ERROR(status
) || SE_PROCSTATUS(status
) != 0)
159 errx(SE_PROCSTATUS(status
),
160 "daemon child preparation failed (child exited)");
174 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
175 roken_detach_finish(const char *dir
, int daemon_child_fd
)
182 if (pipefds
[1] == -1 && daemon_child_fd
!= -1)
183 pipefds
[1] = daemon_child_fd
;
184 if (pipefds
[0] != -1)
185 (void) close(pipefds
[0]);
186 if (pipefds
[1] == -1)
191 err(1, "failed to detach from tty");
196 * Hopefully we've written any pidfiles by now, if they had to be in
197 * the current directory...
199 * The daemons do re-open logs and so on, therefore this chdir()
200 * call needs to be optional for testing.
202 if (dir
!= NULL
&& chdir(dir
) == -1)
203 err(1, "failed to chdir to /");
207 bytes
= write(pipefds
[1], buf
, sizeof(buf
));
208 } while (bytes
== -1 && errno
== EINTR
);
210 err(1, "failed to signal parent while detaching");
211 (void) close(pipefds
[1]);
212 if (bytes
!= sizeof(buf
))
213 errx(1, "failed to signal parent while detaching");
215 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
217 err(1, "failed to open /dev/null");
219 * Maybe we should check that our output got written, if redirected
220 * to a file. File utils normally do this.
222 (void) dup2(fd
, STDOUT_FILENO
);
223 (void) dup2(fd
, STDERR_FILENO
);