zsh-users
 help / color / mirror / code / Atom feed
* Close *all* file descriptors
@ 2022-05-21  6:29 Zach Riggle
  2022-05-21 20:02 ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Zach Riggle @ 2022-05-21  6:29 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 424 bytes --]

Hello all!

This is probably an usual request, but is there any way to close (or
redirect to /dev/null) all open file descriptors known / opened by zsh?

Let's assume I have e.g.

    exec {mynewfd}>&2

And various other redirections / file descriptor creations / dups.

Is there a way to close all of them in a concise way that does not affect
e.g. shell pipelines and redirection (i.e., stdout and stderr)?

*Zach Riggle*

[-- Attachment #2: Type: text/html, Size: 745 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Close *all* file descriptors
  2022-05-21  6:29 Close *all* file descriptors Zach Riggle
@ 2022-05-21 20:02 ` Bart Schaefer
  2022-05-22 10:49   ` Zach Riggle
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2022-05-21 20:02 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On Fri, May 20, 2022 at 11:32 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
> This is probably an usual request, but is there any way to close (or redirect to /dev/null) all open file descriptors known / opened by zsh?

I expected this to work if you have /proc/:

opened=(/proc/self/fd/*(:t)) wanted=(0 1 2 10)
for close in ${^${opened:|wanted}}; exec {close}>&-

But zsh temporarily opens a file descriptor (for xtrace output, I
believe) during the assignment to $opened, and it's not possible to
predict what number that descriptor will have, which means one of
those exec is going to error on bad file descriptor and kill the loop.

So ... the following seems to be pretty good, though on repeated tries
I have sometimes seen descriptors remain open:

integer close
for close in /proc/self/fd/*(:t); [[ -h /proc/self/fd/$close && ! -t
$close ]] && exec {close}>&-

> Is there a way to close all of them in a concise way that does not affect e.g. shell pipelines and redirection (i.e., stdout and stderr)?

No guarantees about that.  If there are descriptors open for temporary
files, etc., that are needed by coproc or whatever, this could break
it.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Close *all* file descriptors
  2022-05-21 20:02 ` Bart Schaefer
@ 2022-05-22 10:49   ` Zach Riggle
  2022-05-22 19:17     ` Dominik Vogt
  0 siblings, 1 reply; 9+ messages in thread
From: Zach Riggle @ 2022-05-22 10:49 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1570 bytes --]

Unfortunately, macOS does not support proofs, so there’s no nice way to do
it the way you recommended.

I can just force-close the first 1024 file descriptors that I don’t care
about, and hope that it’s sufficient.

On Sat, May 21, 2022 at 3:02 PM Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Fri, May 20, 2022 at 11:32 PM Zach Riggle <zachriggle@gmail.com> wrote:
> >
> > This is probably an usual request, but is there any way to close (or
> redirect to /dev/null) all open file descriptors known / opened by zsh?
>
> I expected this to work if you have /proc/:
>
> opened=(/proc/self/fd/*(:t)) wanted=(0 1 2 10)
> for close in ${^${opened:|wanted}}; exec {close}>&-
>
> But zsh temporarily opens a file descriptor (for xtrace output, I
> believe) during the assignment to $opened, and it's not possible to
> predict what number that descriptor will have, which means one of
> those exec is going to error on bad file descriptor and kill the loop.
>
> So ... the following seems to be pretty good, though on repeated tries
> I have sometimes seen descriptors remain open:
>
> integer close
> for close in /proc/self/fd/*(:t); [[ -h /proc/self/fd/$close && ! -t
> $close ]] && exec {close}>&-
>
> > Is there a way to close all of them in a concise way that does not
> affect e.g. shell pipelines and redirection (i.e., stdout and stderr)?
>
> No guarantees about that.  If there are descriptors open for temporary
> files, etc., that are needed by coproc or whatever, this could break
> it.
>
-- 

*Zach Riggle*

[-- Attachment #2: Type: text/html, Size: 2263 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Close *all* file descriptors
  2022-05-22 10:49   ` Zach Riggle
@ 2022-05-22 19:17     ` Dominik Vogt
  2022-05-22 19:49       ` Dominik Vogt
  2022-05-23  0:28       ` Bart Schaefer
  0 siblings, 2 replies; 9+ messages in thread
From: Dominik Vogt @ 2022-05-22 19:17 UTC (permalink / raw)
  To: zsh-users

On Sun, May 22, 2022 at 05:49:50AM -0500, Zach Riggle wrote:
> Unfortunately, macOS does not support proofs, so there???s no nice way to do
> it the way you recommended.
>
> I can just force-close the first 1024 file descriptors that I don???t care
> about, and hope that it???s sufficient.

Does this help?

  $ for FD in $(lsof -p $$ |
                tr -s " " |
                cut -f 4 -d " " |
                grep "^[0-9]*[urw]$" |
                sed -e "s/[urw]$//"); do
        # close/redirect FD or whatever
        :
    done

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Close *all* file descriptors
  2022-05-22 19:17     ` Dominik Vogt
@ 2022-05-22 19:49       ` Dominik Vogt
  2022-05-22 23:23         ` Bart Schaefer
  2022-05-23  0:28       ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Dominik Vogt @ 2022-05-22 19:49 UTC (permalink / raw)
  To: zsh-users

On Sun, May 22, 2022 at 08:17:58PM +0100, Dominik Vogt wrote:
> On Sun, May 22, 2022 at 05:49:50AM -0500, Zach Riggle wrote:
> > Unfortunately, macOS does not support proofs, so there???s no nice way to do
> > it the way you recommended.
> >
> > I can just force-close the first 1024 file descriptors that I don???t care
> > about, and hope that it???s sufficient.
>
> Does this help?
>
>   $ for FD in $(lsof -p $$ |
>                 tr -s " " |
>                 cut -f 4 -d " " |
>                 grep "^[0-9]*[urw]$" |
>                 sed -e "s/[urw]$//"); do
>         # close/redirect FD or whatever
>         :
>     done

Or simpler:

  $ ...  $(lsof -p $$ -F F | grep "^f[0-9]*$" | tr -d f) ...

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Close *all* file descriptors
  2022-05-22 19:49       ` Dominik Vogt
@ 2022-05-22 23:23         ` Bart Schaefer
  2022-05-23  6:42           ` Stephane Chazelas
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2022-05-22 23:23 UTC (permalink / raw)
  To: Zsh Users

On Sun, May 22, 2022 at 12:50 PM Dominik Vogt <dominik.vogt@gmx.de> wrote:
>
> Or simpler:
>
>   $ ...  $(lsof -p $$ -F F | grep "^f[0-9]*$" | tr -d f) ...

Or even:  ${${(M)$(lsof -p $$ -F F):#f<->*}#f}

Curiously, despite the man page for lsof asserting that PID filters
are applied first, if you add a -d filter to include only certain
types of descriptors, you get the descriptors for every process.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Close *all* file descriptors
  2022-05-22 19:17     ` Dominik Vogt
  2022-05-22 19:49       ` Dominik Vogt
@ 2022-05-23  0:28       ` Bart Schaefer
  1 sibling, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2022-05-23  0:28 UTC (permalink / raw)
  To: dominik.vogt, Zsh Users

On Sun, May 22, 2022 at 12:18 PM Dominik Vogt <dominik.vogt@gmx.de> wrote:
>
> Does this help?

One problem with using $(lsof ...) is that the descriptor opened for
the process substitution will be listed in the output, so you have to
be careful not to attempt to close it or you'll get an EBADF error.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Close *all* file descriptors
  2022-05-22 23:23         ` Bart Schaefer
@ 2022-05-23  6:42           ` Stephane Chazelas
  2022-05-23  8:56             ` Dominik Vogt
  0 siblings, 1 reply; 9+ messages in thread
From: Stephane Chazelas @ 2022-05-23  6:42 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

2022-05-22 16:23:05 -0700, Bart Schaefer:
[...]
> Or even:  ${${(M)$(lsof -p $$ -F F):#f<->*}#f}
> 
> Curiously, despite the man page for lsof asserting that PID filters
> are applied first, if you add a -d filter to include only certain
> types of descriptors, you get the descriptors for every process.
[...]

You also need -a for "and" (default being "or"):

lsof -w -Ff -p $$ -a -d 3-999999 | sed -n 's/^f//p'

(-F F would be "F  file structure address (0x<hexadecimal>)"
according to the manual, I need -F f for fds here).

Here, doing:

sh -c 'exec lsof -w -p "$$" -Ff -ad3-999999' | sed -n 's/^f//p'

Would filter out the fds that have the close-on-exec flag (as
I'd expect would be the case for the internal fds of zsh which
we don't want to close).

(would also filter out the ones created by the user with 
sysopen -o cloexec and might list internal sh fds).

-- 
Stephane


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Close *all* file descriptors
  2022-05-23  6:42           ` Stephane Chazelas
@ 2022-05-23  8:56             ` Dominik Vogt
  0 siblings, 0 replies; 9+ messages in thread
From: Dominik Vogt @ 2022-05-23  8:56 UTC (permalink / raw)
  To: zsh-users; +Cc: Bart Schaefer

On Mon, May 23, 2022 at 07:42:22AM +0100, Stephane Chazelas wrote:
> 2022-05-22 16:23:05 -0700, Bart Schaefer:
> [...]
> > Or even:  ${${(M)$(lsof -p $$ -F F):#f<->*}#f}
> >
> > Curiously, despite the man page for lsof asserting that PID filters
> > are applied first, if you add a -d filter to include only certain
> > types of descriptors, you get the descriptors for every process.
> [...]
>
> You also need -a for "and" (default being "or"):
>
> lsof -w -Ff -p $$ -a -d 3-999999 | sed -n 's/^f//p'
>
> (-F F would be "F  file structure address (0x<hexadecimal>)"
> according to the manual, I need -F f for fds here).

Right, the manpage says that.  On my system -F f and -F F seem to
do exactly the same.  (The "F" was a typo which I didn't notice
because of that behaviour.)

Ciao

Dominik ^_^  ^_^

--

Dominik Vogt


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-05-23  8:57 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-21  6:29 Close *all* file descriptors Zach Riggle
2022-05-21 20:02 ` Bart Schaefer
2022-05-22 10:49   ` Zach Riggle
2022-05-22 19:17     ` Dominik Vogt
2022-05-22 19:49       ` Dominik Vogt
2022-05-22 23:23         ` Bart Schaefer
2022-05-23  6:42           ` Stephane Chazelas
2022-05-23  8:56             ` Dominik Vogt
2022-05-23  0:28       ` Bart Schaefer

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).