I have the following program
int external_apply(char *type)
{
int pfds[2];
if (pipe(pfds) < 0)
return -1;
if ((pid = fork()) == -1)
goto error;
if (pid == 0) {
/* child */
const char *argv[8];
int i = 0;
argv[i++] = "/bin/sh";
argv[i++] = "script_file.sh";
argv[i++] = "apply";
close(pfds[0]);
dup2(pfds[1], 1);
close(pfds[1]);
execvp(argv[0], (char **) argv);
exit(ESRCH);
} else if (pid < 0)
goto error;
/* parent */
close(pfds[1]);
int status;
while (wait(&status) != pid) {
printf("waiting for child to exit");
}
return 0;
error:
close(pfds[0]);
return -1;
}
The fork call my script file. The script file contains command that cause a pipe close (sometimes). If the pipe is closed by the scipt the wait will cause a crash of the program.
How to avoid the crash of the program when the pipe is closed by the script?