From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 11000 invoked from network); 22 May 2022 10:51:01 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 22 May 2022 10:51:01 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1653216661; b=fK+q9ZgQBqb2TJR+HOGPvB3mg0umoCXCaakwkgxAwKTPIYydRVDMm8IOVFxlyBJ+plYh6jByrQ GsZ+f0mRBeQvx76ovQ0WtKmVMJcPBUEXzEYhGU4AiodMQ32QeeRIckV40N6mgfZ4Kqk+3FiHB3 S742FWSPFZRUAArpGwbFPmwaIiFKB7zoSbyhi+AwQCVclK/Nvq4jdscgDvsnGKteBIFckNY0bn r3qO0gEVowS7BfjxENdxCY2EUf+Zw7rdCHbJ0KHW1erfeSpyjL3nxy9xsaXT4xezwHe4JGJsEL v9VatZgFuPfX/E74AWJapWP6Js6nWyZ0E7Djm6T6ADrrQQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-wr1-f54.google.com) smtp.remote-ip=209.85.221.54; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1653216661; bh=Le7Ebdm43IyrouoHR2778V8uD3GHOtohS4AMAoMbVpI=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=LbPt7C1ghSSnkSjFR9NMnlzNHcMaszgygC2J+FsGQt/Xb1B+gZ2E6CzWh3+y0Mg3hTuFGP7tdJ PGocXCGXJeBfpoAbmYELrNih6+WOOMIgmNXU2ebDYhbVqH3kzQcOJvxs8TXtnshmD8SwRxsiAF 2IXIkeTHQWqFgFGxKvDnmqXZ6dDhrPgYrjofkvcDVsCQrfkzqPU7eY0kVqJGsgUlcmrjbtWHd/ T3TffxTT/27oM0VzucTcLtq9cKd00pXewkdrWEBhd4CC/7VA+rIrEgFVpcKxljqmQp7pp5e0CV bTzcbA/376veCnoT0zVsnz1QWOwmPfACj4BvkX7U8nf5nA==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=Kwe5W6USsbNylZNhfxbQnf5BLFrNCynLqtD2FgDicUA=; b=m4Amc4lZ5w1dbFSKzTfjmm/Grz V9FLCET7luADqlZugxoLl8eI2Y7v0mHjes2AcaePgSVD3DS6ALPwty4xZy8ZzmChTgmjUwaKm+ong t/fwQv/cEem9aZ9vzAktwQBk+I+J06/WhTB8SsplKHhMkhGtHT3OlAw3AfjKBpMANu8xdtYysc4mV lxaPG58hAl3GIXPOxj/xvDFcsh8QMvj4lmPu0APy/g7kS6kHvfHAz08OVxtxmfAtg68kanjNj+/5Q FyfW360goL1V6TKbNeHIHSdn2kfd6IDh4FnbGizVS+Q2o5oyrDOTbojzesH/+WvaRBDcDVERMJt4m FUEO+RMQ==; Received: from authenticated user by zero.zsh.org with local id 1nsjAp-00042v-H5; Sun, 22 May 2022 10:50:59 +0000 Authentication-Results: zsh.org; iprev=pass (mail-wr1-f54.google.com) smtp.remote-ip=209.85.221.54; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-wr1-f54.google.com ([209.85.221.54]:40846) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1nsj9u-0003MD-Oy; Sun, 22 May 2022 10:50:04 +0000 Received: by mail-wr1-f54.google.com with SMTP id s28so16978725wrb.7 for ; Sun, 22 May 2022 03:50:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Kwe5W6USsbNylZNhfxbQnf5BLFrNCynLqtD2FgDicUA=; b=j+2M0HPTG+5fjdBMkXoZeEvep/qHjidOuL3tKeKeapEz4uayXn5QPPvUg3viwuQi9p lanAhG59AyHhkO9pVVwKWsnUIPofPb1xxAhLLbl/OXvrmhNarTyhIanAjM4jzVuf3dP0 3G6ZOMjNVRde/HdP01ycctp7eGBNjcjqLTP3OM3zYfEa5zfvs6KQXTnMp4sX3KWRN0Fl HFUnpR+l4lS1WpqTmHQhC5yUWKGnGdFiaqDb4BqMfPJVvHkP6UTO8C/Htst6IcTsmQr4 q5PdiR1MttR8FgajO1ArTYwCvMEtqBoeSA1cY7IBA7jUWgdyayr5U0Hx+6ZaX7h28xIq v22w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Kwe5W6USsbNylZNhfxbQnf5BLFrNCynLqtD2FgDicUA=; b=YjCwlb5ajf4Cfz05jacEjceYpuw+6pTWmLFVzNWwZUP98KOP3I8AnHQLjCA8zQDSig b8dXoSOeOgm7psCzPGjsKzwOxdq+yyacL+fTlXtQmxW8ZC5MLo9a4O2k8/4p6kTs/mNw GvUBe2t+A9muDYKbPHuM0m+hsTd5eej1+A+8wWUthqS/CpzBggGUuXLUf8m4OmNVbNRh ZS98dEmXkplqYQbV3zmO4J1G57loVRpLpGu8u1rplZfDaoOI1tSI1uV4feoRDTms++s5 IbBk1XVv7ltKOZVzkQwyExyFYitYGwciPTZNQx/pvkiBr1dstTKa8lgfu3zeTkzwL91l 2hsw== X-Gm-Message-State: AOAM531YQ3G+C6r88IYXqecvbzxrd+OHCpy6Ueu8o9mljKT3pTqXZDQS v+ycBAP0H/2sEwSpA7y5ED/XQe/Y49WuO48dXO3UwS6coHs= X-Google-Smtp-Source: ABdhPJyU4+WWGtwU8rhNQdLFA87AybZXOHjSWzQAj2sI0UKfyPcN/mHbCaDKWlqQfDz0QVze5iDho6GxChBgUCl5V9c= X-Received: by 2002:a5d:47ce:0:b0:20f:d6b5:4648 with SMTP id o14-20020a5d47ce000000b0020fd6b54648mr1664308wrc.73.1653216602029; Sun, 22 May 2022 03:50:02 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Zach Riggle Date: Sun, 22 May 2022 05:49:50 -0500 Message-ID: Subject: Re: Close *all* file descriptors To: Bart Schaefer Cc: Zsh Users Content-Type: multipart/alternative; boundary="0000000000004d03f705df977c90" X-Seq: 27782 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: --0000000000004d03f705df977c90 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Unfortunately, macOS does not support proofs, so there=E2=80=99s no nice wa= y to do it the way you recommended. I can just force-close the first 1024 file descriptors that I don=E2=80=99t= care about, and hope that it=E2=80=99s sufficient. On Sat, May 21, 2022 at 3:02 PM Bart Schaefer wrote: > On Fri, May 20, 2022 at 11:32 PM Zach Riggle 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=3D(/proc/self/fd/*(:t)) wanted=3D(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. > --=20 *Zach Riggle* --0000000000004d03f705df977c90 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Unfortunately, macOS does not support proofs, so there=E2= =80=99s no nice way to do it the way you recommended.

I can just force-close the first 1024 file de= scriptors that I don=E2=80=99t care about, and hope that it=E2=80=99s suffi= cient.

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 r= edirect to /dev/null) all open file descriptors known / opened by zsh?

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

opened=3D(/proc/self/fd/*(:t)) wanted=3D(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 aff= ect e.g. shell pipelines and redirection (i.e., stdout and stderr)?

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

Zach Riggle

--0000000000004d03f705df977c90--