csh Rerouting stderr

D. Jason Penney penneyj at servio.UUCP
Tue May 16 03:22:57 AEST 1989


I've found my problem!  The answer is interesting enough that I'm posting
this follow-up, as I threatened I would.

To recap the problem description:  In a fork()'d and execve()'d task,
stderr was not getting printed, and in fact programs were trashing their
address space in an odd way.  This ONLY happened under csh -- Bourne shell
worked properly.

Here is what I did wrong: before doing the execve() I closed stdin so that
the forked process would not share stdin with the parent process:

close(0);

What csh is evidently doing is closing stderr and then re-opening it,
thus leaving descriptor 2 uninitialized.

Thus, if you want to bit-bucket stdin in a forked process, the correct
approach is,

close(0);
open("/dev/null", O_RDONLY);

so that csh will get file descriptor 2 when it reopens stderr.

Many thanks to Maarten Litmaath @ VU Amsterdam, who encouraged me
to keep looking until I found the problem.

-- 
D. Jason Penney                  Ph: (503) 629-8383
Beaverton, OR 97006              uucp: ...ogccse!servio!penneyj
STANDARD DISCLAIMER:  Should I or my opinions be caught or killed, the
company will disavow any knowledge of my actions...



More information about the Comp.unix.wizards mailing list