Is there a way to temporary disable fd 2 in UNIX?
I've written an application which uses fork and execv to launch another process. I made a pipe for notification about status, and from main entry of the launched process, I write some bytes to notify that it's launched correctly. Otherwise, read returns 0 since the writing pipe has been closed in forked process.
The only trouble is when the process does not launch (absent library), I get a message in stderr about this. The message is written to shell what is wrong. I'd prefer to do i开发者_如何转开发t quietly. However if I do close(2) before execv I don't have the message what is what I want. Still I'd like to keep the fd 2 open for stderr.
Presumably the application does not like not having a stderr stream at all. Rather than
close(2);
I'd try
int tmpfd = open("/dev/null", O_WRONLY);
dup2(tmpfd, 2);
close(tmpfd);
Of course you are cordially invited to add error handling.
Thank you both. I resolved the problem with redirecting stderr to /dev/null. However I have to launch the child process two times. First I check if it launches at all with suppressed stderr and then I launch it again if the first time was successful. So I have correct default stderr for child process and no shell output in case of failure.
精彩评论