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