Cleanup: tests: name all temporary files to better identify leakage
[lttng-tools.git] / tests / regression / kernel / test_ns_contexts_change
1 #!/bin/bash
2 #
3 # Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
4 #
5 # SPDX-License-Identifier: LGPL-2.1-only
6
7 TEST_DESC="Kernel tracer - Namespace contexts change"
8
9 CURDIR=$(dirname "$0")/
10 TESTDIR=$CURDIR/../..
11
12 TESTAPP_PATH="$TESTDIR/utils/testapp"
13 TESTAPP_NAME="gen-ns-events"
14 TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
15
16 TESTS_PER_NS=19
17
18 NUM_TESTS=$((TESTS_PER_NS * 6))
19
20 source "$TESTDIR/utils/utils.sh"
21
22 # MUST set TESTDIR before calling those functions
23
24 function add_context_kernel_skip_ok()
25 {
26 local session_name=$1
27 local channel_name=$2
28 local context_name=$3
29 local skip_num=$4
30
31 local ret
32
33 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" add-context -k \
34 -s "$session_name" -c "$channel_name" \
35 -t "$context_name" 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
36 ret=$?
37
38 if [ "$ret" == "4" ]; then
39 skip 0 "Current kernel doesn't implement '$context_name' context" $((skip_num + 1))
40 else
41 ok $ret "Add context command for type: $context_name"
42 fi
43
44 return $ret
45 }
46
47 function enable_kernel_lttng_event_filter_ok()
48 {
49 local session_name=$1
50 local syscall_name=$2
51 local channel_name=$3
52 local filter=$4
53
54 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" enable-event -k \
55 -c "$channel_name" -s "$session_name" \
56 --syscall "$syscall_name" \
57 -f "$filter" \
58 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
59
60 ok $? "Add syscall with filter"
61 }
62
63 function test_ns()
64 {
65 local ns=$1
66
67 local session_name="${ns}_ns"
68 local chan_name="${ns}_ns"
69 local context_name="${ns}_ns"
70
71 local trace_path
72 local ns_inode
73 local file_sync_wait_before_unshare
74 local file_sync_wait_after_unshare
75 local file_sync_signal_after_unshare
76
77 # Check if the kernel has support for this ns type
78 if [ ! -f "/proc/$$/ns/$ns" ]; then
79 skip 0 "System has no $ns namespace support" $TESTS_PER_NS
80 return
81 fi
82
83 trace_path=$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
84 file_sync_wait_before_unshare=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_before_unshare.XXXXXX")
85 file_sync_wait_after_unshare=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_after_unshare.XXXXXX")
86 file_sync_signal_after_unshare=$(mktemp --tmpdir -u "tmp.${FUNCNAME[0]}_sync_signal_after_unshare.XXXXXX")
87
88 # Get the current ns inode number
89 ns_inode=$(stat -c '%i' -L "/proc/$$/ns/$ns")
90 ok $? "Get current $ns namespace inode: $ns_inode" || ns_inode="invalid"
91
92 $TESTAPP_BIN -n "$ns" -a "$file_sync_wait_after_unshare" -b "$file_sync_wait_before_unshare" -s "$file_sync_signal_after_unshare" &
93 ok $? "Launch test app."
94 app_pid=$!
95
96 app_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns")
97 ok $? "Get app current $ns namespace inode: $app_ns_inode" || app_ns_inode="invalid"
98
99 start_lttng_sessiond
100
101 create_lttng_session_ok "$session_name" "$trace_path"
102 enable_kernel_lttng_channel_ok "$session_name" "$chan_name"
103 add_context_kernel_skip_ok "$session_name" "$chan_name" "$context_name" 10
104 if [ "$?" != "4" ]; then
105 lttng_enable_kernel_syscall_ok "$session_name" "unshare" "$chan_name"
106 lttng_track_pid_ok "$app_pid"
107 start_lttng_tracing_ok "$session_name"
108
109 touch "$file_sync_wait_before_unshare"
110
111 while [ ! -f "$file_sync_signal_after_unshare" ]; do
112 # Break if the app failed / died
113 if ! kill -0 "$app_pid" ; then
114 break
115 echo "# App failed"
116 fi
117 echo "# Waiting for app..."
118 sleep 0.5
119 done
120
121 app_unshare_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns")
122 ok $? "Get app current $ns namespace inode: $app_unshare_ns_inode" || app_unshare_ns_inode="invalid"
123
124 test "$app_ns_inode" != "invalid" && test "$app_unshare_ns_inode" != "invalid" && test "$app_ns_inode" != "$app_unshare_ns_inode"
125 ok $? "Reported namespace inode changed after unshare"
126
127 touch "$file_sync_wait_after_unshare"
128
129 stop_lttng_tracing_ok "$session_name"
130
131 # Check that the events contain the right namespace inode number
132 validate_trace_count "${ns}_ns = $app_ns_inode" "$trace_path" 1
133 validate_trace_count "${ns}_ns = $app_unshare_ns_inode" "$trace_path" 1
134 fi
135
136 # stop and destroy
137 destroy_lttng_session_ok "$session_name"
138 stop_lttng_sessiond
139
140 rm -rf "$trace_path"
141 rm -f "$file_sync_wait_after_unshare"
142 rm -f "$file_sync_wait_before_unshare"
143 rm -f "$file_sync_signal_after_unshare"
144 }
145
146
147 plan_tests $NUM_TESTS
148
149 print_test_banner "$TEST_DESC"
150
151
152 isroot=0
153 if [ "$(id -u)" == "0" ]; then
154 isroot=1
155 fi
156
157 skip $isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" && exit 0
158
159
160 system_has_ns=0
161 if [ -d "/proc/$$/ns" ]; then
162 system_has_ns=1
163 fi
164
165 skip $system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0
166
167
168 validate_lttng_modules_present
169
170 test_ns cgroup
171 test_ns ipc
172 test_ns mnt
173 test_ns net
174 #test_ns pid # pid_ns is special, can't be changed that way
175 #test_ns time # time_ns is special, can't be changed that way
176 test_ns user
177 test_ns uts
This page took 0.062072 seconds and 5 git commands to generate.