blob: a8d93ce4f75e625bfdb94af6f8435339572ba3c3 [file] [log] [blame]
Denys Vlasenkoaa3576a2016-08-22 19:54:12 +02001# Builds a " 3>&- 4>&-" string.
2# Note: one of these fds is a directory opened to /proc/self/fd
3# for globbing. It is unwanted, but I don't know how to filter it out.
4find_fds() {
5 fds=""
6 for f in /proc/self/fd/*; do
7 test "$f" = "/proc/self/fd/0" && continue
8 test "$f" = "/proc/self/fd/1" && continue
9 test "$f" = "/proc/self/fd/2" && continue
10 fds="$fds ${f##*/}>&-"
11 done
12}
13
14find_fds
15fds1="$fds"
16
17# One of the fds is open to the script body
18# Close it while executing something.
19eval "find_fds $fds"
20
21# Shell should not lose that fd. Did it?
22find_fds
Denys Vlasenko035486c2017-07-31 04:09:19 +020023test x"$fds1" = x"$fds" \
24&& { echo "Ok: script fd is not closed"; exit 0; }
25
26# One legit way to handle it is to move script fd. For example, if we see that fd 10 moved to fd 11:
27test x"$fds1" = x" 10>&- 3>&-" && \
28test x"$fds" = x" 11>&- 3>&-" \
29&& { echo "Ok: script fd is not closed"; exit 0; }
Denys Vlasenko41ef41b2018-07-24 16:54:41 +020030# or we see that fd 3 moved to fd 10:
31test x"$fds1" = x" 3>&- 4>&-" && \
32test x"$fds" = x" 10>&- 3>&-" \
33&& { echo "Ok: script fd is not closed"; exit 0; }
Denys Vlasenkoaa3576a2016-08-22 19:54:12 +020034
35echo "Bug: script fd is closed"
36echo "fds1:$fds1"
37echo "fds2:$fds"
38exit 1