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