linux - What happens to open file handles after an execv call? (C++) -
on linux, have c++ code want execv application. program outputs data stderr. therefore redirect stderr calling freopen() stderr stream parameter. thing is, want redirect stderr process run.
here scenario working with. fork() current process; in child process, redirect stderr; execv() run separate application.
firstly, have sentry set redirect stderr output. here code:
class stderrsentry { public:   stderrsentry() {     freopen( "nul", "wt", stderr );   }   ~stderrsentry() {     fclose( stderr );   } };   then later in code:
pid_t pid = fork(); int retval=-1;  if( pid < 0 ) {   success = false; } else if( ! pid ) {  // child process   stderrsentry stderrsentry;   // redirecting stderr here!   pid_t chid = setsid();   if (chid == -1 ) {     exit(-1);   }   else {     //  here execv() call:     if( execv(command[0].c_str(), const_cast<char**>(&c_args[0])) < 0 ) {       exit( -1 );     }   } } // ... else etc...   will stderr redirect still technically valid once execv() call replaces current process specified one?
this behaving desire, fluke, or way it?
i cannot activate stderr redirect in application run in execv, since not code.
thanks info on this.
the redirection valid. execv call changes process image, file descriptors remain untouched.
Comments
Post a Comment