Background: We have an embedded linux system running Busybox (limited resources), we have a process running which normally spits out lots of info on the console (via stdout / stderr) but we'd like to temporarily redirect it to syslog (using syslogd / logger) on command - WITHOUT restarting the process or rebooting.
Using the code found here works nicely, until we try to stop/close the logger fd at which point writes to stdout/stderr fail and everything breaks. Example below:
int main()
{
// Got command to direct all output to syslog:
FILE *fl;
fl = popen("logger","w");
if(fl == NULL)
return 1;
fprintf(fl,"logger test new"); // This goes to syslogd
int nf;
nf = fileno(fl);
dup2(nf,STDOUT_FILENO);
dup2(nf,STDERR_FILENO);
fprintf(stdout,"Written in stdout\n");
fprintf(stderr,"Written in stderr\n");
// ...some time later when we've logged enough things:
pclose(fl);
fprintf(stdout,"This will fail\n");
fprintf(stderr,"Catch fire and die\n");
}
So the question is, (how)can we restore the original state once we're done with the logging?
Having RTFM on dup()
and dup2()
I'm not clear on how exactly we can achieve this, if it's possible to "reopen" perhaps freopen()
stdout/stderr, but as a last resort it would be acceptable to perhaps point nf
to /dev/null/
so things at least don't crash.