Fix: sessiond: notification: use after free of trigger object
[lttng-tools.git] / tests / regression / tools / notification / test_notification_multi_app
CommitLineData
434f8068
JR
1#!/bin/bash
2#
9d16b343 3# Copyright (C) 2017 Jonathan Rajotte <jonathan.rajotte-julien@efficiso.com>>
434f8068 4#
9d16b343 5# SPDX-License-Identifier: LGPL-2.1-only
434f8068
JR
6
7TEST_DESC="Notification"
8
9CURDIR=$(dirname $0)/
10TESTDIR=$CURDIR/../../../
11
434f8068
JR
12TESTPOINT=$(readlink -f ${CURDIR}/.libs/libpause_consumer.so)
13
14TESTAPP_PATH="$TESTDIR/utils/testapp"
15TESTAPP_NAME="gen-ust-events"
16TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
854382b8 17TESTAPP_STATE_FILE="$(mktemp -u)"
434f8068 18
854382b8 19NR_ITER=1000
434f8068
JR
20NR_USEC_WAIT=5
21
22SESSION_NAME="my_session"
854382b8 23CHANNEL_NAME="my_channel"
434f8068 24
434f8068 25
434f8068
JR
26DIR=$(readlink -f $TESTDIR)
27
1bd26228
JG
28PAGE_SIZE=$(getconf PAGE_SIZE)
29
854382b8
JR
30NUM_TEST_UST=50
31NUM_TEST_KERNEL=50
32NUM_TESTS=$(($NUM_TEST_UST + $NUM_TEST_KERNEL))
434f8068
JR
33
34source $TESTDIR/utils/utils.sh
25cb2044 35source $CURDIR/util_event_generator.sh
434f8068
JR
36
37consumerd_pipe=()
38file_sync_after_first_event=$(mktemp -u)
39
40# MUST set TESTDIR before calling those functions
41plan_tests $NUM_TESTS
42
43print_test_banner "$TEST_DESC"
44
45app_pids=()
867313e2 46
434f8068
JR
47function start_client {
48 local pid=-1
49 local output_file=$1
50 local session_name=$2
51 local channel_name=$3
52 local domain_type=$4
53 local buffer_usage_type=$5
54 local buffer_usage_threshold_type=$6
55 local buffer_usage_threshold_value=$7
56 local nr_expected_notification=$8
091fa780 57 local use_action_group=$9
434f8068 58
091fa780 59 ${CURDIR}/base_client ${session_name} ${channel_name} ${domain_type} ${buffer_usage_type} ${buffer_usage_threshold_type} ${buffer_usage_threshold_value} ${nr_expected_notification} ${use_action_group} > ${output_file} &
434f8068
JR
60 pid=$!
61
62 app_pids+=("$pid")
63}
64
65function wait_for_message ()
66{
345ed219
JR
67 local directory=$1
68 local file_pattern=$2
69 local message=$3
434f8068 70
345ed219 71 for file in $directory/${file_pattern}*; do
434f8068
JR
72 while(true); do
73 # Check for "error" message
74 grep -q "error:" ${file}
75 app_error=$?
76 if [ $app_error -eq "0" ] ; then
77 # An error occurred
78 fail "Waiting message: error logged see file content: ${message}, ${file}"
79 return 1
80 fi
81
82 grep -q "${message}" ${file}
83 if [[ "$?" -ne "0" ]]; then
84 # Lookup failed restart loop
85 diag "Lookup failed sleep and retry grep for: ${message}, ${file}"
86 sleep 0.25
87 continue
88 fi
89 break
90 done
91 done
92 pass "Message received: ${message}"
93 return 0
94}
95
96function print_errors ()
97{
345ed219
JR
98 local directory=$1
99 local file_pattern=$2
434f8068 100
345ed219 101 for file in $directory/${file_pattern}*; do
434f8068
JR
102 # Check for "error" message
103 error_message=$(grep "error:" ${file})
867313e2 104 if [[ "${error_message}x" != "x" ]]; then
434f8068
JR
105 diag "Errors for application ${file}:"
106 diag "${error_message}"
107 fi
108 done
109}
110
111function comm_consumerd ()
112{
113 local message=$1
114 local pipe=$2
115 echo -ne "${message}" > "${pipe}"
434f8068
JR
116}
117
118function stop_consumerd ()
119{
120 local pipe=$1
121 comm_consumerd "1" "$pipe"
434f8068
JR
122}
123
124function resume_consumerd ()
125{
126 local pipe=$1
127 comm_consumerd "\0" "$pipe"
434f8068
JR
128}
129
130function test_multi_app ()
131{
854382b8
JR
132 local domain_type=$1
133 local event_generator_pid=$2
134
434f8068
JR
135 local app_pids=()
136 local low_output_file_pattern="low_app_output_file_"
137 local high_output_file_pattern="high_app_output_file_"
345ed219 138 local output_dir=$(mktemp -d)
434f8068 139
345ed219 140 local testpoint_base_path=$(readlink -f "$output_dir/lttng.t_p_n_multi_app")
6a1eee92
JR
141 local testpoint_pipe_path=$(mktemp -u "${testpoint_base_path}.XXXXXX")
142
143 local nr_notification_expected=5
144 local nr_client_app=50
854382b8
JR
145 local domain_string=""
146 local event_name=""
147
148 case $domain_type in
149 ust)
150 domain_string=LTTNG_DOMAIN_UST
151 event_name="tp:tptest"
152 ;;
153 kernel)
154 domain_string=LTTNG_DOMAIN_KERNEL
155 event_name="lttng_test_filter_event"
156 ;;
157 *)
158 fail "Invalid domain type"
159 exit 1
160 ;;
161 esac
6a1eee92 162
434f8068 163 # Setup
6a1eee92 164 LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 CONSUMER_PAUSE_PIPE_PATH=${testpoint_pipe_path} LD_PRELOAD=${TESTPOINT}"
434f8068
JR
165 start_lttng_sessiond
166
434f8068 167 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
854382b8
JR
168 enable_${domain_type}_lttng_channel_ok $SESSION_NAME $CHANNEL_NAME --subbuf-size=$PAGE_SIZE
169 enable_${domain_type}_lttng_event_ok $SESSION_NAME $event_name $CHANNEL_NAME
434f8068
JR
170
171 # Fetch consumerd testpoint pipe information
172 # This is needed since the testpoint create a pipe with the consumer type suffixed
345ed219
JR
173 consumerd_pipe=()
174 for f in "$testpoint_pipe_path"*; do
434f8068
JR
175 consumerd_pipe+=("$f")
176 done
177
6a1eee92 178 for (( i = 0; i < $nr_client_app; i++ )); do
345ed219
JR
179 low_app_output_file=$output_dir/${low_output_file_pattern}${i}
180 high_app_output_file=$output_dir/${high_output_file_pattern}${i}
091fa780
FD
181 start_client $low_app_output_file $SESSION_NAME $CHANNEL_NAME $domain_string LOW RATIO 0.0 $nr_notification_expected $(( $i % 2))
182 start_client $high_app_output_file $SESSION_NAME $CHANNEL_NAME $domain_string HIGH RATIO 0.420 $nr_notification_expected $(( $i % 2))
434f8068
JR
183 done
184
345ed219
JR
185 wait_for_message $output_dir "${low_output_file_pattern}" "sync: ready"
186 wait_for_message $output_dir "${high_output_file_pattern}" "sync: ready"
434f8068
JR
187
188 # Test notification reception
6a1eee92 189 for (( i = 0; i < $nr_notification_expected; i++ )); do
434f8068
JR
190
191 # Stop consumerd consumption to force high notification
192 start_lttng_tracing_ok $SESSION_NAME
193 for pipe in "${consumerd_pipe[@]}"; do
194 stop_consumerd "${pipe}"
195 done
196
345ed219 197 wait_for_message $output_dir "${high_output_file_pattern}" "notification: high $i"
434f8068 198
854382b8
JR
199 # Put application in suspend mode to prevent double low
200 # notification and synchronize on state file.
201 kill -s SIGUSR1 $event_generator_pid
202 while [ ! -f "${TESTAPP_STATE_FILE}" ]; do
203 sleep 0.5
204 done
205
434f8068
JR
206 # Resume consumerd
207 for pipe in "${consumerd_pipe[@]}"; do
208 resume_consumerd "${pipe}"
209 done
210 # Stop tracing forcing full buffer consumption
7fe98a98 211 stop_lttng_tracing_ok $SESSION_NAME
434f8068
JR
212
213 # Check for notifications reception
345ed219 214 wait_for_message $output_dir "${low_output_file_pattern}" "notification: low $i"
434f8068
JR
215 ret=$?
216 ok $ret "Notifications $i received"
217 if [[ $ret -ne "0" ]]; then
218 # Error occurred bail out
219 break;
220 fi
854382b8
JR
221
222 # Put application in active mode and synchronize on state file.
223 kill -s SIGUSR1 $event_generator_pid
224 while [ -f "${TESTAPP_STATE_FILE}" ]; do
225 sleep 0.5
226 done
434f8068
JR
227 done
228
345ed219 229 wait_for_message $output_dir "${low_output_file_pattern}" "exit: 0"
434f8068
JR
230 ret=$?
231 ok $ret "Application for low notification terminated normally"
345ed219
JR
232 if [[ $ret -ne "0" ]]; then
233 print_errors $output_dir "${low_output_file_pattern}"
434f8068
JR
234 fi
235
345ed219 236 wait_for_message $output_dir "${high_output_file_pattern}" "exit: 0"
434f8068
JR
237 ret=$?
238 ok $ret "Application for high notification terminated normally"
345ed219
JR
239 if [[ $ret -ne "0" ]]; then
240 print_errors $output_dir "${high_output_file_pattern}"
434f8068
JR
241 fi
242
345ed219 243
854382b8 244 destroy_lttng_session_ok $SESSION_NAME
434f8068 245 stop_lttng_sessiond
854382b8
JR
246
247 for pipe in "${consumerd_pipe[@]}"; do
248 rm -rf "${pipe}"
249 done
81665716
JR
250
251 rm -rf $output_dir
854382b8
JR
252}
253
254function test_multi_app_ust ()
255{
256 diag "Multi client app UST notification"
25cb2044 257 ust_event_generator "$TESTAPP_BIN" "$TESTAPP_STATE_FILE" &
854382b8
JR
258 local generator_pid=$!
259
260 test_multi_app ust $generator_pid
261
25cb2044 262 kill -s SIGUSR2 $generator_pid 2> /dev/null
854382b8
JR
263 wait $generator_pid 2> /dev/null
264 rm -rf ${TESTAPP_STATE_FILE} 2> /dev/null
265}
266
267function test_multi_app_kernel ()
268{
269 diag "Multi client app kernel notification"
270 modprobe lttng-test
271
25cb2044 272 kernel_event_generator generate_filter_events $TESTAPP_STATE_FILE &
854382b8
JR
273 local generator_pid=$!
274
275 test_multi_app kernel $generator_pid
276
277
25cb2044 278 kill -s SIGUSR2 $generator_pid 2> /dev/null
854382b8
JR
279 wait $generator_pid 2> /dev/null
280 rm -rf ${TESTAPP_STATE_FILE} 2> /dev/null
281
d0e263e7 282 modprobe --remove lttng-test
854382b8
JR
283}
284
285function test_on_register_evaluation_ust ()
286{
287 diag "On register notification UST"
288
289 # Start app in infinite loop
25cb2044 290 ust_event_generator "$TESTAPP_BIN" "$TESTAPP_STATE_FILE" &
854382b8
JR
291 local generator_pid=$!
292
293 test_on_register_evaluation ust $generator_pid
294
25cb2044 295 kill -s SIGUSR2 $generator_pid 2> /dev/null
854382b8
JR
296 wait $generator_pid 2> /dev/null
297 rm -rf ${TESTAPP_STATE_FILE} 2> /dev/null
298
299}
300
301function test_on_register_evaluation_kernel()
302{
303 diag "On register notification kernel"
304
305 modprobe lttng-test
306
25cb2044 307 kernel_event_generator generate_filter_events $TESTAPP_STATE_FILE &
854382b8
JR
308 local generator_pid=$!
309
310 test_on_register_evaluation kernel $generator_pid
311
312
25cb2044 313 kill -s SIGUSR2 $generator_pid 2> /dev/null
854382b8
JR
314 wait $generator_pid 2> /dev/null
315 rm -rf ${TESTAPP_STATE_FILE} 2> /dev/null
316
d0e263e7 317 modprobe --remove lttng-test
434f8068
JR
318}
319
867313e2
JR
320function test_on_register_evaluation ()
321{
854382b8
JR
322 local domain_type=$1
323 local event_generator_pid=$2
324
867313e2
JR
325 local app_pids=()
326 local high_output_file_pattern="high_app_output_file_on_register_evaluation"
327
345ed219
JR
328 local output_dir=$(mktemp -d)
329 local testpoint_base_path=$(readlink -f "$output_dir/lttng.t_p_n_register_evaluation")
867313e2 330 local testpoint_pipe_path=$(mktemp -u "${testpoint_base_path}.XXXXXX")
854382b8
JR
331 local domain_string=""
332 local event_name=""
867313e2 333
854382b8
JR
334 case $domain_type in
335 ust)
336 domain_string=LTTNG_DOMAIN_UST
337 event_name="tp:tptest"
338 ;;
339 kernel)
340 domain_string=LTTNG_DOMAIN_KERNEL
341 event_name="lttng_test_filter_event"
342 ;;
343 *)
344 fail "Invalid domain type"
345 exit 1
346 ;;
347 esac
348
867313e2
JR
349 # Setup
350 LTTNG_SESSIOND_ENV_VARS="LTTNG_TESTPOINT_ENABLE=1 CONSUMER_PAUSE_PIPE_PATH=${testpoint_pipe_path} LD_PRELOAD=${TESTPOINT}"
351 start_lttng_sessiond
352
867313e2 353 create_lttng_session_ok $SESSION_NAME $TRACE_PATH
854382b8
JR
354 enable_${domain_type}_lttng_channel_ok $SESSION_NAME $CHANNEL_NAME --subbuf-size=$PAGE_SIZE
355 enable_${domain_type}_lttng_event_ok $SESSION_NAME $event_name $CHANNEL_NAME
867313e2
JR
356
357 # Fetch consumerd testpoint pipe information
358 # This is needed since the testpoint create a pipe with the consumer type suffixed
345ed219
JR
359 consumerd_pipe=()
360 for f in "$testpoint_pipe_path"*; do
867313e2
JR
361 consumerd_pipe+=("$f")
362 done
363
364 high_app_output_file=${high_output_file_pattern}.first_receiver
345ed219 365 high_app_output_path=$output_dir/${high_app_output_file}
091fa780 366 start_client $high_app_output_path $SESSION_NAME $CHANNEL_NAME $domain_string HIGH RATIO 0.420 1 0
867313e2 367
345ed219 368 wait_for_message $output_dir "${high_app_output_file}" "sync: ready"
867313e2
JR
369
370 # Stop consumerd consumption to force high notification
371 start_lttng_tracing_ok $SESSION_NAME
372
373 for pipe in "${consumerd_pipe[@]}"; do
374 stop_consumerd "${pipe}"
375 done
376
345ed219 377 wait_for_message $output_dir "${high_app_output_file}" "notification: high 0"
867313e2
JR
378
379 # Start a second receiver, the receiver should receive a high
380 # notification on subscription
381 high_app_output_file=${high_output_file_pattern}.second_receiver
345ed219 382 high_app_output_path=$output_dir/${high_app_output_file}
091fa780 383 start_client $high_app_output_path $SESSION_NAME $CHANNEL_NAME $domain_string HIGH RATIO 0.420 1 0
345ed219
JR
384 wait_for_message $output_dir "${high_app_output_file}" "sync: ready"
385 wait_for_message $output_dir "${high_app_output_file}" "notification: high 0"
867313e2
JR
386
387 # Resume consumerd
388 for pipe in "${consumerd_pipe[@]}"; do
389 resume_consumerd "${pipe}"
390 done
391
345ed219 392 wait_for_message $output_dir "${high_output_file_pattern}" "exit: 0"
867313e2
JR
393 ret=$?
394 ok $ret "Application for high notification terminated normally"
345ed219 395 if [[ $ret -ne "0" ]]; then
867313e2
JR
396 # Keep the file
397 print_errors "${high_output_file_pattern}"
398 fi
399
345ed219 400
854382b8 401 destroy_lttng_session_ok $SESSION_NAME
867313e2
JR
402 stop_lttng_sessiond
403
25cb2044 404 kill -s SIGUSR2 $generator_pid 2> /dev/null
854382b8
JR
405 wait $generator_pid 2> /dev/null
406
407 for pipe in "${consumerd_pipe[@]}"; do
408 rm -rf "${pipe}"
409 done
410
81665716 411 rm -rf "$output_dir"
867313e2
JR
412}
413
434f8068
JR
414
415TESTS=(
854382b8
JR
416 test_multi_app_ust
417 test_on_register_evaluation_ust
434f8068
JR
418)
419
854382b8 420if [ "$(id -u)" == "0" ]; then
fb180e6e 421 validate_lttng_modules_present
854382b8
JR
422 TESTS+=(
423 test_multi_app_kernel
424 test_on_register_evaluation_kernel
425)
426else
427 skip 0 "Root access is needed. Skipping all kernel multi-app notification tests." $NUM_TEST_KERNEL
428fi
429
430
434f8068
JR
431for fct_test in ${TESTS[@]};
432do
433 TRACE_PATH=$(mktemp -d)
434
435 ${fct_test}
436 if [ $? -ne 0 ]; then
437 break;
438 fi
439
440 # Only delete if successful
441 rm -rf $TRACE_PATH
442done
This page took 0.080768 seconds and 4 git commands to generate.