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