Add kernel and UST time namespace context
[lttng-tools.git] / tests / regression / kernel / test_ns_contexts_change
CommitLineData
5f0d4e78
MJ
1#!/bin/bash
2#
3# Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
4#
9d16b343 5# SPDX-License-Identifier: LGPL-2.1-only
5f0d4e78
MJ
6
7TEST_DESC="Kernel tracer - Namespace contexts change"
8
9CURDIR=$(dirname "$0")/
10TESTDIR=$CURDIR/../..
11
12TESTAPP_PATH="$TESTDIR/utils/testapp"
13TESTAPP_NAME="gen-ns-events"
14TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
15
16TESTS_PER_NS=19
17
18NUM_TESTS=$((TESTS_PER_NS * 6))
19
20source "$TESTDIR/utils/utils.sh"
21
22# MUST set TESTDIR before calling those functions
23
24function add_context_kernel_skip_ok()
25{
26 local session_name=$1
27 local channel_name=$2
28 local context_name=$3
29 local skip_num=$4
30
31 local ret
32
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"
36 ret=$?
37
38 if [ "$ret" == "4" ]; then
39 skip 0 "Current kernel doesn't implement '$context_name' context" $((skip_num + 1))
40 else
41 ok $ret "Add context command for type: $context_name"
42 fi
43
44 return $ret
45}
46
47function enable_kernel_lttng_event_filter_ok()
48{
49 local session_name=$1
50 local syscall_name=$2
51 local channel_name=$3
52 local filter=$4
53
54 "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" enable-event -k \
55 -c "$channel_name" -s "$session_name" \
56 --syscall "$syscall_name" \
57 -f "$filter" \
58 1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
59
60 ok $? "Add syscall with filter"
61}
62
63function test_ns()
64{
65 local ns=$1
66
67 local session_name="${ns}_ns"
68 local chan_name="${ns}_ns"
69 local context_name="${ns}_ns"
70
71 local trace_path
72 local ns_inode
73 local file_sync_wait_before_unshare
74 local file_sync_wait_after_unshare
75 local file_sync_signal_after_unshare
76
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
80 return
81 fi
82
83 trace_path=$(mktemp -d)
84 file_sync_wait_before_unshare=$(mktemp -u)
85 file_sync_wait_after_unshare=$(mktemp -u)
86 file_sync_signal_after_unshare=$(mktemp -u)
87
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"
91
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."
94 app_pid=$!
95
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"
98
99 start_lttng_sessiond
100
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"
108
109 touch "$file_sync_wait_before_unshare"
110
5f0d4e78
MJ
111 while [ ! -f "$file_sync_signal_after_unshare" ]; do
112 # Break if the app failed / died
70a7f9f7 113 if ! kill -0 "$app_pid" ; then
5f0d4e78 114 break
70a7f9f7 115 echo "# App failed"
5f0d4e78
MJ
116 fi
117 echo "# Waiting for app..."
118 sleep 0.5
119 done
120
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"
123
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"
126
127 touch "$file_sync_wait_after_unshare"
128
129 stop_lttng_tracing_ok "$session_name"
130
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
134 fi
135
136 # stop and destroy
137 destroy_lttng_session_ok "$session_name"
138 stop_lttng_sessiond
139
140 rm -rf "$trace_path"
141 rm -f "$file_sync_wait_after_unshare"
142 rm -f "$file_sync_wait_before_unshare"
143 rm -f "$file_sync_signal_after_unshare"
144}
145
146
147plan_tests $NUM_TESTS
148
149print_test_banner "$TEST_DESC"
150
151
152isroot=0
153if [ "$(id -u)" == "0" ]; then
154 isroot=1
155fi
156
157skip $isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" && exit 0
158
159
160system_has_ns=0
161if [ -d "/proc/$$/ns" ]; then
162 system_has_ns=1
163fi
164
165skip $system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0
166
167
168validate_lttng_modules_present
169
170test_ns cgroup
171test_ns ipc
172test_ns mnt
173test_ns net
174#test_ns pid # pid_ns is special, can't be changed that way
d37ac3cd 175#test_ns time # time_ns is special, can't be changed that way
5f0d4e78
MJ
176test_ns user
177test_ns uts
This page took 0.043359 seconds and 4 git commands to generate.