Debian Codegraph @

tests/regression/kernel/test_ns_contexts_change

raw
1 #!/bin/bash
2 #
3 # SPDX-FileCopyrightText: 2019 Michael Jeanson <[email protected]>
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"
15
16 TESTS_PER_NS=21
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 _run_lttng_cmd "$(lttng_client_log_file)" "$(lttng_client_err_file)" add-context -k \
34 -s "$session_name" -c "$channel_name" -t "$context_name"
35 ret=$?
36
37 if [ "$ret" == "4" ]; then
38 skip 0 "Current kernel doesn't implement '$context_name' context" $((skip_num + 1))
39 else
40 ok $ret "Add context command for type: $context_name"
41 fi
42
43 return $ret
44 }
45
46 function enable_kernel_lttng_event_filter_ok()
47 {
48 local session_name=$1
49 local syscall_name=$2
50 local channel_name=$3
51 local filter=$4
52
53 _run_lttng_cmd "$(lttng_client_log_file)" "$(lttng_client_err_file)" enable-event -k \
54 -c "$channel_name" -s "$session_name" \ --syscall "$syscall_name" \
55 -f "$filter"
56
57 ok $? "Add syscall with filter"
58 }
59
60 function test_ns()
61 {
62 local ns=$1
63
64 local session_name="${ns}_ns"
65 local chan_name="${ns}_ns"
66 local context_name="${ns}_ns"
67
68 local trace_path
69 local ns_inode
70 local file_sync_wait_before_unshare
71 local file_sync_wait_after_unshare
72 local file_sync_signal_after_unshare
73
74 # Check if the kernel has support for this ns type
75 if [ ! -f "/proc/$$/ns/$ns" ]; then
76 skip 0 "System has no $ns namespace support" $TESTS_PER_NS
77 return
78 fi
79
80 trace_path=$(mktemp -d -t "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
81 file_sync_wait_before_unshare=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_unshare.XXXXXX")
82 file_sync_wait_after_unshare=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_unshare.XXXXXX")
83 file_sync_signal_after_unshare=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_signal_after_unshare.XXXXXX")
84
85 # Get the current ns inode number
86 ns_inode=$(stat -c '%i' -L "/proc/$$/ns/$ns")
87 ok $? "Get current $ns namespace inode: $ns_inode" || ns_inode="invalid"
88
89 $TESTAPP_BIN -n "$ns" -a "$file_sync_wait_after_unshare" -b "$file_sync_wait_before_unshare" -s "$file_sync_signal_after_unshare" &
90 ok $? "Launch test app."
91 app_pid=$!
92
93 app_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns")
94 ok $? "Get app current $ns namespace inode: $app_ns_inode" || app_ns_inode="invalid"
95
96 lttng_modules_loaded_fail
97 start_lttng_sessiond
98
99 create_lttng_session_ok "$session_name" "$trace_path"
100 enable_kernel_lttng_channel_ok "$session_name" "$chan_name"
101 add_context_kernel_skip_ok "$session_name" "$chan_name" "$context_name" 10
102 if [ "$?" != "4" ]; then
103 lttng_enable_kernel_syscall_ok "$session_name" "unshare" "$chan_name"
104 lttng_track_pid_ok "$app_pid"
105 start_lttng_tracing_ok "$session_name"
106
107 touch "$file_sync_wait_before_unshare"
108
109 while [ ! -f "$file_sync_signal_after_unshare" ]; do
110 # Break if the app failed / died
111 if ! kill -0 "$app_pid" ; then
112 break
113 echo "# App failed"
114 fi
115 echo "# Waiting for app..."
116 sleep 0.5
117 done
118
119 app_unshare_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns")
120 ok $? "Get app current $ns namespace inode: $app_unshare_ns_inode" || app_unshare_ns_inode="invalid"
121
122 test "$app_ns_inode" != "invalid" && test "$app_unshare_ns_inode" != "invalid" && test "$app_ns_inode" != "$app_unshare_ns_inode"
123 ok $? "Reported namespace inode changed after unshare"
124
125 touch "$file_sync_wait_after_unshare"
126
127 stop_lttng_tracing_ok "$session_name"
128
129 # Check that the events contain the right namespace inode number
130 validate_trace_count "${ns}_ns = $app_ns_inode" "$trace_path" 1
131 validate_trace_count "${ns}_ns = $app_unshare_ns_inode" "$trace_path" 1
132 else
133 touch "$file_sync_wait_before_unshare"
134
135 while [ ! -f "$file_sync_signal_after_unshare" ]; do
136 # Break if the app failed / died
137 if ! kill -0 "$app_pid" ; then
138 break
139 echo "# App failed"
140 fi
141 echo "# Waiting for app..."
142 sleep 0.5
143 done
144 touch "$file_sync_wait_after_unshare"
145 fi
146
147
148 # stop and destroy
149 destroy_lttng_session_ok "$session_name"
150 stop_lttng_sessiond
151 lttng_modules_loaded_fail
152
153 rm -rf "$trace_path"
154 rm -f "$file_sync_wait_after_unshare"
155 rm -f "$file_sync_wait_before_unshare"
156 rm -f "$file_sync_signal_after_unshare"
157 }
158
159
160 plan_tests $NUM_TESTS
161
162 print_test_banner "$TEST_DESC"
163
164 check_skip_kernel_test "$NUM_TESTS" "Skipping all tests." && exit 0
165
166 system_has_ns=0
167 if [ -d "/proc/$$/ns" ]; then
168 system_has_ns=1
169 fi
170
171 skip $system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0
172
173
174 validate_lttng_modules_present
175
176 test_ns cgroup
177 test_ns ipc
178 test_ns mnt
179 test_ns net
180 #test_ns pid # pid_ns is special, can't be changed that way
181 #test_ns time # time_ns is special, can't be changed that way
182 test_ns user
183 test_ns uts
184 set +x