3 # Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
5 # This library is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation; version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this library; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 TEST_DESC
="Kernel tracer - Namespace contexts change"
20 CURDIR
=$
(dirname "$0")/
23 TESTAPP_PATH
="$TESTDIR/utils/testapp"
24 TESTAPP_NAME
="gen-ns-events"
25 TESTAPP_BIN
="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
29 NUM_TESTS
=$
((TESTS_PER_NS
* 6))
31 source "$TESTDIR/utils/utils.sh"
33 # MUST set TESTDIR before calling those functions
35 function add_context_kernel_skip_ok
()
44 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" add-context
-k \
45 -s "$session_name" -c "$channel_name" \
46 -t "$context_name" 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
49 if [ "$ret" == "4" ]; then
50 skip
0 "Current kernel doesn't implement '$context_name' context" $
((skip_num
+ 1))
52 ok
$ret "Add context command for type: $context_name"
58 function enable_kernel_lttng_event_filter_ok
()
65 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" enable-event
-k \
66 -c "$channel_name" -s "$session_name" \
67 --syscall "$syscall_name" \
69 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
71 ok $?
"Add syscall with filter"
78 local session_name
="${ns}_ns"
79 local chan_name
="${ns}_ns"
80 local context_name
="${ns}_ns"
84 local file_sync_wait_before_unshare
85 local file_sync_wait_after_unshare
86 local file_sync_signal_after_unshare
88 # Check if the kernel has support for this ns type
89 if [ ! -f "/proc/$$/ns/$ns" ]; then
90 skip
0 "System has no $ns namespace support" $TESTS_PER_NS
94 trace_path
=$
(mktemp
-d)
95 file_sync_wait_before_unshare
=$
(mktemp
-u)
96 file_sync_wait_after_unshare
=$
(mktemp
-u)
97 file_sync_signal_after_unshare
=$
(mktemp
-u)
99 # Get the current ns inode number
100 ns_inode
=$
(stat
-c '%i' -L "/proc/$$/ns/$ns")
101 ok $?
"Get current $ns namespace inode: $ns_inode" || ns_inode
="invalid"
103 $TESTAPP_BIN -n "$ns" -a "$file_sync_wait_after_unshare" -b "$file_sync_wait_before_unshare" -s "$file_sync_signal_after_unshare" &
104 ok $?
"Launch test app."
107 app_ns_inode
=$
(stat
-c '%i' -L "/proc/$app_pid/ns/$ns")
108 ok $?
"Get app current $ns namespace inode: $app_ns_inode" || app_ns_inode
="invalid"
112 create_lttng_session_ok
"$session_name" "$trace_path"
113 enable_kernel_lttng_channel_ok
"$session_name" "$chan_name"
114 add_context_kernel_skip_ok
"$session_name" "$chan_name" "$context_name" 10
115 if [ "$?" != "4" ]; then
116 lttng_enable_kernel_syscall_ok
"$session_name" "unshare" "$chan_name"
117 lttng_track_pid_ok
"$app_pid"
118 start_lttng_tracing_ok
"$session_name"
120 touch "$file_sync_wait_before_unshare"
122 while [ ! -f "$file_sync_signal_after_unshare" ]; do
123 # Break if the app failed / died
124 if ! kill -0 "$app_pid" ; then
128 echo "# Waiting for app..."
132 app_unshare_ns_inode
=$
(stat
-c '%i' -L "/proc/$app_pid/ns/$ns")
133 ok $?
"Get app current $ns namespace inode: $app_unshare_ns_inode" || app_unshare_ns_inode
="invalid"
135 test "$app_ns_inode" != "invalid" && test "$app_unshare_ns_inode" != "invalid" && test "$app_ns_inode" != "$app_unshare_ns_inode"
136 ok $?
"Reported namespace inode changed after unshare"
138 touch "$file_sync_wait_after_unshare"
140 stop_lttng_tracing_ok
"$session_name"
142 # Check that the events contain the right namespace inode number
143 validate_trace_count
"${ns}_ns = $app_ns_inode" "$trace_path" 1
144 validate_trace_count
"${ns}_ns = $app_unshare_ns_inode" "$trace_path" 1
148 destroy_lttng_session_ok
"$session_name"
152 rm -f "$file_sync_wait_after_unshare"
153 rm -f "$file_sync_wait_before_unshare"
154 rm -f "$file_sync_signal_after_unshare"
158 plan_tests
$NUM_TESTS
160 print_test_banner
"$TEST_DESC"
164 if [ "$(id -u)" == "0" ]; then
168 skip
$isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" && exit 0
172 if [ -d "/proc/$$/ns" ]; then
176 skip
$system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0
179 validate_lttng_modules_present
185 #test_ns pid # pid_ns is special, can't be changed that way