summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2015-06-09 20:48:18 +0200
committerJoel Klinghed <the_jk@yahoo.com>2015-06-09 20:48:18 +0200
commit8d087b24cd40f609d030cfd0f2bebe8bb976086a (patch)
treef500cca6f26b5a5616f7fb3cb529d1e9d79b3169
parenta0e21abdfd1e471068fcf0422a0fab39e54e09c2 (diff)
Don't crash sender when closing because the same fd is closed twice
-rw-r--r--src/sender.cc17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/sender.cc b/src/sender.cc
index 75be2d7..2f60a36 100644
--- a/src/sender.cc
+++ b/src/sender.cc
@@ -217,7 +217,7 @@ void queue_message(const std::string& channel, const std::string& message) {
g_info.requests.emplace_back(g_info.multi, g_info.url, obj->str());
}
-int run(const std::string& listener, int fd) {
+int run(const std::string& listener, int* fd) {
openlog("sender", LOG_PID, LOG_DAEMON);
if (curl_global_init(CURL_GLOBAL_ALL)) {
@@ -314,9 +314,10 @@ int run(const std::string& listener, int fd) {
signal(SIGTERM, quit);
while (true) {
- if (write(fd, "", 1) != -1 || errno != EINTR) break;
+ if (write(*fd, "", 1) != -1 || errno != EINTR) break;
}
- close(fd);
+ close(*fd);
+ *fd = -1;
while (!g_quit) {
curl_multi_perform(g_info.multi, &still_running);
@@ -458,11 +459,13 @@ int main() {
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
- int ret = run(listener, fd[1]);
- while (true) {
- if (write(fd[1], "", 1) != -1 || errno != EINTR) break;
+ int ret = run(listener, fd + 1);
+ if (fd[1] != -1) {
+ while (true) {
+ if (write(fd[1], "", 1) != -1 || errno != EINTR) break;
+ }
+ close(fd[1]);
}
- close(fd[1]);
_exit(ret);
} else {
close(fd[1]);