Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

c - How to redirect RUNTIME ERRORS to STDERR?

I have a C program that uses PIPES to communicate with an external application.

I use EXECL for this:

execl("./errorprogram","./errorprogram", NULL);

Now I have also used DUP2 to redirect all STDERR to the writing end of the pipe

dup2(fd[1], STDERR_FILENO);

And if I test that using a simple Python script like:

data = sys.stdin.read() 
sys.stderr.write("Bad error
")

I can happily read that error back in the "parent".

Now for my issue: Let's say I have a C "child program" that has a memory leak in it or an "invalid pointer" error like:

#include<stdio.h>
#include<string.h>

void main (){
    char szInput[1024];
     char *badvar;
    gets(szInput);

     badvar = realloc(badvar, 100);
    puts(szInput);
    fflush(NULL); 
}

The REALLOC statement above is purposely there to cause the program to die, and I will get an error like the following at runtime (obvioulsy the PARENT is not affected, only the CHILD process):

* glibc detected ./error: realloc(): invalid pointer: 0xb77a5250 **

And a whole stace trace etc....

Now how can I get any runtime errors like this to STDERR so I can read the full trace back into the parent?

Do I have to redirect "runtime errors to STDERR"? Surely it is possible to catch these hectic errors raised from the child so that the parent can log them etc?

Any help or advise would be greatly appreciated ;-)

Thanks

Lynton

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By default, Glibc will produce output on the process's controlling terminal, if one exists. It sounds like you want to set the environment variable LIBC_FATAL_STDERR_=1, which will instead send fatal errors to stderr always.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...