Commit | Line | Data |
---|---|---|
5f0d4e78 MJ |
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 | # Let the app do it's thing before entering the synchronisation loop | |
123 | sleep 0.5 | |
124 | ||
125 | while [ ! -f "$file_sync_signal_after_unshare" ]; do | |
126 | # Break if the app failed / died | |
127 | if [ ! -f "/proc/$app_pid" ]; then | |
128 | break | |
129 | fi | |
130 | echo "# Waiting for app..." | |
131 | sleep 0.5 | |
132 | done | |
133 | ||
134 | app_unshare_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns") | |
135 | ok $? "Get app current $ns namespace inode: $app_unshare_ns_inode" || app_unshare_ns_inode="invalid" | |
136 | ||
137 | test "$app_ns_inode" != "invalid" && test "$app_unshare_ns_inode" != "invalid" && test "$app_ns_inode" != "$app_unshare_ns_inode" | |
138 | ok $? "Reported namespace inode changed after unshare" | |
139 | ||
140 | touch "$file_sync_wait_after_unshare" | |
141 | ||
142 | stop_lttng_tracing_ok "$session_name" | |
143 | ||
144 | # Check that the events contain the right namespace inode number | |
145 | validate_trace_count "${ns}_ns = $app_ns_inode" "$trace_path" 1 | |
146 | validate_trace_count "${ns}_ns = $app_unshare_ns_inode" "$trace_path" 1 | |
147 | fi | |
148 | ||
149 | # stop and destroy | |
150 | destroy_lttng_session_ok "$session_name" | |
151 | stop_lttng_sessiond | |
152 | ||
153 | rm -rf "$trace_path" | |
154 | rm -f "$file_sync_wait_after_unshare" | |
155 | rm -f "$file_sync_wait_before_unshare" | |
156 | rm -f "$file_sync_signal_after_unshare" | |
157 | } | |
158 | ||
159 | ||
160 | plan_tests $NUM_TESTS | |
161 | ||
162 | print_test_banner "$TEST_DESC" | |
163 | ||
164 | ||
165 | isroot=0 | |
166 | if [ "$(id -u)" == "0" ]; then | |
167 | isroot=1 | |
168 | fi | |
169 | ||
170 | skip $isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" && exit 0 | |
171 | ||
172 | ||
173 | system_has_ns=0 | |
174 | if [ -d "/proc/$$/ns" ]; then | |
175 | system_has_ns=1 | |
176 | fi | |
177 | ||
178 | skip $system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0 | |
179 | ||
180 | ||
181 | validate_lttng_modules_present | |
182 | ||
183 | test_ns cgroup | |
184 | test_ns ipc | |
185 | test_ns mnt | |
186 | test_ns net | |
187 | #test_ns pid # pid_ns is special, can't be changed that way | |
188 | test_ns user | |
189 | test_ns uts |