| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> |
| 4 | # |
| 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. |
| 8 | # |
| 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 |
| 12 | # details. |
| 13 | # |
| 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 |
| 17 | |
| 18 | TEST_DESC="UST - Namespace contexts change" |
| 19 | |
| 20 | CURDIR=$(dirname "$0")/ |
| 21 | TESTDIR=$CURDIR/../../.. |
| 22 | |
| 23 | TESTAPP_PATH="$TESTDIR/utils/testapp" |
| 24 | TESTAPP_NAME="gen-ust-events-ns" |
| 25 | TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME" |
| 26 | NUM_EVENT=1000 |
| 27 | EVENT_NAME="tp:tptest" |
| 28 | |
| 29 | TESTS_PER_NS=16 |
| 30 | |
| 31 | NUM_TESTS=$((TESTS_PER_NS * 5)) |
| 32 | |
| 33 | source "$TESTDIR/utils/utils.sh" |
| 34 | |
| 35 | # MUST set TESTDIR before calling those functions |
| 36 | |
| 37 | function test_ns() |
| 38 | { |
| 39 | local ns=$1 |
| 40 | |
| 41 | local session_name="${ns}_ns" |
| 42 | local chan_name="${ns}_ns" |
| 43 | local context_name="${ns}_ns" |
| 44 | |
| 45 | local trace_path |
| 46 | local ns_inode |
| 47 | local file_sync_before_last |
| 48 | local file_sync_after_unshare |
| 49 | |
| 50 | # Check if the kernel has support for this ns type |
| 51 | if [ ! -f "/proc/$$/ns/$ns" ]; then |
| 52 | skip 0 "System has no $ns namespace support" $TESTS_PER_NS |
| 53 | return |
| 54 | fi |
| 55 | |
| 56 | # Get the current ns inode number |
| 57 | ns_inode=$(stat -c '%i' -L "/proc/$$/ns/$ns") |
| 58 | ok $? "Get current $ns namespace inode: $ns_inode" || ns_inode="invalid" |
| 59 | |
| 60 | trace_path=$(mktemp -d) |
| 61 | file_sync_before_last=$(mktemp -u) |
| 62 | file_sync_after_unshare=$(mktemp -u) |
| 63 | |
| 64 | start_lttng_sessiond |
| 65 | |
| 66 | create_lttng_session_ok "$session_name" "$trace_path" |
| 67 | enable_ust_lttng_channel_ok "$session_name" "$chan_name" |
| 68 | add_context_ust_ok "$session_name" "$chan_name" "$context_name" |
| 69 | enable_ust_lttng_event_ok "$session_name" "$EVENT_NAME" "$chan_name" |
| 70 | start_lttng_tracing_ok "$session_name" |
| 71 | |
| 72 | $TESTAPP_BIN -n "$ns" -i $NUM_EVENT -a "$file_sync_after_unshare" -b "$file_sync_before_last" & |
| 73 | app_pid=$! |
| 74 | |
| 75 | # Let the app do it's thing before entering the synchronisation loop |
| 76 | sleep 0.5 |
| 77 | |
| 78 | while [ ! -f "$file_sync_after_unshare" ]; do |
| 79 | # Break if the app failed / died |
| 80 | if [ ! -f "/proc/$app_pid" ]; then |
| 81 | break |
| 82 | fi |
| 83 | echo "# Waiting for app..." |
| 84 | sleep 0.5 |
| 85 | done |
| 86 | |
| 87 | app_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns") |
| 88 | ok $? "Get current $ns namespace inode: $app_ns_inode" || app_ns_inode="invalid" |
| 89 | |
| 90 | test "$ns_inode" != "invalid" && test "$app_ns_inode" != "invalid" && test "$ns_inode" != "$app_ns_inode" |
| 91 | ok $? "Reported namespace inode changed after unshare" |
| 92 | |
| 93 | touch "$file_sync_before_last" |
| 94 | |
| 95 | # stop and destroy |
| 96 | stop_lttng_tracing_ok "$session_name" |
| 97 | destroy_lttng_session_ok "$session_name" |
| 98 | stop_lttng_sessiond |
| 99 | |
| 100 | # Check that the events contain the right namespace inode number |
| 101 | validate_trace_count "${ns}_ns = $ns_inode" "$trace_path" $NUM_EVENT |
| 102 | validate_trace_count "${ns}_ns = $app_ns_inode" "$trace_path" $NUM_EVENT |
| 103 | |
| 104 | rm -rf "$trace_path" |
| 105 | rm -f "$file_sync_before_last" |
| 106 | rm -f "$file_sync_after_unshare" |
| 107 | } |
| 108 | |
| 109 | |
| 110 | plan_tests $NUM_TESTS |
| 111 | |
| 112 | print_test_banner "$TEST_DESC" |
| 113 | |
| 114 | isroot=0 |
| 115 | if [ "$(id -u)" == "0" ]; then |
| 116 | isroot=1 |
| 117 | fi |
| 118 | |
| 119 | skip $isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" && exit 0 |
| 120 | |
| 121 | system_has_ns=0 |
| 122 | if [ -d "/proc/$$/ns" ]; then |
| 123 | system_has_ns=1 |
| 124 | fi |
| 125 | |
| 126 | skip $system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0 |
| 127 | |
| 128 | |
| 129 | test_ns cgroup |
| 130 | test_ns ipc |
| 131 | test_ns mnt |
| 132 | test_ns net |
| 133 | #test_ns pid # pid_ns is special, can't be changed that way |
| 134 | #test_ns user # user_ns can only be change when the app is single threaded, this is always false for an ust instrumented app |
| 135 | test_ns uts |