]> granicus.if.org Git - strace/blob - tests/clone_parent.c
Update copyright headers
[strace] / tests / clone_parent.c
1 /*
2  * Check handling of CLONE_PARENT'ed processes.
3  *
4  * Copyright (c) 2017-2018 The strace developers.
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11
12 #include <errno.h>
13 #include <sched.h>
14 #include <signal.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sys/wait.h>
18 #include <unistd.h>
19
20 static int
21 child(void *const arg)
22 {
23         return 42;
24 }
25
26 #define child_stack_size        (get_page_size() / 2)
27
28 #ifdef IA64
29 extern int __clone2(int (*)(void *), void *, size_t, int, void *, ...);
30 # define clone(fn, child_stack, flags, arg)     \
31                 __clone2(fn, child_stack, child_stack_size, flags, arg)
32 #endif
33
34 int
35 main(void)
36 {
37         const pid_t pid = clone(child, tail_alloc(child_stack_size),
38                                 CLONE_PARENT | SIGCHLD, 0);
39         if (pid < 0)
40                 perror_msg_and_fail("clone");
41
42         int status;
43         if (wait(&status) >= 0)
44                 error_msg_and_fail("unexpected return code from wait");
45
46         while (!kill(pid, 0))
47                 ;
48         if (errno != ESRCH)
49                 perror_msg_and_fail("kill");
50
51         FILE *const fp = fdopen(3, "a");
52         if (!fp)
53                 perror_msg_and_fail("fdopen");
54         if (fprintf(fp, "%s: Exit of unknown pid %d ignored\n",
55                     getenv("STRACE_EXE") ?: "strace", pid) < 0)
56                 perror_msg_and_fail("fprintf");
57
58         puts("+++ exited with 0 +++");
59         return 0;
60 }