delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1997/09/21/07:20:44

Date: Sun, 21 Sep 1997 14:18:57 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: djgpp-workers AT delorie DOT com
cc: DJ Delorie <dj AT delorie DOT com>, Charles Sandmann <sandmann AT clio DOT rice DOT edu>
Subject: Re: File-handle inheritance
Message-ID: <Pine.SUN.3.91.970921141749.9783C-100000@is>
MIME-Version: 1.0

I have thought a bit about this, and decided to discuss this once
again.

It turns out it is not simple at all to ask DOS to make only handles 0
to 9 to be inheritable.  The problem is that there's no documented way
to know what would be the handle that DOS will return when you call
Int 21h/AH=3Dh *before* you make that call, and there's no function
that will let you set the inherit bit *after* the file is already
open.  (You can, of course, close the file and reopen it again, but
that's too slow.  And besides, there's no guarantee that if you close
the file, then open it, you will get the same handle you closed: we
have just seen a while ago that OpenDOS doesn't work that way.)

The only way that I can think of to achieve this on libc level is to
keep track about which handles in the first 20 are used and which
aren't and make sure at least 10 of them aren't inherited.  (We need
only worry about the first 20 because only they are passed to child
processes.)  This would require to check in crt1.c which handles are
in use (e.g., with Int 21h/AX=4400), keep a table wich tracks the
state of the first 20 handles, and set the NOINHERIT bit for open
calls issued when 10 or more handles are used up.

An alternative would be to have the stub close the 10 handles in the
range [10..19] *before* it tries to open the .exe file.  This is
actually similar to what COMMAND.COM does when it starts (it closes
all handles above 2).

I think the latter way is better.  It only takes a small loop to do
that in assembly (we don't even need to test whether the call failed,
because some of the handles will not even be open), and if we can stay
withing 2K bytes, the size of the executable doesn't change at all.

Comments?

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019