Because when calling the program via ssh a dup on stdoud is made by ssh, so when ssh try to end it cannot because the stdout has also another channel opened.
I try to give you a better answer:
stdout is the channel number 1 if i remember correctly. Making the dup we have also the new channel 3 that is pointing to stdout.
When ssh try to close the channel 2 togheter to channel 0 and 2 (stdinput and stderr) ssh cannot close itself because there is another resource that occupies the channel 3 or better the stdout.
I hope that's enough clear
gaetano
If I run your demo as it is and I get errno = 15 or it could be 16 as described at the end of this answer.
But, if I run it in different way:
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main() {
int out;
int status;
out = dup(STDOUT_FILENO);
// close(out);
FILE *fp = popen("sleep 10\0", "r");
status = pclose(fp);
if (status == -1) {
printf("pclose error: %d", errno);
}
close(out);
}
I don't get any more the error.
To check the error under bash I use: echo $?
That means you have to realease all allocated resources before to exit the c program.
I hope that's all.
Best reagards
gaetano
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */