detach.c revision 1.2.12.2 1 1.2.12.2 snj /* $NetBSD: detach.c,v 1.2.12.2 2017/08/20 05:42:08 snj Exp $ */
2 1.2.12.2 snj
3 1.2.12.2 snj /*-
4 1.2.12.2 snj * Copyright (c) 2015
5 1.2.12.2 snj * Cryptonector LLC. All rights reserved.
6 1.2.12.2 snj *
7 1.2.12.2 snj * Redistribution and use in source and binary forms, with or without
8 1.2.12.2 snj * modification, are permitted provided that the following conditions
9 1.2.12.2 snj * are met:
10 1.2.12.2 snj * 1. Redistributions of source code must retain the above copyright
11 1.2.12.2 snj * notice, this list of conditions and the following disclaimer.
12 1.2.12.2 snj * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.12.2 snj * notice, this list of conditions and the following disclaimer in the
14 1.2.12.2 snj * documentation and/or other materials provided with the distribution.
15 1.2.12.2 snj * 3. Cryptonector LLC may not be used to endorse or promote products
16 1.2.12.2 snj * derived from this software without specific prior written
17 1.2.12.2 snj * permission.
18 1.2.12.2 snj *
19 1.2.12.2 snj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.2.12.2 snj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.2.12.2 snj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.2.12.2 snj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.2.12.2 snj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.2.12.2 snj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.2.12.2 snj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.2.12.2 snj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.2.12.2 snj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.2.12.2 snj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.2.12.2 snj * SUCH DAMAGE.
30 1.2.12.2 snj */
31 1.2.12.2 snj
32 1.2.12.2 snj #include <config.h>
33 1.2.12.2 snj #include <errno.h>
34 1.2.12.2 snj #include <fcntl.h>
35 1.2.12.2 snj #ifdef WIN32
36 1.2.12.2 snj #include <io.h>
37 1.2.12.2 snj #include <stdlib.h>
38 1.2.12.2 snj #else
39 1.2.12.2 snj #include <unistd.h>
40 1.2.12.2 snj #endif
41 1.2.12.2 snj #include <krb5/roken.h>
42 1.2.12.2 snj
43 1.2.12.2 snj #ifdef WIN32
44 1.2.12.2 snj #define dup2 _dup2
45 1.2.12.2 snj #endif
46 1.2.12.2 snj
47 1.2.12.2 snj static int pipefds[2] = {-1, -1};
48 1.2.12.2 snj
49 1.2.12.2 snj ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
50 1.2.12.2 snj roken_detach_prep(int argc, char **argv, char *special_arg)
51 1.2.12.2 snj {
52 1.2.12.2 snj pid_t child;
53 1.2.12.2 snj char buf[1];
54 1.2.12.2 snj ssize_t bytes;
55 1.2.12.2 snj int status;
56 1.2.12.2 snj
57 1.2.12.2 snj pipefds[0] = -1;
58 1.2.12.2 snj pipefds[1] = -1;
59 1.2.12.2 snj
60 1.2.12.2 snj #ifdef WIN32
61 1.2.12.2 snj if (_pipe(pipefds, 4, O_BINARY) == -1)
62 1.2.12.2 snj err(1, "failed to setup to detach daemon (_pipe failed)");
63 1.2.12.2 snj #else
64 1.2.12.2 snj if (pipe(pipefds) == -1)
65 1.2.12.2 snj err(1, "failed to setup to detach daemon (pipe failed)");
66 1.2.12.2 snj #endif
67 1.2.12.2 snj
68 1.2.12.2 snj #ifndef WIN32
69 1.2.12.2 snj fflush(stdout);
70 1.2.12.2 snj child = fork();
71 1.2.12.2 snj #else
72 1.2.12.2 snj {
73 1.2.12.2 snj intptr_t child_handle;
74 1.2.12.2 snj int write_side;
75 1.2.12.2 snj size_t i;
76 1.2.12.2 snj char *fildes;
77 1.2.12.2 snj char **new_argv;
78 1.2.12.2 snj
79 1.2.12.2 snj new_argv = calloc(argc + 2, sizeof(*new_argv));
80 1.2.12.2 snj if (new_argv == NULL)
81 1.2.12.2 snj err(1, "Out of memory");
82