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