Is that possible with C to wait until the popen subprocess is finished?
I have a script launched by a C daemon which will connect to a wifi network when an external signal is triggered.
Here is my code for now :
********************* Wait for external event here ***********************
if((fp = popen(CONNECTION_SCRIPT, "r")) == NULL)
{
SYSLOG_ERR("[Error] Impossible to open pipe to execute %s\n", CONNECTION_SCRIPT);
exit(EXIT_FAILURE);
}
while(fgets(scriptBuffer, SCRIPT_BUF_SIZE, fp) != NULL)
{
SYSLOG("[%s] %s", CONNECTION_SCRIPT, scriptBuffer);
}
if(pclose(fp))
{
SYSLOG_ERR("[Error] Error with %s execution \n", CONNECTION_SCRIPT);
exit(EXIT_FAILURE);
}
However, I'm not waiting for the subprocess to finish so when I'm closing the pipe, the ressource is busy because the script is stil writting in. Is there a way to wait for the end of the connection script before reading in the pipe and closing it?
I know I could do it by forking the process and waiting for the child to terminate but it's quite a lot of modifications just to synchronize threads