1*d3273b5bSchristos /* $NetBSD: test-detach.c,v 1.2 2017/01/28 21:31:50 christos Exp $ */
2b9d004c6Schristos
3b9d004c6Schristos /***********************************************************************
4b9d004c6Schristos * Copyright (c) 2015, Cryptonector LLC
5b9d004c6Schristos * All rights reserved.
6b9d004c6Schristos *
7b9d004c6Schristos * Redistribution and use in source and binary forms, with or without
8b9d004c6Schristos * modification, are permitted provided that the following conditions
9b9d004c6Schristos * are met:
10b9d004c6Schristos *
11b9d004c6Schristos * - Redistributions of source code must retain the above copyright
12b9d004c6Schristos * notice, this list of conditions and the following disclaimer.
13b9d004c6Schristos *
14b9d004c6Schristos * - Redistributions in binary form must reproduce the above copyright
15b9d004c6Schristos * notice, this list of conditions and the following disclaimer in
16b9d004c6Schristos * the documentation and/or other materials provided with the
17b9d004c6Schristos * distribution.
18b9d004c6Schristos *
19b9d004c6Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20b9d004c6Schristos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21b9d004c6Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22b9d004c6Schristos * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23b9d004c6Schristos * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24b9d004c6Schristos * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25b9d004c6Schristos * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26b9d004c6Schristos * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27b9d004c6Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28b9d004c6Schristos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29b9d004c6Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30b9d004c6Schristos * OF THE POSSIBILITY OF SUCH DAMAGE.
31b9d004c6Schristos *
32b9d004c6Schristos **********************************************************************/
33b9d004c6Schristos
34b9d004c6Schristos #include <config.h>
35b9d004c6Schristos
36b9d004c6Schristos #include <sys/types.h>
37b9d004c6Schristos #include <err.h>
38b9d004c6Schristos #include <stdio.h>
39b9d004c6Schristos #include <stdlib.h>
40b9d004c6Schristos #include <errno.h>
41b9d004c6Schristos #include <string.h>
42b9d004c6Schristos #ifdef WIN32
43b9d004c6Schristos #include <process.h>
44b9d004c6Schristos #ifdef getpid
45b9d004c6Schristos #undef getpid
46b9d004c6Schristos #endif
47b9d004c6Schristos #define getpid _getpid
48b9d004c6Schristos #else
49b9d004c6Schristos #include <unistd.h>
50b9d004c6Schristos #endif
51b9d004c6Schristos #include <krb5/roken.h>
52b9d004c6Schristos
main(int argc,char ** argv)53b9d004c6Schristos int main(int argc, char **argv)
54b9d004c6Schristos {
55b9d004c6Schristos char *ends;
56b9d004c6Schristos long n;
57b9d004c6Schristos int fd = -1;
58b9d004c6Schristos
59b9d004c6Schristos if (argc > 1) {
60b9d004c6Schristos if (argc != 3)
61b9d004c6Schristos errx(1, "Usage: test-detach [--daemon-child fd]");
62b9d004c6Schristos fprintf(stderr, "Child started (argv[1] = %s, argv[2] = %s)!\n", argv[1], argv[2]);
63b9d004c6Schristos errno = 0;
64b9d004c6Schristos n = strtol(argv[2], &ends, 10);
65b9d004c6Schristos fd = n;
66b9d004c6Schristos if (errno != 0)
67b9d004c6Schristos err(1, "Usage: test-detach [--daemon-child fd]");
68b9d004c6Schristos if (n < 0 || ends == NULL || *ends != '\0' || n != fd)
69b9d004c6Schristos errx(1, "Usage: test-detach [--daemon-child fd]");
70b9d004c6Schristos } else {
71b9d004c6Schristos fprintf(stderr, "Parent started as %ld\n", (long)getpid());
72b9d004c6Schristos roken_detach_prep(argc, argv, "--daemon-child");
73b9d004c6Schristos }
74b9d004c6Schristos fprintf(stderr, "Now should be the child: %ld\n", (long)getpid());
75b9d004c6Schristos roken_detach_finish(NULL, fd);
76b9d004c6Schristos /*
77b9d004c6Schristos * These printfs will not appear: stderr will have been replaced
78b9d004c6Schristos * with /dev/null.
79b9d004c6Schristos */
80b9d004c6Schristos fprintf(stderr, "Now should be the child: %ld, wrote to parent\n", (long)getpid());
81b9d004c6Schristos sleep(5);
82b9d004c6Schristos fprintf(stderr, "Daemon child done\n");
83b9d004c6Schristos return 0;
84b9d004c6Schristos }
85