3 # Copyright (C) 2018 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
5 # SPDX-License-Identifier: LGPL-2.1-only
7 TEST_DESC
="Change working directory of process"
9 CURDIR
=$
(dirname "$0")/
10 TESTDIR
=$CURDIR/..
/..
/..
/
12 DIR
=$
(readlink
-f "$TESTDIR")
16 source $TESTDIR/utils
/utils.sh
18 #MUST set TESTDIR before calling those functions
21 print_test_banner
"$TEST_DESC"
22 function test_relayd
()
24 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
29 working_dir
=$
(realpath
"$(mktemp -d)")
31 diag
"Test lttng-relayd normal mode change working directory"
33 # There is no rendez-vous mechanism that can guarantee the good timing
34 # to check if the workdir directory was changed.
35 # In the case of lttng-sessiond this would be achieved using the
36 # --sig-parent option but lttng-relayd does not have this feature yet.
37 # Fall back on using polling of the value and unblock when the value is
38 # the one we expect. In case of a failure the test will hang.
39 $relayd_bin_path --working-directory "$working_dir" > /dev
/null
2>&1 &
43 cwd
=$
(readlink
"/proc/${pid}/cwd")
44 if test "$working_dir" = "$cwd"; then
45 # Working dir for process is valid
51 # If we are here the test passed
52 pass
"Working directory changed"
58 function test_relayd_daemon
()
64 working_dir
=$
(realpath
"$(mktemp -d)")
66 diag
"Test lttng-relayd daemon mode change working directory"
68 start_lttng_relayd_opt
1 "-d" "--working-directory $working_dir"
70 pid
=$
(lttng_pgrep
"$RELAYD_MATCH")
71 ok $?
"Found lttng-relayd"
73 cwd
=$
(readlink
"/proc/${pid}/cwd")
75 is
"$cwd" "$working_dir" "Working directory changed"
81 function test_relayd_daemon_no_working_dir
()
83 local expected_working_dir
="/"
87 diag
"Test lttng-relayd daemon mode change working directory"
89 start_lttng_relayd_opt
1 "-d" ""
91 pid
=$
(lttng_pgrep
"$RELAYD_MATCH")
92 ok $?
"Found lttng-relayd"
94 cwd
=$
(readlink
"/proc/${pid}/cwd")
96 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
102 function test_relayd_background
()
108 working_dir
=$
(realpath
"$(mktemp -d)")
110 diag
"Test lttng-relayd background mode change working directory"
112 start_lttng_relayd_opt
1 "-b" "--working-directory $working_dir"
114 pid
=$
(lttng_pgrep
"$RELAYD_MATCH")
115 ok $?
"Found lttng-relayd"
117 cwd
=$
(readlink
"/proc/${pid}/cwd")
119 is
"$cwd" "$working_dir" "Working directory changed"
122 rm -rf "$working_dir"
125 function test_relayd_background_no_working_dir
()
127 local expected_working_dir
="/"
131 diag
"Test lttng-relayd background working directory"
133 start_lttng_relayd_opt
1 "-b" ""
135 pid
=$
(lttng_pgrep
"$RELAYD_MATCH")
136 ok $?
"Found lttng-relayd"
138 cwd
=$
(readlink
"/proc/${pid}/cwd")
140 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
143 rm -rf "$working_dir"
146 function test_relayd_debug_permission
()
150 diag
"Test lttng-relayd change working directory on non writable directory"
152 if [ "$(id -u)" == "0" ]; then
158 skip
$is_user "Skipping permission debug output test; operation can't fail as root" 6 ||
160 local output_pattern
='Working directory \".*\" is not writable'
165 working_dir
=$
(realpath
"$(mktemp -d)")
167 # Removing write access to working dir
168 okx
chmod -w "$working_dir"
170 # Redirect the error output to a temporary file
172 ERROR_OUTPUT_DEST
=$
(mktemp
)
173 start_lttng_relayd_opt
1 "-b" "-v --working-dir $working_dir"
175 pid
=$
(lttng_pgrep
"$RELAYD_MATCH")
176 ok $?
"Found lttng-relayd"
178 cwd
=$
(readlink
"/proc/${pid}/cwd")
179 is
"$cwd" "$working_dir" "Working directory changed"
181 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
182 ok $?
"Warning about missing write permission is present"
185 rm "$ERROR_OUTPUT_DEST"
186 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
187 ERROR_OUTPUT_DEST
=/dev
/null
191 function test_relayd_failure
()
193 local output_pattern
='Failed to change working directory to'
194 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
197 local working_dir_imaginary
201 working_dir
=$
(realpath
"$(mktemp -d)")
202 working_dir_imaginary
="${working_dir}/imaginary_directory"
203 output_dest
=$
(mktemp
)
205 diag
"Test lttng-relayd normal mode change non-existing directory"
207 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
209 ok $?
"Expect failure to start lttng-relayd for non-existent working directory"
211 pid
=$
(lttng_pgrep
"$RELAYD_MATCH")
212 if [ -z "$pid" ]; then
213 pass
"No lttng-relayd present"
215 fail
"No lttng-relayd present"
216 stop_lttng_relayd_notap
219 grep -q "$output_pattern" "$output_dest"
220 ok $?
"Found error message: invalid directory"
223 rm -rf "$working_dir"
226 function test_relayd_env
()
232 working_dir
=$
(mktemp
-d)
233 working_dir
=$
(realpath
"$(mktemp -d)")
235 diag
"Test lttng-relayd change working directory from env. variable"
237 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir}
238 start_lttng_relayd_opt
1 "-b" ""
240 pid
=$
(lttng_pgrep
"$RELAYD_MATCH")
241 ok $?
"Found lttng-relayd"
243 cwd
=$
(readlink
"/proc/$pid/cwd")
245 is
"$cwd" "$working_dir" "Working directory changed"
248 rm -rf "$working_dir"
249 unset LTTNG_RELAYD_WORKING_DIRECTORY
252 function test_relayd_cmdline_overwrite_env
()
254 local working_dir_env
255 local working_dir_cmdline
259 working_dir_env
=$
(realpath
"$(mktemp -d)")
260 working_dir_cmdline
=$
(realpath
"$(mktemp -d)")
262 diag
"Test lttng-relayd change working directory command line overwrite env variable"
264 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir_env}
265 start_lttng_relayd_opt
1 "-b" "--working-dir ${working_dir_cmdline}"
267 pid
=$
(lttng_pgrep
"$RELAYD_MATCH")
268 ok $?
"Found lttng-relayd"
270 cwd
=$
(readlink
"/proc/$pid/cwd")
272 is
"$cwd" "$working_dir_cmdline" "Working directory is the one from command line"
275 rm -rf "$working_dir_env" "$working_dir_cmdline"
276 unset LTTNG_RELAYD_WORKING_DIRECTORY
282 test_relayd_daemon_no_working_dir
283 test_relayd_background
284 test_relayd_background_no_working_dir
285 test_relayd_debug_permission
288 test_relayd_cmdline_overwrite_env
291 for fct_test
in "${TESTS[@]}";
293 if ! ${fct_test}; then