/proc/$$/fd mystery - {t,}csh vs. bash

Dave Johnson dave-gnhlug-list at davej.org
Thu Jan 8 23:53:21 EST 2009


Michael ODonnell writes:
> 
> 
> > Because on startup /bin/csh dups stdin/out/err to higher fds...
> 	[...]
> > this means /bin/csh uses FDs 16,17,18 as it's stdin/out/err instead
> > of 0/1/2.
> 	[...]
> > when running a program: after clone(), it just dup()s back to
> > stdin/out/err before exec():
> 
> Cool - thanks for the analysis.  Why might they be doing this?

from csh.h:

> /*
>  * The shell moves std in/out/diag and the old std input away from units
>  * 0, 1, and 2 so that it is easy to set up these standards for invoked
>  * commands.
>  */
> #define FSHTTY  15              /* /dev/tty when manip pgrps */
> #define FSHIN   16              /* Preferred desc for shell input */
> #define FSHOUT  17              /* ... shell output */
> #define FSHERR  18              /* ... shell diagnostics */
> #define FOLDSTD 19              /* ... old std input */


It does make it less complicated if you invoke programs with weird
redirection arguments.

If fd 0/1/2 are all unused just before you exec() then its simple to
dup/open these based on whatever redirection you want the program to
run under.  If they are used then you may have to move stuff out of
the way before setting up redirection or accidentially misplace the
shell's actual stdin/out/err while setting up redirection.

-- 
Dave


More information about the gnhlug-discuss mailing list