Re: fd allocation [was: light weight user level semaphores]

Edgar Toernig (froese@gmx.de)
Sat, 21 Apr 2001 06:06:25 +0200


Linus Torvalds wrote:
>
> pid = fork();
> if (!pid) {
> close(0);
> close(1);
> dup(pipe[0]); /* input pipe */
> dup(pipe[1]); /* output pipe */
> execve("child");
> exit(1);
> }
>
> The above is absolutely _standard_ behaviour. It's required to work.
>
> And btw, it's _still_ required to work even if there happens to be a
> "malloc()" in between the close() and the dup() calls.

Right. This is expected (and defined) behaviour. But do you have
_any_ example where this is used for fds > 2? I can't remember.
And IMHO that would be pretty fragile too. Shell scripts sometimes
open temporary fds > 2 and these are passed to called programs. I.e.

#!/bin/sh
exec 3>log
echo >&3 "script started"
ls /proc/self/fd # gets fd3 already opened
ls /proc/self/fd 4</dev/null # now 3 and 4 already in use...
# or look into any configure script...

So, IMHO as long as some library does not mess with fds 0, 1, and 2
it should be ok [1]. Yes, it would be against the standard but I
still have to find some code where this semantic is used for fds > 2.

Ciao, ET.

PS: I would prefer to keep the standard semantics but the reasons
for that are pretty weak ... ;-)

PPS: Even your sample code is fragile. It breaks if I start it
with ./a.out <&- ;-) (the close(0) is likely to close one end
of the pipe)

[1] Unintentionally setting the controlling tty may be a problem.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/