From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: "Timothée Mazzucotelli" <timothee.mazzucotelli@gmail.com>
Cc: Bart Schaefer <schaefer@brasslantern.com>,
Peter Stephenson <p.w.stephenson@ntlworld.com>,
"zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Re: Feature request: ZSH_XTRACEFD variable
Date: Fri, 4 Sep 2020 19:48:56 +0000 [thread overview]
Message-ID: <20200904194856.0e115c95@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <CAD8ZDTrVFa5Yt3ReEmxFUvZ_2U4aVgrHiyQLGxje8SgFhGmg+w@mail.gmail.com>
Timothée Mazzucotelli wrote on Thu, 03 Sep 2020 15:51 +0200:
> Hello, I'm coming back to the ZSH_XTRACEFD feature again :)
>
> About closing file descriptors or not: I don't understand what it means to
> "leak a FILE".
"FILE", in this context, is the opaque type defined by the C programming
language. (See the return type of fopen(3), for example.)
In implementations, a FILE typically wraps a "file descriptor" (fd),
which is an int. The kernel caps the number of file descriptors the
process may have open at any one time (see ulimit/unlimit). For this
reason it's important to always close fd's when done using them.
To "leak" a fd means to lose track of its integer value and the fact it
needs to be closed. For example, the following function leaks a fd:
.
static void
f(void)
{
open("/dev/null", O_WRONLY);
return;
}
Every call to f() leaks one fd. If f() were called in a loop,
eventually it would not be possible to open more files:
.
[[[
% repeat $((1 + $(ulimit -n))) { eval "exec {fd$((++i))}>/dev/null" }
zsh: cannot moved fd 3: too many open files
zsh: exit 1
⋮
]]]
(That's rather dense, I'm afraid, so feel free to ask further. The
number of iterations is large enough to ensure the error happens in the
last iteration or earlier. The variable $i is autovivified and used to
generate a different variable name in every iteration of the loop
($fd1, $fd2, …, $fd1025) since I didn't know whether the square
brackets array element variable name syntax could be used in that
context.)
If f() called fopen() rather than open(), it would leak not only a fd
but also whatever other resources the FILE encapsulated (e.g., a small
block of malloc()'d memory).
HTH. I'll leave the ZSH_XTRACEFD-specific aspects for others.
Cheers,
Daniel
P.S. The quoted error message is ungrammatical.
> I'm not sure to understand the past comments either: we only use fdopen
> when the value of ZSH_XTRACEFD is > 2.
> For 0, 1 and 2, we re-use the existing file descriptors stdin, stdout and
> stderr.
>
> Anyway, I added the patch in an attachment. Also, here's the link to the
> commit on my fork:
> https://github.com/pawamoy/zsh/commit/b9b37333fcf02a463f6f742976b37b45ab08742d
>
> In this patch, I never close any file descriptor.
>
> There's one last thing that looks weird to me:
> single instructions like ZSH_XTRACEFD=5 are not properly logged in the
> xtrace output.
> It seems they are truncated up to the end of the variable assignment:
> - with a=0 ZSH_XTRACEFD=5, nothing appear in the output either
> - with ZXH_XTRACEFD=5 a=0, only a=0 appears in the output (but no
> +(eval):18 prefix or similar)
>
> Any idea about this?
>
> Cheers,
> Timothée
>
> On Wed, May 6, 2020 at 12:20 AM Bart Schaefer <schaefer@brasslantern.com>
> wrote:
>
> > (Peter, for some reason Gmail is classifying all email from
> > ntlworld.com as spam, with the notation that it "can't guarantee that
> > this message came from ntlworld.com")
> >
> > On Tue, May 5, 2020 at 9:48 AM Peter Stephenson
> > <p.w.stephenson@ntlworld.com> wrote:
> > >
> > >
> > > The problem is if we fopen() the file descriptor to use stdio as output,
> > we can either
> > > leak the entire FILE, not opened by the user, or we can close the entire
> > FILE.
> >
> > In that case we should be doing the fopen() on a dup() of the
> > descriptor, and fclose()ing the FILE.
> >
> > If it is important that fileno(xtrerr) == $ZSH_XTRACEFD, then we should
> > 1) dup() the descriptor to save a copy
> > 2) fopen() the original
> > 3) after fclose(), dup2() the copy back to the original
> > 4) close() the copy
> >
> > However, I'm not sure it's necessary to be that convoluted.
> >
next prev parent reply other threads:[~2020-09-04 19:49 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-17 15:08 Timothée Mazzucotelli
2019-05-18 7:55 ` Stephane Chazelas
2019-05-20 10:34 ` Stephane Chazelas
2019-07-21 15:08 ` Timothée Mazzucotelli
2019-07-21 15:22 ` Peter Stephenson
2019-07-31 19:40 ` Timothée Mazzucotelli
2019-07-31 19:45 ` Timothée Mazzucotelli
2019-07-31 21:12 ` Sebastian Gniazdowski
2019-08-13 9:14 ` Peter Stephenson
2019-08-13 15:38 ` Peter Stephenson
2019-09-10 22:19 ` Timothée Mazzucotelli
2019-09-11 8:45 ` Peter Stephenson
2020-04-19 10:30 ` Timothée Mazzucotelli
2020-04-20 14:09 ` Peter Stephenson
2020-05-02 18:02 ` Timothée Mazzucotelli
2020-05-02 21:15 ` Bart Schaefer
2020-05-03 0:06 ` Daniel Shahaf
2020-05-03 4:43 ` Roman Perepelitsa
2020-05-03 16:21 ` Daniel Shahaf
2020-05-03 19:54 ` Peter Stephenson
2020-05-03 21:06 ` Daniel Shahaf
2020-05-04 8:35 ` Peter Stephenson
2020-05-05 0:03 ` Daniel Shahaf
2020-05-05 16:36 ` Timothée Mazzucotelli
2020-05-05 16:47 ` Peter Stephenson
2020-05-05 22:19 ` Bart Schaefer
2020-09-03 13:51 ` Timothée Mazzucotelli
2020-09-04 19:48 ` Daniel Shahaf [this message]
2020-09-04 19:53 ` Bart Schaefer
2020-09-05 9:24 ` Timothée Mazzucotelli
2020-05-05 22:11 ` Bart Schaefer
2020-05-03 6:01 ` Stephane Chazelas
2020-05-03 7:07 ` Stephane Chazelas
2020-05-03 17:30 ` Peter Stephenson
2020-05-03 21:23 ` Daniel Shahaf
2020-05-04 8:26 ` Peter Stephenson
2020-05-05 20:50 ` Daniel Shahaf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200904194856.0e115c95@tarpaulin.shahaf.local2 \
--to=d.s@daniel.shahaf.name \
--cc=p.w.stephenson@ntlworld.com \
--cc=schaefer@brasslantern.com \
--cc=timothee.mazzucotelli@gmail.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public 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).