[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bad leaks file fd to child processes
From: |
Dale R. Worley |
Subject: |
Re: Bad leaks file fd to child processes |
Date: |
Mon, 28 Nov 2022 17:00:28 -0500 |
Alexey via Bug reports for the GNU Bourne Again SHell <bug-bash@gnu.org>
> Same behavior was in bash 4.4 (as well as now in bash 5.2):
>
> # echo $BASH_VERSION
> 4.4.0(1)-release
> # exec 66</etc/hosts
> # pvs
> File descriptor 66 (/etc/hosts) leaked on pvs invocation. Parent PID
> 1057606: ./bash
>
> But we use the fact tat bash doesn't close FD for example to preliminary
> open log file for utility that we will `exec' later.
> Unfortunately bash doesn't provide any fcntl() mechanism to control FD
> flags.
> It'll be great to have ability to manage CLOEXEC or any other FD flags
> from the script.
Interesting! I had misunderstood the complaint, which is that the fd
leaks *from* bash *to* the program being executed (pvs in the example).
One aspect is that is the specified behavior of bash.
It's fairly straightforward to prevent a particular fd from leaking to a
single command by explicitly closing it in a redirection on the command:
# echo $BASH_VERSION
5.1.0(1)-release
# exec 66</dev/null
# pvs
File descriptor 66 (/dev/null) leaked on pvs invocation. Parent PID 673839:
bash
PV VG Fmt Attr PSize PFree
/dev/sda3 Hobgoblin01 lvm2 a-- <300.00g <40.00g
/dev/sda5 Hobgoblin01 lvm2 a-- <630.02g <580.02g
/dev/sdb3 Hobgoblin01 lvm2 a-- <1.82t 1.77t
# pvs 66<&-
PV VG Fmt Attr PSize PFree
/dev/sda3 Hobgoblin01 lvm2 a-- <300.00g <40.00g
/dev/sda5 Hobgoblin01 lvm2 a-- <630.02g <580.02g
/dev/sdb3 Hobgoblin01 lvm2 a-- <1.82t 1.77t
#
A relatively natural way to handle this is to have a shell array
variable "fd_close_on_exec" whose values are connected to the fd's
close-on-exec value. Then you could do
$ exec 66</dev/null
$ fd_close_on_exec[66]=1
$
Dale