tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Possible "new" redirect style for /bin/sh (needs a name)



At Sun, 11 Apr 2021 09:05:57 +0200, tlaronde%polynum.com@localhost wrote:
Subject: Re: Possible "new" redirect style for /bin/sh (needs a name)
>
> On Sat, Apr 10, 2021 at 05:04:11PM -0700, Greg A. Woods wrote:
> >
> > So, still, the shell could keep a "high FD watermark" for the range of
> > the FDs allocated by "{var}>file" and open them the normal way then move
> > them with dup2() to the next lower (i.e. unused) FD below this high FD
> > watermark, and reduce it in turn.  If I'm not mistaken the initial value
> > for the high FD watermark could be set to sysconf(_SC_OPEN_MAX)-1.
>
> I thought of this "mapping" too (using big numbers for "named fds" but
> mapping them to real small ones).

not "mapping" -- "moving".

and not moving them to small ones, but using the automatically allocated
next-higher number from open() and moving it up to the top of the
available range

dup2() (and dup3()) lets the caller specify the new FD -- which would be
N-1 in my example where N begins at sysconf(_SC_OPEN_MAX)-1 and moves
down.

/bin/sh already has a function called movefd() to do this (which
basically does the error handling around dup2() or dup3()), and so
"dynamic" redirection could use it like:

	N = sysconf(_SC_OPEN_MAX) - 1;

    ....

	fd = open();
	fd_var = movefd(fd, N-1);
	fd = -1;
	N--;

--
					Greg A. Woods <gwoods%acm.org@localhost>

Kelowna, BC     +1 250 762-7675           RoboHack <woods%robohack.ca@localhost>
Planix, Inc. <woods%planix.com@localhost>     Avoncote Farms <woods%avoncote.ca@localhost>

Attachment: pgpJhphmMdUQB.pgp
Description: OpenPGP Digital Signature



Home | Main Index | Thread Index | Old Index