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