Close the other end of the pipe in the grandparent process before exit
to avoid valgrind error.
Also, in the daemon process avoid closing the pipe for second time in
the 0-1024 close() loop to avoid another error.
/* Don't exit before the 'parent' */
waitpid(pid, NULL, 0);
- close(pipefd[1]);
r = read(pipefd[0], message, sizeof (message));
+
+ close(pipefd[0]);
+ close(pipefd[1]);
+
if (r != 1 || message[0] != '\0') {
if (r > 1) {
/* Print the error message from the child */
} else
exit(0);
} else {
- close(pipefd[0]);
-
setsid();
/* Do 2nd fork, as-per recommended practice for launching daemons. */
LOG_FATAL("fork() failed : %s", strerror(errno));
} else if (pid > 0) {
/* In the 'parent' */
+ close(pipefd[0]);
close(pipefd[1]);
exit(0);
} else {