3 # Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
5 # SPDX-License-Identifier: LGPL-2.1-only
7 TEST_DESC
="Kernel tracer - Namespace contexts change"
9 CURDIR
=$
(dirname "$0")/
12 TESTAPP_PATH
="$TESTDIR/utils/testapp"
13 TESTAPP_NAME
="gen-ns-events"
14 TESTAPP_BIN
="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
18 NUM_TESTS
=$
((TESTS_PER_NS
* 6))
20 source "$TESTDIR/utils/utils.sh"
22 # MUST set TESTDIR before calling those functions
24 function add_context_kernel_skip_ok
()
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"
38 if [ "$ret" == "4" ]; then
39 skip
0 "Current kernel doesn't implement '$context_name' context" $
((skip_num
+ 1))
41 ok
$ret "Add context command for type: $context_name"
47 function enable_kernel_lttng_event_filter_ok
()
54 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" enable-event
-k \
55 -c "$channel_name" -s "$session_name" \
56 --syscall "$syscall_name" \
58 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
60 ok $?
"Add syscall with filter"
67 local session_name
="${ns}_ns"
68 local chan_name
="${ns}_ns"
69 local context_name
="${ns}_ns"
73 local file_sync_wait_before_unshare
74 local file_sync_wait_after_unshare
75 local file_sync_signal_after_unshare
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
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")
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"
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."
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"
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"
109 touch "$file_sync_wait_before_unshare"
111 while [ ! -f "$file_sync_signal_after_unshare" ]; do
112 # Break if the app failed / died
113 if ! kill -0 "$app_pid" ; then
117 echo "# Waiting for app..."
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"
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"
127 touch "$file_sync_wait_after_unshare"
129 stop_lttng_tracing_ok
"$session_name"
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
137 destroy_lttng_session_ok
"$session_name"
141 rm -f "$file_sync_wait_after_unshare"
142 rm -f "$file_sync_wait_before_unshare"
143 rm -f "$file_sync_signal_after_unshare"
147 plan_tests
$NUM_TESTS
149 print_test_banner
"$TEST_DESC"
153 if [ "$(id -u)" == "0" ]; then
157 skip
$isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" && exit 0
161 if [ -d "/proc/$$/ns" ]; then
165 skip
$system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0
168 validate_lttng_modules_present
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