| 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" |
| 8 | |
| 9 | CURDIR=$(dirname "$0")/ |
| 10 | TESTDIR=$CURDIR/../.. |
| 11 | |
| 12 | TESTS_PER_NS=11 |
| 13 | |
| 14 | NUM_TESTS=$((TESTS_PER_NS * 8)) |
| 15 | |
| 16 | source "$TESTDIR/utils/utils.sh" |
| 17 | |
| 18 | # MUST set TESTDIR before calling those functions |
| 19 | function add_context_kernel_skip_ok() |
| 20 | { |
| 21 | local session_name=$1 |
| 22 | local channel_name=$2 |
| 23 | local context_name=$3 |
| 24 | local skip_num=$4 |
| 25 | |
| 26 | local ret |
| 27 | |
| 28 | "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" add-context -k \ |
| 29 | -s "$session_name" -c "$channel_name" \ |
| 30 | -t "$context_name" 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST" |
| 31 | ret=$? |
| 32 | |
| 33 | if [ "$ret" == "4" ]; then |
| 34 | skip 0 "Current kernel doesn't implement '$context_name' context" $((skip_num + 1)) |
| 35 | else |
| 36 | ok $ret "Add context command for type: $context_name" |
| 37 | fi |
| 38 | |
| 39 | return $ret |
| 40 | } |
| 41 | |
| 42 | function enable_kernel_lttng_event_filter_ok() |
| 43 | { |
| 44 | local session_name=$1 |
| 45 | local syscall_name=$2 |
| 46 | local channel_name=$3 |
| 47 | local filter=$4 |
| 48 | |
| 49 | "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" enable-event -k \ |
| 50 | -c "$channel_name" -s "$session_name" \ |
| 51 | --syscall "$syscall_name" \ |
| 52 | -f "$filter" \ |
| 53 | 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST" |
| 54 | |
| 55 | ok $? "Add syscall with filter" |
| 56 | } |
| 57 | |
| 58 | function test_ns() |
| 59 | { |
| 60 | local ns=$1 |
| 61 | |
| 62 | local session_name="${ns}_ns" |
| 63 | local chan_name="${ns}_ns" |
| 64 | local context_name="${ns}_ns" |
| 65 | |
| 66 | local trace_path |
| 67 | local ns_inode |
| 68 | |
| 69 | # Check if the kernel has support for this ns type |
| 70 | if [ ! -f "/proc/$$/ns/$ns" ]; then |
| 71 | skip 0 "System has no $ns namespace support" $TESTS_PER_NS |
| 72 | return |
| 73 | fi |
| 74 | |
| 75 | # Get the current ns inode number |
| 76 | ns_inode=$(stat -c '%i' -L "/proc/$$/ns/$ns") |
| 77 | ok $? "Get current $ns namespace inode: $ns_inode" |
| 78 | |
| 79 | trace_path=$(mktemp --tmpdir -d tmp.{FUNCNAME[0]}_trace_path.XXXXXX) |
| 80 | |
| 81 | start_lttng_sessiond |
| 82 | |
| 83 | create_lttng_session_ok "$session_name" "$trace_path" |
| 84 | enable_kernel_lttng_channel_ok "$session_name" "$chan_name" |
| 85 | add_context_kernel_skip_ok "$session_name" "$chan_name" "$context_name" 4 |
| 86 | if [ "$?" != "4" ]; then |
| 87 | enable_kernel_lttng_event_filter_ok "$session_name" "read" "$chan_name" "\$ctx.$context_name == $ns_inode" |
| 88 | start_lttng_tracing_ok "$session_name" |
| 89 | |
| 90 | # Make sure there is at least one read syscall |
| 91 | cat /proc/cmdline >/dev/null |
| 92 | |
| 93 | stop_lttng_tracing_ok "$session_name" |
| 94 | |
| 95 | # Check that the events contain the right namespace inode number |
| 96 | validate_trace "${ns}_ns = $ns_inode" "$trace_path" |
| 97 | fi |
| 98 | |
| 99 | destroy_lttng_session_ok "$session_name" |
| 100 | stop_lttng_sessiond |
| 101 | |
| 102 | rm -rf "$trace_path" |
| 103 | } |
| 104 | |
| 105 | |
| 106 | plan_tests $NUM_TESTS |
| 107 | |
| 108 | print_test_banner "$TEST_DESC" |
| 109 | |
| 110 | |
| 111 | isroot=0 |
| 112 | if [ "$(id -u)" == "0" ]; then |
| 113 | isroot=1 |
| 114 | fi |
| 115 | |
| 116 | skip $isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" && exit 0 |
| 117 | |
| 118 | |
| 119 | system_has_ns=0 |
| 120 | if [ -d "/proc/$$/ns" ]; then |
| 121 | system_has_ns=1 |
| 122 | fi |
| 123 | |
| 124 | skip $system_has_ns "System does not support namespaces" $NUM_TESTS && exit 0 |
| 125 | |
| 126 | |
| 127 | validate_lttng_modules_present |
| 128 | |
| 129 | test_ns cgroup |
| 130 | test_ns ipc |
| 131 | test_ns mnt |
| 132 | test_ns net |
| 133 | test_ns pid |
| 134 | test_ns time |
| 135 | test_ns user |
| 136 | test_ns uts |