Please look at this code. It runs in CentOS6 64bit.
#include<stdio.h>
int main(int argc, char **argv)
{
fprintf(stderr, "output 1\n");
printf("output 2\n");
fflush(stdout);
system("echo abc");
fprintf(stderr, "output 3\n ");
printf("output 4\n");
fflush(stdout);
daemon(0, 1);
fprintf(stderr, "output 5\n");
printf("output 6\n");
fflush(stdout);
system("echo abc");
fprintf(stderr, "output 7\n");
printf("output 8\n");
fflush(stdout);
}
If I run it, I will see these messages:
output 1
output 2
abc
output 3
output 4
output 5
output 6
abc
output 7
output 8
If I use ssh to login and run it, I will see the same results.
However, If I use the binary name as the parameter of ssh and run it, The program will exit when writing data to stderr after calling daemon(0, 1). Suppose the binary name is myapp. I run
ssh localhost myapp
And I will only see these messages:
output 1
output 2
abc
output 3
output 4
output 5
output 6
abc
Does anyone know why? according to the debugging, the program only exits after doing three things:
- Call daemon(0, 1).
- Call system to run another application or bash command.
- Write something to stderr.
Thanks a lot!