Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.clone/dtrace_clone.c

raw
1 /* Systemtap test case
2 * Copyright (C) 2010, Red Hat Inc.
3 *
4 * This file is part of systemtap, and is free software. You can
5 * redistribute it and/or modify it under the terms of the GNU General
6 * Public License (GPL); either version 2, or (at your option) any
7 * later version.
8 */
9
10 #define _GNU_SOURCE
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <fcntl.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/wait.h>
19 #include <sched.h>
20 #include <errno.h>
21 #include "dtrace_clone_probes.h"
22
23 #if !defined(CLONE_NEWPID)
24 #define CLONE_NEWPID 0
25 #endif
26
27 #define CLONE_FLAGS (CLONE_NEWPID|CLONE_FILES|CLONE_FS|CLONE_VM|SIGCHLD)
28
29 #if !defined(USE_SEMAPHORES)
30 #undef TEST_CHILD2_ENABLED
31 #define TEST_CHILD2_ENABLED() (1)
32 #undef TEST_CHILD1_ENABLED
33 #define TEST_CHILD1_ENABLED() (1)
34 #undef TEST_CHILD2_PID_ENABLED
35 #define TEST_CHILD2_PID_ENABLED() (1)
36 #undef TEST_MAIN_ENABLED
37 #define TEST_MAIN_ENABLED() (1)
38 #undef TEST_CHILD1_PID_ENABLED
39 #define TEST_CHILD1_PID_ENABLED() (1)
40 #undef TEST_MAIN2_ENABLED
41 #define TEST_MAIN2_ENABLED() (1)
42 #endif
43
44 void *stack_base2 = NULL;
45 void *stack_top2 = NULL;
46
47 static int uprobes_ns_child2(void *argv __attribute__((__unused__)))
48 {
49 if (TEST_CHILD2_ENABLED()) {
50 TEST_CHILD2(getpid());
51 }
52 _exit(0);
53 }
54
55 static int uprobes_ns_child1(void *argv __attribute__((__unused__)))
56 {
57 int pid;
58 int rc = 0;
59
60 /* Create the second clone. */
61 if (TEST_CHILD1_ENABLED()) {
62 TEST_CHILD1(getpid());
63 }
64
65 pid = clone(uprobes_ns_child2, stack_top2, CLONE_FLAGS, NULL);
66 if (TEST_CHILD2_PID_ENABLED()) {
67 TEST_CHILD2_PID(pid);
68 }
69 if (pid < 0) {
70 /* Error. */
71 char *msg2 = "clone 2 failed\n";
72 write(2, msg2, strlen(msg2));
73 rc = errno;
74 }
75 else {
76 int status;
77
78 waitpid(pid, &status, 0);
79 rc = WEXITSTATUS(status);
80 }
81 _exit(rc);
82 }
83
84 int
85 main(int argc, char **argv)
86 {
87 long pagesize = sysconf(_SC_PAGESIZE);
88 void *stack_base1, *stack_top1;
89 int pid;
90 int rc = 0;
91
92 /* Allocate both stacks here. */
93 stack_base1 = calloc(pagesize * 4, 1);
94 if (stack_base1 == NULL) {
95 perror("calloc 1 failed:");
96 return -1;
97 }
98 stack_base2 = calloc(pagesize * 4, 1);
99 if (stack_base2 == NULL) {
100 perror("calloc 2 failed:");
101 return -1;
102 }
103
104 /*
105 * Get top of stacks. According to the clone() manpage:
106 * Stacks grow downwards on all processors that run Linux
107 * (except the HP PA processors), so child_stack usually points
108 * to the topmost address of the memory space set up for the
109 * child stack.
110 */
111 stack_top1 = stack_base1 + (pagesize * 4);
112 stack_top2 = stack_base2 + (pagesize * 4);
113
114 /* Create the first clone (which will create the 2nd). */
115 if (TEST_MAIN_ENABLED()) {
116 TEST_MAIN(getpid());
117 }
118 pid = clone(uprobes_ns_child1, stack_top1, CLONE_FLAGS, NULL);
119 if (TEST_CHILD1_PID_ENABLED()) {
120 TEST_CHILD1_PID(pid);
121 }
122 if (pid < 0) {
123 /* error */
124 perror("clone 1 failed:");
125 rc = -1;
126 }
127 else {
128 int status;
129
130 /* Wait on the first clone to finish. */
131 waitpid(pid, &status, 0);
132 rc = WEXITSTATUS(status);
133 }
134 /* Cleanup */
135 free(stack_base1);
136 free(stack_base2);
137 if (TEST_MAIN2_ENABLED()) {
138 TEST_MAIN2();
139 }
140
141 return rc;
142 }