| 1 | #!/bin/bash |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: 2018 Jonathan Rajotte <[email protected]> |
| 4 | # |
| 5 | # SPDX-License-Identifier: LGPL-2.1-only |
| 6 | |
| 7 | TEST_DESC="Change working directory of process" |
| 8 | |
| 9 | CURDIR=$(dirname "$0")/ |
| 10 | TESTDIR=$CURDIR/../../../ |
| 11 | |
| 12 | DIR=$(readlink -f "$TESTDIR") |
| 13 | |
| 14 | NUM_TESTS=36 |
| 15 | |
| 16 | source $TESTDIR/utils/utils.sh |
| 17 | |
| 18 | #MUST set TESTDIR before calling those functions |
| 19 | plan_tests $NUM_TESTS |
| 20 | |
| 21 | print_test_banner "$TEST_DESC" |
| 22 | function test_relayd() |
| 23 | { |
| 24 | local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN" |
| 25 | local working_dir=$(realpath "$(mktemp -d -t "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")") |
| 26 | local pid |
| 27 | local cwd |
| 28 | |
| 29 | diag "Test lttng-relayd normal mode change working directory" |
| 30 | start_lttng_relayd --working-directory "$working_dir" |
| 31 | # When the relayd is successfully started, the PID will be appended |
| 32 | # to `LTTNG_RELAYD_PIDS`. Use the last (most recent) PID for checking |
| 33 | # the working directory. If relayd fails to start, there will be either |
| 34 | # no PID, or a different lttng-relayd PID without the same working |
| 35 | # directory and the test will fail. |
| 36 | pid="${LTTNG_RELAYD_PIDS[-1]}" |
| 37 | |
| 38 | cwd=$(readlink "/proc/${pid}/cwd") |
| 39 | test "$working_dir" = "$cwd" |
| 40 | ok "${?}" "Working directory changed" |
| 41 | |
| 42 | stop_lttng_relayd |
| 43 | rm -rf "$working_dir" |
| 44 | } |
| 45 | |
| 46 | function test_relayd_daemon() |
| 47 | { |
| 48 | local cwd |
| 49 | local pid |
| 50 | local working_dir=$(realpath "$(mktemp -d -t "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")") |
| 51 | |
| 52 | diag "Test lttng-relayd daemon mode change working directory" |
| 53 | |
| 54 | start_lttng_relayd_opt 1 "-d" --working-directory "$working_dir" |
| 55 | # When the relayd is successfully started, the PID will be appended |
| 56 | # to `LTTNG_RELAYD_PIDS`. Use the last (most recent) PID for checking |
| 57 | # the working directory. If relayd fails to start, there will be either |
| 58 | # no PID, or a different lttng-relayd PID without the same working |
| 59 | # directory and the test will fail. |
| 60 | pid="${LTTNG_RELAYD_PIDS[-1]}" |
| 61 | ps -p "${pid}" >/dev/null |
| 62 | ok $? "Found lttng-relayd" |
| 63 | |
| 64 | cwd=$(readlink "/proc/${pid}/cwd") |
| 65 | is "${cwd:-no working directory found}" "$working_dir" "Working directory changed" |
| 66 | |
| 67 | stop_lttng_relayd |
| 68 | rm -rf "$working_dir" |
| 69 | } |
| 70 | |
| 71 | function test_relayd_daemon_no_working_dir() |
| 72 | { |
| 73 | local expected_working_dir="/" |
| 74 | local cwd |
| 75 | local pid |
| 76 | |
| 77 | diag "Test lttng-relayd daemon mode change working directory" |
| 78 | |
| 79 | start_lttng_relayd_opt 1 "-d" |
| 80 | # When the relayd is successfully started, the PID will be appended |
| 81 | # to `LTTNG_RELAYD_PIDS`. Use the last (most recent) PID for checking |
| 82 | # the working directory. If relayd fails to start, there will be either |
| 83 | # no PID, or a different lttng-relayd PID without the same working |
| 84 | # directory and the test will fail. |
| 85 | pid="${LTTNG_RELAYD_PIDS[-1]}" |
| 86 | ps -p $pid >/dev/null |
| 87 | ok $? "Found lttng-relayd" |
| 88 | |
| 89 | cwd=$(readlink "/proc/${pid}/cwd") |
| 90 | is "${cwd:-no working directory found}" "$expected_working_dir" "Working directory is $expected_working_dir" |
| 91 | |
| 92 | stop_lttng_relayd |
| 93 | rm -rf "$working_dir" |
| 94 | } |
| 95 | |
| 96 | function test_relayd_background() |
| 97 | { |
| 98 | local cwd |
| 99 | local pid |
| 100 | local working_dir=$(realpath "$(mktemp -d -t "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")") |
| 101 | |
| 102 | diag "Test lttng-relayd background mode change working directory" |
| 103 | |
| 104 | start_lttng_relayd_opt 1 "-b" --working-directory "$working_dir" |
| 105 | # When the relayd is successfully started, the PID will be appended |
| 106 | # to `LTTNG_RELAYD_PIDS`. Use the last (most recent) PID for checking |
| 107 | # the working directory. If relayd fails to start, there will be either |
| 108 | # no PID, or a different lttng-relayd PID without the same working |
| 109 | # directory and the test will fail. |
| 110 | pid="${LTTNG_RELAYD_PIDS[-1]}" |
| 111 | ps -p "${pid}" >/dev/null |
| 112 | ok $? "Found lttng-relayd" |
| 113 | |
| 114 | cwd=$(readlink "/proc/${pid}/cwd") |
| 115 | is "${cwd:-no working directory found}" "$working_dir" "Working directory changed" |
| 116 | |
| 117 | stop_lttng_relayd |
| 118 | rm -rf "$working_dir" |
| 119 | } |
| 120 | |
| 121 | function test_relayd_background_no_working_dir() |
| 122 | { |
| 123 | local expected_working_dir="/" |
| 124 | local cwd |
| 125 | local pid |
| 126 | |
| 127 | diag "Test lttng-relayd background working directory" |
| 128 | |
| 129 | start_lttng_relayd_opt 1 "-b" |
| 130 | # When the relayd is successfully started, the PID will be appended |
| 131 | # to `LTTNG_RELAYD_PIDS`. Use the last (most recent) PID for checking |
| 132 | # the working directory. If relayd fails to start, there will be either |
| 133 | # no PID, or a different lttng-relayd PID without the same working |
| 134 | # directory and the test will fail. |
| 135 | pid="${LTTNG_RELAYD_PIDS[-1]}" |
| 136 | ps -p "${pid}" >/dev/null |
| 137 | ok $? "Found lttng-relayd" |
| 138 | |
| 139 | cwd=$(readlink "/proc/${pid}/cwd") |
| 140 | is "${cwd:-no working directory found}" "$expected_working_dir" "Working directory is $expected_working_dir" |
| 141 | |
| 142 | stop_lttng_relayd |
| 143 | rm -rf "$working_dir" |
| 144 | } |
| 145 | |
| 146 | function test_relayd_debug_permission() |
| 147 | { |
| 148 | local is_user |
| 149 | |
| 150 | diag "Test lttng-relayd change working directory on non writable directory" |
| 151 | |
| 152 | if [ "$(id -u)" == "0" ]; then |
| 153 | is_user=0 |
| 154 | else |
| 155 | is_user=1 |
| 156 | fi |
| 157 | |
| 158 | skip $is_user "Skipping permission debug output test; operation can't fail as root" 6 || |
| 159 | { |
| 160 | local output_pattern='Working directory \".*\" is not writable' |
| 161 | local cwd |
| 162 | local pid |
| 163 | local working_dir=$(realpath "$(mktemp -d -t "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")") |
| 164 | |
| 165 | # Removing write access to working dir |
| 166 | okx chmod -w "$working_dir" |
| 167 | |
| 168 | # Redirect the error output to a temporary file |
| 169 | |
| 170 | RELAYD_ERROR_OUTPUT_DEST=$(mktemp -t "tmp.${FUNCNAME[0]}_error_output.XXXXXX") |
| 171 | start_lttng_relayd_opt 1 "-b" -v --working-dir "$working_dir" |
| 172 | # When the relayd is successfully started, the PID will be appended |
| 173 | # to `LTTNG_RELAYD_PIDS`. Use the last (most recent) PID for checking |
| 174 | # the working directory. If relayd fails to start, there will be either |
| 175 | # no PID, or a different lttng-relayd PID without the same working |
| 176 | # directory and the test will fail. |
| 177 | pid="${LTTNG_RELAYD_PIDS[-1]}" |
| 178 | ps -p "${pid}" > /dev/null |
| 179 | ok $? "Found lttng-relayd" |
| 180 | |
| 181 | cwd=$(readlink "/proc/${pid}/cwd") |
| 182 | is "${cwd:-no working directory found}" "$working_dir" "Working directory changed" |
| 183 | |
| 184 | grep -q "$output_pattern" "${RELAYD_ERROR_OUTPUT_DEST}" |
| 185 | ok $? "Warning about missing write permission is present" |
| 186 | |
| 187 | stop_lttng_relayd |
| 188 | rm "${RELAYD_ERROR_OUTPUT_DEST}" |
| 189 | rm -rf "$working_dir" "${RELAYD_ERROR_OUTPUT_DEST}" |
| 190 | unset RELAYD_ERROR_OUTPUT_DEST |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | function test_relayd_failure() |
| 195 | { |
| 196 | local output_pattern='Failed to change working directory to' |
| 197 | local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN" |
| 198 | local working_dir=$(realpath "$(mktemp -d -t "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")") |
| 199 | local output_dest=$(mktemp -t "tmp.${FUNCNAME[0]}_working_dir.XXXXXX") |
| 200 | |
| 201 | local working_dir_imaginary |
| 202 | local pid |
| 203 | |
| 204 | working_dir_imaginary="${working_dir}/imaginary_directory" |
| 205 | |
| 206 | diag "Test lttng-relayd normal mode change non-existing directory" |
| 207 | |
| 208 | RELAYD_ERROR_OUTPUT_DEST="${output_dest}" start_lttng_relayd_opt 0 -b --working-directory "$working_dir_imaginary" |
| 209 | test $? -eq "1" |
| 210 | ok $? "Expect failure to start lttng-relayd for non-existent working directory" |
| 211 | |
| 212 | # When the relayd is successfully started, the PID will be appended |
| 213 | # to `LTTNG_RELAYD_PIDS`. Use the last (most recent) PID for checking |
| 214 | # the working directory. If relayd fails to start, there will be no PID. |
| 215 | pid="${LTTNG_RELAYD_PIDS[-1]}" |
| 216 | if [ -z "$pid" ]; then |
| 217 | pass "No lttng-relayd present" |
| 218 | else |
| 219 | fail "No lttng-relayd present" |
| 220 | stop_lttng_relayd_notap |
| 221 | fi |
| 222 | |
| 223 | grep -q "$output_pattern" "$output_dest" |
| 224 | ok $? "Found error message: invalid directory" |
| 225 | |
| 226 | rm -rf "$working_dir" "${output_dest}" |
| 227 | } |
| 228 | |
| 229 | function test_relayd_env() |
| 230 | { |
| 231 | local cwd |
| 232 | local pid |
| 233 | local working_dir=$(realpath "$(mktemp -d -t "tmp.${FUNCNAME[0]}_working_dir.XXXXXX")") |
| 234 | |
| 235 | diag "Test lttng-relayd change working directory from env. variable" |
| 236 | |
| 237 | export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir} |
| 238 | start_lttng_relayd_opt 1 "-b" |
| 239 | |
| 240 | # When the relayd is successfully started, the PID will be appended |
| 241 | # to `LTTNG_RELAYD_PIDS`. Use the last (most recent) PID for checking |
| 242 | # the working directory. If relayd fails to start, there will be either |
| 243 | # no PID, or a different lttng-relayd PID without the same working |
| 244 | # directory and the test will fail. |
| 245 | pid="${LTTNG_RELAYD_PIDS[-1]}" |
| 246 | ps -p "${pid}" >/dev/null |
| 247 | ok $? "Found lttng-relayd" |
| 248 | |
| 249 | cwd=$(readlink "/proc/$pid/cwd") |
| 250 | is "${cwd:-no working directory found}" "$working_dir" "Working directory changed" |
| 251 | |
| 252 | stop_lttng_relayd |
| 253 | rm -rf "$working_dir" |
| 254 | unset LTTNG_RELAYD_WORKING_DIRECTORY |
| 255 | } |
| 256 | |
| 257 | function test_relayd_cmdline_overwrite_env() |
| 258 | { |
| 259 | local cwd |
| 260 | local pid |
| 261 | local working_dir_env=$(realpath "$(mktemp -d -t "tmp.${FUNCNAME[0]}_working_dir_even.XXXXXX")") |
| 262 | local working_dir_cmdline=$(realpath "$(mktemp -d -t "tmp.${FUNCNAME[0]}_working_dir_cmdline.XXXXXX")") |
| 263 | |
| 264 | diag "Test lttng-relayd change working directory command line overwrite env variable" |
| 265 | |
| 266 | export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir_env} |
| 267 | start_lttng_relayd_opt 1 "-b" --working-dir "${working_dir_cmdline}" |
| 268 | |
| 269 | # When the relayd is successfully started, the PID will be appended |
| 270 | # to `LTTNG_RELAYD_PIDS`. Use the last (most recent) PID for checking |
| 271 | # the working directory. If relayd fails to start, there will be either |
| 272 | # no PID, or a different lttng-relayd PID without the same working |
| 273 | # directory and the test will fail. |
| 274 | pid="${LTTNG_RELAYD_PIDS[-1]}" |
| 275 | ps -p "${pid}" >/dev/null |
| 276 | ok $? "Found lttng-relayd" |
| 277 | |
| 278 | cwd=$(readlink "/proc/$pid/cwd") |
| 279 | is "${cwd:-no working directory found}" "$working_dir_cmdline" "Working directory is the one from command line" |
| 280 | |
| 281 | stop_lttng_relayd |
| 282 | rm -rf "$working_dir_env" "$working_dir_cmdline" |
| 283 | unset LTTNG_RELAYD_WORKING_DIRECTORY |
| 284 | } |
| 285 | |
| 286 | TESTS=( |
| 287 | test_relayd |
| 288 | test_relayd_daemon |
| 289 | test_relayd_daemon_no_working_dir |
| 290 | test_relayd_background |
| 291 | test_relayd_background_no_working_dir |
| 292 | test_relayd_debug_permission |
| 293 | test_relayd_failure |
| 294 | test_relayd_env |
| 295 | test_relayd_cmdline_overwrite_env |
| 296 | ) |
| 297 | |
| 298 | for fct_test in "${TESTS[@]}"; |
| 299 | do |
| 300 | if ! ${fct_test}; then |
| 301 | break; |
| 302 | fi |
| 303 | done |