Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/switch-mount-ns.exp

raw
1 set test "switch-mount-ns"
2 set testpath "$srcdir/$subdir"
3 set work_dir [pwd]
4
5 if {! [installtest_p]} { untested $test; return }
6 if {! [uretprobes_p]} { untested $test; return }
7
8 # --- TEST 1 ---
9
10 set subtest1 "TEST 1: --target-namespace + uprobes in containers (no -x PID)"
11 if {![min_kernel_vers_p 5.8.0]} {
12 untested "switch-mount-ns (kernel version is too low; 5.8+ required)"
13 } else {
14 set res [target_compile ${testpath}/${test}_1.c ./a.out executable \
15 "additional_flags=-O additional_flags=-g additional_flags=-O0"]
16 if {$res ne ""} {
17 verbose "target_compile failed: $res" 2
18 fail "$test: $subtest1: unable to compile ${test}_1.c"
19 } else {
20 if {! [process_template_file "$srcdir/$subdir/${test}_1.stp" \
21 "./${test}_1.stp" "./a.out" nm_err]} {
22 fail "$test: $subtest1: $nm_err"
23 } else {
24 set test_name "$test: $subtest1"
25
26 set rc [catch {exec sh -c "cd '$work_dir' && rm -f *.pid"} res]
27 if {$rc != 0} {
28 fail "$test: $subtest1: failed to clean up files under '$work_dir': $res"
29 return;
30 }
31 spawn -noecho bash -c "$srcdir/$subdir/${test}_1.sh '$work_dir/a.out' '${test}-1' > '$work_dir/${test}-1-bg-shell-out.log' 2>&1"
32 set bg_sh_pid [exp_pid $spawn_id]
33 set bg_pid $bg_sh_pid
34
35 set ns_pid ""
36 set ns_pid_file "$work_dir/ns.pid"
37 for {set i 0} {$i < 1000} {incr i} {
38 if {[file exists $ns_pid_file]} {
39 set f [open "$ns_pid_file" r]
40 set ns_pid [read $f]
41 close $f
42 break
43 }
44 # in sec
45 sleep 0.01
46 }
47 if {![info exists ns_pid] || $ns_pid eq ""} {
48 fail "$test: $subtest1: ns.pid was never found under $work_dir: [exec ls $work_dir]"
49 return
50 }
51
52 if {[file exists "$work_dir/target.pid"]} {
53 set f [open "$work_dir/target.pid" r]
54 set bg_pid [read $f]
55 close $f
56 }
57 set stp_opts "--target-namespace=\$^NS_PID -d \$^WORK_DIR/tmp/newtarget/a.out"
58 regsub -all -- {\$\^WORK_DIR\y} $stp_opts $work_dir stp_opts
59 regsub -all -- {\$\^NS_PID\y} $stp_opts $ns_pid stp_opts
60 set cmd "stap $stp_opts './${test}_1.stp'"
61 set exit_code [run_cmd_2way_as_root $cmd out stderr]
62 catch {kill -INT -$bg_sh_pid 2}
63 catch {kill -KILL $bg_sh_pid}
64 set exp_out "Hit
65 target pid: 0
66 "
67 is "${test_name}: stdout" $out $exp_out
68 is "${test_name}: exit code" $exit_code 0
69 set no_stderr_pat "Failed to open file '/proc/\\d+/ns/mnt': No such file or directory"
70 unlike "${test_name}: stderr should not match" $stderr $no_stderr_pat "-lineanchor"
71 }
72 }
73 }
74
75 # --- TEST 2 ---
76
77 set subtest2 "TEST 2: --target-namespace + uprobes in containers (with -x PID)"
78 if {![min_kernel_vers_p 5.8.0]} {
79 untested "switch-mount-ns (kernel version is too low; 5.8+ required)"
80 } else {
81 set res [target_compile ${testpath}/${test}_1.c ./a.out executable \
82 "additional_flags=-O additional_flags=-g additional_flags=-O0"]
83 if {$res ne ""} {
84 verbose "target_compile failed: $res" 2
85 fail "$test: $subtest2: unable to compile ${test}_1.c"
86 } else {
87 if {! [process_template_file "$srcdir/$subdir/${test}_1.stp" \
88 "./${test}_1.stp" "./a.out" nm_err]} {
89 fail "$test: $subtest2: $nm_err"
90 } else {
91 set test_name "$test: $subtest2"
92
93 set rc [catch {exec sh -c "cd '$work_dir' && rm -f *.pid"} res]
94 if {$rc != 0} {
95 fail "$test: $subtest2: failed to clean up files under '$work_dir': $res"
96 return;
97 }
98 spawn -noecho bash -c "$srcdir/$subdir/${test}_1.sh '$work_dir/a.out' '${test}-2' > '$work_dir/${test}-2-bg-shell-out.log' 2>&1"
99 set bg_sh_pid [exp_pid $spawn_id]
100 set bg_pid $bg_sh_pid
101
102 set ns_pid ""
103 set ns_pid_file "$work_dir/ns.pid"
104 for {set i 0} {$i < 1000} {incr i} {
105 if {[file exists $ns_pid_file]} {
106 set f [open "$ns_pid_file" r]
107 set ns_pid [read $f]
108 close $f
109 break
110 }
111 # in sec
112 sleep 0.01
113 }
114 if {![info exists ns_pid] || $ns_pid eq ""} {
115 fail "$test: $subtest2: ns.pid was never found under $work_dir: [exec ls $work_dir]"
116 return
117 }
118
119 if {[file exists "$work_dir/target.pid"]} {
120 set f [open "$work_dir/target.pid" r]
121 set bg_pid [read $f]
122 close $f
123 }
124 set stp_opts "--target-namespace=\$^NS_PID -d \$^WORK_DIR/tmp/newtarget/a.out -x \$^PID"
125 regsub -all -- {\$\^WORK_DIR\y} $stp_opts $work_dir stp_opts
126 regsub -all -- {\$\^NS_PID\y} $stp_opts $ns_pid stp_opts
127 regsub -all -- {\$\^PID\y} $stp_opts $bg_pid stp_opts
128 set cmd "stap $stp_opts './${test}_1.stp'"
129 set exit_code [run_cmd_2way_as_root $cmd out stderr]
130 catch {kill -INT -$bg_sh_pid 2}
131 catch {kill -KILL $bg_sh_pid}
132 set out_pat "\\AHit
133 target pid: \[1-9\]\\d*
134 \\Z"
135 like "${test_name}: stdout" $out $out_pat "-lineanchor"
136 is "${test_name}: exit code" $exit_code 0
137 set no_stderr_pat "Failed to open file '/proc/\\d+/ns/mnt': No such file or directory"
138 unlike "${test_name}: stderr should not match" $stderr $no_stderr_pat "-lineanchor"
139 }
140 }
141 }
142
143 # --- TEST 3 ---
144
145 set subtest3 "TEST 3: NO --target-namespace for uprobes in containers (no -x PID)"
146
147 # No hits
148 set res [target_compile ${testpath}/${test}_1.c ./a.out executable \
149 "additional_flags=-O additional_flags=-g additional_flags=-O0"]
150 if {$res ne ""} {
151 verbose "target_compile failed: $res" 2
152 fail "$test: $subtest3: unable to compile ${test}_1.c"
153 } else {
154 if {! [process_template_file "$srcdir/$subdir/${test}_1.stp" \
155 "./${test}_1.stp" "./a.out" nm_err]} {
156 fail "$test: $subtest3: $nm_err"
157 } else {
158 set test_name "$test: $subtest3"
159
160 set rc [catch {exec sh -c "cd '$work_dir' && rm -f *.pid"} res]
161 if {$rc != 0} {
162 fail "$test: $subtest3: failed to clean up files under '$work_dir': $res"
163 return;
164 }
165 spawn -noecho bash -c "$srcdir/$subdir/${test}_1.sh '$work_dir/a.out' '${test}-3' > '$work_dir/${test}-3-bg-shell-out.log' 2>&1"
166 set bg_sh_pid [exp_pid $spawn_id]
167 set bg_pid $bg_sh_pid
168
169 set ns_pid ""
170 set ns_pid_file "$work_dir/ns.pid"
171 for {set i 0} {$i < 1000} {incr i} {
172 if {[file exists $ns_pid_file]} {
173 set f [open "$ns_pid_file" r]
174 set ns_pid [read $f]
175 close $f
176 break
177 }
178 # in sec
179 sleep 0.01
180 }
181 if {![info exists ns_pid] || $ns_pid eq ""} {
182 fail "$test: $subtest3: ns.pid was never found under $work_dir: [exec ls $work_dir]"
183 return
184 }
185
186 if {[file exists "$work_dir/target.pid"]} {
187 set f [open "$work_dir/target.pid" r]
188 set bg_pid [read $f]
189 close $f
190 }
191 set stp_opts "-d \$^WORK_DIR/tmp/newtarget/a.out -x \$^PID"
192 regsub -all -- {\$\^WORK_DIR\y} $stp_opts $work_dir stp_opts
193 regsub -all -- {\$\^PID\y} $stp_opts $bg_pid stp_opts
194 set cmd "stap $stp_opts './${test}_1.stp'"
195 set exit_code [run_cmd_2way_as_root $cmd out stderr]
196 catch {kill -INT -$bg_sh_pid 2}
197 catch {kill -KILL $bg_sh_pid}
198 set exp_out ""
199 is "${test_name}: stdout" $out $exp_out
200 is "${test_name}: exit code" $exit_code 0
201 set no_stderr_pat "Failed to open file '/proc/\\d+/ns/mnt': No such file or directory"
202 unlike "${test_name}: stderr should not match" $stderr $no_stderr_pat "-lineanchor"
203 }
204 }
205
206 # --- TEST 4 ---
207
208 set subtest4 "TEST 4: mismatched ns in --target-namespace for uprobes in containers (no -x PID)"
209
210 # No hits
211 set res [target_compile ${testpath}/${test}_1.c ./a.out executable \
212 "additional_flags=-O additional_flags=-g additional_flags=-O0"]
213 if {$res ne ""} {
214 verbose "target_compile failed: $res" 2
215 fail "$test: $subtest4: unable to compile ${test}_1.c"
216 } else {
217 if {! [process_template_file "$srcdir/$subdir/${test}_1.stp" \
218 "./${test}_1.stp" "./a.out" nm_err]} {
219 fail "$test: $subtest4: $nm_err"
220 } else {
221 set test_name "$test: $subtest4"
222
223 set rc [catch {exec sh -c "cd '$work_dir' && rm -f *.pid"} res]
224 if {$rc != 0} {
225 fail "$test: $subtest4: failed to clean up files under '$work_dir': $res"
226 return;
227 }
228 spawn -noecho bash -c "$srcdir/$subdir/${test}_1.sh '$work_dir/a.out' '${test}-4' > '$work_dir/${test}-4-bg-shell-out.log' 2>&1"
229 set bg_sh_pid [exp_pid $spawn_id]
230 set bg_pid $bg_sh_pid
231
232 set ns_pid ""
233 set ns_pid_file "$work_dir/ns.pid"
234 for {set i 0} {$i < 1000} {incr i} {
235 if {[file exists $ns_pid_file]} {
236 set f [open "$ns_pid_file" r]
237 set ns_pid [read $f]
238 close $f
239 break
240 }
241 # in sec
242 sleep 0.01
243 }
244 if {![info exists ns_pid] || $ns_pid eq ""} {
245 fail "$test: $subtest4: ns.pid was never found under $work_dir: [exec ls $work_dir]"
246 return
247 }
248
249 if {[file exists "$work_dir/target.pid"]} {
250 set f [open "$work_dir/target.pid" r]
251 set bg_pid [read $f]
252 close $f
253 }
254 set stp_opts "--target-namespace 1 -d \$^WORK_DIR/tmp/newtarget/a.out"
255 regsub -all -- {\$\^WORK_DIR\y} $stp_opts $work_dir stp_opts
256 set cmd "stap $stp_opts './${test}_1.stp'"
257 set exit_code [run_cmd_2way_as_root $cmd out stderr]
258 catch {kill -INT -$bg_sh_pid 2}
259 catch {kill -KILL $bg_sh_pid}
260 set exp_out ""
261 is "${test_name}: stdout" $out $exp_out
262 is "${test_name}: exit code" $exit_code 0
263 set no_stderr_pat "Failed to open file '/proc/\\d+/ns/mnt': No such file or directory"
264 unlike "${test_name}: stderr should not match" $stderr $no_stderr_pat "-lineanchor"
265 }
266 }
267
268 # --- TEST 5 ---
269
270 set subtest5 "TEST 5: ns pid not found: --target-namespace + uprobes in containers (no -x PID)"
271 set res [target_compile ${testpath}/${test}_1.c ./a.out executable \
272 "additional_flags=-O additional_flags=-g additional_flags=-O0"]
273 if {$res ne ""} {
274 verbose "target_compile failed: $res" 2
275 fail "$test: $subtest5: unable to compile ${test}_1.c"
276 } else {
277 if {! [process_template_file "$srcdir/$subdir/${test}_1.stp" \
278 "./${test}_1.stp" "./a.out" nm_err]} {
279 fail "$test: $subtest5: $nm_err"
280 } else {
281 set test_name "$test: $subtest5"
282
283 set rc [catch {exec sh -c "cd '$work_dir' && rm -f *.pid"} res]
284 if {$rc != 0} {
285 fail "$test: $subtest5: failed to clean up files under '$work_dir': $res"
286 return;
287 }
288 spawn -noecho bash -c "$srcdir/$subdir/${test}_1.sh '$work_dir/a.out' '${test}-5' > '$work_dir/${test}-5-bg-shell-out.log' 2>&1"
289 set bg_sh_pid [exp_pid $spawn_id]
290 set bg_pid $bg_sh_pid
291
292 set ns_pid ""
293 set ns_pid_file "$work_dir/ns.pid"
294 for {set i 0} {$i < 1000} {incr i} {
295 if {[file exists $ns_pid_file]} {
296 set f [open "$ns_pid_file" r]
297 set ns_pid [read $f]
298 close $f
299 break
300 }
301 # in sec
302 sleep 0.01
303 }
304 if {![info exists ns_pid] || $ns_pid eq ""} {
305 fail "$test: $subtest5: ns.pid was never found under $work_dir: [exec ls $work_dir]"
306 return
307 }
308
309 if {[file exists "$work_dir/target.pid"]} {
310 set f [open "$work_dir/target.pid" r]
311 set bg_pid [read $f]
312 close $f
313 }
314 set stp_opts "--target-namespace=123456 -d \$^WORK_DIR/tmp/newtarget/a.out"
315 regsub -all -- {\$\^WORK_DIR\y} $stp_opts $work_dir stp_opts
316 set cmd "stap $stp_opts './${test}_1.stp'"
317 set exit_code [run_cmd_2way $cmd out stderr]
318 catch {kill -INT -$bg_sh_pid 2}
319 catch {kill -KILL $bg_sh_pid}
320 set exp_out ""
321 is "${test_name}: stdout" $out $exp_out
322 isnt "${test_name}: exit code" $exit_code 0
323 set stderr_pat "^Failed to open file '/proc/123456/ns/mnt': No such file or directory
324 WARNING: \[^\\n\]*?\\ystaprun exited with status: 1
325 "
326 like "${test_name}: stderr" $stderr $stderr_pat "-lineanchor"
327 }
328 }