Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/bz6850.c

raw
1 /* Regression test for bugzilla 6850 */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9
10 #define PASS_MARKER "./bz6850_pass"
11
12 /* All this in an attempt to defeat gcc's over-aggressive inlining... */
13 typedef pid_t (*forker)(int);
14 static forker call_chain[];
15
16 /*
17 * Both parent and child return from fork2() and fork1(). Both
18 * processes will hit the uretprobe trampolines. The handlers should
19 * run in the parent. With the bug fix in place, the child will return
20 * correctly and do the exec (but won't run the handlers).
21 */
22 static pid_t fork2(int ignored)
23 {
24 return fork();
25 }
26
27 static pid_t fork1(int func_index)
28 {
29 ++func_index;
30 return call_chain[func_index](func_index); /* fork2() */
31 }
32
33 static pid_t fork_and_exec2(int func_index)
34 {
35 pid_t child;
36 ++func_index;
37 child = call_chain[func_index](func_index); /* fork1() */
38 if (child == 0) {
39 /* I'm the child. Create the marker file. */
40 char *child_args[] = { "/bin/touch", PASS_MARKER, NULL };
41 char *child_env[] = { NULL };
42 execve(child_args[0], child_args, child_env);
43 perror("execve");
44 fprintf(stderr, "FAIL: child couldn't exec.\n");
45 exit(2);
46 }
47 return child;
48 }
49
50 static pid_t fork_and_exec1(int func_index)
51 {
52 ++func_index;
53 return call_chain[func_index](func_index); /* fork_and_exec2() */
54 }
55
56 static forker call_chain[] = {
57 fork_and_exec1,
58 fork_and_exec2,
59 fork1,
60 fork2,
61 NULL
62 };
63
64 int main()
65 {
66 pid_t child, wait_child;
67 int status = 0;
68
69 (void) unlink(PASS_MARKER);
70 child = call_chain[0](0); /* fork_and_exec1() */
71 if (child < 0) {
72 fprintf(stderr, "FAIL: fork_and_exec1() failed.\n");
73 exit(1);
74 }
75 wait_child = wait(&status);
76 if (wait_child != child) {
77 fprintf(stderr, "FAIL: waited for %d but got %d\n",
78 child, wait_child);
79 exit(1);
80 }
81 if (WEXITSTATUS(status) != 0) {
82 fprintf(stderr, "FAIL: child died with status = %d\n",
83 WEXITSTATUS(status));
84 exit(1);
85 }
86 exit(0);
87 }