// pipe
int pipefd[2];
if (d != NULL) {
- if (0 != pipe(pipefd))
+ if (0 != pipe(pipefd)) {
throw runtime::Exception("pipe() failed:" + std::to_string(errno));
+ }
}
pid_t pid = ::fork();
- if (pid == -1)
+ if (pid == -1) {
throw runtime::Exception("fork() failed: " + std::to_string(errno));
+ }
if (pid == 0) {
if (d != NULL) {
::close(pipefd[1]);
int fd = TEMP_FAILURE_RETRY(dup2(pipefd[0], STDIN_FILENO));
- if (fd == -1)
+ if (fd == -1) {
exit(EXIT_FAILURE);
+ }
::close(pipefd[0]);
}
// write the pipefd[1]
ssize_t ret = TEMP_FAILURE_RETRY(write(pipefd[1], d->data(), d->size()));
- if (ret < 0 || (size_t)ret != d->size())
+ if (ret < 0 || (size_t)ret != d->size()) {
throw runtime::Exception("write() failed");
+ }
::close(pipefd[1]);
}
throw runtime::Exception("waitpid() failed: " + std::to_string(errno));
}
}
+ if (!WIFEXITED(status)) {
+ throw runtime::Exception(std::string(argv[0]) + " terminated abnormally");
+ }
+ int ret = WEXITSTATUS(status);
+ if (ret != 0) {
+ throw runtime::Exception(std::string(argv[0]) + " failed with: " + std::to_string(ret));
+ }
}
} // anonymous namespace