Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Feature request: ZSH_XTRACEFD variable
- X-seq: zsh-workers 47358
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
- To: Timothée Mazzucotelli <timothee.mazzucotelli@xxxxxxxxx>
- Subject: Re: Feature request: ZSH_XTRACEFD variable
- Date: Fri, 4 Sep 2020 19:48:56 +0000
- Archived-at: <https://zsh.org/workers/47358>
- Archived-at: <http://www.zsh.org/sympa/arcsearch_id/zsh-workers/2020-09/20200904194856.0e115c95%40tarpaulin.shahaf.local2>
- Authentication-results: zsh.org; iprev=pass (out3-smtp.messagingengine.com) smtp.remote-ip=66.111.4.27; dkim=pass header.d=daniel.shahaf.name header.s=fm1 header.a=rsa-sha256; dkim=pass header.d=messagingengine.com header.s=fm3 header.a=rsa-sha256; dmarc=none header.from=daniel.shahaf.name; arc=none
- Cc: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>, "zsh-workers@xxxxxxx" <zsh-workers@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= daniel.shahaf.name; h=date:from:to:cc:subject:message-id :in-reply-to:references:mime-version:content-type :content-transfer-encoding; s=fm1; bh=Sr1z533qVO7CVK5RQZl/ynFtV5 xqQAuKvFvnb0DATBA=; b=R6mXPLb1N44AvvqjrxXvxLZAAsI6nr0wFhrMAgXvtn o8aiZtfIgd6X+r7quGNJrFHoNSOirNDcaW4bb4lviXFtm8pWvU+hdBKKrGjpoeMZ AzVnerAxVILvYBHUM/fg+h9mbNGucsEDX25NEUmd/ZKRTy37ilAm38jBJMUuvlZo 95QbXx1nq/9z6qNF/S/HA0np9Qsy2PxCcyNI3TLMvek5FdNS+l342G4NL4XY3Z8z qhcyaU+DgAvPfDcOgytDSVL2FO0DSXQ7pl0B2uJJy3sVbAtdwcFUU+V4Znhu20bJ k/A4lZrKLr8hZFzQS7m9Bc93jKQ6A8IbTzvyRiougOSg==
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=Sr1z533qVO7CVK5RQZl/ynFtV5xqQAuKvFvnb0DAT BA=; b=ibV2OJzIiahbgXIdu5TBdjA7knPp8Nl9dCrj+2xLvFF567b+Y+o8xiCeQ g3KC+/1OoFH0mmd5P5JOjuyJODRWWmDeWL66nNqC9hMJbPdsmORfUFCymb+b7Hro quPAKunTic2MCp37HUa6uRNmkSsRZyHh8KQx6V6OtEh6KGRrI5aR3tn5TOlfFdfY xCekNx9nj/HIDDksygR0P742Zl0/OujuT4n5Vatsro3MT521ssSAj9DjrWu/NJMQ uxL4dFFYpCFLB5IjsmZyjfprLW8W6VMtDNyb3P3WPG6BqvLc2E5Q+mBwLrnoJbGw myRTC+MMxBYJDLF3B4IONH1ai8HXQ==
- In-reply-to: <CAD8ZDTrVFa5Yt3ReEmxFUvZ_2U4aVgrHiyQLGxje8SgFhGmg+w@mail.gmail.com>
- List-archive: <http://www.zsh.org/sympa/arc/zsh-workers>
- List-help: <mailto:sympa@zsh.org?subject=help>
- List-id: <zsh-workers.zsh.org>
- List-owner: <mailto:zsh-workers-request@zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-subscribe: <mailto:sympa@zsh.org?subject=subscribe%20zsh-workers>
- List-unsubscribe: <mailto:sympa@zsh.org?subject=unsubscribe%20zsh-workers>
- References: <EL9xNuAdKSLWp41th3XxhRLVtul7jHkT9ons2s2c_35aNjCbCMzo4DqiCNV1SoNjdLhNgdUGLNMHQxQ9q4jDMcNIsxzonWx5jzOB5Hr8rBs=@protonmail.com> <309829031.4459446.1587391766024@mail2.virginmedia.com> <CAD8ZDTp=rYsWiEkq3byjU=BRAA2iLwxt5QG_19MBc+Jo8dC-0w@mail.gmail.com> <CAH+w=7Ymtz4XH708BGvgwo89WNB=HttfyX+fHVHakz+Ybf==ag@mail.gmail.com> <20200503000658.6fddb904@tarpaulin.shahaf.local2> <CAN=4vMrcvyEa17Szf+j=gHPBiasFHOiP0gR-h80TrDBLYbH9zg@mail.gmail.com> <e909f9a6be010fa1027c465829db7511a334fa35.camel@ntlworld.com> <20200503210618.5c639014@tarpaulin.shahaf.local2> <505277422.148264.1588581302888@mail2.virginmedia.com> <20200505000331.59294412@tarpaulin.shahaf.local2> <CAD8ZDTpOf6W3iMwygCio53KumTF1rsjF7wtXeHCb8pDe-wr9qQ@mail.gmail.com> <600054363.204367.1588697277901@mail2.virginmedia.com> <CAH+w=7YpEEy-H8tXt6toaC3gNQ95SAgjGJ=_n5_+XwXCchC3+Q@mail.gmail.com> <CAD8ZDTrVFa5Yt3ReEmxFUvZ_2U4aVgrHiyQLGxje8SgFhGmg+w@mail.gmail.com>
- Sender: zsh-workers-request@xxxxxxx
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@xxxxxxxxxxxxxxxx>
> 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@xxxxxxxxxxxx> 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.
> >
Messages sorted by:
Reverse Date,
Date,
Thread,
Author