Date: Sun, 21 Sep 1997 14:18:57 +0300 (IDT) From: Eli Zaretskii To: djgpp-workers AT delorie DOT com cc: DJ Delorie , Charles Sandmann Subject: Re: File-handle inheritance Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk 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?